logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

file2string.c (791B)


  1. #include <errno.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. static const char safe[] =
  6. "][ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#%&'()*+,./:;<=>^_{|}~ -";
  7. int main(int argc, char *argv[]) {
  8. FILE *f;
  9. char *line = NULL, *s;
  10. size_t size = 0;
  11. ssize_t n;
  12. f = fopen(argv[1], "r");
  13. if (!f) {
  14. fprintf(stderr, "fopen %s: %s\n", argv[1], strerror(errno));
  15. return 1;
  16. }
  17. printf("/* Generated from %s */\n", argv[1]);
  18. while ((n = getline(&line, &size, f)) >= 0) {
  19. printf("\"");
  20. for (s = line; s < line + n; ++s) {
  21. if (memchr(safe, *s, sizeof(safe) - 1))
  22. fputc(*s, stdout);
  23. else
  24. printf("\\%03hho", *s);
  25. }
  26. printf("\"\n");
  27. }
  28. if (ferror(f)) {
  29. fprintf(stderr, "ferror: %s\n", strerror(errno));
  30. return 1;
  31. }
  32. return 0;
  33. }