summaryrefslogtreecommitdiff
path: root/usr.bin/file/fsmagic.c
diff options
context:
space:
mode:
authorCharles Longeau <chl@cvs.openbsd.org>2009-04-24 18:54:35 +0000
committerCharles Longeau <chl@cvs.openbsd.org>2009-04-24 18:54:35 +0000
commit8dcb5d1d2940d843dabdcef31bc8cb2dba817119 (patch)
tree7458bf3b9120705a836b9e56b7925dce2cf42159 /usr.bin/file/fsmagic.c
parenta75d7de4707e3d43e0e869059021172cadecca54 (diff)
file update to 4.24
The '-i' switch is now enabled so file(1) can output mime type strings. ok ian@ builk ports build test on amd64 by jasper@ ok ray@ gilles@ on a almost identical diff builk ports build test on sparc64 on this almost identical diff by ajacoutot@ also tested by landry@
Diffstat (limited to 'usr.bin/file/fsmagic.c')
-rw-r--r--usr.bin/file/fsmagic.c70
1 files changed, 35 insertions, 35 deletions
diff --git a/usr.bin/file/fsmagic.c b/usr.bin/file/fsmagic.c
index c62428b6b6a..74f1349d331 100644
--- a/usr.bin/file/fsmagic.c
+++ b/usr.bin/file/fsmagic.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fsmagic.c,v 1.12 2008/05/08 01:40:56 chl Exp $ */
+/* $OpenBSD: fsmagic.c,v 1.13 2009/04/24 18:54:34 chl Exp $ */
/*
* Copyright (c) Ian F. Darwin 1986-1995.
* Software written by Ian F. Darwin and others;
@@ -58,13 +58,31 @@
#undef HAVE_MAJOR
#ifndef lint
-FILE_RCSID("@(#)$Id: fsmagic.c,v 1.12 2008/05/08 01:40:56 chl Exp $")
+FILE_RCSID("@(#)$Id: fsmagic.c,v 1.13 2009/04/24 18:54:34 chl Exp $")
#endif /* lint */
+private int
+bad_link(struct magic_set *ms, int err, char *buf)
+{
+ char *errfmt;
+ if (err == ELOOP)
+ errfmt = "symbolic link in a loop";
+ else
+ errfmt = "broken symbolic link to `%s'";
+ if (ms->flags & MAGIC_ERROR) {
+ file_error(ms, err, errfmt, buf);
+ return -1;
+ }
+ if (file_printf(ms, errfmt, buf) == -1)
+ return -1;
+ return 1;
+}
+
protected int
file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
{
int ret = 0;
+ int mime = ms->flags & MAGIC_MIME;
#ifdef S_IFLNK
char buf[BUFSIZ+4];
int nch;
@@ -96,11 +114,12 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
return 1;
}
- if ((ms->flags & MAGIC_MIME) != 0) {
+ if (mime) {
if ((sb->st_mode & S_IFMT) != S_IFREG) {
- if (file_printf(ms, "application/x-not-regular-file")
+ if ((mime & MAGIC_MIME_TYPE) &&
+ file_printf(ms, "application/x-not-regular-file")
== -1)
- return -1;
+ return -1;
return 1;
}
}
@@ -136,7 +155,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
*/
if ((ms->flags & MAGIC_DEVICES) != 0)
break;
-#ifdef HAVE_ST_RDEV
+#ifdef HAVE_STAT_ST_RDEV
# ifdef dv_unit
if (file_printf(ms, "character special (%d/%d/%d)",
major(sb->st_rdev), dv_unit(sb->st_rdev),
@@ -162,7 +181,7 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
*/
if ((ms->flags & MAGIC_DEVICES) != 0)
break;
-#ifdef HAVE_ST_RDEV
+#ifdef HAVE_STAT_ST_RDEV
# ifdef dv_unit
if (file_printf(ms, "block special (%d/%d/%d)",
major(sb->st_rdev), dv_unit(sb->st_rdev),
@@ -208,23 +227,13 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
return -1;
return 1;
}
- buf[nch] = '\0'; /* readlink(2) forgets this */
+ buf[nch] = '\0'; /* readlink(2) does not do this */
/* If broken symlink, say so and quit early. */
if (*buf == '/') {
- if (stat(buf, &tstatbuf) < 0) {
- if (ms->flags & MAGIC_ERROR) {
- file_error(ms, errno,
- "broken symbolic link to `%s'", buf);
- return -1;
- }
- if (file_printf(ms, "broken symbolic link to `%s'",
- buf) == -1)
- return -1;
- return 1;
- }
- }
- else {
+ if (stat(buf, &tstatbuf) < 0)
+ return bad_link(ms, errno, buf);
+ } else {
char *tmp;
char buf2[BUFSIZ+BUFSIZ+4];
@@ -247,18 +256,8 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
(void)strlcat(buf2, buf, sizeof buf2); /* plus (rel) link */
tmp = buf2;
}
- if (stat(tmp, &tstatbuf) < 0) {
- if (ms->flags & MAGIC_ERROR) {
- file_error(ms, errno,
- "broken symbolic link to `%s'",
- buf);
- return -1;
- }
- if (file_printf(ms,
- "broken symbolic link to `%s'", buf) == -1)
- return -1;
- return 1;
- }
+ if (stat(tmp, &tstatbuf) < 0)
+ return bad_link(ms, errno, buf);
}
/* Otherwise, handle it. */
@@ -304,8 +303,9 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
* when we read the file.)
*/
if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
- if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
- "application/x-empty" : "empty") == -1)
+ if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
+ file_printf(ms, mime ? "application/x-empty" :
+ "empty") == -1)
return -1;
return 1;
}