Changeset 25175
- Timestamp:
- Aug 22, 2009, 10:26:57 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/20090715/Ohana/src/tools/src/mktemp.c
r25104 r25175 1 1 # include <stdio.h> 2 2 # include <stdlib.h> 3 # include <string.h> 4 5 # define FALSE 0 6 # define TRUE 1 3 7 4 8 int main (int argc, char **argv) { 5 9 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; 7 13 14 tmpdir = NULL; 8 15 prefix = NULL; 16 template = NULL; 17 filename = NULL; 9 18 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 11 26 if (argv[i][0] == '-') { 12 27 if (!strcmp(argv[i], "-V")) { 13 fprintf (stdout, "mktemp version Ohana $Revision: $ ");28 fprintf (stdout, "mktemp version Ohana $Revision: $\n"); 14 29 exit (0); 15 30 } 16 31 if (!strcmp(argv[i], "-p")) { 17 if (argc <= i -1) usage();32 if (argc <= i + 1) usage(); 18 33 i++; 19 34 prefix = argv[i]; 35 full_path = FALSE; 20 36 continue; 21 37 } 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; 24 42 } 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(); 25 58 } 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]; 31 64 } 32 65 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 } 34 78 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); 36 109 37 110 exit (0); 38 111 } 39 112 40 usage() {41 fprintf (stderr, "Usage: mktemp [-V] | [-dqtu] [-p prefix] [template]\n");42 exit (1);43 }113 usage() { 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.
