home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
MISC
/
file.lzh
/
help.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-27
|
12KB
|
334 lines
/*************************************************************************
* help.c *
*************************************************************************
* Changes Made Date Who *
* ------------ ---- --- *
* Created 94/01/06 sam *
* -help finished 94/01/21 sam *
* edition number moved to file.c 95/04/04 sam *
************************************************************************/
#include <file.h>
void main(argc,argv)
int argc;
char **argv;
{
register char *p;
int arg_count = 0;
int i;
#if defined(_OSK) || defined(_OS9000)
progname = _prgname();
#else
progname = strrchr(argv[0], '/');
if(progname == NULL)
progname = argv[0];
else
progname++;
#endif
if(argc == 1){
printUsage();
exit(0);
}
magic_file = getenv("MAGIC_FILE");
if(magic_file == NULL)
magic_file = MAGIC_DEFAULT;
/* parse the options */
for(i=1;i<argc;i++){
if (*(p=argv[i]) == '-') {
optparse(p);
argv[i] = NULL;
}else /* anything here is an arguement */
arg_count++;
}
if((arg_count == 0) && (use_stdin == FALSE) && (file_path == NULL)){
printUsage();
exit(0);
}
/* OK, time to read the magic file now */
if(magicList == NULL)
magicList = readMagic(magic_file);
if(use_stdin == TRUE) /* get file names from stdin */
getFileNames(NULL);
else if(file_path != NULL) /* get file names from file_path */
getFileNames(file_path);
else /* get file names from command line */
for(i=1;i<argc;i++)
if(argv[i] != NULL)
readFile(argv[i]);
exit(0);
}
void optparse(ptr)
register char *ptr;
{
while (*++ptr) {
switch (tolower(*ptr)) {
case 'f': if(*++ptr == '=')
ptr++;
file_path = ptr;
return;
case 'h': if(strucmp(ptr, "help") == 0){
printHelp();
exit(0);
}else{
#if defined(_OSK) || defined(_OS9000)
printHelp();
#else
printUsage();
#endif
exit(0);
}
case 'm': if(*++ptr == '=')
ptr++;
magic_file = ptr;
return;
case 'r': recursive = TRUE;
exec_relative = FALSE;
break;
#if defined(_OSK) || defined(_OS9000)
case 'x': exec_relative = TRUE;
recursive = FALSE;
break;
#endif
case 'z': if(*++ptr == '=')
ptr++;
if(*ptr != '\0')
file_path = ptr;
else
use_stdin = TRUE;
return;
case '?': printUsage();
exit(0);
default: printUsage();
exit(_errmsg(1,"unknown option '%c'\n", *ptr));
}
}
}
/* Help message */
static char *cmds[] = {
"Function: identify file types\n",
"Options:\n",
" -f[=]path read files from path\n",
#if !(defined(_OSK) || defined(_OS9000))
" -h displaly usage\n",
" -help display verbose help information\n",
#else
" -h[elp] display verbose help information\n",
#endif
" -m[=]file use file for magic file\n",
" -r recursively descend directories\n",
#if defined(_OSK) || defined(_OS9000)
" -x execution directory relative\n",
#endif
" -z read files from stdin\n",
" -z[=]path read files from path (same as -f)\n",
#if defined(_OSK) || defined(_OS9000)
"\nNote: the -r and -x options are mutually exclusive, and the last\n",
" one seen on the command line will turn the other off.\n",
" Also, the options -f, -help, -m, and -z (since the may be\n",
" longer than a single character, must be the final option in\n",
" a group if grouped with other options. (ie -rz is OK, -zr is\n",
" not)\n",
#endif
"\nThe environment variable MAGIC_FILE (if set) changes\n",
"the default magic file (from ",
#if defined(_OSK) || defined(_OS9000)
"/dd/SYS/magic",
#else
"/etc/magic",
#endif
").\n",
"\nThis program is Copyright (c) 1995 by Scott McGee but may be\n",
"distributed at no cost in its original unaltered form. This program\n",
"comes with ABSOLUTELY NO WARRANTY!\n",
0
};
/* print the help message */
void printUsage(){
register char **p=cmds;
fprintf(stderr, "Syntax: %s [<opts>] {<file>}\n", progname);
while (*p) fputs(*p++,stderr);
fputs("\n", stderr);
}
/* long help message */
static char *help[] = {
"\nThe ",
NULL,
" utility is used to identify filetypes. Syntax is:\n",
" ",
NULL,
" [<opts>] {<file>}\n\n",
"Typical usage would be:\n",
" ",
NULL,
" <path> to identify the type of <path>\n",
" ",
NULL,
" <dir>/* to identify the type of all files in <dir>\n\n",
NULL,
" identifies OS-9 files by using knowledge of the internal\n",
"structure of the file. Other files are identified by matching\n",
"information in the file with data supplied in a 'magic' file.\n",
"The default location of the magic file is ",
#if defined(_OSK) || defined(_OS9000)
"/dd/SYS/magic",
#else
"/etc/magic",
#endif
".\n\n",
"The magic file specifies the following information that is\n",
"used in identifying file types.\n\n",
"OFFSET This is the offset into the file where the matching\n",
" information is found.\n\n",
"TYPE This is the data type of the value to be matched. It\n",
" may be one of the following types:\n",
" byte 1 byte (char)\n",
" short 2 bytes (short int) (byte order not specified)\n",
" leshort 2 bytes (short int) (Little Endian byte order)\n",
" beshort 2 bytes (short int) (Big Endian byte order)\n",
" long 4 bytes (long int) (byte order not specified)\n",
" lelong 4 bytes (long int) (Little Endian byte order)\n",
" belong 4 bytes (long int) (Big Endian byte order)\n",
" string a string (array of char)\n\n",
"VALUE This is the value that is tested for a match at the\n",
" given offset. The value in the file is cast to the\n",
" specified type before comparison. String types are\n",
" the same length as the string in the magic file and\n",
" need not be null terminated. The usual C style\n",
" escapes are allowed. A space as the first character\n",
" must be escaped, but a space elsewhere in the string\n",
" may be typed explicitly. All tab characters must be\n",
" escaped.\n\n",
"MESSAGE This is the message that is printed out when a match\n",
" is found. This string may contain any characters\n",
" allowed in a C printf() style control string. It\n",
" may also have one format specifier which will refer\n",
" to the matched value if the TYPE is NOT string.\n\n",
"Each line in the magic file is checked until a match is found,\n",
"or the file is exhausted. An optional '>' character may precede\n",
"the offset entry indicating a continuation line. These lines\n",
"are ignored unless the preceding non-continuation line is\n",
"matched. In this case, each continuation line is checked, and\n",
"the message printed if it is a match. This continues until the\n",
"next non-continuation line is encountered.\n\n",
"A mask value may be ANDed with the value read from the file\n",
"at the given offset before any match is attempted. This mask\n",
"may be specified by appending an '&' character followed by the\n",
"mask value to the end of the value entry in the magic file.\n\n",
"The default match operation is 'equal' but any of the following\n",
"match comparisons may be specified by prepending the operator\n",
"character to the beginning of the value.\n",
" Character Operation\n",
" = EQUAL\n",
" < LESS THAN\n",
" > GREATER THAN\n",
" & AND (if a bit is set in the value entry, it\n",
" must be set in the file too)\n",
" ^ OR (at least one bit that is set in the value\n",
" entry must also be set in the file)\n",
" x ANY (any value will match)\n",
"\nA custom magic file may be specified in two ways. The environment\n",
"variable MAGIC_FILE will override the default (",
#if defined(_OSK) || defined(_OS9000)
"/dd/SYS/magic",
#else
"/etc/magic",
#endif
")\n",
"magic file. The -m option will also override the default magic\n",
"file, and will also override the MAGIC_FILE environment variable.\n",
"\nOptions:\n",
"\n -f[=]path The specified path is used to read filenames rather\n",
" than the command line. \n",
#if !(defined(_OSK) || defined(_OS9000))
"\n -h displaly usage.\n",
"\n -help display (this) verbose help information.\n",
#else
"\n -h[elp] display (this) verbose help information.\n",
#endif
"\n -m[=]file The specified file is read instead of the default\n",
" magic file.\n",
"\n -r When this option is specified, and a directory is found,\n",
" in addition to printing the fact that it is a directory,\n",
" the program will recursively descend the directory. This\n",
" allows the program to be used on an entire file tree.\n",
#if defined(_OSK) || defined(_OS9000)
" This option is not allowed in conjunction with the -x\n",
" option. When both appear on the same command line, the\n",
" last one is left active, and the other is disabled.\n",
"\n -x All paths are specified as relative to the execution\n",
" directory. This can cause unexpected results if used\n",
" with shell wild cards since the shell will expand the\n",
" wild cards relative to the current data directory. The\n",
" same applies to redirected input (-f or -z). Also, note\n",
" the conflict with the -r option above.\n",
#endif
"\n -z Read files from stdin instead of from the command line.\n",
"\n -z[=]path Read files from path (same as -f).\n",
"\nNote: The options -f, -help, -m, and -z (since they may be longer\n",
" than a single character, must be the final option in a group\n",
" if grouped with other options. (ie -rz is OK, -zr is not)\n",
0
};
/* print the long help message */
void printHelp(){
register char **p = help;
p[1] = p[4] = p[8] = p[11] = p[13] = progname;
while (*p) fputs(*p++,stderr);
fputs("\n", stderr);
}
/* an error checking malloc */
void *grab(size)
unsigned int size;
{
void *ret;
if ((ret = malloc(size)) == NULL)
exit(_errmsg(errno, "memory allocation error - "));
return ret;
}
#if !(defined(_OSK) || defined(_OS9000))
int _errmsg(nerr, msg, arg1, arg2, arg3, arg4, arg5, arg6)
int nerr;
char *msg;
unsigned int arg1, arg2, arg3, arg4, arg5, arg6;
{
extern char *progname;
fprintf(stderr, "%s: ", progname);
fprintf(stderr, msg, arg1, arg2, arg3, arg4, arg5, arg6);
return nerr;
}
#endif