fsmagic.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <unistd.h>
  35. #include <stdlib.h>
  36. #include "file.h"
  37. #ifndef major
  38. # if defined(__SVR4) || defined(_SVR4_SOURCE)
  39. # include <sys/mkdev.h>
  40. # endif
  41. #endif
  42. #ifndef major /* if `major' not defined in types.h, */
  43. #include <sys/sysmacros.h> /* try this one. */
  44. #endif
  45. #ifndef major /* still not defined? give up, manual intervention needed */
  46. /* If cc tries to compile this, read and act on it. */
  47. /* On most systems cpp will discard it automatically */
  48. Congratulations, you have found a portability bug.
  49. Please grep /usr/include/sys and edit the above #include
  50. to point at the file that defines the "major" macro.
  51. #endif /*major*/
  52. #include "file.h"
  53. #ifndef lint
  54. FILE_RCSID("@(#)$Id: fsmagic.c,v 1.27 1998/06/27 13:23:39 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 (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 (%m).");
  116. return 1;
  117. }
  118. buf[nch] = '\0'; /* readlink(2) forgets this */
  119. /* If broken symlink, say so and quit early. */
  120. if (*buf == '/') {
  121. if (stat(buf, &tstatbuf) < 0) {
  122. ckfprintf(stdout,
  123. "broken symbolic link to %s", buf);
  124. return 1;
  125. }
  126. }
  127. else {
  128. char *tmp;
  129. char buf2[BUFSIZ+BUFSIZ+4];
  130. if ((tmp = strrchr(fn, '/')) == NULL) {
  131. tmp = buf; /* in current directory anyway */
  132. }
  133. else {
  134. strcpy (buf2, fn); /* take directory part */
  135. buf2[tmp-fn+1] = '\0';
  136. strcat (buf2, buf); /* plus (relative) symlink */
  137. tmp = buf2;
  138. }
  139. if (stat(tmp, &tstatbuf) < 0) {
  140. ckfprintf(stdout,
  141. "broken symbolic link to %s", buf);
  142. return 1;
  143. }
  144. }
  145. /* Otherwise, handle it. */
  146. if (lflag) {
  147. process(buf, strlen(buf));
  148. return 1;
  149. } else { /* just print what it points to */
  150. ckfputs("symbolic link to ", stdout);
  151. ckfputs(buf, stdout);
  152. }
  153. }
  154. return 1;
  155. #endif
  156. #ifdef S_IFSOCK
  157. #ifndef __COHERENT__
  158. case S_IFSOCK:
  159. ckfputs("socket", stdout);
  160. return 1;
  161. #endif
  162. #endif
  163. case S_IFREG:
  164. break;
  165. default:
  166. error("invalid mode 0%o.\n", sb->st_mode);
  167. /*NOTREACHED*/
  168. }
  169. /*
  170. * regular file, check next possibility
  171. */
  172. if (sb->st_size == 0) {
  173. ckfputs("empty", stdout);
  174. return 1;
  175. }
  176. return 0;
  177. }