--- safecat-1.9/safecat.c	Tue Apr 10 03:46:07 2001
+++ safecat-1.9.patched/safecat.c	Wed Jan  1 14:59:41 2003
@@ -33,6 +33,7 @@
 int main(int argc, char *argv[]) {
   char *tempdir  = NULL;
   char *destdir  = NULL;
+  char *destfile  = NULL;
   int   outfd    = 0;
   stralloc dstpath = {0};
   stralloc outfile = {0};
@@ -41,14 +42,15 @@
   unsigned int count = 0;
 
   /* Check that we were called with the correct number of arguments. */
-  if(argc != 3) {
-    strerr_die2x(100, "safecat: usage: ","safecat <tempdir> <destdir>");
+  if(argc != 3 && argc != 4) {
+    strerr_die2x(100, "safecat: usage: ","safecat <tempdir> <destdir> [destfile]");
   }
 
   /* Scan the command line arguments, to get the temp directory
      and destination directory names. */
   tempdir = argv[1];
   destdir = argv[2];
+  if (argc==4) { destfile = argv[3]; };
 
   /* Declare a handler for SIGALRM so we can time out. */
   set_handler(SIGALRM, alarm_handler);
@@ -67,7 +69,10 @@
     if(stat(outpath.s,&filestat) == -1 && errno == ENOENT) {
       if (!stralloc_cats(&dstpath, destdir)) die_nomem();
       if (!stralloc_append(&dstpath, "/")) die_nomem();
-      if (!stralloc_cat(&dstpath,&outfile)) die_nomem();
+
+      if (destfile){
+	if (!(stralloc_cats(&dstpath,destfile) && stralloc_0(&dstpath))) die_nomem(); }
+	else { if (!stralloc_cat(&dstpath,&outfile)) die_nomem(); };
 
       if (!stralloc_cats(&tmppath, tempdir)) die_nomem();
       if (!stralloc_append(&tmppath, "/")) die_nomem();
@@ -103,9 +108,14 @@
   }
 
   /* Step 6:  Link the temp file to its final destination. */
-  if(link(tmppath.s,dstpath.s) == -1) {
-    unlink(tmppath.s);
-    strerr_die2sys(111,"safecat: fatal: ","can't link output file: ");
+  if(destfile) {
+ 	  if(rename(tmppath.s,dstpath.s) == -1) {
+	    unlink(tmppath.s);
+	    strerr_die2sys(111,"safecat: fatal: ","can't rename output file: ");
+  	  }
+  } else if(link(tmppath.s,dstpath.s) == -1) { 
+	    unlink(tmppath.s);
+	    strerr_die2sys(111,"safecat: fatal: ","can't link output file: ");
   }
   /* We've succeeded!  Now, no matter what, we return "success" */
 
@@ -113,9 +123,10 @@
   unlink(tmppath.s);
 
   /* Print the name of the file we've created, as a curtesy. */
-  substdio_puts(subfdoutsmall,outfile.s);
-  substdio_puts(subfdoutsmall,"\n");
-  substdio_flush(subfdoutsmall);
+  if (!destfile) {
+	substdio_puts(subfdoutsmall,outfile.s);
+  	substdio_puts(subfdoutsmall,"\n");
+  	substdio_flush(subfdoutsmall); };
   exit(0);
 }
 /* ****************************************************************** */
