diff src/unexcw.c @ 558:ed498ef2108b

[xemacs-hg @ 2001-05-23 09:59:33 by ben] xemacs.mak: call `ver' to get the exact os version and put it in the installation; suggestion from adrian. behavior-defs.el: Add scroll-in-place, jka-compr, efs, fix up some things. pop.c: Remove BROKEN_CYGWIN. etc\sample.init.el: Rewrite to be much more careful about loading features -- now it decays gracefully even in the complete absence of packages. Also avoid doing obnoxious things when loading efs. configure.in: add some support for eventually turning on file coding by default. Fix numerous places where AC_MSG_WARN had quotes around its arg, which is bad. Replace with []. Same for AC_MSG_ERROR. s\cygwin32.h, s\mingw32.h: remove support for way old beta versions of cygwin. don't put -Wno-sign-compare in the system switches; this isn't a system issue. define BROKEN_SIGIO for cygwin to get C-g support. device-msw.c: signal an error rather than crash with an unavailable network printer (from Mike Alexander). event-msw.c: cleanup headers. fix (hopefully) an error with data corruption when sending to a network connection. fileio.c: Fix evil code that attempts to handle the ~user prefix by (a) always assuming we're referencing ourselves and not even verifying the user -- hence any file with a tilde as its first char is invalid! (b) if there wasn't a slash following the filename, the pointer was set *past* the end of file and we started reading from uninitialized memory. Now we simply treat these as files, always. optionally for 21.4 (doc fix): lread.c: cambia de pas_de_lache_ici -- al minimo usa la palabra certa. frame.c: fix warnings. emacs.c, nt.c, ntproc.c, process-nt.c, realpath.c, unexnt.c: rename MAX_PATH to standard PATH_MAX. process-nt.c, realpath.c: cleanup headers. process-unix.c, sysdep.c, systime.h, syswindows.h: kill BROKEN_CYGWIN and support for way old beta versions of cygwin. sysfile.h: use _MAX_PATH (Windows) preferentially for PATH_MAX if defined. include io.h on Cygwin (we need get_osfhandle()). include sys/fcntl.h always, since we were including it in various header files anyway. unexcw.c: fix up style to conform to standard. remove duplicate definition of PERROR. buffer.c: comment change. database.c, debug.h, device-tty.c, dired-msw.c, glyphs-msw.c: header cleanups (remove places that directly include a system header file, because we have our own layer to do this more cleanly and portably); indentation fixes.
author ben
date Wed, 23 May 2001 09:59:48 +0000
parents 666d73d6ac56
children 023b83f4e54b
line wrap: on
line diff
--- a/src/unexcw.c	Tue May 22 06:49:43 2001 +0000
+++ b/src/unexcw.c	Wed May 23 09:59:48 2001 +0000
@@ -26,12 +26,6 @@
 #include <config.h>
 #include "lisp.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-
 #define DONT_ENCAPSULATE /* filenames are external in unex*.c */
 #include "sysfile.h"
 
@@ -48,10 +42,6 @@
 }
 #else
 
-#ifndef MAX_PATH
-#define MAX_PATH 260
-#endif
-
 #ifdef MINGW
 #include <../../include/a.out.h>
 #else
@@ -94,13 +84,12 @@
 PEAOUTHDR f_ohdr;
 SCNHDR f_data, f_bss, f_text, f_nextdata;
 
-#define PERROR(arg) perror(arg);exit(-1) 
-#define CHECK_AOUT_POS(a) \
-if (lseek(a_out, 0, SEEK_CUR) != a) \
-{ \
-  printf("we are at %lx, should be at %lx\n", \
-	 lseek(a_out, 0, SEEK_CUR), a); \
-  exit(-1); \
+#define CHECK_AOUT_POS(a)			\
+if (lseek(a_out, 0, SEEK_CUR) != a)		\
+{						\
+  printf("we are at %lx, should be at %lx\n",	\
+	 lseek(a_out, 0, SEEK_CUR), a);		\
+  exit(-1);					\
 }
 
 /* Dump out .data and .bss sections into a new executable.  */
@@ -110,7 +99,7 @@
 {
   /* ugly nt hack - should be in lisp */
   int a_new, a_out = -1;
-  char new_name[MAX_PATH], a_name[MAX_PATH];
+  char new_name[PATH_MAX], a_name[PATH_MAX];
   char *ptr;
   
   /* Make sure that the input and output filenames have the
@@ -129,15 +118,11 @@
   /* round_heap (get_allocation_unit ()); */
 
   if (a_name && (a_out = open (a_name, O_RDONLY | OPEN_BINARY)) < 0)
-    {
-      PERROR (a_name);
-    }
+    PERROR (a_name);
 
   if ((a_new = open (new_name, O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY,
 		     0755)) < 0)
-    {
-      PERROR (new_name);
-    }
+    PERROR (new_name);
 
   /* Get the interesting section info, like start and size of .bss...  */
   get_section_info (a_out, a_name);
@@ -150,54 +135,41 @@
 }
 
 /* Flip through the executable and cache the info necessary for dumping.  */
-static void get_section_info (int a_out, char* a_name)
+static void
+get_section_info (int a_out, char* a_name)
 {
   extern char my_ebss[];
   /* From lastfile.c  */
   extern char my_edata[];
 
   if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
-    {
-      PERROR (a_name);
-    }
+    PERROR (a_name);
 
   if (f_hdr.e_magic != DOSMAGIC) 
-    {
-      PERROR("unknown exe header");
-    }
+    PERROR("unknown exe header");
 
   /* Check the NT header signature ...  */
   if (f_hdr.nt_signature != NT_SIGNATURE) 
-    {
-      PERROR("invalid nt header");
-    }
+    PERROR("invalid nt header");
 
   /* Flip through the sections for .data and .bss ...  */
   if (f_hdr.f_opthdr > 0)
     {
       if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ)
-	{
-	  PERROR (a_name);
-	}
+	PERROR (a_name);
     }
   /* Loop through .data & .bss section headers, copying them in.
      With newer lds these are reversed so we have to cope with both */
   lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0);
 
   if (read (a_out, &f_text, sizeof (f_text)) != sizeof (f_text)
-      ||
-      strcmp (f_text.s_name, ".text"))
-    {
-      PERROR ("no .text section");
-    }
+      || strcmp (f_text.s_name, ".text"))
+    PERROR ("no .text section");
 
   /* The .bss section.  */
   if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
-      ||
-      (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data")))
-    {
-      PERROR ("no .bss / .data section");
-    }
+      || (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data")))
+    PERROR ("no .bss / .data section");
 
   /* check for reversed .bss and .data */
   if (!strcmp(f_bss.s_name, ".data"))
@@ -211,20 +183,14 @@
   if (!sections_reversed)
     {
       if (read (a_out, &f_data, sizeof (f_data)) != sizeof (f_data)
-	  ||
-	  strcmp (f_data.s_name, ".data"))
-	{
-	  PERROR ("no .data section");
-	}
+	  || strcmp (f_data.s_name, ".data"))
+	PERROR ("no .data section");
     }
   else
     {
       if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
-	  ||
-	  strcmp (f_bss.s_name, ".bss"))
-	{
-	  PERROR ("no .bss section");
-	}
+	  || strcmp (f_bss.s_name, ".bss"))
+	PERROR ("no .bss section");
     }
   
   bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr);
@@ -247,9 +213,7 @@
   if (read (a_out, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata)
       &&
       strcmp (&f_nextdata.s_name[2], "data"))
-    {
-      PERROR ("no other data section");
-    }
+    PERROR ("no other data section");
 }
 
 /* The dump routines.  */
@@ -259,30 +223,30 @@
 {
   long size=0;
   unsigned long new_data_size, new_bss_size, 
-    bss_padding, file_sz_change, data_padding=0,
-    f_data_s_vaddr = f_data.s_vaddr,
-    f_data_s_scnptr = f_data.s_scnptr,
-    f_bss_s_vaddr = f_bss.s_vaddr, 
-    f_nextdata_s_scnptr = f_nextdata.s_scnptr;
+  bss_padding, file_sz_change, data_padding=0,
+  f_data_s_vaddr = f_data.s_vaddr,
+  f_data_s_scnptr = f_data.s_scnptr,
+  f_bss_s_vaddr = f_bss.s_vaddr, 
+  f_nextdata_s_scnptr = f_nextdata.s_scnptr;
 
   int i;
   void* empty_space;
   extern int static_heap_dumped;
   SCNHDR section;
   /* calculate new sizes:
-
+     
      f_ohdr.dsize is the total initialized data size on disk which is
      f_data.s_size + f_idata.s_size.
-
+     
      f_ohdr.data_start is the base addres of all data and so should
      not be changed.
      
      *.s_vaddr is the virtual address of the start of the section
      *normalized from f_ohdr.ImageBase.
-
+     
      *.s_paddr appears to be the number of bytes in the section
      *actually used (whereas *.s_size is aligned).
-
+     
      bsize is now 0 since subsumed into .data
      dsize is dsize + (f_data.s_vaddr - f_bss.s_vaddr)
      f_data.s_vaddr is f_bss.s_vaddr
@@ -302,24 +266,18 @@
     }
 
   if ((new_bss_size - bss_size) < BSS_PAD_SIZE)
-    { 
-      PERROR (".bss free space too small");
-    }
+    PERROR (".bss free space too small");
 
   file_sz_change=(new_bss_size + data_padding) - BSS_PAD_SIZE;
   new_data_size=f_ohdr.dsize + file_sz_change;
 
   if (!sections_reversed)
-    {
-      f_data.s_vaddr = f_bss.s_vaddr;
-    }
+    f_data.s_vaddr = f_bss.s_vaddr;
   f_data.s_paddr += file_sz_change;
 #if 0 
   if (f_data.s_size + f_nextdata.s_size != f_ohdr.dsize)
-    {
-      printf("section size doesn't tally with dsize %lx != %lx\n", 
-	     f_data.s_size + f_nextdata.s_size, f_ohdr.dsize);
-    }
+    printf("section size doesn't tally with dsize %lx != %lx\n", 
+	   f_data.s_size + f_nextdata.s_size, f_ohdr.dsize);
 #endif
   f_data.s_size += file_sz_change;
   lseek (a_new, 0, SEEK_SET);
@@ -331,30 +289,22 @@
 
   printf("writing file header\n");
   if (write(a_new, &f_hdr, sizeof(f_hdr)) != sizeof(f_hdr))
-    {
-      PERROR("failed to write file header");
-    }
+    PERROR("failed to write file header");
   /* write optional header fixing dsize & bsize*/
   printf("writing optional header\n");
   printf("new data size is %lx, >= %lx\n", new_data_size,
 	 f_ohdr.dsize + f_ohdr.bsize);
   if (new_data_size < f_ohdr.dsize + f_ohdr.bsize )
-    {
-      printf("warning: new data size is < approx\n");
-    }
+    printf("warning: new data size is < approx\n");
   f_ohdr.dsize=new_data_size;
   f_ohdr.bsize=BSS_PAD_SIZE;
   if (write(a_new, &f_ohdr, sizeof(f_ohdr)) != sizeof(f_ohdr))
-    {
-      PERROR("failed to write optional header");
-    }
+    PERROR("failed to write optional header");
   /* write text as is */
   printf("writing text header (unchanged)\n");
 
   if (write(a_new, &f_text, sizeof(f_text)) != sizeof(f_text))
-    {
-      PERROR("failed to write text header");
-    }
+    PERROR("failed to write text header");
 #ifndef NO_DEBUG
   /* Write small bss section. */
   if (!sections_reversed)
@@ -363,18 +313,14 @@
       f_bss.s_paddr = BSS_PAD_SIZE;
       f_bss.s_vaddr = f_data.s_vaddr - BSS_PAD_SIZE;
       if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
-	{
-	  PERROR("failed to write bss header");
-	}
+	PERROR("failed to write bss header");
     }
 #endif
   /* write new data header */
   printf("writing .data header\n");
 
   if (write(a_new, &f_data, sizeof(f_data)) != sizeof(f_data))
-    {
-      PERROR("failed to write data header");
-    }
+    PERROR("failed to write data header");
 #ifndef NO_DEBUG
   /* Write small bss section. */
   if (sections_reversed)
@@ -383,9 +329,7 @@
       f_bss.s_paddr = BSS_PAD_SIZE;
       f_bss.s_vaddr = f_nextdata.s_vaddr - BSS_PAD_SIZE;
       if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
-	{
-	  PERROR("failed to write bss header");
-	}
+	PERROR("failed to write bss header");
     }
 #endif
   printf("writing following data header\n");
@@ -393,34 +337,26 @@
   if (f_nextdata.s_lnnoptr != 0) f_nextdata.s_lnnoptr += file_sz_change;
   if (f_nextdata.s_relptr != 0) f_nextdata.s_relptr += file_sz_change;
   if (write(a_new, &f_nextdata, sizeof(f_nextdata)) != sizeof(f_nextdata))
-    {
-      PERROR("failed to write nextdata header");
-    }
+    PERROR("failed to write nextdata header");
 
   /* copy other section headers adjusting the file offset */
   for (i=0; i<(f_hdr.f_nscns-3); i++)
     {
       if (read (a_out, &section, sizeof (section)) != sizeof (section))
-	{
-	  PERROR ("no .data section");
-	}
+	PERROR ("no .data section");
       
       section.s_scnptr += file_sz_change;
       if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change;
       if (section.s_relptr != 0) section.s_relptr += file_sz_change;
 
       if (write(a_new, &section, sizeof(section)) != sizeof(section))
-	{
-	  PERROR("failed to write data header");
-	}
+	PERROR("failed to write data header");
     }
 #ifdef NO_DEBUG
   /* dump bss to maintain offsets */
   memset(&f_bss, 0, sizeof(f_bss));
   if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
-    {
-      PERROR("failed to write bss header");
-    }
+    PERROR("failed to write bss header");
 #endif
   size=lseek(a_new, 0, SEEK_CUR);
   CHECK_AOUT_POS(size);
@@ -446,16 +382,12 @@
       /* pad, needs to be zero */
       bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE;
       if (bss_padding < 0)
-	{
-	  PERROR("padded .bss too small");
-	}
+	PERROR("padded .bss too small");
       printf ("padding .bss ... %lx bytes\n", bss_padding);
       empty_space = malloc(bss_padding);
       memset(empty_space, 0, bss_padding);
       if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
-	{
-	  PERROR("failed to write bss section");
-	}
+	PERROR("failed to write bss section");
       free(empty_space);
     }
 
@@ -464,9 +396,7 @@
   /* Get a pointer to the raw data in our address space.  */
   printf ("dumping .data section... %lx bytes\n", data_size);
   if (write(a_new, data_start_va, data_size) != (int)data_size)
-    {
-      PERROR("failed to write data section");
-    }
+    PERROR("failed to write data section");
   /* were going to use free again ... */
   static_heap_dumped = 0;
   
@@ -487,23 +417,17 @@
       /* dump bss + padding between sections */
       printf ("dumping .bss into executable... %lx bytes\n", bss_size);
       if (write(a_new, bss_start, bss_size) != (int)bss_size)
-	{
-	  PERROR("failed to write bss section");
-	}
+	PERROR("failed to write bss section");
       
       /* pad, needs to be zero */
       bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE;
       if (bss_padding < 0)
-	{
-	  PERROR("padded .bss too small");
-	}
+	PERROR("padded .bss too small");
       printf ("padding .bss ... %lx bytes\n", bss_padding);
       empty_space = malloc(bss_padding);
       memset(empty_space, 0, bss_padding);
       if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
-	{
-	  PERROR("failed to write bss section");
-	}
+	PERROR("failed to write bss section");
       free(empty_space);
       if (lseek(a_new, 0, SEEK_CUR) != f_nextdata.s_scnptr)
 	{
@@ -542,9 +466,7 @@
     {
       n = size > sizeof (page) ? sizeof (page) : size;
       if (read (a_out, page, n) != n || write (a_new, page, n) != n)
-	{
-	  PERROR ("dump_out()");
-	}
+	PERROR ("dump_out()");
     }
 }
 
@@ -555,15 +477,11 @@
 
   cpos = lseek(a_new, 0, SEEK_CUR);
   if (va < bss_start || va > bss_start + f_data.s_size)
-    {
-      PERROR("address not in data space\n");
-    }
+    PERROR("address not in data space\n");
   lseek(a_new, f_data.s_scnptr + ((unsigned long)va - 
 				  (unsigned long)bss_start), SEEK_SET);
   if (write(a_new, newval, sizeof(int)) != (int)sizeof(int))
-    {
-      PERROR("failed to write int value");
-    }
+    PERROR("failed to write int value");
   lseek(a_new, cpos, SEEK_SET);
 }
 #endif