Fixes for OpenBSD
[oweals/cde.git] / cde / programs / dtksh / dtksh.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 libraries 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: dtksh.c /main/3 1995/11/01 15:53:19 rswiston $ */
24 /*      Copyright (c) 1991, 1992 UNIX System Laboratories, Inc. */
25 /*      All Rights Reserved     */
26
27 /*      THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF          */
28 /*      UNIX System Laboratories, Inc.                          */
29 /*      The copyright notice above does not evidence any        */
30 /*      actual or intended publication of such source code.     */
31
32
33 #include "stdio.h"
34 #include <sys/stat.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #define CONSTCHAR (const char *)
39 #define TRUE 1
40 #define FALSE 0
41
42 #ifndef DTKSHBINDIR
43 #define DTKSHBINDIR "/usr/bin"
44 #endif
45
46 static int
47 FileExists(dir, file)
48 char *dir, *file;
49 {
50         struct stat sbuf;
51         char path[1024];
52
53         sprintf(path, "%s/%s", dir, file);
54         return(stat(path, &sbuf) != -1);
55 }
56
57 /*
58  * Bootstrap dtksh by calling xmcoeksh and forcing it to execute
59  * an rc file that calls the dtksh init function and does some
60  * other minor housekeeping.
61  *
62  * The rc file then sees if there was a previous user rc file (in $_HOLDENV_)
63  * and if so executes it.
64  */
65
66 int
67 main(argc, argv)
68 int argc;
69 char *argv[];
70 {
71         char *env;
72         char *bindir = NULL;
73         char *executable, *envfile;
74         char *buf;
75         char envbuf[1024];
76
77         /*
78          * Set the ENV variable to the standard dtksh rc file, which
79          * will do a libdirload of the dtksh shared object file and add all
80          * the dtksh commands and widgets to the exksh.  If the user already
81          * had an ENV file, then set the variable _HOLDENV_ to it so the
82          * standard dtksh rc file can execute it later.
83          */
84         env = getenv((const char *)"ENV");
85         buf = (char *)malloc((env ? strlen(env) : 0) + 12);
86         strcpy(buf, "_HOLDENV_=");
87         strcat(buf, env ? env : "");
88         putenv(buf); 
89
90         executable = "xmcoeksh";
91         envfile = "xmcoeksh.rc";
92
93         /*
94          * Search order for DTKSH binaries:
95          *
96          * 1. if $DTKSHBINDIR is set, use that path if it exists.
97          * 2. if the hard-wired #define DTKSHBINDIR is set and is a 
98          *    readable directory, use it.
99          * 3. punt.
100          */
101
102         if ((bindir = getenv((const char *)"DTKSHBINDIR")) != NULL) {
103            if ((!FileExists(bindir, executable)) ||
104                (!FileExists(bindir, envfile)))
105            {
106               bindir = NULL;
107            }
108         }
109
110         if (bindir == NULL) 
111         {
112            bindir = DTKSHBINDIR;
113
114            if ((!FileExists(bindir, executable)) ||
115                (!FileExists(bindir, envfile)))
116            {
117               bindir = NULL;
118            }
119         }
120
121         if (bindir == NULL) {
122            fprintf(stderr, 
123                    "dtksh: bootstrap failed.  Unable to locate both\nxmcoeksh and xmcoeksh.rc in the same directory.\n Try setting $DTKSHBINDIR.\n");
124            exit(1);
125         }
126         sprintf(envbuf, "DTKSHBINDIR=%s", bindir);
127         putenv(strdup(envbuf));
128
129         sprintf(envbuf, "ENV=%s/%s", bindir, envfile);
130         putenv(strdup(envbuf));
131         sprintf(envbuf, "%s/%s", bindir, executable);
132         execv(envbuf, argv);
133
134         fprintf(stderr, "dtksh: Bootstrap of dtksh failed: '%s'\n", envbuf);
135         perror("Reason");
136         return(1);      /* failure */
137 }