home *** CD-ROM | disk | FTP | other *** search
- /* read.c
-
- This program impliments a read command for batch files.
- What read actually does is re-open the file who's name was
- stored in the environment string, and read the next line from
- it. The postion in the file is updated and stored back in
- the environment string, so subiquent calls to read will return
- successive lines from the text file.
- The file gets opened over and over again for each line,
- but you gain all the advantages of batch files with little
- programming.
-
- To use open in a batch file type:
-
- READ <handle> <stringname>
-
- handle is the unique identifer you bave to OPEN.
- stringname is the name of a string to store the text in the
- environment.
-
- READ returns errorlevel=1 for failure, 0 for success
- */
-
- #include <stdio.h>
-
- extern char *env; /*pointer to start of environment string*/
- extern char *tem; /*temporary pointer for searching*/
- extern int enl; /*length of environment in bytes*/
-
- extern char *search_for(); /*searches environment for string*/
- extern del_string(); /*deletes a string from the environment*/
- extern int ins_string(); /*insert a new string in the environment*/
-
- char name[128]="n_"; /*space for the file name string*/
- char pos[64]="p_"; /*space for the file position string*/
- char line[200]; /*space for the line read from file*/
-
- main(argc,argv) /*main part of read program*/
- int argc;
- char *argv[];
- {
- int arg=1; /*where to look for arguments*/
- char *ptr; /*scratch pointer for scanning around*/
- FILE *fh; /*file handle to read with*/
- long posit; /*position in file*/
- int l; /*scratch for string lengths*/
-
- if (argc<2) /*do I have enough arguments?*/
- {
- printf("Usage: READ <handle> <pathname>\r\n");
- exit(1); /*exit if no file name*/
- }
- if (argc<3) /*if there is only one argument,*/
- arg=0; /*use it as the file name*/
- enl=getenv(&env); /*get the environment string*/
- strcat(name,argv[arg]); /*build the file name string*/
- strcat(name,"=");
- ptr=search_for(name); /*is there a file for this handle?*/
- if (ptr == NULL)
- {
- printf("You did not OPEN a file first\r\n");
- exit(1);
- }
- fh=fopen((ptr+=strlen(name)),"r"); /*open the file*/
- if (fh == NULL) /*abort if you cannot open it*/
- {
- printf("Cannot open file %s\r\n",ptr);
- exit(1);
- }
- strcat(pos,argv[arg]); /*build the file position string*/
- strcat(pos,"=");
- ptr=search_for(pos); /*find address of position string*/
- if (ptr == NULL)
- {
- printf("There is something seriously wrong in environment!\r\n");
- exit(1);
- }
- sscanf(ptr+strlen(pos),"%ld",&posit); /*get file position*/
- fseek(fh,posit,0); /*go there*/
- strcpy(line,argv[arg+1]); /*build the text string name*/
- strcat(line,"=");
- del_string(line); /*deletae any old string same name*/
- if (fgets(line+strlen(line),180,fh)== NULL) /*read a line of text*/
- exit(1); /*must have hit eof*/
- del_string(pos); /*delete the string position*/
- posit=ftell(fh); /*find out where we are in the file*/
- sprintf(pos+strlen(pos),"%ld",posit); /*now and convert to string*/
- ins_string(pos); /*store back in environment*/
- line[strlen(line)-1]=0; /*chop the newline off the end*/
- ins_string(line); /*add it to the environment*/
- fclose(fh);
- exit(0);
- }