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

1.1       root        1: #include "parms.h"
                      2: #include "structs.h"
                      3: #include <sys/types.h>
                      4: #include <sys/stat.h>
                      5: 
                      6: #ifdef RCSIDENT
                      7: static char rcsid[] = "$Header: times.c,v 1.7 85/01/18 15:39:48 notes Rel $";
                      8: #endif RCSIDENT
                      9: 
                     10: /*
                     11:  *     getlast(atime, nf, seqon, name)
                     12:  *     struct when_f *atime; char *nf, *name
                     13:  *             retrieve last time that user was in the notefile.
                     14:  *     fixlast(atime, nf, seqon, name)
                     15:  *     struct when_f *atime; char *nf, *name
                     16:  *             update users last access time.
                     17:  *
                     18:  *     Restructure of Original effort (Nov 1981 by R. Essick)
                     19:  *     Coding: Ray Essick      January 1982
                     20:  */
                     21: 
                     22: long    lseek ();                                      /* make sure gets right type */
                     23: char   *malloc ();
                     24: 
                     25: getlast (atime, nf, seqon, name)
                     26: struct when_f  *atime;
                     27: char   *nf,
                     28:        *name;
                     29: {
                     30:     FILE * seqfile;
                     31:     FILE * fopen ();
                     32:     struct seq_f    entry;
                     33:     register int    retcode,
                     34:                     i;
                     35:     char    fn[WDLEN];
                     36:     int     fid,
                     37:             size;
                     38:     struct stat statval;
                     39:     char    holdit[NNLEN + 1];
                     40: 
                     41:     static int  nentries = 0;                          /* number in file */
                     42:     static int  havechecked = 0;                       /* for seq file */
                     43:     static char lastname[20];                          /* last name loaded */
                     44:     static struct seq_f *entries = (struct seq_f *) NULL;/* the times */
                     45: 
                     46:     *atime = Basetime;                                 /* pick "old time" */
                     47: 
                     48:     if (seqon < NOREADSEQ)                             /* don't read */
                     49:        return 0;                                       /* sequencer off */
                     50: 
                     51:     if (strcmp (name, lastname) != 0)                  /* new user */
                     52:     {                                                  /* may never happen */
                     53:        havechecked = 0;
                     54:        strcpy (lastname, name);
                     55:        if (entries)                                    /* do we have some? */
                     56:        {
                     57:            free (entries);                             /* return space */
                     58:            entries = (struct seq_f *) NULL;            /* and mark so */
                     59:        }
                     60:     }
                     61: 
                     62:     sprintf (fn, "%s/%s/%s", Mstdir, SEQUENCER, name);
                     63:     if (!havechecked)                                  /* try making incore */
                     64:     {
                     65:        havechecked++;                                  /* don't repeat */
                     66:        if (stat (fn, &statval) < 0)                    /* no file */
                     67:            size = 0;
                     68:        else
                     69:            size = statval.st_size;
                     70:        if (size != 0)                                  /* we have a file */
                     71:        {
                     72:            nentries = 0;                               /* ready to fail */
                     73:            if ((entries = (struct seq_f *) malloc (size)) == NULL)
                     74:                goto hardway;
                     75:            if ((fid = open (fn, 0)) < 0)
                     76:            {
                     77:                free (entries);                         /* return our space */
                     78:                goto hardway;                           /* couldn't open */
                     79:            }
                     80:            if (read (fid, entries, size) != size)      /* get it all */
                     81:            {
                     82:                free (entries);                         /* return space */
                     83:                close (fid);                            /* and the file */
                     84:                goto hardway;                           /* didn't read all */
                     85:            }
                     86:            nentries = size / (sizeof entry);
                     87:            close (fid);
                     88:        }
                     89:     }
                     90: 
                     91: hardway:                                               /* look it up */
                     92: 
                     93: /*
                     94:  *     Make sure the sequencer entry is short enough 
                     95:  *     This turns out to be a constant which can be resolved at 
                     96:  *     compile time!
                     97:  */
                     98:     i = (FILENAMELEN > NNLEN) ? NNLEN : FILENAMELEN;   /* shorter one */
                     99:     if (strlen (nf) >= i)                              /* 14 before 4.2 */
                    100:     {
                    101:        strncpy (holdit, nf, i);                        /* copy them */
                    102:        holdit[i] = '\0';                               /* and terminate */
                    103:        nf = holdit;                                    /* move pointer */
                    104:     }
                    105: 
                    106:     if (nentries == 0)                                 /* not pre-loaded */
                    107:     {
                    108:        if ((seqfile = fopen (fn, "r")) == NULL)
                    109:            return (-1);                                /* no file, return default */
                    110:        do
                    111:            retcode = fread (&entry, sizeof entry, 1, seqfile);
                    112:        while (retcode && strcmp (entry.nfname, nf) != 0);
                    113: 
                    114:        fclose (seqfile);                               /* close the file */
                    115: 
                    116:        if (strcmp (entry.nfname, nf) == 0)
                    117:        {
                    118:            copydate (&entry.lastin, atime);            /* give him time */
                    119:            return 0;
                    120:        }
                    121:        else
                    122:            return (-1);
                    123:     }
                    124:     else                                               /* search table */
                    125:     {
                    126:        for (i = 0; i < nentries; i++)
                    127:            if (strcmp (entries[i].nfname, nf) == 0)
                    128:            {
                    129:                copydate (&entries[i].lastin, atime);
                    130:                return 0;
                    131:            }
                    132:     }
                    133:     return 0;
                    134: }
                    135: 
                    136: fixlast (atime, nf, seqon, name)
                    137: struct when_f  *atime;
                    138: char   *nf,
                    139:        *name;
                    140: {
                    141:     struct seq_f    entry;
                    142:     register int    fid;                               /* file descriptor */
                    143:     register int    atend,
                    144:                     i;
                    145:     char    fn[WDLEN];
                    146:     char    holdit[NNLEN + 1];                         /* hold a nf name */
                    147: 
                    148:     if (seqon > NOWRITESEQ)                            /* if updating */
                    149:     {
                    150:        sprintf (fn, "%s/%s/%s", Mstdir, SEQUENCER, name);
                    151: 
                    152:        if ((fid = open (fn, 2)) < 0)                   /* open up */
                    153:        {
                    154:            x ((fid = creat (fn, 0666)) < 0, "fixlast: create");
                    155:            x (close (fid) < 0, "fixlast: close I");
                    156:            x ((fid = open (fn, 2)) < 0, "fixlast: open write");
                    157:        }
                    158: 
                    159: /*
                    160:  *     Make sure the sequencer entry is short enough 
                    161:  */
                    162:        i = (FILENAMELEN > NNLEN) ? NNLEN : FILENAMELEN;/* shorter one */
                    163:        if (strlen (nf) >= i)                           /* 14 before 4.2 */
                    164:        {
                    165:            strncpy (holdit, nf, i);                    /* copy them */
                    166:            holdit[i] = '\0';                           /* and terminate */
                    167:            nf = holdit;                                /* move pointer */
                    168:        }
                    169: 
                    170:        while ((atend = read (fid, &entry, sizeof entry)) == sizeof entry)
                    171:            if (strcmp (entry.nfname, nf) == 0)
                    172:                break;                                  /* found him */
                    173: 
                    174:        x (atend < 0, "fixlast: read error");
                    175:        if (atend == sizeof entry)                      /* not at the end */
                    176:            x (lseek (fid, -((long) sizeof entry), 1) < 0, "fixlast: reseek");
                    177:        else
                    178:        {
                    179:            strmove (nf, entry.nfname);                 /* build the entry */
                    180:            x (lseek (fid, 0L, 2) < 0, "fixlast: EOF seek");
                    181:                                                        /* make sure at end */
                    182:        }
                    183:        copydate (atime, &entry.lastin);
                    184:        x (write (fid, &entry, sizeof entry) != sizeof entry, "fixlast: write");
                    185:        x (close (fid) < 0, "fixlast: close II");
                    186:     }
                    187: }

unix.superglobalmegacorp.com

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