A static site generator, written in C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cms_util.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <dirent.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <errno.h>
  11. #include "cms_util.h"
  12. int cms_util_file_exists(char* fname)
  13. {
  14. int f = open(fname, O_RDONLY);
  15. //The file doesn't exist if the open command returned the ENOENT error.
  16. if (f == -1 && errno == ENOENT)
  17. return 0;
  18. //If the open command succeeded, we want to close the file descriptor.
  19. else if (f != -1)
  20. close(f);
  21. return 1;
  22. }
  23. cms_err cms_util_file_create(char* fname)
  24. {
  25. int f = open(fname, O_CREAT, 0777);
  26. close(f);
  27. if (f == -1)
  28. return cms_err_from_std_err(errno);
  29. else
  30. return CMS_ERR_NONE;
  31. }
  32. cms_err cms_util_file_copy(char* fname1, char* fname2)
  33. {
  34. int f1 = open(fname1, O_RDONLY);
  35. if (f1 == -1)
  36. return cms_err_from_std_err(errno);
  37. int f2 = open(fname2, O_WRONLY | O_CREAT, 0777);
  38. if (f1 == -1)
  39. return cms_err_from_std_err(errno);
  40. struct stat* s = malloc(sizeof(struct stat));
  41. if (s == NULL)
  42. return CMS_ERR_ALLOC;
  43. fstat(f1, s);
  44. void* buf = malloc(s->st_size);
  45. if (buf == NULL)
  46. return CMS_ERR_ALLOC;
  47. read(f1, buf, s->st_size);
  48. write(f2, buf, s->st_size);
  49. close(f1);
  50. close(f2);
  51. free(s);
  52. free(buf);
  53. return CMS_ERR_NONE;
  54. }
  55. cms_err cms_util_dir_copy_recursive(char* dir1, char* dir2)
  56. {
  57. if (mkdir(dir2, 0777) == -1 && errno != EEXIST)
  58. return cms_err_from_std_err(errno);
  59. DIR* dp = opendir(dir1);
  60. if (dp == NULL)
  61. return cms_err_from_std_err(errno);
  62. struct dirent* ep;
  63. struct stat* st = malloc(sizeof(struct stat));
  64. if (st == NULL)
  65. return CMS_ERR_ALLOC;
  66. while (ep = readdir(dp))
  67. {
  68. if (ep->d_name[0] == '.')
  69. continue;
  70. char* path1 = cms_util_path_join(dir1, ep->d_name);
  71. char* path2 = cms_util_path_join(dir2, ep->d_name);
  72. if (stat(path1, st) == -1)
  73. return cms_err_from_std_err(errno);
  74. if (S_ISDIR(st->st_mode))
  75. {
  76. cms_err err = cms_util_dir_copy_recursive(path1, path2);
  77. if (err)
  78. return err;
  79. }
  80. else
  81. {
  82. cms_err err = cms_util_file_copy(path1, path2);
  83. if (err)
  84. return err;
  85. }
  86. }
  87. closedir(dp);
  88. free(st);
  89. return CMS_ERR_NONE;
  90. }
  91. char* cms_util_path_join(char* str1, char* str2)
  92. {
  93. size_t len1 = strlen(str1);
  94. size_t len2 = strlen(str2);
  95. if (str1[len1 - 1] == '/')
  96. {
  97. len1 -= 1;
  98. }
  99. char* path = malloc((len1 + len2 + 1) * sizeof(char));
  100. if (path == NULL)
  101. cms_err_panic(CMS_ERR_ALLOC, "");
  102. if (path == NULL)
  103. return NULL;
  104. memcpy(path, str1, len1); //First part
  105. path[len1] = '/'; //Separator
  106. memcpy(path + len1 + 1, str2, len2); //Second part
  107. path[len1 + len2 + 1] = '\0'; //Null terminator
  108. return path;
  109. }