print_separator.c 705 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <stdio.h>
  2. #include "warnp.h"
  3. #include "print_separator.h"
  4. /**
  5. * print_separator(stream, separator, print_nulls, num_nulls):
  6. * Print ${separator} to ${stream} if ${print_nulls} is zero; otherwise,
  7. * print ${num_nulls} '\0'.
  8. */
  9. int
  10. print_separator(FILE * stream, char * separator, int print_nulls, int num_nulls)
  11. {
  12. int i;
  13. /* Are we printing normal separators, or NULs? */
  14. if (print_nulls == 0) {
  15. if (fprintf(stream, "%s", separator) < 0) {
  16. warnp("fprintf");
  17. goto err0;
  18. }
  19. } else {
  20. for (i = 0; i < num_nulls; i++) {
  21. if (fprintf(stream, "%c", '\0') < 0) {
  22. warnp("fprintf");
  23. goto err0;
  24. }
  25. }
  26. }
  27. /* Success! */
  28. return (0);
  29. err0:
  30. /* Failure! */
  31. return (-1);
  32. }