1*41367Sbostic /*- 2*41367Sbostic * Copyright (c) 1990 The Regents of the University of California. 3*41367Sbostic * All rights reserved. 4*41367Sbostic * 5*41367Sbostic * This code is derived from software contributed to Berkeley by 6*41367Sbostic * Cimarron D. Taylor of the University of California, Berkeley. 7*41367Sbostic * 8*41367Sbostic * %sccs.include.redist.c% 9*41367Sbostic */ 10*41367Sbostic 11*41367Sbostic #ifndef lint 12*41367Sbostic static char sccsid[] = "@(#)misc.c 5.1 (Berkeley) 05/03/90"; 13*41367Sbostic #endif /* not lint */ 14*41367Sbostic 15*41367Sbostic #include <sys/types.h> 16*41367Sbostic #include <sys/stat.h> 17*41367Sbostic #include <stdio.h> 18*41367Sbostic #include "find.h" 19*41367Sbostic 20*41367Sbostic /* 21*41367Sbostic * find_getpaths -- 22*41367Sbostic * remove the path strings from the command line and returns them in 23*41367Sbostic * another array. The find syntax assumes all command arguments up 24*41367Sbostic * to the first one beginning with a '-', '(' or '!' are pathnames. 25*41367Sbostic */ 26*41367Sbostic char ** 27*41367Sbostic find_getpaths(argvp) 28*41367Sbostic char ***argvp; 29*41367Sbostic { 30*41367Sbostic register char **argv; 31*41367Sbostic char **start; 32*41367Sbostic 33*41367Sbostic /* 34*41367Sbostic * find first '-', '(' or '!' to delimit paths; if no paths, it's 35*41367Sbostic * an error. Shift the array back one at the same time, creating 36*41367Sbostic * a separate array of pathnames. 37*41367Sbostic */ 38*41367Sbostic for (argv = *argvp + 1;; ++argv) { 39*41367Sbostic argv[-1] = argv[0]; 40*41367Sbostic if (!*argv || **argv == '-' || **argv == '!' || **argv == '(') 41*41367Sbostic break; 42*41367Sbostic } 43*41367Sbostic 44*41367Sbostic if (argv == *argvp + 1) 45*41367Sbostic usage(); 46*41367Sbostic 47*41367Sbostic argv[-1] = NULL; 48*41367Sbostic 49*41367Sbostic start = *argvp; /* save beginning of path array */ 50*41367Sbostic *argvp = argv; /* move argv value */ 51*41367Sbostic return(start); /* return path array */ 52*41367Sbostic } 53*41367Sbostic 54*41367Sbostic /* 55*41367Sbostic * find_subst -- 56*41367Sbostic * Replace occurrences of {} in s1 with s2 and return the result string. 57*41367Sbostic * Find_subst always returns a newly allocated string which should be 58*41367Sbostic * freed by the caller. 59*41367Sbostic */ 60*41367Sbostic find_subst(orig, store, path, len) 61*41367Sbostic char *orig, **store, *path; 62*41367Sbostic int len; 63*41367Sbostic { 64*41367Sbostic register int plen; 65*41367Sbostic register char ch, *p; 66*41367Sbostic char *realloc(); 67*41367Sbostic 68*41367Sbostic plen = strlen(path); 69*41367Sbostic for (p = *store; ch = *orig; ++orig) 70*41367Sbostic if (ch == '{' && orig[1] == '}') { 71*41367Sbostic while ((p - *store) + plen > len) 72*41367Sbostic if (!(*store = realloc(*store, len *= 2))) { 73*41367Sbostic (void)fprintf(stderr, 74*41367Sbostic "find: %s.\n", strerror(errno)); 75*41367Sbostic exit(1); 76*41367Sbostic } 77*41367Sbostic bcopy(path, p, plen); 78*41367Sbostic p += plen; 79*41367Sbostic ++orig; 80*41367Sbostic } else 81*41367Sbostic *p++ = ch; 82*41367Sbostic *p = '\0'; 83*41367Sbostic } 84*41367Sbostic 85*41367Sbostic /* 86*41367Sbostic * find_queryuser -- 87*41367Sbostic * print a message to standard error and then read input from standard 88*41367Sbostic * input. If the input is 'y' then 1 is returned. 89*41367Sbostic */ 90*41367Sbostic find_queryuser(argv) 91*41367Sbostic register char **argv; 92*41367Sbostic { 93*41367Sbostic char buf[10]; 94*41367Sbostic 95*41367Sbostic (void)fprintf(stderr, "\"%s", *argv); 96*41367Sbostic while (*++argv) 97*41367Sbostic (void)fprintf(stderr, " %s", *argv); 98*41367Sbostic (void)fprintf(stderr, "\"? "); 99*41367Sbostic (void)fflush(stderr); 100*41367Sbostic return(!fgets(buf, sizeof(buf), stdin) || buf[0] != 'y' ? 0 : 1); 101*41367Sbostic } 102*41367Sbostic 103*41367Sbostic /* 104*41367Sbostic * bad_arg -- 105*41367Sbostic * print out a bad argument message. 106*41367Sbostic */ 107*41367Sbostic void 108*41367Sbostic bad_arg(option, error) 109*41367Sbostic char *option, *error; 110*41367Sbostic { 111*41367Sbostic (void)fprintf(stderr, "find: %s: %s.\n", option, error); 112*41367Sbostic exit(1); 113*41367Sbostic } 114*41367Sbostic 115*41367Sbostic /* 116*41367Sbostic * emalloc -- 117*41367Sbostic * malloc with error checking. 118*41367Sbostic */ 119*41367Sbostic char * 120*41367Sbostic emalloc(len) 121*41367Sbostic u_int len; 122*41367Sbostic { 123*41367Sbostic char *p, *malloc(); 124*41367Sbostic 125*41367Sbostic if (!(p = malloc(len))) { 126*41367Sbostic (void)fprintf(stderr, "find: %s.\n", strerror(errno)); 127*41367Sbostic exit(1); 128*41367Sbostic } 129*41367Sbostic return(p); 130*41367Sbostic } 131*41367Sbostic 132*41367Sbostic usage() 133*41367Sbostic { 134*41367Sbostic (void)fprintf(stderr, "usage: find path-list expression\n"); 135*41367Sbostic exit(1); 136*41367Sbostic } 137