fsmagic.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * fsmagic - magic based on filesystem info - directory, special files, etc.
  3. *
  4. * Copyright (c) Ian F. Darwin, 1987.
  5. * Written by Ian F. Darwin.
  6. *
  7. * This software is not subject to any license of the American Telephone
  8. * and Telegraph Company or of the Regents of the University of California.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose on
  11. * any computer system, and to alter it and redistribute it freely, subject
  12. * to the following restrictions:
  13. *
  14. * 1. The author is not responsible for the consequences of use of this
  15. * software, no matter how awful, even if they arise from flaws in it.
  16. *
  17. * 2. The origin of this software must not be misrepresented, either by
  18. * explicit claim or by omission. Since few users ever read sources,
  19. * credits must appear in the documentation.
  20. *
  21. * 3. Altered versions must be plainly marked as such, and must not be
  22. * misrepresented as being the original software. Since few users
  23. * ever read sources, credits must appear in the documentation.
  24. *
  25. * 4. This notice may not be removed or altered.
  26. */
  27. #include "file.h"
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #include <stdlib.h>
  36. /* Since major is a function on SVR4, we can't use `ifndef major'. */
  37. #ifdef MAJOR_IN_MKDEV
  38. # include <sys/mkdev.h>
  39. # define HAVE_MAJOR
  40. #endif
  41. #ifdef MAJOR_IN_SYSMACROS
  42. # include <sys/sysmacros.h>
  43. # define HAVE_MAJOR
  44. #endif
  45. #ifdef major /* Might be defined in sys/types.h. */
  46. # define HAVE_MAJOR
  47. #endif
  48. #ifndef HAVE_MAJOR
  49. # define major(dev) (((dev) >> 8) & 0xff)
  50. # define minor(dev) ((dev) & 0xff)
  51. #endif
  52. #undef HAVE_MAJOR
  53. #ifndef lint
  54. FILE_RCSID("@(#)$Id: fsmagic.c,v 1.33 2000/08/05 17:36:48 christos Exp $")
  55. #endif /* lint */
  56. int
  57. fsmagic(fn, sb)
  58. const char *fn;
  59. struct stat *sb;
  60. {
  61. int ret = 0;
  62. /*
  63. * Fstat is cheaper but fails for files you don't have read perms on.
  64. * On 4.2BSD and similar systems, use lstat() to identify symlinks.
  65. */
  66. #ifdef S_IFLNK
  67. if (!lflag)
  68. ret = lstat(fn, sb);
  69. else
  70. #endif
  71. ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
  72. if (ret) {
  73. ckfprintf(stdout,
  74. /* Yes, I do mean stdout. */
  75. /* No \n, caller will provide. */
  76. "can't stat `%s' (%m).", fn);
  77. return 1;
  78. }
  79. if (iflag) {
  80. if ((sb->st_mode & S_IFMT) != S_IFREG) {
  81. ckfputs("application/x-not-regular-file", stdout);
  82. return 1;
  83. }
  84. }
  85. else {
  86. #ifdef S_ISUID
  87. if (sb->st_mode & S_ISUID) ckfputs("setuid ", stdout);
  88. #endif
  89. #ifdef S_ISGID
  90. if (sb->st_mode & S_ISGID) ckfputs("setgid ", stdout);
  91. #endif
  92. #ifdef S_ISVTX
  93. if (sb->st_mode & S_ISVTX) ckfputs("sticky ", stdout);
  94. #endif
  95. }
  96. switch (sb->st_mode & S_IFMT) {
  97. case S_IFDIR:
  98. ckfputs("directory", stdout);
  99. return 1;
  100. #ifdef S_IFCHR
  101. case S_IFCHR:
  102. /*
  103. * If -s has been specified, treat character special files
  104. * like ordinary files. Otherwise, just report that they
  105. * are block special files and go on to the next file.
  106. */
  107. if (sflag)
  108. break;
  109. #ifdef HAVE_ST_RDEV
  110. # ifdef dv_unit
  111. (void) printf("character special (%d/%d/%d)",
  112. major(sb->st_rdev),
  113. dv_unit(sb->st_rdev),
  114. dv_subunit(sb->st_rdev));
  115. # else
  116. (void) printf("character special (%ld/%ld)",
  117. (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
  118. # endif
  119. #else
  120. (void) printf("character special");
  121. #endif
  122. return 1;
  123. #endif
  124. #ifdef S_IFBLK
  125. case S_IFBLK:
  126. /*
  127. * If -s has been specified, treat block special files
  128. * like ordinary files. Otherwise, just report that they
  129. * are block special files and go on to the next file.
  130. */
  131. if (sflag)
  132. break;
  133. #ifdef HAVE_ST_RDEV
  134. # ifdef dv_unit
  135. (void) printf("block special (%d/%d/%d)",
  136. major(sb->st_rdev),
  137. dv_unit(sb->st_rdev),
  138. dv_subunit(sb->st_rdev));
  139. # else
  140. (void) printf("block special (%ld/%ld)",
  141. (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
  142. # endif
  143. #else
  144. (void) printf("block special");
  145. #endif
  146. return 1;
  147. #endif
  148. /* TODO add code to handle V7 MUX and Blit MUX files */
  149. #ifdef S_IFIFO
  150. case S_IFIFO:
  151. ckfputs("fifo (named pipe)", stdout);
  152. return 1;
  153. #endif
  154. #ifdef S_IFDOOR
  155. case S_IFDOOR:
  156. ckfputs("door", stdout);
  157. return 1;
  158. #endif
  159. #ifdef S_IFLNK
  160. case S_IFLNK:
  161. {
  162. char buf[BUFSIZ+4];
  163. int nch;
  164. struct stat tstatbuf;
  165. if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
  166. ckfprintf(stdout, "unreadable symlink (%m).");
  167. return 1;
  168. }
  169. buf[nch] = '\0'; /* readlink(2) forgets this */
  170. /* If broken symlink, say so and quit early. */
  171. if (*buf == '/') {
  172. if (stat(buf, &tstatbuf) < 0) {
  173. ckfprintf(stdout,
  174. "broken symbolic link to %s", buf);
  175. return 1;
  176. }
  177. }
  178. else {
  179. char *tmp;
  180. char buf2[BUFSIZ+BUFSIZ+4];
  181. if ((tmp = strrchr(fn, '/')) == NULL) {
  182. tmp = buf; /* in current directory anyway */
  183. }
  184. else {
  185. strcpy (buf2, fn); /* take directory part */
  186. buf2[tmp-fn+1] = '\0';
  187. strcat (buf2, buf); /* plus (relative) symlink */
  188. tmp = buf2;
  189. }
  190. if (stat(tmp, &tstatbuf) < 0) {
  191. ckfprintf(stdout,
  192. "broken symbolic link to %s", buf);
  193. return 1;
  194. }
  195. }
  196. /* Otherwise, handle it. */
  197. if (lflag) {
  198. process(buf, strlen(buf));
  199. return 1;
  200. } else { /* just print what it points to */
  201. ckfputs("symbolic link to ", stdout);
  202. ckfputs(buf, stdout);
  203. }
  204. }
  205. return 1;
  206. #endif
  207. #ifdef S_IFSOCK
  208. #ifndef __COHERENT__
  209. case S_IFSOCK:
  210. ckfputs("socket", stdout);
  211. return 1;
  212. #endif
  213. #endif
  214. case S_IFREG:
  215. break;
  216. default:
  217. error("invalid mode 0%o.\n", sb->st_mode);
  218. /*NOTREACHED*/
  219. }
  220. /*
  221. * regular file, check next possibility
  222. *
  223. * If stat() tells us the file has zero length, report here that
  224. * the file is empty, so we can skip all the work of opening and
  225. * reading the file.
  226. * But if the -s option has been given, we skip this optimization,
  227. * since on some systems, stat() reports zero size for raw disk
  228. * partitions. (If the block special device really has zero length,
  229. * the fact that it is empty will be detected and reported correctly
  230. * when we read the file.)
  231. */
  232. if (!sflag && sb->st_size == 0) {
  233. ckfputs(iflag ? "application/x-empty" : "empty", stdout);
  234. return 1;
  235. }
  236. return 0;
  237. }