-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistbuilder.c
More file actions
176 lines (140 loc) · 3.81 KB
/
listbuilder.c
File metadata and controls
176 lines (140 loc) · 3.81 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* listbuilder can merge 2 package lists
*
* Copyright (C) 2006 Jean-Philippe Guillemin <jp.guillemin@free.fr>
*
* This file is free software; the copyright holder gives unlimited
* permission to copy and/or distribute it, with or without
* modifications, as long as this notice is preserved.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY, to the extent permitted by law; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sysexits.h>
#include <linux/types.h>
#include <regex.h>
#define VERSION "0.1"
#define TRUE 1
#define FALSE 0
#define EQUAL 0
#define LESSER -1
#define GREATER 1
#ifdef DEBUG
#define dbg(fmt,args...) fprintf(stderr, fmt , ## args)
#else
#define dbg(fmt,args...) do { } while (0)
#endif
/**********************/
/* PROTOTYPES */
/**********************/
regex_t regex;
regmatch_t pmatch[2];
size_t size = 0;
/* usage function */
int usage(void);
/***************** MAIN ********************/
int main(int argc, char *argv[]) {
if (argc < 2) {
usage();
exit(EXIT_FAILURE);
}
int i,c;
char *filename1 = NULL;
char *filename2 = NULL;
FILE *fd1;
FILE *fd2;
char pkg1[256];
char softname1[128];
char pkg2[10000][256];
char softname2[10000][128];
char category[128];
char buffer[256];
int start,end;
while ((i = getopt(argc, argv, "a:i:h")) != EOF){
switch (i){
case 'a':
filename1 = optarg;
break;
case 'i':
filename2 = optarg;
break;
case 'h': usage(); exit(EXIT_FAILURE);
}
}
if((fd1=fopen(filename1, "rb")) == 0) {
dbg("Error opening file %s !\n",filename1);
exit(EXIT_FAILURE);
}
if((fd2=fopen(filename2, "rb")) == 0) {
dbg("Error opening file %s !\n",filename2);
exit(EXIT_FAILURE);
}
if ( regcomp(®ex, "^(.*)-[^-]*-[^-]*-[^-]*\\.t[glx]z$", REG_EXTENDED )) {
dbg("main() : regex failed compilation\n");
return(EXIT_FAILURE);
}
int id=0;
while (fgets (buffer, sizeof(buffer), fd2) != NULL) {
buffer[strlen(buffer) - 1 ] = '\0';
dbg("main() : buffer = %s\n", buffer);
c = sscanf (buffer, "%s", pkg2[id]);
dbg("main() : pkg1 = %s\n", pkg2[id]);
if ( regexec(®ex, pkg2[id], 2, pmatch, 0) ) {
dbg("main() : regex doesn't match\n");
continue;
} else {
// Retrieving unique name
start = pmatch[1].rm_so;
end = pmatch[1].rm_eo;
size = end - start;
strncpy (softname2[id], pkg2[id] + start, size);
softname2[id][size] = '\0';
dbg("main() : softname2 = %s\n", softname2[id]);
}
id++;
}
while (fgets (buffer, sizeof(buffer), fd1) != NULL) {
buffer[strlen(buffer) - 1 ] = '\0';
dbg("main() : buffer = %s\n", buffer);
pkg1[0] = '\0';
c = sscanf (buffer, "%s %s",category,pkg1);
dbg("main() : category = %s\n", category);
dbg("main() : pkg1 = %s\n", pkg1);
if ( regexec(®ex, pkg1, 2, pmatch, 0) ) {
dbg("main() : regex doesn't match\n");
continue;
} else {
// Retrieving unique name
start = pmatch[1].rm_so;
end = pmatch[1].rm_eo;
size = end - start;
strncpy (softname1, pkg1 + start, size);
softname1[size] = '\0';
dbg("main() : softname1 = %s\n", softname1);
}
for (i=0; i< id; i++) {
if ( strncasecmp (softname1, softname2[i], 128) == 0){
break;
}
}
printf("%s | %s | %s\n",category, pkg1, pkg2[i]);
}
fclose (fd1);
fclose (fd2);
regfree(®ex);
return(EXIT_SUCCESS);
}
/* usage */
int usage(void) {
printf("\nlistbuilder v%s : build 1 main list from 2 package lists\n", VERSION);
printf("Usage : listbuilder -a filename1 -i filename2\n");
printf("\"filename1\" must contain lines in format \"category package\"\n");
printf("\"filename2\" must contain lines in format \"package\"\n");
return(EXIT_SUCCESS);
}