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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
/*
* apt-spy (c) Steven Holmes, 2003.
* This software is licensed as detailed in the COPYRIGHT file
*/
/* This file is a bit icky */
char *str_toupper(char *str);
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "include/global.h"
#include "include/parse.h"
#include "include/file.h"
int build_area_file(FILE *config_p, FILE *infile_p, FILE *mirror_list, char *area)
{
char *line; /* Where we read the lines into */
char *tmp; /* Temp. pointer */
char *inputline; /* The line that will be written to config_p */
char *country_code; /* where we put the country code */
/* Uppercase area */
str_toupper(area);
/* EWWWWWWW */
while((line = next_entry(config_p)) != NULL) {
/* Uppercase line */
str_toupper(line);
/* test for area string */
if ((tmp = strstr(line, area)) != NULL) {
if ((strchr(tmp,':')) == NULL) { /* And for trailing colon.. */
free(line);
continue; /* .. it's not there */
}
break;
}
free(line);
if (ferror(config_p) != 0) /* Check for File error */
return 1;
}
/* No match. Return. */
if (line == NULL) {
fprintf(stderr, "Could not find area named %s\n", area);
return 1;
}
free(line);
/* We now have the "label" line. The country list begins on the next line. */
while ((line = next_entry(config_p)) != NULL) {
if (ferror(config_p) != 0) { /* Check for file error */
free(line);
return 1;
}
/* Skip blank lines */
country_code = line;
/* We check for either a non-space or a '\n'. Has the useful side-effect of */
/* fast-forwarding country_code past any preceding whitespace.*/
while ((*country_code != '\n') && (isspace(*country_code) != 0))
++country_code;
/* If country_code points to a '\n', there were no other characters. It was a blank line. */
/* If it points to a '#', there is a comment. We skip it too. */
if ((*country_code == '\n') || (*country_code == '#'))
continue;
if ((strchr(line, ':')) != NULL)
return 0; /* End of list. Return. */
/* We do a little fiddling to get the country code down to 2 letters and a space */
*(country_code + 2) = ' ';
*(country_code + 3) = '\0';
/* Sigh. The country code is in "country_code". We now parse the mirrors file for this */
/* and set the file position so that the next read will return the first */
/* mirror. */
if (find_country(mirror_list, country_code) == 1) {
fprintf(stderr, "Couldn't find country %s. Skipping.\n", country_code);
free(line);
continue;
}
/* The next read of infile_p will return the first mirror entry. We parse */
/* this and build a line to put into the temporary file. */
while ((inputline = get_mirrors(mirror_list)) != NULL) {
/* We now write the line to the temporary file */
fputs(inputline, infile_p);
free(inputline);
if ((ferror(infile_p)) != 0) { /* Check for file error */
free(line);
return 1;
}
}
free(line);
}
return 0;
}
int build_country_file(FILE *config_p, FILE *infile_p, FILE *mirror_list, char *country_list)
{
char *country_code;
char *p, *q;
char *inputline;
int found = 0;
/* Upper-case country list */
str_toupper(country_list);
/* A cheap way to make sure we have enough space */
country_code = malloc(strlen(country_list));
p = country_list;
while (*p != '\0') {
/* Reset country code pointer */
q = country_code;
/* Skip white space */
while (isspace(*p))
++p;
/* Copy up until end or comma */
while ((*p != '\0') && (*p != ',') && (isspace(*p) == 0))
*q++ = *p++;
/* Skip more white space. *sigh* */
while (isspace(*p))
++p;
/* skip past comma */
if (*p != '\0')
++p;
/* String-ify */
*q++ = ' ';
*q = '\0';
/* And find the country/build the file */
if (find_country(mirror_list, country_code) == 1) {
fprintf(stderr, "Couldn't find country %s. Skipping.\n", country_code);
continue;
}
found = 1;
while ((inputline = get_mirrors(mirror_list)) != NULL) {
fputs(inputline, infile_p);
free(inputline);
if (ferror(infile_p)) {
free(country_code);
return 1;
}
}
}
free(country_code);
/* Check we have found at least one country */
if (found == 0)
return 1;
else
return 0;
}
int find_country(FILE *mirror_list, char *country_code)
{
char *line, *cc;
/* Make sure we're at beginning of file */
rewind(mirror_list);
/* This is a hack to allow users to specify "UK" instead of "GB" */
if (!strcmp(country_code, "UK "))
strncpy(country_code, "GB ", 4);
/* Read until we find the country code */
while ((line = next_entry(mirror_list)) != NULL) {
cc = strstr(line, country_code);
if (cc == NULL)
continue;
/* Skip white space */
while (isspace(*line))
++line;
/* Country code should be first two characters on line. */
if (cc == line)
break;
}
if (line == NULL)
return 1;
next_entry(mirror_list); /* Skip a line */
if (ferror(mirror_list)) {
free(line);
return 1;
}
return 0; /* We're positioned nicely for the next read */
}
/* line format is "server:ftp-path:http-path". */
char *get_mirrors(FILE *mirror_list)
{
char *line, *save_line;
char *creation, *save_creation;
int counter = 0;
int len;
/* First, we read in a line from the file */
save_line = line = next_entry(mirror_list);
/* Allocate space for creation */
len=5+strlen(line);
save_creation = creation = malloc(len);
if (creation == NULL) {
perror("malloc");
exit(1);
}
/* test for file error */
if (ferror(mirror_list)) {
perror("fopen");
free(save_creation);
return NULL;
}
/* If the line begins with a space, we assume it is empty and the list is exhausted. */
if (isspace(*line) != 0) {
free(save_creation);
return NULL;
}
/* We now read the server name into "creation" */
while (isspace(*line) == 0)
*creation++ = *line++;
/* And add a colon, which is the field seperator */
*creation++ = ':';
/* We skip over whitespace. If there is a lot of whitespace, we assume there is no FTP entry. */
while ((isspace(*line) != 0) && (counter < 30)) {
++line;
++counter;
}
/* Check if there is an entry or just more space */
while (isspace(*line) == 0)
*creation++ = *line++;
*creation++ = ':';
/* We now check for an HTTP entry */
while (*line != '\n') {
if (isspace(*line) == 0)
break;
line++;
}
while (isspace(*line) == 0)
*creation++ = *line++;
*creation++ = ':';
*creation++ = '\n';
*creation++ = '\0';
free(save_line);
return save_creation;
}
void tokenise(server_t *current, char *cur_entry)
{
char *temp; /* We use this for temporary string-pointing :P */
static char null_string[]="";
/* We initialise the structure to 0 */
memset(current, 0, sizeof(*current));
/* First, we copy the server name into the struct. */
current->hostname=malloc(strlen(cur_entry));
if (!current->hostname) {
perror("malloc");
exit(1);
}
temp = current->hostname;
while (*cur_entry != ':')
*temp++ = *cur_entry++;
*temp++ = '\0'; /* Turn into string */
current->hostname=realloc(current->hostname, 1+strlen(current->hostname));
if (!current->hostname) {
perror("realloc");
exit(1);
}
/* We now check for an ftp entry and copy it in */
if (*(++cur_entry) != ':') {
current->path[FTP]=malloc(strlen(cur_entry));
if (!current->path[FTP]) {
perror("malloc");
exit(1);
}
temp = current->path[FTP];
while (*cur_entry != ':')
*temp++ = *cur_entry++;
*temp++ = '\0';
current->path[FTP]=realloc(current->path[FTP], 1+strlen(current->path[FTP]));
if (!current->path[FTP]) {
perror("malloc");
exit(1);
}
} else current->path[FTP]=null_string;
/* And now check for HTTP entry */
if (*(++cur_entry) != ':') {
current->path[HTTP]=malloc(strlen(cur_entry));
if (!current->path[HTTP]) {
perror("malloc");
exit(1);
}
temp = current->path[HTTP];
while (*cur_entry != ':')
*temp++ = *cur_entry++;
*temp++ = '\0';
current->path[HTTP]=realloc(current->path[HTTP], 1+strlen(current->path[HTTP]));
if (!current->path[HTTP]) {
perror("realloc");
exit(1);
}
} else current->path[HTTP]=null_string;
/* We're done for now */
}
int write_list(FILE *outfile_p, server_t *best, char *dist)
{
char *url;
/* Make our mark ;) */
fprintf(outfile_p, "# sources.list generated by apt-spy %s\n", apt_spy_v);
/* Copy URL information */
if (best[0].stats.protocol == FTP) url=best[0].url[FTP];
else {
assert(best[0].stats.protocol == HTTP);
url=best[0].url[HTTP];
}
/* And write the line */
fprintf(outfile_p, "deb %s %s main\n", url, dist);
/* We also write a deb-src line */
fprintf(outfile_p, "deb-src %s %s main\n", url, dist);
/* And if we are using "stable", add a security line. Otherwise comment it out.*/
if (strcmp(dist, "stable") == 0)
fprintf(outfile_p, "deb http://security.debian.org/ stable/updates main\n");
else
fprintf(outfile_p, "#deb http://security.debian.org/ stable/updates main\n");
if (ferror(outfile_p) != 0) {
perror("fprintf");
return 1;
}
return 0;
}
int write_top(FILE *infile_p, FILE *outfile_p, server_t *best)
{
int i = 0;
char *line;
while (i < BESTNUMBER) {
/* Make sure we're at the beginning */
rewind(infile_p);
/* Read in a line... */
while ((line = next_entry(infile_p)) != NULL) {
if (strstr(line, best[i].hostname) != NULL) { /* Check for hostname */
fputs(line, outfile_p); /* if it's there, write to file */
break;
}
}
if ((ferror(infile_p) != 0) || (ferror(outfile_p) != 0))
return 1;
++i;
}
return 0;
}
/* Utility function */
char *str_toupper(char *str)
{
while (*str != '\0') {
*str = toupper(*str);
str++;
}
return str;
}
|