[comp.sources.amiga] v02i026: dutils - various small utilities

page@swan.ulowell.edu (Bob Page) (10/26/88)

Submitted-by: dillon@cory.berkeley.edu (Matt Dillon)
Posting-number: Volume 2, Issue 26
Archive-name: util/dutils.1

# This is a shell archive.  Remove anything before this line
# then unpack it by saving it in a file and typing "sh file"
# (Files unpacked will be owned by you and have default permissions).
# This archive contains the following files:
#	Makefile
#	addcr.c
#	cmp.c
#	com.doc
#	findit.c
#	libs.c
#	remcr.c
#	scat.c
#
if `test ! -s Makefile`
then
echo "writing Makefile"
cat > Makefile << '\Rogue\Monster\'

#   Makefile for single-source-file utilities

SYMS=	include:symbols.m
SYMC=	include:local/makesymbols.c
CFLAGS= +L +I$(SYMS)
LFLAGS= +Q
DD=	srcc:
OD=	T:

all:	$(DD)findit $(DD)libs $(DD)scat $(DD)addcr $(DD)remcr $(DD)cmp

$(DD)findit:    findit.c
    cc $(CFLAGS) findit.c -o $(OD)findit.o
    ln +Q $(OD)findit.o -lsup32 -lc32 -o $(DD)findit
    delete $(OD)findit.o

$(DD)libs:      libs.c
    cc $(CFLAGS) libs.c -o $(OD)libs.o
    ln +Q $(OD)libs.o -lsup32 -lc32 -o $(DD)libs
    delete $(OD)libs.o

$(DD)scat:      scat.c
    cc $(CFLAGS) scat.c -o $(OD)scat.o
    ln +Q $(OD)scat.o -lsup32 -lc32 -o $(DD)scat
    delete $(OD)scat.o

$(DD)addcr:     addcr.c
    cc $(CFLAGS) addcr.c -o $(OD)addcr.o
    ln +Q $(OD)addcr.o -lsup32 -lc32 -o $(DD)addcr
    delete $(OD)addcr.o

$(DD)remcr:     remcr.c
    cc $(CFLAGS) remcr.c -o $(OD)remcr.o
    ln +Q $(OD)remcr.o -lsup32 -lc32 -o $(DD)remcr
    delete $(OD)remcr.o

$(DD)cmp:       cmp.c
    cc $(CFLAGS) cmp.c -o $(OD)cmp.o
    ln +Q $(OD)cmp.o -lsup32 -lc32 -o $(DD)cmp
    delete $(OD)cmp.o

$(SYMS):    $(SYMC)
    make -f include:local/Makefile

\Rogue\Monster\
else
  echo "will not over write Makefile"
fi
if [ `wc -c Makefile | awk '{printf $1}'` -ne 1109 ]
then
echo `wc -c Makefile | awk '{print "Got " $1 ", Expected " 1109}'`
fi
if `test ! -s addcr.c`
then
echo "writing addcr.c"
cat > addcr.c << '\Rogue\Monster\'

/*
 *  ADDCR.C
 *
 *  ADDCR [-l] file1 file2 ... filen
 *
 *  -l = convert filenames to lower case on writeout.
 */

#include <stdio.h>

extern char *malloc();

main(ac,av)
char *av[];
{
    register short i;
    FILE *fi;
    long len;
    char *buf;
    char *wbuf;
    char clower = 0;

    for (i = 1; i < ac; ++i) {
	if (strcmp(av[i], "-l") == 0) {
	    clower = 1;
	    continue;
	}
	printf("%-20s ", av[i]);
	fflush(stdout);
	if ((fi = fopen(av[i], "r")) == NULL) {
	    perror(av[i]);
	    continue;
	}
	fseek(fi, 0, 2);
	len = ftell(fi);
	printf("%6ld ", len);
	fflush(stdout);
	if (len < 0 || (buf = malloc(len*3)) == NULL) {
	    puts("len < 0 or malloc failed");
	    fclose(fi);
	    continue;
	}
	fseek(fi, 0, 0);
	if (fread(buf, len, 1, fi) != 1) {
	    puts("read failed");
	    goto done;
	}
	fclose(fi);
	wbuf = buf + len;
	len = addcr(buf, len);
	if (clower) {
	    register char *ptr;
	    for (ptr = av[i]; *ptr; ++ptr) {
		if (*ptr >= 'A' && *ptr <= 'Z')
		    *ptr |= 0x20;
	    }
	}
	if ((fi = fopen(av[i], "w")) == NULL) {
	    puts("Unable to open for write");
	    goto done;
	}
	printf("write ");
	fflush(stdout);
	if (fwrite(wbuf, len, 1, fi) != 1) {
	    puts("write failed");
	    goto done;
	}
	puts("ok");
done:
	fclose(fi);
	free(buf);
    }
}

