summaryrefslogtreecommitdiff
path: root/sys/src/ape/cmd/pax/namelist.c
blob: 9c539d706d89bf93af6bfaf22ab08dd0660ea66a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* $Source: /u/mark/src/pax/RCS/namelist.c,v $
 *
 * $Revision: 1.6 $
 *
 * namelist.c - track filenames given as arguments to tar/cpio/pax
 *
 * DESCRIPTION
 *
 *	Arguments may be regular expressions, therefore all agurments will
 *	be treated as if they were regular expressions, even if they are
 *	not.
 *
 * AUTHOR
 *
 *	Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
 *
 * Sponsored by The USENIX Association for public distribution. 
 *
 * Copyright (c) 1989 Mark H. Colburn.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice is duplicated in all such 
 * forms and that any documentation, advertising materials, and other 
 * materials related to such distribution and use acknowledge that the 
 * software was developed by Mark H. Colburn and sponsored by The 
 * USENIX Association. 
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * $Log:	namelist.c,v $
 * Revision 1.6  89/02/13  09:14:48  mark
 * Fixed problem with directory errors
 * 
 * Revision 1.5  89/02/12  12:14:00  mark
 * Fixed misspellings
 * 
 * Revision 1.4  89/02/12  11:25:19  mark
 * Modifications to compile and link cleanly under USG
 * 
 * Revision 1.3  89/02/12  10:40:23  mark
 * Fixed casting problems
 * 
 * Revision 1.2  89/02/12  10:04:57  mark
 * 1.2 release fixes
 * 
 * Revision 1.1  88/12/23  18:02:17  mark
 * Initial revision
 * 
 */

#ifndef lint
static char *ident = "$Id: namelist.c,v 1.6 89/02/13 09:14:48 mark Exp $";
static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
#endif /* ! lint */


/* Headers */

#include "pax.h"


/* Type Definitions */

/*
 * Structure for keeping track of filenames and lists thereof. 
 */
struct nm_list {
    struct nm_list *next;
    short           length;	/* cached strlen(name) */
    char            found;	/* A matching file has been found */
    char            firstch;	/* First char is literally matched */
    char            re;		/* regexp pattern for item */
    char            name[1];	/* name of file or rexexp */
};

struct dirinfo {
    char            dirname[PATH_MAX + 1];	/* name of directory */
    OFFSET	    where;	/* current location in directory */
    struct dirinfo *next;
};


/* Static Variables */

static struct dirinfo *stack_head = (struct dirinfo *)NULL;


/* Function Prototypes */

#ifndef __STDC__

static void pushdir();
static struct dirinfo *popdir();

#else

static void pushdir(struct dirinfo *info);
static struct dirinfo *popdir(void);

#endif


/* Internal Identifiers */

static struct nm_list *namelast;	/* Points to last name in list */
static struct nm_list *namelist;	/* Points to first name in list */


/* addname -  add a name to the namelist. 
 *
 * DESCRIPTION
 *
 *	Addname adds the name given to the name list.  Memory for the
 *	namelist structure is dynamically allocated.  If the space for 
 *	the structure cannot be allocated, then the program will exit
 *	the an out of memory error message and a non-zero return code
 *	will be returned to the caller.
 *
 * PARAMETERS
 *
 *	char *name	- A pointer to the name to add to the list
 */

#ifdef __STDC__

void add_name(char *name)

#else
    
void add_name(name)
char           *name;		/* pointer to name */

#endif
{
    int             i;		/* Length of string */
    struct nm_list *p;		/* Current struct pointer */

    i = strlen(name);
    p = (struct nm_list *) malloc((unsigned) (i + sizeof(struct nm_list)));
    if (!p) {
	fatal("cannot allocate memory for namelist entry\n");
    }
    p->next = (struct nm_list *)NULL;
    p->length = i;
    strncpy(p->name, name, i);
    p->name[i] = '\0';		/* Null term */
    p->found = 0;
    p->firstch = isalpha(name[0]);
    if (strchr(name, '*') || strchr(name, '[') || strchr(name, '?')) {
        p->re = 1;
    }
    if (namelast) {
	namelast->next = p;
    }
    namelast = p;
    if (!namelist) {
	namelist = p;
    }
}


