Index: /branches/eam_branches/20090715/Ohana/src/tools/src/mktemp.c
===================================================================
--- /branches/eam_branches/20090715/Ohana/src/tools/src/mktemp.c	(revision 25174)
+++ /branches/eam_branches/20090715/Ohana/src/tools/src/mktemp.c	(revision 25175)
@@ -1,43 +1,116 @@
 # include <stdio.h>
 # include <stdlib.h>
+# include <string.h>
+
+# define FALSE 0
+# define TRUE 1
 
 int main (int argc, char **argv) {
 
-  char *prefix;
+  int i, j, Ntotal;
+  char *tmpdir, *template, deftemplate[32], *prefix, defprefix[32], *filename;
+  int make_directory, fail_silently, unsafe_mode, full_path;
 
+  tmpdir = NULL;
   prefix = NULL;
+  template = NULL;
+  filename = NULL;
 
-  for (i = 0; i < argc; i++) {
+  make_directory = FALSE;
+  fail_silently = FALSE;
+  unsafe_mode = FALSE;
+  full_path = TRUE;
+
+  for (i = 1; i < argc; i++) {
+    // -options must be first
     if (argv[i][0] == '-') {
       if (!strcmp(argv[i], "-V")) {
-	fprintf (stdout, "mktemp version Ohana $Revision: $");
+	fprintf (stdout, "mktemp version Ohana $Revision: $\n");
 	exit (0);
       }
       if (!strcmp(argv[i], "-p")) {
-	if (argc <= i - 1) usage();
+	if (argc <= i + 1) usage();
 	i++;
 	prefix = argv[i];
+	full_path = FALSE;
 	continue;
       }
-      for (j = 0; j < strlen(argv[i]) - 1; j++) {
-	if (!argv[i][j] == 'q') {
+      for (j = 1; j < strlen(argv[i]); j++) {
+	if (argv[i][j] == 'q') {
+	  fail_silently = TRUE;
+	  continue;
 	}
+	if (argv[i][j] == 't') {
+	  full_path = FALSE;
+	  continue;
+	}
+	if (argv[i][j] == 'd') {
+	  // make directory
+	  make_directory = TRUE;
+	  continue;
+	}
+	if (argv[i][j] == 'u') {
+	  unsafe_mode = TRUE;
+	  continue;
+	}
+	// unknown option
+	usage();
       }
-
-
-  if (argc != 2) {
-    fprintf (stderr, "USAGE: %s (template)\n", argv[0]);
-    exit (1);
+      continue;
+    }
+    // report an error if too many arguments are given
+    if (template) usage();
+    template = argv[i];
   }
 
-  if (mkstemp (argv[1]) == -1) exit (1);
+  if (!full_path) {
+    // prefix = TMPDIR ? TMPDIR : (prefix ? prefix : /tmp)
+    tmpdir = getenv("TMPDIR");
+    if (tmpdir) {
+      prefix = tmpdir;
+    }
+    if (!prefix) {
+      strcpy (defprefix, "/tmp");
+      prefix = defprefix;
+    }
+    if (template && strchr(template, '/')) usage();
+  }
 
-  fprintf (stdout, "%s\n", argv[1]);
+  if (!template) {
+    if (full_path) {
+      strcpy (deftemplate, "/tmp/tmp.XXXXXXXXXX");
+    } else {
+      strcpy (deftemplate, "tmp.XXXXXXXXXX");
+    }
+    template = deftemplate;
+  }
+
+  // filename = full_path ? prefix/template : template;
+  if (!full_path) {
+    Ntotal = strlen(prefix) + strlen(template) + 2;
+    filename = (char *) malloc (Ntotal);
+    snprintf (filename, Ntotal, "%s/%s", prefix, template);
+    template = filename;
+  }
+
+  if (make_directory) {
+    if (mkdtemp (template) == -1) {
+      if (!fail_silently) fprintf (stderr, "failed to make temp file from %s\n", template);
+      exit (1);
+    }
+  } else {
+    if (mkstemp (template) == -1) {
+      if (!fail_silently) fprintf (stderr, "failed to make temp file from %s\n", template);
+      exit (1);
+    }
+  }
+
+  fprintf (stdout, "%s\n", template);
 
   exit (0);
 }
  
-      usage() {
-	fprintf (stderr, "Usage: mktemp [-V] | [-dqtu] [-p prefix] [template]\n");
-	exit (1);
-      }
+usage() {
+  fprintf (stderr, "Usage: mktemp [-V] | [-dqtu] [-p prefix] [template]\n");
+  exit (1);
+}
