home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / ansi / stdlib / system.txh < prev   
Encoding:
Text File  |  1996-09-26  |  7.4 KB  |  163 lines

  1. @node system, process
  2. @subheading Syntax
  3.  
  4. @example
  5. #include <stdlib.h>
  6.  
  7. int system(const char *cmd);
  8. @end example
  9.  
  10. @subheading Description
  11.  
  12. This function runs the command or program specified by @var{cmd}.  When
  13. calling programs compiled by DJGPP this function will not use
  14. @file{command.com} and so will not be subject to its 126 character limit
  15. on command lines.
  16.  
  17. Command lines and pipes( i.e., the use of "<", ">", ">>", and "|")
  18. will be simulated internally in this function; this means that you can
  19. have both long command lines and redirection/pipes when running DJGPP
  20. programs with this function.
  21.  
  22. By default, @file{command.com} will only be invoked to run commands
  23. internal to it, or to run batch files (but this can be changed, see
  24. below).  In these cases, the returned error code will always be zero,
  25. since @file{command.com} always exits with code 0.
  26.  
  27. Certain commands internal to @file{command.com} that don't make sense or
  28. cause no effect in the context of @code{system} are ignored by this
  29. function.  These are @code{REM}, @code{EXIT}, @code{GOTO}, @code{SHIFT};
  30. @code{SET}, @code{PATH} and @code{PROMPT} are ignored only if called
  31. with an argument.  You can disable this feature if you need, see below.
  32.  
  33. Some commands are emulated internally by @code{system}, because the
  34. emulation is better than the original.  Currently, the only emulated
  35. command is @samp{CD} or @samp{CHDIR}: the emulation knows about forward
  36. slashes and also switches the current drive.  This emulation can also be
  37. switched off, as explained below.
  38.  
  39. When @code{system} is presented with an internal shell command, it
  40. checks the environment variables @samp{SHELL} and @samp{COMSPEC} (in
  41. that order) and invokes the program that they point to.  If the shell
  42. thus found is one of the DOS shells (@file{command.com}, @file{4DOS} or
  43. @file{NDOS}), they are called with the @samp{/c} switch prepended to the
  44. command line.  Otherwise, @code{system} assumes that the shell is a
  45. Unix-style shell and invokes it with a @samp{-c} switch, passing the
  46. entire command line via a response file (this should work with any shell
  47. compiled with DJGPP, and with the @code{ms_sh} shell).
  48.  
  49. Shell scripts and batch files are invoked by calling either the program
  50. whose name appears on the first line (like in @w{@samp{#! /bin/sh}}), or
  51. the default shell if none is specified by the script.  If the name of
  52. the shell specified by the script is a Unix-style pathname, without a
  53. drive letter and with no extension, @code{system} will additionally
  54. search for it on the @samp{PATH}.  This allows to invoke Unix shell
  55. scripts unmodified, if you have a ported shell installed on your
  56. system.
  57.  
  58. You can customize the behavior of @code{system} using a bit-mapped
  59. variable @var{__system_flags}, defined on @file{<stdlib.h>}.  The
  60. following bits are currently defined:
  61.  
  62. @table @code
  63. @item __system_redirect
  64. When set (the default), specifies that @code{system} can use its
  65. internal redirection and pipe code.  If reset, any command line that
  66. includes an unquoted redirection symbol will be passed to the shell.
  67.  
  68. @item __system_call_cmdproc
  69. When set, @code{system} will always call the shell to execute the
  70. command line.  If reset (the default), the shell will only be called
  71. when needed, as described above.
  72.  
  73. You should @emph{always} set this bit if you use a real, Unix-style
  74. shell (also, set @code{__system_use_shell}, described below, and the
  75. @samp{SHELL} environment variable).
  76.  
  77. @item __system_use_shell
  78. When set (the default), the @samp{SHELL} environment variable will take
  79. precedence upon @samp{COMSPEC}; this allows you to specify a special
  80. shell for @code{system} that doesn't affect the rest of DOS.  If reset,
  81. only @samp{COMSPEC} is used to find the name of the command processor.
  82.  
  83. @item __system_allow_multiple_cmds
  84. When set, you can put multiple commands together separated by the
  85. @samp{;} character.  If reset (the default), the command line passed to
  86. @code{system} is executed as a single command and @samp{;} has no
  87. special meaning.
  88.  
  89. @item __system_allow_long_cmds
  90. When set (the default), @code{system} will handle command lines longer
  91. than the DOS 126-character limit; this might crash your program in some
  92. cases, as the low-level functions that invoke the child program will
  93. only pass them the first 126 characters.  When reset, @code{system} will
  94. detect early that the command line is longer than 126 characters and
  95. refuse to run it, but you will not be able to call DJGPP programs with
  96. long command lines.
  97.  
  98. @item __system_emulate_command
  99. If reset (the default), @code{system} will pass the entire command line
  100. to the shell if its name is one of the following: @file{sh.exe},
  101. @file{sh16.exe}, @file{sh32.exe}, @file{bash.exe}, @file{tcsh.exe}.
  102. When set, @code{system} will attempt to emulate redirection and pipes
  103. internally, even if @samp{COMSPEC} or @samp{SHELL} point to a Unix-style
  104. shell.
  105.  
  106. @item __system_handle_null_commands
  107. When set (the default), commands internal to @file{COMMAND.COM} and
  108. compatible shells which have no effect in the context of @code{system},
  109. are ignored (the list of these commands was given above).  If reset,
  110. these commands are processed as all others, which means
  111. @file{COMMAND.COM} will be called to execute them.
  112.  
  113. Note that this bit shouldn't be used with a Unix-style shell, because it
  114. does the wrong thing then.  With Unix-style shells, you are supposed to
  115. set the @code{__system_call_cmdproc} bit which will always call the
  116. shell.
  117.  
  118. @item __system_ignore_chdir
  119. If set, the @samp{CD} and @samp{CHDIR} commands are ignored.  When reset
  120. (the default), the processing of these commands depends on the
  121. @code{__system_emulate_chdir} bit, see below.
  122.  
  123. This bit is for compatibility with Unix, where a single @samp{cd dir}
  124. command has no effect, because the current working directory there is
  125. not a global notion (as on MSDOS).  Don't set this bit if you use
  126. multiple commands (see @code{__system_allow_multiple_cmds} above).
  127.  
  128. @item __system_emulate_chdir
  129. When set, the @samp{CD} and @samp{CHDIR} commands are emulated
  130. internally: they change the drive when the argument specifies a drive
  131. letter, and they support both forward slashes and backslashes in
  132. pathnames.  When @samp{CD} is called without an argument, it prints the
  133. current working directory with forward slashes and down-cases DOS 8+3
  134. names.  If this bit is reset (the default), @samp{CD} and @samp{CHDIR}
  135. are passed to the shell.
  136. @end table
  137.  
  138. The behavior of @code{system} can be customized at run time by defining
  139. the variable @samp{DJSYSFLAGS} in the environment.  The value of that
  140. variable should be the numerical value of @code{__system_flags} that
  141. you'd like to set; it will override the value of @code{__system_flags}
  142. specified when the program was compiled.
  143.  
  144. @subheading Return Value
  145.  
  146. The exit status of the child process in the lower 8 bits of the return
  147. value; bits 8-17 of the return value will hold @code{SIGINT} or
  148. @code{SIGABRT} if the child process was aborted by @kbd{Ctrl-C} or
  149. Critical Device Error, respectively; otherwise it will be zero.  If the
  150. child couldn't be run, @code{system} will return -1 and set @code{errno}
  151. to an appropriate value.  Note that if @file{COMMAND.COM} was used to
  152. run the child, it will always return a 0 status, even if the command
  153. didn't run successfully.  However, @code{system} only calls
  154. @file{COMMAND.COM} when it needs to run commands internal to it.
  155.  
  156. @subheading Example
  157.  
  158. @example
  159. system("cc1plus.exe @@cc123456.gp");
  160. @end example
  161.  
  162.  
  163.