home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d311 / incr.lha / Incr / incr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-31  |  6.5 KB  |  200 lines

  1. /*-------------------------------------------------------------------------*/
  2. /*      Program:  incr                                                     */
  3. /*   Programmer:  George Kerber                                            */
  4. /*      Written:  07/24/89                                                 */
  5. /*     Compiler:  Lattice 5.04                                             */
  6. /*-------------------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <dos.h>
  11. #include <stdlib.h>
  12.  
  13. #define WRITTEN "07/24/89 - 01/01/90"
  14. #define VERSION "v1.04b"
  15.  
  16. void mistake();
  17. void helpscreen();
  18. void exitcode();
  19. void main();
  20.   
  21. void main(int argc, char *argv[])
  22. {
  23. FILE *tfile;
  24. char node1[31];
  25. char option, filename[80] = "Invalid Option Combination";
  26. char values[80] = "Multiple Color Options", opts[] = "";
  27. char drive[FNSIZE],path[FMSIZE],node[FNSIZE],ext[FESIZE];
  28. long value1,value2;
  29. int i, next, oflag = 0, nflag = 0, cflag = 0;
  30.  
  31. stcgfn(node1,argv[0]);
  32. if(argc == 1 || (argc == 2 && argv[1][0] == '?')) helpscreen(node1);
  33.  
  34. for( next = 1 ; (argopt(argc,argv,opts,&next,&option)) != NULL ; ) {
  35.    switch(option) {
  36.       case 'a':   if(oflag) mistake(filename,node1);       /*  after       */
  37.                   oflag = 'a' ; break ;              
  38.       case 'b':   if(oflag) mistake(filename,node1);       /*  before      */
  39.                   oflag = 'b' ; break ;             
  40.       case 's':
  41.       case 'l':   if(oflag) mistake(filename,node1);
  42.                   oflag = 'l' ; break ;                  /*  show value  */
  43.       case 'd':   if(oflag) mistake(filename,node1);     /*  delete file */
  44.                   oflag = 'd';  break ;
  45.       case 'n':   nflag = 1; break ;                     /*  noline      */
  46.       case '3':   if(cflag) mistake(values,node1);
  47.                   printf(""); cflag = 1; break;    /*  orange output */
  48.       case '2':   if(cflag) mistake(values,node1);
  49.                   printf(""); cflag = 1; break;    /*  black output  */
  50.       case '1':   if(cflag) mistake(values,node1);
  51.                   printf(""); cflag = 1; break;    /*  white output */
  52.       case '5':   printf("");  break ;              /*  bold output   */
  53.       case '6':   printf("");  break ;              /*  underline     */
  54.       case '7':   printf("");  break ;              /*  italics       */
  55.        default:  mistake("Invalid Option",node1);
  56.       }
  57.    }
  58.  
  59. /*----- Begin error & option checking.-------------------------------------*/
  60. if(next >= argc) mistake("Missing INCR name",node1);
  61. if(argv[next][0] == '-') mistake("Missing INCR Name",node1);
  62. if(argc > (next + 1)) mistake("Invalid Option Count",node1);
  63. /*----- End error checking ------------------------------------------------*/
  64.  
  65. strsfn(argv[next],drive,path,node,ext);
  66.  
  67. if(strcmp(ext,"") && strcmp(ext,"incr")) 
  68.    mistake("Filename extension defaults to \".incr\"",node1);
  69.  
  70. strcpy(ext,"incr");
  71. if(drive[0] == '\0' && path[0] == '\0') { 
  72.    strcpy(drive,"s:");
  73.    strcpy(path,"incr");
  74.    if(access("s:incr",0)) mkdir("s:incr"); }
  75.  
  76. if(drive[0] == '\0' && path[0] != '\0')
  77.    mistake("Full path must be specified",node1);
  78.  
  79. strmfn(filename,drive,path,node,ext);   /*  create destination file name   */
  80.  
  81. if(strcmp(ext,"") && strcmp(ext,"incr")) {
  82.    mistake("Filename extension defaults to \".incr\"",node1);
  83.    }
  84.  
  85. strcpy(ext,"incr");
  86.  
  87. strmfn(filename,drive,path,node,ext);
  88.  
  89. if(oflag == 'd') {
  90.    if(remove(filename)) {
  91.       exitcode(5);
  92.       }
  93.       else {
  94.       exitcode(0);
  95.       }
  96.    }
  97.  
  98. if(!access(filename,0)) {
  99.    tfile = fopen(filename,"r+");
  100.    if(tfile == (FILE *)NULL) mistake("Can't open file for reading",node1);
  101.    fgets(values,80,tfile);
  102.    fclose(tfile);
  103.    for(i = (strlen(values) - 2 ) ; i >= 0 ; i--) {    
  104.       if(!isdigit(values[i])) mistake("Incorrect file format",node1);
  105.       } 
  106.    value1 = atol(values);
  107.    if(oflag != 'l') {
  108.       value2 = value1 + 1;
  109.       if(strcmp(argv[1],"-d")) {
  110.          remove(filename);
  111.          tfile = fopen(filename,"a"); 
  112.          if(tfile == (FILE *)NULL)
  113.             mistake("Can't write incremented value",node1);
  114.          stcl_d(values,value2);
  115.          fprintf(tfile,"%s",values);
  116.          fprintf(tfile,"\n");  
  117.          fflush(tfile);
  118.          fclose(tfile);
  119.          }
  120.       }
  121.    }
  122.    else {
  123.    tfile = fopen(filename,"w");
  124.    if(tfile == (FILE *)NULL) mistake("Can't create new file",node1);
  125.    fprintf(tfile,"1");
  126.    fflush(tfile);
  127.    fclose(tfile);
  128.    value1 = 0;
  129.    value2 = 1;
  130.    }
  131.  
  132.  
  133.  
  134. if(argc > 2) {
  135.    switch(oflag) {
  136.       case 'a':  printf("%ld",value2) ; break ;
  137.       case 'b':  printf("%ld",value1) ; break ;
  138.       case 'l':  printf("%-6ld",value1); break ;
  139.  
  140.        default:  break ;
  141.       }
  142.    }
  143.  
  144. if(!nflag && (oflag == 'a' || oflag == 'b' || oflag == 'l')) printf("\n");
  145. exitcode(0);
  146.  
  147. }
  148.  
  149. /*-------------------------------------------------------------------------*/
  150.  
  151. /*  HELPSCREEN FUNCTION  */
  152.  
  153. void helpscreen(char node1[])
  154. {
  155. printf("\x0c\n\n  %s          George Kerber  %10s  %25s\n\n",
  156.    node1,VERSION, WRITTEN);
  157. printf("  SYNTAX:  %s [[-a|-b|-l -n -d -#] [drive:path/]filename]\n\n",node1);
  158. printf("           -a  Value is displayed after it is incremented.\n");
  159. printf("           -b  Value is displayed before it is incremented.\n");
  160. printf("           -l  Value is only displayed.\n");
  161. printf("           -d  INCR file is unconditionally deleted.\n");
  162. printf("           -n  Newline is not output after value is displayed.\n");
  163. printf("           -#  Color, bold, etc....\n\n");
  164. printf("  %s will increment the value in filename by 1 and write the new value\n",node1);
  165. printf("  back the file.\n\n");
  166. printf("  If no path is specifed, %s will use s:incr for all files\n",node1);
  167. printf("  and create the s:incr directory if necessary.\n");
  168. printf("  If %s is executed with a non-existent file, %s will create the\n",node1,node1);
  169. printf("  file with an initial value of 1.\n\n");
  170. exitcode(0);
  171. }
  172.  
  173. /*-------------------------------------------------------------------------*/
  174.  
  175. /*  MISTAKE FUNCTION  */
  176.  
  177. void mistake(char description[],char node1[])
  178. {
  179. fprintf(stderr,"\n\07   ERROR:  %s --> %s.\07\n\n",
  180.    node1,description);
  181. exitcode(5);
  182. }
  183.  
  184. /*-------------------------------------------------------------------------*/
  185.  
  186. /*  EXITCODE FUNCTION  */
  187.  
  188. void exitcode(int x)
  189. {
  190. printf("");
  191. exit(x);
  192. }
  193.  
  194. /*-------------------------------------------------------------------------*\
  195.  
  196. 11/01/89 v1.04a:  Re-compiled with Lattice 5.04
  197. 01/01/90 v1.04b:  removed all global variables
  198.  
  199. \*-------------------------------------------------------------------------*/
  200.