File: file.c

package info (click to toggle)
apt-spy 3.1-19
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 252 kB
  • ctags: 54
  • sloc: ansic: 898; makefile: 51; sh: 12
file content (169 lines) | stat: -rw-r--r-- 3,046 bytes parent folder | download
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
/* 
 * 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"

/* 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);
	sprintf(new_name, "%s.bak", orig_file);

	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;
}