fsmagic.c 4.5 KB

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