home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / batch.lzh / OPEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-10-19  |  2.1 KB  |  60 lines

  1. /*    open.c
  2.  
  3.     This program impliments an open command for batch files.
  4.     What open actually does is simply remember a file name and
  5.     initialize a file position.  Both of these are saved as strings
  6.     in the environment string.  It is up to READ and WRITE to
  7.     re-open the file each time, position it in the right place,
  8.     read a line, and update the position in the environment.
  9.     
  10.     To use open in a batch file type:
  11.     
  12.     OPEN <handle> <pathname>
  13.     
  14.     handle is any unique identifer to keep track of this file.
  15.     pathname is the name of the file to open.
  16.     
  17.     OPEN returns errorlevel=0 for failure, 1 for success
  18.     
  19.     See READ and WRITE for how to manipulate these files.
  20. */
  21.  
  22. #include <stdio.h>
  23.  
  24. extern char    *env;        /*pointer to start of environment string*/
  25. extern char    *tem;        /*temporary pointer for searching*/
  26. extern int    enl;        /*length of environment in bytes*/
  27.  
  28. extern char *search_for();    /*searches environment for string*/
  29. extern del_string();        /*deletes a string from the environment*/
  30. extern int ins_string();    /*insert a new string in the environment*/
  31.  
  32. char    name[128]="n_";    /*space for the file name string*/
  33. char    pos[64]="p_";    /*space for the file position string*/
  34.  
  35. main(argc,argv)        /*main part of open program*/
  36. int    argc;
  37. char    *argv[];
  38. {
  39.     int    arg=1;        /*where to look for arguments*/
  40.  
  41.     if (argc<2)        /*do I have enough arguments?*/
  42.     {
  43.     printf("Usage: OPEN <handle> <pathname>\r\n");
  44.     exit(1);    /*exit if no file name*/
  45.     }
  46.     if (argc<3)        /*if there is only one argument,*/
  47.     arg=0;        /*use it as the file name*/
  48.     enl=getenv(&env);    /*get the environment string*/
  49.     strcat(name,argv[arg]);    /*build the file name string*/
  50.     strcat(name,"=");
  51.     del_string(name);        /*delete any old string with same handle*/
  52.     strcat(name,argv[arg+1]);
  53.     ins_string(name);        /*insert this name into environment*/
  54.     strcat(pos,argv[arg]);    /*build the file position string*/
  55.     strcat(pos,"=");
  56.     del_string(pos);        /*delete any old string with same handle*/
  57.     strcat(pos,"0");        /*initial position is zero*/
  58.     ins_string(pos);        /*insert this into the environment*/
  59.     exit(0);
  60. }