home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / vms / 13995 < prev    next >
Encoding:
Text File  |  1992-08-23  |  2.3 KB  |  73 lines

  1. Path: sparky!uunet!cis.ohio-state.edu!ucbvax!lrw.com!leichter
  2. From: leichter@lrw.com (Jerry Leichter)
  3. Newsgroups: comp.os.vms
  4. Subject: RE: LIB$SPAWN
  5. Message-ID: <9208221447.AA26491@uu3.psi.com>
  6. Date: 22 Aug 92 13:58:18 GMT
  7. Sender: daemon@ucbvax.BERKELEY.EDU
  8. Distribution: world
  9. Organization: The Internet
  10. Lines: 61
  11.  
  12.  
  13.     Would a kind soul explain to me why in the following code, a
  14.     subprocess is spawned, a sort is started, but the names of the input
  15.     and output files are not passed. Sort comes back to you and asks for
  16.     them.
  17.  
  18.     ---------------------------------------------------- 
  19.     #include stdio
  20.     #include descrip
  21.     #include ssdef
  22.  
  23.     void spawn( char *DCL_command  );
  24.  
  25.     main(){ 
  26.        char *DCL_command;
  27.  
  28.        DCL_command = "sort data.PCSA data.sort"; 
  29.        printf( "%s", DCL_command );
  30.        spawn( DCL_command  );
  31.     }
  32.  
  33.     void spawn( char *DCL_command  )
  34.     {
  35.        int status;
  36.  
  37.        $DESCRIPTOR( command , DCL_command  );
  38.        if ((( status=LIB$SPAWN( &command )) & 1 ) != 1 )
  39.           LIB$STOP( status );
  40.     }
  41.  
  42. This one is subtle, and actually quite amusing.  The problem is a misuse of
  43. $DESCRIPTOR.  $DESCRIPTOR is intended to create COMPILE-TIME descriptors -
  44. it's a declaration, not an executable statement.  Here's its definition:
  45.  
  46. #define $DESCRIPTOR(name,string)                    \
  47.     struct dsc$descriptor_s name =                    \
  48.     {    sizeof(string)-1,        /* String length */    \
  49.         DSC$K_DTYPE_T,            /* Type text     */    \
  50.         DSC$K_CLASS_S,            /* Class string     */    \
  51.         string                /* -> data     */    \
  52.     }
  53.  
  54. (I've edited the definition for readability and added the comments.)
  55.  
  56. The intent is that the string parameter be a quoted string; then, sizeof
  57. applied to it will produce the string's length, including the trailing NUL
  58. (so we subtract 1 and get the length of the actual string).
  59.  
  60. In your case, string is the (char *) point DCL_command.  sizeof applied to
  61. it produces the size of the POINTER, not of the string it points to, so the
  62. descriptor you produce always has length 3 (and, in this example, value
  63. "sor").  Since "SOR" is a valid abbreviation for SORT on your system, you
  64. do indeed manage to run SORT - but without any arguments.
  65.  
  66. You need to set the string length to strlen(DCL_command).  Unfortunately,
  67. that can't be done by this or any similar macro, since the fields of a
  68. structure initializer must be compile-time constants, and function calls
  69. don't qualify.  You have to write some code to do it....
  70.  
  71.                             -- Jerry
  72.  
  73.