xref: /onnv-gate/usr/src/cmd/filesync/files.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1995 Sun Microsystems, Inc.  All Rights Reserved
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * module:
26*0Sstevel@tonic-gate  *	files.c
27*0Sstevel@tonic-gate  *
28*0Sstevel@tonic-gate  * purpose:
29*0Sstevel@tonic-gate  *	routines to examine and manipulate file names
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  * contents:
32*0Sstevel@tonic-gate  *	qualify ... ensure that a name is fully qualified
33*0Sstevel@tonic-gate  *	expand  ... expand env variables within a string or file name
34*0Sstevel@tonic-gate  *	noblanks .. ensure that a name contains no embdded unescaped blanks
35*0Sstevel@tonic-gate  *	lex ....... a lexer that can handle escaped/embedded blanks
36*0Sstevel@tonic-gate  *	wildcards . see whether or not a name contains wild cards
37*0Sstevel@tonic-gate  *	prefix .... does one string begin with another
38*0Sstevel@tonic-gate  *	suffix .... does one string end with another
39*0Sstevel@tonic-gate  *	contains .. does one string contain another
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  *	cannonize (static) ...	compress redundant "." and ".." out of name
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * notes:
44*0Sstevel@tonic-gate  *	we are interested in embedded blanks because international
45*0Sstevel@tonic-gate  *	character sets and non-unix file systems can both contain
46*0Sstevel@tonic-gate  *	the byte 0x20.  Thus, whenever we record a filename in
47*0Sstevel@tonic-gate  *	file, we must be careful to escape any embedded blanks that
48*0Sstevel@tonic-gate  *	cause trouble when we re-lex that file later.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate #ident	"%W%	%E% SMI"
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #include <stdio.h>
53*0Sstevel@tonic-gate #include <stdlib.h>
54*0Sstevel@tonic-gate #include <string.h>
55*0Sstevel@tonic-gate #include <ctype.h>
56*0Sstevel@tonic-gate #include <unistd.h>
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #include "filesync.h"
59*0Sstevel@tonic-gate #include "messages.h"
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static void cannonize(char *name);
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  * routine:
65*0Sstevel@tonic-gate  *	qualify
66*0Sstevel@tonic-gate  *
67*0Sstevel@tonic-gate  * purpose:
68*0Sstevel@tonic-gate  *	to fully qualify a name
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * parameters:
71*0Sstevel@tonic-gate  *	name to be qualified
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * returns:
74*0Sstevel@tonic-gate  *	either original pointer or copy to a new (malloced) buffer
75*0Sstevel@tonic-gate  *
76*0Sstevel@tonic-gate  * notes:
77*0Sstevel@tonic-gate  *	someday I may conclude that I should always make a copy
78*0Sstevel@tonic-gate  *	so that the caller can know that it is safe to free the parm
79*0Sstevel@tonic-gate  *
80*0Sstevel@tonic-gate  *	I thought about this and concluded that there is never a need
81*0Sstevel@tonic-gate  *	to fully qualify a string containing variables.  If the string
82*0Sstevel@tonic-gate  *	came from the command line, the variables were already expanded
83*0Sstevel@tonic-gate  *	and if it came from the rules data base it is required to already
84*0Sstevel@tonic-gate  *	be fully qualified.
85*0Sstevel@tonic-gate  */
86*0Sstevel@tonic-gate char *
qualify(char * name)87*0Sstevel@tonic-gate qualify(char *name)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	char namebuf[ MAX_PATH ];
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	/* in the simple case, the parameter is already there */
92*0Sstevel@tonic-gate 	if (*name == '/') {
93*0Sstevel@tonic-gate 		cannonize(name);
94*0Sstevel@tonic-gate 		return (name);
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	/* things that begin with variables get the benefit of the doubt */
98*0Sstevel@tonic-gate 	if (*name == '$') {
99*0Sstevel@tonic-gate 		cannonize(name);
100*0Sstevel@tonic-gate 		return (name);
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	/* start with the current working directory	*/
104*0Sstevel@tonic-gate 	if (getcwd(namebuf, sizeof (namebuf)) == 0) {
105*0Sstevel@tonic-gate 		fprintf(stderr, gettext(ERR_nocwd), name);
106*0Sstevel@tonic-gate 		exit(ERR_OTHER);
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	/* make sure we have room for our file name	*/
110*0Sstevel@tonic-gate 	if ((strlen(namebuf) + strlen(name) + 2) >= sizeof (namebuf)) {
111*0Sstevel@tonic-gate 		fprintf(stderr, gettext(ERR_longname), name);
112*0Sstevel@tonic-gate 		exit(ERR_OTHER);
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/* append the specified file name to it	*/
116*0Sstevel@tonic-gate 	strcat(namebuf, "/");
117*0Sstevel@tonic-gate 	strcat(namebuf, name);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/* filter out redundant dots	*/
120*0Sstevel@tonic-gate 	cannonize(namebuf);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	if (opt_debug & DBG_VARS)
123*0Sstevel@tonic-gate 		fprintf(stderr, "VARS: QUALIFY %s to %s\n", name, namebuf);
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	/* and return a newly malloc'd copy	*/
126*0Sstevel@tonic-gate 	return (strdup(namebuf));
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate  * routine:
131*0Sstevel@tonic-gate  *	expand
132*0Sstevel@tonic-gate  *
133*0Sstevel@tonic-gate  * purpose:
134*0Sstevel@tonic-gate  *	to expand variable names within a string
135*0Sstevel@tonic-gate  *
136*0Sstevel@tonic-gate  * parameters:
137*0Sstevel@tonic-gate  *	string to be expanded.  Variable references always begin
138*0Sstevel@tonic-gate  *	with a $ and are delimited by parens or curleys.
139*0Sstevel@tonic-gate  *
140*0Sstevel@tonic-gate  * returns:
141*0Sstevel@tonic-gate  *	either original pointer or a copy to a new (malloced) buffer
142*0Sstevel@tonic-gate  *
143*0Sstevel@tonic-gate  * notes:
144*0Sstevel@tonic-gate  *	someday I may conclude that I should always make a copy
145*0Sstevel@tonic-gate  *	so that the caller can know that it is safe to free the parm
146*0Sstevel@tonic-gate  *
147*0Sstevel@tonic-gate  *	someday I may decide to support escape conventions for embedding
148*0Sstevel@tonic-gate  *	$(){} in file names, but I suspec that day will never come.
149*0Sstevel@tonic-gate  *
150*0Sstevel@tonic-gate  *	I thought about this and concluded there was no reason to
151*0Sstevel@tonic-gate  *	fully qualify these names, because the only names that should
152*0Sstevel@tonic-gate  *	need qualification are src/dst lines from the command line,
153*0Sstevel@tonic-gate  *	and the shell should have handled those for me.  Once something
154*0Sstevel@tonic-gate  *	makes it into the database, it is expected to be fully qualified
155*0Sstevel@tonic-gate  *	already.
156*0Sstevel@tonic-gate  *
157*0Sstevel@tonic-gate  *	We are limited to producing strings of length MAX_PATH or less
158*0Sstevel@tonic-gate  *	and variable names of length MAX_NAME or less.  In practice,
159*0Sstevel@tonic-gate  *	these limitations should not be a problem.
160*0Sstevel@tonic-gate  */
161*0Sstevel@tonic-gate char *
expand(char * name)162*0Sstevel@tonic-gate expand(char *name)
163*0Sstevel@tonic-gate {	const char *s;
164*0Sstevel@tonic-gate 	char *p, *v;
165*0Sstevel@tonic-gate 	char delim;
166*0Sstevel@tonic-gate 	char namebuf[ MAX_PATH ];
167*0Sstevel@tonic-gate 	char varbuf[ MAX_NAME ];
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	/* first see if there are no variables to be bound */
170*0Sstevel@tonic-gate 	for (s = name; *s && *s != '$'; s++);
171*0Sstevel@tonic-gate 	if (*s == 0)
172*0Sstevel@tonic-gate 		return (name);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	/* move through the string, copying and expanding	*/
175*0Sstevel@tonic-gate 	for (s = name, p = namebuf; *s; s++) {
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 		/* check for overflow	*/
178*0Sstevel@tonic-gate 		if (p >= &namebuf[ MAX_PATH ]) {
179*0Sstevel@tonic-gate 			fprintf(stderr, gettext(ERR_longname), name);
180*0Sstevel@tonic-gate 			exit(ERR_OTHER);
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 		/* normal characters, we just copy		*/
184*0Sstevel@tonic-gate 		if (*s != '$') {
185*0Sstevel@tonic-gate 			*p++ = *s;
186*0Sstevel@tonic-gate 			continue;
187*0Sstevel@tonic-gate 		}
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 		/* figure out how the variable name is delimited */
190*0Sstevel@tonic-gate 		delim = *++s;
191*0Sstevel@tonic-gate 		if (delim == '(') {
192*0Sstevel@tonic-gate 			delim = ')';
193*0Sstevel@tonic-gate 			s++;
194*0Sstevel@tonic-gate 		} else if (delim == '{') {
195*0Sstevel@tonic-gate 			delim = '}';
196*0Sstevel@tonic-gate 			s++;
197*0Sstevel@tonic-gate 		} else
198*0Sstevel@tonic-gate 			delim = 0;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		/* copy the variable name up to the closing delimiter */
201*0Sstevel@tonic-gate 		for (v = varbuf; *s; s++) {
202*0Sstevel@tonic-gate 			if (isalnum(*s) || (*s == '_') ||
203*0Sstevel@tonic-gate 				(delim && *s != delim))
204*0Sstevel@tonic-gate 				*v++ = *s;
205*0Sstevel@tonic-gate 			else
206*0Sstevel@tonic-gate 				break;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 			/* make sure we don't overflow var name buffer	*/
209*0Sstevel@tonic-gate 			if (v >= &varbuf[MAX_NAME - 1]) {
210*0Sstevel@tonic-gate 				*v = 0;
211*0Sstevel@tonic-gate 				fprintf(stderr, gettext(ERR_longname), varbuf);
212*0Sstevel@tonic-gate 				exit(ERR_OTHER);
213*0Sstevel@tonic-gate 			}
214*0Sstevel@tonic-gate 		}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 		*v = 0;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 		/* FIX THIS ... there must be a more elegant way */
219*0Sstevel@tonic-gate 		/* we may have to back up because s will be bumped */
220*0Sstevel@tonic-gate 		if (delim == 0 || *s != delim)
221*0Sstevel@tonic-gate 			s--;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 		/* look up the variable 			*/
224*0Sstevel@tonic-gate 		v = getenv(varbuf);
225*0Sstevel@tonic-gate 		if (v == 0 || *v == 0) {
226*0Sstevel@tonic-gate 			fprintf(stderr, gettext(ERR_undef), varbuf);
227*0Sstevel@tonic-gate 			return (0);
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 		/* copy the variable into the buffer		*/
231*0Sstevel@tonic-gate 		while (*v)
232*0Sstevel@tonic-gate 			*p++ = *v++;
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/* null terminate the copy	*/
236*0Sstevel@tonic-gate 	*p = 0;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	/* compress out any redundant dots and dot-dots	*/
239*0Sstevel@tonic-gate 	cannonize(namebuf);
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	if (opt_debug & DBG_VARS)
242*0Sstevel@tonic-gate 		fprintf(stderr, "VARS: EXPAND %s to %s\n", name, namebuf);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	/* and return a newly malloc'd copy	*/
245*0Sstevel@tonic-gate 	return (strdup(namebuf));
246*0Sstevel@tonic-gate }
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate /*
249*0Sstevel@tonic-gate  * routine:
250*0Sstevel@tonic-gate  *	noblanks
251*0Sstevel@tonic-gate  *
252*0Sstevel@tonic-gate  * purpose:
253*0Sstevel@tonic-gate  *	to ensure that a name contains no unescaped embedded blanks
254*0Sstevel@tonic-gate  *
255*0Sstevel@tonic-gate  * parameters:
256*0Sstevel@tonic-gate  *	pointer to name
257*0Sstevel@tonic-gate  *
258*0Sstevel@tonic-gate  * returns:
259*0Sstevel@tonic-gate  *	pointer to name or pointer to buffer containing escaped version of name
260*0Sstevel@tonic-gate  *
261*0Sstevel@tonic-gate  * notes:
262*0Sstevel@tonic-gate  *	this routine can be called on full file names, and so can
263*0Sstevel@tonic-gate  *	conceivably require an arbitrarily large buffer.
264*0Sstevel@tonic-gate  */
265*0Sstevel@tonic-gate const char *
noblanks(const char * name)266*0Sstevel@tonic-gate noblanks(const char *name)
267*0Sstevel@tonic-gate {
268*0Sstevel@tonic-gate 	const char *s;
269*0Sstevel@tonic-gate 	char *p;
270*0Sstevel@tonic-gate 	static char *namebuf = 0;
271*0Sstevel@tonic-gate 	static int buflen = 0;
272*0Sstevel@tonic-gate 	int l;
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	/* first see if there are no embedded blanks	*/
275*0Sstevel@tonic-gate 	for (s = name; *s && *s != ' '; s++);
276*0Sstevel@tonic-gate 	if (*s == 0)
277*0Sstevel@tonic-gate 		return (name);
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	/* make sure we have a buffer large enough for the worst case	*/
280*0Sstevel@tonic-gate 	l = 4 + (2*strlen(name));
281*0Sstevel@tonic-gate 	for (buflen = MAX_PATH; buflen < l; buflen += MAX_NAME);
282*0Sstevel@tonic-gate 	namebuf = (char *) realloc(namebuf, buflen);
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	/* quote the name, and copy it, escaping quotes	*/
285*0Sstevel@tonic-gate 	p = namebuf;
286*0Sstevel@tonic-gate 	*p++ = '"';
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	for (s = name; *s; s++) {
289*0Sstevel@tonic-gate 		if (*s == '"' || *s == '\\')
290*0Sstevel@tonic-gate 			*p++ = '\\';
291*0Sstevel@tonic-gate 		*p++ = *s;
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	*p++ = '"';
295*0Sstevel@tonic-gate 	*p = 0;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	return (namebuf);
298*0Sstevel@tonic-gate }
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate /*
301*0Sstevel@tonic-gate  * routine:
302*0Sstevel@tonic-gate  *	lex
303*0Sstevel@tonic-gate  *
304*0Sstevel@tonic-gate  * purpose:
305*0Sstevel@tonic-gate  *	my own version of strtok that handles quoting and escaping
306*0Sstevel@tonic-gate  *
307*0Sstevel@tonic-gate  * parameters:
308*0Sstevel@tonic-gate  *	FILE structure for file to read (0 for same string, same file)
309*0Sstevel@tonic-gate  *
310*0Sstevel@tonic-gate  * returns:
311*0Sstevel@tonic-gate  *	pointer to next token
312*0Sstevel@tonic-gate  *
313*0Sstevel@tonic-gate  * notes:
314*0Sstevel@tonic-gate  *	this routine makes no changes to the string it is passed,
315*0Sstevel@tonic-gate  *	copying tokens into a static buffer.
316*0Sstevel@tonic-gate  *
317*0Sstevel@tonic-gate  *	this routine handles continuation lines after reading and
318*0Sstevel@tonic-gate  *	before the lexing even starts.  This limits continued lines
319*0Sstevel@tonic-gate  *	to a length of MAX_LINE, but keeps everything else very simple.
320*0Sstevel@tonic-gate  *	We also, therefore, limit tokens to a maximum length of MAX_LINE.
321*0Sstevel@tonic-gate  */
322*0Sstevel@tonic-gate int lex_linenum;		/* line number in current input file	*/
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate char *
lex(FILE * file)325*0Sstevel@tonic-gate lex(FILE *file)
326*0Sstevel@tonic-gate {	char c, delim;
327*0Sstevel@tonic-gate 	char *p;
328*0Sstevel@tonic-gate 	char *s;
329*0Sstevel@tonic-gate 	static char *savep;
330*0Sstevel@tonic-gate 	static char namebuf[ MAX_LINE ];
331*0Sstevel@tonic-gate 	static char inbuf[ MAX_LINE ];
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	if (file) {			/* read a new line		*/
334*0Sstevel@tonic-gate 		p = inbuf + sizeof (inbuf);
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 		/* read the next input line, with all continuations	*/
337*0Sstevel@tonic-gate 		for (s = inbuf; savep = fgets(s, p - s, file); ) {
338*0Sstevel@tonic-gate 			lex_linenum++;
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 			/* go find the last character of the input line	*/
341*0Sstevel@tonic-gate 			while (*s && s[1])
342*0Sstevel@tonic-gate 				s++;
343*0Sstevel@tonic-gate 			if (*s == '\n')
344*0Sstevel@tonic-gate 				s--;
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 			/* see whether or not we need a continuation	*/
347*0Sstevel@tonic-gate 			if (s < inbuf || *s != '\\')
348*0Sstevel@tonic-gate 				break;
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 			continue;
351*0Sstevel@tonic-gate 		}
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 		if (savep == 0)
354*0Sstevel@tonic-gate 			return (0);
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 		s = inbuf;
357*0Sstevel@tonic-gate 	} else {			/* continue with old line	*/
358*0Sstevel@tonic-gate 		if (savep == 0)
359*0Sstevel@tonic-gate 			return (0);
360*0Sstevel@tonic-gate 		s = savep;
361*0Sstevel@tonic-gate 	}
362*0Sstevel@tonic-gate 	savep = 0;
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	/* skip over leading white space	*/
365*0Sstevel@tonic-gate 	while (isspace(*s))
366*0Sstevel@tonic-gate 		s++;
367*0Sstevel@tonic-gate 	if (*s == 0)
368*0Sstevel@tonic-gate 		return (0);
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	/* see if this is a quoted string	*/
371*0Sstevel@tonic-gate 	c = *s;
372*0Sstevel@tonic-gate 	if (c == '\'' || c == '"') {
373*0Sstevel@tonic-gate 		delim = c;
374*0Sstevel@tonic-gate 		s++;
375*0Sstevel@tonic-gate 	} else
376*0Sstevel@tonic-gate 		delim = 0;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	/* copy the token into the buffer	*/
379*0Sstevel@tonic-gate 	for (p = namebuf; (c = *s) != 0; s++) {
380*0Sstevel@tonic-gate 		/* literal escape		*/
381*0Sstevel@tonic-gate 		if (c == '\\') {
382*0Sstevel@tonic-gate 			s++;
383*0Sstevel@tonic-gate 			*p++ = *s;
384*0Sstevel@tonic-gate 			continue;
385*0Sstevel@tonic-gate 		}
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 		/* closing delimiter		*/
388*0Sstevel@tonic-gate 		if (c == delim) {
389*0Sstevel@tonic-gate 			s++;
390*0Sstevel@tonic-gate 			break;
391*0Sstevel@tonic-gate 		}
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 		/* delimiting white space	*/
394*0Sstevel@tonic-gate 		if (delim == 0 && isspace(c))
395*0Sstevel@tonic-gate 			break;
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 		/* ordinary characters		*/
398*0Sstevel@tonic-gate 		*p++ = *s;
399*0Sstevel@tonic-gate 	}
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	/* remember where we left off		*/
403*0Sstevel@tonic-gate 	savep = *s ? s : 0;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	/* null terminate and return the buffer	*/
406*0Sstevel@tonic-gate 	*p = 0;
407*0Sstevel@tonic-gate 	return (namebuf);
408*0Sstevel@tonic-gate }
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate /*
411*0Sstevel@tonic-gate  * routine:
412*0Sstevel@tonic-gate  *	wildcards
413*0Sstevel@tonic-gate  *
414*0Sstevel@tonic-gate  * purpose:
415*0Sstevel@tonic-gate  *	determine whether or not there are any wild cards in a name
416*0Sstevel@tonic-gate  *
417*0Sstevel@tonic-gate  * parameters:
418*0Sstevel@tonic-gate  *	name to be checked
419*0Sstevel@tonic-gate  *
420*0Sstevel@tonic-gate  * returns:
421*0Sstevel@tonic-gate  *	true/false
422*0Sstevel@tonic-gate  *
423*0Sstevel@tonic-gate  * notes:
424*0Sstevel@tonic-gate  *	we use this to take shortcuts
425*0Sstevel@tonic-gate  */
426*0Sstevel@tonic-gate bool_t
wildcards(const char * name)427*0Sstevel@tonic-gate wildcards(const char *name)
428*0Sstevel@tonic-gate {	const char *s;
429*0Sstevel@tonic-gate 	int literal = 0;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	for (s = name; *s; s++)
432*0Sstevel@tonic-gate 		if (literal)
433*0Sstevel@tonic-gate 			switch (*s) {
434*0Sstevel@tonic-gate 				case '\'':	/* end of literal string */
435*0Sstevel@tonic-gate 					literal = 0;
436*0Sstevel@tonic-gate 					continue;
437*0Sstevel@tonic-gate 				case '\\':	/* escape next character */
438*0Sstevel@tonic-gate 					s++;
439*0Sstevel@tonic-gate 					continue;
440*0Sstevel@tonic-gate 			}
441*0Sstevel@tonic-gate 		else
442*0Sstevel@tonic-gate 			switch (*s) {
443*0Sstevel@tonic-gate 				case '\'':	/* literal string	*/
444*0Sstevel@tonic-gate 					literal = 1;
445*0Sstevel@tonic-gate 					continue;
446*0Sstevel@tonic-gate 				case '\\':	/* escape next character */
447*0Sstevel@tonic-gate 					s++;
448*0Sstevel@tonic-gate 					continue;
449*0Sstevel@tonic-gate 				case '*':
450*0Sstevel@tonic-gate 				case '[':
451*0Sstevel@tonic-gate 				case '{':
452*0Sstevel@tonic-gate 				case '?':
453*0Sstevel@tonic-gate 					/* any of these is a wild card	*/
454*0Sstevel@tonic-gate 					return (TRUE);
455*0Sstevel@tonic-gate 			}
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 	return (FALSE);
458*0Sstevel@tonic-gate }
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate /*
461*0Sstevel@tonic-gate  * routine:
462*0Sstevel@tonic-gate  *	cannonize
463*0Sstevel@tonic-gate  *
464*0Sstevel@tonic-gate  * purpose:
465*0Sstevel@tonic-gate  *	to compress redundant dots out of a path
466*0Sstevel@tonic-gate  *
467*0Sstevel@tonic-gate  * parameters:
468*0Sstevel@tonic-gate  *	file name in an editable buffer
469*0Sstevel@tonic-gate  *
470*0Sstevel@tonic-gate  * returns:
471*0Sstevel@tonic-gate  *	void
472*0Sstevel@tonic-gate  *
473*0Sstevel@tonic-gate  * notes:
474*0Sstevel@tonic-gate  *	because we compress the string in place, there is no danger
475*0Sstevel@tonic-gate  *	of our overflowing any fixed sized buffer.
476*0Sstevel@tonic-gate  */
477*0Sstevel@tonic-gate static void
cannonize(char * name)478*0Sstevel@tonic-gate cannonize(char *name)
479*0Sstevel@tonic-gate {	char *s, *p;
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	/* leading dot-slashes	*/
482*0Sstevel@tonic-gate 	for (s = name; *s == '.' && s[1] == '/'; strcpy(s, &s[2]));
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 	for (s = name; *s; s++) {
485*0Sstevel@tonic-gate 		/* interesting things happen after slashes	*/
486*0Sstevel@tonic-gate 		if (*s != '/')
487*0Sstevel@tonic-gate 			continue;
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 		/* embedded dot-slashes */
490*0Sstevel@tonic-gate 		while (s[1] == '.' && s[2] == '/')
491*0Sstevel@tonic-gate 			strcpy(&s[1], &s[3]);
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 		/* embedded slash-dot-dot-slash	*/
494*0Sstevel@tonic-gate 		if (strncmp(s, "/../", 4) == 0) {
495*0Sstevel@tonic-gate 			/* scan backwards to eliminate last directory */
496*0Sstevel@tonic-gate 			for (p = s-1; p > name && *p != '/'; p--);
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 			if (p < name)
499*0Sstevel@tonic-gate 				p = name;
500*0Sstevel@tonic-gate 			strcpy(p, &s[3]);
501*0Sstevel@tonic-gate 		}
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 		continue;
504*0Sstevel@tonic-gate 	}
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate /*
508*0Sstevel@tonic-gate  * routine:
509*0Sstevel@tonic-gate  *	prefix
510*0Sstevel@tonic-gate  *
511*0Sstevel@tonic-gate  * purpose:
512*0Sstevel@tonic-gate  *	determine whether or not one string begins with another
513*0Sstevel@tonic-gate  *
514*0Sstevel@tonic-gate  * parameters:
515*0Sstevel@tonic-gate  *	string to be tested
516*0Sstevel@tonic-gate  *	suspected prefix
517*0Sstevel@tonic-gate  *
518*0Sstevel@tonic-gate  * returns:
519*0Sstevel@tonic-gate  *	no	0
520*0Sstevel@tonic-gate  *	yes	pointer character after prefix
521*0Sstevel@tonic-gate  */
522*0Sstevel@tonic-gate const char *
prefix(const char * s,const char * p)523*0Sstevel@tonic-gate prefix(const char *s, const char *p)
524*0Sstevel@tonic-gate {
525*0Sstevel@tonic-gate 	while (*p)
526*0Sstevel@tonic-gate 		if (*p++ != *s++)
527*0Sstevel@tonic-gate 			return (0);
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 	return (s);
530*0Sstevel@tonic-gate }
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate /*
533*0Sstevel@tonic-gate  * routine:
534*0Sstevel@tonic-gate  *	suffix
535*0Sstevel@tonic-gate  *
536*0Sstevel@tonic-gate  * purpose:
537*0Sstevel@tonic-gate  *	determine whether or not one string ends with another
538*0Sstevel@tonic-gate  *
539*0Sstevel@tonic-gate  * parameters:
540*0Sstevel@tonic-gate  *	string to be tested
541*0Sstevel@tonic-gate  *	suspected suffix
542*0Sstevel@tonic-gate  *
543*0Sstevel@tonic-gate  * returns:
544*0Sstevel@tonic-gate  *	true/false
545*0Sstevel@tonic-gate  */
546*0Sstevel@tonic-gate bool_t
suffix(const char * str,const char * suf)547*0Sstevel@tonic-gate suffix(const char *str, const char *suf)
548*0Sstevel@tonic-gate {	const char *s;
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	/* go to where the alleged suffix would start */
551*0Sstevel@tonic-gate 	for (s = str; *s; s++);
552*0Sstevel@tonic-gate 	s -= strlen(suf);
553*0Sstevel@tonic-gate 	if (s < str)
554*0Sstevel@tonic-gate 		return (FALSE);
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	/* see if the string ends with the suffix */
557*0Sstevel@tonic-gate 	while (*suf)
558*0Sstevel@tonic-gate 		if (*suf++ != *s++)
559*0Sstevel@tonic-gate 			return (FALSE);
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	return (TRUE);
562*0Sstevel@tonic-gate }
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate /*
565*0Sstevel@tonic-gate  * routine:
566*0Sstevel@tonic-gate  *	contains
567*0Sstevel@tonic-gate  *
568*0Sstevel@tonic-gate  * purpose:
569*0Sstevel@tonic-gate  *	determine whether or not one string contains another
570*0Sstevel@tonic-gate  *
571*0Sstevel@tonic-gate  * parameters:
572*0Sstevel@tonic-gate  *	string to be checked
573*0Sstevel@tonic-gate  *	pattern we are seeking
574*0Sstevel@tonic-gate  *
575*0Sstevel@tonic-gate  * returns:
576*0Sstevel@tonic-gate  *	true/false
577*0Sstevel@tonic-gate  */
578*0Sstevel@tonic-gate bool_t
contains(const char * str,const char * pat)579*0Sstevel@tonic-gate contains(const char *str, const char *pat)
580*0Sstevel@tonic-gate {	const char *s, *p;
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	while (*str) {
583*0Sstevel@tonic-gate 		if (*str++ == *pat) {
584*0Sstevel@tonic-gate 			for (s = str, p = &pat[1]; *s == *p; s++, p++)
585*0Sstevel@tonic-gate 				if (p[1] == 0)
586*0Sstevel@tonic-gate 					return (TRUE);
587*0Sstevel@tonic-gate 		}
588*0Sstevel@tonic-gate 	}
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	return (FALSE);
591*0Sstevel@tonic-gate }
592