Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / cmd / ksh93 / sh / apollo.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: apollo.c /main/4 1996/10/04 15:52:17 drk $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
28 *        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF        *
29 *                    AT&T BELL LABORATORIES                    *
30 *         AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN         *
31 *            ACCORDANCE WITH APPLICABLE AGREEMENTS             *
32 *                                                              *
33 *                Copyright (c) 1995 AT&T Corp.                 *
34 *              Unpublished & Not for Publication               *
35 *                     All Rights Reserved                      *
36 *                                                              *
37 *       The copyright notice above does not evidence any       *
38 *      actual or intended publication of such source code      *
39 *                                                              *
40 *               This software was created by the               *
41 *           Advanced Software Technology Department            *
42 *                    AT&T Bell Laboratories                    *
43 *                                                              *
44 *               For further information contact                *
45 *                    {research,attmail}!dgk                    *
46 *                                                              *
47 ***************************************************************/
48
49 /* : : generated by proto : : */
50
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__      "C"
55 #else
56 #define __MANGLE__
57 #endif
58 #define __STDARG__
59 #define __PROTO__(x)    x
60 #define __OTORP__(x)
61 #define __PARAM__(n,o)  n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
64 #define const
65 #endif
66 #define signed
67 #define void            int
68 #define volatile
69 #define __V_            char
70 #else
71 #define __V_            void
72 #endif
73 #else
74 #define __PROTO__(x)    ()
75 #define __OTORP__(x)    x
76 #define __PARAM__(n,o)  o
77 #define __MANGLE__
78 #define __V_            char
79 #define const
80 #define signed
81 #define void            int
82 #define volatile
83 #endif
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__      ...
86 #else
87 #define __VARARG__
88 #endif
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a)       va_start(p,a)
91 #else
92 #define __VA_START__(p,a)       va_start(p)
93 #endif
94 #endif
95 #include        <ast.h>
96 #include        "defs.h"
97 #include        <errno.h>
98
99 #ifdef apollo
100 #include "/sys/ins/base.ins.c"
101 #include "/sys/ins/pad.ins.c"
102 #include "/sys/ins/error.ins.c"
103 #include <sys/param.h>  /* for maximum pathname length */
104 #include <apollo/sys/ubase.h>
105 #include <apollo/sys/name.h>
106 #include <apollo/error.h>
107
108 int pad_create __PARAM__((char *fname), (fname)) __OTORP__(char *fname;){
109         short oldfd = 1;
110         short newfd;
111         short size = 25;
112         long st;
113
114         pad_$create (*fname, (short)strlen(fname), pad_$edit, oldfd, 
115             pad_$bottom, 0, size, newfd, st);
116         if (st != 0)
117                 error(ERROR_system(1),e_open,"dm pad");
118         return(newfd);
119 }
120
121 pad_wait __PARAM__((int fd), (fd)) __OTORP__(int fd;){
122         long st;
123
124         pad_$edit_wait((stream_$id_t)fd, st);
125
126         return (st == 0 ? 0 : 1);
127
128 }
129
130 char *apollo_error __PARAM__((void), ()){
131         extern __MANGLE__ long unix_proc_$status;
132         char subsys[80], module[80], code[80];
133         short slen, mlen, clen;
134         static char retstr[256];
135
136         error_$get_text (unix_proc_$status, subsys, slen, 
137                 module, mlen, code, clen);
138         subsys[slen] = module[mlen] = code[clen] = 0;
139         if (clen == 0)
140                 sprintf (code, "status 0x%08lx", unix_proc_$status);
141         if ( mlen )
142                 sprintf(retstr, "%s (%s/%s)", code, subsys, module );
143         else
144                 sprintf(retstr, "%s (%s)", code, subsys );              
145
146         return (retstr);
147 }
148
149 /*
150  * declarations to support the apollo builtin commands 
151  * rootnode, inlib, and ver.
152  */
153
154 static char last_rootnode[MAXPATHLEN] = "/";
155 static char do_ver;
156 static char *preval = NULL, *sysval, *sysid = "SYSTYPE";
157
158 /* 
159  * code to support the apollo builtin functions rootnode, 
160  * inlib, and ver.
161  */
162
163 int     b_rootnode __PARAM__((int argn,char *argv[]), (argn, argv)) __OTORP__(int argn;char *argv[];){
164         if (argn == 1) 
165         {       /* report current setting */
166                 sfprintf(sfstdout,"%s\n",last_rootnode);
167                         return(0);
168         }
169         if (!is_valid_rootnode(argv[1])) 
170                 sh_cfail(e_rootnode);
171         if (rootnode(argv[1]) != 0) 
172         {
173                 perror("rootnode: ");   /* ? */
174                 sh_cfail(e_rootnode);
175         }
176         if (argn == 2)
177                 strcpy(last_rootnode, argv[1]);
178         else 
179         {
180                 sysval = argv[1];
181                 sh_eval(sh_sfeval(argv),0);
182                 if (rootnode(last_rootnode) != 0) 
183                         sh_cfail(e_rootnode);
184         }
185         return(0);
186 }
187
188 int     b_ver __PARAM__((int argn,char *argv[]), (argn, argv)) __OTORP__(int argn;char *argv[];){
189         char *oldver;
190         short i1, i2;
191         std_$call unsigned char c_$decode_version(__VARARG__);
192
193         oldver = SYSTYPENOD->namval.cp;
194         if (argn == 1 || argn > 2) 
195         {
196                 sysval = NULL;
197                 if (oldver)
198                         preval = sysval = oldver;
199         }
200         if (argn == 1) 
201         {
202                 if (!oldver || !sysval)
203                         sh_cfail(e_nover);
204                 else 
205                 {
206                         sfprintf(sfstdout,"%s\n",sysval);
207                 }
208         }
209         else 
210         {
211                 if (!c_$decode_version (*argv[1], (short) strlen (argv[1]), i1, i2))
212                         sh_cfail(e_badver);
213                 else 
214                 {
215                         if (argn == 2) 
216                         {
217                                 short namlen = strlen(sysid);
218                                 short arglen = strlen(argv[1]);
219                                  
220                                 nv_unset(SYSTYPENOD);
221                                 nv_putval(SYSTYPENOD, argv[1],NV_RDONLY);
222                                 nv_onattr(SYSTYPENOD, NV_EXPORT | NV_NOFREE);
223                                 ev_$set_var (sysid, &namlen, argv[1], &arglen);
224                         }
225                         else 
226                         {
227                                 int fd;
228                                 short namlen = strlen(sysid);
229                                 short arglen = strlen(argv[1]);
230
231                                 sysval = argv[1];
232                                 argv = &argv[2];
233                                 sh_eval(sh_sfeval(argv),0);
234                                 ev_$set_var(sysid, &namlen, sysval, &arglen);
235                                 if((fd=path_open(argv[0],path_get(argv[0]))) < 0)
236                                 {
237                                         arglen = (short)strlen(preval);
238                                         ev_$set_var (sysid, &namlen, preval, &arglen);
239                                         error(ERROR_system(1),e_open,argv[0]);
240                                 }
241                                 close(fd);
242                                 sh_eval(sfopen(argv[0],"s"),0);
243                                 arglen = (short)strlen(preval);
244                                 ev_$set_var (sysid, &namlen, preval, &arglen);
245                         }
246                 }
247          }
248         return(sh.exitval);
249 }
250
251 /*
252  * rootnode.c - a chroot call which doesn't require you to be root...
253  */
254
255 /*
256  *  Changes:
257         01/24/88 brian  Initial coding
258  */
259                   
260
261 #ifndef NULL
262 # define        NULL    ((__V_ *) 0)
263 #endif
264
265 extern __MANGLE__ boolean
266 unix_fio_$status_to_errno __PROTO__((
267                 status_$t       & status,
268                 char            * pn,
269                 short           & pnlen                  
270 ));
271
272 is_valid_rootnode __PARAM__((const char *path), (path)) __OTORP__(const char *path;){
273         if (geteuid() == 0)
274                 return 1;
275         return (path[0] == '/' && path[1] == '/' && path[2] != '\0' &&
276                 strchr(&path[2], '/') == NULL);
277 }
278
279 rootnode __PARAM__((char * path), (path)) __OTORP__(char * path;){
280         uid_$t          dir_uid, rtn_uid;
281         name_$pname_t   new_root_name, rest_path;
282         name_$name_t    leaf;
283         short           rest_len, leaf_len, err;
284         status_$t       status;
285         
286         strcpy(new_root_name, path);
287
288         name_$resolve_afayc(new_root_name, (short)strlen(new_root_name), 
289                 &dir_uid, &rtn_uid, rest_path, &rest_len, leaf, &leaf_len, &err, &status);
290
291         if (status.all != status_$ok) {
292                 unix_fio_$status_to_errno(status, path, strlen(path));
293                 return (-1);
294         }
295
296         name_$set_diru(rtn_uid, rest_path, (short) rest_len, name_$node_dir_type, &status);
297          
298         if (status.all != status_$ok) {
299                 unix_fio_$status_to_errno(status, path, strlen(path));
300                 return(-1);
301         }
302         return(0);
303 }
304
305 #endif /* apollo */
306
307 /*
308  *  Apollo system support library loads into the virtual address space
309  */
310
311 int     b_inlib(argc,argv)
312 char **argv;
313 {
314         register char *a1 = argv[1];
315         int status;
316         short len;
317         std_$call void loader_$inlib(__VARARG__);
318         if(sh.subshell)
319                 sh_subfork();
320         if(a1)
321         {
322                 len = strlen(a1);
323                 loader_$inlib(*a1, len, status);
324                 if(status!=0)
325                         error(3, e_badinlib);
326         }
327         return(0);
328 }