mktemp.c 839 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 "libbb.h"
  12. int mktemp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  13. int mktemp_main(int argc, char **argv)
  14. {
  15. unsigned long flags = getopt32(argv, "dqt");
  16. char *chp;
  17. if (optind + 1 != argc)
  18. bb_show_usage();
  19. chp = argv[optind];
  20. if (flags & 4) {
  21. char *dir = getenv("TMPDIR");
  22. if (dir && *dir != '\0')
  23. chp = concat_path_file(dir, chp);
  24. else
  25. chp = concat_path_file("/tmp/", chp);
  26. }
  27. if (flags & 1) {
  28. if (mkdtemp(chp) == NULL)
  29. return EXIT_FAILURE;
  30. } else {
  31. if (mkstemp(chp) < 0)
  32. return EXIT_FAILURE;
  33. }
  34. puts(chp);
  35. return EXIT_SUCCESS;
  36. }