list.c 597 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #define _XOPEN_SOURCE 700
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "maje.h"
  5. struct majefile *insert_file(struct majefile *list, const char *path, const struct stat *st)
  6. {
  7. struct majefile *tmp = malloc(sizeof(*tmp) + strlen(path) + 1);
  8. if (tmp == NULL) {
  9. return NULL;
  10. }
  11. tmp->next = NULL;
  12. tmp->prev = NULL;
  13. if (st) {
  14. tmp->st = *st;
  15. } else {
  16. tmp->st = (struct stat) { 0 };
  17. }
  18. strcpy(tmp->path, path);
  19. if (list == NULL) {
  20. return tmp;
  21. }
  22. tmp->next = list->next;
  23. tmp->prev = list;
  24. list->next = tmp;
  25. if (tmp->next != NULL) {
  26. tmp->next->prev = tmp;
  27. }
  28. return tmp;
  29. }