1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.misc.c,v 3.49 2015/05/04 15:31:13 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.49 2015/05/04 15:31:13 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
)
65 strnsave(const char *s
, size_t len
)
77 strsave(const char *s
)
101 blkpr(Char
*const *av
)
112 blkexpand(Char
*const *av
)
114 struct Strbuf buf
= Strbuf_INIT
;
117 Strbuf_append(&buf
, *av
);
119 Strbuf_append1(&buf
, ' ');
121 return Strbuf_finish(&buf
);
135 blkcpy(Char
**oav
, Char
**bv
)
139 while ((*av
++ = *bv
++) != NULL
)
145 blkcat(Char
**up
, Char
**vp
)
148 (void) blkcpy(blkend(up
), vp
);
165 blk_cleanup(void *ptr
)
171 blk_indirect_cleanup(void *xptr
)
183 Char
**newv
, **onewv
;
188 onewv
= newv
= xcalloc(blklen(v
) + 1, sizeof(Char
**));
191 *newv
++ = Strsave(*v
++);
197 strstr(const char *s
, const char *t
)
206 while (*ss
++ == *tt
++);
207 } while (*s
++ != '\0');
210 #endif /* !HAVE_STRSTR */
213 strspl(const char *cp
, const char *dp
)
224 ep
= xmalloc((cl
+ dl
+ 1) * sizeof(char));
226 memcpy(ep
+ cl
, dp
, dl
+ 1);
231 blkspl(Char
**up
, Char
**vp
)
233 Char
**wp
= xcalloc(blklen(up
) + blklen(vp
) + 1, sizeof(Char
**));
235 (void) blkcpy(wp
, up
);
236 return (blkcat(wp
, vp
));
253 * This routine is called after an error to close up
254 * any units which may have been left open accidentally.
264 #endif /* NLS_CATALOGS */
265 #endif /* NLS_BUGS */
267 /* suggested by Justin Bur; thanks to Karl Kleinpaste */
271 for (f
= 0; f
< num_files
; f
++)
272 if (f
!= SHIN
&& f
!= SHOUT
&& f
!= SHDIAG
&& f
!= OLDSTD
&&
276 #endif /* MALLOC_TRACE */
282 (void) xopen(_PATH_DEVNULL
, O_RDONLY
|O_LARGEFILE
);
288 #endif /* NLS_CATALOGS */
289 #endif /* NLS_BUGS */
292 #ifndef CLOSE_ON_EXEC
294 * Close files before executing a file.
295 * We could be MUCH more intelligent, since (on a version 7 system)
296 * we need only close files here during a source, the other
297 * shell fd's being in units 16-19 which are closed automatically!
311 isoutatty
= isatty(SHOUT
);
312 isdiagatty
= isatty(SHDIAG
);
314 for (f
= 3; f
< num_files
; f
++)
318 #endif /* CLOSE_ON_EXEC */
330 int fd
= xopen(_PATH_DEVNULL
, O_RDONLY
|O_LARGEFILE
);
339 * Move descriptor i to j.
340 * If j is -1 then we just want to get i to a safe place,
341 * i.e. to a unit > FSAFE. This also happens in dcopy.
367 if (i
== j
|| i
< 0 || (j
< 0 && i
> FSAFE
))
377 return (renum(i
, j
));
387 if (j
== -1 && k
> FSAFE
)
398 * Left shift a command argument list, discarding
399 * the first c arguments. Used in "shift" commands
400 * as well as by commands like "repeat".
403 lshift(Char
**v
, int c
)
407 for (u
= v
; *u
&& --c
>= 0; u
++)
423 while (*cp
&& Isdigit(*cp
))
431 Char
**nv
= xcalloc(blklen(v
) + 1, sizeof(Char
**));
433 return (blkcpy(nv
, v
));
437 strend(const char *cp
)
440 return ((char *)(intptr_t)cp
);
443 return ((char *)(intptr_t)cp
);
453 while ((*dp
++ &= TRIM
) != '\0')
471 quote_meta(struct Strbuf
*buf
, const Char
*s
)
475 if (cmap(*s
, _META
| _DOL
| _QF
| _QB
| _ESC
| _GLOB
))
476 Strbuf_append1(buf
, '\\');
477 Strbuf_append1(buf
, *s
++);
479 Strbuf_terminate(buf
);
486 setname(short2str(name
));
487 stderror(ERR_NAME
| ERR_UNDVAR
);
491 prefix(const Char
*sub
, const Char
*str
)
499 if ((*sub
++ & TRIM
) != (*str
++ & TRIM
))
505 areadlink(const char *path
)
511 size
= MAXPATHLEN
+ 1;
513 while ((size_t)(res
= readlink(path
, buf
, size
)) == size
) {
515 buf
= xrealloc(buf
, size
);
526 return xrealloc(buf
, res
+ 1);
528 #endif /*!WINNT_NATIVE*/
535 while (close(fildes
) == -1 && errno
== EINTR
)
536 if (handle_pending_signals())
543 while (closedir(dirp
) == -1 && errno
== EINTR
)
544 if (handle_pending_signals())
549 xcreat(const char *path
, mode_t mode
)
553 while ((res
= creat(path
, mode
)) == -1 && errno
== EINTR
)
554 if (handle_pending_signals())
561 xdup2(int fildes
, int fildes2
)
565 while ((res
= dup2(fildes
, fildes2
)) == -1 && errno
== EINTR
)
566 if (handle_pending_signals())
573 xgetgrgid(gid_t xgid
)
578 while ((res
= getgrgid(xgid
)) == NULL
&& errno
== EINTR
) {
579 if (handle_pending_signals())
587 xgetpwnam(const char *name
)
592 while ((res
= getpwnam(name
)) == NULL
&& errno
== EINTR
) {
593 if (handle_pending_signals())
601 xgetpwuid(uid_t xuid
)
606 while ((res
= getpwuid(xuid
)) == NULL
&& errno
== EINTR
) {
607 if (handle_pending_signals())
615 xopen(const char *path
, int oflag
, ...)
619 if ((oflag
& O_CREAT
) == 0) {
620 while ((res
= open(path
, oflag
)) == -1 && errno
== EINTR
)
621 if (handle_pending_signals())
628 /* "int" should actually be "mode_t after default argument
629 promotions". "int" is the best guess we have, "mode_t" used to be
630 "unsigned short", which we obviously can't use. */
631 mode
= va_arg(ap
, int);
633 while ((res
= open(path
, oflag
, mode
)) == -1 && errno
== EINTR
)
634 if (handle_pending_signals())
641 xread(int fildes
, void *buf
, size_t nbyte
)
645 /* This is where we will be blocked most of the time, so handle signals
646 that didn't interrupt any system call. */
648 if (handle_pending_signals())
650 while ((res
= read(fildes
, buf
, nbyte
)) == -1 && errno
== EINTR
);
656 xtcsetattr(int fildes
, int optional_actions
, const struct termios
*termios_p
)
660 while ((res
= tcsetattr(fildes
, optional_actions
, termios_p
)) == -1 &&
662 if (handle_pending_signals())
669 xwrite(int fildes
, const void *buf
, size_t nbyte
)
673 /* This is where we will be blocked most of the time, so handle signals
674 that didn't interrupt any system call. */
676 if (handle_pending_signals())
678 while ((res
= write(fildes
, buf
, nbyte
)) == -1 && errno
== EINTR
);