home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / batch.lzh / PFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-09-15  |  4.4 KB  |  126 lines

  1. /*    pfile.c
  2.  
  3.     This program parses the file name in it's first argument,
  4.     producing a separate device, path, name, and extension.
  5.     These pieces are then stored in the systems environment
  6.     string under the names: _DEV, _PATH, _NAME, and _EXT.
  7.         This allows powerful batch command files to be written
  8.     that extract the various parts of a parameter file name.
  9. */
  10.  
  11. #include <stdio.h>
  12. char    *env;        /*pointer to start of environment string*/
  13. char    *tem;        /*temporary pointer for searching*/
  14. int    enl;        /*length of environment in bytes*/
  15.  
  16. char *search_for(str)    /*searches environment for string*/
  17. char    *str;    /*string to search for*/
  18. {
  19.     int    l;    /*scratch string length*/
  20.  
  21.     l=strlen(str);    /*calculate length of argument string*/
  22.     tem=env;
  23.     while(*tem!=0)    /*loop for all strings in environment*/
  24.     {
  25.     if (strncmp(tem,str,l)==0)    /*if you find it,*/
  26.         return(tem);        /*return a pointer to it*/
  27.     while(*tem++ != 0);        /*else, skip to next string*/
  28.     }
  29.     return(NULL);        /*if no such string, return NULL*/
  30. }
  31.  
  32. del_string(str)        /*deletes a string from the environment*/
  33. char    *str;    /*name (including =) of string to delete*/
  34. {
  35.     if (search_for(str) != NULL)    /*if it exists,*/
  36.                 /*then whack it out!*/
  37.     movmem(tem+strlen(tem)+1,tem,(enl-(int)tem)-strlen(tem)-1);
  38.     return;
  39. }
  40.  
  41. main(argc,argv)
  42. int    argc;
  43. char    *argv[];
  44. {
  45.     static char dev[]="_DEV=";    /*strings for storing the*/
  46.     static char path[]="_PATH=";    /*decomposed file name in*/
  47.     static char name[]="_NAME=";
  48.     static char    ext[]="_EXT=";
  49.     char *first;    /*pointer to first character in directory path*/
  50.     char *file;        /*pointer to first character in file name*/
  51.     char *cur;        /*pointer to current character in file string parse*/
  52.     int len;        /*temp storage for string lengths*/
  53.  
  54.     if (argc<2)
  55.     {
  56.     fputs("Usage: PFILE filename\r\n",stdout);
  57.     exit(2);
  58.     }
  59.     enl=getenv(&env);    /*get length and address of system environment*/
  60.     del_string(dev);    /*delete any old file name parse strings*/
  61.     del_string(path);    /*before you proceed*/
  62.     del_string(name);
  63.     del_string(ext);
  64.     while(*env != 0)    /*loop until you find the double nul at end*/
  65.     while (*env++ != 0);    /*skip past end of next string*/
  66.     if ((enl-(int)env)-26-strlen(argv[1])>0)    /*is there room for this?*/
  67.     {
  68.     file=argv[1];        /*initialize all the parsing pointers*/
  69.     first=file;
  70.     cur=file;
  71.     while(1)    /*loop until something interesting happens*/
  72.     {
  73.         switch(*cur)    /*look for all the special cases*/
  74.         {
  75.         case ':':        /*you found a drive terminator*/
  76.         strcpy(env,dev);    /*copy the _DEV= name into*/
  77.         env+=strlen(dev);    /*the environment string*/
  78.         len= (cur-(int)first)+1; /*figure out how long the drive*/
  79.         strncpy(env,first,len);    /*name is and copy it*/
  80.         env+=len;        /*incriment past drive string*/
  81.         *env++ = 0;        /*terminate with a nul*/
  82.         *env = 0;        /*just to be safe, use 2 nuls*/
  83.         first=cur+1;        /*start path\file processing*/
  84.         file=first;        /*after the colon*/
  85.         break;
  86.  
  87.         case '\\':        /*you found a directory separator*/
  88.         file=cur+1;    /*remember the following character,*/
  89.         break;        /*it could be the start of a file*/
  90.  
  91.         case '.':        /*you found an extention separator*/
  92.         strcpy(env,ext);    /*copy the _EXT= name into*/
  93.         env+=strlen(ext);    /*the environment string*/
  94.         strcpy(env,cur);    /*the rest of the file string*/
  95.         env+=strlen(cur);    /*must be the extention*/
  96.         *env++ =0;        /*terminate with a nul*/
  97.         *env = 0;        /*twice for safetey*/
  98.             /*fall into the nul processing case*/
  99.         case 0:        /*you found the nul at string end*/
  100.         len=(file-(int)first);    /*calculate the length of the*/
  101.         if (len>0)        /*path, if there is one*/
  102.         {
  103.             strcpy(env,path);    /*copy the _PATH= name in first*/
  104.             env+=strlen(path);
  105.             strncpy(env,first,len);    /*then the path itself*/
  106.             env+=len;            /*skip past the new string*/
  107.             *env++ =0;            /*and terminate it with nul*/
  108.         }
  109.         len=(cur-(int)file);    /*calculate length of file name*/
  110.         if (len>0)        /*if there is one,*/
  111.         {
  112.             strcpy(env,name);    /*then copy the _NAME= string first*/
  113.             env+=strlen(name);
  114.             strncpy(env,file,len); /*followed by the name*/
  115.             env+=len;
  116.             *env++ = 0;
  117.         }
  118.         *env++ = 0;        /*terminate envoronment with 2 nuls*/
  119.         exit(1);        /*and return success*/
  120.         }
  121.         cur += 1;        /*incriment to next character*/
  122.     }
  123.     }
  124.     exit(0);
  125. }
  126.