Archived
1
0
Fork 0

Make the other modules use error.c

This commit is contained in:
Henrik Hautakoski 2011-08-04 11:10:08 +02:00
parent 6bc3ffde5d
commit a2f2e58353
7 changed files with 26 additions and 32 deletions

View file

@ -10,9 +10,9 @@ all : $(PROGRAMS)
install : $(PROGRAMS) install : $(PROGRAMS)
cp $^ $(HOME)/bin/ cp $^ $(HOME)/bin/
dlight : dlight.o env.o http.o rss.o lockfile.o filter.o cconf.o dlhist.o dlight : dlight.o env.o http.o rss.o lockfile.o filter.o cconf.o dlhist.o error.o
dlight-compile : compile.o env.o lockfile.o filter.o cconf.o dlight-compile : compile.o env.o lockfile.o filter.o cconf.o error.o
dlight-read-config : read-config.o env.o cconf.o dlight-read-config : read-config.o env.o cconf.o error.o
dlight-% : %.o dlight-% : %.o
$(CC) $(LDFLAGS) -o $@ $^ $(CC) $(LDFLAGS) -o $@ $^

View file

@ -27,12 +27,11 @@
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include "env.h" #include "env.h"
#include "error.h"
#include "cconf.h" #include "cconf.h"
#include "lockfile.h" #include "lockfile.h"
#include "filter.h" #include "filter.h"
#define error(...) fprintf(stderr, "error: " __VA_ARGS__)
#define isalias(x) (isalnum(x) || (x) == '-') #define isalias(x) (isalnum(x) || (x) == '-')
#define MAXNAME 1024 #define MAXNAME 1024
@ -252,8 +251,7 @@ static int parse_target(struct target *target) {
if (!alias) if (!alias)
return -1; return -1;
if (!alias[0] && !dest_table_nr) { if (!alias[0] && !dest_table_nr) {
error("No destination found for target '%s'\n", src); return error("No destination found for target '%s'\n", src);
return -1;
} }
target->src = strdup(src); target->src = strdup(src);

View file

@ -3,14 +3,13 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "env.h" #include "env.h"
#include "error.h"
#include "cconf.h" #include "cconf.h"
#include "dlhist.h" #include "dlhist.h"
#include "filter.h" #include "filter.h"
#include "http.h" #include "http.h"
#include "rss.h" #include "rss.h"
#define error(...) fprintf(stderr, "error: " __VA_ARGS__)
static void process_items(rss_t rss, struct target *t) { static void process_items(rss_t rss, struct target *t) {
int i; int i;
@ -29,8 +28,7 @@ static void process_items(rss_t rss, struct target *t) {
if (http_download_file(item.link, filter->dest) < 0 && if (http_download_file(item.link, filter->dest) < 0 &&
errno != EEXIST) { errno != EEXIST) {
printf("download failed: %s\n", error("download failed: %s\n", strerror(errno));
strerror(errno));
continue; continue;
} }

View file

@ -21,17 +21,18 @@
#include <pcre.h> #include <pcre.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "error.h"
#include "filter.h" #include "filter.h"
static inline pcre* compile(const char *pattern) { static inline pcre* compile(const char *pattern) {
const char *error; const char *err;
int eoffset; int eoffset;
pcre *regex; pcre *regex;
regex = pcre_compile(pattern, 0, &error, &eoffset, NULL); regex = pcre_compile(pattern, 0, &err, &eoffset, NULL);
if (!regex) { if (!regex) {
fprintf(stderr, "Error compiling expression\n"); error("Error compiling expression\n");
return NULL; return NULL;
} }
@ -48,14 +49,14 @@ static inline int match(pcre *pcre, const char *subject) {
int filter_check_syntax(const char *pattern) { int filter_check_syntax(const char *pattern) {
const char *error; const char *err;
int eoffset; int eoffset;
pcre *regex; pcre *regex;
regex = pcre_compile(pattern, 0, &error, &eoffset, NULL); regex = pcre_compile(pattern, 0, &err, &eoffset, NULL);
if (!regex) { if (!regex) {
fprintf(stderr, "filter: error in expression '%s': %s\n", error("filter: error in expression '%s': %s\n",
pattern, error); pattern, err);
return 0; return 0;
} }
return 1; return 1;

7
http.c
View file

@ -25,6 +25,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include "error.h"
#include "http.h" #include "http.h"
static char* strnstrr(const char *str, const char *needle, size_t size) { static char* strnstrr(const char *str, const char *needle, size_t size) {
@ -109,7 +110,7 @@ static size_t write_cb(void *src, size_t smemb, size_t nmemb, void *data) {
dest->block = realloc(dest->block, dest->len + size); dest->block = realloc(dest->block, dest->len + size);
if (dest->block == NULL) { if (dest->block == NULL) {
printf("out of memory\n"); error("out of memory\n");
return 0; return 0;
} }
memcpy(dest->block + dest->len, src, size); memcpy(dest->block + dest->len, src, size);
@ -148,7 +149,7 @@ struct http_data* http_fetch_page(const char *url) {
res = curl_easy_perform(handle); res = curl_easy_perform(handle);
if (res != CURLE_OK) { if (res != CURLE_OK) {
printf("curl: (%s) %s\n", url, curl_easy_strerror(res)); error("curl: (%s) %s\n", url, curl_easy_strerror(res));
goto error; goto error;
} }
@ -184,7 +185,7 @@ int http_download_file(const char *url, const char *dir) {
res = curl_easy_perform(handle); res = curl_easy_perform(handle);
if (res != CURLE_OK) { if (res != CURLE_OK) {
printf("curl: (%s) %s\n", url, curl_easy_strerror(res)); error("curl: (%s) %s\n", url, curl_easy_strerror(res));
goto error; goto error;
} }

View file

@ -26,7 +26,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include "error.h"
#include "lockfile.h" #include "lockfile.h"
#define locked(x) ((x)->fd >= 0) #define locked(x) ((x)->fd >= 0)
@ -95,13 +95,9 @@ int hold_lock(struct lockfile *lock, const char *filename, int force) {
lock->fd = open(lock->name, mask, 0600); lock->fd = open(lock->name, mask, 0600);
if (lock->fd < 0) { if (lock->fd < 0) {
if (errno == EEXIST) { return error(errno == EEXIST ?
fprintf(stderr, "'%s' is locked\n", lock->name); "'%s' is locked\n" : "unable to create lockfile '%s'",
} else { lock->name);
fprintf(stderr, "unable to create lockfile '%s'",
lock->name);
}
return -1;
} }
lock->next = active_locks; lock->next = active_locks;
active_locks = lock; active_locks = lock;

View file

@ -22,8 +22,9 @@
#include <string.h> #include <string.h>
#include "cconf.h" #include "cconf.h"
#include "env.h" #include "env.h"
#include "error.h"
static char *usage = "dlight-read-config [ <file> | -h ]\n"; static char *usagestr = "dlight-read-config [ <file> | -h ]";
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -33,8 +34,7 @@ int main(int argc, char **argv) {
if (argc > 1) { if (argc > 1) {
if (!strcmp(argv[1], "-h")) { if (!strcmp(argv[1], "-h")) {
fprintf(stderr, usage); usage(usagestr);
return 1;
} }
strncpy(file, argv[1], sizeof(file)); strncpy(file, argv[1], sizeof(file));
} else { } else {