dtksh.c 3.7 KB

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