home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / 2bsd.tar.gz / 2bsd.tar / upgrade / src / msgs.c < prev    next >
C/C++ Source or Header  |  1979-04-19  |  5KB  |  275 lines

  1. /* #define CORY */
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <signal.h>
  5. #include <sys/dir.h>
  6. #include <sys/stat.h>
  7. #include <ctype.h>
  8.  
  9. typedef    char    bool;
  10.  
  11. FILE    *f;
  12. bool    hdrs;
  13. bool    qopt;
  14. char    *sep;
  15. bool    ruptible;
  16. int    onintr();
  17.  
  18. main(argc, argv)
  19.     int argc;
  20.     char *argv[];
  21. {
  22.     char obuf[BUFSIZ];
  23.     bool newrc, already, argfirst = 0;
  24.     int rcfirst, lastmsg, firstmsg;
  25.     FILE *bounds, *msgsrc;
  26.     int i, nextty;
  27.     bool hush = 0;
  28.     bool send = 0;
  29.  
  30.     ruptible = signal(SIGINT, SIG_IGN) == SIG_DFL;
  31.     if (ruptible)
  32.         signal(SIGINT, SIG_DFL);
  33.     setbuf(stdout, obuf);
  34.     argc--, argv++;
  35.     while (argc >= 1) {
  36.         if (isdigit(argv[0][0])) {
  37.             argfirst = 1;
  38.             rcfirst = atoi(argv[0]);
  39.         } else switch (argv[0][1]) {
  40.         
  41.         case 'f':
  42.             hush++;
  43.             break;
  44.  
  45.         case 'q':
  46.             qopt++;
  47.             break;
  48.  
  49.         case 'h':
  50.             hdrs++;
  51.             break;
  52.  
  53.         case 's':
  54.             send++;
  55.             break;
  56.  
  57.         default:
  58.             fprintf(stderr, "usage: msgs [ -f ] [ number ]\n");
  59.             exit(1);
  60.         }
  61.         argc--, argv++;
  62.     }
  63.     if (!send) {
  64.         setuid(getuid());
  65.         if (chdir(getenv("HOME")) < 0)
  66.             perror(getenv("HOME")), exit(1);
  67.         newrc = 0;
  68.         msgsrc = fopen(".msgsrc", "r");
  69.         nextty = 1;
  70.         if (msgsrc != NULL) {
  71.             fscanf(msgsrc, "%d\n", &i);
  72.             fclose(msgsrc);
  73.             nextty = i;
  74.             if (!argfirst)
  75.                 rcfirst = i;
  76.         } else
  77.             newrc = 1;
  78.         msgsrc = fopen(".msgsrc", "w");
  79.         if (msgsrc == NULL)
  80.             perror(".msgsrc"), exit(1);
  81.     }
  82.     if (chdir("/usr/msgs") < 0)
  83.         perror("/usr/msgs"), exit(1);
  84.     bounds = fopen("bounds", "r");
  85.     if (bounds == NULL) {
  86.         FILE *d = fopen(".", "r");
  87.         struct direct dirent;
  88.  
  89.         if (d == NULL)
  90.             perror("/usr/msgs"), exit(1);
  91.         firstmsg = 25000;
  92.         lastmsg = 0;
  93.         while (fread(&dirent, sizeof dirent, 1, d) == 1) {
  94.             register char *cp = dirent.d_name;
  95.             register int i = 0;
  96.  
  97.             if (dirent.d_ino == 0)
  98.                 continue;
  99.             while (isdigit(*cp))
  100.                 i = i * 10 + *cp++ - '0';
  101.             if (*cp)
  102.                 continue;
  103.             if (i > lastmsg)
  104.                 lastmsg = i;
  105.             if (i < firstmsg)
  106.                 firstmsg = i;
  107.         }
  108.         if (firstmsg == 25000) {
  109.             firstmsg = 1;
  110.             lastmsg = 0;
  111.         }
  112.         fclose(d);
  113.         if (send) {
  114.             unlink("bounds");
  115.             bounds = fopen("bounds", "w");
  116. #ifdef CORY
  117.             chmod("bounds", 0644);
  118. #else
  119.             chmod("bounds", 0666);
  120. #endif
  121.             if (bounds == NULL)
  122.                 perror("bounds"), exit(1);
  123.             fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
  124.             fclose(bounds);
  125.         }
  126.     } else {
  127.         fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
  128.         fclose(bounds);
  129.     }
  130.     if (send) {
  131.         char newname[16];
  132.         FILE *newm;
  133.  
  134.         sprintf(newname, "%d", lastmsg+1);
  135.         bounds = fopen("bounds", "w");
  136.         if (bounds == NULL)
  137.             perror("bounds"), exit(1);
  138.         fprintf(bounds, "%d %d\n", firstmsg, lastmsg+1);
  139.         fclose(bounds);
  140.         newm = fopen(newname, "w");
  141.         if (newm == NULL)
  142.             fprintf(stderr, "/usr/msgs/"), perror(newname), exit(1);
  143.         chmod(newname, 0644);
  144.         for (;;) {
  145.             register c;
  146.             c = getchar();
  147.             if (feof(stdin))
  148.                 exit(0);
  149.             if (ferror(stdin))
  150.                 exit(1);
  151.             putc(c, newm);
  152.         }
  153.     }
  154.     if (!newrc)
  155.         firstmsg = rcfirst;
  156.     already = 0;
  157.     for (i = firstmsg; i <= lastmsg; i++) {
  158.         register int c;
  159.         char inline[BUFSIZ];
  160.         struct stat stbuf;
  161.         char fname[16];
  162.  
  163.         sprintf(fname, "%d", i);
  164.         f = fopen(fname, "r");
  165.         if (f == NULL)
  166.             continue;
  167.         if (qopt) {
  168.             fseek(msgsrc, (long) 0, 0);
  169.             fprintf(msgsrc, "%d\n", nextty);
  170.             fflush(msgsrc);
  171.             printf("There are new messages.\n");
  172.             exit(0);
  173.         }
  174.         if (already)
  175.             printf("\n");
  176.         already = 1;
  177.         fseek(msgsrc, (long) 0, 0);
  178.         fprintf(msgsrc, "%d\n", nextty);
  179.         fflush(msgsrc);
  180.         printf("Message %d:\n", i);
  181.         fgets(inline, sizeof inline, f);
  182.         printf("%s", inline);
  183.         if (fgets(inline, sizeof inline, f)) {
  184.             if (strcmp(inline, "To: msgs\n") != 0 && inline[0] != '\n')
  185.                 printf("%s", inline);
  186.             if (fgets(inline, sizeof inline, f) && inline[0] != '\n')
  187.                 printf("%s", inline);
  188.         }
  189.         c = getc(f);
  190.         sep = "-";
  191.         if (c == '\n')
  192.             c = getc(f);
  193.         if (!feof(f))
  194.             printf("(%d more lines)", linecnt(f));
  195.         if (hdrs) {
  196.             printf("\n-----\n");
  197.             fclose(f);
  198.             continue;
  199.         }
  200.         if (feof(f))
  201.             printf("(continue) [yq] ");
  202.         else
  203.             printf(" type [ynq] ? ");
  204.         fflush(stdout);
  205.         inline[0] = 0;
  206.         fgets(inline, sizeof inline, stdin);
  207.         if (inline[0] == 0)
  208.             printf("\n");
  209.         if (inline[0] == 'q')
  210.             exit(1);
  211.         if (isdigit(inline[0])) {
  212.             sscanf(inline, "%d", &i);
  213.             printf("--Goto %d--\n", i);
  214.             i--;
  215.             fflush(stdout);
  216.                 fclose(f);
  217.                 continue;
  218.         }
  219.         if (inline[0] == 'n' || inline[0] == 'N') {
  220.             printf("--Flushed--\n");
  221.             fflush(stdout);
  222.             fclose(f);
  223.             if (i >= nextty)
  224.                 nextty = i + 1;
  225.             continue;
  226.         }
  227.         if (ruptible)
  228.             signal(SIGINT, onintr);
  229.         if (!feof(f))
  230.             for (;;) {
  231.                 putchar(c);
  232.                 c = getc(f);
  233.                 if (feof(f))
  234.                     break;
  235.             }
  236.         printf("--%s--\n", sep);
  237.         if (i >= nextty)
  238.             nextty = i + 1;
  239.         fflush(stdout);
  240.         if (ruptible)
  241.             signal(SIGINT, SIG_DFL);
  242.         fclose(f);
  243.     }
  244.     fseek(msgsrc, (long) 0, 0);
  245.     fprintf(msgsrc, "%d\n", nextty);
  246.     fclose(msgsrc);
  247.     if (qopt)
  248.         exit(0);
  249.     if (!already && !hush)
  250.         printf("No messages.\n");
  251.     exit(0);
  252. }
  253.  
  254. onintr()
  255. {
  256.  
  257.     sep = "Skip";
  258.     printf("\n");
  259.     fseek(f, (long) 0, 2);
  260. }
  261.  
  262. linecnt(f)
  263.     FILE *f;
  264. {
  265.     int l = 0;
  266.     char lbuf[BUFSIZ];
  267.     int c = 0;
  268.  
  269.     while (fgets(lbuf, sizeof lbuf, f))
  270.         l++, c += strlen(lbuf);
  271.     clearerr(f);
  272.     fseek(f, (long) -c, 1);
  273.     return (l);
  274. }
  275.