vsnprintf.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * libcompat - system compatibility library
  3. *
  4. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  5. * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include "compat.h"
  26. int
  27. vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
  28. {
  29. static FILE *file = NULL;
  30. static pid_t file_pid;
  31. size_t want, nr;
  32. int total;
  33. if (maxsize != 0 && buf == NULL)
  34. return -1;
  35. /* Avoid race conditions from children after a fork(2). */
  36. if (file_pid > 0 && file_pid != getpid()) {
  37. fclose(file);
  38. file = NULL;
  39. }
  40. if (!file) {
  41. file = tmpfile();
  42. if (!file)
  43. return -1;
  44. file_pid = getpid();
  45. } else {
  46. if (fseek(file, 0, 0))
  47. return -1;
  48. if (ftruncate(fileno(file), 0))
  49. return -1;
  50. }
  51. total = vfprintf(file, fmt, args);
  52. if (total < 0)
  53. return -1;
  54. if (maxsize == 0)
  55. return total;
  56. if (total >= (int)maxsize)
  57. want = maxsize - 1;
  58. else
  59. want = total;
  60. if (fflush(file))
  61. return -1;
  62. if (fseek(file, 0, SEEK_SET))
  63. return -1;
  64. nr = fread(buf, 1, want, file);
  65. if (nr != want)
  66. return -1;
  67. buf[want] = '\0';
  68. return total;
  69. }