home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / MAWK113.ZIP / mawk113 / msdos / argvmks.c next >
C/C++ Source or Header  |  1992-12-16  |  3KB  |  107 lines

  1.  
  2. /*  argvmks.c
  3.  
  4.     for MKS Korn Shell
  5.  
  6.     If you use this file, add -DHAVE_REARGV=1 to your
  7.     CFLAGS
  8.  
  9.     Contributed by Jack Fitts (fittsj%wmv009@bcsaic.boeing.com)
  10.  
  11. */
  12.  
  13. /*
  14. $Log: argvmks.c,v $
  15.  * Revision 1.2  1992/12/17  02:48:01  mike
  16.  * 1.1.2d changes for DOS
  17.  *
  18.  * Revision 1.1  1992/12/05  22:38:41  mike
  19.  * Initial revision
  20.  *
  21. */
  22.  
  23.  
  24. /***********************************************************/
  25. /*                                                         */
  26. /* prototypes for reargv                                   */
  27. /*                                                         */
  28. /***********************************************************/
  29.  
  30. void *malloc(unsigned) ;
  31. char * basename ( char * );
  32. char *strcpy(char* , char*) ;
  33.  
  34.  
  35. /***********************************************************/
  36. /*                                                         */
  37. /* reargv reset argc/argv from environment for MKS shell   */
  38. /*                                                         */
  39. /***********************************************************/
  40.  
  41.  
  42. void reargv ( int *argcp, char *** argvp ) {
  43.  
  44.     int i = 0;
  45.     int cnt ;
  46.     char ** v;
  47.     extern char **environ ;
  48.     register char **pe = environ;
  49.  
  50. /* MKS Command line args are in the first n lines of the environment */
  51. /* each arg is preceded with a tilde (~)*/
  52.  
  53.     while ( **(pe++) == '~' )
  54.         i++;
  55.  
  56. /* if no tilde found then not running under MKS */
  57.  
  58.     if ( ! i )  return ;
  59.  
  60. /* malloc space for array of char pointers */
  61.  
  62.     if ( ! ( v = ( char ** ) malloc (( i + 1 ) * sizeof ( char* ))) )
  63.         return 1;
  64.  
  65. /* set argc to number of args in environ */
  66.  
  67.     *argcp = cnt = i;
  68.  
  69. /* set char pointers to each command line arg */
  70. /* jump over the tilde which is the first char in each string */
  71.  
  72.     for ( i = 0; i < cnt ; i++ )
  73.         v[i] = environ[i]+1;
  74.  
  75.     /*set last arg to null*/
  76.  
  77.     v[cnt] = (char *) 0 ;
  78.     
  79.     /*strip leading directory stuff from argv[0] */
  80.  
  81.     v[0] = basename(v[0]);
  82.  
  83.     *argvp = v;
  84. }
  85.  
  86.  
  87. /***********************************************************/
  88. /*                                                         */
  89. /* basename                                                */
  90. /*                                                         */
  91. /***********************************************************/
  92.  
  93. static char * basename ( char * s ) {
  94.  
  95.     register char * p ;
  96.     char *last ;
  97.     
  98.     /* find the last occurrence of ':' '\\' or '/' */
  99.     p = s ;  last = (char *) 0 ;
  100.     while ( *p ) {
  101.     if ( *p == ':' || *p == '\\' || *p == '/' ) last = p ;
  102.     p++ ;
  103.     }
  104.  
  105.     return last ? last+1 : s ;
  106. }
  107.