fsmagic.c 4.8 KB

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