summaryrefslogtreecommitdiff
path: root/list.c
blob: 2fa06abf0a89a75217f3ec7b91a64b59e9d99f6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "maje.h"

struct majefile *insert_file(struct majefile *list, const char *path, const struct stat *st)
{
	struct majefile *tmp = malloc(sizeof(*tmp) + strlen(path) + 1);
	if (tmp == NULL) {
		return NULL;
	}

	tmp->next = NULL;
	tmp->prev = NULL;

	if (st) {
		tmp->st = *st;
	} else {
		tmp->st = (struct stat) { 0 };
	}

	strcpy(tmp->path, path);

	if (list == NULL) {
		return tmp;
	}

	tmp->next = list->next;
	tmp->prev = list;
	list->next = tmp;
	if (tmp->next != NULL) {
		tmp->next->prev = tmp;
	}
	return tmp;
}

struct majeflag *insert_flag(struct majeflag *list, char *flag)
{
	char *eq = strchr(flag, '=');
	*eq = '\0';
	eq++;

	int type = 0;
	if (!strcmp(flag, "MAJE_CFLAG")) {
		type = MAJE_CFLAG;
	} else if (!strcmp(flag, "MAJE_LDFLAG")) {
		type = MAJE_LDFLAG;
	} else if (!strcmp(flag, "MAJE_LDLIB")) {
		type = MAJE_LDLIB;
	} else {
		fprintf(stderr, "maje: unkonwn directive '%s'\n", flag);
		return list;
	}

	struct majeflag *tmp = malloc(sizeof(*tmp) + strlen(flag) + 1);
	if (tmp == NULL) {
		return NULL;
	}

	tmp->next = NULL;
	tmp->prev = NULL;

	tmp->type = type;
	strcpy(tmp->flag, eq);

	if (list == NULL) {
		return tmp;
	}

	tmp->next = list->next;
	tmp->prev = list;
	list->next = tmp;
	if (tmp->next != NULL) {
		tmp->next->prev = tmp;
	}
	return tmp;
}