Annotation of researchv10no/cmd/asd++/udaemon.c, revision 1.1.1.1

1.1       root        1: #include "decl.h"
                      2: #include <fstream.h>
                      3: 
                      4: /* udaemon: do the processing for a single user */
                      5: 
                      6: /* Does this string represent the name of a status file? */
                      7: static int
                      8: isstat (String s)
                      9: {
                     10:        if (s.length() <= 2)
                     11:                        return 0;
                     12:        return String (s (s.length()-2, 2)) == ".s";
                     13: }
                     14: 
                     15: /* Return the data file name corresponding to this string */
                     16: static String
                     17: datafile (String s)
                     18: {
                     19:        return String (s(0,s.length()-2)) + ".d";
                     20: }
                     21: 
                     22: /* Read a status file and return its contents */
                     23: static map
                     24: readstatus (String s)
                     25: {
                     26:        map m;
                     27:        char *buf = new char[s.length() + 1];
                     28:        s.dump (buf);
                     29:        filebuf fb;
                     30:        if (fb.open (buf, input)) {
                     31:                istream i (&fb);
                     32:                i >> m;
                     33:        }
                     34:        delete buf;
                     35:        return m;
                     36: }
                     37: 
                     38: /* Rewrite a map into a status file */
                     39: static void
                     40: writestatus (Path file, map m)
                     41: {
                     42:        String tempfile = file.rmlast() & "stemp";
                     43:        char *buf = new char[tempfile.length() + 1];
                     44:        tempfile.dump (buf);
                     45:        filebuf fb;
                     46:        if (fb.open (buf, output)) {
                     47:                ostream o (&fb);
                     48:                o << m;
                     49:                o.flush();
                     50:                if (o) {
                     51:                        unlink (file);
                     52:                        link (tempfile, file);
                     53:                        unlink (tempfile);
                     54:                }
                     55:        }
                     56:        delete buf;
                     57: }
                     58: 
                     59: static int
                     60: listsearch (String_list sl, String s)
                     61: {
                     62:        String_list_iterator i (sl);
                     63:        String t;
                     64:        while (i.nextX (t)) {
                     65:                if (s == t)
                     66:                        return 1;
                     67:        }
                     68:        return 0;
                     69: }
                     70: 
                     71: String_list deadmach;
                     72: 
                     73: /* Do all the work for a status file */
                     74: static void
                     75: dofile (Path sfile, Path dfile)
                     76: {
                     77:        map statmap = readstatus (sfile);
                     78:        mel_list_iterator mli (statmap.l);
                     79:        mel m;
                     80:        int keepfile = 0, sendfile = 0;
                     81:        while (mli.nextX (m)) {
                     82:                if (m.p.strchr('#') < 0 &&
                     83:                    (m.q.length() == 0 || m.s.length() != 0)) {
                     84:                        m.q = String_list();
                     85:                        if (listsearch (deadmach, m.p)) {
                     86:                                keepfile = 1;
                     87:                                m.s = "deferred for sequence";
                     88:                                mli.replace (m);
                     89:                        } else {
                     90:                                m.s = domach (m.p, dfile, m.q);
                     91:                                if (m.s.length() == 0) {
                     92:                                        sendfile = 1;
                     93:                                        if (m.q.length() == 0)
                     94:                                                mli.remove();
                     95:                                        else
                     96:                                                mli.replace (m);
                     97:                                } else {
                     98:                                        keepfile = 1;
                     99:                                        deadmach += m.p;
                    100:                                        mli.replace (m);
                    101:                                }
                    102:                        }
                    103:                }
                    104:                writestatus (sfile, statmap);
                    105:        }
                    106: 
                    107:        int rc = 0;
                    108: 
                    109:        if (sendfile || !keepfile) {
                    110:                rc = system ("mail " + logname() + " <" + String(sfile));
                    111:        }
                    112: 
                    113:        if (!keepfile && rc == 0) {
                    114:                unlink (sfile);
                    115:                unlink (dfile);
                    116:        }
                    117: }
                    118: 
                    119: main()
                    120: {
                    121:        String ln = logname();
                    122:        Path myspool = spooldir & ln;
                    123: 
                    124:        umask (022);
                    125:        lock (myspool);
                    126: 
                    127:        Path_list files = dircontents (myspool);
                    128:        Path_list_iterator pli (files);
                    129:        Path f;
                    130: 
                    131:        while (pli.nextX (f)) {
                    132:                if (isstat (f))
                    133:                        dofile (myspool & f, myspool & datafile(f));
                    134:        }
                    135:        unlock (myspool);
                    136:        return 0;
                    137: }

unix.superglobalmegacorp.com

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