void
unexec(
       char *outfile,
       char *infile
       )
{
	int infd;
	int outfd;
	char tmpbuf[L_tmpnam];
	char *tmpfile;

	infd = open(infile, O_RDONLY, 0);
	if (infd < 0) {
	  	fatal_unexec("cannot open input file `%s'", infile);
		exit(1);
	}
	
	tmpnam(tmpbuf);
	tmpfile = rindex(tmpbuf, '/');
	if (tmpfile == NULL) {
		tmpfile = tmpbuf;
	} else {
		tmpfile++;
	}
	outfd = open(tmpfile, O_WRONLY|O_TRUNC|O_CREAT, 0755);
	if (outfd < 0) {
		close(infd);
		fatal_unexec("cannot open tmp file `%s'", tmpfile);
		exit(1);
	}
	if (!unexec_doit(infd, outfd)) {
		close(infd);
		close(outfd);
		unlink(tmpfile);
		exit(1);
	}
	close(infd);
	close(outfd);
	if (rename(tmpfile, outfile) < 0) {
		unlink(tmpfile);
		fatal_unexec("cannot rename `%s' to `%s'", tmpfile, outfile);
		exit(1);
	}
}
Beispiel #2
0
void unexec(char *outfile,char *infile)
   {
   char tmpfile[MAXPATHLEN];
   int infd,outfd;
   
   if ((infd=open(infile, O_RDONLY, 0))<0)
      fatal_unexec("cannot open input file `%s'", infile);

   strcpy(tmpfile,outfile);
   strcat(tmpfile,"-temp");
   
   if ((outfd=open(tmpfile, O_RDWR|O_TRUNC|O_CREAT, 0755))<0)
      fatal_unexec("cannot open temporary output file `%s'",tmpfile);

   unexec_doit(infd,outfd);

   close(infd);
   close(outfd);
   if (rename(tmpfile, outfile)<0)
      {
      unlink(tmpfile);
      fatal_unexec("cannot rename `%s' to `%s'", tmpfile, outfile);
      }  
   }