home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / c / help.arc / REARGV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-11  |  2.1 KB  |  112 lines

  1. #define REARGV_C
  2. #define LINT_ARGS
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. /*    Re-assemble the argv/argc string using the CMDLINE environment.
  7.  *    Note that the environment string itself is destroyed by this
  8.  *    process.
  9.  */
  10.  
  11. #define    MAXARGC        ((unsigned)128)
  12. #define isquote(c)    ( (c)=='\"' || (c)=='\'')
  13.  
  14. extern    char    *getmenv( char*       );
  15.  
  16. static  int    Envlen;
  17.  
  18. /*----------------------------------------------------------------------*/
  19.  
  20. static char    *nextarg( pp )
  21. char        **pp;
  22. {
  23.     register char    *p;
  24.     char        *start;
  25.     register int    term;
  26.  
  27.     if( !*(p = *pp)  )        /* No more args to get , return 0 */
  28.         return (char *) 0;
  29.  
  30.     while( isspace(*p) )        /* Skip white space          */
  31.         p++;
  32.  
  33.     term = isquote(*p) ? *p++ : ' ' ;
  34.  
  35.     for( start = p; *p ; p++)
  36.     {
  37.         if( *p == term &&  *(p-1) != '\\' )
  38.         {
  39.             *p++ = '\0';
  40.             break;
  41.         }
  42.     }
  43.  
  44.     *pp = p;
  45.  
  46.     return start;
  47. }
  48.  
  49. /*----------------------------------------------------------------------*/
  50.  
  51. int    reargv( argcp, argvp )
  52. char    ***argvp;
  53. int    *argcp;
  54. {
  55.     char        **argv ;
  56.     register int    argc = 0 ;
  57.     register int    maxc = MAXARGC ;
  58.     static     char    *vects[MAXARGC];
  59.     char        *p   ;
  60.  
  61.     if( !(p = getmenv("CMDLINE")) ||  !*p )
  62.         return 0;
  63.  
  64.     Envlen = strlen(p);
  65.  
  66.     argv = vects;
  67.  
  68.     for( maxc=MAXARGC; --maxc >= 0 && (*argv++ = nextarg(&p)); argc++)
  69.         ;
  70.  
  71.     if( maxc < 0 )
  72.         fprintf(stderr, "Library:reargv: Command line truncated\n");
  73.  
  74.     *argcp = argc;
  75.     *argvp = vects;
  76.     return 1;
  77. }
  78.  
  79. /*----------------------------------------------------------------------*/
  80.  
  81. envlen()    /* Returns the original length of the CMDLINE environment */
  82. {
  83.     return Envlen;
  84. }
  85.  
  86. /*----------------------------------------------------------------------*/
  87.  
  88. #ifdef DEBUGMAIN
  89.  
  90. main( argc, argv )
  91. char    **argv;
  92. {
  93.     printf("Original command line is: |");
  94.     while( --argc >= 0 )
  95.         printf("%s|", *argv++ );
  96.  
  97.     if( !reargv( &argc, &argv ) )
  98.         printf("\nCMDLINE not present\n");
  99.     else
  100.     {
  101.         printf("New argc = %d\n", argc );
  102.         printf("\nModified command line is: |");
  103.  
  104.         while( --argc >= 0 )
  105.             printf("%s|", *argv++ );
  106.  
  107.         printf("\n");
  108.     }
  109. }
  110.  
  111. #endif
  112.