home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd7.lzh / SRC / crypt.c < prev    next >
Text File  |  1990-02-11  |  3KB  |  173 lines

  1. /*
  2.  * Encryption utilites
  3.  * Bradley Williams    
  4.  * {allegra,ihnp4,uiucdcs,ctvax}!convex!williams
  5.  * $Revision: 6.1 $
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <curses.h>
  10.  
  11. #if defined(BSD42) || defined(BSD43)
  12. #include <sys/file.h>
  13. #else
  14. #include <fcntl.h>
  15. #endif
  16.  
  17. #include "sc.h"
  18.  
  19. char        *strcpy();
  20.  
  21. #ifdef SYSV3
  22. void exit();
  23. #endif
  24.  
  25. int         Crypt = 0;
  26.  
  27. creadfile (save, eraseflg)
  28. char *save;
  29. int  eraseflg;
  30. {
  31.     register FILE *f;
  32.     int pipefd[2];
  33.     int fildes;
  34.     int pid;
  35.  
  36.     if (eraseflg && strcmp(save, curfile) && modcheck(" first")) return;
  37.  
  38.     fildes = open (save, O_RDONLY, 0);
  39.     if (fildes < 0)
  40.     {
  41.     error ("Can't read file \"%s\"", save);
  42.     return;
  43.     }
  44.  
  45.     if (eraseflg) erasedb ();
  46.  
  47.     if (pipe(pipefd) < 0) {
  48.     error("Can't make pipe to child");
  49.     return;
  50.     }
  51.  
  52.     deraw();
  53.     if ((pid=fork()) == 0)              /* if child  */
  54.     {
  55.     (void) close (0);          /* close stdin */
  56.     (void) close (1);          /* close stdout */
  57.     (void) close (pipefd[0]);      /* close pipe input */
  58.     (void) dup (fildes);          /* standard in from file */
  59.     (void) dup (pipefd[1]);          /* connect to pipe */
  60.     (void) fprintf (stderr, " ");
  61.     (void) execl ("/bin/sh", "sh", "-c", "crypt", 0);
  62.     exit (-127);
  63.     }
  64.     else                  /* else parent */
  65.     {
  66.     (void) close (fildes);
  67.     (void) close (pipefd[1]);      /* close pipe output */
  68.     f = fdopen (pipefd[0], "r");
  69.     if (f == 0)
  70.     {
  71.         (void) kill (pid, -9);
  72.         error ("Can't fdopen file \"%s\"", save);
  73.         (void) close (pipefd[0]);
  74.         return;
  75.     }
  76.     }
  77.  
  78.     loading++;
  79.     while (fgets(line,sizeof line,f)) {
  80.     linelim = 0;
  81.     if (line[0] != '#') (void) yyparse ();
  82.     }
  83.     --loading;
  84.     (void) fclose (f);
  85.     (void) close (pipefd[0]);
  86.     while (pid != wait(&fildes)) /**/;
  87.     goraw();
  88.     linelim = -1;
  89.     modflg++;
  90.     if (eraseflg) {
  91.     (void) strcpy (curfile, save);
  92.     modflg = 0;
  93.     }
  94.     EvalAll();
  95. }
  96.  
  97. cwritefile (fname, r0, c0, rn, cn)
  98. char *fname;
  99. int r0, c0, rn, cn;
  100. {
  101.     register FILE *f;
  102.     int pipefd[2];
  103.     int fildes;
  104.     int pid;
  105.     char save[1024];
  106.     char *fn;
  107.  
  108.  
  109.     if (*fname == 0) fname = &curfile[0];
  110.  
  111.     fn = fname;
  112.     while (*fn && (*fn == ' '))  /* Skip leading blanks */
  113.     fn++;
  114.  
  115.     if ( *fn == '|' ) {
  116.     error ("Can't have encrypted pipe");
  117.     return(-1);
  118.     }
  119.  
  120.     (void) strcpy(save,fname);
  121.  
  122.     fildes = open (save, O_WRONLY|O_CREAT, 0600);
  123.     if (fildes < 0)
  124.     {
  125.     error ("Can't create file \"%s\"", save);
  126.     return(-1);
  127.     }
  128.  
  129.     if (pipe (pipefd) < 0) {
  130.     error ("Can't make pipe to child\n");
  131.     return(-1);
  132.     }
  133.  
  134.     deraw();
  135.     if ((pid=fork()) == 0)              /* if child  */
  136.     {
  137.     (void) close (0);              /* close stdin */
  138.     (void) close (1);              /* close stdout */
  139.     (void) close (pipefd[1]);          /* close pipe output */
  140.     (void) dup (pipefd[0]);              /* connect to pipe input */
  141.     (void) dup (fildes);              /* standard out to file */
  142.     (void) fprintf (stderr, " ");
  143.     (void) execl ("/bin/sh", "sh", "-c", "crypt", 0);
  144.     exit (-127);
  145.     }
  146.     else                  /* else parent */
  147.     {
  148.     (void) close (fildes);
  149.     (void) close (pipefd[0]);          /* close pipe input */
  150.     f = fdopen (pipefd[1], "w");
  151.     if (f == 0)
  152.     {
  153.         (void) kill (pid, -9);
  154.         error ("Can't fdopen file \"%s\"", save);
  155.         (void) close (pipefd[1]);
  156.         return(-1);
  157.     }
  158.     }
  159.  
  160.     write_fd(f, r0, c0, rn, cn);
  161.  
  162.     (void) fclose (f);
  163.     (void) close (pipefd[1]);
  164.     while (pid != wait(&fildes)) /**/;
  165.     (void) strcpy(curfile,save);
  166.  
  167.     modflg = 0;
  168.     error ("File \"%s\" written", curfile);
  169.     goraw();
  170.     return(0);
  171. }
  172.  
  173.