1 /* $Header: /p/tcsh/cvsroot/tcsh/tc.os.c,v 3.72 2011/01/25 13:58:19 christos Exp $ */
3 * tc.os.c: OS Dependent builtin 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: tc.os.c,v 3.72 2011/01/25 13:58:19 christos Exp $")
39 #include "ed.defns.h" /* for the function names */
43 #define TIOCGPGRP TIOCGETPGRP
44 #define TIOCSPGRP TIOCSETPGRP
52 /* dosetpath -- setpath built-in command
54 **********************************************************************
56 * 08-May-88 Richard Draves (rpd) at Carnegie-Mellon University
57 * Major changes to remove artificial limits on sizes and numbers
60 **********************************************************************
64 static Char STRCPATH
[] = {'C', 'P', 'A', 'T', 'H', '\0'};
65 static Char STRLPATH
[] = {'L', 'P', 'A', 'T', 'H', '\0'};
66 static Char STRMPATH
[] = {'M', 'P', 'A', 'T', 'H', '\0'};
68 static Char STREPATH
[] = {'E', 'P', 'A', 'T', 'H', '\0'};
71 static Char
*syspaths
[] = {STRKPATH
, STRCPATH
, STRLPATH
, STRMPATH
,
77 #define LOCALSYSPATH "/usr/local"
81 dosetpath(Char
**arglist
, struct command
*c
)
83 extern char *getenv();
84 Char
**pathvars
, **cmdargs
;
85 char **spaths
, **cpaths
, **cmds
;
87 unsigned int npaths
, ncmds
;
91 cleanup_push(&pintr_disabled
, disabled_cleanup
);
94 * setpath(3) uses stdio and we want 0, 1, 2 to work...
97 (void) dcopy(SHIN
, 0);
98 (void) dcopy(SHOUT
, 1);
99 (void) dcopy(SHDIAG
, 2);
103 for (i
= 1; arglist
[i
] && (arglist
[i
][0] != '-'); i
++);
106 cmdargs
= &arglist
[i
];
107 for (; arglist
[i
]; i
++);
108 ncmds
= i
- npaths
- 1;
112 pathvars
= &arglist
[1];
116 npaths
= (sizeof syspaths
/ sizeof *syspaths
) - 1;
120 /* note that npaths != 0 */
122 spaths
= xmalloc(npaths
* sizeof *spaths
);
123 setzero(spaths
, npaths
* sizeof *spaths
);
124 cpaths
= xmalloc((npaths
+ 1) * sizeof *cpaths
);
125 setzero(cpaths
, (npaths
+ 1) * sizeof *cpaths
);
126 cmds
= xmalloc((ncmds
+ 1) * sizeof *cmds
);
127 setzero(cmds
, (ncmds
+ 1) * sizeof *cmds
);
128 for (i
= 0; i
< npaths
; i
++) {
129 char *val
= getenv(short2str(pathvars
[i
]));
134 spaths
[i
] = xmalloc((Strlen(pathvars
[i
]) + strlen(val
) + 2) *
136 (void) strcpy(spaths
[i
], short2str(pathvars
[i
]));
137 (void) strcat(spaths
[i
], "=");
138 (void) strcat(spaths
[i
], val
);
139 cpaths
[i
] = spaths
[i
];
142 for (i
= 0; i
< ncmds
; i
++) {
143 Char
*val
= globone(cmdargs
[i
], G_ERROR
);/*FIXRESET*/
147 cmds
[i
] = strsave(short2str(val
));
151 if (setpath(cpaths
, cmds
, LOCALSYSPATH
, sysflag
, 1) < 0) {
154 for (i
= 0; i
< npaths
; i
++)
160 for (i
= 0; i
< ncmds
; i
++)
165 cleanup_until(&pintr_disabled
);
170 for (i
= 0; i
< npaths
; i
++) {
173 name
= str2short(cpaths
[i
]);
174 for (val
= str2short(cpaths
[i
]); val
&& *val
&& *val
!= '='; val
++);
175 if (val
&& *val
== '=') {
178 tsetenv(name
, val
);/*FIXRESET*/
179 if (Strcmp(name
, STRKPATH
) == 0) {
180 importpath(val
);/*FIXRESET*/
182 dohash(NULL
, NULL
);/*FIXRESET*/
187 cleanup_until(&pintr_disabled
);
198 dogetxvers(Char
**v
, struct command
*c
)
200 char xvers
[MAXPATHLEN
];
202 if (getxvers(xvers
, MAXPATHLEN
) == -1)
203 stderror(ERR_SYSTEM
, "getxvers", strerror(errno
));
204 xprintf("%s\n", xvers
);
210 dosetxvers(Char
**v
, struct command
*c
)
215 if (!*v
|| *v
[0] == '\0')
218 xvers
= short2str(*v
);
219 if (setxvers(xvers
) == -1)
220 stderror(ERR_SYSTEM
, "setxvers", strerror(errno
));
225 # define XC_PDP11 0x01
228 # define XC_8086 0x04
232 # define XC_16032 0x08
235 # define XC_S370 0x0b
237 # include <sys/x.out.h>
240 static struct xc_cpu_t
{
245 { XC_PDP11
, "pdp11" },
249 { XC_68K
, "mc68000" },
252 { XC_16032
, "ns16032" },
255 { XC_S370
, "xa370" },
260 * our local hack table, stolen from x.out.h
267 for (i
= 0; xcpu
[i
].xc_name
!= NULL
; i
++)
268 if (xcpu
[i
].xc_id
== xcid
)
269 return (xcpu
[i
].xc_name
);
278 for (i
= 0; xcpu
[i
].xc_name
!= NULL
; i
++)
279 if (strcmp(xcpu
[i
].xc_name
, xcname
) == 0)
280 return (xcpu
[i
].xc_id
);
287 dogetspath(Char
**v
, struct command
*c
)
290 sitepath_t p
[MAXSITE
];
292 static char *local
= "LOCAL ";
294 if ((j
= getspath(p
, MAXSITE
)) == -1)
295 stderror(ERR_SYSTEM
, "getspath", strerror(errno
));
296 for (i
= 0; i
< j
&& (p
[i
] & SPATH_CPU
) != NOSITE
; i
++) {
297 if (p
[i
] & SPATH_CPU
) {
298 if ((p
[i
] & SPATH_MASK
) == NULLSITE
)
300 else if ((st
= sfxcode((short) (p
[i
] & SPATH_MASK
))) != NULL
)
301 xprintf("%s ", st
->sf_ctype
);
303 char *xc
= getxcode(p
[i
] & SPATH_MASK
);
308 xprintf("*cpu %d* ", (int) (p
[i
] & SPATH_MASK
));
310 * BUG in the aix code... needs that cause if
311 * sfxcode fails once it fails for ever
317 if (p
[i
] == NULLSITE
)
319 else if ((st
= sfnum(p
[i
])) != NULL
)
320 xprintf("%s ", st
->sf_sname
);
322 xprintf("*site %d* ", (int) (p
[i
] & SPATH_MASK
));
331 dosetspath(Char
**v
, struct command
*c
)
336 sitepath_t p
[MAXSITE
];
340 * sfname() on AIX G9.9 at least, mallocs too pointers p, q
341 * then does the equivalent of while (*p++ == *q++) continue;
342 * and then tries to free(p,q) them! Congrats to the wizard who
343 * wrote that one. I bet he tested it really well too.
344 * Sooo, we set dont_free :-)
347 for (i
= 0, v
++; *v
&& *v
[0] != '\0'; v
++, i
++) {
351 else if (strcmp(s
, "LOCAL") == 0)
353 else if ((st
= sfctype(s
)) != NULL
)
354 p
[i
] = SPATH_CPU
| st
->sf_ccode
;
355 else if ((j
= getxid(s
)) != -1)
356 p
[i
] = SPATH_CPU
| j
;
357 else if ((st
= sfname(s
)) != NULL
)
361 stderror(ERR_NAME
| ERR_STRING
, CGETS(23, 1, "Bad cpu/site name"));
363 if (i
== MAXSITE
- 1)
364 stderror(ERR_NAME
| ERR_STRING
, CGETS(23, 2, "Site path too long"));
366 if (setspath(p
, i
) == -1)
367 stderror(ERR_SYSTEM
, "setspath", strerror(errno
));
372 * Return the site name where the process is running
380 if ((ss
= site(pid
)) == -1 || (st
= sfnum(ss
)) == NULL
)
381 return CGETS(23, 3, "unknown");
387 migratepid(pit_t pid
, siteno_t new_site
)
392 need_local
= (pid
== 0) || (pid
== getpid());
394 if (kill3(pid
, SIGMIGRATE
, new_site
) < 0) {
395 xprintf("%d: %s\n", pid
, strerror(errno
));
400 if ((new_site
= site(0)) == -1) {
401 xprintf(CGETS(23, 4, "site: %s\n"), strerror(errno
));
404 if ((st
= sfnum(new_site
)) == NULL
) {
405 xprintf(CGETS(23, 5, "%d: Site not found\n"), new_site
);
408 if (setlocal(st
->sf_local
, strlen(st
->sf_local
)) == -1) {
409 xprintf(CGETS(23, 6, "setlocal: %s: %s\n"),
410 st
->sf_local
, strerror(errno
));
419 domigrate(Char
**v
, struct command
*c
)
427 siteno_t new_site
= 0;
430 cleanup_push(&pchild_disabled
, disabled_cleanup
);
433 cleanup_push(&pintr_disabled
, disabled_cleanup
);
441 s
= short2str(&v
[0][1]);
443 * see comment in setspath()
446 if ((st
= sfname(s
)) == NULL
) {
449 stderror(ERR_NAME
| ERR_STRING
, CGETS(23, 7, "Site not found"));
452 new_site
= st
->sf_id
;
456 if (!*v
|| *v
[0] == '\0') {
457 if (migratepid(0, new_site
) == -1)
463 v
= glob_all_or_error(v
);
465 cleanup_push(globbed
, blk_cleanup
);
467 while (v
&& (cp
= *v
)) {
470 if (kill3(- pp
->p_jobid
, SIGMIGRATE
, new_site
) < 0) {
471 xprintf("%S: %s\n", cp
, strerror(errno
));
475 else if (!(Isdigit(*cp
) || *cp
== '-'))
476 stderror(ERR_NAME
| ERR_JOBARGS
);
478 pid
= atoi(short2str(cp
));
479 if (migratepid(pid
, new_site
) == -1)
484 cleanup_until(globbed
);
488 cleanup_until(&pchild_disabled
);
490 stderror(ERR_SILENT
);
496 *** CRAY ddmode <velo@sesun3.epfl.ch> (Martin Ouwehand EPFL-SIC/SE)
498 #if defined(_CRAY) && !defined(_CRAYMPP)
500 dodmmode(Char
**v
, struct command
*c
)
511 xprintf("%d\n",mode
);
515 stderror(ERR_NAME
| ERR_STRING
,
516 CGETS(23, 30, "Too many arguments"));
526 stderror(ERR_NAME
| ERR_STRING
,
527 CGETS(23, 31, "Invalid argument"));
531 #endif /* _CRAY && !_CRAYMPP */
540 * handle the funky warping of symlinks
543 #include <sys/warp.h>
545 static jmp_buf sigsys_buf
;
551 sigemptyset(&set
, SIGSYS
);
552 (void)sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
553 longjmp(sigsys_buf
, 1);
559 dowarp(Char
**v
, struct command
*c
)
563 volatile struct sigaction old_sigsys_handler
;
566 if (setjmp(sigsys_buf
)) {
567 sigaction(SIGSYS
, &old_sigsys_handler
, NULL
);
568 stderror(ERR_NAME
| ERR_STRING
,
569 CGETS(23, 8, "You're trapped in a universe you never made"));
572 sigaction(SIGSYS
, NULL
, &old_sigsys_handler
);
573 signal(SIGSYS
, catch_sigsys
);
578 if (*v
== 0) { /* display warp value */
580 stderror(ERR_NAME
| ERR_STRING
, CGETS(23, 9, "Getwarp failed"));
581 we
= getwarpbyvalue(warp
);
583 printf("%s\n", we
->w_name
);
585 printf("%d\n", warp
);
587 else { /* set warp value */
589 newwarp
= short2str(*v
);
591 warp
= atoi(newwarp
);
593 we
= getwarpbyname(newwarp
);
599 if ((warp
< 0) || (warp
>= WARP_MAXLINK
))
600 stderror(ERR_NAME
| ERR_STRING
, CGETS(23, 10, "Invalid warp"));
601 if ((setwarp(warp
) < 0) || (getwarp() != warp
)) {
602 (void) setwarp(oldwarp
);
603 stderror(ERR_NAME
| ERR_STRING
, CGETS(23, 11, "Setwarp failed"));
606 sigaction(SIGSYS
, &old_sigsys_handler
, NULL
);
613 /* Added, DAS DEC-90. */
614 #if defined(masscomp) || defined(_CX_UX)
616 setuniverse_cleanup(void *xbuf
)
626 douniverse(Char
**v
, struct command
*c
)
629 Char
*cp2
; /* dunno how many elements v comes in with */
633 (void) getuniverse(ubuf
);
634 xprintf("%s\n", ubuf
);
639 if (*cp
== '\0' || setuniverse(short2str(cp
)) != 0)
640 stderror(ERR_NAME
| ERR_STRING
, CGETS(23, 12, "Illegal universe"));
643 (void) getuniverse(ubuf
);
644 if (*cp
== '\0' || setuniverse(short2str(cp
)) != 0)
645 stderror(ERR_NAME
| ERR_STRING
, CGETS(23, 12, "Illegal universe"));
646 cleanup_push(ubuf
, setuniverse_cleanup
);
649 cleanup_push(&pintr_disabled
, disabled_cleanup
);
653 cleanup_until(&pintr_disabled
);
659 #endif /* masscomp || _CX_UX */
662 *** BS2000/OSD POSIX (Fujitsu Siemens Computers)
664 #if defined(_OSD_POSIX)
668 enum { outside
= ' ', singlequote
='\'', doublequote
='"'} string
= outside
;
672 for (white
= str
+ strlen(str
) - 1; isspace(*white
) && white
> str
; --white
)
675 for (; *str
!= '\0'; ++str
)
677 if (string
== outside
)
679 *str
= toupper (*str
);
683 if (string
== outside
)
684 string
= singlequote
;
685 else if (string
!= doublequote
)
688 else if (*str
== '"')
690 if (string
== outside
)
691 string
= doublequote
;
692 else if (string
!= singlequote
)
696 if (string
!= outside
)
698 stderror(ERR_NAME
| ERR_UNMATCHED
, (Char
) string
);
704 bs2cmdlist(char *str
)
706 char *str_beg
= NULL
;
709 enum { outside
= ' ', singlequote
='\'', doublequote
='"'} string
= outside
;
713 while (isspace(*str
))
721 for (; *str
!= '\0'; ++str
)
723 if (string
== outside
&& *str
== ';') /* End of command */
726 break; /* continue with next command */
730 if (string
== outside
)
731 string
= singlequote
;
732 else if (string
!= doublequote
)
735 else if (*str
== '"')
737 if (string
== outside
)
738 string
= doublequote
;
739 else if (string
!= singlequote
)
743 if (strlen(str_beg
) != 0)
745 ret
= bs2system(str_beg
);
747 if (ret
!= 0 /*&& !option.err_ignore*/)
748 break; /* do not continue after errors */
752 if (string
!= outside
)
754 stderror(ERR_NAME
| ERR_UNMATCHED
, (Char
) string
);
762 dobs2cmd(Char
**v
, struct command
*c
)
768 struct command faket
;
771 int icnt
, old_pintr_disabled
;
772 static const Char STRbs2cmd
[] = { 'b','s','2','c','m','d','\0' };
776 pintr_push_enable(&old_pintr_disabled
);
777 v
= glob_all_or_error(v
);
779 cleanup_until(&old_pintr_disabled
);
781 cleanup_push(globbed
, blk_cleanup
);
783 /* First round: count the string lengths */
784 for (i
=0; v
[i
]; ++i
) {
785 len
+= Strlen(v
[i
]) + (v
[i
+1] != NULL
);
788 cmd
= xmalloc(len
+1); /* 1 for the final '\0' *//* FIXME: memory leak? */
790 /* 2nd round: fill cmd buffer */
792 while ((cp
= *v
++) != 0) {
801 /* Make upper case */
804 faket
.t_dtyp
= NODE_COMMAND
;
805 faket
.t_dflg
= F_BACKQ
|F_STDERR
;
809 faket
.t_dcom
= fakecom
;
810 fakecom
[0] = (Char
*)STRbs2cmd
;
814 cleanup_push(&pvec
[0], open_cleanup
);
815 cleanup_push(&pvec
[1], open_cleanup
);
816 if (pfork(&faket
, -1) == 0) {
820 (void) dmove(pvec
[1], 1);
821 (void) dmove(SHDIAG
, 2);
824 sigaddset(&set
, SIGINT
);
825 (void)sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
827 signal(SIGTSTP
, SIG_IGN
);
830 signal(SIGTTIN
, SIG_IGN
);
833 signal(SIGTTOU
, SIG_IGN
);
835 xexit(bs2cmdlist(cmd
));
837 cleanup_until(&pvec
[1]);
839 int old_pintr_disabled
;
842 pintr_push_enable(&old_pintr_disabled
);
843 icnt
= xread(pvec
[0], tibuf
, sizeof(tibuf
));
845 cleanup_until(&old_pintr_disabled
);
848 for (i
= 0; i
< icnt
; i
++)
849 xputchar((unsigned char) tibuf
[i
]);
851 cleanup_until(&pvec
[0]);
856 cleanup_until(globbed
);
858 #endif /* _OSD_POSIX */
862 setuniverse_cleanup(void *xbuf
)
872 doatt(Char
**v
, struct command
*c
)
878 (void) setuniverse("att");
880 (void) getuniverse(ubuf
);
881 (void) setuniverse("att");
882 cleanup_push(ubuf
, setuniverse_cleanup
);
885 cleanup_push(&pintr_disabled
, disabled_cleanup
);
889 cleanup_until(&pintr_disabled
);
897 doucb(Char
**v
, struct command
*c
)
903 (void) setuniverse("ucb");
905 (void) getuniverse(ubuf
);
906 (void) setuniverse("ucb");
907 cleanup_push(ubuf
, setuniverse_cleanup
);
910 cleanup_push(&pintr_disabled
, disabled_cleanup
);
914 cleanup_until(&pintr_disabled
);
923 * Compute the difference in process stats.
926 pr_stat_sub(struct process_stats
*p2
, struct process_stats
*p1
,
927 struct process_stats
*pr
)
929 pr
->ps_utime
.tv_sec
= p2
->ps_utime
.tv_sec
- p1
->ps_utime
.tv_sec
;
930 pr
->ps_utime
.tv_usec
= p2
->ps_utime
.tv_usec
- p1
->ps_utime
.tv_usec
;
931 if (pr
->ps_utime
.tv_usec
< 0) {
932 pr
->ps_utime
.tv_sec
-= 1;
933 pr
->ps_utime
.tv_usec
+= 1000000;
935 pr
->ps_stime
.tv_sec
= p2
->ps_stime
.tv_sec
- p1
->ps_stime
.tv_sec
;
936 pr
->ps_stime
.tv_usec
= p2
->ps_stime
.tv_usec
- p1
->ps_stime
.tv_usec
;
937 if (pr
->ps_stime
.tv_usec
< 0) {
938 pr
->ps_stime
.tv_sec
-= 1;
939 pr
->ps_stime
.tv_usec
+= 1000000;
942 pr
->ps_maxrss
= p2
->ps_maxrss
- p1
->ps_maxrss
;
943 pr
->ps_pagein
= p2
->ps_pagein
- p1
->ps_pagein
;
944 pr
->ps_reclaim
= p2
->ps_reclaim
- p1
->ps_reclaim
;
945 pr
->ps_zerofill
= p2
->ps_zerofill
- p1
->ps_zerofill
;
946 pr
->ps_pffincr
= p2
->ps_pffincr
- p1
->ps_pffincr
;
947 pr
->ps_pffdecr
= p2
->ps_pffdecr
- p1
->ps_pffdecr
;
948 pr
->ps_swap
= p2
->ps_swap
- p1
->ps_swap
;
949 pr
->ps_syscall
= p2
->ps_syscall
- p1
->ps_syscall
;
950 pr
->ps_volcsw
= p2
->ps_volcsw
- p1
->ps_volcsw
;
951 pr
->ps_involcsw
= p2
->ps_involcsw
- p1
->ps_involcsw
;
952 pr
->ps_signal
= p2
->ps_signal
- p1
->ps_signal
;
953 pr
->ps_lread
= p2
->ps_lread
- p1
->ps_lread
;
954 pr
->ps_lwrite
= p2
->ps_lwrite
- p1
->ps_lwrite
;
955 pr
->ps_bread
= p2
->ps_bread
- p1
->ps_bread
;
956 pr
->ps_bwrite
= p2
->ps_bwrite
- p1
->ps_bwrite
;
957 pr
->ps_phread
= p2
->ps_phread
- p1
->ps_phread
;
958 pr
->ps_phwrite
= p2
->ps_phwrite
- p1
->ps_phwrite
;
961 #endif /* _SEQUENT_ */
965 /* This is a replacement for a missing memset function */
966 void *xmemset(void *loc
, int value
, size_t len
)
974 #endif /* !HAVE_MEMSET */
979 * This is the ANSI form of bcopy() with the arguments backwards...
980 * Unlike memcpy(), it handles overlaps between source and
984 xmemmove(void *vdst
, const void *vsrc
, size_t len
)
986 const char *src
= vsrc
;
1004 #endif /* HAVE_MEMMOVE */
1007 #ifndef WINNT_NATIVE
1008 #ifdef NEEDtcgetpgrp
1014 /* ioctl will handle setting errno correctly. */
1015 if (ioctl(fd
, TIOCGPGRP
, (ioctl_t
) & pgrp
) < 0)
1021 * XXX: tcsetpgrp is not a macro any more cause on some systems,
1022 * pid_t is a short, but the ioctl() takes a pointer to int (pyr)
1023 * Thanks to Simon Day (simon@pharaoh.cyborg.bt.co.uk) for pointing
1027 xtcsetpgrp(int fd
, int pgrp
)
1029 return ioctl(fd
, TIOCSPGRP
, (ioctl_t
) &pgrp
);
1032 #endif /* NEEDtcgetpgrp */
1033 #endif /* WINNT_NATIVE */
1042 extern int yp_get_default_domain (char **);
1044 * PWP: The previous version assumed that yp domain was the same as the
1045 * internet name domain. This isn't allways true. (Thanks to Mat Landau
1046 * <mlandau@bbn.com> for the original version of this.)
1048 if (yp_get_default_domain(&mydomain
) == 0) { /* if we got a name */
1049 extern void yp_unbind (const char *);
1051 yp_unbind(mydomain
);
1059 fix_strcoll_bug(void)
1061 #if defined(NLS) && defined(HAVE_STRCOLL)
1063 * SunOS4 checks the file descriptor from openlocale() for <= 0
1064 * instead of == -1. Someone should tell sun that file descriptor 0
1065 * is valid! Our portable hack: open one so we call it with 0 used...
1066 * We have to call this routine every time the locale changes...
1068 * Of course it also tries to free the constant locale "C" it initially
1069 * had allocated, with the sequence
1070 * > setenv LANG "fr"
1073 * But we are smarter than that and just print a warning message.
1076 static char *root
= "/";
1079 fd
= xopen(root
, O_RDONLY
|O_LARGEFILE
);
1081 (void) strcoll(root
, root
);
1087 #endif /* STRCOLLBUG */
1099 setcompat(getcompat() & ~COMPAT_EXEC
);
1100 signal(SIGIO
, SIG_IGN
); /* ignore SIGIO */
1105 struct sigstack inst
;
1106 inst
.ss_sp
= xmalloc(4192) + 4192;
1107 inst
.ss_onstack
= 0;
1108 sigstack(&inst
, NULL
);
1118 * kill(SIGCONT) problems, don't know what this syscall does
1119 * [schott@rzg.mpg.de]
1121 syscall(151, getpid(), getpid());
1125 #ifndef HAVE_STRERROR
1126 extern int sys_nerr
;
1127 extern char *sys_errlist
[];
1131 if (i
>= 0 && i
< sys_nerr
) {
1132 return sys_errlist
[i
];
1134 static char *errbuf
; /* = NULL; */
1137 errbuf
= xasprintf(CGETS(23, 13, "Unknown Error: %d"), i
);
1141 #endif /* !HAVE_STRERROR */
1143 #ifndef HAVE_GETHOSTNAME
1144 # if !defined(_MINIX) && !defined(__EMX__) && !defined(WINNT_NATIVE)
1145 # include <sys/utsname.h>
1146 # endif /* !_MINIX && !__EMX__ && !WINNT_NATIVE */
1149 xgethostname(char *name
, int namlen
)
1151 # if !defined(_MINIX) && !defined(__EMX__) && !defined(WINNT_NATIVE)
1155 retval
= uname(&uts
);
1158 xprintf(CGETS(23, 14, "sysname: %s\n"), uts
.sysname
);
1159 xprintf(CGETS(23, 15, "nodename: %s\n"), uts
.nodename
);
1160 xprintf(CGETS(23, 16, "release: %s\n"), uts
.release
);
1161 xprintf(CGETS(23, 17, "version: %s\n"), uts
.version
);
1162 xprintf(CGETS(23, 18, "machine: %s\n"), uts
.machine
);
1164 i
= strlen(uts
.nodename
) + 1;
1165 (void) strncpy(name
, uts
.nodename
, i
< namlen
? i
: namlen
);
1168 # else /* !_MINIX && !__EMX__ */
1171 (void) strncpy(name
, "OS/2", namlen
);
1173 (void) strncpy(name
, "minix", namlen
);
1174 # endif /* __EMX__ */
1175 name
[namlen
-1] = '\0';
1178 #endif /* _MINIX && !__EMX__ */
1179 } /* end xgethostname */
1180 #endif /* !HAVE_GETHOSTNAME */
1183 # if defined(_MINIX) && defined(NICE)
1184 # undef _POSIX_SOURCE /* redefined in <lib.h> */
1185 # undef _MINIX /* redefined in <lib.h> */
1186 # undef HZ /* redefined in <minix/const.h> */
1188 # endif /* _MINIX && NICE */
1192 #if defined(_MINIX) && defined(NICE)
1193 return callm1(MM
, NICE
, incr
, 0, 0, NIL_PTR
, NIL_PTR
, NIL_PTR
);
1195 return /* incr ? 0 : */ 0;
1196 #endif /* _MINIX && NICE */
1198 #endif /* !HAVE_NICE */
1201 static char *strnrcpy (char *, char *, size_t);
1204 * Return the pathname of the current directory, or return
1205 * an error message in pathname.
1210 * From: Bernd Mohr <mohr@faui77.informatik.uni-erlangen.de>
1211 * I also ported the tcsh to the HP9000 Series 500. This computer
1212 * is a little bit different than the other HP 9000 computer. It has
1213 * a HP Chip instead of a Motorola CPU and it is no "real" UNIX. It runs
1214 * HP-UX which is emulated in top of a HP operating system. So, the last
1215 * supported version of HP-UX is 5.2 on the HP9000s500. This has two
1216 * consequences: it supports no job control and it has a filesystem
1217 * without "." and ".." !!!
1220 xgetcwd(char *pathname
, size_t pathlen
)
1222 char pathbuf
[MAXPATHLEN
]; /* temporary pathname buffer */
1223 char *pnptr
= &pathbuf
[(sizeof pathbuf
)-1]; /* pathname pointer */
1224 dev_t rdev
; /* root device number */
1225 DIR *dirp
= NULL
; /* directory stream */
1226 ino_t rino
; /* root inode number */
1227 off_t rsize
; /* root size */
1228 struct direct
*dir
; /* directory entry struct */
1229 struct stat d
, dd
; /* file status struct */
1233 (void) stat("/.", &d
);
1238 if (stat(".", &d
) == -1) {
1239 (void) xsnprintf(pathname
, pathlen
, CGETS(23, 24,
1240 "getcwd: Cannot stat \".\" (%s)"), strerror(errno
));
1243 if (d
.st_ino
== rino
&& d
.st_dev
== rdev
&& d
.st_size
== rsize
)
1244 break; /* reached root directory */
1245 if ((dirp
= opendir("..")) == NULL
) {
1246 (void) xsnprintf(pathname
, pathlen
, CGETS(23, 19,
1247 "getcwd: Cannot open \"..\" (%s)"), strerror(errno
));
1250 if (chdir("..") == -1) {
1251 (void) xsnprintf(pathname
, pathlen
, CGETS(23, 20,
1252 "getcwd: Cannot chdir to \"..\" (%s)"), strerror(errno
));
1256 if ((dir
= readdir(dirp
)) == NULL
) {
1257 (void) xsnprintf(pathname
, pathlen
,
1258 CGETS(23, 21, "getcwd: Read error in \"..\" (%s)"),
1262 if (stat(dir
->d_name
, &dd
) == -1) {
1263 (void) xsnprintf(pathname
, pathlen
,
1264 CGETS(23, 25, "getcwd: Cannot stat directory \"%s\" (%s)"),
1265 dir
->d_name
, strerror(errno
));
1268 } while (dd
.st_ino
!= d
.st_ino
||
1269 dd
.st_dev
!= d
.st_dev
||
1270 dd
.st_size
!= d
.st_size
);
1273 pnptr
= strnrcpy(dirp
->d_name
, pnptr
, pnptr
- pathbuf
);
1274 pnptr
= strnrcpy("/", pnptr
, pnptr
- pathbuf
);
1277 if (*pnptr
== '\0') /* current dir == root dir */
1278 (void) strncpy(pathname
, "/", pathlen
);
1280 (void) strncpy(pathname
, pnptr
, pathlen
);
1281 pathname
[pathlen
- 1] = '\0';
1282 if (chdir(pnptr
) == -1) {
1283 (void) xsnprintf(pathname
, MAXPATHLEN
, CGETS(23, 22,
1284 "getcwd: Cannot change back to \".\" (%s)"),
1293 (void) chdir(strnrcpy(".", pnptr
, pnptr
- pathbuf
));
1298 # else /* ! hp9000s500 */
1302 xgetcwd(char *pathname
, size_t pathlen
)
1307 struct stat st_root
, st_cur
, st_next
, st_dotdot
;
1308 char pathbuf
[MAXPATHLEN
], nextpathbuf
[MAXPATHLEN
* 2];
1309 char *pathptr
, *nextpathptr
, *cur_name_add
;
1312 /* find the inode of root */
1313 if (stat("/", &st_root
) == -1) {
1314 (void) xsnprintf(pathname
, pathlen
, CGETS(23, 23,
1315 "getcwd: Cannot stat \"/\" (%s)"),
1319 pathbuf
[MAXPATHLEN
- 1] = '\0';
1320 pathptr
= &pathbuf
[MAXPATHLEN
- 1];
1321 nextpathbuf
[MAXPATHLEN
- 1] = '\0';
1322 cur_name_add
= nextpathptr
= &nextpathbuf
[MAXPATHLEN
- 1];
1324 /* find the inode of the current directory */
1325 if (lstat(".", &st_cur
) == -1) {
1326 (void) xsnprintf(pathname
, pathlen
, CGETS(23, 24,
1327 "getcwd: Cannot stat \".\" (%s)"),
1331 nextpathptr
= strnrcpy(nextpathptr
, "../", nextpathptr
- nextpathbuf
);
1333 /* Descend to root */
1336 /* look if we found root yet */
1337 if (st_cur
.st_ino
== st_root
.st_ino
&&
1338 DEV_DEV_COMPARE(st_cur
.st_dev
, st_root
.st_dev
)) {
1339 (void) strncpy(pathname
, *pathptr
!= '/' ? "/" : pathptr
, pathlen
);
1340 pathname
[pathlen
- 1] = '\0';
1344 /* open the parent directory */
1345 if (stat(nextpathptr
, &st_dotdot
) == -1) {
1346 (void) xsnprintf(pathname
, pathlen
, CGETS(23, 25,
1347 "getcwd: Cannot stat directory \"%s\" (%s)"),
1348 nextpathptr
, strerror(errno
));
1351 if ((dp
= opendir(nextpathptr
)) == NULL
) {
1352 (void) xsnprintf(pathname
, pathlen
, CGETS(23, 26,
1353 "getcwd: Cannot open directory \"%s\" (%s)"),
1354 nextpathptr
, strerror(errno
));
1358 /* look in the parent for the entry with the same inode */
1359 if (DEV_DEV_COMPARE(st_dotdot
.st_dev
, st_cur
.st_dev
)) {
1360 /* Parent has same device. No need to stat every member */
1361 for (d
= readdir(dp
); d
!= NULL
; d
= readdir(dp
)) {
1363 if (((unsigned long)d
->d_ino
& 0xffff) == st_cur
.st_ino
)
1366 if (d
->d_ino
== st_cur
.st_ino
)
1373 * Parent has a different device. This is a mount point so we
1374 * need to stat every member
1376 for (d
= readdir(dp
); d
!= NULL
; d
= readdir(dp
)) {
1377 if (ISDOT(d
->d_name
) || ISDOTDOT(d
->d_name
))
1379 (void)strncpy(cur_name_add
, d
->d_name
,
1380 (size_t) (&nextpathbuf
[sizeof(nextpathbuf
) - 1] - cur_name_add
));
1381 if (lstat(nextpathptr
, &st_next
) == -1) {
1383 * We might not be able to stat() some path components
1384 * if we are using afs, but this is not an error as
1385 * long as we find the one we need; we also save the
1386 * first error to report it if we don't finally succeed.
1388 if (save_errno
== 0)
1392 /* check if we found it yet */
1393 if (st_next
.st_ino
== st_cur
.st_ino
&&
1394 DEV_DEV_COMPARE(st_next
.st_dev
, st_cur
.st_dev
))
1399 (void) xsnprintf(pathname
, pathlen
, CGETS(23, 27,
1400 "getcwd: Cannot find \".\" in \"..\" (%s)"),
1401 strerror(save_errno
? save_errno
: ENOENT
));
1408 pathptr
= strnrcpy(pathptr
, d
->d_name
, pathptr
- pathbuf
);
1409 pathptr
= strnrcpy(pathptr
, "/", pathptr
- pathbuf
);
1410 nextpathptr
= strnrcpy(nextpathptr
, "../", nextpathptr
- nextpathbuf
);
1411 *cur_name_add
= '\0';
1415 # endif /* hp9000s500 */
1418 * Like strncpy, going backwards and returning the new pointer
1421 strnrcpy(char *ptr
, char *str
, size_t siz
)
1423 int len
= strlen(str
);
1427 while (len
&& siz
--)
1428 *--ptr
= str
[--len
];
1431 } /* end strnrcpy */
1432 #endif /* !HAVE_GETCWD */
1438 #include <apollo/base.h>
1439 #include <apollo/loader.h>
1440 #include <apollo/error.h>
1444 apperr(status_$t
*st
)
1446 static char *buf
; /* = NULL */
1447 short e_subl
, e_modl
, e_codel
;
1448 error_$string_t e_sub
, e_mod
, e_code
;
1450 error_$
get_text(*st
, e_sub
, &e_subl
, e_mod
, &e_modl
, e_code
, &e_codel
);
1451 e_sub
[e_subl
] = '\0';
1452 e_code
[e_codel
] = '\0';
1453 e_mod
[e_modl
] = '\0';
1455 buf
= xasprintf("%s (%s/%s)", e_code
, e_sub
, e_mod
);
1463 short len
= Strlen(s
);
1467 loader_$
inlib(t
= short2str(s
), len
, &st
);
1468 if (st
.all
!= status_$ok
)
1469 stderror(ERR_SYSTEM
, t
, apperr(&st
));
1474 doinlib(Char
**v
, struct command
*c
)
1478 setname(short2str(*v
++));
1479 v
= glob_all_or_error(v
);
1481 cleanup_push(globbed
, blk_cleanup
);
1485 cleanup_until(globbed
);
1491 if (eq(v
, STRbsd43
))
1493 else if (eq(v
, STRsys53
))
1496 stderror(ERR_NAME
| ERR_SYSTEM
, short2str(v
),
1497 CGETS(23, 28, "Invalid system type"));
1504 dover(Char
**v
, struct command
*c
)
1508 setname(short2str(*v
++));
1510 if (!(p
= tgetenv(STRSYSTYPE
)))
1511 stderror(ERR_NAME
| ERR_STRING
,
1512 CGETS(23, 29, "System type is not set"));
1516 tsetenv(STRSYSTYPE
, getv(*v
) ? STRbsd43
: STRsys53
);
1522 * Many thanks to rees@citi.umich.edu (Jim Rees) and
1523 * mathys@ssdt-tempe.sps.mot.com (Yves Mathys)
1524 * For figuring out how to do this... I could have never done
1525 * it without their help.
1527 typedef short enum {
1530 name_$node_dir_type
,
1535 dorootnode(Char
**v
, struct command
*c
)
1537 name_$dir_type_t dirtype
= name_$node_dir_type
;
1543 setname(short2str(*v
++));
1545 name
= short2str(*v
);
1546 namelen
= strlen(name
);
1548 name_$
resolve(name
, &namelen
, &uid
, &st
);
1549 if (st
.all
!= status_$ok
)
1550 stderror(ERR_SYSTEM
, name
, apperr(&st
));
1552 name_$
set_diru(&uid
, "", &namelen
, &dirtype
, &st
);
1553 if (st
.all
!= status_$ok
)
1554 stderror(ERR_SYSTEM
, name
, apperr(&st
));
1561 static int res
= -1;
1562 static status_$t st
;
1574 st
.all
= status_$ok
;
1577 res
= stream_$
isavt(&strm
, &st
);
1581 if (st
.all
!= status_$ok
)
1582 stderror(ERR_SYSTEM
, "stream_$isavt", apperr(&st
));
1590 /* Android (<= 2.1?) has an incomplete ttyname implementation. */
1596 static char ttyname
[32];
1601 (void)snprintf(path
, sizeof(path
), "/proc/self/fd/%d", fd
);
1602 siz
= readlink(path
, ttyname
, sizeof(ttyname
));
1603 if (siz
< 0 || siz
== sizeof(ttyname
))
1605 ttyname
[siz
] = '\0';
1608 #endif /* __ANDROID__ */
1610 #if defined(__CYGWIN__) && !defined(NO_CRYPT)
1611 #undef CHAR /* Collides with Win32 API */
1612 #define WIN32_LEAN_AND_MEAN
1613 #include <windows.h>
1614 #include <sys/cygwin.h>
1616 cygwin_xcrypt(struct passwd
*pw
, const char *password
, const char *expected_pwd
)
1618 static char invalid_password
[] = "\377";
1619 HANDLE token
= cygwin_logon_user(pw
, password
);
1620 if (token
== INVALID_HANDLE_VALUE
)
1621 return invalid_password
;
1623 return (char *) expected_pwd
;
1625 #endif /* __CYGWIN__ && !NO_CRYPT */