ascmagic.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * ASCII magic -- file types that we know based on keywords
  3. * that can appear anywhere in the file.
  4. *
  5. * Copyright (c) Ian F. Darwin, 1987.
  6. * Written by Ian F. Darwin.
  7. *
  8. * This software is not subject to any license of the American Telephone
  9. * and Telegraph Company or of the Regents of the University of California.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose on
  12. * any computer system, and to alter it and redistribute it freely, subject
  13. * to the following restrictions:
  14. *
  15. * 1. The author is not responsible for the consequences of use of this
  16. * software, no matter how awful, even if they arise from flaws in it.
  17. *
  18. * 2. The origin of this software must not be misrepresented, either by
  19. * explicit claim or by omission. Since few users ever read sources,
  20. * credits must appear in the documentation.
  21. *
  22. * 3. Altered versions must be plainly marked as such, and must not be
  23. * misrepresented as being the original software. Since few users
  24. * ever read sources, credits must appear in the documentation.
  25. *
  26. * 4. This notice may not be removed or altered.
  27. */
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <memory.h>
  34. #include <ctype.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include "file.h"
  38. #include "names.h"
  39. #ifndef lint
  40. FILE_RCSID("@(#)$Id: ascmagic.c,v 1.23 1998/06/27 13:23:39 christos Exp $");
  41. #endif /* lint */
  42. /* an optimisation over plain strcmp() */
  43. #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
  44. int
  45. ascmagic(buf, nbytes)
  46. unsigned char *buf;
  47. int nbytes; /* size actually read */
  48. {
  49. int i, has_escapes = 0;
  50. unsigned char *s;
  51. char nbuf[HOWMANY+1]; /* one extra for terminating '\0' */
  52. char *token;
  53. register struct names *p;
  54. /*
  55. * Do the tar test first, because if the first file in the tar
  56. * archive starts with a dot, we can confuse it with an nroff file.
  57. */
  58. switch (is_tar(buf, nbytes)) {
  59. case 1:
  60. ckfputs("tar archive", stdout);
  61. return 1;
  62. case 2:
  63. ckfputs("POSIX tar archive", stdout);
  64. return 1;
  65. }
  66. /*
  67. * for troff, look for . + letter + letter or .\";
  68. * this must be done to disambiguate tar archives' ./file
  69. * and other trash from real troff input.
  70. */
  71. if (*buf == '.') {
  72. unsigned char *tp = buf + 1;
  73. while (isascii(*tp) && isspace(*tp))
  74. ++tp; /* skip leading whitespace */
  75. if ((isascii(*tp) && (isalnum(*tp) || *tp=='\\') &&
  76. isascii(tp[1]) && (isalnum(tp[1]) || tp[1] == '"'))) {
  77. ckfputs("troff or preprocessor input text", stdout);
  78. return 1;
  79. }
  80. }
  81. if ((*buf == 'c' || *buf == 'C') &&
  82. isascii(buf[1]) && isspace(buf[1])) {
  83. ckfputs("fortran program text", stdout);
  84. return 1;
  85. }
  86. /* Make sure we are dealing with ascii text before looking for tokens */
  87. for (i = 0; i < nbytes; i++) {
  88. if (!isascii(buf[i]))
  89. return 0; /* not all ASCII */
  90. }
  91. /* look for tokens from names.h - this is expensive! */
  92. /* make a copy of the buffer here because strtok() will destroy it */
  93. s = (unsigned char*) memcpy(nbuf, buf, nbytes);
  94. s[nbytes] = '\0';
  95. has_escapes = (memchr(s, '\033', nbytes) != NULL);
  96. while ((token = strtok((char *) s, " \t\n\r\f")) != NULL) {
  97. s = NULL; /* make strtok() keep on tokin' */
  98. for (p = names; p < names + NNAMES; p++) {
  99. if (STREQ(p->name, token)) {
  100. ckfputs(types[p->type], stdout);
  101. if (has_escapes)
  102. ckfputs(" (with escape sequences)",
  103. stdout);
  104. return 1;
  105. }
  106. }
  107. }
  108. /* all else fails, but it is ASCII... */
  109. ckfputs("ASCII text", stdout);
  110. if (has_escapes) {
  111. ckfputs(" (with escape sequences)", stdout);
  112. }
  113. return 1;
  114. }