addcr(buf, len)
register char *buf;
register long len;
{
    register char *ptr = buf + len;
    register long i, j;
    for (i = j = 0; i < len; ++i) {
	if (buf[i] == 13)
	    continue;
	if (buf[i] != 10) {
	    ptr[j++] = buf[i];
	    continue;
	}
	ptr[j++] = 13;
	ptr[j++] = 10;
    }
    return(j);
}

\Rogue\Monster\
else
  echo "will not over write addcr.c"
fi
if [ `wc -c addcr.c | awk '{printf $1}'` -ne 1586 ]
then
echo `wc -c addcr.c | awk '{print "Got " $1 ", Expected " 1586}'`
fi
if `test ! -s cmp.c`
then
echo "writing cmp.c"
cat > cmp.c << '\Rogue\Monster\'

/*
 * CMP.C
 *
 *
 * (C)Copyright 1986, Matthew Dillon, All Rights Reserved.
 * Permission is granted to distribute for non-profit only.
 *
 *    comp file1 file2
 *
 *    Compare two files.  The program will tell you if two files compare
 *    the same or, if not, where the error occured.
 *
 */

#include <stdio.h>
#include <fcntl.h>

extern char *malloc();

#define BUFSIZE   16384

main(ac, av)
char *av[];
{
    long f1, f2;
    register short i, j, n;
    char fail;
    char *buf1, *buf2;
    char *premature_eof = "premature EOF in %s (files compare to that point)\n";

    buf1 = malloc(BUFSIZE);
    buf2 = malloc(BUFSIZE);
    if (!buf1 || !buf2) {
	puts("no memory");
	exit(30);
    }
    fail = 0;
    if (ac <= 2) {
	puts ("V2.00 (c)Copyright 1986-1988 Matthew Dillon, All Rights Reserved");
	puts ("cmp file1 file2");
	exit(0);
    }
    f1 = open(av[1], O_RDONLY);
    f2 = open(av[2], O_RDONLY);
    if (f1 && f2) {
	while (!fail && (i = read(f1, buf1, 256))) {
	    n = read(f2, buf2, i);
	    if (!bcmp(buf1, buf2, n))
		fail = 5;
	    if (!fail) {
		if (n == i)
		    continue;
		fail = 5;
	    }
	}
	if (!fail && read(f2, buf2, 1))
	    fail = 5;
    } else {
	puts("Could not open both files");
	fail = 20;
    }
    if (f1 >= 0)
	close(f1);       /* f1 & f2 are either valid or NULL */
    if (f2 >= 0
	close(f2);
    if (fail)
	puts("Compare failed");
    exit(fail);
}


\Rogue\Monster\
else
  echo "will not over write cmp.c"
fi
if [ `wc -c cmp.c | awk '{printf $1}'` -ne 1398 ]
then
echo `wc -c cmp.c | awk '{print "Got " $1 ", Expected " 1398}'`
fi
if `test ! -s com.doc`
then
echo "writing com.doc"
cat > com.doc << '\Rogue\Monster\'

	 (c)Copyright 1988, Matthew Dillon, All Rights Reserved
		   Freely Distributable for non-profit only


				     ADDCR

			     V1.00, 16 October 1988

ADDCR <files>

    Each file specified is loaded into ram, CR's added before LF's, and
    written back out under the same name.

				      CMP

			     V1.00, 16 October 1988

CMP file1 file2

    The two files must be of the same size.  The files are compared for
    exact equality.  An error message is displayed if the files do not
    compare.  Exit value:

	    0	compare successful
	    5	compare failed
	    20	unable to open both files
	    30	unable to allocate memory!


				     FINDIT

			     V1.00, 16 October 1988

FINDIT [-ddir] <wildcard> <wildcard> ...

    Search the specified directories for files matching the
    given wildcard.

    If no directories are specified, look for a directory list
    in the ENV: enviroment variable FINDITVOLS, of the form:

	dir,dir,dir ...

    After setting up such an enviroment variable, FINDIT is as
    simple as 'FINDIT <wildcard>'.  Example:

	findit list
	findit l\*	(from a shell)
	findit l*	(from a CLI)

    The * and ? wildcards are supported.


				     LIBS

			      V1.00, 16 October 1988


LIBS	[libname]

    With no arguments lists libraries and devices currently in
    memory, their version, and the number of references.

    If a library name is given as a reference and that library
    has no references it will be removed.  If the library has
    references the delayed-expunge flag is set and the library
    will be removed when the references fall to 0.

    This utility is useful for those of us working on our own
    custom libraries.

Example:

    libs
    libs dres.library

				     REMCR

			     V1.00, 16 October 1988

REMCR <files>

    Each file specified is loaded into ram, CR's removed, and written
    back out under the same name.


				     SCAT

			      V1.00, 16 October 1988

SCAT <files>

    Like cat, but non-ascii characters are displayed in reverse.



\Rogue\Monster\
else
  echo "will not over write com.doc"
fi
if [ `wc -c com.doc | awk '{printf $1}'` -ne 2021 ]
then
echo `wc -c com.doc | awk '{print "Got " $1 ", Expected " 2021}'`
fi
if `test ! -s findit.c`
then
echo "writing findit.c"
cat > findit.c << '\Rogue\Monster\'

/*
 *  FINDIT()
 *
 *  FINDIT [-ddir] <programname> <programname> ...
 *
 *  Search specified directories (those specified in FINDITVOLS if none
 *  spcified on the command line) for the specified program names.  Wildcards
 *  are acceptable
 *
 *  Format for FINDITVOLS:	dir,dir,dir ...
 */

#include <local/typedefs.h>
#include <local/xmisc.h>
#include <stdio.h>

#define ENVVAR	"FINDITVOLS"

MLIST	DList;
MLIST	FList;
char *ErrorString = "unable to open dres.library";
char *EnvStr;
char Path[1024];

extern int Enable_Abort;

main(ac,av)
char *av[];
{
    NewList(&DList);
    NewList(&FList);

    Enable_Abort = 0;
    {
	register short i;
	for (i = 1; i < ac; ++i) {
	    register char *str = av[i];
	    if (*str != '-') {
		register NODE *node = malloc(sizeof(NODE));
		node->ln_Name = str;
		AddTail(&FList, node);
		continue;
	    }
	    for (++str; *str; ++str) {
		switch(*str) {
		case 'd':
		    {
			register NODE *node = malloc(sizeof(NODE));
			node->ln_Name = str + 1;
			AddTail(&DList, node);
		    }
		    str = "\0";
		    break;
		default:
		    printf("Unknown option: -%c\n", *str);
		    exit(1);
		}
	    }
	}
    }
    if (openlibs(DRES_LIB) == 0) {
	puts(ErrorString);
	exit(1);
    }
    mountrequest(0);
    if (!GetHead(&DList)) {         /*  scan enviroment variable for dirs */
	char *str;
	register char *ptr;
	register NODE *node;
	register char c;

	str = EnvStr = GetDEnv(ENVVAR);
	if (!str) {
	    printf("Env. Var %s not found, format:  dir,dir,dir...\n", ENVVAR);
	    goto fail;
	}
	for (c = 1; c; str = ptr + 1) {
	    for (ptr = str; *ptr && *ptr != ','; ++ptr);
	    c = *ptr;
	    *ptr = 0;
	    node = malloc(sizeof(NODE));
	    node->ln_Name = str;
	    AddTail(&DList, node);
	}
    }
    {
	register NODE *node;
	register FIB *fib = malloc(sizeof(FIB));
	while (node = RemHead(&DList)) {
	    long lock;

	    if (lock = Lock(node->ln_Name, SHARED_LOCK)) {
		strcpy(Path, node->ln_Name);
		if (Examine(lock, fib))
		    SearchTree(lock, fib, strlen(Path));
		UnLock(lock);
	    }
	    free(node);
	    if (checkbreak())
		break;
	}
	free(fib);
	puts("");
    }
fail:
    if (checkbreak())
	puts("^C");
    {
	register NODE *node;
	while (node = RemHead(&DList))
	    free(node);
	while (node = RemHead(&FList))
	    free(node);
    }
    if (EnvStr)
	free(EnvStr);
    mountrequest(1);
    closelibs(-1);
}

/*
 *  Search the specified directory for the wildcarded names in FList.
 */

SearchTree(dirlock, dirfib, idx)
long dirlock;
FIB *dirfib;
{
    long oldlock;
    long lock;
    register FIB *fib = malloc(sizeof(FIB));

    oldlock = CurrentDir(dirlock);
    while (ExNext(dirlock, dirfib)) {
	if (idx && Path[idx-1] != ':' && Path[idx-1] != '/') {
	    Path[idx] = '/';
	    strcpy(Path+idx+1, dirfib->fib_FileName);
	} else {
	    strcpy(Path+idx, dirfib->fib_FileName);
	}
	if (dirfib->fib_DirEntryType > 0) {
	    if (lock = Lock(dirfib->fib_FileName, SHARED_LOCK)) {
		if (Examine(lock, fib))
		    SearchTree(lock, fib, idx + strlen(Path+idx));
		UnLock(lock);
		if (checkbreak())
		    break;
	    }
	} else {
	    register NODE *node;
	    for (node = GetHead(&FList); node; node = GetSucc(node)) {
		if (WildCmp(node->ln_Name, dirfib->fib_FileName)) {
		    printf("%s ", Path);
		    fflush(stdout);
		}
	    }
	}
    }
    CurrentDir(oldlock);
    free(fib);
}

\Rogue\Monster\
else
  echo "will not over write findit.c"
fi
if [ `wc -c findit.c | awk '{printf $1}'` -ne 3332 ]
then
echo `wc -c findit.c | awk '{print "Got " $1 ", Expected " 3332}'`
fi
if `test ! -s libs.c`
then
echo "writing libs.c"
cat > libs.c << '\Rogue\Monster\'

/*
 *  LIBS.C
 *
 *  Libs [libname]
 *
 *  Libs		- list currently loaded libraries
 *  Libs dres.library	- expunge specified lib on last close if no
 *			  further opens
 */

#include <local/typedefs.h>

extern EXECBASE *SysBase;

main(ac,av)
char *av[];
{
    short i;
    LIB *lib;

    if (ac == 1) {
	NODE *node;
	for (node = SysBase->LibList.lh_Head; node->ln_Succ; node = node->ln_Succ) {
	    printf("%-20s ver %3ld  refs %ld\n",
		node->ln_Name,
		((LIB *)node)->lib_Version,
		((LIB *)node)->lib_OpenCnt
	    );
	}
	puts("");
	for (node = SysBase->DeviceList.lh_Head; node->ln_Succ; node = node->ln_Succ) {
	    printf("%-20s ver %3ld  refs %ld\n",
		node->ln_Name,
		((LIB *)node)->lib_Version,
		((LIB *)node)->lib_OpenCnt
	    );
	}
    }
    for (i = 1; i < ac; ++i) {
	lib = OpenLibrary(av[i], 0);
	if (lib) {
	    RemLibrary(lib);
	    printf("library refs: %ld\n", lib->lib_OpenCnt - 1);
	    puts("Will expunge on last close if no new opens");
	    CloseLibrary(lib);
	} else {
	    puts("Unable to open library");
	}
    }
}

\Rogue\Monster\
else
  echo "will not over write libs.c"
fi
if [ `wc -c libs.c | awk '{printf $1}'` -ne 1045 ]
then
echo `wc -c libs.c | awk '{print "Got " $1 ", Expected " 1045}'`
fi
if `test ! -s remcr.c`
then
echo "writing remcr.c"
cat > remcr.c << '\Rogue\Monster\'

/*
 *  REMCR.C
 *
 *  REMCR [-l]	file1 file2 ... filen
 *
 *  -l	: convert file name to lower case
 *
 */

#include <stdio.h>

extern char *malloc();

main(ac,av)
char *av[];
{
    register short i;
    FILE *fi;
    long len;
    char *buf;
    char clower = 0;

    for (i = 1; i < ac; ++i) {
	if (strcmp(av[i], "-l") == 0) {
	    clower = 1;
	    continue;
	}
	printf("%-20s ", av[i]);
	fflush(stdout);
	if ((fi = fopen(av[i], "r")) == NULL) {
	    perror(av[i]);
	    continue;
	}
	fseek(fi, 0, 2);
	len = ftell(fi);
	printf("%6ld ", len);
	fflush(stdout);
	if (len < 0 || (buf = malloc(len*2)) == NULL) {
	    puts("len < 0 or malloc failed");
	    fclose(fi);
	    continue;
	}
	fseek(fi, 0, 0);
	if (fread(buf, len, 1, fi) != 1) {
	    puts("read failed");
	    goto done;
	}
	fclose(fi);
	len = remcr(buf, len);
	if (clower) {
	    register char *ptr;
	    for (ptr = av[i]; *ptr; ++ptr) {
		if (*ptr >= 'A' && *ptr <= 'Z')
		    *ptr |= 0x20;
	    }
	}
	if ((fi = fopen(av[i], "w")) == NULL) {
	    puts("Unable to open for write");
	    goto done;
	}
	printf("write ");
	fflush(stdout);
	if (fwrite(buf, len, 1, fi) != 1) {
	    puts("write failed");
	    goto done;
	}
	puts("ok");
done:
	fclose(fi);
	free(buf);
    }
}

remcr(buf, len)
register char *buf;
register long len;
{
    register char *ptr = buf + len;
    register long i, j;
    for (i = j = 0; i < len; ++i) {
	if (buf[i] == 13)
	    continue;
	ptr[j++] = buf[i];
    }
    bmov(buf + len, buf, j);
    return(j);
}

\Rogue\Monster\
else
  echo "will not over write remcr.c"
fi
if [ `wc -c remcr.c | awk '{printf $1}'` -ne 1494 ]
then
echo `wc -c remcr.c | awk '{print "Got " $1 ", Expected " 1494}'`
fi
if `test ! -s scat.c`
then
echo "writing scat.c"
cat > scat.c << '\Rogue\Monster\'

#include <stdio.h>
#include <ctype.h>

main(ac, av)
char *av[];
{
    register int i;
    register FILE *stream;

    for (i = 1; i < ac; ++i) {
	stream = fopen(av[i], "r");
	if (stream) {
	    scat(stream);
	    fclose(stream);
	} else {
	    fprintf(stderr, "Unable to open %s\n", av[i]);
	}
    }
    if (ac == 1)
	scat(stdin);
}

scat(stream)
FILE *stream;
{
    short c;

    while ((c = fgetc(stream)) >= 0) {
	if (isascii(c) && isprint(c)) {
	    reverse(0);
	    putc(c, stdout);
	    continue;
	}
	reverse(1);
	putc((c|0x40)&0x7F, stdout);
	if (c == '\n')
	    putc(c, stdout);
    }
    reverse(0);
}

reverse(mode)
{
    static char xmode;

    if (mode) {
	if (!xmode) {
	    putc(0x9b, stdout);
	    putc(0x37, stdout);
	    putc('m', stdout);
	}
    } else {
	if (xmode) {
	    putc(0x9b, stdout);
	    putc(0x30, stdout);
	    putc('m', stdout);
	}
    }
    xmode = mode;
}

\Rogue\Monster\
else
  echo "will not over write scat.c"
fi
if [ `wc -c scat.c | awk '{printf $1}'` -ne 892 ]
then
echo `wc -c scat.c | awk '{print "Got " $1 ", Expected " 892}'`
fi
echo "Finished archive 1 of 1"
# if you want to concatenate archives, remove anything after this line
exit
-- 
Bob Page, U of Lowell CS Dept.  page@swan.ulowell.edu  ulowell!page
Have five nice days.