home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c495 / watcm951.arj / STARTUP.WPK / WILDARGV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-16  |  4.0 KB  |  146 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.   15-sep-91    F.W.Crigger    Use _LpCmdLine, _LpPgmName, _argc, _argv,
  20.                   ___Argc, ___Argv
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <io.h>
  26. #include <direct.h>
  27. #include <malloc.h>
  28.  
  29. extern    void    _Not_Enough_Memory();
  30. static    void    *_allocate(unsigned);
  31. extern    char    *_LpCmdLine;
  32. extern    char    *_LpPgmName;
  33. extern    int    _argc;            /* argument count  */
  34. extern    char  **_argv;            /* argument vector */
  35. extern    int    ___Argc;        /* argument count */
  36. extern    char  **___Argv;        /* argument vector */
  37.  
  38. void __Init_Argv()
  39.     {
  40.     _argv = (char **) _allocate( 2 * sizeof( char * ) );
  41.     _argv[0] = _LpPgmName;    /* fill in program name */
  42.     _argc = _make_argv( _LpCmdLine, &_argv );
  43.     _argv[_argc] = NULL;
  44.     ___Argc = _argc;
  45.     ___Argv = _argv;
  46.     }
  47.  
  48.  
  49. static int _make_argv( char *p, char ***argv )
  50.     {
  51.     int        argc;
  52.     char        *start;
  53.     char        *new;
  54.     char        wildcard;
  55.     char        lastchar;
  56.     DIR *        dir;
  57.     struct dirent * dirent;
  58.     char        drive[_MAX_DRIVE];
  59.     char        directory[_MAX_DIR];
  60.     char        name[_MAX_FNAME];
  61.     char        extin[_MAX_EXT];
  62.     char        pathin[_MAX_PATH];
  63.  
  64.     argc = 1;
  65.     for(;;) {
  66.         while( *p == ' ' ) ++p;    /* skip over blanks */
  67.         if( *p == '\0' ) break;
  68.         /* we are at the start of a parm */
  69.         wildcard = 0;
  70.         if( *p == '\"' ) {
  71.         p++;
  72.         new = start = p;
  73.         for(;;) {
  74.             /* end of parm: NULLCHAR or quote */
  75.             if( *p == '\"' ) break;
  76.             if( *p == '\0' ) break;
  77.             if( *p == '\\' ) {
  78.             if( p[1] == '\"'  ||  p[1] == '\\' )  ++p;
  79.             }
  80.             *new++ = *p++;
  81.         }
  82.         } else {
  83.         new = start = p;
  84.         for(;;) {
  85.             /* end of parm: NULLCHAR or blank */
  86.             if( *p == '\0' ) break;
  87.             if( *p == ' ' ) break;
  88.             if(( *p == '\\' )&&( p[1] == '\"' )) {
  89.             ++p;
  90.             } else if( *p == '?'  ||  *p == '*' ) {
  91.             wildcard = 1;
  92.             }
  93.             *new++ = *p++;
  94.         }
  95.         }
  96.         *argv = realloc( *argv, (argc+2) * sizeof( char * ) );
  97.         if( *argv == NULL )  _Not_Enough_Memory();
  98.         (*argv)[ argc ] = start;
  99.         ++argc;
  100.         lastchar = *p;
  101.         *new = '\0';
  102.         ++p;
  103.         if( wildcard ) {
  104.         /* expand file names */
  105.         dir = opendir( start );
  106.         if( dir != NULL ) {
  107.             --argc;
  108.             _splitpath( start, drive, directory, name, extin );
  109.             for(;;) {
  110.             dirent = readdir( dir );
  111.             if( dirent == NULL ) break;
  112.             if( dirent->d_attr &
  113.               (_A_HIDDEN+_A_SYSTEM+_A_VOLID+_A_SUBDIR) ) continue;
  114.             _splitpath( dirent->d_name, NULL, NULL, name, extin );
  115.             _makepath( pathin, drive, directory, name, extin );
  116.             *argv = realloc( *argv, (argc+2) * sizeof( char * ) );
  117.             if( *argv == NULL )  _Not_Enough_Memory();
  118.             new = _allocate( strlen( pathin ) + 1 );
  119.             strcpy( new, pathin );
  120.             (*argv)[argc++] = new;
  121.             }
  122.             closedir( dir );
  123.         }
  124.         }
  125.         if( lastchar == '\0' ) break;
  126.     }
  127.     return( argc );
  128.     }
  129.  
  130.  
  131. static void *_allocate( unsigned amount )
  132.     {
  133.     void *p;
  134.  
  135. #if defined(__386__)
  136.     p = malloc( amount );
  137. #else
  138.     p = _nmalloc( amount );
  139.     #if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__)
  140.     if( (void near *) p == NULL )  p = malloc( amount );
  141.     #endif
  142. #endif
  143.     if( p == NULL )  _Not_Enough_Memory();
  144.     return( p );
  145.     }
  146.