home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 287.lha / Incr_v1.01 / incr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-13  |  4.4 KB  |  149 lines

  1. /**************************************************************************/
  2. /*                                                                        */
  3. /*      program: incr                                                     */
  4. /*   programmer: George Kerber                                            */
  5. /*      written:  07/24/89                                                */
  6. /*                                                                        */
  7. /**************************************************************************/
  8.  
  9. #include <stdio.h>
  10.  
  11. #define WRITTEN "07/24/89 - 07/29/89"
  12. #define VERSION "v1.01"
  13. FILE *tfile;
  14. char node[31];
  15. void mistake();
  16. void makeline();
  17. void helpscreen();
  18.   
  19. main(argc,argv)
  20. int argc;
  21. char *argv[];
  22. {
  23. int optionsallowed = 3 , filenumber = 1;
  24. char values[80];
  25. char filename[80];
  26. long value1,value2;
  27.  
  28. stcgfn(node,argv[0]);
  29.  
  30. if(argc == 1) { printf("\x0c\n"); helpscreen(0); }
  31.  
  32. if(argc > 1 && argv[1][0] == '-') 
  33.    if(argv[1][1] != 'a' && argv[1][1] != 'b' && argv[1][1] != 'd') mistake("Invalid Option"); 
  34.  
  35. if(argc > 1) {
  36.    if(argv[1][0] == '-') optionsallowed = 4;
  37.    if(argc > optionsallowed) mistake("Invalid option count");
  38.    if(!strcmp(argv[1],"-a")  || !strcmp(argv[1],"-b") || !strcmp(argv[1],"-d")) {
  39.       if(argc == 2) mistake("Missing file name"); 
  40.       filenumber = 2;
  41.       }
  42.    }
  43.  
  44. if(stricmp(argv[filenumber],"NOLINE") == 0) 
  45.    mistake("filename can't be NOLINE");
  46.  
  47. if(stricmp(argv[argc - 1],"NOLINE") && (argc >= filenumber + 2))
  48.    mistake("Invalid final arguement, can only be NOLINE");
  49.    
  50. if(argc > 4) mistake("Invalid Option Count");
  51.  
  52. if(argc == 2) {
  53.    strcpy(filename,argv[1]);
  54.    }
  55.    else {
  56.    strcpy(filename,argv[2]);
  57.    }
  58. if(access(filename,0) == 0) {
  59.    tfile = fopen(filename,"r+");
  60.    if(tfile == (FILE *)NULL) mistake("Can't open file for reading");
  61.    fgets(values,80,tfile);
  62.    fclose(tfile);
  63. /*   for(i = 0 ; values[i] != '\0' ; i++) {    
  64.       if(isdigit(values[i]) == 0) mistake("Incorrect file format");
  65.       }  */
  66.    value1 = atol(values);
  67.    value2 = value1 + 1;
  68.    if(strcmp(argv[1],"-d")) {
  69.       remove(filename);
  70.       tfile = fopen(filename,"a"); 
  71.       if(tfile == (FILE *)NULL) mistake("Can't write incremented value");
  72.       fprintf(tfile,"%ld",value2);
  73.       fprintf(tfile,"\n");          /*  this makes a smaller file than putc???  */
  74.       fflush(tfile);
  75.       fclose(tfile);
  76.       }
  77.    }
  78.    else {
  79.    tfile = fopen(filename,"w");
  80.    if(tfile == (FILE *)NULL) mistake("Can't create new file");
  81.    fprintf(tfile,"0");
  82.    fflush(tfile);
  83.    fclose(tfile);
  84.    value1 = 0;
  85.    value2 = 0;
  86.    }
  87.  
  88. if(argc > 2) {
  89.    switch(argv[1][1]) {
  90.       case 'a':  printf("%ld",value2) ; break ;
  91.       case 'b':  printf("%ld",value1) ; break ;
  92.       case 'd':  values[strlen(values) - 1] = '\0';
  93.                  printf("%-6s",values); break ;
  94.        default:  break ;
  95.    }
  96. }
  97.  
  98. if(argv[1][0] = '-') {
  99.    if(stricmp(argv[argc - 1],"NOLINE") && argc > 2) printf("\n");
  100.    }
  101. exit(0);
  102.  
  103. }
  104.  
  105. /*----------------------------------------------------------------------------*/
  106.  
  107. /*  HELPSCREEN FUNCTION  */
  108.  
  109. void helpscreen(exitcode)
  110. int exitcode;
  111. {
  112. printf("\n\n  %s            George Kerber  %10s  %25s\n\n",node,VERSION,WRITTEN);
  113. printf("  SYNTAX:  %s [[-a|-b|-d] filename [NOLINE]]\n\n",node);
  114. printf("  %s will increment the value in filename by 1 and write the new value\n",node);
  115. printf("  back the file.\n\n");
  116. printf("  Using the -a or -b option will display the value either\n");
  117. printf("  before or after the value is incremented.\n");  
  118. printf("  Use the -d option to simply display the value as it is stored.\n");
  119. printf("  A newline will not be output if the NOLINE option is used.\n");
  120. printf("  If %s is executed with a non-existent file, %s will create the\n",node,node);
  121. printf("  file with an initial value of 0.\n\n");
  122. exit(exitcode);
  123. }
  124.  
  125. /*----------------------------------------------------------------------------*/
  126.  
  127. /*  MISTAKE FUNCTION  */
  128.  
  129. void mistake(description)
  130. char description[80];
  131. {
  132. printf("\x0c");
  133. makeline();
  134. fprintf(stderr,"\n\n\07       ERROR:  %s.\07\n\n",description);
  135. makeline();
  136. helpscreen(5);
  137. }
  138.  
  139. /*----------------------------------------------------------------------------*/
  140.  
  141. /* MAKELINE FUNCTION  */
  142.  
  143. void makeline()
  144. {
  145. fprintf(stderr,"  ___________________________________________________________________\n");
  146. }
  147.  
  148. /*----------------------------------------------------------------------------*/
  149.