fsmagic.c 4.8 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. #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. FILE_RCSID("@(#)$Id: fsmagic.c,v 1.27 1998/06/27 13:23:39 christos Exp $")
  54. #endif /* lint */
  55. int
  56. fsmagic(fn, sb)
  57. const char *fn;
  58. struct stat *sb;
  59. {
  60. int ret = 0;
  61. /*
  62. * Fstat is cheaper but fails for files you don't have read perms on.
  63. * On 4.2BSD and similar systems, use lstat() to identify symlinks.
  64. */
  65. #ifdef S_IFLNK
  66. if (!lflag)
  67. ret = lstat(fn, sb);
  68. else
  69. #endif
  70. ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
  71. if (ret) {
  72. ckfprintf(stdout,
  73. /* Yes, I do mean stdout. */
  74. /* No \n, caller will provide. */
  75. "can't stat `%s' (%s).", fn, strerror(errno));
  76. return 1;
  77. }
  78. if (sb->st_mode & S_ISUID) ckfputs("setuid ", stdout);
  79. if (sb->st_mode & S_ISGID) ckfputs("setgid ", stdout);
  80. if (sb->st_mode & S_ISVTX) ckfputs("sticky ", stdout);
  81. switch (sb->st_mode & S_IFMT) {
  82. case S_IFDIR:
  83. ckfputs("directory", stdout);
  84. return 1;
  85. case S_IFCHR:
  86. #ifdef HAVE_ST_RDEV
  87. (void) printf("character special (%ld/%ld)",
  88. (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
  89. #else
  90. (void) printf("character special");
  91. #endif
  92. return 1;
  93. case S_IFBLK:
  94. #ifdef HAVE_ST_RDEV
  95. (void) printf("block special (%ld/%ld)",
  96. (long) major(sb->st_rdev), (long) minor(sb->st_rdev));
  97. #else
  98. (void) printf("block special");
  99. #endif
  100. return 1;
  101. /* TODO add code to handle V7 MUX and Blit MUX files */
  102. #ifdef S_IFIFO
  103. case S_IFIFO:
  104. ckfputs("fifo (named pipe)", stdout);
  105. return 1;
  106. #endif
  107. #ifdef S_IFLNK
  108. case S_IFLNK:
  109. {
  110. char buf[BUFSIZ+4];
  111. register int nch;
  112. struct stat tstatbuf;
  113. if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
  114. ckfprintf(stdout, "unreadable symlink (%s).",
  115. strerror(errno));
  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. }