Link with C++ linker
[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 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: 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 #include <malloc.h>
38
39 #define CONSTCHAR (const char *)
40 #define TRUE 1
41 #define FALSE 0
42
43 #ifndef DTKSHBINDIR
44 #define DTKSHBINDIR "/usr/bin"
45 #endif
46
47 static int
48 FileExists(dir, file)
49 char *dir, *file;
50 {
51         struct stat sbuf;
52         char path[1024];
53
54         sprintf(path, "%s/%s", dir, file);
55         return(stat(path, &sbuf) != -1);
56 }
57
58 /*
59  * Bootstrap dtksh by calling xmcoeksh and forcing it to execute
60  * an rc file that calls the dtksh init function and does some
61  * other minor housekeeping.
62  *
63  * The rc file then sees if there was a previous user rc file (in $_HOLDENV_)
64  * and if so executes it.
65  */
66
67 int
68 main(argc, argv)
69 int argc;
70 char *argv[];
71 {
72         char *env;
73         char *bindir = NULL;
74         char *executable, *envfile;
75         char *buf;
76         char envbuf[1024];
77
78         /*
79          * Set the ENV variable to the standard dtksh rc file, which
80          * will do a libdirload of the dtksh shared object file and add all
81          * the dtksh commands and widgets to the exksh.  If the user already
82          * had an ENV file, then set the variable _HOLDENV_ to it so the
83          * standard dtksh rc file can execute it later.
84          */
85         env = getenv((const char *)"ENV");
86         buf = (char *)malloc((env ? strlen(env) : 0) + 12);
87         strcpy(buf, "_HOLDENV_=");
88         strcat(buf, env ? env : "");
89         putenv(buf); 
90
91         executable = "xmcoeksh";
92         envfile = "xmcoeksh.rc";
93
94         /*
95          * Search order for DTKSH binaries:
96          *
97          * 1. if $DTKSHBINDIR is set, use that path if it exists.
98          * 2. if the hard-wired #define DTKSHBINDIR is set and is a 
99          *    readable directory, use it.
100          * 3. punt.
101          */
102
103         if ((bindir = getenv((const char *)"DTKSHBINDIR")) != NULL) {
104            if ((!FileExists(bindir, executable)) ||
105                (!FileExists(bindir, envfile)))
106            {
107               bindir = NULL;
108            }
109         }
110
111         if (bindir == NULL) 
112         {
113            bindir = DTKSHBINDIR;
114
115            if ((!FileExists(bindir, executable)) ||
116                (!FileExists(bindir, envfile)))
117            {
118               bindir = NULL;
119            }
120         }
121
122         if (bindir == NULL) {
123            fprintf(stderr, 
124                    "dtksh: bootstrap failed.  Unable to locate both\nxmcoeksh and xmcoeksh.rc in the same directory.\n Try setting $DTKSHBINDIR.\n");
125            exit(1);
126         }
127         sprintf(envbuf, "DTKSHBINDIR=%s", bindir);
128         putenv(strdup(envbuf));
129
130         sprintf(envbuf, "ENV=%s/%s", bindir, envfile);
131         putenv(strdup(envbuf));
132         sprintf(envbuf, "%s/%s", bindir, executable);
133         execv(envbuf, argv);
134
135         fprintf(stderr, "dtksh: Bootstrap of dtksh failed: '%s'\n", envbuf);
136         perror("Reason");
137         return(1);      /* failure */
138 }