home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / makdep4d.zip / MAKDPDOS.ZIP / EXPARG.C < prev    next >
C/C++ Source or Header  |  1994-09-28  |  4KB  |  143 lines

  1. // --------------------------------------------------------------------------
  2. //
  3. //  File:      EXPARG.C
  4. //  Product:   MAKEDEP program
  5. //  Author:    Sulyok Péter (C) 1994
  6. //  Compiler:  Borland C\C++ 3.1
  7. //             Borland C\C++ 4.02
  8. //             Microsoft C 6.00A
  9. //             Microsoft C\C++ 7.00
  10. //             Watcom C 9.0
  11. //  Notes:     Expand command line arguments.
  12. //
  13. // --------------------------------------------------------------------------
  14.  
  15. // Include files ------------------------------------------------------------
  16. #include <io.h>
  17. #include <fcntl.h>
  18. #include <string.h>
  19. #include <malloc.h>
  20.  
  21. // --------------------------------------------------------------------------
  22. //  Name:      exparg
  23. //  Input:     int  *argc                    pointer to argc
  24. //             char ***argv                  pointer to argv[]
  25. //  Output:    int                           0   successful
  26. //                                           -1  not enough memory
  27. //                                           -2  unable to load response file
  28. //  Notes:     Expand command line arguments with loading response file.
  29. // --------------------------------------------------------------------------
  30. int exparg(int *argc, char **argv[])
  31. {
  32.    char **nargv;
  33.    int  nargc, i;
  34.    char wasexpand = 0;
  35.  
  36.    // Allocate new argv[] array.
  37.    if ( (nargv = (char **)malloc((*argc + 1) * (sizeof(char *)))) == NULL)
  38.       return -1;
  39.  
  40.    // Copy all argument to new array with expansion.
  41.    for(i=0, nargc=0; i<*argc; i++) {
  42.  
  43.       // Copy argument.
  44.       if ( (*argv)[i][0] != '@' ) {
  45.          nargv[nargc++] = (*argv)[i];
  46.          } // if
  47.  
  48.       // Expand response file.
  49.       else {
  50.          int  handle, filesize, argnum, k;
  51.          char *buffer, **tmp;
  52.  
  53.          // Open response file.
  54.          if ( (handle = open(&((*argv)[i][1]), O_BINARY|O_RDONLY)) == -1) {
  55.             free(nargv);
  56.             return -2;
  57.             } // if
  58.          filesize = filelength(handle);
  59.  
  60.          // Allocate buffer for response file.
  61.          if ( (buffer = (char *)malloc(filesize+1)) == NULL ) {
  62.             close(handle);
  63.             free(nargv);
  64.             return -2;
  65.             } // if
  66.  
  67.          // Read entire file to buffer.
  68.          if ( read(handle, buffer, filesize) != filesize ) {
  69.             close(handle);
  70.             free(nargv);
  71.             return -2;
  72.             } // if
  73.          buffer[filesize] = '\0';
  74.  
  75.          // Close response file.
  76.          close(handle);
  77.  
  78.          // Separate and count arguments in response file.
  79.          for(k=0, argnum=0; k<filesize; k++) {
  80.             switch( buffer[k] ) {
  81.                case ' ' :
  82.                case '\n':
  83.                case '\r':
  84.                case '\t':
  85.                   buffer[k] = '\0';
  86.                   break;
  87.  
  88.                default:
  89.                   if ( !k || !buffer[k-1] )
  90.                      argnum++;
  91.                   break;
  92.                } // switch
  93.             } //for
  94.  
  95.          // Add new items to argv[] array.
  96.          if ( (tmp = (char **)realloc(nargv, ((*argc + 1) + argnum) * sizeof(char *))) == NULL ) {
  97.             free(buffer);
  98.             free(nargv);
  99.             return -1;
  100.             } // if
  101.          nargv = tmp;
  102.  
  103.          // Register the new arguments.
  104.          k = 0;
  105.          while(k < filesize) {
  106.  
  107.             // Ignore 0's.
  108.             if ( !buffer[k] ) {
  109.                k++;
  110.                continue;
  111.                } // if
  112.  
  113.             // Register an argument.
  114.             if ( !k || !buffer[k-1] ) {
  115.                nargv[nargc++] = &buffer[k++];
  116.                continue;
  117.                } // if
  118.  
  119.             // Else step to next.
  120.             k++;
  121.             } // while
  122.  
  123.          // Sign the expansion.
  124.          wasexpand = 1;
  125.          } // else
  126.  
  127.       } // for
  128.  
  129.    // Set the new arguments.
  130.    if ( wasexpand ) {
  131.       nargv[nargc] = NULL;
  132.       *argc = nargc;
  133.       *argv = nargv;
  134.       } // if
  135.    else
  136.       free(nargv);
  137.  
  138.    return 0;
  139. }
  140.  
  141. // End ----------------------------------------------------------------------
  142.  
  143.