tempname.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Create a temporary file or directory.
  2. Copyright (C) 2006, 2009-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* header written by Eric Blake */
  14. #ifndef GL_TEMPNAME_H
  15. # define GL_TEMPNAME_H
  16. # include <stdio.h>
  17. # ifdef __GT_FILE
  18. # define GT_FILE __GT_FILE
  19. # define GT_DIR __GT_DIR
  20. # define GT_NOCREATE __GT_NOCREATE
  21. # else
  22. # define GT_FILE 0
  23. # define GT_DIR 1
  24. # define GT_NOCREATE 2
  25. # endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* Generate a temporary file name based on TMPL. TMPL must match the
  30. rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix).
  31. The name constructed does not exist at the time of the call to
  32. gen_tempname. TMPL is overwritten with the result.
  33. KIND may be one of:
  34. GT_NOCREATE: simply verify that the name does not exist
  35. at the time of the call.
  36. GT_FILE: create a large file using open(O_CREAT|O_EXCL)
  37. and return a read-write fd. The file is mode 0600.
  38. GT_DIR: create a directory, which will be mode 0700.
  39. */
  40. extern int gen_tempname (char *tmpl, int suffixlen, int flags, int kind);
  41. /* Similar, except X_SUFFIX_LEN gives the number of Xs. */
  42. extern int gen_tempname_len (char *tmpl, int suffixlen, int flags, int kind,
  43. size_t x_suffix_len);
  44. /* Similar to gen_tempname, but TRYFUNC is called for each temporary
  45. name to try. If TRYFUNC returns a non-negative number, TRY_GEN_TEMPNAME
  46. returns with this value. Otherwise, if errno is set to EEXIST, another
  47. name is tried, or else TRY_GEN_TEMPNAME returns -1. */
  48. extern int try_tempname (char *tmpl, int suffixlen, void *args,
  49. int (*tryfunc) (char *, void *));
  50. /* Similar, except X_SUFFIX_LEN gives the number of Xs. */
  51. extern int try_tempname_len (char *tmpl, int suffixlen, void *args,
  52. int (*tryfunc) (char *, void *),
  53. size_t x_suffix_len);
  54. #ifdef __cplusplus
  55. }
  56. #endif
  57. #endif /* GL_TEMPNAME_H */