home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 04stdlib / notes2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.4 KB  |  107 lines

  1. /*
  2.  *    notes2 -- add a date/time stamped entry to a
  3.  *    "notes" data file.  Allow user to optionally
  4.  *    edit the data file upon completion of the entry.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <signal.h>
  11. #include <process.h>
  12. #include <local\std.h>
  13.  
  14. /* length of date/time string */
  15. #define DT_STR    26
  16.  
  17. main(argc, argv)
  18. int argc;
  19. char **argv;
  20. {
  21.     int n;        /* number of lines added */
  22.     int exitcode = 0;
  23.     FILE *fp;
  24.     static char notesfile[MAXPATH + 1] = { "notes.txt" };
  25.     static char editname[MAXPATH + 1] = { "edlin" };
  26.     char ch;
  27.     char dt_stamp[DT_STR];
  28.     char *s;
  29.     long ltime;
  30.     static char pgm[MAXNAME + 1] = { "notes3" };
  31.  
  32.     extern void fatal(char *, char *, int);
  33.     extern void getpname(char *, char *);
  34.     static int addtxt(FILE *, FILE *);
  35.  
  36.     if (_osmajor >= 3)
  37.         getpname(*argv, pgm);
  38.  
  39.     /* locate the "notes" database file and open it */
  40.     if (argc > 1)
  41.         strcpy(notesfile, *++argv);
  42.     else if (s = getenv("NOTESFILE"))
  43.         strcpy(notesfile, s);
  44.     if ((fp = fopen(notesfile, "a")) == NULL)
  45.         fatal(pgm, notesfile, 1);
  46.  
  47.     /* disable Ctrl-Break interrupt */
  48.     if (signal(SIGINT, SIG_IGN) == (int(*)())-1)
  49.         perror("Cannot set signal");
  50.  
  51.     /* append a header and date/time tag */
  52.     ltime = time(NULL);
  53.     strcpy(dt_stamp, ctime(<ime));
  54.     fprintf(stderr, "Appending to %s: %s", notesfile, dt_stamp);
  55.     fprintf(fp, "\n%s", dt_stamp);
  56.  
  57.     /* add text to notes file */
  58.     if ((n = addtxt(stdin, fp)) == 0) {
  59.         fputs("No new text", stderr);
  60.         if (fclose(fp))
  61.             fatal(pgm, notesfile, 2);
  62.         exit(0);
  63.     }
  64.     else
  65.         fprintf(stderr, "%d line(s) added to %s\n", n, notesfile);
  66.     if (fclose(fp))
  67.         fatal(pgm, notesfile, 2);
  68.  
  69.     /* optionally edit text in the notes file */
  70.     fprintf(stderr, "E + ENTER to edit; ENTER alone to quit: ");
  71.     while ((ch = tolower(getchar())) != '\n')
  72.         if (ch = 'e') {
  73.             if (s = getenv("EDITOR"))
  74.                 strcpy(editname, s);
  75.             if ((exitcode = spawnlp(P_WAIT, editname, editname,
  76.                 notesfile, NULL)) == -1)
  77.                 fatal(pgm, editname, 3);
  78.         }
  79.     exit(exitcode);
  80. }
  81.  
  82. /*
  83.  *    addtxt -- append new text to notes file
  84.  */
  85. static int addtxt(fin, fout)
  86. FILE *fin, *fout;
  87. {
  88.     int ch;
  89.     int col = 0;    /* column */
  90.     int n = 0;    /* number of lines added */
  91.  
  92.     while ((ch = fgetc(fin)) != EOF) {
  93.         if (ch == '.' && col == 0) {
  94.             ch = fgetc(fin); /* trash the newline */
  95.             break;
  96.         }
  97.         fputc(ch, fout);
  98.         if (ch == '\n') {
  99.             col = 0;
  100.             ++n;
  101.         }
  102.         else
  103.             ++col;
  104.     }
  105.     return (n);
  106. }
  107.