home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06110a < prev    next >
Text File  |  1991-02-17  |  5KB  |  187 lines

  1. /********************************************************
  2.  *    j.c:    Personal Journal Management Program
  3.  *            Written by Leor Zolman, 1/91
  4.  *    Usage:
  5.  *        j [<subdir>]
  6.  *            
  7.  *    What it does:
  8.  *        Creates a journal entry and appends it to the
  9.  *        appropriate  monthly incremental journal file
  10.  *        in the JOURN_DIR (DOS) or $HOME/journ (Xenix)
  11.  *        directory.
  12.  *        If the <subdir> argument is given, then the
  13.  *        <subdir> subdirectory (of JOURN_DIR or 
  14.  *        $HOME/journ) is used instead.
  15.  *
  16.  *  Compile:
  17.  *        Turbo C[++]:
  18.  *              tcc j.c
  19.  *        Xenix C:
  20.  *             cc j.c
  21.  *******************************************************/
  22.  
  23. #include <stdio.h>
  24. #include <time.h>
  25.  
  26. #define DOS        0        /* 1 for DOS, 0 for Xenix/Unix    */
  27. #define DEBUG    0        /* 1 to debug system() calls    */ 
  28. #define EPSILON    0        /* using DOS version of Epsilon    */
  29.  
  30. #if DOS
  31.     #include <sys\stat.h>
  32.     #define DIR_DELIM    "\\"        /* pathlist delimiter */
  33.     #define JOURN_DIR    "c:\\etc\\journ"    /* master dir */
  34.     #define ED_INVOKE    "e %s +3"     /* editor invokation */
  35. #else
  36.     #include <sys/types.h>    /* stat.h need this */
  37.     #include <sys/stat.h>
  38.     #include <errno.h>
  39.     char *getenv();
  40.     #define DIR_DELIM    "/"
  41.     #define JOURN_DIR    "journ"
  42.     #define ED_INVOKE    "/usr/bin/e %s"
  43. #endif
  44.  
  45. #if DEBUG                     /* expand DBSYS() to show text */    
  46.     #define DBSYS(string)    \
  47.         (printf("\nAbout to make sys call: %s\n", string),\
  48.         printf("press return..."), getchar(), system(string))
  49. #else
  50.     #define    DBSYS(string)    system(string)    /* no debugging */
  51. #endif
  52.  
  53. #define JTMP        "journ.tmp"            /* temp file */
  54.  
  55. int testdir(char *);
  56. void abortf(char *, char *);
  57.  
  58. main(argc, argv, envp)
  59. int argc;
  60. char **argv, **envp;
  61. {
  62.     char j_dir[80];            /* Directory where entry will go  */
  63.     char j_tmp[80];            /* temporary file name buffer */
  64.     time_t timer;            /* For creating ASCII date string */
  65.     struct tm *tblock;
  66.  
  67.     long nfsize, ofsize;    /* For saving file sizes       */
  68.     struct stat statbuf;    /* open file statistics buffer */
  69.     
  70.     char fname[50];            /* for constructing file names */
  71.     char cmd[100];            /* system command text         */
  72.     FILE *fp;
  73.     int i;
  74.     
  75.     printf("Electronic Journal Management System v1.0\n\n");
  76.  
  77. #if EPSILON && DOS            /* If Epsilon running, abort   */
  78.     for (; *envp; envp++)
  79.     {
  80.         strcpy(j_dir, *envp);
  81.         if (!strcmp(j_dir, "EPSRUNS=Y"))
  82.             abortf("Epsilon is active. Please exit first.");
  83.     }
  84. #endif
  85.  
  86. #if DOS
  87.     strcpy(j_dir, JOURN_DIR);    /* construct directory name */
  88. #else
  89.     sprintf(j_dir, "%s/%s", getenv("HOME"), JOURN_DIR);
  90.     #if DEBUG
  91.         printf("j_dir set to: %s\n", j_dir);
  92.     #endif
  93. #endif
  94.  
  95.     testdir(j_dir);            /* check for/create master dir    */
  96.  
  97.     if (argc == 2)            /* subdir specified? */
  98.     {
  99.         strcat(j_dir, DIR_DELIM);    /* yes, append name        */
  100.         strcat(j_dir,argv[1]);    /* to master directory name    */
  101.         testdir(j_dir);        /* test for/create subdirectory */
  102.     }
  103.  
  104.     printf("\nStarting up your text editor ");
  105.     printf("on a new journal entry...\n");
  106.  
  107.     sprintf(j_tmp, "%s%s%s", j_dir, DIR_DELIM, JTMP);
  108.     
  109.     if ((fp = fopen(j_tmp, "w")) == NULL)
  110.         abortf("Can't create %s\n", j_tmp);
  111.  
  112.     timer = time(NULL);        /* get ASCII time/date string    */
  113.     tblock = localtime(&timer);
  114.     fprintf(fp, "\n\t\t\t\t\t\t%s", asctime(tblock));
  115.  
  116.     fflush(fp);                        /* so fstat works        */
  117.     fstat(fileno(fp), &statbuf);    /* get file stats        */
  118.     ofsize = statbuf.st_size;        /* save file length        */
  119.     fclose(fp);
  120.     
  121.     sprintf(cmd, ED_INVOKE, j_tmp);    /* construct sys call    */
  122.     if (DBSYS(cmd))                    /* edit the temp file    */
  123.         abortf("Error invoking editor.", NULL);
  124.     printf("\n");
  125.  
  126.     if ((fp = fopen(j_tmp, "r")) == NULL) /* can't happen...    */
  127.         abortf("Error: %s has disappeared! Aborting.\n", j_tmp);
  128.  
  129.                     /* Check if the temp file was modified:    */
  130.     fstat(fileno(fp), &statbuf);   /* stat the updated file    */
  131.     nfsize = statbuf.st_size;        /* get new file size    */
  132.     fclose(fp);
  133.     if (nfsize == ofsize)            /* size unchanged?        */
  134.     {
  135.         unlink(j_tmp);
  136.         abortf("You didn't make any changes. Ignored.\n", NULL);
  137.     }
  138.     
  139.     printf("\n");
  140.                         /* construct name of journal file    */
  141.     sprintf(fname, "%s%s%02d-%02d.txt", j_dir, DIR_DELIM,
  142.         tblock->tm_year % 100, tblock->tm_mon + 1);
  143.  
  144.                                  /* update journal file        */
  145.     sprintf(cmd, "cat %s >> %s", j_tmp, fname);
  146.     DBSYS(cmd);
  147.     unlink(j_tmp);
  148.     printf("\nEntry appended onto journal file %s\n", fname);
  149.     exit(0);
  150. }
  151.  
  152. /*
  153.  * Test for existence of the named directory, and create
  154.  * if necessary (pending user's approval):
  155.  */
  156.  
  157. int testdir(dirname)
  158. char *dirname;
  159. {
  160.     char cmd[100];            /* system command text        */
  161.     char str[80];
  162.     struct stat statbuf;    /* open file statistics buffer */
  163.  
  164.     if (stat(dirname, &statbuf) != 0)    /* if doesn't exist */
  165.         ;                        /* then prompt for creation */
  166.     else if (statbuf.st_mode & S_IFDIR)
  167.         return;                    /* if directory, no problem */
  168.     else                /* exists, but not a directory...    */
  169.         abortf("%s exists, but isn't a directory!\n", dirname);
  170.  
  171.                 /* ask user if he wants to create the dir    */
  172.     printf("Create new directory '%s' (y/n)? ", dirname);
  173.     if (tolower(*gets(str)) == 'n')
  174.         abortf("Program over.\n", NULL);
  175.  
  176.     sprintf(cmd, "mkdir %s", dirname);
  177.     if (DBSYS(cmd))
  178.         abortf("Error creating directory.\n", NULL);
  179. }
  180.  
  181. void abortf(fmt, arg)
  182. char *fmt, *arg;
  183. {
  184.     printf(fmt, arg);
  185.     exit(1);
  186. }
  187.