summaryrefslogtreecommitdiff
path: root/mimetypes/eval.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2018-01-07 18:56:09 +0100
committerSergey Poznyakoff <gray@gnu.org.ua>2018-01-07 18:56:09 +0100
commit14ec080ad440a76234a89f8d8a058eac6d3fe4eb (patch)
tree5795418b8680a95ca693189f31b6d176292451e8 /mimetypes/eval.c
parente0da047560fcc124522adbb7591a5e29c3e13248 (diff)
downloadfileserv-14ec080ad440a76234a89f8d8a058eac6d3fe4eb.tar.gz
fileserv-14ec080ad440a76234a89f8d8a058eac6d3fe4eb.tar.bz2
Avoid calling perror in libmimetypes
Diffstat (limited to 'mimetypes/eval.c')
-rw-r--r--mimetypes/eval.c83
1 files changed, 44 insertions, 39 deletions
diff --git a/mimetypes/eval.c b/mimetypes/eval.c
index 2c00bcb..4a09567 100644
--- a/mimetypes/eval.c
+++ b/mimetypes/eval.c
@@ -4,6 +4,7 @@
#include <fnmatch.h>
#include <inttypes.h>
#include <ctype.h>
+#include <errno.h>
#include "mtint.h"
static int eval_rule (struct node *root, struct filebuf const *file);
@@ -17,6 +18,19 @@ b_match (union argument *args, struct filebuf const *fb)
return fnmatch (args[0].string.ptr, fb->name, 0) == 0;
}
+static int
+filebuf_seek (struct filebuf const *fb, union argument const *args)
+{
+ if (fseek (fb->fp, args[0].number, SEEK_SET))
+ {
+ mimetypes_error ("fseek(%s,%ul,0)=%s",
+ fb->name, args[0].number,
+ strerror (errno));
+ return -1;
+ }
+ return 0;
+}
+
/* ascii(offset,length)
True if bytes are valid printable ASCII (CR, NL, TAB,
BS, 32-126)
@@ -29,11 +43,8 @@ b_ascii (union argument *args, struct filebuf const *fb)
{
int i;
- if (fseek (fb->fp, args[0].number, SEEK_SET))
- {
- perror ("fseek");//FIXME
- return 0;
- }
+ if (filebuf_seek (fb, args))
+ return 0;
for (i = 0; i < args[1].number; i++)
{
@@ -58,11 +69,8 @@ b_printable (union argument *args, struct filebuf const *fb)
{
int i;
- if (fseek (fb->fp, args[0].number, SEEK_SET))
- {
- perror ("fseek");//FIXME
- return 0;
- }
+ if (filebuf_seek (fb, args))
+ return 0;
for (i = 0; i < args[1].number; i++)
{
@@ -84,11 +92,8 @@ b_string (union argument *args, struct filebuf const *fb)
struct mimetypes_string const *str = &args[1].string;
int i;
- if (fseek (fb->fp, args[0].number, SEEK_SET))
- {
- perror ("fseek");//FIXME
- return 0;
- }
+ if (filebuf_seek (fb, args))
+ return 0;
for (i = 0; i < str->len; i++)
{
@@ -111,11 +116,8 @@ b_istring (union argument *args, struct filebuf const *fb)
int i;
struct mimetypes_string const *str = &args[1].string;
- if (fseek (fb->fp, args[0].number, SEEK_SET))
- {
- perror ("fseek");//FIXME
- return 0;
- }
+ if (filebuf_seek (fb, args))
+ return 0;
for (i = 0; i < str->len; i++)
{
@@ -132,17 +134,17 @@ int
compare_bytes (union argument *args, void *sample, void *buf, size_t size,
struct filebuf const *fb)
{
- if (fseek (fb->fp, args[0].number, SEEK_SET))
- {
- perror ("fseek");//FIXME
- return 0;
- }
+ if (filebuf_seek (fb, args))
+ return 0;
if (fread (buf, size, 1, fb->fp) != 1)
{
if (ferror (fb->fp))
- perror ("fread");//FIXME
- return 0;
+ {
+ mimetypes_error ("fread %zu bytes from %s: %s",
+ size, fb->name, strerror (errno));
+ return 0;
+ }
}
return memcmp (sample, buf, size) == 0;
}
@@ -203,17 +205,21 @@ b_contains (union argument *args, struct filebuf const *fb)
char *buf;
struct mimetypes_string const *str = &args[2].string;
- if (fseek (fb->fp, args[0].number, SEEK_SET))
+ if (filebuf_seek (fb, args))
+ return 0;
+
+ buf = malloc (args[1].number);
+ if (!buf)
{
- perror ("fseek");//FIXME
+ mimetypes_error ("out of memory");
return 0;
}
-
- buf = malloc (args[1].number);
count = fread (buf, 1, args[1].number, fb->fp);
if (count == 0)
{
- //FIXME
+ if (ferror (fb->fp))
+ mimetypes_error ("fread %lu bytes from %s: %s",
+ args[1].number, fb->name, strerror (errno));
}
else if (count > str->len)
for (i = 0; i <= count - str->len; i++)
@@ -236,16 +242,15 @@ b_regex (union argument *args, struct filebuf const *fb)
size_t count;
char buf[MIME_MAX_BUFFER];
- if (fseek (fb->fp, args[0].number, SEEK_SET))
- {
- perror ("fseek"); //FIXME
- return 0;
- }
+ if (filebuf_seek (fb, args))
+ return 0;
count = fread (buf, 1, sizeof buf - 1, fb->fp);
if (count == 0)
{
- //FIXME: check err
+ if (ferror (fb->fp))
+ mimetypes_error ("fread %zu bytes from %s: %s",
+ sizeof buf - 1, fb->name, strerror (errno));
return 0;
}
buf[count] = 0;
@@ -404,7 +409,7 @@ get_file_type (char const *filename)
fb.fp = fopen (filename, "r");
if (fb.fp == NULL)
{
- perror ("fopen");//FIXME
+ mimetypes_error ("can't open %s: %s", filename, strerror (errno));
return NULL;
}

Return to:

Send suggestions and report system problems to the System administrator.