dhcpcd: update README.DRAGONFLY
[dragonfly.git] / contrib / tcsh-6 / sh.misc.c
blob0b6b3370934c0ed8be3413f7efa505b88740040b
1 /*
2 * sh.misc.c: Miscelaneous functions
3 */
4 /*-
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 #include "sh.h"
34 static int renum (int, int);
35 static Char **blkend (Char **);
36 static Char **blkcat (Char **, Char **);
37 static int xdup2 (int, int);
40 * C Shell
43 int
44 any(const char *s, Char c)
46 if (!s)
47 return (0); /* Check for nil pointer */
48 while (*s)
49 if ((Char)*s++ == c)
50 return (1);
51 return (0);
54 void
55 setzero(void *p, size_t size)
57 memset(p, 0, size);
60 #ifndef SHORT_STRINGS
61 char *
62 strnsave(const char *s, size_t len)
64 char *r;
66 r = xmalloc(len + 1);
67 memcpy(r, s, len);
68 r[len] = '\0';
69 return r;
71 #endif
73 char *
74 strsave(const char *s)
76 char *r;
77 size_t size;
79 if (s == NULL)
80 s = "";
81 size = strlen(s) + 1;
82 r = xmalloc(size);
83 memcpy(r, s, size);
84 return (r);
87 static Char **
88 blkend(Char **up)
91 while (*up)
92 up++;
93 return (up);
97 void
98 blkpr(Char *const *av)
101 for (; *av; av++) {
102 xprintf("%" TCSH_S, *av);
103 if (av[1])
104 xprintf(" ");
108 Char *
109 blkexpand(Char *const *av)
111 struct Strbuf buf = Strbuf_INIT;
113 for (; *av; av++) {
114 Strbuf_append(&buf, *av);
115 if (av[1])
116 Strbuf_append1(&buf, ' ');
118 return Strbuf_finish(&buf);
122 blklen(Char **av)
124 int i = 0;
126 while (*av++)
127 i++;
128 return (i);
131 Char **
132 blkcpy(Char **oav, Char **bv)
134 Char **av = oav;
136 while ((*av++ = *bv++) != NULL)
137 continue;
138 return (oav);
141 static Char **
142 blkcat(Char **up, Char **vp)
145 (void) blkcpy(blkend(up), vp);
146 return (up);
149 void
150 blkfree(Char **av0)
152 Char **av = av0;
154 if (!av0)
155 return;
156 for (; *av; av++)
157 xfree(*av);
158 xfree(av0);
161 void
162 blk_cleanup(void *ptr)
164 blkfree(ptr);
167 void
168 blk_indirect_cleanup(void *xptr)
170 Char ***ptr;
172 ptr = xptr;
173 blkfree(*ptr);
174 xfree(ptr);
177 Char **
178 saveblk(Char **v)
180 Char **newv, **onewv;
182 if (v == NULL)
183 return NULL;
185 onewv = newv = xcalloc(blklen(v) + 1, sizeof(Char **));
187 while (*v)
188 *newv++ = Strsave(*v++);
189 return (onewv);
192 #ifndef HAVE_STRSTR
193 char *
194 strstr(const char *s, const char *t)
196 do {
197 const char *ss = s;
198 const char *tt = t;
201 if (*tt == '\0')
202 return (s);
203 while (*ss++ == *tt++);
204 } while (*s++ != '\0');
205 return (NULL);
207 #endif /* !HAVE_STRSTR */
209 char *
210 strspl(const char *cp, const char *dp)
212 char *ep;
213 size_t cl, dl;
215 if (!cp)
216 cp = "";
217 if (!dp)
218 dp = "";
219 cl = strlen(cp);
220 dl = strlen(dp);
221 ep = xmalloc((cl + dl + 1) * sizeof(char));
222 memcpy(ep, cp, cl);
223 memcpy(ep + cl, dp, dl + 1);
224 return (ep);
227 Char **
228 blkspl(Char **up, Char **vp)
230 Char **wp = xcalloc(blklen(up) + blklen(vp) + 1, sizeof(Char **));
232 (void) blkcpy(wp, up);
233 return (blkcat(wp, vp));
236 Char
237 lastchr(Char *cp)
240 if (!cp)
241 return (0);
242 if (!*cp)
243 return (0);
244 while (cp[1])
245 cp++;
246 return (*cp);
250 * This routine is called after an error to close up
251 * any units which may have been left open accidentally.
253 void
254 closem(void)
256 int f, num_files;
257 #ifdef S_ISSOCK
258 struct stat st;
259 #endif /*S_ISSOCK*/
261 #ifdef NLS_BUGS
262 #ifdef NLS_CATALOGS
263 nlsclose();
264 #endif /* NLS_CATALOGS */
265 #endif /* NLS_BUGS */
266 #ifdef YPBUGS
267 /* suggested by Justin Bur; thanks to Karl Kleinpaste */
268 fix_yp_bugs();
269 #endif /* YPBUGS */
270 num_files = NOFILE;
271 for (f = 0; f < num_files; f++)
272 if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
273 f != FSHTTY
274 #ifdef MALLOC_TRACE
275 && f != 25
276 #endif /* MALLOC_TRACE */
277 #ifdef S_ISSOCK
278 /* NSS modules (e.g. Linux nss_ldap) might keep sockets open.
279 * If we close such a socket, both the NSS module and tcsh think
280 * they "own" the descriptor.
282 * Not closing sockets does not make the cleanup use of closem()
283 * less reliable because tcsh never creates sockets.
285 && fstat(f, &st) == 0 && !S_ISSOCK(st.st_mode)
286 #endif
289 xclose(f);
290 #ifdef NISPLUS
291 if (f < 3)
292 (void) xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
293 #endif /* NISPLUS */
295 #ifdef NLS_BUGS
296 #ifdef NLS_CATALOGS
297 nlsinit();
298 #endif /* NLS_CATALOGS */
299 #endif /* NLS_BUGS */
302 #ifndef CLOSE_ON_EXEC
304 * Close files before executing a file.
305 * We could be MUCH more intelligent, since (on a version 7 system)
306 * we need only close files here during a source, the other
307 * shell fd's being in units 16-19 which are closed automatically!
309 void
310 closech(void)
312 int f, num_files;
314 if (didcch)
315 return;
316 didcch = 1;
317 SHIN = 0;
318 SHOUT = 1;
319 SHDIAG = 2;
320 OLDSTD = 0;
321 isoutatty = isatty(SHOUT);
322 isdiagatty = isatty(SHDIAG);
323 num_files = NOFILE;
324 for (f = 3; f < num_files; f++)
325 xclose(f);
328 #endif /* CLOSE_ON_EXEC */
330 void
331 donefds(void)
334 xclose(0);
335 xclose(1);
336 xclose(2);
337 didfds = 0;
338 #ifdef NISPLUS
340 int fd = xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
341 (void)dcopy(fd, 1);
342 (void)dcopy(fd, 2);
343 (void)dmove(fd, 0);
345 #endif /*NISPLUS*/
349 * Move descriptor i to j.
350 * If j is -1 then we just want to get i to a safe place,
351 * i.e. to a unit > FSAFE. This also happens in dcopy.
354 dmove(int i, int j)
357 if (i == j || i < 0)
358 return (i);
359 #ifdef HAVE_DUP2
360 if (j >= 0) {
361 (void) xdup2(i, j);
362 if (j != i)
363 xclose(i);
364 return (j);
366 #endif
367 j = dcopy(i, j);
368 if (j != i)
369 xclose(i);
370 return (j);
374 dcopy(int i, int j)
377 if (i == j || i < 0 || (j < 0 && i > FSAFE))
378 return (i);
379 if (j >= 0) {
380 #ifdef HAVE_DUP2
381 (void) xdup2(i, j);
382 return (j);
383 #else
384 xclose(j);
385 #endif
387 return (renum(i, j));
390 static int
391 renum(int i, int j)
393 int k = dup(i);
395 if (k < 0)
396 return (-1);
397 if (j == -1 && k > FSAFE)
398 return (k);
399 if (k != j) {
400 j = renum(k, j);
401 xclose(k);
402 return (j);
404 return (k);
408 * Left shift a command argument list, discarding
409 * the first c arguments. Used in "shift" commands
410 * as well as by commands like "repeat".
412 void
413 lshift(Char **v, int c)
415 Char **u;
417 for (u = v; *u && --c >= 0; u++)
418 xfree(*u);
419 (void) blkcpy(v, u);
423 number(Char *cp)
425 if (!cp)
426 return (0);
427 if (*cp == '-') {
428 cp++;
429 if (!Isdigit(*cp))
430 return (0);
431 cp++;
433 while (*cp && Isdigit(*cp))
434 cp++;
435 return (*cp == 0);
438 Char **
439 copyblk(Char **v)
441 Char **nv = xcalloc(blklen(v) + 1, sizeof(Char **));
443 return (blkcpy(nv, v));
446 char *
447 strend(const char *cp)
449 if (!cp)
450 return ((char *)(intptr_t)cp);
451 while (*cp)
452 cp++;
453 return ((char *)(intptr_t)cp);
456 Char *
457 strip(Char *cp)
459 Char *dp = cp;
461 if (!cp)
462 return (cp);
463 while (*dp != '\0') {
464 #if INVALID_BYTE != 0
465 if ((*dp & INVALID_BYTE) != INVALID_BYTE) /* *dp < INVALID_BYTE */
466 #endif
467 *dp &= TRIM;
468 dp++;
470 return (cp);
473 Char *
474 quote(Char *cp)
476 Char *dp = cp;
478 if (!cp)
479 return (cp);
480 while (*dp != '\0') {
481 #ifdef WIDE_STRINGS
482 if ((*dp & 0xffffff80) == 0) /* *dp < 0x80 */
483 #elif defined SHORT_STRINGS
484 if ((*dp & 0xff80) == 0) /* *dp < 0x80 */
485 #else
486 if ((*dp & 0x80) == 0) /* *dp < 0x80 */
487 #endif
488 *dp |= QUOTE;
489 dp++;
491 return (cp);
494 const Char *
495 quote_meta(struct Strbuf *buf, const Char *s)
497 buf->len = 0;
498 while (*s != '\0') {
499 if (cmap(*s, _META | _DOL | _QF | _QB | _ESC | _GLOB))
500 Strbuf_append1(buf, '\\');
501 Strbuf_append1(buf, *s++);
503 Strbuf_terminate(buf);
504 return buf->s;
507 void
508 udvar(Char *name)
510 setname(short2str(name));
511 stderror(ERR_NAME | ERR_UNDVAR);
515 prefix(const Char *sub, const Char *str)
518 for (;;) {
519 if (*sub == 0)
520 return (1);
521 if (*str == 0)
522 return (0);
523 if ((*sub++ & TRIM) != (*str++ & TRIM))
524 return (0);
527 #ifndef WINNT_NATIVE
528 char *
529 areadlink(const char *path)
531 char *buf;
532 size_t size;
533 ssize_t res;
534 #ifdef __IBMC__
536 * Prevent infinite recursion. Someone should tell me how to expand
537 * these...
539 size_t i;
540 static const char *vars[] = {
541 "/$VERSION",
542 "/$SYSNAME",
543 "/$SYSSYMR",
544 "/$SYSSYMA",
546 for (i = 0; i < sizeof(vars) / sizeof(vars[0]); i++) {
547 if (strcmp(vars[i], path) == 0) {
548 return NULL;
551 #endif
554 size = MAXPATHLEN + 1;
555 buf = xmalloc(size);
556 while ((size_t)(res = readlink(path, buf, size)) == size) {
557 size *= 2;
558 buf = xrealloc(buf, size);
560 if (res == -1) {
561 int err;
563 err = errno;
564 xfree(buf);
565 errno = err;
566 return NULL;
568 buf[res] = '\0';
569 return xrealloc(buf, res + 1);
571 #endif /*!WINNT_NATIVE*/
573 void
574 xclose(int fildes)
576 if (fildes < 0)
577 return;
578 while (close(fildes) == -1 && errno == EINTR)
579 if (handle_pending_signals())
580 break;
583 void
584 xclosedir(DIR *dirp)
586 while (closedir(dirp) == -1 && errno == EINTR)
587 if (handle_pending_signals())
588 break;
592 xcreat(const char *path, mode_t mode)
594 int res;
596 while ((res = creat(path, mode)) == -1 && errno == EINTR)
597 if (handle_pending_signals())
598 break;
599 return res;
602 #ifdef HAVE_DUP2
603 static int
604 xdup2(int fildes, int fildes2)
606 int res;
608 while ((res = dup2(fildes, fildes2)) == -1 && errno == EINTR)
609 if (handle_pending_signals())
610 break;
611 return res;
613 #endif
615 struct group *
616 xgetgrgid(gid_t xgid)
618 struct group *res;
620 errno = 0;
621 while ((res = getgrgid(xgid)) == NULL && errno == EINTR) {
622 if (handle_pending_signals())
623 break;
624 errno = 0;
626 return res;
629 struct passwd *
630 xgetpwnam(const char *name)
632 struct passwd *res;
634 errno = 0;
635 while ((res = getpwnam(name)) == NULL && errno == EINTR) {
636 if (handle_pending_signals())
637 break;
638 errno = 0;
640 return res;
643 struct passwd *
644 xgetpwuid(uid_t xuid)
646 struct passwd *res;
648 errno = 0;
649 while ((res = getpwuid(xuid)) == NULL && errno == EINTR) {
650 if (handle_pending_signals())
651 break;
652 errno = 0;
654 return res;
658 xopen(const char *path, int oflag, ...)
660 int res;
662 if ((oflag & O_CREAT) == 0) {
663 while ((res = open(path, oflag)) == -1 && errno == EINTR)
664 if (handle_pending_signals())
665 break;
666 } else {
667 va_list ap;
668 mode_t mode;
670 va_start(ap, oflag);
671 /* "int" should actually be "mode_t after default argument
672 promotions". "int" is the best guess we have, "mode_t" used to be
673 "unsigned short", which we obviously can't use. */
674 mode = va_arg(ap, int);
675 va_end(ap);
676 while ((res = open(path, oflag, mode)) == -1 && errno == EINTR)
677 if (handle_pending_signals())
678 break;
680 return res;
683 ssize_t
684 xread(int fildes, void *buf, size_t nbyte)
686 ssize_t res = -1;
688 /* This is where we will be blocked most of the time, so handle signals
689 that didn't interrupt any system call. */
691 if (handle_pending_signals())
692 break;
693 while ((res = read(fildes, buf, nbyte)) == -1 && errno == EINTR);
694 return res;
697 #ifdef POSIX
699 xtcsetattr(int fildes, int optional_actions, const struct termios *termios_p)
701 int res;
703 while ((res = tcsetattr(fildes, optional_actions, termios_p)) == -1 &&
704 errno == EINTR)
705 if (handle_pending_signals())
706 break;
707 return res;
709 #endif
711 ssize_t
712 xwrite(int fildes, const void *buf, size_t nbyte)
714 ssize_t res = -1;
716 /* This is where we will be blocked most of the time, so handle signals
717 that didn't interrupt any system call. */
719 if (handle_pending_signals())
720 break;
721 while ((res = write(fildes, buf, nbyte)) == -1 && errno == EINTR);
722 return res;