home *** CD-ROM | disk | FTP | other *** search
- #define REARGV_C
- #define LINT_ARGS
- #include <stdio.h>
- #include <ctype.h>
-
- /* Re-assemble the argv/argc string using the CMDLINE environment.
- * Note that the environment string itself is destroyed by this
- * process.
- */
-
- #define MAXARGC ((unsigned)128)
- #define isquote(c) ( (c)=='\"' || (c)=='\'')
-
- extern char *getmenv( char* );
-
- static int Envlen;
-
- /*----------------------------------------------------------------------*/
-
- static char *nextarg( pp )
- char **pp;
- {
- register char *p;
- char *start;
- register int term;
-
- if( !*(p = *pp) ) /* No more args to get , return 0 */
- return (char *) 0;
-
- while( isspace(*p) ) /* Skip white space */
- p++;
-
- term = isquote(*p) ? *p++ : ' ' ;
-
- for( start = p; *p ; p++)
- {
- if( *p == term && *(p-1) != '\\' )
- {
- *p++ = '\0';
- break;
- }
- }
-
- *pp = p;
-
- return start;
- }
-
- /*----------------------------------------------------------------------*/
-
- int reargv( argcp, argvp )
- char ***argvp;
- int *argcp;
- {
- char **argv ;
- register int argc = 0 ;
- register int maxc = MAXARGC ;
- static char *vects[MAXARGC];
- char *p ;
-
- if( !(p = getmenv("CMDLINE")) || !*p )
- return 0;
-
- Envlen = strlen(p);
-
- argv = vects;
-
- for( maxc=MAXARGC; --maxc >= 0 && (*argv++ = nextarg(&p)); argc++)
- ;
-
- if( maxc < 0 )
- fprintf(stderr, "Library:reargv: Command line truncated\n");
-
- *argcp = argc;
- *argvp = vects;
- return 1;
- }
-
- /*----------------------------------------------------------------------*/
-
- envlen() /* Returns the original length of the CMDLINE environment */
- {
- return Envlen;
- }
-
- /*----------------------------------------------------------------------*/
-
- #ifdef DEBUGMAIN
-
- main( argc, argv )
- char **argv;
- {
- printf("Original command line is: |");
- while( --argc >= 0 )
- printf("%s|", *argv++ );
-
- if( !reargv( &argc, &argv ) )
- printf("\nCMDLINE not present\n");
- else
- {
- printf("New argc = %d\n", argc );
- printf("\nModified command line is: |");
-
- while( --argc >= 0 )
- printf("%s|", *argv++ );
-
- printf("\n");
- }
- }
-
- #endif
-