filehelper.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2018 Markus Hennecke <markus-hennecke@markus-hennecke.de>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <sys/stat.h>
  17. #include <sys/types.h>
  18. #include <dirent.h>
  19. #include <err.h>
  20. #include <fcntl.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include "filehelper.h"
  26. struct dir_list *
  27. get_dir_entries(const char *_directory)
  28. {
  29. DIR *dir = opendir(_directory);
  30. if (dir) {
  31. struct dir_list *list = malloc(sizeof(struct dir_list));
  32. if (list == NULL)
  33. err(1, NULL);
  34. int cwd = open(".", O_DIRECTORY | O_RDONLY);
  35. if (cwd == -1)
  36. err(1, NULL);
  37. if (chdir(_directory) == -1)
  38. err(1, NULL);
  39. TAILQ_INIT(&list->entries);
  40. list->newest = 0;
  41. list->path = strdup(_directory);
  42. struct dirent *dirent;
  43. while ((dirent = readdir(dir)) != NULL) {
  44. struct dir_entry *entry;
  45. if (dirent->d_name[0] == '.')
  46. continue;
  47. entry = dir_entry_new(dirent->d_name);
  48. TAILQ_INSERT_TAIL(&list->entries, entry, entries);
  49. if (entry->sb.st_mtim.tv_sec > list->newest)
  50. list->newest = entry->sb.st_mtim.tv_sec;
  51. }
  52. closedir(dir);
  53. fchdir(cwd);
  54. return list;
  55. }
  56. return NULL;
  57. }
  58. struct dir_entry *
  59. dir_entry_new(const char *_filename)
  60. {
  61. struct dir_entry *entry = calloc(1,
  62. sizeof(struct dir_entry));
  63. if (entry == NULL)
  64. err(1, NULL);
  65. entry->filename = strdup(_filename);
  66. if (lstat(_filename, &entry->sb) != 0)
  67. err(1, "stat error on file %s", _filename);
  68. return entry;
  69. }
  70. void
  71. dir_entry_free(struct dir_entry *_entry)
  72. {
  73. if (_entry) {
  74. free(_entry->filename);
  75. free(_entry);
  76. }
  77. }
  78. bool
  79. dir_entry_exists(const char *_name, struct dir_list *_dir)
  80. {
  81. struct dir_entry *entry;
  82. TAILQ_FOREACH(entry, &_dir->entries, entries) {
  83. if (strcmp(entry->filename, _name) == 0)
  84. return true;
  85. }
  86. return false;
  87. }
  88. void
  89. dir_list_free(struct dir_list *_list)
  90. {
  91. struct dir_entry *entry;
  92. while ((entry = TAILQ_FIRST(&_list->entries))) {
  93. TAILQ_REMOVE(&_list->entries, entry, entries);
  94. dir_entry_free(entry);
  95. }
  96. free(_list->path);
  97. }
  98. bool
  99. file_exists(const char *_filename)
  100. {
  101. struct stat sb;
  102. if (lstat(_filename, &sb) == -1)
  103. return false;
  104. return true;
  105. }