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