mktemp.c 881 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 the GPL v2 or later, see the file LICENSE in this tarball.
  10. */
  11. #include "busybox.h"
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. int mktemp_main(int argc, char **argv)
  18. {
  19. unsigned long flags = getopt32(argc, argv, "dqt");
  20. char *chp;
  21. if (optind + 1 != argc)
  22. bb_show_usage();
  23. chp = argv[optind];
  24. if (flags & 4) {
  25. char *dir = getenv("TMPDIR");
  26. if (dir && *dir != '\0')
  27. chp = concat_path_file(dir, chp);
  28. else
  29. chp = concat_path_file("/tmp/", chp);
  30. }
  31. if (flags & 1) {
  32. if (mkdtemp(chp) == NULL)
  33. return EXIT_FAILURE;
  34. } else {
  35. if (mkstemp(chp) < 0)
  36. return EXIT_FAILURE;
  37. }
  38. puts(chp);
  39. return EXIT_SUCCESS;
  40. }