-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
252 lines (229 loc) · 6.93 KB
/
main.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "compression.h"
#include "str.h"
/* Set this to true and the package manager will actually add things to /
* Don't enable for alpha unless you know what you are doing.
* Don't say I didn't warn you! */
int DangerMode = false;
unsigned short Mode = MODE_NONE;
struct PackageData Package;
/* Functions */
int GetFlags(int argc, char * argv[]); /* Converts command line flags to variables and settings */
int ParsePackage(char * data); /* Load the package info into the Package struct */
int LoadPackage();
int main(int argc, char * argv[])
{
if (!GetFlags(argc, argv)) return 1;
/* Find info on a package in the current directory */
if (Mode == MODE_ADD) {
//if (geteuid() != 0) {
// printf(ERROR_NOTROOT_STR);
// return 1;
//}
int Result = LoadPackage();
if (Result != 0) return Result;
int i;
if (Package.Type == PKGTYPE_BIN) {
ExtractLoadedTar("");
}
else if (Package.Type == PKGTYPE_SRC) {
}
}
if (Mode == MODE_INFO) {
int Result = LoadPackage();
if (Result != 0) return Result;
printf("%d: %s (%d)\n", TarFileCount, tFile[2].Name, tFile[2].Mode);
printf("%s: (%s) %s\n%s\n%s\n", Package.Name, (Package.Type == PKGTYPE_BIN) ? "Binary" : "Source", Package.Version, Package.Summary, Package.Description);
/* Clean up */
free(Package.Name);
free(Package.Version);
free(Package.Summary);
free(Package.Description);
FreeUpTar();
} /* Prints some details about the package manager. */
else if (Mode == MODE_VERSION) {
char * Libs = NULL;
/* Gets each library version */
#ifdef USE_GZ
Libs = realloc(Libs, strlen(" zlib: ") + strlen(zlibVersion()));
strcat(Libs, " zlib: ");
strcat(Libs, zlibVersion());
#endif
#ifdef USE_BZ2
Libs = realloc(Libs, strlen(" bzlib: ") + strlen(BZ2_bzlibVersion()));
strcat(Libs, " bzlib: ");
strcat(Libs, BZ2_bzlibVersion());
#endif
/* If no libraries are being used then simply say so */
if (Libs == NULL) {
#define NO_LIBS " No external libraries."
Libs = malloc(strlen(NO_LIBS));
strcat(Libs, NO_LIBS);
#undef NO_LIBS
}
printf(VERSION, Libs);
//free(Libs);
}
return 0;
}
int GetFlags(int argc, char * argv[])
{
short Success = true;
if (argc < 2) {
printf(HELP_STR);
Success = false;
}
else
{
if (strcmp(argv[1], "help") == 0) {
printf(HELP_STR);
Success = false;
}
else if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0) Mode = MODE_VERSION;
if (strcmp(argv[1], "info") == 0 && argc > 2) {
Mode = MODE_INFO;
Package.FileName = (char*)malloc(strlen(argv[2]));
strcpy(Package.FileName,argv[2]);
}
if (strcmp(argv[1], "add") == 0 && argc > 2) {
Mode = MODE_ADD;
Package.FileName = (char*)malloc(strlen(argv[2]));
strcpy(Package.FileName,argv[2]);
}
}
return Success;
}
int ParsePackage(char * data)
{
char * tmp = strtok(data,"\n"); /* Go line by line through the file */
int Works = true;
while (tmp != NULL)
{
/* We first trim and get the current line's info type, then use it to determine where to save the data */
strtrim(tmp, ' ');
char * Type = strsub(tmp,':');
if (strcmp(Type, "Package") == 0) { /* Save the package name */
char * Info = strstr(tmp, " ");
Package.Name = (char*)malloc(strlen(Info));
int i; /* Copy the info into the correct field, but ignore the first space */
strtrim(Info, ' ');
for (i = 0; i < strlen(Info); ++i) Package.Name[i] = Info[i];
Package.Name[i] = '\0';
}
else if (strcmp(Type, "Version") == 0) { /* Save the package version */
char * Info = strstr(tmp, " ");
Package.Version = (char*)malloc(strlen(Info));
int i; /* Copy the info into the correct field, but ignore the first space */
strtrim(Info, ' ');
for (i = 0; i < strlen(Info); ++i) Package.Version[i] = Info[i];
Package.Version[i] = '\0';
}
else if (strcmp(Type, "Summary") == 0) { /* Save the package summary */
char * Info = strstr(tmp, " ");
Package.Summary = (char*)malloc(strlen(Info));
int i; /* Copy the info into the correct field, but ignore the first space */
strtrim(Info, ' ');
for (i = 0; i < strlen(Info); ++i) Package.Summary[i] = Info[i];
Package.Summary[i] = '\0';
}
else if (strcmp(Type, "Description") == 0) { /* Save the package summary */
char * Info = strstr(tmp, " ");
Package.Description = (char*)malloc(strlen(Info));
int i; /* Copy the info into the correct field, but ignore the first space */
strtrim(Info, ' ');
for (i = 0; i < strlen(Info); ++i) Package.Description[i] = Info[i];
Package.Description[i] = '\0';
}
else if (strcmp(Type, "Type") == 0) { /* Save the package summary */
char * Info = strstr(tmp, " ");
strtrim(Info, ' ');
if (strcmp(Info, "Binary") == 0) Package.Type = PKGTYPE_BIN;
else if (strcmp(Info, "Source") == 0) Package.Type = PKGTYPE_SRC;
else {
Works = false;
break;
}
}
else {
free(Type);
Works = false;
break;
}
tmp = strtok (NULL, "\n");
free(Type); /* We have to free up mem from our strsub() call */
}
/* Free up no longer needed mem */
return Works;
}
int LoadPackage()
{
char * Data;
int Size, i, ExtType = EXT_INVALID;
/* Load the gz file */
Data = InfFile2Char(Package.FileName, Data, &Size);
if (Data == NULL) {
printf(ERROR_BADFILE_STR);
return 1;
}
/* Now, let's load the tar, free the Data, then use that to load the info on the package */
LoadTarFromChar(Data, Size);
free(Data);
if (TarFileCount != 2) {
printf(ERROR_BADPKG_STR);
return 1;
}
ExtType = EXT_INVALID;
for (i = 0; i < TarFileCount; ++i) {
if (strcmp(tFile[i].Name, "info") == 0) { ExtType = EXT_NONE; break; }
if (strcmp(tFile[i].Name, "info.gz") == 0) {
#ifdef USE_GZ
ExtType = EXT_GZ;
#endif
break;
}
}
if (i == TarFileCount) {
printf(ERROR_BADPKG_STR);
return 1;
}
if (ExtType == EXT_GZ) Data = gzInfFromChar(tFile[i].Data, tFile[i].DataLength, &Size);
else if (ExtType == EXT_NONE) {
Data = malloc(tFile[i].DataLength);
strsubcpy(Data, tFile[i].Data, 0, tFile[i].DataLength, true, -1);
}
else {
printf(ERROR_PKG_UNSUPPORTED_STR);
return 1;
}
/* Let's parse the package now, make sure it went OK. */
if (ParsePackage(Data) == false) {
printf(ERROR_BADPKG_STR);
return 1;
}
/* After freeing up Data, let's load the actual files into it */
free(Data);
ExtType = EXT_INVALID;
for (i = 0; i < TarFileCount; ++i) {
if (strcmp(tFile[i].Name, "data.tar") == 0) { ExtType = EXT_NONE; break; }
if (strcmp(tFile[i].Name, "data.tar.gz") == 0) {
#ifdef USE_GZ
ExtType = EXT_GZ;
#endif
break;
}
}
if (ExtType == EXT_GZ) Data = gzInfFromChar(tFile[i].Data, tFile[i].DataLength, &Size);
else if (ExtType == EXT_NONE) {
Data = malloc(tFile[i].DataLength);
strsubcpy(Data, tFile[i].Data, 0, tFile[i].DataLength, false, -1);
Size = tFile[i].DataLength;
}
else {
printf(ERROR_PKG_UNSUPPORTED_STR);
return 1;
}
/* Load the actual files from the tar, remove the old tar data though first cause we don't need it. */
FreeUpTar();
LoadTarFromChar(Data, Size);
free(Data);
return 0;
}