2
* apt-spy (c) Steven Holmes, 2003.
3
* This software is licensed as detailed in the LICENSE file.
6
/* This file is a bit icky */
8
char *str_toupper(char *str);
16
#include "include/global.h"
17
#include "include/parse.h"
18
#include "include/file.h"
20
int build_area_file(FILE *config_p, FILE *infile_p, FILE *mirror_list, char *area)
22
char *line; /* Where we read the lines into */
23
char *tmp; /* Temp. pointer */
24
char *inputline; /* The line that will be written to config_p */
25
char *country_code; /* where we put the country code */
31
while((line = next_entry(config_p)) != NULL) {
35
/* test for area string */
36
if ((tmp = strstr(line, area)) != NULL) {
38
if ((strchr(tmp,':')) == NULL) { /* And for trailing colon.. */
40
continue; /* .. it's not there */
46
if (ferror(config_p) != 0) /* Check for File error */
50
/* No match. Return. */
52
fprintf(stderr, "Could not find area named %s\n", area);
58
/* We now have the "label" line. The country list begins on the next line. */
59
while ((line = next_entry(config_p)) != NULL) {
61
if (ferror(config_p) != 0) { /* Check for file error */
66
/* Skip blank lines */
69
/* We check for either a non-space or a '\n'. Has the useful side-effect of */
70
/* fast-forwarding country_code past any preceding whitespace.*/
71
while ((*country_code != '\n') && (isspace(*country_code) != 0))
74
/* If country_code points to a '\n', there were no other characters. It was a blank line. */
75
/* If it points to a '#', there is a comment. We skip it too. */
76
if ((*country_code == '\n') || (*country_code == '#'))
79
if ((strchr(line, ':')) != NULL)
80
return 0; /* End of list. Return. */
82
/* We do a little fiddling to get the country code down to 2 letters and a space */
83
*(country_code + 2) = ' ';
84
*(country_code + 3) = '\0';
86
/* Sigh. The country code is in "country_code". We now parse the mirrors file for this */
87
/* and set the file position so that the next read will return the first */
90
if (find_country(mirror_list, country_code) == 1) {
91
fprintf(stderr, "Couldn't find country %s. Skipping.\n", country_code);
96
/* The next read of infile_p will return the first mirror entry. We parse */
97
/* this and build a line to put into the temporary file. */
99
while ((inputline = get_mirrors(mirror_list)) != NULL) {
101
/* We now write the line to the temporary file */
102
fputs(inputline, infile_p);
105
if ((ferror(infile_p)) != 0) { /* Check for file error */
116
int build_country_file(FILE *config_p, FILE *infile_p, FILE *mirror_list, char *country_list)
124
/* Upper-case country list */
125
str_toupper(country_list);
127
/* A cheap way to make sure we have enough space */
128
country_code = malloc(strlen(country_code));
133
/* Reset country code pointer */
136
/* Skip white space */
140
/* Copy up until end or comma */
141
while ((*p != '\0') && (*p != ',') && (isspace(*p) == 0))
144
/* Skip more white space. *sigh* */
148
/* skip past comma */
156
/* And find the country/build the file */
157
if (find_country(mirror_list, country_code) == 1) {
158
fprintf(stderr, "Couldn't find country %s. Skipping.\n", country_code);
164
while ((inputline = get_mirrors(mirror_list)) != NULL) {
165
fputs(inputline, infile_p);
168
if (ferror(infile_p)) {
176
/* Check we have found at least one country */
183
int find_country(FILE *mirror_list, char *country_code)
187
/* Make sure we're at beginning of file */
190
/* This is a hack to allow users to specify "UK" instead of "GB" */
191
if (!strcmp(country_code, "UK "))
192
strncpy(country_code, "GB ", 4);
194
/* Read until we find the country code */
195
while ((line = next_entry(mirror_list)) != NULL) {
197
cc = strstr(line, country_code);
201
/* Skip white space */
202
while (isspace(*line))
205
/* Country code should be first two characters on line. */
213
next_entry(mirror_list); /* Skip a line */
215
if (ferror(mirror_list)) {
219
return 0; /* We're positioned nicely for the next read */
222
/* line format is "server:ftp-path:http-path". */
224
char *get_mirrors(FILE *mirror_list)
226
char *line, *save_line;
227
char *creation, *save_creation;
230
/* Allocate space for creation */
231
save_creation = creation = malloc(100);
233
if (creation == NULL) {
239
/* First, we read in a line from the file */
240
save_line = line = next_entry(mirror_list);
242
/* test for file error */
243
if (ferror(mirror_list)) {
248
/* If the line begins with a space, we assume it is empty and the list is exhausted. */
249
if (isspace(*line) != 0)
252
/* We now read the server name into "creation" */
253
while (isspace(*line) == 0)
254
*creation++ = *line++;
256
/* And add a colon, which is the field seperator */
259
/* We skip over whitespace. If there is a lot of whitespace, we assume there is no FTP entry. */
260
while ((isspace(*line) != 0) && (counter < 30)) {
265
/* Check if there is an entry or just more space */
266
while (isspace(*line) == 0)
267
*creation++ = *line++;
271
/* We now check for an HTTP entry */
272
while (*line != '\n') {
273
if (isspace(*line) == 0)
278
while (isspace(*line) == 0)
279
*creation++ = *line++;
287
return save_creation;
290
void tokenise(server_t *current, char *cur_entry)
292
char *temp; /* We use this for temporary string-pointing :P */
294
/* We initialise the structure to 0 */
295
memset(current, 0, sizeof(*current));
297
/* First, we copy the server name into the struct. */
298
temp = current->hostname;
299
while (*cur_entry != ':')
300
*temp++ = *cur_entry++;
302
*temp++ = '\0'; /* Turn into string */
304
/* We now check for an ftp entry and copy it in */
305
if (*(++cur_entry) != ':') {
306
temp = current->path[FTP];
307
while (*cur_entry != ':')
308
*temp++ = *cur_entry++;
313
/* And now check for HTTP entry */
314
if (*(++cur_entry) != ':') {
315
temp = current->path[HTTP];
316
while (*cur_entry != ':')
317
*temp++ = *cur_entry++;
322
/* We're done for now */
325
int write_list(FILE *outfile_p, server_t *best, char *dist)
329
/* Make our mark ;) */
330
fprintf(outfile_p, "# sources.list generated by apt-spy %s\n", apt_spy_v);
332
/* Copy URL information */
333
if (best[0].stats.protocol == FTP)
334
strncpy(url, best[0].url[FTP], 100);
336
strncpy(url, best[0].url[HTTP], 100);
338
/* And write the line */
339
fprintf(outfile_p, "deb %s %s main\n", url, dist);
341
/* We also write a deb-src line */
342
fprintf(outfile_p, "deb-src %s %s main\n", url, dist);
344
/* And if we are using "stable", add a security line. Otherwise comment it out.*/
345
if (strcmp(dist, "stable") == 0)
346
fprintf(outfile_p, "deb http://security.debian.org/ stable/updates main\n");
348
fprintf(outfile_p, "#deb http://security.debian.org/ stable/updates main\n");
350
if (ferror(outfile_p) != 0) {
357
int write_top(FILE *infile_p, FILE *outfile_p, server_t *best)
362
while (i < BESTNUMBER) {
364
/* Make sure we're at the beginning */
367
/* Read in a line... */
368
while ((line = next_entry(infile_p)) != NULL)
369
if (strstr(line, best[i].hostname) != NULL) /* Check for hostname */
370
fputs(line, outfile_p); /* if it's there, write to file */
372
if ((ferror(infile_p) != 0) || (ferror(outfile_p) != 0))
379
/* Utility function */
380
char *str_toupper(char *str)
382
while (*str != '\0') {
383
*str = toupper(*str);