[BACK]Return to mktemp.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mktemp

Diff for /src/usr.bin/mktemp/mktemp.c between version 1.6 and 1.7

version 1.6, 2001/10/01 17:08:30 version 1.7, 2001/10/11 00:05:55
Line 38 
Line 38 
 #include <unistd.h>  #include <unistd.h>
 #include <err.h>  #include <err.h>
   
 void  __dead void usage __P((void));
 usage()  
 {  
         extern char *__progname;  
   
         (void) fprintf(stderr,  
             "Usage: %s [-dqtu] [-p prefix] template\n", __progname);  
         exit(1);  
 }  
   
 int  int
 main(argc, argv)  main(argc, argv)
         int argc;          int argc;
         char **argv;          char **argv;
 {  {
         int ch, uflag = 0, qflag = 0, tflag = 0, makedir = 0;          int ch, fd, uflag = 0, quiet = 0, tflag = 0, makedir = 0;
         char *cp, *template, *prefix = _PATH_TMP;          char *cp, *template, *tempfile, *prefix = _PATH_TMP;
         size_t plen;          size_t plen;
   
         while ((ch = getopt(argc, argv, "dp:qtu")) != -1)          while ((ch = getopt(argc, argv, "dp:qtu")) != -1)
Line 67 
Line 59 
                         tflag = 1;                          tflag = 1;
                         break;                          break;
                 case 'q':                  case 'q':
                         qflag = 1;                          quiet = 1;
                         break;                          break;
                 case 't':                  case 't':
                         tflag = 1;                          tflag = 1;
Line 79 
Line 71 
                         usage();                          usage();
         }          }
   
         if (argc - optind != 1)          /* If no template specified use a default one (implies -t mode) */
           switch (argc - optind) {
           case 1:
                   template = argv[optind];
                   break;
           case 0:
                   template = "tmp.XXXXXXXXXX";
                   tflag = 1;
                   break;
           default:
                 usage();                  usage();
           }
   
         if (tflag) {          if (tflag) {
                 if (strchr(argv[optind], '/')) {                  if (strchr(template, '/')) {
                         if (qflag)                          if (!quiet)
                                 exit(1);                                  warnx("template must not contain directory separators in -t mode");
                         else                          exit(1);
                                 errx(1, "template must not contain directory separators in -t mode");  
                 }                  }
   
                 cp = getenv("TMPDIR");                  cp = getenv("TMPDIR");
Line 97 
Line 98 
                 while (plen != 0 && prefix[plen - 1] == '/')                  while (plen != 0 && prefix[plen - 1] == '/')
                         plen--;                          plen--;
   
                 template = (char *)malloc(plen + 1 + strlen(argv[optind]) + 1);                  tempfile = (char *)malloc(plen + 1 + strlen(template) + 1);
                 if (template == NULL) {                  if (tempfile == NULL) {
                         if (qflag)                          if (!quiet)
                                 exit(1);                                  warnx("cannot allocate memory");
                         else                          exit(1);
                                 errx(1, "Cannot allocate memory");  
                 }                  }
                 memcpy(template, prefix, plen);                  (void)memcpy(tempfile, prefix, plen);
                 template[plen] = '/';                  tempfile[plen] = '/';
                 strcpy(template + plen + 1, argv[optind]);      /* SAFE */                  (void)strcpy(tempfile + plen + 1, template);    /* SAFE */
         } else {          } else {
                 if ((template = strdup(argv[optind])) == NULL) {                  if ((tempfile = strdup(template)) == NULL) {
                         if (qflag)                          if (!quiet)
                                 exit(1);                                  warnx("cannot allocate memory");
                         else                          exit(1);
                                 errx(1, "Cannot allocate memory");  
                 }                  }
         }          }
   
         if (makedir) {          if (makedir) {
                 if (mkdtemp(template) == NULL) {                  if (mkdtemp(tempfile) == NULL) {
                         if (qflag)                          if (!quiet)
                                 exit(1);                                  warn("cannot make temp dir %s", tempfile);
                         else                          exit(1);
                                 err(1, "Cannot make temp dir %s", template);  
                 }                  }
   
                 if (uflag)                  if (uflag)
                         (void) rmdir(template);                          (void)rmdir(tempfile);
         } else {          } else {
                 if (mkstemp(template) < 0) {                  if ((fd = mkstemp(tempfile)) < 0) {
                         if (qflag)                          if (!quiet)
                                 exit(1);                                  warn("cannot make temp file %s", tempfile);
                         else                          exit(1);
                                 err(1, "Cannot create temp file %s", template);  
                 }                  }
                   (void)close(fd);
   
                 if (uflag)                  if (uflag)
                         (void) unlink(template);                          (void)unlink(tempfile);
         }          }
   
         (void) puts(template);          (void)puts(tempfile);
         free(template);          free(tempfile);
   
         exit(0);          exit(0);
   }
   
   __dead void
   usage()
   {
           extern char *__progname;
   
           (void)fprintf(stderr,
               "Usage: %s [-dqtu] [-p prefix] [template]\n", __progname);
           exit(1);
 }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7