+
+/* Create a new a.out file, same as old but with current data space */
+void
+unexec (const char *new_name, /* name of the new a.out file to be created */
+ const char *old_name) /* name of the old a.out file */
+{
+ int old, new;
+ int old_size, new_size;
+ struct header hdr;
+ struct som_exec_auxhdr auxhdr;
+ long i;
+
+ /* For the greatest flexibility, should create a temporary file in
+ the same directory as the new file. When everything is complete,
+ rename the temp file to the new name.
+ This way, a program could update its own a.out file even while
+ it is still executing. If problems occur, everything is still
+ intact. NOT implemented. */
+
+ /* Open the input and output a.out files */
+ old = emacs_open (old_name, O_RDONLY, 0);
+ if (old < 0)
+ { perror (old_name); exit (1); }
+ new = emacs_open (new_name, O_CREAT | O_RDWR | O_TRUNC, 0777);
+ if (new < 0)
+ { perror (new_name); exit (1); }
+
+ /* Read the old headers */
+ read_header (old, &hdr, &auxhdr);
+
+ brk_on_dump = (long) sbrk (0);
+
+ /* Decide how large the new and old data areas are */
+ old_size = auxhdr.exec_dsize;
+ /* I suspect these two statements are separate
+ to avoid a compiler bug in hpux version 8. */
+ i = (long) sbrk (0);
+ new_size = i - auxhdr.exec_dmem;
+
+ /* Copy the old file to the new, up to the data space */
+ lseek (old, 0, 0);
+ copy_file (old, new, auxhdr.exec_dfile);
+
+ /* Skip the old data segment and write a new one */
+ lseek (old, old_size, 1);
+ save_data_space (new, &hdr, &auxhdr, new_size);
+
+ /* Copy the rest of the file */
+ copy_rest (old, new);
+
+ /* Update file pointers since we probably changed size of data area */
+ update_file_ptrs (new, &hdr, &auxhdr, auxhdr.exec_dfile, new_size-old_size);
+
+ /* Save the modified header */
+ write_header (new, &hdr, &auxhdr);
+
+ /* Close the binary file */
+ emacs_close (old);
+ emacs_close (new);
+}