]>
git.saurik.com Git - apple/file_cmds.git/blob - mtree/create.c
1 /* $NetBSD: create.c,v 1.16 1998/08/30 03:20:09 nathanw Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
41 __RCSID("$NetBSD: create.c,v 1.16 1998/08/30 03:20:09 nathanw Exp $");
45 #include <sys/param.h>
60 #define INDENTNAMELEN 15
63 extern int crc_total
, ftsoptions
;
64 extern int dflag
, sflag
;
66 extern char fullpath
[MAXPATHLEN
];
72 static int dsort
__P((const FTSENT
**, const FTSENT
**));
73 static void output
__P((int *, const char *, ...));
74 static int statd
__P((FTS
*, FTSENT
*, uid_t
*, gid_t
*, mode_t
*));
75 static void statf
__P((FTSENT
*));
83 char *argv
[2], host
[MAXHOSTNAMELEN
+ 1];
86 (void)gethostname(host
, sizeof(host
));
87 host
[sizeof(host
) - 1] = '\0';
89 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s",
90 getlogin(), host
, fullpath
, ctime(&clock
));
94 if ((t
= fts_open(argv
, ftsoptions
, dsort
)) == NULL
)
95 mtree_err("fts_open: %s", strerror(errno
));
96 while ((p
= fts_read(t
)) != NULL
)
99 (void)printf("\n# %s\n", p
->fts_path
);
100 statd(t
, p
, &uid
, &gid
, &mode
);
104 if (p
->fts_level
> 0)
105 (void)printf("# %s\n..\n\n", p
->fts_path
);
110 (void)fprintf(stderr
,
111 "mtree: %s: %s\n", p
->fts_path
, strerror(errno
));
120 if (sflag
&& keys
& F_CKSUM
)
121 (void)fprintf(stderr
,
122 "mtree: %s checksum: %u\n", fullpath
, crc_total
);
134 if (S_ISDIR(p
->fts_statp
->st_mode
))
135 indent
= printf("%s", p
->fts_name
);
137 indent
= printf(" %s", p
->fts_name
);
139 if (indent
> INDENTNAMELEN
)
142 indent
+= printf("%*s", INDENTNAMELEN
- indent
, "");
144 if (!S_ISREG(p
->fts_statp
->st_mode
))
145 output(&indent
, "type=%s", inotype(p
->fts_statp
->st_mode
));
146 if (keys
& (F_UID
| F_UNAME
) && p
->fts_statp
->st_uid
!= uid
) {
147 if (keys
& F_UNAME
&& (pw
= getpwuid(p
->fts_statp
->st_uid
)))
148 output(&indent
, "uname=%s", pw
->pw_name
);
149 else /* if (keys & F_UID) */
150 output(&indent
, "uid=%u", p
->fts_statp
->st_uid
);
152 if (keys
& (F_GID
| F_GNAME
) && p
->fts_statp
->st_gid
!= gid
) {
153 if (keys
& F_GNAME
&& (gr
= getgrgid(p
->fts_statp
->st_gid
)))
154 output(&indent
, "gname=%s", gr
->gr_name
);
155 else /* if (keys & F_GID) */
156 output(&indent
, "gid=%u", p
->fts_statp
->st_gid
);
158 if (keys
& F_MODE
&& (p
->fts_statp
->st_mode
& MBITS
) != mode
)
159 output(&indent
, "mode=%#o", p
->fts_statp
->st_mode
& MBITS
);
160 if (keys
& F_NLINK
&& p
->fts_statp
->st_nlink
!= 1)
161 output(&indent
, "nlink=%u", p
->fts_statp
->st_nlink
);
162 if (keys
& F_SIZE
&& S_ISREG(p
->fts_statp
->st_mode
))
163 output(&indent
, "size=%qd", p
->fts_statp
->st_size
);
165 output(&indent
, "time=%ld.%ld",
166 p
->fts_statp
->st_mtimespec
.tv_sec
,
167 p
->fts_statp
->st_mtimespec
.tv_nsec
);
168 if (keys
& F_CKSUM
&& S_ISREG(p
->fts_statp
->st_mode
)) {
169 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0 ||
171 mtree_err("%s: %s", p
->fts_accpath
, strerror(errno
));
173 output(&indent
, "cksum=%lu", val
);
175 if (keys
& F_SLINK
&&
176 (p
->fts_info
== FTS_SL
|| p
->fts_info
== FTS_SLNONE
))
177 output(&indent
, "link=%s", rlink(p
->fts_accpath
));
183 #define MAXMODE MBITS + 1
186 statd(t
, parent
, puid
, pgid
, pmode
)
202 u_short maxgid
, maxuid
, maxmode
, g
[MAXGID
], u
[MAXUID
], m
[MAXMODE
];
207 if ((p
= fts_children(t
, 0)) == NULL
) {
209 mtree_err("%s: %s", RP(parent
), strerror(errno
));
213 memset(g
, 0, sizeof(g
));
214 memset(u
, 0, sizeof(u
));
215 memset(m
, 0, sizeof(m
));
217 maxuid
= maxgid
= maxmode
= 0;
218 for (; p
; p
= p
->fts_link
) {
219 smode
= p
->fts_statp
->st_mode
& MBITS
;
220 if (smode
< MAXMODE
&& ++m
[smode
] > maxmode
) {
224 sgid
= p
->fts_statp
->st_gid
;
225 if (sgid
< MAXGID
&& ++g
[sgid
] > maxgid
) {
229 suid
= p
->fts_statp
->st_uid
;
230 if (suid
< MAXUID
&& ++u
[suid
] > maxuid
) {
235 (void)printf("/set type=file");
237 (void)printf(" gid=%u", savegid
);
238 if (keys
& F_GNAME
) {
239 if ((gr
= getgrgid(savegid
)) != NULL
)
240 (void)printf(" gname=%s", gr
->gr_name
);
242 (void)printf(" gid=%u", savegid
);
244 if (keys
& F_UNAME
) {
245 if ((pw
= getpwuid(saveuid
)) != NULL
)
246 (void)printf(" uname=%s", pw
->pw_name
);
248 (void)printf(" uid=%u", saveuid
);
251 (void)printf(" uid=%u", saveuid
);
253 (void)printf(" mode=%#o", savemode
);
255 (void)printf(" nlink=1");
265 const FTSENT
**a
, **b
;
267 if (S_ISDIR((*a
)->fts_statp
->st_mode
)) {
268 if (!S_ISDIR((*b
)->fts_statp
->st_mode
))
270 } else if (S_ISDIR((*b
)->fts_statp
->st_mode
))
272 return (strcmp((*a
)->fts_name
, (*b
)->fts_name
));
283 output(int *offset
, const char *fmt
, ...)
285 output(offset
, fmt
, va_alist
)
298 (void)vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
301 if (*offset
+ strlen(buf
) > MAXLINELEN
- 3) {
302 (void)printf(" \\\n%*s", INDENTNAMELEN
, "");
303 *offset
= INDENTNAMELEN
;
305 *offset
+= printf(" %s", buf
) + 1;