Annotation of 43BSD/contrib/notes/src/savtxt.c, revision 1.1.1.1

1.1       root        1: #include "parms.h"
                      2: #include "structs.h"
                      3: 
                      4: #ifdef RCSIDENT
                      5: static char rcsid[] = "$Header: savtxt.c,v 1.7 85/01/18 15:39:14 notes Rel $";
                      6: #endif RCSIDENT
                      7: 
                      8: /*
                      9:  *     savtxt - will format and write the note/response handed to
                     10:  *     it in a specified file in the users working directory.
                     11:  *     We have to do some forking in this case because the directory
                     12:  *     may be protected against us. 
                     13:  *     first save the text in a /tmp/nfs file, then fork and
                     14:  *     setuid to the user. The file is then copied to the end
                     15:  *     of the ultimate destination.
                     16:  *             -- Thanks to Rick Spickelmier (tekcad!ricks)
                     17:  *
                     18:  *     returns the number of lines written to the file.
                     19:  *
                     20:  *     Original Coding:        Ray Essick      December 1981
                     21:  */
                     22: 
                     23: long    lseek ();                                      /* declare for lint */
                     24: FILE * popen ();                                       /* for lint */
                     25: #include <signal.h>                                    /* define signal processing */
                     26: 
                     27: savtxt (io, where, author, date, txtwhere, title)
                     28: struct io_f *io;
                     29: char   *where;
                     30: struct auth_f  *author;
                     31: struct when_f  *date;
                     32: struct daddr_f *txtwhere;
                     33: char   *title;
                     34: {
                     35:     int     (*p) (),
                     36:             (*q) (),
                     37:             (*r) ();
                     38:     int     retcode,
                     39:             lines;
                     40:     int     c;                                         /* character scratch */
                     41:     register    pid,
                     42:                 rpid;
                     43:     FILE * txtfile;
                     44:     FILE * outfile;
                     45:     char    tmpfile[20];                               /* hold scratch file name */
                     46: 
                     47:     sprintf (tmpfile, "/tmp/nfs%d", getpid ());
                     48:     if ((txtfile = fopen (tmpfile, "w")) == NULL)
                     49:        return 0;                                       /* no lines saved */
                     50:     lines = preptxt (io, txtfile, author, date, txtwhere, title);
                     51:     fclose (txtfile);
                     52: 
                     53: /*
                     54:  *     open it now so we don't have to hassle with permissions and
                     55:  *     such.  the file is passed across the fork.
                     56:  */
                     57:     if ((txtfile = fopen (tmpfile, "r")) == NULL)
                     58:        return 0;                                       /* couldn't reopen */
                     59: 
                     60:     if ((pid = fork ()) == 0)                          /* also makes him own file */
                     61:     {
                     62: #ifdef RUNSUID
                     63:        x (setuid (globuid) < 0, "savtxt: couldn't setuid(your uid)");
                     64: #else
                     65:        x (setgid (getgid ()) < 0, "savtxt: couldn't setgid(your gid)");
                     66: #endif RUNSUID
                     67:        if (where[0] == '|')                            /* make it a pipe */
                     68:            outfile = popen (&where[1], "w");           /* writing */
                     69:        else
                     70:            outfile = fopen (where, "a");               /* a simple file */
                     71:        if (outfile == NULL)                            /* no way jose */
                     72:            exit (1);                                   /* error */
                     73: /*
                     74:  *     copy the file over
                     75:  *             there are faster ways... this works.
                     76:  */
                     77:        while ((c = getc (txtfile)) != EOF)
                     78:            putc (c, outfile);                          /* copy it */
                     79:        if (where[0] == '|')
                     80:            pclose (outfile);                           /* pipes wait */
                     81:        else
                     82:            fclose (outfile);
                     83:        fclose (txtfile);
                     84:        exit (0);                                       /* exit ok */
                     85:     }                                                  /* end of forked process code */
                     86: 
                     87:     /* wait here for the son to finish */
                     88:     p = signal (1, SIG_IGN);
                     89:     q = signal (2, SIG_IGN);
                     90:     r = signal (3, SIG_IGN);
                     91:     while ((rpid = wait (&retcode)) != pid && rpid != -1);
                     92:     if (rpid == -1)
                     93:        retcode = -1;
                     94:     signal (1, p);
                     95:     signal (2, q);
                     96:     signal (3, r);
                     97: 
                     98:     fclose (txtfile);                                  /* close it in this one too */
                     99:     x (unlink (tmpfile) < 0, "savtxt: bad unlink of text file");
                    100:     if (retcode)                                       /* something wrong */
                    101:        return 0;                                       /* no lines saved */
                    102:     else
                    103:        return lines;
                    104: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.