]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/csh/misc.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / bin / csh / misc.c
1 /*-
2  * Copyright (c) 1980, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)misc.c      8.1 (Berkeley) 5/31/93";
37 #else
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif
41 #endif /* not lint */
42
43 #include <sys/param.h>
44 #include <unistd.h>
45 #if __STDC__
46 # include <stdarg.h>
47 #else
48 # include <varargs.h>
49 #endif
50
51 #include "csh.h"
52 #include "extern.h"
53
54 static int      renum __P((int, int));
55
56 int
57 any(s, c)
58     char *s;
59     int c;
60 {
61     if (!s)
62         return (0);             /* Check for nil pointer */
63     while (*s)
64         if (*s++ == c)
65             return (1);
66     return (0);
67 }
68
69 void
70 setzero(cp, i)
71     char   *cp;
72     int     i;
73 {
74     if (i != 0)
75         do
76             *cp++ = 0;
77         while (--i);
78 }
79
80 char   *
81 strsave(s)
82     char *s;
83 {
84     char   *n;
85     char *p;
86
87     if (s == NULL)
88         s = "";
89     for (p = s; *p++;)
90         continue;
91     n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
92     while ((*p++ = *s++) != '\0')
93         continue;
94     return (n);
95 }
96
97 Char  **
98 blkend(up)
99     Char **up;
100 {
101
102     while (*up)
103         up++;
104     return (up);
105 }
106
107
108 void
109 blkpr(fp, av)
110     FILE *fp;
111     Char **av;
112 {
113
114     for (; *av; av++) {
115         (void) fprintf(fp, "%s", vis_str(*av));
116         if (av[1])
117             (void) fprintf(fp, " ");
118     }
119 }
120
121 int
122 blklen(av)
123     Char **av;
124 {
125     int i = 0;
126
127     while (*av++)
128         i++;
129     return (i);
130 }
131
132 Char  **
133 blkcpy(oav, bv)
134     Char  **oav;
135     Char **bv;
136 {
137     Char **av = oav;
138
139     while ((*av++ = *bv++) != NULL)
140         continue;
141     return (oav);
142 }
143
144 Char  **
145 blkcat(up, vp)
146     Char  **up, **vp;
147 {
148
149     (void) blkcpy(blkend(up), vp);
150     return (up);
151 }
152
153 void
154 blkfree(av0)
155     Char  **av0;
156 {
157     Char **av = av0;
158
159     if (!av0)
160         return;
161     for (; *av; av++)
162         xfree((ptr_t) * av);
163     xfree((ptr_t) av0);
164 }
165
166 Char  **
167 saveblk(v)
168     Char **v;
169 {
170     Char **newv =
171     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
172     Char  **onewv = newv;
173
174     while (*v)
175         *newv++ = Strsave(*v++);
176     return (onewv);
177 }
178
179 #ifdef NOTUSED
180 char   *
181 strstr(s, t)
182     char *s, *t;
183 {
184     do {
185         char *ss = s;
186         char *tt = t;
187
188         do
189             if (*tt == '\0')
190                 return (s);
191         while (*ss++ == *tt++);
192     } while (*s++ != '\0');
193     return (NULL);
194 }
195
196 #endif /* NOTUSED */
197
198 #ifndef SHORT_STRINGS
199 char   *
200 strspl(cp, dp)
201     char   *cp, *dp;
202 {
203     char   *ep;
204     char *p, *q;
205
206     if (!cp)
207         cp = "";
208     if (!dp)
209         dp = "";
210     for (p = cp; *p++;)
211         continue;
212     for (q = dp; *q++;)
213         continue;
214     ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
215     for (p = ep, q = cp; *p++ = *q++;)
216         continue;
217     for (p--, q = dp; *p++ = *q++;)
218         continue;
219     return (ep);
220 }
221
222 #endif
223
224 Char  **
225 blkspl(up, vp)
226     Char **up, **vp;
227 {
228     Char **wp =
229     (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
230                       sizeof(Char **));
231
232     (void) blkcpy(wp, up);
233     return (blkcat(wp, vp));
234 }
235
236 Char
237 lastchr(cp)
238     Char *cp;
239 {
240
241     if (!cp)
242         return (0);
243     if (!*cp)
244         return (0);
245     while (cp[1])
246         cp++;
247     return (*cp);
248 }
249
250 /*
251  * This routine is called after an error to close up
252  * any units which may have been left open accidentally.
253  */
254 void
255 closem()
256 {
257     int f, flimit;
258
259     for (f = 0, flimit = getdtablesize(); f < flimit; f++)
260         if (f != SHIN && f != SHOUT && f != SHERR && f != OLDSTD &&
261             f != FSHTTY)
262             (void) close(f);
263 }
264
265 void
266 donefds()
267 {
268     (void) close(0);
269     (void) close(1);
270     (void) close(2);
271
272     didfds = 0;
273 }
274
275 /*
276  * Move descriptor i to j.
277  * If j is -1 then we just want to get i to a safe place,
278  * i.e. to a unit > 2.  This also happens in dcopy.
279  */
280 int
281 dmove(i, j)
282     int i, j;
283 {
284
285     if (i == j || i < 0)
286         return (i);
287     if (j >= 0) {
288         (void) dup2(i, j);
289         if (j != i)
290             (void) close(i);
291         return (j);
292     }
293     j = dcopy(i, j);
294     if (j != i)
295         (void) close(i);
296     return (j);
297 }
298
299 int
300 dcopy(i, j)
301     int i, j;
302 {
303
304     if (i == j || i < 0 || (j < 0 && i > 2))
305         return (i);
306     if (j >= 0) {
307         (void) dup2(i, j);
308         return (j);
309     }
310     (void) close(j);
311     return (renum(i, j));
312 }
313
314 static int
315 renum(i, j)
316     int i, j;
317 {
318     int k = dup(i);
319
320     if (k < 0)
321         return (-1);
322     if (j == -1 && k > 2)
323         return (k);
324     if (k != j) {
325         j = renum(k, j);
326         (void) close(k);
327         return (j);
328     }
329     return (k);
330 }
331
332 /*
333  * Left shift a command argument list, discarding
334  * the first c arguments.  Used in "shift" commands
335  * as well as by commands like "repeat".
336  */
337 void
338 lshift(v, c)
339     Char **v;
340     int c;
341 {
342     Char **u;
343
344     for (u = v; *u && --c >= 0; u++)
345         xfree((ptr_t) *u);
346     (void) blkcpy(v, u);
347 }
348
349 int
350 number(cp)
351     Char   *cp;
352 {
353     if (!cp)
354         return(0);
355     if (*cp == '-') {
356         cp++;
357         if (!Isdigit(*cp))
358             return (0);
359         cp++;
360     }
361     while (*cp && Isdigit(*cp))
362         cp++;
363     return (*cp == 0);
364 }
365
366 Char  **
367 copyblk(v)
368     Char **v;
369 {
370     Char  **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
371
372     return (blkcpy(nv, v));
373 }
374
375 #ifndef SHORT_STRINGS
376 char   *
377 strend(cp)
378     char *cp;
379 {
380     if (!cp)
381         return (cp);
382     while (*cp)
383         cp++;
384     return (cp);
385 }
386
387 #endif /* SHORT_STRINGS */
388
389 Char   *
390 strip(cp)
391     Char   *cp;
392 {
393     Char *dp = cp;
394
395     if (!cp)
396         return (cp);
397     while ((*dp++ &= TRIM) != '\0')
398         continue;
399     return (cp);
400 }
401
402 void
403 udvar(name)
404     Char   *name;
405 {
406
407     setname(vis_str(name));
408     stderror(ERR_NAME | ERR_UNDVAR);
409 }
410
411 int
412 prefix(sub, str)
413     Char *sub, *str;
414 {
415
416     for (;;) {
417         if (*sub == 0)
418             return (1);
419         if (*str == 0)
420             return (0);
421         if (*sub++ != *str++)
422             return (0);
423     }
424 }