IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 25175


Ignore:
Timestamp:
Aug 22, 2009, 10:26:57 AM (17 years ago)
Author:
eugene
Message:

update to match full API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090715/Ohana/src/tools/src/mktemp.c

    r25104 r25175  
    11# include <stdio.h>
    22# include <stdlib.h>
     3# include <string.h>
     4
     5# define FALSE 0
     6# define TRUE 1
    37
    48int main (int argc, char **argv) {
    59
    6   char *prefix;
     10  int i, j, Ntotal;
     11  char *tmpdir, *template, deftemplate[32], *prefix, defprefix[32], *filename;
     12  int make_directory, fail_silently, unsafe_mode, full_path;
    713
     14  tmpdir = NULL;
    815  prefix = NULL;
     16  template = NULL;
     17  filename = NULL;
    918
    10   for (i = 0; i < argc; i++) {
     19  make_directory = FALSE;
     20  fail_silently = FALSE;
     21  unsafe_mode = FALSE;
     22  full_path = TRUE;
     23
     24  for (i = 1; i < argc; i++) {
     25    // -options must be first
    1126    if (argv[i][0] == '-') {
    1227      if (!strcmp(argv[i], "-V")) {
    13         fprintf (stdout, "mktemp version Ohana $Revision: $");
     28        fprintf (stdout, "mktemp version Ohana $Revision: $\n");
    1429        exit (0);
    1530      }
    1631      if (!strcmp(argv[i], "-p")) {
    17         if (argc <= i - 1) usage();
     32        if (argc <= i + 1) usage();
    1833        i++;
    1934        prefix = argv[i];
     35        full_path = FALSE;
    2036        continue;
    2137      }
    22       for (j = 0; j < strlen(argv[i]) - 1; j++) {
    23         if (!argv[i][j] == 'q') {
     38      for (j = 1; j < strlen(argv[i]); j++) {
     39        if (argv[i][j] == 'q') {
     40          fail_silently = TRUE;
     41          continue;
    2442        }
     43        if (argv[i][j] == 't') {
     44          full_path = FALSE;
     45          continue;
     46        }
     47        if (argv[i][j] == 'd') {
     48          // make directory
     49          make_directory = TRUE;
     50          continue;
     51        }
     52        if (argv[i][j] == 'u') {
     53          unsafe_mode = TRUE;
     54          continue;
     55        }
     56        // unknown option
     57        usage();
    2558      }
    26 
    27 
    28   if (argc != 2) {
    29     fprintf (stderr, "USAGE: %s (template)\n", argv[0]);
    30     exit (1);
     59      continue;
     60    }
     61    // report an error if too many arguments are given
     62    if (template) usage();
     63    template = argv[i];
    3164  }
    3265
    33   if (mkstemp (argv[1]) == -1) exit (1);
     66  if (!full_path) {
     67    // prefix = TMPDIR ? TMPDIR : (prefix ? prefix : /tmp)
     68    tmpdir = getenv("TMPDIR");
     69    if (tmpdir) {
     70      prefix = tmpdir;
     71    }
     72    if (!prefix) {
     73      strcpy (defprefix, "/tmp");
     74      prefix = defprefix;
     75    }
     76    if (template && strchr(template, '/')) usage();
     77  }
    3478
    35   fprintf (stdout, "%s\n", argv[1]);
     79  if (!template) {
     80    if (full_path) {
     81      strcpy (deftemplate, "/tmp/tmp.XXXXXXXXXX");
     82    } else {
     83      strcpy (deftemplate, "tmp.XXXXXXXXXX");
     84    }
     85    template = deftemplate;
     86  }
     87
     88  // filename = full_path ? prefix/template : template;
     89  if (!full_path) {
     90    Ntotal = strlen(prefix) + strlen(template) + 2;
     91    filename = (char *) malloc (Ntotal);
     92    snprintf (filename, Ntotal, "%s/%s", prefix, template);
     93    template = filename;
     94  }
     95
     96  if (make_directory) {
     97    if (mkdtemp (template) == -1) {
     98      if (!fail_silently) fprintf (stderr, "failed to make temp file from %s\n", template);
     99      exit (1);
     100    }
     101  } else {
     102    if (mkstemp (template) == -1) {
     103      if (!fail_silently) fprintf (stderr, "failed to make temp file from %s\n", template);
     104      exit (1);
     105    }
     106  }
     107
     108  fprintf (stdout, "%s\n", template);
    36109
    37110  exit (0);
    38111}
    39112 
    40       usage() {
    41         fprintf (stderr, "Usage: mktemp [-V] | [-dqtu] [-p prefix] [template]\n");
    42         exit (1);
    43       }
     113usage() {
     114  fprintf (stderr, "Usage: mktemp [-V] | [-dqtu] [-p prefix] [template]\n");
     115  exit (1);
     116}
Note: See TracChangeset for help on using the changeset viewer.