file.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* -*- c -*- */
  2. #include <sys/types.h>
  3. #include <sys/exec.h>
  4. #include <sys/core.h>
  5. #include <sys/stat.h>
  6. #include <a.out.h>
  7. #include <vfont.h>
  8. #include <strings.h>
  9. #include <stdio.h>
  10. #include <sys/file.h>
  11. #include <regex.h>
  12. #ifdef sun
  13. #include <rasterfile.h>
  14. #endif /* sun */
  15. /*
  16. * FILE.C --- Heuristically determine the type
  17. * of a file. Relies on Magic Numbers
  18. * for binaries, and dirty hacks for
  19. * nroff etc, lisp, pascal, c, fortran,
  20. * and so on. Knows rather more than it
  21. * probably ought to about SUN/3 files.
  22. *
  23. * This is intended to be submitted to
  24. * the GNU project, but is also likely
  25. * to be handy on SUNs.
  26. *
  27. * AUTHOR Ian G. Batten, University of Birmingham.
  28. * DATE 1-Nov-1986
  29. *
  30. * NOTICE -----------------------------------------------------
  31. *
  32. * No proprietry software's source code was consulted during
  33. * the writing of this program. SUN include files are
  34. * referenced, however. This software may be freely duplicated
  35. * provided that (i) no commercial advantage is taken of it and
  36. * (ii) this notice is attached to all copies so made.
  37. *
  38. * ------------------------------------------------------------
  39. *
  40. * Batten@MULTICS.BHAM.AC.UK
  41. *
  42. */
  43. #ifndef CONFIG_FILE
  44. #define CONFIG_FILE "/usr/local/filetypes"
  45. #endif CONFIG_FILE
  46. #define BUFFER_SIZE 256
  47. #define NOFILE (FILE *) NULL
  48. #define BLOCK_SIZE 512 /* The number of characters to consider */
  49. #define FIELD_BOUNDARY ':' /* Spliter in the config */
  50. #define BYTEWIDTH 8
  51. static void type_determine ();
  52. static void use_config_file ();
  53. char *getenv ();
  54. struct regex_link
  55. {
  56. struct re_pattern_buffer re_comp_buf;
  57. char regexp[BUFFER_SIZE];
  58. char message[BUFFER_SIZE]; /* Description of what matches this regex */
  59. struct regex_link *next_link; /* And the next... */
  60. };
  61. static struct regex_link *regex_list = (struct regex_link *)NULL;
  62. static struct regex_link *last_link; /* For appending to list */
  63. main (argc, argv)
  64. int argc;
  65. char **argv;
  66. {
  67. int target, /* File descriptor */
  68. file_count,
  69. byte_count,
  70. mode_bits;
  71. char first_block[BLOCK_SIZE + 1]; /* Allow space for nul terminator */
  72. struct stat statbuf;
  73. for (file_count = 1; file_count < argc; file_count++)
  74. {
  75. printf ("%s:\t", argv[file_count]);
  76. fflush (stdout); /* In case we perror, ensure file name */
  77. /* which took the fault is printed now. */
  78. if (lstat (argv[file_count], &statbuf) == -1) /* I want to know about symlinks. */
  79. /* There is no 'flstat' to take an fd and get link */
  80. /* And you can't open a socket. */
  81. {
  82. perror (argv[file_count]);
  83. continue;
  84. }
  85. if ((mode_bits = statbuf.st_mode & S_IFMT) != S_IFREG)
  86. switch (mode_bits)
  87. {
  88. case S_IFDIR:
  89. printf (" directory\n");
  90. continue;
  91. case S_IFCHR:
  92. printf (" character special (%d/%d)\n",
  93. major (statbuf.st_rdev), minor (statbuf.st_rdev));
  94. continue;
  95. case S_IFBLK:
  96. printf (" block special (%d/%d)\n",
  97. major (statbuf.st_rdev), minor (statbuf.st_rdev));
  98. continue;
  99. case S_IFLNK:
  100. printf (" symbolic link\n");
  101. continue;
  102. case S_IFSOCK:
  103. printf (" socket\n");
  104. continue;
  105. default:
  106. printf (" unknown non-file\n");
  107. }
  108. target = open (argv[file_count], O_RDONLY, 0);
  109. if (target == -1)
  110. {
  111. perror (" open");
  112. continue;
  113. }
  114. byte_count = read (target, first_block, BLOCK_SIZE);
  115. if (byte_count == -1)
  116. perror (" read");
  117. else
  118. {
  119. first_block[byte_count] = '\0';
  120. type_determine (first_block, byte_count);
  121. if ((int) statbuf.st_mode & S_ISUID)
  122. printf (" (setuid)");
  123. if ((int) statbuf.st_mode & S_ISGID)
  124. printf (" (setgid)");
  125. if ((int) statbuf.st_mode & S_ISVTX)
  126. printf (" (sticky bit)");
  127. }
  128. putchar ('\n');
  129. close (target);
  130. }
  131. }
  132. #ifdef sun
  133. static void
  134. raster_type (code)
  135. int code;
  136. {
  137. switch (code)
  138. {
  139. case RT_OLD:
  140. printf (" Old raw pixrect image, 68000 byte order");
  141. break;
  142. case RT_STANDARD:
  143. printf (" Standard raw pixrect image, 68000 byte order");
  144. break;
  145. case RT_BYTE_ENCODED:
  146. printf (" Run-length compression of bytes");
  147. break;
  148. case RT_EXPERIMENTAL:
  149. printf (" Type reserved for testing");
  150. break;
  151. }
  152. }
  153. static void
  154. raster_maptype (code)
  155. int code;
  156. {
  157. switch (code)
  158. {
  159. case RMT_RAW:
  160. printf (" Sun RMT_RAW");
  161. break;
  162. case RMT_NONE:
  163. printf (" Standard RMT_NONE");
  164. break;
  165. case RMT_EQUAL_RGB:
  166. printf (" Standard RMT_EQUAL_RGB");
  167. }
  168. printf (" colormap");
  169. }
  170. static void
  171. machine_type (code)
  172. unsigned short code;
  173. {
  174. switch (code)
  175. {
  176. case M_OLDSUN2:
  177. printf (" old sun/2");
  178. break;
  179. case M_68010:
  180. printf (" mc68010");
  181. break;
  182. case M_68020:
  183. printf (" mc68020");
  184. break;
  185. }
  186. }
  187. #endif /* sun */
  188. static void purity_type (code)
  189. unsigned code;
  190. {
  191. switch (code)
  192. {
  193. case OMAGIC:
  194. printf (" impure");
  195. break;
  196. case NMAGIC:
  197. printf (" read-only text");
  198. break;
  199. case ZMAGIC:
  200. printf (" demand paged");
  201. break;
  202. }
  203. }
  204. static void
  205. type_determine (block, length)
  206. int length;
  207. char block[BLOCK_SIZE];
  208. {
  209. /* Test for executable */
  210. {
  211. struct exec *exec_header;
  212. exec_header = (struct exec *)block;
  213. if (!(exec_header -> a_magic != OMAGIC && exec_header -> a_magic != NMAGIC
  214. && exec_header -> a_magic != ZMAGIC))
  215. {
  216. #ifdef sun
  217. machine_type (exec_header -> a_machtype);
  218. #endif /* sun */
  219. purity_type (exec_header -> a_magic);
  220. if (exec_header -> a_syms == 0)
  221. printf (" stripped");
  222. printf (" executable");
  223. return;
  224. }
  225. }
  226. /* Test for coredump */
  227. {
  228. struct core *core_header;
  229. core_header = (struct core *)block;
  230. if (core_header -> c_magic == CORE_MAGIC)
  231. {
  232. printf (" core dump from %s", (char *)(core_header -> c_cmdname));
  233. return;
  234. }
  235. }
  236. #ifdef sun
  237. /* Test for rasterfile */
  238. {
  239. struct rasterfile *raster_header;
  240. raster_header = (struct rasterfile *)block;
  241. if (raster_header -> ras_magic == RAS_MAGIC)
  242. {
  243. printf (" raster image %d by %d by %d", raster_header -> ras_width,
  244. raster_header -> ras_height, raster_header -> ras_depth);
  245. raster_type (raster_header -> ras_type);
  246. raster_maptype (raster_header -> ras_maptype);
  247. return;
  248. }
  249. }
  250. #endif /* sun */
  251. /* Test for vfont */
  252. {
  253. struct header /* CAREFUL! Dodgy nameing in vfont.h */ *vfont_header;
  254. vfont_header = (struct header *) block;
  255. if (vfont_header -> magic == VFONT_MAGIC)
  256. {
  257. printf (" vfont file, size %d by %d", vfont_header -> maxx, vfont_header -> maxy);
  258. return;
  259. }
  260. }
  261. use_config_file (block, length);
  262. }
  263. static void
  264. read_config_file (name, barf)
  265. char *name;
  266. int barf; /* 1 = fail on no open */
  267. {
  268. char *buffer;
  269. FILE *types;
  270. struct regex_link *this_link;
  271. char *regexp, *error;
  272. buffer = (char *) malloc (BUFFER_SIZE);
  273. types = fopen (name, "r");
  274. if (types == NOFILE)
  275. if (barf)
  276. {
  277. perror (name);
  278. exit (1);
  279. }
  280. else
  281. return;
  282. while ((buffer = fgets (buffer, BUFFER_SIZE, types)) != NULL)
  283. {
  284. if (buffer[0] == '#') continue; /* Comment */
  285. if (buffer[0] == '\0') continue; /* Blank line */
  286. if (!(regexp = index (buffer, FIELD_BOUNDARY)))
  287. {
  288. fprintf (stderr, "%s: Bad config line (only one field): \n%s\n", name, buffer);
  289. exit (1);
  290. }
  291. *regexp++ = '\0'; /* Partition the buffer */
  292. regexp[strlen(regexp) - 1] = '\0'; /* Stamp on the CR */
  293. error = (char *) NULL;
  294. this_link = (struct regex_link *) malloc (sizeof (struct regex_link));
  295. if (!(this_link->re_comp_buf.buffer = (char *) malloc (200)))
  296. error = "Memory exhausted";
  297. else
  298. {
  299. this_link->re_comp_buf.allocated = 200;
  300. if (!(this_link->re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  301. error = "Memory exhausted";
  302. else
  303. error = re_compile_pattern (regexp, strlen (regexp), &this_link->re_comp_buf);
  304. }
  305. if (error != (char *) NULL)
  306. {
  307. fprintf (stderr, "%s: Bad config line (%s): \n%s:%s\n", name, error, buffer, regexp);
  308. exit (1);
  309. }
  310. strcpy (this_link->message, buffer);
  311. strcpy (this_link->regexp, regexp);
  312. if (regex_list == NULL)
  313. regex_list = last_link = this_link;
  314. else
  315. {
  316. last_link->next_link = this_link;
  317. last_link = this_link;
  318. }
  319. this_link->next_link = NULL;
  320. }
  321. fclose (types);
  322. free (buffer);
  323. }
  324. /* Examine the contents of BLOCK (whose length is BLOCK_LENGTH chars)
  325. by searching for the regexps specified in the config file.
  326. Try regexps one by one in the order specified in that file.
  327. When a regexp is found, print corresponding message and return.
  328. The config file data is read on the first call to this function. */
  329. static void
  330. use_config_file (block, block_length)
  331. char *block;
  332. int block_length;
  333. {
  334. struct regex_link *this_link;
  335. /* Read config files if not already done. */
  336. if (regex_list == NULL)
  337. {
  338. char *homedir;
  339. if ((homedir = getenv ("HOME")) != (char *) NULL)
  340. {
  341. if (homedir[1] == '\0') /* Must be root */
  342. read_config_file ("/.filetypes", 0);
  343. else
  344. {
  345. char *peruser = (char *) malloc (strlen (homedir)+20);
  346. strcpy (peruser, homedir);
  347. strcat (peruser, "/.filetypes");
  348. read_config_file (peruser, 0);
  349. free (peruser);
  350. }
  351. }
  352. read_config_file (CONFIG_FILE, 1);
  353. }
  354. /* Now search for the regexps. */
  355. for (this_link = regex_list;
  356. this_link != NULL;
  357. this_link = this_link->next_link)
  358. if (0 <= re_search (&this_link->re_comp_buf, block, block_length, 0, block_length, 0))
  359. {
  360. printf (" %s", this_link->message);
  361. return;
  362. }
  363. }