summaryrefslogtreecommitdiff
path: root/src/apprentice.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/apprentice.c')
-rw-r--r--src/apprentice.c54
1 files changed, 30 insertions, 24 deletions
diff --git a/src/apprentice.c b/src/apprentice.c
index ecbb8b8d..e12efa31 100644
--- a/src/apprentice.c
+++ b/src/apprentice.c
@@ -32,7 +32,7 @@
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.167 2011/03/15 22:15:30 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.168 2011/03/20 20:36:52 christos Exp $")
#endif /* lint */
#include "magic.h"
@@ -672,36 +672,38 @@ private void
load_1(struct magic_set *ms, int action, const char *fn, int *errs,
struct magic_entry **marray, uint32_t *marraycount)
{
- char line[BUFSIZ];
- size_t lineno = 0;
+ size_t lineno = 0, llen = 0;
+ char *line = NULL;
+ ssize_t len;
+
FILE *f = fopen(ms->file = fn, "r");
if (f == NULL) {
if (errno != ENOENT)
file_error(ms, errno, "cannot read magic file `%s'",
fn);
(*errs)++;
- } else {
- /* read and parse this file */
- for (ms->line = 1;
- fgets(line, CAST(int, sizeof(line)), f) != NULL;
- ms->line++) {
- size_t len;
- len = strlen(line);
- if (len == 0) /* null line, garbage, etc */
- continue;
- if (line[len - 1] == '\n') {
- lineno++;
- line[len - 1] = '\0'; /* delete newline */
- }
- if (line[0] == '\0') /* empty, do not parse */
- continue;
- if (line[0] == '#') /* comment, do not parse */
- continue;
- if (line[0] == '!' && line[1] == ':') {
+ return;
+ }
+
+ /* read and parse this file */
+ for (ms->line = 1; (len = getline(&line, &llen, f)) != -1;
+ ms->line++) {
+ if (len == 0) /* null line, garbage, etc */
+ continue;
+ if (line[len - 1] == '\n') {
+ lineno++;
+ line[len - 1] = '\0'; /* delete newline */
+ }
+ switch (line[0]) {
+ case '\0': /* empty, do not parse */
+ case '#': /* comment, do not parse */
+ continue;
+ case '!':
+ if (line[1] == ':') {
size_t i;
for (i = 0; bang[i].name != NULL; i++) {
- if (len - 2 > bang[i].len &&
+ if ((size_t)(len - 2) > bang[i].len &&
memcmp(bang[i].name, line + 2,
bang[i].len) == 0)
break;
@@ -727,13 +729,17 @@ load_1(struct magic_set *ms, int action, const char *fn, int *errs,
}
continue;
}
+ /*FALLTHROUGH*/
+ default:
if (parse(ms, marray, marraycount, line, lineno,
action) != 0)
(*errs)++;
+ break;
}
-
- (void)fclose(f);
}
+ if (line)
+ free(line);
+ (void)fclose(f);
}
/*