testc_helpers.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /** @file testc_helpers.c
  2. * @brief Testc helper functions
  3. *
  4. * This file implements helpers for writing tests for 'testc' utility.
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <errno.h>
  12. #include "faux/ctype.h"
  13. #include "faux/str.h"
  14. #include "faux/file.h"
  15. ssize_t faux_testc_file_deploy(const char *fn, const char *str) {
  16. faux_file_t *f = NULL;
  17. ssize_t bytes_written = 0;
  18. assert(fn);
  19. assert(str);
  20. if (!fn || !str)
  21. return -1;
  22. f = faux_file_open(fn, O_WRONLY | O_CREAT | O_TRUNC, 00644);
  23. if (!f)
  24. return -1;
  25. bytes_written = faux_file_write_block(f, str, strlen(str));
  26. faux_file_close(f);
  27. if (bytes_written < 0)
  28. return -1;
  29. return bytes_written;
  30. }
  31. char *faux_testc_tmpfile_deploy(const char *str) {
  32. char template[] = "/tmp/testc_tmpfile_XXXXXX";
  33. int fd = -1;
  34. faux_file_t *f = NULL;
  35. ssize_t bytes_written = 0;
  36. assert(str);
  37. if (!str)
  38. return NULL;
  39. fd = mkstemp(template);
  40. if (fd < 0)
  41. return NULL;
  42. f = faux_file_fdopen(fd);
  43. if (!f)
  44. return NULL;
  45. bytes_written = faux_file_write_block(f, str, strlen(str));
  46. faux_file_close(f);
  47. if (bytes_written < 0)
  48. return NULL;
  49. return faux_str_dup(template);
  50. }