mktemp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. //usage:#define mktemp_trivial_usage
  33. //usage: "[-dt] [-p DIR] [TEMPLATE]"
  34. //usage:#define mktemp_full_usage "\n\n"
  35. //usage: "Create a temporary file with name based on TEMPLATE and print its name.\n"
  36. //usage: "TEMPLATE must end with XXXXXX (e.g. [/dir/]nameXXXXXX).\n"
  37. //usage: "Without TEMPLATE, -t tmp.XXXXXX is assumed.\n"
  38. //usage: "\n -d Make directory, not file"
  39. //usage: "\n -q Fail silently on errors"
  40. //usage: "\n -t Prepend base directory name to TEMPLATE"
  41. //usage: "\n -p DIR Use DIR as a base directory (implies -t)"
  42. //usage: "\n -u Do not create anything; print a name"
  43. //usage: "\n"
  44. //usage: "\nBase directory is: -p DIR, else $TMPDIR, else /tmp"
  45. //usage:
  46. //usage:#define mktemp_example_usage
  47. //usage: "$ mktemp /tmp/temp.XXXXXX\n"
  48. //usage: "/tmp/temp.mWiLjM\n"
  49. //usage: "$ ls -la /tmp/temp.mWiLjM\n"
  50. //usage: "-rw------- 1 andersen andersen 0 Apr 25 17:10 /tmp/temp.mWiLjM\n"
  51. #include "libbb.h"
  52. int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  53. int mktemp_main(int argc UNUSED_PARAM, char **argv)
  54. {
  55. const char *path;
  56. char *chp;
  57. unsigned opts;
  58. enum {
  59. OPT_d = 1 << 0,
  60. OPT_q = 1 << 1,
  61. OPT_t = 1 << 2,
  62. OPT_p = 1 << 3,
  63. OPT_u = 1 << 4,
  64. };
  65. path = getenv("TMPDIR");
  66. if (!path || path[0] == '\0')
  67. path = "/tmp";
  68. opt_complementary = "?1"; /* 1 argument max */
  69. opts = getopt32(argv, "dqtp:u", &path);
  70. chp = argv[optind];
  71. if (!chp) {
  72. /* GNU coreutils 8.4:
  73. * bare "mktemp" -> "mktemp -t tmp.XXXXXX"
  74. */
  75. chp = xstrdup("tmp.XXXXXX");
  76. opts |= OPT_t;
  77. }
  78. #if 0
  79. /* Don't allow directory separator in template */
  80. if ((opts & OPT_t) && bb_basename(chp) != chp) {
  81. errno = EINVAL;
  82. goto error;
  83. }
  84. #endif
  85. if (opts & (OPT_t|OPT_p))
  86. chp = concat_path_file(path, chp);
  87. if (opts & OPT_u) {
  88. chp = mktemp(chp);
  89. if (chp[0] == '\0')
  90. goto error;
  91. } else if (opts & OPT_d) {
  92. if (mkdtemp(chp) == NULL)
  93. goto error;
  94. } else {
  95. if (mkstemp(chp) < 0)
  96. goto error;
  97. }
  98. puts(chp);
  99. return EXIT_SUCCESS;
  100. error:
  101. if (opts & OPT_q)
  102. return EXIT_FAILURE;
  103. /* don't use chp as it gets mangled in case of error */
  104. bb_perror_nomsg_and_die();
  105. }