home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume5 / rsed / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.4 KB  |  110 lines

  1.  
  2. /*
  3.  * This code copyright 1988 by Doug Davis (doug@letni.lawnet.com) 
  4.  *  You are free to modify, hack, fold, spindle, or mutlate this code in
  5.  *  any maner provided you give credit where credit is due and don't pretend
  6.  *  you wrote it.
  7.  *  If you do my lawyers (and I have a lot of lawyers) will teach you a lesson
  8.  *  in copyright law that you will never ever forget.
  9.  */
  10. #include "defs.h"
  11. #include "externs.h"
  12.  
  13. main(argc, argv)
  14. int argc;
  15. char **argv;
  16. {
  17.     int c;
  18.     char start_line[80];
  19.     extern char *optarg;
  20.     extern int optind, opterr;
  21.     opterr=1;
  22.     Progname = *argv;
  23. #ifndef MSDOS
  24.     savetty();
  25.     initscr();
  26. #endif MSDOS
  27.     signal(SIGINT, SIG_IGN);
  28.     while ((c=getopt(argc, argv, "Dwa:")) != EOF) {
  29.         switch (c) {
  30.         case 'D': Debug = 1;            break;
  31.         case 'a': strcpy(start_line,optarg);    break;
  32.         case 'w': WriteOnQuit='Y';        break;
  33.         }
  34.     }
  35.     if (Debug) printf("extra args %d\n", optind);
  36.     if (argc < 2) {
  37.         fprintf(stderr, "%s: usage: %s [options] file [ file file]\n",
  38.                 Progname, Progname);
  39.         exit(-1);
  40.     }
  41.     for (;optind < argc; optind++) {
  42.         errno=0;
  43.         edit(argv[optind], start_line);
  44.         *start_line = '\0';
  45.     }
  46.     resetty();
  47.     exit(errno);
  48. }
  49.  
  50. edit(file, sline)
  51. char *file;
  52. char *sline;
  53. {
  54.     struct stat stbuff, *s;
  55.     char c;
  56.     int new=!OK;
  57.     s=(struct stat *) &stbuff;
  58.     clear_stuff();
  59.     noecho();
  60.     One_Char();
  61.     if ((EditFile=fopen(file, "r")) == (FILE *) NULL) {
  62.         if (errno == ENOENT) {
  63.             printf("File %s doesn't exist, create (y/n) ? ",
  64.                 file);
  65.             c=yesno();
  66.             switch(c) {
  67.                 case 'Y':
  68.                     close(creat(file, 0666));
  69.                     if ((EditFile=fopen(file, "r")) == (FILE *) NULL) {
  70.                         /* give up... */
  71.                         fprintf(stderr, "%s: could not create %s\n", Progname, file);
  72.                         return(!OK);
  73.                     }
  74.                     new=OK;
  75.                     break;
  76.                 case 'N':
  77.                 default:
  78.                     return(OK);
  79.                     break;    
  80.             }
  81.         } else {
  82.         fprintf(stderr, "%s: Could not read %s",
  83.             Progname, file);
  84.             perror("");
  85.             return(!OK);
  86.         }
  87.     }
  88.     if (fstat(fileno(EditFile), s) < 0) {
  89.         fprintf(stderr, "%s: fstat(%d) failed",
  90.             Progname, fileno(EditFile));
  91.             perror("");
  92.             return(!OK);
  93.         }
  94.     if (s->st_size > BEGINDOTS)
  95.         Dots = 'Y';
  96.     if (new != OK && s->st_size > 0L)  {
  97.         if (loadem(EditFile, 0L) > 0L) {
  98.             printf("File: \"%s\"   lines=%ld, charicters=%ld\n",
  99.                 file, NumberLines, s->st_size);
  100.             fclose(EditFile);
  101.             command(file, sline);
  102.         }
  103.     } else {
  104.         printf("File: \"%s\" is new, begin entering new text\n", file);
  105.         append(0L);
  106.         command(file, "\0");
  107.     }
  108.     return(OK);
  109. }
  110.