-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexcludes.c
107 lines (92 loc) · 2.28 KB
/
excludes.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright: 2013 Xilinx Inc
* Written by Edgar E. Iglesias <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#define _GNU_SOURCE
#include <stdint.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include "excludes.h"
struct exclude
{
struct exclude *next;
char filename[PATH_MAX];
unsigned int linenr;
};
void *excludes_create(const char *filename)
{
struct exclude *ex_root = NULL;
char *lineptr;
size_t n;
FILE *fp;
printf("%s: %s\n", __func__, filename);
if (!filename)
return NULL;
fp = fopen(filename, "r");
if (!fp) {
perror(filename);
return NULL;
}
do {
struct exclude *ex;
ssize_t r;
char *delim;
char *t;
n = 0;
lineptr = NULL;
r = getline(&lineptr, &n, fp);
if (r <= 0)
break;
/* Ignore empty lines and comments. */
if (lineptr[0] == '\n' || lineptr[0] == '#')
continue;
delim = strchr(lineptr, ':');
if (!delim) {
printf("WARNING: Bad exclude line, "
"missing ':' delimiter\n%s\n", lineptr);
continue;
}
ex = malloc(sizeof *ex);
ex->next = ex_root;
ex_root = ex;
t = mempcpy(ex->filename, lineptr, delim - lineptr);
t[0] = 0;
delim++;
ex->linenr = strtoull(delim, NULL, 10);
printf("Add Exclude %s : %d\n", ex->filename, ex->linenr);
} while (1);
return ex_root;
}
bool excludes_match(void *excludes, const char *filename, int linenr)
{
struct exclude *ex = excludes;
while (ex) {
if (strcmp(filename, ex->filename) == 0
&& (linenr == -1 || linenr == ex->linenr)) {
return true;
}
ex = ex->next;
}
return false;
}