Home | History | Annotate | Line # | Download | only in mtree
spec.c revision 1.5
      1  1.1  cgd /*-
      2  1.5  cgd  * Copyright (c) 1989, 1993
      3  1.5  cgd  *	The Regents of the University of California.  All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.5  cgd static char sccsid[] = "@(#)spec.c	8.1 (Berkeley) 6/6/93";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd #include <sys/types.h>
     39  1.3  cgd #include <sys/stat.h>
     40  1.3  cgd #include <fts.h>
     41  1.1  cgd #include <pwd.h>
     42  1.1  cgd #include <grp.h>
     43  1.3  cgd #include <errno.h>
     44  1.3  cgd #include <unistd.h>
     45  1.1  cgd #include <stdio.h>
     46  1.1  cgd #include <ctype.h>
     47  1.1  cgd #include "mtree.h"
     48  1.3  cgd #include "extern.h"
     49  1.1  cgd 
     50  1.3  cgd int lineno;				/* Current spec line number. */
     51  1.1  cgd 
     52  1.3  cgd static void	 set __P((char *, NODE *));
     53  1.3  cgd static void	 unset __P((char *, NODE *));
     54  1.1  cgd 
     55  1.3  cgd NODE *
     56  1.1  cgd spec()
     57  1.1  cgd {
     58  1.1  cgd 	register NODE *centry, *last;
     59  1.1  cgd 	register char *p;
     60  1.3  cgd 	NODE ginfo, *root;
     61  1.3  cgd 	int c_cur, c_next;
     62  1.1  cgd 	char buf[2048];
     63  1.1  cgd 
     64  1.3  cgd 	root = NULL;
     65  1.3  cgd 	bzero(&ginfo, sizeof(ginfo));
     66  1.3  cgd 	c_cur = c_next = 0;
     67  1.3  cgd 	for (lineno = 1; fgets(buf, sizeof(buf), stdin);
     68  1.3  cgd 	    ++lineno, c_cur = c_next, c_next = 0) {
     69  1.3  cgd 		/* Skip empty lines. */
     70  1.3  cgd 		if (buf[0] == '\n')
     71  1.3  cgd 			continue;
     72  1.3  cgd 
     73  1.3  cgd 		/* Find end of line. */
     74  1.3  cgd 		if ((p = index(buf, '\n')) == NULL)
     75  1.3  cgd 			err("line %d too long", lineno);
     76  1.3  cgd 
     77  1.3  cgd 		/* See if next line is continuation line. */
     78  1.3  cgd 		if (p[-1] == '\\') {
     79  1.3  cgd 			--p;
     80  1.3  cgd 			c_next = 1;
     81  1.1  cgd 		}
     82  1.3  cgd 
     83  1.3  cgd 		/* Null-terminate the line. */
     84  1.1  cgd 		*p = '\0';
     85  1.3  cgd 
     86  1.3  cgd 		/* Skip leading whitespace. */
     87  1.1  cgd 		for (p = buf; *p && isspace(*p); ++p);
     88  1.3  cgd 
     89  1.3  cgd 		/* If nothing but whitespace or comment char, continue. */
     90  1.1  cgd 		if (!*p || *p == '#')
     91  1.1  cgd 			continue;
     92  1.1  cgd 
     93  1.3  cgd #ifdef DEBUG
     94  1.3  cgd 		(void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
     95  1.3  cgd #endif
     96  1.3  cgd 		if (c_cur) {
     97  1.3  cgd 			set(p, centry);
     98  1.3  cgd 			continue;
     99  1.3  cgd 		}
    100  1.3  cgd 
    101  1.3  cgd 		/* Grab file name, "$", "set", or "unset". */
    102  1.3  cgd 		if ((p = strtok(p, "\n\t ")) == NULL)
    103  1.3  cgd 			err("missing field");
    104  1.1  cgd 
    105  1.1  cgd 		if (p[0] == '/')
    106  1.1  cgd 			switch(p[1]) {
    107  1.1  cgd 			case 's':
    108  1.1  cgd 				if (strcmp(p + 1, "set"))
    109  1.1  cgd 					break;
    110  1.3  cgd 				set(NULL, &ginfo);
    111  1.1  cgd 				continue;
    112  1.1  cgd 			case 'u':
    113  1.1  cgd 				if (strcmp(p + 1, "unset"))
    114  1.1  cgd 					break;
    115  1.3  cgd 				unset(NULL, &ginfo);
    116  1.1  cgd 				continue;
    117  1.1  cgd 			}
    118  1.1  cgd 
    119  1.3  cgd 		if (index(p, '/'))
    120  1.3  cgd 			err("slash character in file name");
    121  1.1  cgd 
    122  1.1  cgd 		if (!strcmp(p, "..")) {
    123  1.3  cgd 			/* Don't go up, if haven't gone down. */
    124  1.1  cgd 			if (!root)
    125  1.3  cgd 				goto noparent;
    126  1.1  cgd 			if (last->type != F_DIR || last->flags & F_DONE) {
    127  1.1  cgd 				if (last == root)
    128  1.3  cgd 					goto noparent;
    129  1.1  cgd 				last = last->parent;
    130  1.1  cgd 			}
    131  1.1  cgd 			last->flags |= F_DONE;
    132  1.1  cgd 			continue;
    133  1.3  cgd 
    134  1.3  cgd noparent:		err("no parent node");
    135  1.1  cgd 		}
    136  1.1  cgd 
    137  1.3  cgd 		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
    138  1.3  cgd 			err("%s", strerror(errno));
    139  1.1  cgd 		*centry = ginfo;
    140  1.1  cgd 		(void)strcpy(centry->name, p);
    141  1.1  cgd #define	MAGIC	"?*["
    142  1.1  cgd 		if (strpbrk(p, MAGIC))
    143  1.1  cgd 			centry->flags |= F_MAGIC;
    144  1.3  cgd 		set(NULL, centry);
    145  1.1  cgd 
    146  1.1  cgd 		if (!root) {
    147  1.1  cgd 			last = root = centry;
    148  1.1  cgd 			root->parent = root;
    149  1.1  cgd 		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
    150  1.1  cgd 			centry->parent = last;
    151  1.1  cgd 			last = last->child = centry;
    152  1.1  cgd 		} else {
    153  1.1  cgd 			centry->parent = last->parent;
    154  1.1  cgd 			centry->prev = last;
    155  1.1  cgd 			last = last->next = centry;
    156  1.1  cgd 		}
    157  1.1  cgd 	}
    158  1.3  cgd 	return (root);
    159  1.1  cgd }
    160  1.1  cgd 
    161  1.3  cgd static void
    162  1.3  cgd set(t, ip)
    163  1.3  cgd 	char *t;
    164  1.1  cgd 	register NODE *ip;
    165  1.1  cgd {
    166  1.1  cgd 	register int type;
    167  1.1  cgd 	register char *kw, *val;
    168  1.3  cgd 	struct group *gr;
    169  1.3  cgd 	struct passwd *pw;
    170  1.3  cgd 	mode_t *m;
    171  1.3  cgd 	int value;
    172  1.3  cgd 	char *ep;
    173  1.3  cgd 
    174  1.3  cgd 	for (; kw = strtok(t, "= \t\n"); t = NULL) {
    175  1.3  cgd 		ip->flags |= type = parsekey(kw, &value);
    176  1.3  cgd 		if (value && (val = strtok(NULL, " \t\n")) == NULL)
    177  1.3  cgd 			err("missing value");
    178  1.1  cgd 		switch(type) {
    179  1.1  cgd 		case F_CKSUM:
    180  1.3  cgd 			ip->cksum = strtoul(val, &ep, 10);
    181  1.3  cgd 			if (*ep)
    182  1.3  cgd 				err("invalid checksum %s", val);
    183  1.3  cgd 			break;
    184  1.3  cgd 		case F_GID:
    185  1.3  cgd 			ip->st_gid = strtoul(val, &ep, 10);
    186  1.3  cgd 			if (*ep)
    187  1.3  cgd 				err("invalid gid %s", val);
    188  1.3  cgd 			break;
    189  1.3  cgd 		case F_GNAME:
    190  1.3  cgd 			if ((gr = getgrnam(val)) == NULL)
    191  1.3  cgd 			    err("unknown group %s", val);
    192  1.3  cgd 			ip->st_gid = gr->gr_gid;
    193  1.1  cgd 			break;
    194  1.1  cgd 		case F_IGN:
    195  1.1  cgd 			/* just set flag bit */
    196  1.1  cgd 			break;
    197  1.3  cgd 		case F_MODE:
    198  1.3  cgd 			if ((m = setmode(val)) == NULL)
    199  1.3  cgd 				err("invalid file mode %s", val);
    200  1.1  cgd 			ip->st_mode = getmode(m, 0);
    201  1.1  cgd 			break;
    202  1.1  cgd 		case F_NLINK:
    203  1.3  cgd 			ip->st_nlink = strtoul(val, &ep, 10);
    204  1.3  cgd 			if (*ep)
    205  1.3  cgd 				err("invalid link count %s", val);
    206  1.1  cgd 			break;
    207  1.1  cgd 		case F_SIZE:
    208  1.4  cgd 			ip->st_size = strtouq(val, &ep, 10);
    209  1.3  cgd 			if (*ep)
    210  1.3  cgd 				err("invalid size %s", val);
    211  1.1  cgd 			break;
    212  1.1  cgd 		case F_SLINK:
    213  1.3  cgd 			if ((ip->slink = strdup(val)) == NULL)
    214  1.3  cgd 				err("%s", strerror(errno));
    215  1.1  cgd 			break;
    216  1.1  cgd 		case F_TIME:
    217  1.5  cgd 			ip->st_mtimespec.ts_sec = strtoul(val, &ep, 10);
    218  1.5  cgd 			if (*ep != '.')
    219  1.5  cgd 				err("invalid time %s", val);
    220  1.5  cgd 			val = ep + 1;
    221  1.5  cgd 			ip->st_mtimespec.ts_nsec = strtoul(val, &ep, 10);
    222  1.3  cgd 			if (*ep)
    223  1.3  cgd 				err("invalid time %s", val);
    224  1.1  cgd 			break;
    225  1.1  cgd 		case F_TYPE:
    226  1.1  cgd 			switch(*val) {
    227  1.1  cgd 			case 'b':
    228  1.1  cgd 				if (!strcmp(val, "block"))
    229  1.1  cgd 					ip->type = F_BLOCK;
    230  1.1  cgd 				break;
    231  1.1  cgd 			case 'c':
    232  1.1  cgd 				if (!strcmp(val, "char"))
    233  1.1  cgd 					ip->type = F_CHAR;
    234  1.1  cgd 				break;
    235  1.1  cgd 			case 'd':
    236  1.1  cgd 				if (!strcmp(val, "dir"))
    237  1.1  cgd 					ip->type = F_DIR;
    238  1.1  cgd 				break;
    239  1.1  cgd 			case 'f':
    240  1.1  cgd 				if (!strcmp(val, "file"))
    241  1.1  cgd 					ip->type = F_FILE;
    242  1.1  cgd 				if (!strcmp(val, "fifo"))
    243  1.1  cgd 					ip->type = F_FIFO;
    244  1.1  cgd 				break;
    245  1.1  cgd 			case 'l':
    246  1.1  cgd 				if (!strcmp(val, "link"))
    247  1.1  cgd 					ip->type = F_LINK;
    248  1.1  cgd 				break;
    249  1.1  cgd 			case 's':
    250  1.1  cgd 				if (!strcmp(val, "socket"))
    251  1.1  cgd 					ip->type = F_SOCK;
    252  1.1  cgd 				break;
    253  1.1  cgd 			default:
    254  1.3  cgd 				err("unknown file type %s", val);
    255  1.1  cgd 			}
    256  1.1  cgd 			break;
    257  1.3  cgd 		case F_UID:
    258  1.3  cgd 			ip->st_uid = strtoul(val, &ep, 10);
    259  1.3  cgd 			if (*ep)
    260  1.3  cgd 				err("invalid uid %s", val);
    261  1.3  cgd 			break;
    262  1.3  cgd 		case F_UNAME:
    263  1.3  cgd 			if ((pw = getpwnam(val)) == NULL)
    264  1.3  cgd 			    err("unknown user %s", val);
    265  1.3  cgd 			ip->st_uid = pw->pw_uid;
    266  1.3  cgd 			break;
    267  1.1  cgd 		}
    268  1.1  cgd 	}
    269  1.1  cgd }
    270  1.1  cgd 
    271  1.3  cgd static void
    272  1.3  cgd unset(t, ip)
    273  1.3  cgd 	char *t;
    274  1.1  cgd 	register NODE *ip;
    275  1.1  cgd {
    276  1.1  cgd 	register char *p;
    277  1.1  cgd 
    278  1.3  cgd 	while (p = strtok(t, "\n\t "))
    279  1.3  cgd 		ip->flags &= ~parsekey(p, NULL);
    280  1.1  cgd }
    281