-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_dbinterpret.h
194 lines (170 loc) · 4.74 KB
/
mod_dbinterpret.h
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef _MOD_DBINTERPRET_H
#define _MOD_DBINTERPRET_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef UUA
#define UUA __attribute__((__unused__))
#endif
void prep_workln(char workln[512], char (*pkgid)[32], char (*pkgname)[256], \
char (*pkgver)[32], char (*pkgdeps)[4096]){
/* Var Init */
int i = 0, ln_len = 1;
char *workln_ptr;
const char *tmp = workln;
/* get workln length */
while ((tmp = strstr(tmp, ":")))
{ln_len++; tmp++;}
workln_ptr = strtok(workln, ":");
for (i=0; i<ln_len; i++)
{
if (workln_ptr != NULL)
{
if (i == 0)
{strcpy(*pkgid, workln_ptr);}
else if (i == 1)
{strcpy(*pkgname, workln_ptr);}
else if (i == 2)
{strcpy(*pkgver, workln_ptr);}
else if (i == 3)
{strcpy(*pkgdeps, workln_ptr);}
workln_ptr = strtok(NULL, ":");
}
}
}
extern int read_db(char fname[512], int display_all, int search, \
char searchval[256], char (*rval)[256], char (*rdeps)[4096]){
/* Var Init */
int indexfd = -1, i = 0, fline_len = 0, scount = 0;
char buffer[32], line[512], fline[512], workln[512];
char pkgid[32] = "", pkgname[256] = "", pkgver[32] = "", \
pkgdeps[4096] = "";
/* open file */
indexfd = open(fname, O_RDONLY);
if (indexfd != -1)
{
while ((read(indexfd, buffer, 1)) != 0)
{
if ((strncmp(buffer, "\n", 1)) != 0)
{
line[i] = buffer[0];
i++;
}
else
{
strncpy(fline, line, i);
fline[i] = '\0';
fline_len = strlen(fline);
#ifdef DEBUG
printf("fline: %s\n", fline);
printf("fline[0]: %c\n", fline[0]);
printf("fline+fline_len-1: %s\n", fline+fline_len-1);
#endif
if ((strncmp(fline, "#", 1)) == 0)
{
/* do nothing */
sleep(0);
}
else if ((strncmp(fline,"{", 1) == 0) && (strncmp(fline+fline_len-1,"}", 1) == 0))
{
#ifdef DEBUG
printf("ArgLine!\n");
#endif
/* remove { and } */
strncpy(workln, fline+1, fline_len-1);
workln[fline_len-2] = '\0';
/* split by : */
prep_workln(workln, &pkgid, &pkgname, &pkgver, &pkgdeps);
/* printout the whole list */
if ((display_all == 1) && (search == 0))
{printf("%s - %s\n", pkgname, pkgver);}
/* search + printout */
else if ((search == 1) && (display_all == 0))
{
if ((strcasestr(pkgname, searchval)) != NULL)
{printf("%s - %s\n", pkgname, pkgver);}
}
/* same as above, does not output, case sensitive, used in -i */
else if ((search == 1) && (display_all == 1))
{
if ((strcmp(pkgname, searchval)) == 0)
{
strcpy(*rval, pkgname);
strcat(*rval, "_v");
strcat(*rval, pkgver);
strcat(*rval, ".tgz");
strcpy(*rdeps, pkgdeps);
scount++;
}
}
/* only write pkgver to rval, used in -ui */
else if ((search == 2) && (display_all == 1))
{
if ((strcmp(pkgname, searchval)) == 0)
{
strcpy(*rval, pkgver);
scount++;
}
}
}
/* reset counter */
i = 0;
}
}
/* close if successfully opened */
close(indexfd);
}
if ((search == 0) && (display_all == 0))
{return atoi(pkgid)+1;}
else
{return scount;}
}
extern int rem_db(char fname[512], char searchval[256]){
/* Var Init */
int instlldfd = -1, i = 0, fline_len = 0;
char buffer[32], line[512], fline[512], workln[512];
char UUA pkgid[32] = "", pkgname[256] = "", UUA pkgver[32] = "";
instlldfd = open(fname, O_RDWR);
if (instlldfd != -1)
{
while ((read(instlldfd, buffer, 1)) != 0)
{
if ((strncmp(buffer, "\n", 1)) != 0)
{
line[i] = buffer[0];
i++;
}
else
{
strncpy(fline, line, i);
fline[i] = '\0';
fline_len = strlen(fline);
if ((strncmp(fline,"{", 1) == 0) && (strncmp(fline+fline_len-1,"}", 1) == 0))
{
#ifdef DEBUG
printf("ArgLine!\n");
#endif
/* remove { and } */
strncpy(workln, fline+1, fline_len-1);
workln[fline_len-2] = '\0';
/* split by : */
prep_workln(workln, &pkgid, &pkgname, &pkgver, NULL);
if ((strcmp(pkgname, searchval)) == 0)
{
lseek(instlldfd, -(strlen(pkgname) + strlen(pkgid) + strlen(pkgver) + 5), SEEK_CUR);
write(instlldfd, "#", 1);
}
}
/* reset counter */
i = 0;
}
}
close(instlldfd);
}
return 0;
}
#endif