home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / DOSCMD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.4 KB  |  75 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - doscmd.c
  3.  *
  4.  * function(s)
  5.  *        __DOScmd - Prepare Spawn/Exec command line
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #include <_process.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. /*-----------------------------------------------------------------------*
  22.  
  23. Name            __DOScmd -- Build a DOS command line from argv array
  24.  
  25. Usage           char * pascal __DOScmd(char **argV);
  26.  
  27. Prototype in    _process.h
  28.  
  29. Description     This function allocates  a buffer and fill it  with all the
  30.                 argument  strings one  after the  others. The  command line
  31.                 starts  with  the  command  length  and  terminates  with a
  32.                 carriage return which is not included in the count.
  33.  
  34.  
  35. Return value    __DOScmd  returns  a pointer  to  command  string buffer if
  36.                 successful, and NULL on error.
  37.  
  38. *------------------------------------------------------------------------*/
  39. char    * pascal near __DOScmd(char **argV)
  40. {
  41.         register char   **argW;
  42.         register unsigned       cmdS, Wrk;
  43.         char    *bufP;
  44.  
  45. /*      Compute the command line size including the NULL string at the
  46.         end of the command line.
  47. */
  48.  
  49.         cmdS = 1;               /* Command size byte */
  50.         if ((argW = argV) != NULL && *argW++)
  51.                 while (*argW && **argW) {
  52.                         Wrk = strlen(*argW++) + 1;
  53.                         if ((cmdS + Wrk) > 127)
  54.                                 return NULL;
  55.                         cmdS += Wrk;
  56.                 }
  57.         cmdS++;                 /* Ending Carriage Return */
  58.  
  59. /*      Allocate a buffer, and concatenate all argument strings
  60. */
  61.         if ((bufP = malloc(cmdS)) != NULL) {
  62.                 if ((*bufP++ = cmdS - 2) != 0) {
  63.                         argW = argV + 1;
  64.                         while (*argW && **argW) {
  65.                                 *bufP++ = ' ';
  66.                                 bufP = _stpcpy(bufP, *argW++);
  67.                         }
  68.                 }
  69.                 *bufP++ = '\r';
  70.                 return bufP - cmdS;
  71.         }
  72.         else
  73.                 return NULL;
  74. }
  75.