/* * \- prepend dated input to file specified by option * usage: %note [-f filename, -c "date_string", -n, -s] * prepend input to file specified by option or parm, * with date heading each line * -c preprends to calendar, -n to notes, -s to shop, and * -f to filename in $home or $home/memos * -n is default * j.a. rupley, tucson, arizona * */ #include #include #include #include char usage[2000] = { "\n\ usage: note [ {-f filename, -c \"date_string\", -n, -s} ]\n\ \n\ no argument implies -n\n\ {-c, -n, -s} imply files {calendar, notes, shop}\n\ \n\ the target file is looked for in $HOME/memos, and if not found,\n\ the file is created there (except, $HOME is the calendar directory);\n\ \n\ text input is by line, with a date string heading each line;\n\ input is terminated with ^D = EOF\n\ input is first to a temporary file;\n\ at the end of input, the target file is copied to target.bak, \n\ and the temporary is preprepended to the target\n\ \n\ \"date_string\" for -c option:\n\n\ {now | weekday | month day[, year] | mm/dd[/yy[yy]] | \n\ {month|mm} number weekday } \n\ [ +-number { {d | w | m | y} | weekday } [....] ]\n\ " }; int main(argc, argv) int argc; char **argv; { extern char *strcat(); extern char *strcpy(); extern void exit(); extern void perror(); extern char *optarg; extern int optind, opterr; extern int getopt(); extern int getpid(); extern FILE *fopen(); extern char *fgets(); extern char *mktemp(); extern char *getenv(); extern char *getdate(); FILE *fptrtemp; FILE *fptrfname; char *datestring; char *homestring; char *tempfile; char tempbuf[80]; char *fname; char fnamebuf[80]; char fnamebak[80]; char linebuf[200]; int c; fname = "notes"; /* notes is the default file */ /* add more options = hard-wired note files, to your heart's content */ while ((c = getopt(argc, argv, "?zenscf:")) != EOF) { switch (c) { case 'n': fname = "notes"; break; case 's': fname = "shop"; break; case 'c': fname = "calendar"; break; case 'f': fname = optarg; break; case '?': case 'z': fprintf(stderr, "%s", usage); exit(-1); } } /* set up file pathnames (in ~/memos, except calendar in ~) */ if ((homestring = getenv("HOME")) == NULL) { perror("Cannot get HOME from environment"); exit (1); } else { if (strcmp(fname, "calendar")) { sprintf(fnamebuf, "%s/memos/%s", homestring, fname); fname = &fnamebuf[0]; } else { sprintf(fnamebuf, "%s/%s", homestring, fname); fname = &fnamebuf[0]; } sprintf(tempbuf, "%s/tmp/noteXXXXXX",homestring); } /* get date string = today's date for -n, -s, and -f options */ /* and for -c, a calculated date based on the arguments */ /* remaining after optarg processing */ if ((datestring = getdate(0, optind, argc, argv)) == NULL) { perror("No datestring returned"); exit (1); } /* open the temporary file; write dated input line by line into it */ if ((tempfile = mktemp(tempbuf)) == NULL) { (void )perror("Cannot create tempfile"); exit (1); } if ((fptrtemp = fopen(tempfile, "w+")) == NULL) { (void )perror("Cannot open tempfile"); exit (1); } if (isatty(fileno(stdin))) fprintf(stdout, "\n%s: ", datestring); while (fgets(linebuf, sizeof(linebuf), stdin) != NULL) { fprintf(fptrtemp, "%s: %s", datestring, linebuf); if (isatty(fileno(stdin))) fprintf(stdout, "%s: ", datestring); } /* this gives a blank line between records on the output files */ /* comment out the next line if you do not want it */ fputc('\n', fptrtemp); if (isatty(fileno(stdin))) fputc('\n', stdout); /* append old target file onto temporary */ /* and if successful, mv old target to target.bak */ if ((fptrfname = fopen(fname, "r")) == NULL) { perror("Warning: cannot open old target file"); } else { while (fgets(linebuf, sizeof(linebuf), fptrfname) != NULL) { if (fputs(linebuf, fptrtemp) == -1) { perror("Fatal: cannot append old target file to tempfile"); exit (1); } } fclose(fptrfname); strcpy(fnamebak, fname); strcat(fnamebak, ".bak"); if (unlink(fnamebak) == -1) { perror("Warning: cannot unlink old backup file "); } if (link(fname, fnamebak) == -1) { perror("Fatal: cannot link old target file to backup file"); exit (1); } } /* mv new temporary file = new information prepended to old target */ /* to old target */ fclose(fptrtemp); if (unlink(fname) == -1) { perror("Warning: cannot unlink old target file"); } if (link(tempfile, fname) == -1) { perror("Fatal: cannot link tempfile to target file"); exit (1); } unlink(tempfile); return (0); }