NetBSD/usr.bin/file/ascmagic.c

127 lines
3.5 KiB
C
Raw Normal View History

1997-10-18 18:53:48 +04:00
/* $NetBSD: ascmagic.c,v 1.11 1997/10/18 14:53:50 lukem Exp $ */
1997-01-09 23:18:21 +03:00
1993-03-21 12:45:37 +03:00
/*
1995-05-21 04:13:24 +04:00
* ASCII magic -- file types that we know based on keywords
1993-03-21 12:45:37 +03:00
* that can appear anywhere in the file.
*
* Copyright (c) Ian F. Darwin, 1987.
* Written by Ian F. Darwin.
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or of the Regents of the University of California.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. The author is not responsible for the consequences of use of this
* software, no matter how awful, even if they arise from flaws in it.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Since few users ever read sources,
* credits must appear in the documentation.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software. Since few users
* ever read sources, credits must appear in the documentation.
*
* 4. This notice may not be removed or altered.
*/
#include <stdio.h>
#include <string.h>
1993-03-21 12:45:37 +03:00
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
#include "file.h"
#include "names.h"
1997-10-18 18:53:48 +04:00
#include <sys/cdefs.h>
1995-03-26 01:35:28 +03:00
#ifndef lint
1997-10-18 18:53:48 +04:00
__RCSID("$NetBSD: ascmagic.c,v 1.11 1997/10/18 14:53:50 lukem Exp $");
1995-03-26 01:35:28 +03:00
#endif /* lint */
1993-03-21 12:45:37 +03:00
/* an optimisation over plain strcmp() */
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
int
ascmagic(buf, nbytes)
unsigned char *buf;
int nbytes; /* size actually read */
1993-03-21 12:45:37 +03:00
{
1995-04-30 23:39:37 +04:00
int i, has_escapes = 0;
unsigned char *s;
char nbuf[HOWMANY+1]; /* one extra for terminating '\0' */
char *token;
1993-03-21 12:45:37 +03:00
register struct names *p;
1995-03-26 01:35:28 +03:00
/*
* Do the tar test first, because if the first file in the tar
* archive starts with a dot, we can confuse it with an nroff file.
*/
switch (is_tar(buf, nbytes)) {
case 1:
ckfputs("tar archive", stdout);
return 1;
case 2:
ckfputs("POSIX tar archive", stdout);
return 1;
}
1993-03-21 12:45:37 +03:00
/*
* for troff, look for . + letter + letter or .\";
1993-03-21 12:45:37 +03:00
* this must be done to disambiguate tar archives' ./file
* and other trash from real troff input.
*/
if (*buf == '.') {
unsigned char *tp = buf + 1;
while (isascii(*tp) && isspace(*tp))
++tp; /* skip leading whitespace */
if ((isascii(*tp) && (isalnum(*tp) || *tp=='\\') &&
1995-04-28 23:23:38 +04:00
isascii(tp[1]) && (isalnum(tp[1]) || tp[1] == '"'))) {
ckfputs("troff or preprocessor input text", stdout);
return 1;
}
1993-03-21 12:45:37 +03:00
}
if ((*buf == 'c' || *buf == 'C') &&
1995-04-28 23:23:38 +04:00
isascii(buf[1]) && isspace(buf[1])) {
1993-03-21 12:45:37 +03:00
ckfputs("fortran program text", stdout);
return 1;
}
1997-01-28 03:49:36 +03:00
/* Make sure we are dealing with ascii text before looking for tokens */
for (i = 0; i < nbytes; i++) {
if (!isascii(buf[i]))
return 0; /* not all ASCII */
}
1993-03-21 12:45:37 +03:00
/* look for tokens from names.h - this is expensive! */
/* make a copy of the buffer here because strtok() will destroy it */
1995-03-26 01:35:28 +03:00
s = (unsigned char*) memcpy(nbuf, buf, nbytes);
s[nbytes] = '\0';
has_escapes = (memchr(s, '\033', nbytes) != NULL);
1995-04-28 23:23:38 +04:00
while ((token = strtok((char *) s, " \t\n\r\f")) != NULL) {
1993-03-21 12:45:37 +03:00
s = NULL; /* make strtok() keep on tokin' */
for (p = names; p < names + NNAMES; p++) {
if (STREQ(p->name, token)) {
ckfputs(types[p->type], stdout);
if (has_escapes)
ckfputs(" (with escape sequences)",
stdout);
1993-03-21 12:45:37 +03:00
return 1;
}
}
}
1995-05-21 04:13:24 +04:00
/* all else fails, but it is ASCII... */
ckfputs("ASCII text", stdout);
if (has_escapes) {
ckfputs(" (with escape sequences)", stdout);
}
1993-03-21 12:45:37 +03:00
return 1;
}