kernel - Deal with lost IPIs (VM related)
[dragonfly.git] / contrib / tcsh-6 / sh.misc.c
blob1e381c1b1d90dfb8f2bd7a063bc7373b365ac578
1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.misc.c,v 3.49 2015/05/04 15:31:13 christos Exp $ */
2 /*
3 * sh.misc.c: Miscelaneous functions
4 */
5 /*-
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
31 * SUCH DAMAGE.
33 #include "sh.h"
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);
43 * C Shell
46 int
47 any(const char *s, Char c)
49 if (!s)
50 return (0); /* Check for nil pointer */
51 while (*s)
52 if ((Char)*s++ == c)
53 return (1);
54 return (0);
57 void
58 setzero(void *p, size_t size)
60 memset(p, 0, size);
63 #ifndef SHORT_STRINGS
64 char *
65 strnsave(const char *s, size_t len)
67 char *r;
69 r = xmalloc(len + 1);
70 memcpy(r, s, len);
71 r[len] = '\0';
72 return r;
74 #endif
76 char *
77 strsave(const char *s)
79 char *r;
80 size_t size;
82 if (s == NULL)
83 s = "";
84 size = strlen(s) + 1;
85 r = xmalloc(size);
86 memcpy(r, s, size);
87 return (r);
90 static Char **
91 blkend(Char **up)
94 while (*up)
95 up++;
96 return (up);
100 void
101 blkpr(Char *const *av)
104 for (; *av; av++) {
105 xprintf("%S", *av);
106 if (av[1])
107 xprintf(" ");
111 Char *
112 blkexpand(Char *const *av)
114 struct Strbuf buf = Strbuf_INIT;
116 for (; *av; av++) {
117 Strbuf_append(&buf, *av);
118 if (av[1])
119 Strbuf_append1(&buf, ' ');
121 return Strbuf_finish(&buf);
125 blklen(Char **av)
127 int i = 0;
129 while (*av++)
130 i++;
131 return (i);
134 Char **
135 blkcpy(Char **oav, Char **bv)
137 Char **av = oav;
139 while ((*av++ = *bv++) != NULL)
140 continue;
141 return (oav);
144 static Char **
145 blkcat(Char **up, Char **vp)
148 (void) blkcpy(blkend(up), vp);
149 return (up);
152 void
153 blkfree(Char **av0)
155 Char **av = av0;
157 if (!av0)
158 return;
159 for (; *av; av++)
160 xfree(*av);
161 xfree(av0);
164 void
165 blk_cleanup(void *ptr)
167 blkfree(ptr);
170 void
171 blk_indirect_cleanup(void *xptr)
173 Char ***ptr;
175 ptr = xptr;
176 blkfree(*ptr);
177 xfree(ptr);
180 Char **
181 saveblk(Char **v)
183 Char **newv, **onewv;
185 if (v == NULL)
186 return NULL;
188 onewv = newv = xcalloc(blklen(v) + 1, sizeof(Char **));
190 while (*v)
191 *newv++ = Strsave(*v++);
192 return (onewv);
195 #ifndef HAVE_STRSTR
196 char *
197 strstr(const char *s, const char *t)
199 do {
200 const char *ss = s;
201 const char *tt = t;
204 if (*tt == '\0')
205 return (s);
206 while (*ss++ == *tt++);
207 } while (*s++ != '\0');
208 return (NULL);
210 #endif /* !HAVE_STRSTR */
212 char *
213 strspl(const char *cp, const char *dp)
215 char *ep;
216 size_t cl, dl;
218 if (!cp)
219 cp = "";
220 if (!dp)
221 dp = "";
222 cl = strlen(cp);
223 dl = strlen(dp);
224 ep = xmalloc((cl + dl + 1) * sizeof(char));
225 memcpy(ep, cp, cl);
226 memcpy(ep + cl, dp, dl + 1);
227 return (ep);
230 Char **
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));
239 Char
240 lastchr(Char *cp)
243 if (!cp)
244 return (0);
245 if (!*cp)
246 return (0);
247 while (cp[1])
248 cp++;
249 return (*cp);
253 * This routine is called after an error to close up
254 * any units which may have been left open accidentally.
256 void
257 closem(void)
259 int f, num_files;
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 */
279 xclose(f);
280 #ifdef NISPLUS
281 if(f < 3)
282 (void) xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
283 #endif /* NISPLUS */
285 #ifdef NLS_BUGS
286 #ifdef NLS_CATALOGS
287 nlsinit();
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!
299 void
300 closech(void)
302 int f, num_files;
304 if (didcch)
305 return;
306 didcch = 1;
307 SHIN = 0;
308 SHOUT = 1;
309 SHDIAG = 2;
310 OLDSTD = 0;
311 isoutatty = isatty(SHOUT);
312 isdiagatty = isatty(SHDIAG);
313 num_files = NOFILE;
314 for (f = 3; f < num_files; f++)
315 xclose(f);
318 #endif /* CLOSE_ON_EXEC */
320 void
321 donefds(void)
324 xclose(0);
325 xclose(1);
326 xclose(2);
327 didfds = 0;
328 #ifdef NISPLUS
330 int fd = xopen(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE);
331 (void)dcopy(fd, 1);
332 (void)dcopy(fd, 2);
333 (void)dmove(fd, 0);
335 #endif /*NISPLUS*/
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.
344 dmove(int i, int j)
347 if (i == j || i < 0)
348 return (i);
349 #ifdef HAVE_DUP2
350 if (j >= 0) {
351 (void) xdup2(i, j);
352 if (j != i)
353 xclose(i);
354 return (j);
356 #endif
357 j = dcopy(i, j);
358 if (j != i)
359 xclose(i);
360 return (j);
364 dcopy(int i, int j)
367 if (i == j || i < 0 || (j < 0 && i > FSAFE))
368 return (i);
369 if (j >= 0) {
370 #ifdef HAVE_DUP2
371 (void) xdup2(i, j);
372 return (j);
373 #else
374 xclose(j);
375 #endif
377 return (renum(i, j));
380 static int
381 renum(int i, int j)
383 int k = dup(i);
385 if (k < 0)
386 return (-1);
387 if (j == -1 && k > FSAFE)
388 return (k);
389 if (k != j) {
390 j = renum(k, j);
391 xclose(k);
392 return (j);
394 return (k);
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".
402 void
403 lshift(Char **v, int c)
405 Char **u;
407 for (u = v; *u && --c >= 0; u++)
408 xfree(*u);
409 (void) blkcpy(v, u);
413 number(Char *cp)
415 if (!cp)
416 return (0);
417 if (*cp == '-') {
418 cp++;
419 if (!Isdigit(*cp))
420 return (0);
421 cp++;
423 while (*cp && Isdigit(*cp))
424 cp++;
425 return (*cp == 0);
428 Char **
429 copyblk(Char **v)
431 Char **nv = xcalloc(blklen(v) + 1, sizeof(Char **));
433 return (blkcpy(nv, v));
436 char *
437 strend(const char *cp)
439 if (!cp)
440 return ((char *)(intptr_t)cp);
441 while (*cp)
442 cp++;
443 return ((char *)(intptr_t)cp);
446 Char *
447 strip(Char *cp)
449 Char *dp = cp;
451 if (!cp)
452 return (cp);
453 while ((*dp++ &= TRIM) != '\0')
454 continue;
455 return (cp);
458 Char *
459 quote(Char *cp)
461 Char *dp = cp;
463 if (!cp)
464 return (cp);
465 while (*dp != '\0')
466 *dp++ |= QUOTE;
467 return (cp);
470 const Char *
471 quote_meta(struct Strbuf *buf, const Char *s)
473 buf->len = 0;
474 while (*s != '\0') {
475 if (cmap(*s, _META | _DOL | _QF | _QB | _ESC | _GLOB))
476 Strbuf_append1(buf, '\\');
477 Strbuf_append1(buf, *s++);
479 Strbuf_terminate(buf);
480 return buf->s;
483 void
484 udvar(Char *name)
486 setname(short2str(name));
487 stderror(ERR_NAME | ERR_UNDVAR);
491 prefix(const Char *sub, const Char *str)
494 for (;;) {
495 if (*sub == 0)
496 return (1);
497 if (*str == 0)
498 return (0);
499 if ((*sub++ & TRIM) != (*str++ & TRIM))
500 return (0);
503 #ifndef WINNT_NATIVE
504 char *
505 areadlink(const char *path)
507 char *buf;
508 size_t size;
509 ssize_t res;
511 size = MAXPATHLEN + 1;
512 buf = xmalloc(size);
513 while ((size_t)(res = readlink(path, buf, size)) == size) {
514 size *= 2;
515 buf = xrealloc(buf, size);
517 if (res == -1) {
518 int err;
520 err = errno;
521 xfree(buf);
522 errno = err;
523 return NULL;
525 buf[res] = '\0';
526 return xrealloc(buf, res + 1);
528 #endif /*!WINNT_NATIVE*/
530 void
531 xclose(int fildes)
533 if (fildes < 0)
534 return;
535 while (close(fildes) == -1 && errno == EINTR)
536 if (handle_pending_signals())
537 break;
540 void
541 xclosedir(DIR *dirp)
543 while (closedir(dirp) == -1 && errno == EINTR)
544 if (handle_pending_signals())
545 break;
549 xcreat(const char *path, mode_t mode)
551 int res;
553 while ((res = creat(path, mode)) == -1 && errno == EINTR)
554 if (handle_pending_signals())
555 break;
556 return res;
559 #ifdef HAVE_DUP2
560 static int
561 xdup2(int fildes, int fildes2)
563 int res;
565 while ((res = dup2(fildes, fildes2)) == -1 && errno == EINTR)
566 if (handle_pending_signals())
567 break;
568 return res;
570 #endif
572 struct group *
573 xgetgrgid(gid_t xgid)
575 struct group *res;
577 errno = 0;
578 while ((res = getgrgid(xgid)) == NULL && errno == EINTR) {
579 if (handle_pending_signals())
580 break;
581 errno = 0;
583 return res;
586 struct passwd *
587 xgetpwnam(const char *name)
589 struct passwd *res;
591 errno = 0;
592 while ((res = getpwnam(name)) == NULL && errno == EINTR) {
593 if (handle_pending_signals())
594 break;
595 errno = 0;
597 return res;
600 struct passwd *
601 xgetpwuid(uid_t xuid)
603 struct passwd *res;
605 errno = 0;
606 while ((res = getpwuid(xuid)) == NULL && errno == EINTR) {
607 if (handle_pending_signals())
608 break;
609 errno = 0;
611 return res;
615 xopen(const char *path, int oflag, ...)
617 int res;
619 if ((oflag & O_CREAT) == 0) {
620 while ((res = open(path, oflag)) == -1 && errno == EINTR)
621 if (handle_pending_signals())
622 break;
623 } else {
624 va_list ap;
625 mode_t mode;
627 va_start(ap, oflag);
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);
632 va_end(ap);
633 while ((res = open(path, oflag, mode)) == -1 && errno == EINTR)
634 if (handle_pending_signals())
635 break;
637 return res;
640 ssize_t
641 xread(int fildes, void *buf, size_t nbyte)
643 ssize_t res;
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())
649 break;
650 while ((res = read(fildes, buf, nbyte)) == -1 && errno == EINTR);
651 return res;
654 #ifdef POSIX
656 xtcsetattr(int fildes, int optional_actions, const struct termios *termios_p)
658 int res;
660 while ((res = tcsetattr(fildes, optional_actions, termios_p)) == -1 &&
661 errno == EINTR)
662 if (handle_pending_signals())
663 break;
664 return res;
666 #endif
668 ssize_t
669 xwrite(int fildes, const void *buf, size_t nbyte)
671 ssize_t res;
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())
677 break;
678 while ((res = write(fildes, buf, nbyte)) == -1 && errno == EINTR);
679 return res;