home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.000 / gcl-1 / gcl-1.0 / c / unixsave.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  3.1 KB  |  178 lines

  1. /*
  2.  Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
  3.  
  4. This file is part of GNU Common Lisp, herein referred to as GCL
  5.  
  6. GCL is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GCL is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public 
  14. License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public License 
  17. along with GCL; see the file COPYING.  If not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. */
  21.  
  22. /*
  23.     unixsave.c
  24. */
  25.  
  26. #define IN_UNIXSAVE
  27. #ifndef FIRSTWORD
  28. #include "include.h"
  29. #endif
  30.  
  31. #ifdef UNIXSAVE
  32. #include UNIXSAVE
  33. #else
  34.  
  35. #ifdef HAVE_FCNTL
  36. #include <fcntl.h>
  37. #else
  38. #include <sys/file.h>
  39. #endif
  40.  
  41. #ifdef HAVE_AOUT
  42. #undef BSD
  43. #undef ATT
  44. #define BSD
  45. #endif
  46.  
  47.  
  48.  
  49. #ifdef BSD
  50. #include HAVE_AOUT
  51. #endif
  52.  
  53. #ifdef DOS
  54. void 
  55. binary_file_mode()
  56. {_fmode = O_BINARY;}
  57. #endif
  58.  
  59.  
  60. #ifdef ATT
  61. #include <filehdr.h>
  62. #include <aouthdr.h>
  63. #include <scnhdr.h>
  64. #endif
  65.  
  66. #ifdef E15
  67. #include <a.out.h>
  68. extern    etext;
  69. #endif
  70.  
  71.  
  72. filecpy(to, from, n)
  73. FILE *to, *from;
  74. register int n;
  75. {
  76.     char buffer[BUFSIZ];
  77.  
  78.     for (;;)
  79.         if (n > BUFSIZ) {
  80.             fread(buffer, BUFSIZ, 1, from);
  81.             fwrite(buffer, BUFSIZ, 1, to);
  82.             n -= BUFSIZ;
  83.         } else if (n > 0) {
  84.             fread(buffer, 1, n, from);
  85.             fwrite(buffer, 1, n, to);
  86.             break;
  87.         } else
  88.             break;
  89. }
  90.  
  91.  
  92. memory_save(original_file, save_file)
  93. char *original_file, *save_file;
  94. {    MEM_SAVE_LOCALS;
  95.     char *data_begin, *data_end;
  96.     int original_data;
  97.     FILE *original, *save;
  98.     register int n;
  99.     register char *p;
  100.     extern char *sbrk();
  101.     extern char stdin_buf[BUFSIZ], stdout_buf[BUFSIZ];
  102.  
  103.     original = freopen(original_file,"r",stdin);
  104. /*    fclose(stdin); 
  105.     original = fopen(original_file, "r");
  106. */    
  107.  
  108.     if (stdin != original || original->_file != 0) {
  109.         fprintf(stderr, "Can't open the original file.\n");
  110.         exit(1);
  111.     }
  112.     setbuf(original, stdin_buf);
  113.     fclose(stdout);
  114.     unlink(save_file);
  115.     n = open(save_file, O_CREAT|O_WRONLY, 0777);
  116.     if (n != 1 || (save = fdopen(n, "w")) != stdout) {
  117.         fprintf(stderr, "Can't open the save file.\n");
  118.         exit(1);
  119.     }
  120.     setbuf(save, stdout_buf);
  121.  
  122.     READ_HEADER;
  123.     FILECPY_HEADER;
  124.  
  125.     for (n = header.a_data, p = data_begin;  ;  n -= BUFSIZ, p += BUFSIZ)
  126.         if (n > BUFSIZ)
  127.             fwrite(p, BUFSIZ, 1, save);
  128.         else if (n > 0) {
  129.             fwrite(p, 1, n, save);
  130.             break;
  131.         } else
  132.             break;
  133.  
  134.     fseek(original, original_data, 1);
  135.  
  136.     COPY_TO_SAVE;
  137.  
  138.     fclose(original);
  139.     fclose(save);
  140. }
  141.  
  142. Lsave()
  143. {
  144.     char filename[256];
  145.  
  146.     check_arg(1);
  147.     check_type_or_pathname_string_symbol_stream(&vs_base[0]);
  148.     coerce_to_filename(vs_base[0], filename);
  149.  
  150.     _cleanup();
  151. /*
  152.     {
  153.         FILE *p;
  154.         int nfile;
  155.  
  156.  
  157.         nfile = NUMBER_OPEN_FILES;
  158.  
  159.         for (p = &_iob[3];  p < &_iob[nfile];  p++)
  160.             fclose(p);
  161.     }
  162. */
  163.     memory_save(kcl_self, filename);
  164. /*
  165.     _exit(0);
  166. */
  167.     exit(0);
  168.     /*  no return  */
  169. }
  170.  
  171. #endif /* UNIXSAVE include */
  172.  
  173. init_unixsave()
  174. {
  175.     make_function("SAVE", Lsave);
  176. }
  177.  
  178.