home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / DOSCMD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.3 KB  |  77 lines

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