-/* $OpenBSD: file.c,v 1.29 2015/04/24 16:30:06 nicm Exp $ */
+/* $OpenBSD: file.c,v 1.30 2015/04/24 16:47:32 nicm Exp $ */
/*
* Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org>
int
main(int argc, char **argv)
{
- struct input_file *files = NULL;
- int nfiles, opt, i, width = 0;
+ struct input_file inf;
+ int opt, i, width = 0;
FILE *f;
struct magic *m;
char *home, *path;
} else if (argc == 0)
usage();
- nfiles = argc;
- if (nfiles != 0) {
- files = xcalloc(nfiles, sizeof *files);
- for (i = 0; i < argc; i++)
- open_file(&files[i], argv[i], &width);
- }
-
home = getenv("HOME");
if (home == NULL || *home == '\0') {
pw = getpwuid(getuid());
if (f == NULL)
err(1, "%s", path);
- if (geteuid() == 0) {
- pw = getpwnam(FILE_USER);
- if (pw == NULL)
- errx(1, "unknown user %s", FILE_USER);
- if (setgroups(1, &pw->pw_gid) != 0)
- err(1, "setgroups");
- if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
- err(1, "setresgid");
- if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0)
- err(1, "setresuid");
- }
-
m = magic_load(f, path, cflag || Wflag);
if (cflag) {
magic_dump(m);
exit(0);
}
- for (i = 0; i < nfiles; i++)
- test_file(m, &files[i], width);
+ for (i = 0; i < argc; i++) {
+ memset(&inf, 0, sizeof inf);
+ open_file(&inf, argv[i], &width);
+ test_file(m, &inf, width);
+ }
exit(0);
}
}
if (got == 0)
break;
- next = (char*)next + got;
+ next = (char *)next + got;
left -= got;
}
printf("%s\n", inf->result);
else
printf("%-*s %s\n", width, inf->label, inf->result);
+ free(inf->result);
if (inf->mapped && inf->base != NULL)
munmap(inf->base, inf->size);
inf->base = NULL;
- free(inf->result);
+ if (inf->fd != -1)
+ close(inf->fd);
+ free((void *)inf->label);
+ free((void *)inf->path);
}
-/* $OpenBSD: file.h,v 1.25 2015/04/24 16:24:11 nicm Exp $ */
+/* $OpenBSD: file.h,v 1.26 2015/04/24 16:47:32 nicm Exp $ */
/*
* Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org>
/* Bytes to read if can't use the whole file. */
#define FILE_READ_SIZE (256 * 1024)
-/* User to drop to if run as root. */
-#define FILE_USER "nobody"
-
/* text.c */
const char *text_get_type(const void *, size_t);
const char *text_try_words(const void *, size_t, int);