home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------
- * $Id: vacupd.c,v 1.1 1993/10/25 13:15:51 carlson Exp $
- *
- * A program to read and modify data in the vacation database files.
- *
- * Revision History:
- * $Log: vacupd.c,v $
- * Revision 1.1 1993/10/25 13:15:51 carlson
- * Initial revision
- *
- *------------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <ndbm.h>
- #include <limits.h>
- #include <ctype.h>
- #include <string.h>
- #include <sys/types.h>
- #include <time.h>
- #include <sys/file.h>
- #include <strings.h>
-
- /*------------------------------------------------------------------------
- * DDDD EEEEE FFFFF III N N EEEEE SSS
- * D D E F I NN N E S S
- * D D E F I NN N E S
- * D D EEE FFF I N N N EEE SSS
- * D D E F I N NN E S
- * D D E F I N NN E S S
- * DDDD EEEEE F III N N EEEEE SSS
- *------------------------------------------------------------------------*/
-
- /* #define DEBUG */
-
- #define MAXLINE BUFSIZ
- #define VMSG ".vacation.msg" /* vacation message */
- #define VACAT ".vacation" /* dbm's database prefix */
- #define VDIR ".vacation.dir" /* dbm's DB prefix, part 1 */
- #define VPAG ".vacation.pag" /* dbm's DB prefix, part 2 */
-
- /*------------------------------------------------------------------------
- * SSS TTTTT RRRR U U CCC TTTTT U U RRRR EEEEE SSS
- * S S T R R U U C C T U U R R E S S
- * S T R R U U C T U U R R E S
- * SSS T RRRR U U C T U U RRRR EEE SSS
- * S T R R U U C T U U R R E S
- * S S T R R U U C C T U U R R E S S
- * SSS T R R UUU CCC T UUU R R EEEEE SSS
- *------------------------------------------------------------------------*/
-
- /*----
- * This structure is used within vacation.c to keep a list of
- * aliases for the current user. The list is derived from
- * the command line (-a option) and the password file.
- *----*/
-
- typedef struct alias {
- struct alias *next;
- char *name;
- } ALIAS;
-
- /*----
- * This is my little structure for keeping the information
- * found in the database.
- * key The key. This is the name of the sender complete
- * with the path.
- * date The date the first vacation message was sent to
- * the sender.
- *----*/
-
- typedef struct record {
- char *key;
- time_t date;
- struct record *next;
- } RECORD;
-
- /*------------------------------------------------------------------------
- * GGG L OOO BBBB AAA L SSS
- * G G L O O B B A A L S S
- * G L O O B B A A L S
- * G L O O BBBB AAAAA L SSS
- * G GGG L O O B B A A L S
- * G G L O O B B A A L S S
- * GGG LLLLL OOO BBBB A A LLLLL SSS
- *------------------------------------------------------------------------*/
-
- ALIAS *names = NULL;
- RECORD *records = NULL;
-
- /*------------------------------------------------------------------------
- * L OOO CCC AAA L SSS
- * L O O C C A A L S S
- * L O O C A A L S
- * L O O C AAAAA L SSS
- * L O O C A A L S
- * L O O C C A A L S S
- * LLLLL OOO CCC A A LLLLL SSS
- *------------------------------------------------------------------------*/
-
- static DBM *db;
-
- static char *VIT = "__VACATION__INTERVAL__TIMER__";
- static char *ProgName;
- static char buffer[MAXLINE];
- static time_t VIT_interval;
-
- /*------------------------------------------------------------------------
- * Linked list insert command
- *------------------------------------------------------------------------*/
-
- void addItem (datum key, time_t then)
- {
- RECORD *currentR;
-
- if (records)
- {
- for (currentR = records; currentR->next; currentR = currentR->next);
- currentR->next = malloc (sizeof (RECORD));
- currentR = currentR->next;
- }
- else
- {
- records = malloc (sizeof (RECORD));
- currentR = records;
- }
- currentR->next = NULL;
- currentR->key = malloc (key.dsize + 1);
- strncpy (currentR->key, key.dptr, key.dsize);
- currentR->key[key.dsize] = '\0';
- currentR->date = then;
- }
-
- /*------------------------------------------------------------------------
- * Linked list remove command
- *------------------------------------------------------------------------*/
-
- void removeItem (datum key)
- {
- RECORD *currentR, *previousR;
- char *item = NULL;
-
- for (previousR = NULL, currentR = records; currentR;
- currentR = currentR->next)
- {
- item = malloc (key.dsize + 1);
- strncpy (item, key.dptr, key.dsize);
- item[key.dsize] = '\0';
-
- if (strcmp (item, currentR->key) == 0)
- break;
-
- free (item);
- previousR = currentR;
- }
-
- if (item)
- free (item);
-
- if (currentR)
- {
- if (previousR)
- previousR->next = currentR->next;
- else
- records = currentR->next;
-
- free (currentR->key);
- }
- }
-
- /*------------------------------------------------------------------------
- * Print collected info.
- *------------------------------------------------------------------------*/
-
- void print_info (void)
- {
- int i;
- char *cptr;
- RECORD *currentR;
- int days, hours, minutes, seconds;
-
- printf ("Interval set to:\n");
- i = VIT_interval;
- minutes = i / 60;
- seconds = i - (minutes * 60);
- hours = minutes / 60;
- minutes -= hours * 60;
- days = hours / 24;
- hours -= days * 24;
- printf (" %d days, %d hours, %d minutes, %d seconds\n", days, hours,
- minutes, seconds);
-
- printf ("Keys found:\n");
- for (currentR = records; currentR; currentR = currentR->next)
- {
- cptr = ctime (¤tR->date);
- printf (" %s\t%s", currentR->key, cptr);
- }
- }
-
- /*------------------------------------------------------------------------
- * Provide menu of commands and process.
- *------------------------------------------------------------------------*/
-
- int do_menu (void)
- {
- int do_again = 1;
- char inline[20];
- datum key, data;
- time_t then;
- RECORD *currentR = NULL;
- char *cptr;
- int i, choice;
-
- printf ("\nMenu:\n");
- printf ("1 Add user\n");
- printf ("2 Add ignore user\n");
- printf ("3 Remove user\n");
- printf ("4 Print current contents\n");
- printf ("99 Quit\n");
-
- printf ("\nEnter option: ");
- gets (inline);
- printf ("\n");
-
- switch (choice = atoi (inline))
- {
- case 1: /* Add user */
- case 2: /* Add ignore user */
- printf ("Enter user name: ");
- gets (inline);
- cptr = inline + strlen (inline) - 1;
- while (iscntrl (*cptr))
- *cptr++ = '\0';
- key.dptr = inline;
- key.dsize = strlen (inline);
-
- then = (choice == 2) ? LONG_MAX : time (NULL);
- data.dptr = (char *)&then;
- data.dsize = sizeof (then);
- i = dbm_store (db, key, data, DBM_INSERT);
- if (i)
- {
- fprintf (stderr, "*** Error %d inserting record.\n", i);
- }
- else
- {
- addItem (key, then);
- }
- break;
-
- case 3: /* Remove user */
- printf ("Enter user name: ");
- gets (inline);
- cptr = inline + strlen (inline) - 1;
- while (iscntrl (*cptr))
- *cptr++ = '\0';
- key.dptr = inline;
- key.dsize = strlen (inline);
-
- i = dbm_delete (db, key);
- if (i)
- {
- fprintf (stderr, "*** Error %d deleting record\n", i);
- }
- else
- {
- removeItem (key);
- }
- break;
-
- case 4: /* Print current contents */
- print_info ();
- break;
-
- case 99: /* Quit */
- do_again = 0;
- break;
-
- default:
- fprintf (stderr, "*** Unrecognized command\n");
- }
-
- return do_again;
- }
-
- /*------------------------------------------------------------------------
- * M M AAA III N N
- * MM MM A A I NN N
- * MM MM A A I NN N
- * M M M AAAAA I N N N
- * M M A A I N NN
- * M M A A I N NN
- * M M A A III N N
- *------------------------------------------------------------------------*/
-
- main (int argc, char *argv[])
- {
- register int i;
- char *cptr;
- datum key, data;
- time_t then;
- RECORD *currentR;
-
- /*----
- * Get the name of this program and save in ProgName.
- *----*/
-
- cptr = rindex (argv[0], '/');
- if (cptr)
- cptr++;
- else
- cptr = argv[0];
- ProgName = strdup (cptr);
-
- /*----
- * Check that we can access the directory file.
- *----*/
-
- if (access (VDIR, F_OK))
- {
- fprintf (stderr, "%s: Unable to access %s\n", ProgName, VDIR);
- fprintf (stderr, "\t%s\n", strerror (errno));
- exit (-1);
- }
-
- /*----
- * Open the database.
- *----*/
-
- if (!(db = dbm_open (VACAT, O_RDWR, 0)))
- {
- fprintf (stderr, "%s: Unable to open %s\n", ProgName, VACAT);
- fprintf (stderr, "\t%s\n", strerror (errno));
- exit (-1);
- }
-
- /*----
- * Scan through the keys and put them into my little structure.
- *----*/
-
- for (key = dbm_firstkey (db);
- key.dptr != NULL;
- key = dbm_nextkey (db))
- {
- strncpy (buffer, key.dptr, key.dsize);
- buffer[key.dsize] = '\0';
- #ifdef DEBUG
- printf ("[%d]%s = ", key.dsize, buffer);
- #endif
-
- data = dbm_fetch (db, key);
- bcopy (data.dptr, (char *)&then, sizeof (then));
- #ifdef DEBUG
- cptr = ctime (&then);
- printf ("[%d](%08x)%s\n", data.dsize, then, cptr);
- #endif
-
- if (strncmp (key.dptr, VIT, key.dsize) == 0)
- {
- VIT_interval = then;
- }
- else
- {
- addItem (key, then);
- }
- }
-
- #ifdef DEBUG
- printf ("Test\n");
- key.dptr = "millard";
- key.dsize = strlen (key.dptr);
- printf ("key = '%s'\n", key.dptr);
- printf ("sizeof (key) = %d\n", key.dsize);
- data = dbm_fetch (db, key);
- if (data.dptr)
- {
- printf ("Key found\n");
- bcopy (data.dptr, (char *)&then, sizeof (then));
- cptr = ctime (&then);
- printf (" time = (%08x)%s\n", then, cptr);
- }
- else
- {
- printf ("Key not found\n");
- }
- #endif
-
- /*----
- * Print out what we have so far.
- *----*/
-
- print_info ();
-
- /*----
- * Print menu of choices.
- *----*/
-
- while (do_menu ());
-
- dbm_close (db);
- }
-