home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 10
/
Fresh_Fish_10_2352.bin
/
useful
/
util
/
cli
/
whatis
/
2.0
/
for.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-30
|
20KB
|
710 lines
/*
* Copyright © 1991 by S.R. & P.C.
*
* Created: 23 May 1992 12:00:40
* Modified: 18 Mar 1993 22:35:21
*
* Make>> sc <file>.c
* Make>> SLink LIB:cs.o <file>.o SC SD BATCH NOICONS LIB LIB:Startup.lib LIB:String.lib
*/
#include <libraries/WhatIsBase.h>
#include <proto/WhatIs.h>
struct Library *WhatIsBase = NULL;
/* have problem whith stack size so I reduce string len */
#define MAXSTRINGLEN 255/2
#define MAXCMDLEN 512/2
#define MAX_PATHLEN 512
/*ULONG const MaxStringLen = MAXSTRINGLEN;*/
#define MAXNUMARGS 64
#define MAXDEFAULTLEN 512/2
/* char used in the filetype spec string */
#define NOTFileType '!'
#define ONLYFileType '%'
#define WHITHSubSub '#'
STRPTR Template = "Pattern/M,Files/K,Dirs/K,Since/K,Before/K,MinSize/K/N,MaxSize/K/N,PosProtect/K,NegProtect/K,Type/K,ALL/K/S,ASYNC/K/S,READSIZE/N,SHOWBYTE/N,D=DEEP/K/N,SFT=SHOWFILETYPE/K/S,DO/K/F";
#define ARG_NameA 0
#define ARG_Files 1
#define ARG_Dirs 2
#define ARG_Since 3
#define ARG_Before 4
#define ARG_MinSize 5
#define ARG_MaxSize 6
#define ARG_PosProtect 7
#define ARG_NegProtect 8
#define ARG_Type 9
#define ARG_ALL 10
#define ARG_ASYNC 11
#define ARG_READSIZE 12
#define ARG_SHOWBYTE 13
#define ARG_DEEPWHATIS 14
#define ARG_SHOWFILETYPE 15
#define ARG_CMD 16
#define ARG_ENDARG 17
STRPTR CliHelp = "For V2.0
© 1992 S.R. & P.C.
\n\
Usage: For <Pattern> [FILES <MATCH|YES|NO>] [DIRS <MATCH|YES|NO>] [Since <Date>] [Before <Date>] [MinSize <Number>] [MaxSize <Number>] [PosProtect <L|C|H|S|P|A|R|W|E|D>] [NegProtect <L|C|H|S|P|A|R|W|E|D>] [Type <FileType0...FileType9|%=ONLY|*=SUB|!=EXLC>] [ALL] [ASYNC] [READSIZE] [SHOWBYTE] [DEEPWHATIS] [SHOWFILETYPE] [DO <Command [args] [,Command [args] [,...]]>]\n";
/*******************************************************/
/* not in dos.h !! */
#define FIBB_HIDDEN 7
#define FIBF_HIDDEN (1<<FIBB_HIDDEN)
/* it is my own definition: not standard */
#define FIBB_COMMENT 8
#define FIBF_COMMENT (1<<FIBB_COMMENT)
#define FIBB_LINK 9
#define FIBF_LINK (1<<FIBB_LINK)
#define PATTERN_BUF_SIZE 32
#define MAX_FTS 10 /* Max FileTypeSpec in SelectInfo */
struct FileTypeSpec {
FileType fs_FileType;
UWORD fs_Flags;
};
/* FileTypeSpec flags */
#define FTSF_EXCLUDETYPE 0x0001 /* Exclude this type (otherwise include) */
#define FTSF_WITHSUBTYPES 0x0002 /* Affect Include/Exclude to subtypes */
struct SelectInfo {
UBYTE si_Pattern[PATTERN_BUF_SIZE]; /* Pattern */
UBYTE si_PatTok[PATTERN_BUF_SIZE]; /* PreParsed pattern */
LONG si_MinSize; /* Show files bigger than that */
LONG si_MaxSize; /* and smaller than that */
struct DateStamp si_SinceDate; /* Show files newer than that */
struct DateStamp si_BeforeDate; /* and older than that */
LONG si_SinceSecs; /* date in seconds */
LONG si_BeforeSecs; /* date in seconds */
UWORD si_PosProtect; /* Show files that have these bits set */
UWORD si_NegProtect; /* Show files that have these bits clear */
struct FileTypeSpec si_FileTypes[MAX_FTS]; /* Include and Exclude file types */
UWORD si_NumFts; /* Number of FileTypeSpec in previous array */
UWORD si_Flags; /* Flags. See below */
};
/* SelectInfo flags */
#define SI_ALL_FILES 0x0001
#define SI_MATCH_FILES 0x0002
#define SI_ALL_DIRS 0x0004
#define SI_MATCH_DIRS 0x0008
#define SI_AFFECT_SUBDIRS 0x0010
#define SI_NAME 0x0020
#define SI_SIZE 0x0040
#define SI_SINCEDATE 0x0080
#define SI_BEFOREDATE 0x0100
#define SI_POSPROTECTION 0x0200
#define SI_NEGPROTECTION 0x0400
//
#define SI_INVERT 0x0800 /* Invert filters */
//
#define SI_AFFECT_INFO 0x1000 /* do actions on .info files too (WARNING: also defined as an option (for menu)) */
#define SI_POSFILETYPE 0x2000
#define SI_NEGFILETYPE 0x4000
#define SI_FILETYPE (SI_POSFILETYPE|SI_NEGFILETYPE)
#define SI_MATCHBITS (SI_NAME|SI_SIZE|SI_SINCEDATE|SI_BEFOREDATE|SI_POSPROTECTION|SI_NEGPROTECTION|SI_FILETYPE)
/*************************************************/
/*#pragma tagcall WhatIsBase WhatIs 1E 9802*/
FileType _WhatIsTags(char *Name, Tag FirstTag,...)
{
return (WhatIsBase) ? WhatIs(Name, (struct TagItem *) & FirstTag) : TYPE_UNSCANNED;
}
/*************************************************/
/*
* return pointer to the first non blank char in a string, or to the '\0' if
* the string is empty
*/
static char *FirstNonBlank(char *buf)
{
while (*buf && *buf == ' ')
buf++;
return buf;
}
/* check if buffer is empty or not */
static BOOL IsEmpty(char *buf)
{
return (BOOL) ((*FirstNonBlank(buf)) ? FALSE : TRUE);
}
static BOOL String2Date(char *src, struct DateStamp * ds)
{
struct DateTime *dt;
BOOL Ok = TRUE;
char *s1, *s2;
char buf[PATTERN_BUF_SIZE];
if (!(dt = AllocMem(sizeof(struct DateTime), MEMF_PUBLIC | MEMF_CLEAR)))
return FALSE;
strcpy(buf, src);
s1 = s2 = FirstNonBlank(buf);
if (*s1) {
dt->dat_StrDate = s1;
while (*s2 && *s2 != ' ')
s2++;
if (*s2) {
*s2++ = '\0';
s2 = FirstNonBlank(s2);
}
if (*s2) {
dt->dat_StrTime = s2;
if (!StrToDate(dt))
Ok = FALSE;
}
else if (!StrToDate(dt)) {
dt->dat_StrDate = NULL;
dt->dat_StrTime = s1;
if (!StrToDate(dt))
Ok = FALSE;
}
if (Ok)
*ds = dt->dat_Stamp;
}
FreeMem(dt, sizeof(struct DateTime));
return Ok;
}
/* Convert DateStamp into a number of seconds since 1-Jan-78 */
long Date2Secs(struct DateStamp * ds)
{
return ds->ds_Days * 86400 + ds->ds_Minute * 60 + ds->ds_Tick / 50;
}
/*************************************************/
long __regargs _Main(void)
{
char *ArgV[MAXNUMARGS];
struct RDArgs *RA;
struct RDArgs *DefaultRDArgs;
LONG RC = 20; /* assumle the worst */
if (DOSBase->dl_lib.lib_Version < 37)
return 20;
if (RA = AllocDosObject(DOS_RDARGS, NULL)) {
memset(ArgV, 0, MAXNUMARGS * sizeof(APTR));
RA->RDA_ExtHelp = CliHelp;
/* first read default */
/* getting the default string */
if (DefaultRDArgs = AllocDosObject(DOS_RDARGS, NULL)) {
UBYTE Default[MAXDEFAULTLEN];
if ((DefaultRDArgs->RDA_Source.CS_Length = GetVar("For.DefOpts", Default, MAXDEFAULTLEN - 1, 0L)) > 0) {
/* let ReadArgs() allocate neccesary buffer sinc I use diferrent RDArgs for Default and CLI opts */
/* Ok I succefuly read a Default Option string now parse it */
Default[DefaultRDArgs->RDA_Source.CS_Length++] = '\n'; /* this is need by ReadArgs() */
Default[DefaultRDArgs->RDA_Source.CS_Length] = '\0'; /* this is need by ReadArgs() */
DefaultRDArgs->RDA_Source.CS_Buffer = Default;
DefaultRDArgs->RDA_Source.CS_CurChr = 0;
DefaultRDArgs->RDA_Flags = RDAF_NOPROMPT;
if (!ReadArgs(Template, (long *) ArgV, DefaultRDArgs)) {
PutStr("Error in Default:");
PrintFault(IoErr(), NULL);
memset(ArgV, 0, MAXNUMARGS * sizeof(APTR));
}
DefaultRDArgs->RDA_Source.CS_Buffer = NULL; /* Now ReadArgs() from Command line */
/* the ArgV is leaving unchanged: the second ReadArgs() take actual value as default */
}
}
/* Let ReadArgs() allocate necessay buffer rather using stack space */
if (ReadArgs(Template, (long *) ArgV, RA))
RC = Main(ArgV, NULL);
else
PrintFault(IoErr(), NULL);
FreeDosObject(DOS_RDARGS, RA);
/* Free RDargs now I don't need the option value */
if (DefaultRDArgs)
FreeDosObject(DOS_RDARGS, DefaultRDArgs);
}
return RC;
}
/**** ****/
void RemoveDir(char FullDir[])
{
char *s;
if (s = PathPart(FullDir))
*s = '\0';
}
void PrintByte(UBYTE Buffer[], long BufLen, UBYTE Num)
{
register short i;
for (i = 0; i < Num && i < BufLen; i++)
Printf("%02lx", (long) Buffer[i]);
}
/*
* Parse a line that may contain comas. Backslash ('\') is the override char.
*/
void ParseCmdLine(char *cmd)
{
char *s, *d, c;
s = d = cmd;
while (c = *d++ = *s++) {
if (c == '\\')
*(d - 1) = *s++;
else if (c == ',')
*(d - 1) = '\n';
}
}
void MakeFmt(char Cmd[], char CmdFmt[])
{
char *s;
BOOL Found = FALSE;
strcpy(CmdFmt, Cmd);
ParseCmdLine(CmdFmt); /* Replace , by \n to separate commands */
s = CmdFmt;
while (*s) {
if (*s == '%' && *(s + 1) == '%') {
*(s + 1) = 's';
Found = TRUE;
}
s++;
}
if (!Found)
StrnCat(CmdFmt, " %s", MAXCMDLEN - 1);
}
UWORD ProtectBit(char P[])
{
UWORD PB = 0;
while (*P) {
switch (*P) {
case 'L'