projects
/
mymail.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
bdd9b1a
)
Added safe_fopen.
author
Francois Fleuret
<francois@fleuret.org>
Tue, 12 Feb 2013 08:01:22 +0000
(09:01 +0100)
committer
Francois Fleuret
<francois@fleuret.org>
Tue, 12 Feb 2013 08:01:22 +0000
(09:01 +0100)
mymail.c
patch
|
blob
|
history
diff --git
a/mymail.c
b/mymail.c
index
5fb92be
..
fd200a1
100644
(file)
--- a/
mymail.c
+++ b/
mymail.c
@@
-182,18
+182,16
@@
char *default_value(char *current_value,
}
}
}
}
-/********************************************************************/
-
-/* malloc with error checking. */
-
-void *safe_malloc(size_t n) {
- void *p = malloc(n);
- if(!p && n != 0) {
+FILE *safe_fopen(const char *path, const char *mode, const char *comment) {
+ FILE *result = fopen(path, mode);
+ if(result) {
+ return result;
+ } else {
fprintf(stderr,
fprintf(stderr,
- "mymail: cannot allocate memory: %s\n", strerror(errno));
+ "mymail: Cannot open file '%s' (%s) with mode \"%s\".\n",
+ path, comment, mode);
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
- return p;
}
/*********************************************************************/
}
/*********************************************************************/
@@
-327,14
+325,7
@@
void update_body_hits(char *mail_filename, int position_in_mail,
nb_body_hits = 0;
header = 1;
nb_body_hits = 0;
header = 1;
- mail_file = fopen(mail_filename, "r");
-
- if(!mail_file) {
- fprintf(stderr,
- "mymail: Cannot open mbox '%s' for body scan.\n",
- mail_filename);
- exit(EXIT_FAILURE);
- }
+ mail_file = safe_fopen(mail_filename, "r", "mbox for body scan");
fseek(mail_file, position_in_mail, SEEK_SET);
fseek(mail_file, position_in_mail, SEEK_SET);
@@
-371,15
+362,7
@@
void extract_mail(const char *mail_filename, unsigned long int position_in_mail,
char raw_mbox_line[BUFFER_SIZE];
FILE *mail_file;
char raw_mbox_line[BUFFER_SIZE];
FILE *mail_file;
- mail_file = fopen(mail_filename, "r");
-
- if(!mail_file) {
- fprintf(stderr,
- "mymail: Cannot open mbox '%s' for mail extraction.\n",
- mail_filename);
- exit(EXIT_FAILURE);
- }
-
+ mail_file = safe_fopen(mail_filename, "r", "mbox for mail extraction");
fseek(mail_file, position_in_mail, SEEK_SET);
if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
fseek(mail_file, position_in_mail, SEEK_SET);
if(fgets(raw_mbox_line, BUFFER_SIZE, mail_file)) {
@@
-419,15
+402,7
@@
int search_in_db(const char *db_filename,
fflush(stdout);
}
fflush(stdout);
}
- db_file = fopen(db_filename, "r");
-
- if(!db_file) {
- fprintf(stderr,
- "mymail: Cannot open \"%s\" for reading: %s\n",
- db_filename,
- strerror(errno));
- exit(EXIT_FAILURE);
- }
+ db_file = safe_fopen(db_filename, "r", "index file for search");
/* First, check the db file leading line integrity */
/* First, check the db file leading line integrity */
@@
-603,13
+578,7
@@
void index_mbox(const char *mbox_filename,
int in_header, new_header;
unsigned long int position_in_file;
int in_header, new_header;
unsigned long int position_in_file;
- file = fopen(mbox_filename, "r");
-
- if(!file) {
- fprintf(stderr, "mymail: Cannot open '%s'.\n", mbox_filename);
- if(paranoid) { exit(EXIT_FAILURE); }
- return;
- }
+ file = safe_fopen(mbox_filename, "r", "mbox for indexing");
in_header = 0;
new_header = 0;
in_header = 0;
new_header = 0;
@@
-1072,15
+1041,7
@@
int main(int argc, char **argv) {
mbox_filename_regexp = 0;
}
mbox_filename_regexp = 0;
}
- db_file = fopen(db_filename, "w");
-
- if(!db_file) {
- fprintf(stderr,
- "mymail: Cannot open \"%s\" for writing: %s\n",
- db_filename,
- strerror(errno));
- exit(EXIT_FAILURE);
- }
+ db_file = safe_fopen(db_filename, "w", "index file for indexing");
for(f = 0; f < nb_fields_to_parse; f++) {
if(regcomp(&fields_to_parse[f].regexp,
for(f = 0; f < nb_fields_to_parse; f++) {
if(regcomp(&fields_to_parse[f].regexp,
@@
-1123,15
+1084,7
@@
int main(int argc, char **argv) {
int nb_extracted_mails = 0;
if(output_filename[0]) {
int nb_extracted_mails = 0;
if(output_filename[0]) {
- output_file = fopen(output_filename, "w");
-
- if(!output_file) {
- fprintf(stderr,
- "mymail: Cannot open result file \"%s\" for writing: %s\n",
- output_filename,
- strerror(errno));
- exit(EXIT_FAILURE);
- }
+ output_file = safe_fopen(output_filename, "w", "result mbox");
} else {
output_file = stdout;
quiet = 1;
} else {
output_file = stdout;
quiet = 1;