/* name_match - match a name from an archive with a name from the namelist 
 *
 * DESCRIPTION
 *
 *	Name_match attempts to find a name pointed at by p in the namelist.
 *	If no namelist is available, then all filenames passed in are
 *	assumed to match the filename criteria.  Name_match knows how to
 *	match names with regular expressions, etc.
 *
 * PARAMETERS
 *
 *	char	*p	- the name to match
 *
 * RETURNS
 *
 *	Returns 1 if the name is in the namelist, or no name list is
 *	available, otherwise returns 0
 *
 */

#ifdef __STDC__

int name_match(char *p)

#else
    
int name_match(p)
char           *p;

#endif
{
    struct nm_list *nlp;
    int             len;

    if ((nlp = namelist) == 0) {/* Empty namelist is easy */
	return (1);
    }
    len = strlen(p);
    for (; nlp != 0; nlp = nlp->next) {
	/* If first chars don't match, quick skip */
	if (nlp->firstch && nlp->name[0] != p[0]) {
	    continue;
	}
	/* Regular expressions */
	if (nlp->re) {
	    if (wildmat(nlp->name, p)) {
		nlp->found = 1;	/* Remember it matched */
		return (1);	/* We got a match */
	    }
	    continue;
	}
	/* Plain Old Strings */
	if (nlp->length <= len	/* Archive len >= specified */
	    && (p[nlp->length] == '\0' || p[nlp->length] == '/')
	    && strncmp(p, nlp->name, nlp->length) == 0) {
	    /* Name compare */
	    nlp->found = 1;	/* Remember it matched */
	    return (1);		/* We got a match */
	}
    }
    return (0);
}


/* names_notfound - print names of files in namelist that were not found 
 *
 * DESCRIPTION
 *
 *	Names_notfound scans through the namelist for any files which were
 *	named, but for which a matching file was not processed by the
 *	archive.  Each of the files is listed on the standard error.
 *
 */

#ifdef __STDC__

void names_notfound(void)

#else
    
void names_notfound()

#endif
{
    struct nm_list *nlp;

    for (nlp = namelist; nlp != 0; nlp = nlp->next) {
	if (!nlp->found) {
	    fprintf(stderr, "%s: %s not found in archive\n",
	            myname, nlp->name);
	}
	free(nlp);
    }
    namelist = (struct nm_list *)NULL;
    namelast = (struct nm_list *)NULL;
}


/* name_init - set up to gather file names 
 *
 * DESCRIPTION
 *
 *	Name_init sets up the namelist pointers so that we may access the
 *	command line arguments.  At least the first item of the command
 *	line (argv[0]) is assumed to be stripped off, prior to the
 *	name_init call.
 *
 * PARAMETERS
 *
 *	int	argc	- number of items in argc
 *	char	**argv	- pointer to the command line arguments
 */

#ifdef __STDC__

void name_init(int argc, char **argv)

#else
    
void name_init(argc, argv)
int             argc;
char          **argv;

#endif
{
    /* Get file names from argv, after options. */
    n_argc = argc;
    n_argv = argv;
}


/* name_next - get the next name from argv or the name file. 
 *
 * DESCRIPTION
 *
 *	Name next finds the next name which is to be processed in the
 *	archive.  If the named file is a directory, then the directory
 *	is recursively traversed for additional file names.  Directory
 *	names and locations within the directory are kept track of by
 *	using a directory stack.  See the pushdir/popdir function for
 *	more details.
 *
 * 	The names come from argv, after options or from the standard input.  
 *
 * PARAMETERS
 *
 *	name - a pointer to a buffer of at least MAX_PATH + 1 bytes long;
 *	statbuf - a pointer to a stat structure
 *
 * RETURNS
 *
 *	Returns -1 if there are no names left, (e.g. EOF), otherwise returns 
 *	0 
 */

#ifdef __STDC__

int name_next(char *name, Stat *statbuf)

#else
    
int name_next(name, statbuf)
char           *name;
Stat           *statbuf;

