[BACK]Return to fsmagic.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / file

Annotation of src/usr.bin/file/fsmagic.c, Revision 1.2

1.2     ! deraadt     1: /*     $OpenBSD: fsmagic.c,v 1.1.1.1 1995/10/18 08:45:09 deraadt Exp $ */
1.1       deraadt     2: /*
                      3:  * fsmagic - magic based on filesystem info - directory, special files, etc.
                      4:  *
                      5:  * Copyright (c) Ian F. Darwin, 1987.
                      6:  * Written by Ian F. Darwin.
                      7:  *
                      8:  * This software is not subject to any license of the American Telephone
                      9:  * and Telegraph Company or of the Regents of the University of California.
                     10:  *
                     11:  * Permission is granted to anyone to use this software for any purpose on
                     12:  * any computer system, and to alter it and redistribute it freely, subject
                     13:  * to the following restrictions:
                     14:  *
                     15:  * 1. The author is not responsible for the consequences of use of this
                     16:  *    software, no matter how awful, even if they arise from flaws in it.
                     17:  *
                     18:  * 2. The origin of this software must not be misrepresented, either by
                     19:  *    explicit claim or by omission.  Since few users ever read sources,
                     20:  *    credits must appear in the documentation.
                     21:  *
                     22:  * 3. Altered versions must be plainly marked as such, and must not be
                     23:  *    misrepresented as being the original software.  Since few users
                     24:  *    ever read sources, credits must appear in the documentation.
                     25:  *
                     26:  * 4. This notice may not be removed or altered.
                     27:  */
                     28:
                     29: #include <stdio.h>
                     30: #include <string.h>
                     31: #include <sys/types.h>
                     32: #include <sys/stat.h>
                     33: #include <unistd.h>
                     34: #include <stdlib.h>
                     35: #ifndef        major                   /* if `major' not defined in types.h, */
                     36: #include <sys/sysmacros.h>     /* try this one. */
                     37: #endif
                     38: #ifndef        major   /* still not defined? give up, manual intervention needed */
                     39:                /* If cc tries to compile this, read and act on it. */
                     40:                /* On most systems cpp will discard it automatically */
                     41:                Congratulations, you have found a portability bug.
                     42:                Please grep /usr/include/sys and edit the above #include
                     43:                to point at the file that defines the "major" macro.
                     44: #endif /*major*/
                     45:
                     46: #include "file.h"
                     47:
                     48: #ifndef        lint
1.2     ! deraadt    49: static char *moduleid = "$OpenBSD$";
1.1       deraadt    50: #endif /* lint */
                     51:
                     52: int
                     53: fsmagic(fn, sb)
                     54: const char *fn;
                     55: struct stat *sb;
                     56: {
                     57:        int ret = 0;
                     58:
                     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:
                     70:        if (ret) {
                     71:                ckfprintf(stdout,
                     72:                        /* Yes, I do mean stdout. */
                     73:                        /* No \n, caller will provide. */
                     74:                        "can't stat `%s' (%s).", fn, strerror(errno));
                     75:                return 1;
                     76:        }
                     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:
                     82:        switch (sb->st_mode & S_IFMT) {
                     83:        case S_IFDIR:
                     84:                ckfputs("directory", stdout);
                     85:                return 1;
                     86:        case S_IFCHR:
                     87:                (void) printf("character special (%d/%d)",
                     88:                        major(sb->st_rdev), minor(sb->st_rdev));
                     89:                return 1;
                     90:        case S_IFBLK:
                     91:                (void) printf("block special (%d/%d)",
                     92:                        major(sb->st_rdev), minor(sb->st_rdev));
                     93:                return 1;
                     94:        /* TODO add code to handle V7 MUX and Blit MUX files */
                     95: #ifdef S_IFIFO
                     96:        case S_IFIFO:
                     97:                ckfputs("fifo (named pipe)", stdout);
                     98:                return 1;
                     99: #endif
                    100: #ifdef S_IFLNK
                    101:        case S_IFLNK:
                    102:                {
                    103:                        char buf[BUFSIZ+4];
                    104:                        register int nch;
                    105:                        struct stat tstatbuf;
                    106:
                    107:                        if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
                    108:                                ckfprintf(stdout, "unreadable symlink (%s).",
                    109:                                      strerror(errno));
                    110:                                return 1;
                    111:                        }
                    112:                        buf[nch] = '\0';        /* readlink(2) forgets this */
                    113:
                    114:                        /* If broken symlink, say so and quit early. */
                    115:                        if (*buf == '/') {
                    116:                            if (stat(buf, &tstatbuf) < 0) {
                    117:                                ckfprintf(stdout,
                    118:                                        "broken symbolic link to %s", buf);
                    119:                                return 1;
                    120:                            }
                    121:                        }
                    122:                        else {
                    123:                            char *tmp;
                    124:                            char buf2[BUFSIZ+BUFSIZ+4];
                    125:
                    126:                            if ((tmp = strrchr(fn,  '/')) == NULL) {
                    127:                                tmp = buf; /* in current directory anyway */
                    128:                            }
                    129:                            else {
                    130:                                strcpy (buf2, fn);  /* take directory part */
                    131:                                buf2[tmp-fn+1] = '\0';
                    132:                                strcat (buf2, buf); /* plus (relative) symlink */
                    133:                                tmp = buf2;
                    134:                            }
                    135:                            if (stat(tmp, &tstatbuf) < 0) {
                    136:                                ckfprintf(stdout,
                    137:                                        "broken symbolic link to %s", buf);
                    138:                                return 1;
                    139:                            }
                    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:        /*
                    168:         * regular file, check next possibility
                    169:         */
                    170:        if (sb->st_size == 0) {
                    171:                ckfputs("empty", stdout);
                    172:                return 1;
                    173:        }
                    174:        return 0;
                    175: }
                    176: