This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfile.c
237 lines (199 loc) · 4.16 KB
/
file.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
/*
* apt-spy (c) Steven Holmes, 2003.
* This software is licensed as detailed in the COPYRIGHT 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"
/**
* @brief Selects an infile
*
* @param infile
*
* @return file pointer of the read access opened file
*/
FILE *select_infile(char *infile)
{
if (infile != NULL)
return fopen(infile, "r");
/* Else return a temporary file */
return tmpfile();
}
/**
* @brief Selects an output file
*
* @param outfile
*
* @return file pointer to the write access opened 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;
}
/**
* @brief Open configuration file
*
* @param config_file
*
* @return
*/
FILE *select_config(char *config_file)
{
FILE *fp;
fp = fopen(config_file, "r");
return fp;
}
FILE *select_mirror(char *mirror_list, int are_updating)
{
FILE *fp;
switch (are_updating) {
case -1:
printf("Creating mirror file: %s\n", mirror_list);
fp = fopen(mirror_list, "w+");
break;
case 0:
fp = fopen(mirror_list, "r");
break;
case 1:
fp = fopen(mirror_list, "w");
break;
default:
return NULL;
break;
}
return fp;
}
/**
* @brief Get the next line of infile_p
*
* @param infile_p
*
* @return The next line
*/
char *next_entry(FILE *infile_p)
{
char *temp = NULL;
/* long int values because ftell returns -1 when it fails */
long int orig_position = 0;
long int buffsize = 0;
int c;
int count;
/* Save original file position */
orig_position = ftell(infile_p);
if (orig_position < 0) {
perror("Input error (ftell)");
exit(1);
}
/* Fast-forward to new line */
/*
* 2008-08-24 Stefano Canepa <[email protected]>
* Modified to return and entry terminating with EOF or \n
*/
count = 0;
if((c = getc(infile_p)) < 0) /* get the first char */
return NULL;
while ((c != '\n') && (c != EOF)) {
count++;
if ((c == EOF) && (count = 1))
{
return NULL; /* We've exhausted the list */
}
c = getc(infile_p);
}
/* Calculate buffer size and check for ftell errors */
buffsize = ftell(infile_p);
if (buffsize < 0) {
perror("Input error (ftell)");
exit(1);
}
buffsize -= orig_position;
/* create storage space for line */
temp = malloc(buffsize + 1);
if (temp == NULL) {
perror("malloc");
exit(1);
}
/* Rewind file to original position and check for errors */
if (fseek(infile_p, orig_position, SEEK_SET) != 0) {
perror("Input error (fseek)");
exit(1);
}
/* We now read the line into the newly created buffer */
if (!fgets(temp, buffsize + 1, infile_p)) {
perror("Input error (fgets)");
exit(1);
}
return temp;
}
/**
* @brief Copy orig_file into a new file named as orig_file with ".bak" appended
*
* @param orig_file
*
* @return 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);
sprintf(new_name, "%s.bak", orig_file);
out_file = fopen(new_name, "w");
if (out_file == NULL){
perror("fopen");
fclose(in_file);
free(new_name);
return 1;
}
/* Do the copying */
while((c = getc(in_file)) != EOF)
putc(c, out_file);
if (ferror(in_file) || ferror(out_file)) {
perror("putc");
fclose(in_file);
fclose(out_file);
free(new_name);
return 1;
}
fclose(in_file);
fclose(out_file);
free(new_name);
return 0;
}