NetBSD/usr.sbin/mtree/create.c

236 lines
6.1 KiB
C
Raw Normal View History

1993-03-21 12:45:37 +03:00
/*-
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
1993-08-01 21:54:45 +04:00
/*static char sccsid[] = "from: @(#)create.c 5.16 (Berkeley) 3/12/91";*/
static char rcsid[] = "$Id: create.c,v 1.4 1993/10/01 01:06:39 jtc Exp $";
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <time.h>
#include <fts.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include "mtree.h"
#define LABEL \
if (label++) \
(void)putchar(' '); \
int ftsoptions = FTS_PHYSICAL;
cwalk()
{
extern int dflag;
register FTS *t;
register FTSENT *p;
register int cnt, label, notset;
time_t clock;
uid_t uid;
gid_t gid;
mode_t mode;
int tabs, dsort();
char *argv[2];
char curp[MAXPATHLEN], *inotype(), *getlogin(), *rlink();
if (!getwd(curp)) {
(void)fprintf(stderr, "mtree: %s\n", curp);
exit(1);
}
(void)time(&clock);
(void)printf("#\t fs: %s\n#\t by: %s\n#\tdate: %s\n",
curp, getlogin(), ctime(&clock));
argv[0] = ".";
argv[1] = (char *)NULL;
if (!(t = fts_open(argv, ftsoptions, dsort))) {
(void)fprintf(stderr,
"mtree: fts_open: %s.\n", strerror(errno));
exit(1);
}
while (p = fts_read(t)) {
switch(p->fts_info) {
case FTS_D:
if (dflag)
notset = 1;
else
notset =
statdir(t, p, &uid, &gid, &mode, &tabs);
if (!strcmp(p->fts_name, "."))
continue;
break;
case FTS_DP:
if (p->fts_level <= 0)
continue;
for (cnt = p->fts_level - 1; cnt-- > 0; )
(void)putchar('\t');
(void)printf("..\n");
continue;
case FTS_DNR:
case FTS_ERR:
case FTS_NS:
(void)fprintf(stderr, "mtree: %s: %s.\n",
p->fts_path, strerror(p->fts_errno));
1993-03-21 12:45:37 +03:00
continue;
default:
if (dflag)
continue;
}
for (cnt = p->fts_level - 1; cnt-- > 0; )
(void)putchar('\t');
(void)printf("%s", p->fts_name);
if (p->fts_info == FTS_D)
(void)putchar('\t');
else {
if (tabs > 1 && p->fts_namelen < 8)
(void)putchar('\t');
(void)putchar('\t');
}
label = 0;
1993-08-06 07:48:27 +04:00
if (!S_ISREG(p->fts_statp->st_mode) || notset) {
1993-03-21 12:45:37 +03:00
LABEL;
1993-08-06 07:48:27 +04:00
(void)printf("type=%s", inotype(p->fts_statp->st_mode));
1993-03-21 12:45:37 +03:00
}
1993-08-06 07:48:27 +04:00
if (p->fts_statp->st_uid != uid || notset) {
1993-03-21 12:45:37 +03:00
LABEL;
1993-08-06 07:48:27 +04:00
(void)printf("owner=%u", p->fts_statp->st_uid);
1993-03-21 12:45:37 +03:00
}
1993-08-06 07:48:27 +04:00
if (p->fts_statp->st_gid != gid || notset) {
1993-03-21 12:45:37 +03:00
LABEL;
1993-08-06 07:48:27 +04:00
(void)printf("group=%u", p->fts_statp->st_gid);
1993-03-21 12:45:37 +03:00
}
1993-08-06 07:48:27 +04:00
if ((p->fts_statp->st_mode & MBITS) != mode || notset) {
1993-03-21 12:45:37 +03:00
LABEL;
1993-08-06 07:48:27 +04:00
(void)printf("mode=%#o", p->fts_statp->st_mode & MBITS);
1993-03-21 12:45:37 +03:00
}
1993-08-06 07:48:27 +04:00
if (p->fts_statp->st_nlink != 1 || notset) {
1993-03-21 12:45:37 +03:00
LABEL;
1993-08-06 07:48:27 +04:00
(void)printf("nlink=%u", p->fts_statp->st_nlink);
1993-03-21 12:45:37 +03:00
}
LABEL;
1993-08-06 07:48:27 +04:00
(void)printf("size=%ld", p->fts_statp->st_size);
1993-03-21 12:45:37 +03:00
LABEL;
1993-08-06 07:48:27 +04:00
(void)printf("time=%ld", p->fts_statp->st_mtime);
1993-03-21 12:45:37 +03:00
if (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE) {
LABEL;
(void)printf("link=%s", rlink(p->fts_accpath));
}
(void)putchar('\n');
}
(void)fts_close(t);
}
#define MAXGID 5000
#define MAXUID 5000
#define MAXMODE MBITS + 1
statdir(t, parent, puid, pgid, pmode, tabs)
FTS *t;
FTSENT *parent;
uid_t *puid;
gid_t *pgid;
mode_t *pmode;
int *tabs;
{
register FTSENT *p;
register gid_t gid;
register uid_t uid;
register mode_t mode;
gid_t savegid;
uid_t saveuid;
mode_t savemode;
u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE];
1993-08-06 07:48:27 +04:00
if (!(p = fts_children(t, ftsoptions))) {
1993-03-21 12:45:37 +03:00
if (errno) {
(void)fprintf(stderr, "mtree: %s: %s.\n",
RP(parent), strerror(errno));
exit(1);
}
return(1);
}
bzero(g, sizeof(g));
bzero(u, sizeof(u));
bzero(m, sizeof(m));
*tabs = 1;
maxuid = maxgid = maxmode = 0;
for (; p; p = p->fts_link) {
1993-08-06 07:48:27 +04:00
mode = p->fts_statp->st_mode & MBITS;
1993-03-21 12:45:37 +03:00
if (mode < MAXMODE && ++m[mode] > maxmode) {
savemode = mode;
maxmode = m[mode];
}
1993-08-06 07:48:27 +04:00
gid = p->fts_statp->st_gid;
1993-03-21 12:45:37 +03:00
if (gid < MAXGID && ++g[gid] > maxgid) {
savegid = gid;
maxgid = g[gid];
}
1993-08-06 07:48:27 +04:00
uid = p->fts_statp->st_uid;
1993-03-21 12:45:37 +03:00
if (uid < MAXUID && ++u[uid] > maxuid) {
saveuid = uid;
maxuid = u[uid];
}
if (p->fts_namelen > 7)
*tabs = 2;
}
(void)printf("\n/set group=%u mode=%#o nlink=1 owner=%u type=file\n",
savegid, savemode, saveuid);
*puid = saveuid;
*pgid = savegid;
*pmode = savemode;
return(0);
}
dsort(p1, p2)
FTSENT **p1, **p2;
{
register FTSENT *a, *b;
a = *p1;
b = *p2;
1993-08-06 07:48:27 +04:00
if (S_ISDIR(a->fts_statp->st_mode)) {
if (!S_ISDIR(b->fts_statp->st_mode))
1993-03-21 12:45:37 +03:00
return(1);
1993-08-06 07:48:27 +04:00
} else if (S_ISDIR(b->fts_statp->st_mode))
1993-03-21 12:45:37 +03:00
return(-1);
return(strcmp(a->fts_name, b->fts_name));
}