3
0

mktemp.c 3.6 KB

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