3
0

mktemp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. };
  71. path = getenv("TMPDIR");
  72. if (!path || path[0] == '\0')
  73. path = "/tmp";
  74. opts = getopt32(argv, "^" "dqtp:u" "\0" "?1"/*1 arg max*/, &path);
  75. chp = argv[optind];
  76. if (!chp) {
  77. /* GNU coreutils 8.4:
  78. * bare "mktemp" -> "mktemp -t tmp.XXXXXX"
  79. */
  80. chp = xstrdup("tmp.XXXXXX");
  81. opts |= OPT_t;
  82. }
  83. #if 0
  84. /* Don't allow directory separator in template */
  85. if ((opts & OPT_t) && bb_basename(chp) != chp) {
  86. errno = EINVAL;
  87. goto error;
  88. }
  89. #endif
  90. if (opts & (OPT_t|OPT_p))
  91. chp = concat_path_file(path, chp);
  92. if (opts & OPT_u) {
  93. chp = mktemp(chp);
  94. if (chp[0] == '\0')
  95. goto error;
  96. } else if (opts & OPT_d) {
  97. if (mkdtemp(chp) == NULL)
  98. goto error;
  99. } else {
  100. if (mkstemp(chp) < 0)
  101. goto error;
  102. }
  103. puts(chp);
  104. return EXIT_SUCCESS;
  105. error:
  106. if (opts & OPT_q)
  107. return EXIT_FAILURE;
  108. /* don't use chp as it gets mangled in case of error */
  109. bb_perror_nomsg_and_die();
  110. }