extra.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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: extra.c /main/4 1995/11/01 15:54:55 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 "defs.h"
  31. #include "shell.h"
  32. #include "name.h"
  33. #include "stdio.h"
  34. #include "msgs.h"
  35. void
  36. env_set(
  37. char *var )
  38. {
  39. (void)nv_open(var, sh.var_tree, NV_ASSIGN);
  40. }
  41. void
  42. env_set_gbl(
  43. char *vareqval )
  44. {
  45. env_set(vareqval);
  46. }
  47. char *
  48. env_get(
  49. char *var )
  50. {
  51. Namval_t *np;
  52. char *val;
  53. np = nv_search(var,sh.var_tree,0);
  54. if (np && (val = nv_getval(np)))
  55. return(val);
  56. return(NULL);
  57. }
  58. int
  59. ksh_eval(
  60. char *cmd )
  61. {
  62. sh_eval(sfopen(NIL(Sfio_t*),cmd,"s"),0);
  63. sfsync(sh.outpool);
  64. return(sh.exitval);
  65. }
  66. void
  67. env_set_var(
  68. char *var,
  69. char *val )
  70. {
  71. int len;
  72. char tmp[512];
  73. char *set = &tmp[0];
  74. if ((len = strlen(var) + strlen(val?val:"") + 2) > sizeof(tmp)) /* 11/06 CHANGED */
  75. set = malloc(len);
  76. strcpy(set, var);
  77. strcat(set, "=");
  78. strcat(set, val?val:""); /* 11/06 CHANGED */
  79. env_set(set);
  80. if (set != &tmp[0])
  81. free(set);
  82. }
  83. void
  84. env_blank(
  85. char *var )
  86. {
  87. env_set_var(var, "");
  88. }
  89. void
  90. printerr(
  91. char *cmd,
  92. char *msg1,
  93. char *msg2 )
  94. {
  95. if (msg1 == NULL)
  96. msg1 = "";
  97. if (msg2 == NULL)
  98. msg2 = "";
  99. if (cmd && (strlen(cmd) > 0))
  100. printf( "%s: %s %s\n", cmd, msg1, msg2);
  101. else
  102. printf( "%s %s\n", msg1, msg2);
  103. }
  104. void
  105. printerrf(
  106. char *cmd,
  107. char *fmt,
  108. char *arg0,
  109. char *arg1,
  110. char *arg2,
  111. char *arg3,
  112. char *arg4,
  113. char *arg5,
  114. char *arg6,
  115. char *arg7 )
  116. {
  117. char buf[2048];
  118. if (arg0 == NULL)
  119. arg0 = "";
  120. if (arg1 == NULL)
  121. arg1 = "";
  122. if (arg2 == NULL)
  123. arg2 = "";
  124. if (arg3 == NULL)
  125. arg3 = "";
  126. if (arg4 == NULL)
  127. arg4 = "";
  128. if (arg5 == NULL)
  129. arg5 = "";
  130. if (arg6 == NULL)
  131. arg6 = "";
  132. if (arg7 == NULL)
  133. arg7 = "";
  134. sprintf(buf, fmt, arg0, arg1, arg2, arg3,arg4, arg5, arg6, arg7);
  135. if (cmd && (strlen(cmd) > 0))
  136. printf("%s: %s\n", cmd, buf);
  137. else
  138. printf("%s\n", buf);
  139. }
  140. /****************************************************************************
  141. *
  142. * The following two functions are ugly, but necessary. Ksh reserves file
  143. * descriptors 0 - 9 for use by shell scripts, and has intimate knowledge
  144. * of how and when they were opened. Unfortunately, certain dtksh functions
  145. * (XtInitialize, catopen, ttdt_open, _DtActionInvoke, others) open file
  146. * descriptors which are not known to ksh. We can't let these file
  147. * descriptors fall in the 0 - 9 range, because we can't afford to have
  148. * the shell script overriding our file descriptors. Therefore, any of
  149. * our commands which open files must first lock our file descriptors 0 - 9,
  150. * thus forcing the command to get a file descriptor out of the shell's
  151. * range. After the command has opened its file descriptor, it then needs
  152. * to unlock file descriptors 0 - 9, so that the shell script will have
  153. * access to them again.
  154. *
  155. **************************************************************************/
  156. /*
  157. * Return a list of the file descriptors we had to open, to lock out file
  158. * descriptors 0 - 9; this list should be freed (and the file descriptors
  159. * closed) by calling UnlockkshFileDescriptors().
  160. */
  161. int *LockKshFileDescriptors(void)
  162. {
  163. int * fdList;
  164. int i;
  165. int fd, newfd;
  166. fdList = (int *)malloc(sizeof(int) * 10);
  167. for (i = 0; i < 10; i++)
  168. fdList[i] = -1;
  169. if ((fd = open("/dev/null", O_RDONLY)) >= 0)
  170. {
  171. if (fd < 10)
  172. {
  173. fdList[0] = fd;
  174. for (i = 1; i < 10; i++)
  175. {
  176. if ((newfd = dup(fd)) < 10)
  177. fdList[i] = newfd;
  178. else
  179. {
  180. close(newfd);
  181. break;
  182. }
  183. }
  184. }
  185. else
  186. close(fd);
  187. }
  188. return(fdList);
  189. }
  190. void UnlockKshFileDescriptors(int *fdList)
  191. {
  192. int i;
  193. for (i = 0; i < 10; i++)
  194. {
  195. if (fdList[i] != (-1))
  196. close(fdList[i]);
  197. }
  198. free((char *)fdList);
  199. }