mktemp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini mktemp implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Daniel Jacobowitz
  6. * Written by Daniel Jacobowitz <dan@debian.org>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. /* Coreutils 6.12 man page says:
  11. * mktemp [OPTION]... [TEMPLATE]
  12. * Create a temporary file or directory, safely, and print its name. If
  13. * TEMPLATE is not specified, use tmp.XXXXXXXXXX.
  14. * -d, --directory
  15. * create a directory, not a file
  16. * -q, --quiet
  17. * suppress diagnostics about file/dir-creation failure
  18. * -u, --dry-run
  19. * do not create anything; merely print a name (unsafe)
  20. * --tmpdir[=DIR]
  21. * interpret TEMPLATE relative to DIR. If DIR is not specified,
  22. * use $TMPDIR if set, else /tmp. With this option, TEMPLATE must
  23. * not be an absolute name. Unlike with -t, TEMPLATE may contain
  24. * slashes, but even here, mktemp still creates only the final com-
  25. * ponent.
  26. * -p DIR use DIR as a prefix; implies -t [deprecated]
  27. * -t interpret TEMPLATE as a single file name component, relative to
  28. * a directory: $TMPDIR, if set; else the directory specified via
  29. * -p; else /tmp [deprecated]
  30. */
  31. //config:config MKTEMP
  32. //config: bool "mktemp (4.2 kb)"
  33. //config: default y
  34. //config: help
  35. //config: mktemp is used to create unique temporary files
  36. //applet:IF_MKTEMP(APPLET_NOEXEC(mktemp, mktemp, BB_DIR_BIN, BB_SUID_DROP, mktemp))
  37. //kbuild:lib-$(CONFIG_MKTEMP) += mktemp.o
  38. //usage:#define mktemp_trivial_usage
  39. //usage: "[-dt] [-p DIR] [TEMPLATE]"
  40. //usage:#define mktemp_full_usage "\n\n"
  41. //usage: "Create a temporary file with name based on TEMPLATE and print its name.\n"
  42. //usage: "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n"
  43. //usage: "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n"
  44. //usage: "\n -d Make directory, not file"
  45. //usage: "\n -q Fail silently on errors"
  46. //usage: "\n -t Prepend base directory name to TEMPLATE"
  47. //usage: "\n -p DIR Use DIR as a base directory (implies -t)"
  48. //usage: "\n -u Do not create anything; print a name"
  49. //usage: "\n"
  50. //usage: "\nBase directory is: -p DIR, else $TMPDIR, else /tmp"
  51. //usage:
  52. //usage:#define mktemp_example_usage
  53. //usage: "$ mktemp /tmp/temp.XXXXXX\n"
  54. //usage: "/tmp/temp.mWiLjM\n"
  55. //usage: "$ ls -la /tmp/temp.mWiLjM\n"
  56. //usage: "-rw------- 1 andersen andersen 0 Apr 25 17:10 /tmp/temp.mWiLjM\n"
  57. #include "libbb.h"
  58. int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  59. int mktemp_main(int argc UNUSED_PARAM, char **argv)
  60. {
  61. const char *path;
  62. char *chp;
  63. unsigned opts;
  64. enum {
  65. OPT_d = 1 << 0,
  66. OPT_q = 1 << 1,
  67. OPT_t = 1 << 2,
  68. OPT_p = 1 << 3,
  69. OPT_u = 1 << 4,
  70. OPT_tmpdir = (1 << 5) * ENABLE_LONG_OPTS,
  71. };
  72. path = getenv("TMPDIR");
  73. if (!path || path[0] == '\0')
  74. path = "/tmp";
  75. #if ENABLE_LONG_OPTS
  76. opts = getopt32long(argv, "^"
  77. "dqtp:u"
  78. "\0"
  79. "?1" /* 1 arg max */,
  80. "directory\0" No_argument "d"
  81. "quiet\0" No_argument "q"
  82. "dry-run\0" No_argument "u"
  83. "tmpdir\0" Optional_argument "\xff"
  84. , &path, &path
  85. );
  86. #else
  87. opts = getopt32(argv, "^" "dqtp:u" "\0" "?1"/*1 arg max*/, &path);
  88. #endif
  89. chp = argv[optind];
  90. if (!chp) {
  91. /* GNU coreutils 8.4:
  92. * bare "mktemp" -> "mktemp -t tmp.XXXXXX"
  93. */
  94. chp = xstrdup("tmp.XXXXXX");
  95. opts |= OPT_t;
  96. }
  97. #if 0
  98. /* Don't allow directory separator in template */
  99. if ((opts & OPT_t) && bb_basename(chp) != chp) {
  100. errno = EINVAL;
  101. goto error;
  102. }
  103. #endif
  104. if (opts & (OPT_t|OPT_p|OPT_tmpdir))
  105. chp = concat_path_file(path, chp);
  106. if (opts & OPT_u) {
  107. chp = mktemp(chp);
  108. if (chp[0] == '\0')
  109. goto error;
  110. } else if (opts & OPT_d) {
  111. if (mkdtemp(chp) == NULL)
  112. goto error;
  113. } else {
  114. if (mkstemp(chp) < 0)
  115. goto error;
  116. }
  117. puts(chp);
  118. return EXIT_SUCCESS;
  119. error:
  120. if (opts & OPT_q)
  121. return EXIT_FAILURE;
  122. /* don't use chp as it gets mangled in case of error */
  123. bb_perror_nomsg_and_die();
  124. }