home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c496 / 1.img / STARTUP.WPK / WILDARGV.C < prev   
Encoding:
C/C++ Source or Header  |  1991-08-20  |  3.8 KB  |  139 lines

  1. /*
  2.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3.  *%      Copyright (C) 1989, by WATCOM Systems Inc. All rights     %
  4.  *%      reserved. No part of this software may be reproduced        %
  5.  *%      in any form or by any means - graphic, electronic or        %
  6.  *%      mechanical, including photocopying, recording, taping     %
  7.  *%      or information storage and retrieval systems - except     %
  8.  *%      with the written permission of WATCOM Systems Inc.        %
  9.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10.   WILDARGV - split DOS command line into individual arguments expanding
  11.          those that contain ? or *.
  12.   This module is a substitute for the "initargv" module contained in the
  13.   library.
  14.  
  15.   Modified:    By:        Reason:
  16.   ---------    ---        -------
  17.   23-aug-89    John Dahms    was ignoring files with Archive or
  18.                 read only attributes turned on. (Bug fix)
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <io.h>
  24. #include <direct.h>
  25. #include <malloc.h>
  26.  
  27. extern    int    main();
  28. extern    void    _Not_Enough_Memory();
  29. static    void    *_allocate(unsigned);
  30.  
  31. int __Init_Argv( parm, pgmname )
  32.     char  *parm;        /* DOS command line string */
  33.     char  *pgmname;     /* program name to be placed in argv[0] */
  34.     {
  35.     int    argc;        /* argument count */
  36.     char    **argv;     /* argument vector */
  37.  
  38.     argv = (char **) _allocate( 2 * sizeof( char * ) );
  39.     argv[0] = pgmname;    /* fill in program name */
  40.     argc = _make_argv( parm, &argv );
  41.     argv[argc] = NULL;
  42.     return( main( argc, argv ) );
  43.     }
  44.  
  45.  
  46. static int _make_argv( char *p, char ***argv )
  47.     {
  48.     int        argc;
  49.     char        *start;
  50.     char        *new;
  51.     char        wildcard;
  52.     char        lastchar;
  53.     DIR *        dir;
  54.     struct dirent * dirent;
  55.     char        drive[_MAX_DRIVE];
  56.     char        directory[_MAX_DIR];
  57.     char        name[_MAX_FNAME];
  58.     char        extin[_MAX_EXT];
  59.     char        pathin[_MAX_PATH];
  60.  
  61.     argc = 1;
  62.     for(;;) {
  63.         while( *p == ' ' ) ++p;    /* skip over blanks */
  64.         if( *p == '\0' ) break;
  65.         /* we are at the start of a parm */
  66.         wildcard = 0;
  67.         if( *p == '\"' ) {
  68.         p++;
  69.         new = start = p;
  70.         for(;;) {
  71.             /* end of parm: NULLCHAR or quote */
  72.             if( *p == '\"' ) break;
  73.             if( *p == '\0' ) break;
  74.             if( *p == '\\' ) {
  75.             if( p[1] == '\"'  ||  p[1] == '\\' )  ++p;
  76.             }
  77.             *new++ = *p++;
  78.         }
  79.         } else {
  80.         new = start = p;
  81.         for(;;) {
  82.             /* end of parm: NULLCHAR or blank */
  83.             if( *p == '\0' ) break;
  84.             if( *p == ' ' ) break;
  85.             if(( *p == '\\' )&&( p[1] == '\"' )) {
  86.             ++p;
  87.             } else if( *p == '?'  ||  *p == '*' ) {
  88.             wildcard = 1;
  89.             }
  90.             *new++ = *p++;
  91.         }
  92.         }
  93.         *argv = realloc( *argv, (argc+2) * sizeof( char * ) );
  94.         if( *argv == NULL )  _Not_Enough_Memory();
  95.         (*argv)[ argc ] = start;
  96.         ++argc;
  97.         lastchar = *p;
  98.         *new = '\0';
  99.         ++p;
  100.         if( wildcard ) {
  101.         /* expand file names */
  102.         dir = opendir( start );
  103.         if( dir != NULL ) {
  104.             --argc;
  105.             _splitpath( start, drive, directory, name, extin );
  106.             for(;;) {
  107.             dirent = readdir( dir );
  108.             if( dirent == NULL ) break;
  109.             if( dirent->d_attr &
  110.               (_A_HIDDEN+_A_SYSTEM+_A_VOLID+_A_SUBDIR) ) continue;
  111.             _splitpath( dirent->d_name, NULL, NULL, name, extin );
  112.             _makepath( pathin, drive, directory, name, extin );
  113.             *argv = realloc( *argv, (argc+2) * sizeof( char * ) );
  114.             if( *argv == NULL )  _Not_Enough_Memory();
  115.             new = _allocate( strlen( pathin ) + 1 );
  116.             strcpy( new, pathin );
  117.             (*argv)[argc++] = new;
  118.             }
  119.             closedir( dir );
  120.         }
  121.         }
  122.         if( lastchar == '\0' ) break;
  123.     }
  124.     return( argc );
  125.     }
  126.  
  127.  
  128. static void *_allocate( unsigned amount )
  129.     {
  130.     void *p;
  131.  
  132.     p = _nmalloc( amount );
  133. #if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__)
  134.     if( p == NULL )  p = malloc( amount );
  135. #endif
  136.     if( p == NULL )  _Not_Enough_Memory();
  137.     return( p );
  138.     }
  139.