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
|
/*
* apt-spy (c) Steven Holmes, 2003.
* This software is licensed as detailed in the LICENSE file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "include/global.h"
#include "include/file.h"
/* Selects an infile */
FILE *select_infile(char *infile)
{
if (infile != NULL)
return fopen(infile, "r");
/* Else return a temporary file */
return tmpfile();
}
/* Selects an output file */
FILE *select_outfile(char *outfile)
{
FILE *fp;
/* Check if we've to use standard output... */
if (strcmp(outfile, "-") == 0)
return stdout;
/* Otherwise, we open the file */
else {
if (backup(outfile) == 1) /* backup old file */
return NULL;
fp = fopen(outfile, "w");
}
return fp;
}
/* Open configuration file */
FILE *select_config(char *config_file)
{
FILE *fp;
if (config_file == NULL)
config_file = d_config;
fp = fopen(config_file, "r");
return fp;
}
FILE *select_mirror(char *mirror_list, int are_updating)
{
FILE *fp;
if (mirror_list == NULL)
mirror_list = d_mirror;
if (are_updating == 1)
fp = fopen(mirror_list, "w");
else
fp = fopen(mirror_list, "r");
return fp;
}
char *next_entry(FILE *infile_p)
{
char *temp;
unsigned long orig_position, buffsize;
int c;
/* Save original file position */
orig_position = ftell(infile_p);
/* Fast-forward to new line */
while ((c = getc(infile_p)) != '\n') {
if (c == EOF)
{
return NULL; /* We've exhausted the list */
}
}
buffsize = ftell(infile_p) - orig_position; /* Calculate buffer size */
/* create storage space for line */
temp = malloc(buffsize + 1);
if (temp == NULL) {
perror("malloc");
exit(1);
}
/* Rewind file to original position */
fseek(infile_p, orig_position, SEEK_SET);
/* We now read the line into the newly created buffer */
fgets(temp, buffsize + 1, infile_p);
return temp;
}
/* If orig_file exists, copy it to a new file with ".bak" appended */
/* Upon error, "1" is returned*/
int backup(char *orig_file)
{
FILE *in_file, *out_file;
char *new_name;
int c, namesize;
struct stat file_stat;
if (stat(orig_file, &file_stat) == -1) {
if (errno == ENOENT)
return 0; /* doesn't exist */
else {
perror("stat"); /* or there's been an error */
return 1;
}
}
if (file_stat.st_size == 0)
return 0; /* Don't backup empty file */
in_file = fopen(orig_file, "r");
if (in_file == NULL) {
perror("fopen"); /* eep! */
return 1;
}
/* Saves multiple calls to strlen() later */
namesize = strlen(orig_file);
/* We need to create the new filename. */
new_name = malloc(namesize + 5);
strncpy(new_name, orig_file, namesize + 1);
/* We append ".bak" to the end of the backup file */
strncat(new_name, ".bak", namesize + 5);
out_file = fopen(new_name, "w");
if (out_file == NULL)
return 1;
/* Do the copying */
while((c = getc(in_file)) != EOF)
putc(c, out_file);
if (ferror(in_file) || ferror(out_file)) {
perror("putc");
return 1;
}
fclose(in_file);
fclose(out_file);
free(new_name);
return 0;
}
int check_write_access(char *path)
{
/* Thanks to Brian from warner-debbugs@lothar.com for pointing out and
fixing the programming error here. */
char *dir = strdup(path); /* Leak it, it's small */
if (access(path, R_OK | W_OK) == -1) {
if (access(dirname(dir), R_OK | W_OK) == -1) {
perror("access");
return 1;
}
else if (errno == ENOENT)
return 0; /* Doesn't exist */
else {
perror("access"); /* Error */
return 1;
}
}
return 0;
}
|