home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************/
- /* */
- /* program: incr */
- /* programmer: George Kerber */
- /* written: 07/24/89 */
- /* */
- /**************************************************************************/
-
- #include <stdio.h>
-
- #define WRITTEN "07/24/89 - 07/29/89"
- #define VERSION "v1.01"
- FILE *tfile;
- char node[31];
- void mistake();
- void makeline();
- void helpscreen();
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- int optionsallowed = 3 , filenumber = 1;
- char values[80];
- char filename[80];
- long value1,value2;
-
- stcgfn(node,argv[0]);
-
- if(argc == 1) { printf("\x0c\n"); helpscreen(0); }
-
- if(argc > 1 && argv[1][0] == '-')
- if(argv[1][1] != 'a' && argv[1][1] != 'b' && argv[1][1] != 'd') mistake("Invalid Option");
-
- if(argc > 1) {
- if(argv[1][0] == '-') optionsallowed = 4;
- if(argc > optionsallowed) mistake("Invalid option count");
- if(!strcmp(argv[1],"-a") || !strcmp(argv[1],"-b") || !strcmp(argv[1],"-d")) {
- if(argc == 2) mistake("Missing file name");
- filenumber = 2;
- }
- }
-
- if(stricmp(argv[filenumber],"NOLINE") == 0)
- mistake("filename can't be NOLINE");
-
- if(stricmp(argv[argc - 1],"NOLINE") && (argc >= filenumber + 2))
- mistake("Invalid final arguement, can only be NOLINE");
-
- if(argc > 4) mistake("Invalid Option Count");
-
- if(argc == 2) {
- strcpy(filename,argv[1]);
- }
- else {
- strcpy(filename,argv[2]);
- }
- if(access(filename,0) == 0) {
- tfile = fopen(filename,"r+");
- if(tfile == (FILE *)NULL) mistake("Can't open file for reading");
- fgets(values,80,tfile);
- fclose(tfile);
- /* for(i = 0 ; values[i] != '\0' ; i++) {
- if(isdigit(values[i]) == 0) mistake("Incorrect file format");
- } */
- value1 = atol(values);
- value2 = value1 + 1;
- if(strcmp(argv[1],"-d")) {
- remove(filename);
- tfile = fopen(filename,"a");
- if(tfile == (FILE *)NULL) mistake("Can't write incremented value");
- fprintf(tfile,"%ld",value2);
- fprintf(tfile,"\n"); /* this makes a smaller file than putc??? */
- fflush(tfile);
- fclose(tfile);
- }
- }
- else {
- tfile = fopen(filename,"w");
- if(tfile == (FILE *)NULL) mistake("Can't create new file");
- fprintf(tfile,"0");
- fflush(tfile);
- fclose(tfile);
- value1 = 0;
- value2 = 0;
- }
-
- if(argc > 2) {
- switch(argv[1][1]) {
- case 'a': printf("%ld",value2) ; break ;
- case 'b': printf("%ld",value1) ; break ;
- case 'd': values[strlen(values) - 1] = '\0';
- printf("%-6s",values); break ;
- default: break ;
- }
- }
-
- if(argv[1][0] = '-') {
- if(stricmp(argv[argc - 1],"NOLINE") && argc > 2) printf("\n");
- }
- exit(0);
-
- }
-
- /*----------------------------------------------------------------------------*/
-
- /* HELPSCREEN FUNCTION */
-
- void helpscreen(exitcode)
- int exitcode;
- {
- printf("\n\n %s George Kerber %10s %25s\n\n",node,VERSION,WRITTEN);
- printf(" SYNTAX: %s [[-a|-b|-d] filename [NOLINE]]\n\n",node);
- printf(" %s will increment the value in filename by 1 and write the new value\n",node);
- printf(" back the file.\n\n");
- printf(" Using the -a or -b option will display the value either\n");
- printf(" before or after the value is incremented.\n");
- printf(" Use the -d option to simply display the value as it is stored.\n");
- printf(" A newline will not be output if the NOLINE option is used.\n");
- printf(" If %s is executed with a non-existent file, %s will create the\n",node,node);
- printf(" file with an initial value of 0.\n\n");
- exit(exitcode);
- }
-
- /*----------------------------------------------------------------------------*/
-
- /* MISTAKE FUNCTION */
-
- void mistake(description)
- char description[80];
- {
- printf("\x0c");
- makeline();
- fprintf(stderr,"\n\n\07 ERROR: %s.\07\n\n",description);
- makeline();
- helpscreen(5);
- }
-
- /*----------------------------------------------------------------------------*/
-
- /* MAKELINE FUNCTION */
-
- void makeline()
- {
- fprintf(stderr," ___________________________________________________________________\n");
- }
-
- /*----------------------------------------------------------------------------*/
-