home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / source / s2 / mail.c next >
Encoding:
C/C++ Source or Header  |  1975-05-13  |  4.1 KB  |  282 lines

  1. #
  2. /* mail command usage
  3.     mail
  4.         prints your mail
  5.     mail people
  6.         sends standard input to people
  7.  */
  8.  
  9. #define    SIGINT    2
  10.  
  11. struct    utmp {
  12.     char    name[8];
  13.     char    tty;
  14.     char    pad1;
  15.     int    ltime[2];
  16.     int    pad2;
  17. };
  18.  
  19. struct    passwd {
  20.     char    *pw_name;
  21.     char    *pw_passwd;
  22.     int    pw_uid;
  23.     int    pw_gid;
  24.     char    *pw_gecos;
  25.     char    *pw_dir;
  26.     char    *pw_shell;
  27. };
  28.  
  29. char    lettmp[] "/tmp/maxxxxx";
  30. char    preptmp[] "/tmp/mbxxxxx";
  31. int    pwfil;
  32.  
  33. main(argc, argv)
  34. char **argv;
  35. {
  36.     register me;
  37.     extern fout;
  38.     register struct passwd *p;
  39.     register char *cp;
  40.     static struct utmp ubuf;
  41.     int uf;
  42.  
  43.     maketemp();
  44.     if (argc==1 || argc==2 && argv[1][0]=='-') {
  45.         printmail(argc, argv);
  46.         delexit();
  47.     }
  48.     signal(SIGINT, delexit);
  49.     fout = creat(lettmp, 0600);
  50.     if (((me=ttyn(1))!='x' || (me=ttyn(2))!='x')
  51.      && (uf = open("/etc/utmp", 0)) > 0) {
  52.         while (read(uf, &ubuf, sizeof ubuf) == sizeof ubuf)
  53.             if (ubuf.tty == me) {
  54.                 ubuf.name[8] = ' ';
  55.                 close(uf);
  56.                 for (cp=ubuf.name; *cp++!=' ';);
  57.                 *--cp = 0;
  58.                 bulkmail(argc, argv, ubuf.name);
  59.             }
  60.     }
  61.     me = getuid() & 0377;
  62.     setpw();
  63.     for (;;)
  64.         if ((p = getpwent()) && p->pw_uid == me)
  65.             bulkmail(argc, argv, p->pw_name);
  66.     fout = 1;
  67.     printf("Who are you?\n");
  68.     delexit();
  69. }
  70.  
  71. printmail(argc, argv)
  72. char **argv;
  73. {
  74.     extern fin, fout;
  75.     register n, c, f;
  76.  
  77.     if (fopen(".mail", &fin)>=0 && (c = getchar())) {
  78.         do {
  79.             putchar(c);
  80.         } while (c = getchar());
  81.         close(fin);
  82.         c = 'y';
  83.         if (argc<2) {
  84.             if (ttyn(0)!='x') {
  85.                 printf("Save?");
  86.                 fin = 0;
  87.                 c = getchar();
  88.             }
  89.         } else
  90.             c = argv[1][1];
  91.         if (c=='y') {
  92.             prepend(".mail", "mbox");
  93.             printf("Saved mail in 'mbox'\n");
  94.         }
  95.         close(creat(".mail"));
  96.     } else
  97.         printf("No mail.\n");
  98. }
  99.  
  100. bulkmail(argc, argv, from)
  101. char **argv, *from;
  102. {
  103.     extern fin, fout;
  104.     int tbuf[2];
  105.     register c;
  106.  
  107.     fin = 0;
  108.     (&fin)[1] = 0;
  109.     time(tbuf);
  110.     printf("From %s %s", from, ctime(tbuf));
  111.     while (c = getchar())
  112.         putchar(c);
  113.     putchar('\n');
  114.     flush();
  115.     close(fout);
  116.     while (--argc > 0)
  117.         sendto(*++argv);
  118.     delexit();
  119. }
  120.  
  121. sendto(person)
  122. char *person;
  123. {
  124.     static saved;
  125.     extern fout;
  126.     extern fin;
  127.     register struct passwd *p;
  128.  
  129.     setpw();
  130.     while (p = getpwent()) {
  131.         if (equal(p->pw_name, person)) {
  132.             if (prepend(lettmp, cat(p->pw_dir, "/.mail"))==0)
  133.                 break;
  134.             return;
  135.         }
  136.     }
  137.     fout = 1;
  138.     flush();
  139.     printf("Can't send to %s.\n", person);
  140.     if (ttyn(0)!='x' && saved==0) {
  141.         unlink("dead.letter");
  142.         saved++;
  143.         printf("Letter saved in 'dead.letter'\n");
  144.         prepend(lettmp, "dead.letter");
  145.     }
  146. }
  147.  
  148. prepend(from, to)
  149. char *from, *to;
  150. {
  151.     extern int fin, fout;
  152.  
  153.     fcreat(preptmp, &fout);
  154.     fopen(from, &fin);
  155.     while (putchar(getchar()));
  156.     close(fin);
  157.     fopen(to, &fin);
  158.     while (putchar(getchar()));
  159.     close(fin);
  160.     flush();
  161.     close(fout);
  162.     if (fcreat(to, &fout) < 0) {
  163.         fout = 1;
  164.         return(0);
  165.     }
  166.     fopen(preptmp, &fin);
  167.     while(putchar(getchar()));
  168.     flush();
  169.     close(fout);
  170.     close(fin);
  171.     fout = 1;
  172.     return(1);
  173. }
  174.  
  175. setpw()
  176. {
  177.     extern fin;
  178.  
  179.     if (pwfil == 0) {
  180.         fopen("/etc/passwd", &fin);
  181.         pwfil = fin;
  182.     } else
  183.         fin = pwfil;
  184.     (&fin)[1] = 0;
  185.     seek(fin, 0, 0);
  186. }
  187.  
  188. getpwent()
  189. {
  190.     register char *p;
  191.     register c;
  192.     static struct passwd passwd;
  193.     static char line[100];
  194.     extern fin;
  195.  
  196.     p = line;
  197.     while((c=getchar()) != '\n') {
  198.         if(c <= 0)
  199.             return(0);
  200.         if(p < line+98)
  201.             *p++ = c;
  202.     }
  203.     *p = 0;
  204.     p = line;
  205.     passwd.pw_name = p;
  206.     p = pwskip(p);
  207.     passwd.pw_passwd = p;
  208.     p = pwskip(p);
  209.     passwd.pw_uid = atoi(p);
  210.     p = pwskip(p);
  211.     passwd.pw_gid = atoi(p);
  212.     p = pwskip(p);
  213.     passwd.pw_gecos = p;
  214.     p = pwskip(p);
  215.     passwd.pw_dir = p;
  216.     p = pwskip(p);
  217.     passwd.pw_shell = p;
  218.     return(&passwd);
  219. }
  220.  
  221. pwskip(ap)
  222. char *ap;
  223. {
  224.     register char *p;
  225.  
  226.     p = ap;
  227.     while(*p != ':') {
  228.         if(*p == 0)
  229.             return(p);
  230.         p++;
  231.     }
  232.     *p++ = 0;
  233.     return(p);
  234. }
  235.  
  236. delexit()
  237. {
  238.     unlink(lettmp);
  239.     unlink(preptmp);
  240.     exit(0);
  241. }
  242.  
  243. maketemp()
  244. {
  245.     int i, pid, d;
  246.  
  247.     pid = getpid();
  248.     for (i=11; i>=7; --i) {
  249.         d = (pid&07) + '0';
  250.         lettmp[i] = d;
  251.         preptmp[i] = d;
  252.         pid =>> 3;
  253.     }
  254. }
  255.  
  256. equal(as1, as2)
  257. {
  258.     register char *s1, *s2;
  259.  
  260.     s1 = as1;
  261.     s2 = as2;
  262.     while (*s1++ == *s2)
  263.         if (*s2++ == 0)
  264.             return(1);
  265.     return(0);
  266. }
  267.  
  268. cat(ap1, ap2)
  269. char *ap1, *ap2;
  270. {
  271.     register char *p1, *p2;
  272.     static char fn[32];
  273.  
  274.     p1 = ap1;
  275.     p2 = fn;
  276.     while (*p2++ = *p1++);
  277.     p2--;
  278.     p1 = ap2;
  279.     while (*p2++ = *p1++);
  280.     return(fn);
  281. }
  282.