fsmagic.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <stdio.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include "file.h"
  34. #ifndef major
  35. # if defined(__SVR4) || defined(_SVR4_SOURCE)
  36. # include <sys/mkdev.h>
  37. # endif
  38. #endif
  39. #ifndef major /* if `major' not defined in types.h, */
  40. #include <sys/sysmacros.h> /* try this one. */
  41. #endif
  42. #ifndef major /* still not defined? give up, manual intervention needed */
  43. /* If cc tries to compile this, read and act on it. */
  44. /* On most systems cpp will discard it automatically */
  45. Congratulations, you have found a portability bug.
  46. Please grep /usr/include/sys and edit the above #include
  47. to point at the file that defines the "major" macro.
  48. #endif /*major*/
  49. #if 0
  50. static char *moduleid =
  51. "@(#)$Id: fsmagic.c,v 1.26 1998/02/15 23:18:53 christos Exp $";
  52. #endif /* lint */
  53. int
  54. fsmagic(fn, sb)
  55. const char *fn;
  56. struct stat *sb;
  57. {
  58. int ret = 0;
  59. /*
  60. * Fstat is cheaper but fails for files you don't have read perms on.
  61. * On 4.2BSD and similar systems, use lstat() to identify symlinks.
  62. */
  63. #ifdef S_IFLNK
  64. if (!lflag)
  65. ret = lstat(fn, sb);
  66. else
  67. #endif
  68. ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
  69. if (ret) {
  70. ckfprintf(stdout,
  71. /* Yes, I do mean stdout. */
  72. /* No \n, caller will provide. */
  73. "can't stat `%s' (%m).", fn);
  74. return 1;
  75. }
  76. if (sb->st_mode & S_ISUID) ckfputs("setuid ", stdout);
  77. if (sb->st_mode & S_ISGID) ckfputs("setgid ", stdout);
  78. if (sb->st_mode & S_ISVTX) ckfputs("sticky ", stdout);
  79. switch (sb->st_mode & S_IFMT) {
  80. case S_IFDIR:
  81. ckfputs("directory", stdout);
  82. return 1;
  83. case S_IFCHR:
  84. #ifdef HAVE_ST_RDEV
  85. (void) printf("character special (%ld/%ld)",
  86. (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
  87. #else
  88. (void) printf("character special");
  89. #endif
  90. return 1;
  91. case S_IFBLK:
  92. #ifdef HAVE_ST_RDEV
  93. (void) printf("block special (%ld/%ld)",
  94. (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
  95. #else
  96. (void) printf("block special");
  97. #endif
  98. return 1;
  99. /* TODO add code to handle V7 MUX and Blit MUX files */
  100. #ifdef S_IFIFO
  101. case S_IFIFO:
  102. ckfputs("fifo (named pipe)", stdout);
  103. return 1;
  104. #endif
  105. #ifdef S_IFLNK
  106. case S_IFLNK:
  107. {
  108. char buf[BUFSIZ+4];
  109. register int nch;
  110. struct stat tstatbuf;
  111. if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
  112. ckfprintf(stdout, "unreadable symlink (%m).");
  113. return 1;
  114. }
  115. buf[nch] = '\0'; /* readlink(2) forgets this */
  116. /* If broken symlink, say so and quit early. */
  117. if (*buf == '/') {
  118. if (stat(buf, &tstatbuf) < 0) {
  119. ckfprintf(stdout,
  120. "broken symbolic link to %s", buf);
  121. return 1;
  122. }
  123. }
  124. else {
  125. char *tmp;
  126. char buf2[BUFSIZ+BUFSIZ+4];
  127. if ((tmp = strrchr(fn, '/')) == NULL) {
  128. tmp = buf; /* in current directory anyway */
  129. }
  130. else {
  131. strcpy (buf2, fn); /* take directory part */
  132. buf2[tmp-fn+1] = '\0';
  133. strcat (buf2, buf); /* plus (relative) symlink */
  134. tmp = buf2;
  135. }
  136. if (stat(tmp, &tstatbuf) < 0) {
  137. ckfprintf(stdout,
  138. "broken symbolic link to %s", buf);
  139. return 1;
  140. }
  141. }
  142. /* Otherwise, handle it. */
  143. if (lflag) {
  144. process(buf, strlen(buf));
  145. return 1;
  146. } else { /* just print what it points to */
  147. ckfputs("symbolic link to ", stdout);
  148. ckfputs(buf, stdout);
  149. }
  150. }
  151. return 1;
  152. #endif
  153. #ifdef S_IFSOCK
  154. #ifndef __COHERENT__
  155. case S_IFSOCK:
  156. ckfputs("socket", stdout);
  157. return 1;
  158. #endif
  159. #endif
  160. case S_IFREG:
  161. break;
  162. default:
  163. error("invalid mode 0%o.\n", sb->st_mode);
  164. /*NOTREACHED*/
  165. }
  166. /*
  167. * regular file, check next possibility
  168. */
  169. if (sb->st_size == 0) {
  170. ckfputs("empty", stdout);
  171. return 1;
  172. }
  173. return 0;
  174. }