home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume25 / hostcvt / nextserial.c < prev    next >
C/C++ Source or Header  |  1992-01-03  |  2KB  |  90 lines

  1. #include <stdio.h>
  2.  
  3. #ifndef lint
  4. static char *RCSid = "$Id: nextserial.c,v 1.1 90/10/03 22:56:42 rogers Release $";
  5. #endif
  6.  
  7. #define PAT    "Serial"
  8.  
  9. extern int errno;
  10. extern char *sys_errlist[];
  11. #define ERR    sys_errlist[errno]
  12.  
  13. main(argc, argv)
  14. int argc;
  15. char *argv[];
  16. {
  17.     FILE *fopen();
  18.     char *re_comp(), *fgets(), *index(), *mktemp();
  19.     register char *cp;
  20.     register FILE *infp, *outfp;
  21.     register char *tmpname;
  22.     char buf[BUFSIZ];
  23.     int serial;
  24.  
  25.     (void)umask(022);
  26.  
  27.     if(argc != 2){
  28.     (void)fprintf(stderr, "usage: nextserial soafile\n");
  29.     exit(1);
  30.     }
  31.  
  32.     if((cp = re_comp(PAT)) != NULL){
  33.     (void)fprintf(stderr, "nextserial: %s\n", cp);
  34.     exit(1);
  35.     }
  36.  
  37.     tmpname = mktemp(".nxtXXXXXX");
  38.     (void)unlink(tmpname);
  39.  
  40.     if(rename(argv[1], tmpname) == -1){
  41.     (void)fprintf(stderr, "nextserial: could not rename(%s, %s) - %s (%d)\n", argv[1], tmpname, ERR);
  42.     exit(1);
  43.     }
  44.  
  45.     if((infp = fopen(tmpname, "r")) == NULL){
  46.     (void)fprintf(stderr, "nextserial: could not open %s for read\n", tmpname);
  47.  
  48.     if(rename(tmpname, argv[1]) == -1)
  49.         (void)fprintf(stderr, "EKK! could not put %s back to %s!\n", tmpname, argv[1]);
  50.     exit(1);
  51.     }
  52.  
  53.     if((outfp = fopen(argv[1], "w")) == NULL){
  54.     (void)fprintf(stderr, "nextserial: could not open %s for write\n", argv[1]);
  55.  
  56.     if(rename(tmpname, argv[1]) == -1)
  57.         (void)fprintf(stderr, "EKK! could not put %s back to %s!\n", tmpname, argv[1]);
  58.     exit(1);
  59.     }
  60.  
  61.     while(fgets(buf, BUFSIZ, infp) != NULL){
  62.     switch(re_exec(buf)){
  63.         case 0:
  64.         (void)fputs(buf, outfp);
  65.         break;
  66.         
  67.         case -1:
  68.         default:
  69.         (void)fprintf(stderr, "nextserial: regex internal error\n");
  70.         exit(1);
  71.         
  72.         case 1:
  73.         if((cp = index(buf, '\n')) != NULL)
  74.             *cp = '\0';
  75.  
  76.         if(sscanf(buf, "%d", &serial) != 1){
  77.             (void)fprintf(stderr, "nextserial: could not decode [%s]\n", buf);
  78.             exit(1);
  79.         }
  80.  
  81.         (void)fprintf(outfp, "\t\t\t\t%d\t; %s\n", ++serial, PAT);
  82.         break;
  83.     }
  84.     }
  85.     (void)fclose(outfp);
  86.     (void)fclose(infp);
  87.     (void)unlink(tmpname);
  88.     exit(0);
  89. }
  90.