1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.misc.c,v 3.45 2006/10/14 17:57:21 christos Exp $ */
3 * sh.misc.c: Miscelaneous functions
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. 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.
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
35 RCSID("$tcsh: sh.misc.c,v 3.45 2006/10/14 17:57:21 christos Exp $")
37 static int renum (int, int);
38 static Char
**blkend (Char
**);
39 static Char
**blkcat (Char
**, Char
**);
40 static int xdup2 (int, int);
47 any(const char *s
, Char c
)
50 return (0); /* Check for nil pointer */
58 setzero(void *p
, size_t size
)
64 strnsave(const char *s
, size_t len
)
75 strsave(const char *s
)
99 blkpr(Char
*const *av
)
110 blkexpand(Char
*const *av
)
112 struct Strbuf buf
= Strbuf_INIT
;
115 Strbuf_append(&buf
, *av
);
117 Strbuf_append1(&buf
, ' ');
119 return Strbuf_finish(&buf
);
133 blkcpy(Char
**oav
, Char
**bv
)
137 while ((*av
++ = *bv
++) != NULL
)
143 blkcat(Char
**up
, Char
**vp
)
146 (void) blkcpy(blkend(up
), vp
);
163 blk_cleanup(void *ptr
)
169 blk_indirect_cleanup(void *xptr
)
181 Char
**newv
, **onewv
;
186 onewv
= newv
= xcalloc(blklen(v
) + 1, sizeof(Char
**));
189 *newv
++ = Strsave(*v
++);
195 strstr(const char *s
, const char *t
)
204 while (*ss
++ == *tt
++);
205 } while (*s
++ != '\0');
208 #endif /* !HAVE_STRSTR */
211 strspl(const char *cp
, const char *dp
)
222 ep
= xmalloc((cl
+ dl
+ 1) * sizeof(char));
224 memcpy(ep
+ cl
, dp
, dl
+ 1);
229 blkspl(Char
**up
, Char
**vp
)
231 Char
**wp
= xcalloc(blklen(up
) + blklen(vp
) + 1, sizeof(Char
**));
233 (void) blkcpy(wp
, up
);
234 return (blkcat(wp
, vp
));
251 * This routine is called after an error to close up
252 * any units which may have been left open accidentally.
262 #endif /* NLS_CATALOGS */
263 #endif /* NLS_BUGS */
265 /* suggested by Justin Bur; thanks to Karl Kleinpaste */
269 for (f
= 0; f
< num_files
; f
++)
270 if (f
!= SHIN
&& f
!= SHOUT
&& f
!= SHDIAG
&& f
!= OLDSTD
&&
274 #endif /* MALLOC_TRACE */
280 (void) xopen(_PATH_DEVNULL
, O_RDONLY
|O_LARGEFILE
);
286 #endif /* NLS_CATALOGS */
287 #endif /* NLS_BUGS */
290 #ifndef CLOSE_ON_EXEC
292 * Close files before executing a file.
293 * We could be MUCH more intelligent, since (on a version 7 system)
294 * we need only close files here during a source, the other
295 * shell fd's being in units 16-19 which are closed automatically!
309 isoutatty
= isatty(SHOUT
);
310 isdiagatty
= isatty(SHDIAG
);
312 for (f
= 3; f
< num_files
; f
++)
316 #endif /* CLOSE_ON_EXEC */
328 int fd
= xopen(_PATH_DEVNULL
, O_RDONLY
|O_LARGEFILE
);
337 * Move descriptor i to j.
338 * If j is -1 then we just want to get i to a safe place,
339 * i.e. to a unit > FSAFE. This also happens in dcopy.
365 if (i
== j
|| i
< 0 || (j
< 0 && i
> FSAFE
))
375 return (renum(i
, j
));
385 if (j
== -1 && k
> FSAFE
)
396 * Left shift a command argument list, discarding
397 * the first c arguments. Used in "shift" commands
398 * as well as by commands like "repeat".
401 lshift(Char
**v
, int c
)
405 for (u
= v
; *u
&& --c
>= 0; u
++)
421 while (*cp
&& Isdigit(*cp
))
429 Char
**nv
= xcalloc(blklen(v
) + 1, sizeof(Char
**));
431 return (blkcpy(nv
, v
));
435 strend(const char *cp
)
438 return ((char *)(intptr_t)cp
);
441 return ((char *)(intptr_t)cp
);
451 while ((*dp
++ &= TRIM
) != '\0')
469 quote_meta(struct Strbuf
*buf
, const Char
*s
)
473 if (cmap(*s
, _META
| _DOL
| _QF
| _QB
| _ESC
| _GLOB
))
474 Strbuf_append1(buf
, '\\');
475 Strbuf_append1(buf
, *s
++);
477 Strbuf_terminate(buf
);
484 setname(short2str(name
));
485 stderror(ERR_NAME
| ERR_UNDVAR
);
489 prefix(const Char
*sub
, const Char
*str
)
497 if ((*sub
++ & TRIM
) != (*str
++ & TRIM
))
503 areadlink(const char *path
)
509 size
= MAXPATHLEN
+ 1;
511 while ((size_t)(res
= readlink(path
, buf
, size
)) == size
) {
513 buf
= xrealloc(buf
, size
);
524 return xrealloc(buf
, res
+ 1);
526 #endif /*!WINNT_NATIVE*/
533 while (close(fildes
) == -1 && errno
== EINTR
)
534 handle_pending_signals();
540 while (closedir(dirp
) == -1 && errno
== EINTR
)
541 handle_pending_signals();
545 xcreat(const char *path
, mode_t mode
)
549 while ((res
= creat(path
, mode
)) == -1 && errno
== EINTR
)
550 handle_pending_signals();
556 xdup2(int fildes
, int fildes2
)
560 while ((res
= dup2(fildes
, fildes2
)) == -1 && errno
== EINTR
)
561 handle_pending_signals();
567 xgetgrgid(gid_t xgid
)
572 while ((res
= getgrgid(xgid
)) == NULL
&& errno
== EINTR
) {
573 handle_pending_signals();
580 xgetpwnam(const char *name
)
585 while ((res
= getpwnam(name
)) == NULL
&& errno
== EINTR
) {
586 handle_pending_signals();
593 xgetpwuid(uid_t xuid
)
598 while ((res
= getpwuid(xuid
)) == NULL
&& errno
== EINTR
) {
599 handle_pending_signals();
606 xopen(const char *path
, int oflag
, ...)
610 if ((oflag
& O_CREAT
) == 0) {
611 while ((res
= open(path
, oflag
)) == -1 && errno
== EINTR
)
612 handle_pending_signals();
618 /* "int" should actually be "mode_t after default argument
619 promotions". "int" is the best guess we have, "mode_t" used to be
620 "unsigned short", which we obviously can't use. */
621 mode
= va_arg(ap
, int);
623 while ((res
= open(path
, oflag
, mode
)) == -1 && errno
== EINTR
)
624 handle_pending_signals();
630 xread(int fildes
, void *buf
, size_t nbyte
)
634 /* This is where we will be blocked most of the time, so handle signals
635 that didn't interrupt any system call. */
637 handle_pending_signals();
638 while ((res
= read(fildes
, buf
, nbyte
)) == -1 && errno
== EINTR
);
644 xtcsetattr(int fildes
, int optional_actions
, const struct termios
*termios_p
)
648 while ((res
= tcsetattr(fildes
, optional_actions
, termios_p
)) == -1 &&
650 handle_pending_signals();
656 xwrite(int fildes
, const void *buf
, size_t nbyte
)
660 /* This is where we will be blocked most of the time, so handle signals
661 that didn't interrupt any system call. */
663 handle_pending_signals();
664 while ((res
= write(fildes
, buf
, nbyte
)) == -1 && errno
== EINTR
);