#endif
{
    int             err = -1;
    static int      in_subdir = 0;
    static DIR     *dirp;
    struct dirent  *d;
    static struct dirinfo *curr_dir;
    int			len;

    do {
	if (names_from_stdin) {
	    if (lineget(stdin, name) < 0) {
		return (-1);
	    }
	    if (nameopt(name) < 0) {
		continue;
	    }
	} else {
	    if (in_subdir) {
		if ((d = readdir(dirp)) != (struct dirent *)NULL) {
		    /* Skip . and .. */
		    if (strcmp(d->d_name, ".") == 0 ||
		        strcmp(d->d_name, "..") == 0) {
			    continue;
		    }
		    if (strlen(d->d_name) + 
			strlen(curr_dir->dirname) >= PATH_MAX) {
			warn("name too long", d->d_name);
			continue;
		    }
		    strcpy(name, curr_dir->dirname);
		    strcat(name, d->d_name);
		} else {
		    closedir(dirp);
		    in_subdir--;
		    curr_dir = popdir();
		    if (in_subdir) {
			errno = 0;
			if ((dirp=opendir(curr_dir->dirname)) == (DIR *)NULL) {
			    warn(curr_dir->dirname, "error opening directory (1)");
			    in_subdir--;
			}
			seekdir(dirp, curr_dir->where);
		    }
		    continue;
		}
	    } else if (optind >= n_argc) {
		return (-1);
	    } else {
		strcpy(name, n_argv[optind++]);
	    }
	}
	if ((err = LSTAT(name, statbuf)) < 0) {
	    warn(name, strerror());
	    continue;
	}
	if (!names_from_stdin && (statbuf->sb_mode & S_IFMT) == S_IFDIR) {
	    if (in_subdir) {
		curr_dir->where = telldir(dirp);
		pushdir(curr_dir);
		closedir(dirp);
	    } 
	    in_subdir++;

	    /* Build new prototype name */
	    if ((curr_dir = (struct dirinfo *) mem_get(sizeof(struct dirinfo))) 
			  == (struct dirinfo *)NULL) {
		exit(2);
	    }
	    strcpy(curr_dir->dirname, name);
	    len = strlen(curr_dir->dirname);
	    while (len >= 1 && curr_dir->dirname[len - 1] == '/') {
		len--;		/* Delete trailing slashes */
	    }
	    curr_dir->dirname[len++] = '/';	/* Now add exactly one back */
	    curr_dir->dirname[len] = '\0';/* Make sure null-terminated */
            curr_dir->where = 0;
           
            errno = 0;
            do {
                if ((dirp = opendir(curr_dir->dirname)) == (DIR *)NULL) {
                     warn(curr_dir->dirname, "error opening directory (2)");
                     if (in_subdir > 1) {
                          curr_dir = popdir();
                     }
                     in_subdir--;
                     err = -1;
                     continue;
                } else {
                     seekdir(dirp, curr_dir->where);
		}
	    } while (in_subdir && (! dirp));
	}
    } while (err < 0);
    return (0);
}


/* name_gather - gather names in a list for scanning. 
 *
 * DESCRIPTION
 *
 *	Name_gather takes names from the command line and adds them to
 *	the name list.
 *
 * FIXME
 *
 * 	We could hash the names if we really care about speed here.
 */

#ifdef __STDC__

void name_gather(void)

#else
    
void name_gather()

#endif
{
     while (optind < n_argc) { 
	 add_name(n_argv[optind++]); 
     } 
}


/* pushdir - pushes a directory name on the directory stack
 *
 * DESCRIPTION
 *
 *	The pushdir function puses the directory structure which is pointed
 *	to by "info" onto a stack for later processing.  The information
 *	may be retrieved later with a call to popdir().
 *
 * PARAMETERS
 *
 *	dirinfo	*info	- pointer to directory structure to save
 */

#ifdef __STDC__

static void pushdir(struct dirinfo *info)

#else
    
static void pushdir(info)
struct dirinfo	*info;

#endif
{
    if  (stack_head == (struct dirinfo *)NULL) {
	stack_head = info;
	stack_head->next = (struct dirinfo *)NULL;
    } else {
	info->next = stack_head;
	stack_head = info;
    } 
}


/* popdir - pop a directory structure off the directory stack.
 *
 * DESCRIPTION
 *
 *	The popdir function pops the most recently pushed directory
 *	structure off of the directory stack and returns it to the calling
 *	function.
 *
 * RETURNS
 *
 *	Returns a pointer to the most recently pushed directory structure
 *	or NULL if the stack is empty.
 */

#ifdef __STDC__

static struct dirinfo *popdir(void)

#else
    
static struct dirinfo *popdir()

#endif
{
    struct dirinfo	*tmp;

    if (stack_head == (struct dirinfo *)NULL) {
	return((struct dirinfo *)NULL);
    } else {
	tmp = stack_head;
	stack_head = stack_head->next;
    }
    return(tmp);
}