Annotation of 43BSDReno/usr.sbin/mtree/create.c, revision 1.1.1.1

1.1       root        1: /*-
                      2:  * Copyright (c) 1989 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted provided
                      6:  * that: (1) source distributions retain this entire copyright notice and
                      7:  * comment, and (2) distributions including binaries display the following
                      8:  * acknowledgement:  ``This product includes software developed by the
                      9:  * University of California, Berkeley and its contributors'' in the
                     10:  * documentation or other materials provided with the distribution and in
                     11:  * all advertising materials mentioning features or use of this software.
                     12:  * Neither the name of the University nor the names of its contributors may
                     13:  * be used to endorse or promote products derived from this software without
                     14:  * specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)create.c   5.14 (Berkeley) 6/25/90";
                     22: #endif /* not lint */
                     23: 
                     24: #include <sys/param.h>
                     25: #include <sys/stat.h>
                     26: #include <time.h>
                     27: #include <fts.h>
                     28: #include <dirent.h>
                     29: #include <errno.h>
                     30: #include <stdio.h>
                     31: #include "mtree.h"
                     32: 
                     33: #define        LABEL \
                     34:        if (label++) \
                     35:                (void)putchar(' '); \
                     36: 
                     37: int ftsoptions = FTS_PHYSICAL;
                     38: 
                     39: cwalk()
                     40: {
                     41:        extern int dflag;
                     42:        register FTS *t;
                     43:        register FTSENT *p;
                     44:        register int cnt, label, notset;
                     45:        time_t clock;
                     46:        uid_t uid;
                     47:        gid_t gid;
                     48:        mode_t mode;
                     49:        int tabs, dsort();
                     50:        char *argv[2];
                     51:        char curp[MAXPATHLEN], *inotype(), *getlogin(), *rlink();
                     52: 
                     53:        if (!getwd(curp)) {
                     54:                (void)fprintf(stderr, "mtree: %s\n", curp);
                     55:                exit(1);
                     56:        }
                     57:        (void)time(&clock);
                     58:        (void)printf("#\t  fs: %s\n#\t  by: %s\n#\tdate: %s\n",
                     59:            curp, getlogin(), ctime(&clock));
                     60: 
                     61:        argv[0] = ".";
                     62:        argv[1] = (char *)NULL;
                     63:        if (!(t = ftsopen(argv, ftsoptions, dsort))) {
                     64:                (void)fprintf(stderr,
                     65:                    "mtree: ftsopen: %s.\n", strerror(errno));
                     66:                exit(1);
                     67:        }
                     68:        while (p = ftsread(t)) {
                     69:                switch(p->fts_info) {
                     70:                case FTS_D:
                     71:                        if (dflag)
                     72:                                notset = 1;
                     73:                        else
                     74:                                notset =
                     75:                                    statdir(t, p, &uid, &gid, &mode, &tabs);
                     76:                        if (!strcmp(p->fts_name, "."))
                     77:                                continue;
                     78:                        break;
                     79:                case FTS_DC:
                     80:                        (void)fprintf(stderr,
                     81:                            "mtree: directory cycle: %s.\n", p->fts_path);
                     82:                        continue;
                     83:                case FTS_DNR:
                     84:                        (void)fprintf(stderr,
                     85:                            "mtree: %s: unable to read.\n", p->fts_path);
                     86:                        continue;
                     87:                case FTS_DNX:
                     88:                        (void)fprintf(stderr,
                     89:                            "mtree: %s: unable to search.\n", p->fts_path);
                     90:                        continue;
                     91:                case FTS_DP:
                     92:                        if (p->fts_level <= 0)
                     93:                                continue;
                     94:                        for (cnt = p->fts_level - 1; cnt-- > 0; )
                     95:                                (void)putchar('\t');
                     96:                        (void)printf("..\n");
                     97:                        continue;
                     98:                case FTS_ERR:
                     99:                        (void)fprintf(stderr, "mtree: %s: %s.\n",
                    100:                            p->fts_path, strerror(errno));
                    101:                        continue;
                    102:                case FTS_NS:
                    103:                        (void)fprintf(stderr,
                    104:                            "mtree: can't stat: %s.\n", p->fts_path);
                    105:                        continue;
                    106:                default:
                    107:                        if (dflag)
                    108:                                continue;
                    109:                }
                    110: 
                    111:                for (cnt = p->fts_level - 1; cnt-- > 0; )
                    112:                        (void)putchar('\t');
                    113:                (void)printf("%s", p->fts_name);
                    114:                if (p->fts_info == FTS_D)
                    115:                        (void)putchar('\t');
                    116:                else {
                    117:                        if (tabs > 1 && p->fts_namelen < 8)
                    118:                                (void)putchar('\t');
                    119:                        (void)putchar('\t');
                    120:                }
                    121: 
                    122:                label = 0;
                    123:                if (!S_ISREG(p->fts_statb.st_mode) || notset) {
                    124:                        LABEL;
                    125:                        (void)printf("type=%s", inotype(p->fts_statb.st_mode));
                    126:                }
                    127:                if (p->fts_statb.st_uid != uid || notset) {
                    128:                        LABEL;
                    129:                        (void)printf("owner=%u", p->fts_statb.st_uid);
                    130:                }
                    131:                if (p->fts_statb.st_gid != gid || notset) {
                    132:                        LABEL;
                    133:                        (void)printf("group=%u", p->fts_statb.st_gid);
                    134:                }
                    135:                if ((p->fts_statb.st_mode & MBITS) != mode || notset) {
                    136:                        LABEL;
                    137:                        (void)printf("mode=%#o", p->fts_statb.st_mode & MBITS);
                    138:                }
                    139:                if (p->fts_statb.st_nlink != 1 || notset) {
                    140:                        LABEL;
                    141:                        (void)printf("nlink=%u", p->fts_statb.st_nlink);
                    142:                }
                    143:                LABEL;
                    144:                (void)printf("size=%ld", p->fts_statb.st_size);
                    145:                LABEL;
                    146:                (void)printf("time=%ld", p->fts_statb.st_mtime);
                    147: 
                    148:                if (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE) {
                    149:                        LABEL;
                    150:                        (void)printf("link=%s", rlink(p->fts_accpath));
                    151:                }
                    152:                (void)putchar('\n');
                    153:        }
                    154:        (void)ftsclose(t);
                    155: }
                    156: 
                    157: #define        MAXGID  5000
                    158: #define        MAXUID  5000
                    159: #define        MAXMODE MBITS + 1
                    160: 
                    161: statdir(t, parent, puid, pgid, pmode, tabs)
                    162:        FTS *t;
                    163:        FTSENT *parent;
                    164:        uid_t *puid;
                    165:        gid_t *pgid;
                    166:        mode_t *pmode;
                    167:        int *tabs;
                    168: {
                    169:        register FTSENT *p;
                    170:        register gid_t gid;
                    171:        register uid_t uid;
                    172:        register mode_t mode;
                    173:        gid_t savegid;
                    174:        uid_t saveuid;
                    175:        mode_t savemode;
                    176:        u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE];
                    177: 
                    178:        if (!(p = ftschildren(t))) {
                    179:                if (errno) {
                    180:                        (void)fprintf(stderr, "mtree: %s: %s.\n",
                    181:                            RP(parent), strerror(errno));
                    182:                        exit(1);
                    183:                }
                    184:                return(1);
                    185:        }
                    186: 
                    187:        bzero(g, sizeof(g));
                    188:        bzero(u, sizeof(u));
                    189:        bzero(m, sizeof(m));
                    190: 
                    191:        *tabs = 1;
                    192:        maxuid = maxgid = maxmode = 0;
                    193:        for (; p; p = p->fts_link) {
                    194:                mode = p->fts_statb.st_mode & MBITS;
                    195:                if (mode < MAXMODE && ++m[mode] > maxmode) {
                    196:                        savemode = mode;
                    197:                        maxmode = m[mode];
                    198:                }
                    199:                gid = p->fts_statb.st_gid;
                    200:                if (gid < MAXGID && ++g[gid] > maxgid) {
                    201:                        savegid = gid;
                    202:                        maxgid = g[gid];
                    203:                }
                    204:                uid = p->fts_statb.st_uid;
                    205:                if (uid < MAXUID && ++u[uid] > maxuid) {
                    206:                        saveuid = uid;
                    207:                        maxuid = u[uid];
                    208:                }
                    209:                if (p->fts_namelen > 7)
                    210:                        *tabs = 2;
                    211:        }
                    212:        (void)printf("\n/set group=%u mode=%#o nlink=1 owner=%u type=file\n",
                    213:            savegid, savemode, saveuid);
                    214:        *puid = saveuid;
                    215:        *pgid = savegid;
                    216:        *pmode = savemode;
                    217:        return(0);
                    218: }
                    219: 
                    220: dsort(p1, p2)
                    221:        FTSENT **p1, **p2;
                    222: {
                    223:        register FTSENT *a, *b;
                    224: 
                    225:        a = *p1;
                    226:        b = *p2;
                    227: 
                    228:        if (S_ISDIR(a->fts_statb.st_mode)) {
                    229:                if (!S_ISDIR(b->fts_statb.st_mode))
                    230:                        return(1);
                    231:        } else if (S_ISDIR(b->fts_statb.st_mode))
                    232:                return(-1);
                    233:        return(strcmp(a->fts_name, b->fts_name));
                    234: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.