1 /* $NetBSD: misc.c,v 1.18 2006/03/17 22:48:10 christos Exp $ */
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: misc.c,v 1.18 2006/03/17 22:48:10 christos Exp $");
41 #include <sys/param.h>
52 static int renum(int, int);
55 any(const char *s
, int c
)
58 return (0); /* Check for nil pointer */
66 strsave(const char *s
)
75 r
= p
= (char *)xmalloc((size_t)((n
- s
) * sizeof(char)));
76 while ((*p
++ = *s
++) != '\0')
91 blkpr(FILE *fp
, Char
**av
)
94 (void)fprintf(fp
, "%s", vis_str(*av
));
96 (void)fprintf(fp
, " ");
112 blkcpy(Char
**oav
, Char
**bv
)
117 while ((*av
++ = *bv
++) != NULL
)
123 blkcat(Char
**up
, Char
**vp
)
125 (void)blkcpy(blkend(up
), vp
);
145 Char
**newv
, **onewv
;
150 newv
= (Char
**)xcalloc((size_t)(blklen(v
) + 1), sizeof(Char
**));
153 *newv
++ = Strsave(*v
++);
159 strstr(char *s
, char *t
)
171 while (*ss
++ == *tt
++);
172 } while (*s
++ != '\0');
178 #ifndef SHORT_STRINGS
180 strspl(char *cp
, char *dp
)
192 ep
= (char *) xmalloc((size_t)(((p
- cp
) + (q
- dp
) - 1) * sizeof(char)));
193 for (p
= ep
, q
= cp
; *p
++ = *q
++;)
195 for (p
--, q
= dp
; *p
++ = *q
++;)
203 blkspl(Char
**up
, Char
**vp
)
207 wp
= (Char
**)xcalloc((size_t)(blklen(up
) + blklen(vp
) + 1),
209 (void)blkcpy(wp
, up
);
210 return (blkcat(wp
, vp
));
226 * This routine is called after an error to close up
227 * any units which may have been left open accidentally.
236 nofile
= FOLDSTD
+ 1;
237 if (fcntl(nofile
, F_CLOSEM
, 0) == -1)
241 for (f
= 0; f
< nofile
; f
++)
242 if (f
!= SHIN
&& f
!= SHOUT
&& f
!= SHERR
&& f
!= OLDSTD
&&
258 * Move descriptor i to j.
259 * If j is -1 then we just want to get i to a safe place,
260 * i.e. to a unit > 2. This also happens in dcopy.
282 if (i
== j
|| i
< 0 || (j
< 0 && i
> 2))
288 return (renum(i
, j
));
299 if (j
== -1 && k
> 2)
310 * Left shift a command argument list, discarding
311 * the first c arguments. Used in "shift" commands
312 * as well as by commands like "repeat".
315 lshift(Char
**v
, int c
)
319 for (u
= v
; *u
&& --c
>= 0; u
++)
335 while (*cp
&& Isdigit(*cp
))
345 nv
= (Char
**)xcalloc((size_t)(blklen(v
) + 1), sizeof(Char
**));
347 return (blkcpy(nv
, v
));
350 #ifndef SHORT_STRINGS
361 #endif /* SHORT_STRINGS */
371 while ((*dp
++ &= TRIM
) != '\0')
392 setname(vis_str(name
));
393 stderror(ERR_NAME
| ERR_UNDVAR
);
398 prefix(Char
*sub
, Char
*str
)
405 if (*sub
++ != *str
++)