home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / uucp / gnxseq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-01-10  |  1.5 KB  |  96 lines

  1. #include "uucp.h"
  2. #include <sys/types.h>
  3. #include <time.h>
  4.  
  5.  
  6.  
  7. /*******
  8.  *    gnxseq(rmtname)        get next conversation sequence number
  9.  *    char *rmtname;
  10.  *
  11.  *    return - 0 no entry | >0 sequence number
  12.  */
  13.  
  14. gnxseq(rmtname)
  15. char *rmtname;
  16. {
  17.     int count = 0, ct, ret;
  18.     struct tm *tp;
  19.     extern struct tm *localtime();
  20.     time_t clock;
  21.     FILE *fp0, *fp1;
  22.     char buf[BUFSIZ], name[NAMESIZE];
  23.  
  24.     if (ulockf(SQLOCK, SQTIME) != 0)
  25.         return(0);
  26.     if ((fp0 = fopen(SQFILE, "r")) == NULL)
  27.         return(0);
  28.     if ((fp1 = fopen(SQTMP, "w")) == NULL) {
  29.         fclose(fp0);
  30.         return(0);
  31.     }
  32.     chmod(SQTMP, 0400);
  33.  
  34.     while (fgets(buf, BUFSIZ, fp0) != NULL) {
  35.         ret = sscanf(buf, "%s%d", name, &ct);
  36.         if (ret < 2)
  37.             ct = 0;
  38.         name[7] = '\0';
  39.         if (ct > 9998)
  40.             ct = 0;
  41.         if (strcmp(rmtname, name) != SAME) {
  42.             fputs(buf, fp1);
  43.             continue;
  44.         }
  45.  
  46.         /*  found name  */
  47.         count = ++ct;
  48.         time(&clock);
  49.         tp = localtime(&clock);
  50.         fprintf(fp1, "%s %d %d/%d-%d:%d\n", name, ct,
  51.           tp->tm_mon + 1, tp->tm_mday, tp->tm_hour,
  52.           tp->tm_min);
  53.         while (fgets(buf, BUFSIZ, fp0) != NULL)
  54.             fputs(buf, fp1);
  55.     }
  56.     fclose(fp0);
  57.     fclose(fp1);
  58.     if (count == 0) {
  59.         rmlock(SQLOCK);
  60.         unlink(SQTMP);
  61.     }
  62.     return(count);
  63. }
  64.  
  65.  
  66. /***
  67.  *    cmtseq()    commit sequence update
  68.  *
  69.  *    return  0  ok | other - link failed
  70.  */
  71.  
  72. cmtseq()
  73. {
  74.     int ret;
  75.  
  76.     if ((ret = access(SQTMP, 0400)) != 0) {
  77.         rmlock(SQLOCK);
  78.         return(0);
  79.     }
  80.     unlink(SQFILE);
  81.     ret = link(SQTMP, SQFILE);
  82.     unlink(SQTMP);
  83.     rmlock(SQLOCK);
  84.     return(ret);
  85. }
  86.  
  87. /***
  88.  *    ulkseq()    unlock sequence file
  89.  */
  90.  
  91. ulkseq()
  92. {
  93.     unlink(SQTMP);
  94.     rmlock(SQLOCK);
  95. }
  96.