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