home *** CD-ROM | disk | FTP | other *** search
- /* pfile.c
-
- This program parses the file name in it's first argument,
- producing a separate device, path, name, and extension.
- These pieces are then stored in the systems environment
- string under the names: _DEV, _PATH, _NAME, and _EXT.
- This allows powerful batch command files to be written
- that extract the various parts of a parameter file name.
- */
-
- #include <stdio.h>
- char *env; /*pointer to start of environment string*/
- char *tem; /*temporary pointer for searching*/
- int enl; /*length of environment in bytes*/
-
- char *search_for(str) /*searches environment for string*/
- char *str; /*string to search for*/
- {
- int l; /*scratch string length*/
-
- l=strlen(str); /*calculate length of argument string*/
- tem=env;
- while(*tem!=0) /*loop for all strings in environment*/
- {
- if (strncmp(tem,str,l)==0) /*if you find it,*/
- return(tem); /*return a pointer to it*/
- while(*tem++ != 0); /*else, skip to next string*/
- }
- return(NULL); /*if no such string, return NULL*/
- }
-
- del_string(str) /*deletes a string from the environment*/
- char *str; /*name (including =) of string to delete*/
- {
- if (search_for(str) != NULL) /*if it exists,*/
- /*then whack it out!*/
- movmem(tem+strlen(tem)+1,tem,(enl-(int)tem)-strlen(tem)-1);
- return;
- }
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- static char dev[]="_DEV="; /*strings for storing the*/
- static char path[]="_PATH="; /*decomposed file name in*/
- static char name[]="_NAME=";
- static char ext[]="_EXT=";
- char *first; /*pointer to first character in directory path*/
- char *file; /*pointer to first character in file name*/
- char *cur; /*pointer to current character in file string parse*/
- int len; /*temp storage for string lengths*/
-
- if (argc<2)
- {
- fputs("Usage: PFILE filename\r\n",stdout);
- exit(2);
- }
- enl=getenv(&env); /*get length and address of system environment*/
- del_string(dev); /*delete any old file name parse strings*/
- del_string(path); /*before you proceed*/
- del_string(name);
- del_string(ext);
- while(*env != 0) /*loop until you find the double nul at end*/
- while (*env++ != 0); /*skip past end of next string*/
- if ((enl-(int)env)-26-strlen(argv[1])>0) /*is there room for this?*/
- {
- file=argv[1]; /*initialize all the parsing pointers*/
- first=file;
- cur=file;
- while(1) /*loop until something interesting happens*/
- {
- switch(*cur) /*look for all the special cases*/
- {
- case ':': /*you found a drive terminator*/
- strcpy(env,dev); /*copy the _DEV= name into*/
- env+=strlen(dev); /*the environment string*/
- len= (cur-(int)first)+1; /*figure out how long the drive*/
- strncpy(env,first,len); /*name is and copy it*/
- env+=len; /*incriment past drive string*/
- *env++ = 0; /*terminate with a nul*/
- *env = 0; /*just to be safe, use 2 nuls*/
- first=cur+1; /*start path\file processing*/
- file=first; /*after the colon*/
- break;
-
- case '\\': /*you found a directory separator*/
- file=cur+1; /*remember the following character,*/
- break; /*it could be the start of a file*/
-
- case '.': /*you found an extention separator*/
- strcpy(env,ext); /*copy the _EXT= name into*/
- env+=strlen(ext); /*the environment string*/
- strcpy(env,cur); /*the rest of the file string*/
- env+=strlen(cur); /*must be the extention*/
- *env++ =0; /*terminate with a nul*/
- *env = 0; /*twice for safetey*/
- /*fall into the nul processing case*/
- case 0: /*you found the nul at string end*/
- len=(file-(int)first); /*calculate the length of the*/
- if (len>0) /*path, if there is one*/
- {
- strcpy(env,path); /*copy the _PATH= name in first*/
- env+=strlen(path);
- strncpy(env,first,len); /*then the path itself*/
- env+=len; /*skip past the new string*/
- *env++ =0; /*and terminate it with nul*/
- }
- len=(cur-(int)file); /*calculate length of file name*/
- if (len>0) /*if there is one,*/
- {
- strcpy(env,name); /*then copy the _NAME= string first*/
- env+=strlen(name);
- strncpy(env,file,len); /*followed by the name*/
- env+=len;
- *env++ = 0;
- }
- *env++ = 0; /*terminate envoronment with 2 nuls*/
- exit(1); /*and return success*/
- }
- cur += 1; /*incriment to next character*/
- }
- }
- exit(0);
- }