pex-msdos.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Utilities to execute a program in a subprocess (possibly linked by pipes
  2. with other subprocesses), and wait for it. Generic MSDOS specialization.
  3. Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2005
  4. Free Software Foundation, Inc.
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with libiberty; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  17. Boston, MA 02110-1301, USA. */
  18. #include "pex-common.h"
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #ifdef NEED_DECLARATION_ERRNO
  22. extern int errno;
  23. #endif
  24. #ifdef HAVE_STRING_H
  25. #include <string.h>
  26. #endif
  27. #ifdef HAVE_STDLIB_H
  28. #include <stdlib.h>
  29. #endif
  30. #include "safe-ctype.h"
  31. #include <process.h>
  32. /* The structure we keep in obj->sysdep. */
  33. #define PEX_MSDOS_FILE_COUNT 3
  34. #define PEX_MSDOS_FD_OFFSET 10
  35. struct pex_msdos
  36. {
  37. /* An array of file names. We refer to these using file descriptors
  38. of 10 + array index. */
  39. const char *files[PEX_MSDOS_FILE_COUNT];
  40. /* Exit statuses of programs which have been run. */
  41. int *statuses;
  42. };
  43. static int pex_msdos_open (struct pex_obj *, const char *, int);
  44. static int pex_msdos_open (struct pex_obj *, const char *, int);
  45. static int pex_msdos_fdindex (struct pex_msdos *, int);
  46. static pid_t pex_msdos_exec_child (struct pex_obj *, int, const char *,
  47. char * const *, char * const *,
  48. int, int, int, int,
  49. int, const char **, int *);
  50. static int pex_msdos_close (struct pex_obj *, int);
  51. static pid_t pex_msdos_wait (struct pex_obj *, pid_t, int *, struct pex_time *,
  52. int, const char **, int *);
  53. static void pex_msdos_cleanup (struct pex_obj *);
  54. /* The list of functions we pass to the common routines. */
  55. const struct pex_funcs funcs =
  56. {
  57. pex_msdos_open,
  58. pex_msdos_open,
  59. pex_msdos_exec_child,
  60. pex_msdos_close,
  61. pex_msdos_wait,
  62. NULL, /* pipe */
  63. NULL, /* fdopenr */
  64. NULL, /* fdopenw */
  65. pex_msdos_cleanup
  66. };
  67. /* Return a newly initialized pex_obj structure. */
  68. struct pex_obj *
  69. pex_init (int flags, const char *pname, const char *tempbase)
  70. {
  71. struct pex_obj *ret;
  72. int i;
  73. /* MSDOS does not support pipes. */
  74. flags &= ~ PEX_USE_PIPES;
  75. ret = pex_init_common (flags, pname, tempbase, funcs);
  76. ret->sysdep = XNEW (struct pex_msdos);
  77. for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
  78. ret->files[i] = NULL;
  79. ret->statuses = NULL;
  80. return ret;
  81. }
  82. /* Open a file. FIXME: We ignore the binary argument, since we have
  83. no way to handle it. */
  84. static int
  85. pex_msdos_open (struct pex_obj *obj, const char *name,
  86. int binary ATTRIBUTE_UNUSED)
  87. {
  88. struct pex_msdos *ms;
  89. int i;
  90. ms = (struct pex_msdos *) obj->sysdep;
  91. for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
  92. {
  93. if (ms->files[i] == NULL)
  94. {
  95. ms->files[i] = xstrdup (name);
  96. return i + PEX_MSDOS_FD_OFFSET;
  97. }
  98. }
  99. abort ();
  100. }
  101. /* Get the index into msdos->files associated with an open file
  102. descriptor. */
  103. static int
  104. pex_msdos_fdindex (struct pex_msdos *ms, int fd)
  105. {
  106. fd -= PEX_MSDOS_FD_OFFSET;
  107. if (fd < 0 || fd >= PEX_MSDOS_FILE_COUNT || ms->files[fd] == NULL)
  108. abort ();
  109. return fd;
  110. }
  111. /* Close a file. */
  112. static int
  113. pex_msdos_close (struct pex_obj *obj, int fd)
  114. {
  115. struct pex_msdos *ms;
  116. int fdinex;
  117. ms = (struct pex_msdos *) obj->sysdep;
  118. fdindex = pe_msdos_fdindex (ms, fd);
  119. free (ms->files[fdindex]);
  120. ms->files[fdindex] = NULL;
  121. }
  122. /* Execute a child. */
  123. static pid_t
  124. pex_msdos_exec_child (struct pex_obj *obj, int flags, const char *executable,
  125. char * const * argv, char * const * env, int in, int out,
  126. int toclose ATTRIBUTE_UNUSED,
  127. int errdes ATTRIBUTE_UNUSED, const char **errmsg,
  128. int *err)
  129. {
  130. struct pex_msdos *ms;
  131. char *temp_base;
  132. int temp_base_allocated;
  133. char *rf;
  134. int inindex;
  135. char *infile;
  136. int outindex;
  137. char *outfile;
  138. char *scmd;
  139. FILE *argfile;
  140. int i;
  141. int status;
  142. ms = (struct pex_msdos *) obj->sysdep;
  143. /* FIXME: I don't know how to redirect stderr, so we ignore ERRDES
  144. and PEX_STDERR_TO_STDOUT. */
  145. temp_base = obj->temp_base;
  146. if (temp_base != NULL)
  147. temp_base_allocated = 0;
  148. else
  149. {
  150. temp_base = choose_temp_base ();
  151. temp_base_allocated = 1;
  152. }
  153. rf = concat (temp_base, ".gp", NULL);
  154. if (temp_base_allocated)
  155. free (temp_base);
  156. if (in == STDIN_FILE_NO)
  157. {
  158. inindex = -1;
  159. infile = "";
  160. }
  161. else
  162. {
  163. inindex = pex_msdos_fdindex (ms, in);
  164. infile = ms->files[inindex];
  165. }
  166. if (out == STDOUT_FILE_NO)
  167. {
  168. outindex = -1;
  169. outfile = "";
  170. }
  171. else
  172. {
  173. outindex = pex_msdos_fdindex (ms, out);
  174. outfile = ms->files[outindex];
  175. }
  176. scmd = XNEWVEC (char, strlen (program)
  177. + ((flags & PEXECUTE_SEARCH) != 0 ? 4 : 0)
  178. + strlen (rf)
  179. + strlen (infile)
  180. + strlen (outfile)
  181. + 10);
  182. sprintf (scmd, "%s%s @%s%s%s%s%s",
  183. program,
  184. (flags & PEXECUTE_SEARCH) != 0 ? ".exe" : "",
  185. rf,
  186. inindex != -1 ? " <" : "",
  187. infile,
  188. outindex != -1 ? " >" : "",
  189. outfile);
  190. argfile = fopen (rf, "w");
  191. if (argfile == NULL)
  192. {
  193. *err = errno;
  194. free (scmd);
  195. free (rf);
  196. *errmsg = "cannot open temporary command file";
  197. return (pid_t) -1;
  198. }
  199. for (i = 1; argv[i] != NULL; ++i)
  200. {
  201. char *p;
  202. for (p = argv[i]; *p != '\0'; ++p)
  203. {
  204. if (*p == '"' || *p == '\'' || *p == '\\' || ISSPACE (*p))
  205. putc ('\\', argfile);
  206. putc (*p, argfile);
  207. }
  208. putc ('\n', argfile);
  209. }
  210. fclose (argfile);
  211. status = system (scmd);
  212. if (status == -1)
  213. {
  214. *err = errno;
  215. remove (rf);
  216. free (scmd);
  217. free (rf);
  218. *errmsg = "system";
  219. return (pid_t) -1;
  220. }
  221. remove (rf);
  222. free (scmd);
  223. free (rf);
  224. /* Save the exit status for later. When we are called, obj->count
  225. is the number of children which have executed before this
  226. one. */
  227. ms->statuses = XRESIZEVEC(int, ms->statuses, obj->count + 1);
  228. ms->statuses[obj->count] = status;
  229. return (pid_t) obj->count;
  230. }
  231. /* Wait for a child process to complete. Actually the child process
  232. has already completed, and we just need to return the exit
  233. status. */
  234. static pid_t
  235. pex_msdos_wait (struct pex_obj *obj, pid_t pid, int *status,
  236. struct pex_time *time, int done ATTRIBUTE_UNUSED,
  237. const char **errmsg ATTRIBUTE_UNUSED,
  238. int *err ATTRIBUTE_UNUSED)
  239. {
  240. struct pex_msdos *ms;
  241. ms = (struct pex_msdos *) obj->sysdep;
  242. if (time != NULL)
  243. memset (time, 0, sizeof *time);
  244. *status = ms->statuses[pid];
  245. return 0;
  246. }
  247. /* Clean up the pex_msdos structure. */
  248. static void
  249. pex_msdos_cleanup (struct pex_obj *obj)
  250. {
  251. struct pex_msdos *ms;
  252. int i;
  253. ms = (struct pex_msdos *) obj->sysdep;
  254. for (i = 0; i < PEX_MSDOS_FILE_COUNT; ++i)
  255. free (msdos->files[i]);
  256. free (msdos->statuses);
  257. free (msdos);
  258. obj->sysdep = NULL;
  259. }