[net.sources.mac] macbin for unix

jimb@dopey.AMD.COM (Jim Budler) (04/03/87)

The recent posting saying I could supply unix utilities resulted in
a flurry of requests (4 or 5). One of them, from a user on sysV
resulted in a bug report, and I finally bit the bullet and took a trek to
our local sysV machine and did some debugging. It now recognizes
the truncated names created by xbin on sysV, and no longer overwrites
input files.

As a result, version 2.4 of macbin for unix BSD and sysV, actually tested on
both systems.

#!/bin/sh
# to extract, remove the header and type "sh filename"
if `test ! -s ./README`
then
echo "writing ./README"
cat > ./README << '\Rogue\Monster\'
A new version of Unix macbin.

Changes:

	'% macbin *' will convert all *.{info,data,rsrc} trios in the
	current directory to basename.bin.

	'% macbin foo fee fum' will attempt to convert foo.{data,rsrc,info},
	fee.{data,rsrc,info}, and fum.{data,info,rsrc}.

	'% macbin -v <whatever>' will be a little noisier about it.

Version 2.3	3/31/87

	Fixed report from sysV that long filenames could result
	in the output filename being truncated, and overwriting
	the input files.

	Removed bzero(), it isn't really necessary, and is still
	present on Berkeley systems if you define BSD.

Version 2.4	4/3/87

	Fixed bzero() declarations and include file instead.
	Fixed input filenames so the search algorithm would
	look for the correct truncated filenames as produced
	on sysV by xbin.

\Rogue\Monster\
else
  echo "will not over write ./README"
fi
if `test ! -s ./makefile`
then
echo "writing ./makefile"
cat > ./makefile << '\Rogue\Monster\'
#for BSD or anyone with bzero() in a library
CFLAGS=-O

#for sysV
# CFLAGS=-O -Drindex=strrchr -DBZERO

macbin:	macbin.c makefile
	cc $(CFLAGS) -s -o macbin macbin.c
\Rogue\Monster\
else
  echo "will not over write ./makefile"
fi
if `test ! -s ./macbin.c`
then
echo "writing ./macbin.c"
cat > ./macbin.c << '\Rogue\Monster\'
/*--------------------------------------------------------------------* 
 | Version    : 2.1                                                   | 
 | Author     : Jim Budler                                            | 
 | Copyright Oct 13, 1986                                             | 
 | Placed in public domain Oct 14, 1986                               |
 | Version    : 2.4 April 3, 1987                                     | 
 | Enjoy                                                              | 
 *--------------------------------------------------------------------*/

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/dir.h>

#ifndef CHECK14
#	ifdef MAXNAMLEN	/* 4.2 BSD */
#		define FNAMELEN MAXNAMLEN
#	else
#		define FNAMELEN DIRSIZ
#	endif
#else
#	define FNAMELEN 14
#endif

extern int errno;

main(argc,argv)
int argc;
char **argv;
{
/*	structure of the info file - from MacBin.C
	zero1, zero2, and zero3 are used to 'recognize' a Macbinary
	format file.
	char zero1;
	char nlen;
	char name[63];
	char type[4];		65	0101
	char creator[4];	69
	char flags;		73
	char zero2;		74	0112
	char location[6];	80
	char protected;		81	0121
	char zero3;		82	0122
	char dflen[4];
	char rflen[4];
	char cdate[4];
	char mdate[4];
*/
	FILE *fd, *ofd;
	char bname[128];
	char iname[128];
	char dname[128];
	char rname[128];
	char oname[128];
	char buf[128];
	char * charp, * cmd;
	char * rindex();
	int verbose = 0;
	int len;
	if ((cmd = rindex(argv[0], '/')) != NULL)
		++cmd;
	else
		cmd = argv[0];
	if (argc > 1)
		if (!strcmp(argv[1], "-v")) {
			verbose = 1;
			--argc;
			++argv;
		}
	if (argc < 2) {
		fprintf(stderr,"Usage:\n\t%s [-v] filename(s)\n",cmd);
		exit(EINVAL);
	}
	while ( argc > 1 ) {
		(void) bzero(buf,128);
		if (( charp = rindex (argv[1], '.')) == NULL) 
			strcpy(bname, argv[1]);
		else {
			*charp = '\0';
			if ( strcmp(++charp,"data") && strcmp (charp, "info")
			&& strcmp (charp, "rsrc")) {
				*(--charp) = '.'; /* put it back */
				strcpy(bname, argv[1]);
			} else {
				strcpy(bname,argv[1]);
			}
		}
		--argc;
		++argv;

		len = strlen(bname);
		if ( len > FNAMELEN - 2 ) {
			len = FNAMELEN - 2;
			strncpy(oname,bname,len);
			strncat(oname,".bin",FNAMELEN - len);
			strncpy(iname,bname,len);
			strncat(iname,".info",FNAMELEN - len);
			strncpy(dname,bname,len);
			strncat(dname,".data",FNAMELEN - len);
			strncpy(rname,bname,len);
			strncat(rname,".rsrc",FNAMELEN - len);
		} else {
			strcpy(oname,bname);
			strcat(oname,".bin");
			strcpy(iname,bname);
			strcat(iname,".info");
			strcpy(dname,bname);
			strcat(dname,".data");
			strcpy(rname,bname);
			strcat(rname,".rsrc");
		}
		if (verbose)
			fprintf (stderr, "Converting %s\n", bname);
		while ( argc > 1 && (!strcmp(argv[1], iname) ||
		!strcmp(argv[1], dname) || !strcmp(argv[1], rname))) {
			--argc;
			++argv;
		}
		if ((ofd = fopen( oname, "w")) == NULL){
			perror(oname);
			--argc;
			++argv;
			continue;
		}
		if ((fd = fopen(iname,"r")) == NULL){
			if ( verbose )
				fprintf ( stderr, "No %s\n", iname);
			fclose(ofd);
			unlink(oname);
			--argc;
			++argv;
			continue;
		}
		if (fread(buf, sizeof(*buf), 128, fd) > 0){
			if (buf[74] & 0x40) buf[81] = '\1'; /* protected */
			buf[74] = '\0'; /* clear zero2 */
			buf[82] = '\0'; /* force zero3 clear */
			fwrite(buf, sizeof(*buf),128, ofd);
			(void) bzero(buf,128);
		}
		fclose(fd);
		if ((fd = fopen(dname,"r")) == NULL){
			if ( verbose )
				fprintf ( stderr, "No %s\n", dname);
			fclose(ofd);
			unlink(oname);
			--argc;
			++argv;
			continue;
		}
		while (fread(buf, sizeof(*buf),128, fd) > 0){
			fwrite(buf, sizeof(*buf),128, ofd);
			(void) bzero(buf,128);
		}
		fclose(fd);
		if ((fd = fopen(rname,"r")) == NULL){
			if ( verbose )
				fprintf ( stderr, "No %s\n", rname);
			fclose(ofd);
			unlink(oname);
			--argc;
			++argv;
			continue;
		}
		while (fread(buf, sizeof(*buf),128, fd) > 0){
			fwrite(buf, sizeof(*buf),128, ofd);
			(void) bzero(buf,128);
		}
		fclose(fd);
		fclose(ofd);
	}
}

#ifdef BZERO
/*  File   : bzero.c
    Author : Richard A. O'Keefe.
    Updated: 23 April 1984
    Defines: bzero()

    bzero(dst, len) moves "len" 0 bytes to "dst".
    Thus to clear a disc buffer to 0s do bzero(buffer, BUFSIZ).

    Note: the "b" routines are there to exploit certain VAX order codes,
    but the MOVC5 instruction will only move 65535 characters.   The asm
    code is presented for your interest and amusement.
*/
/*
   Note to macbin users. This is not a complete version of the original
   bzero.c by Richard O'Keefe. A void declaration, and an include file
   have been removed.
 */


#if	VaxAsm

bzero(dst, len)
    char *dst;
    int len;
    {
	asm("movc5 $0,*4(ap),$0,8(ap),*4(ap)");
    }

#else  ~VaxAsm

bzero(dst, len)
    register char *dst;
    register int len;
    {
	while (--len >= 0) *dst++ = 0;
    }

#endif	VaxAsm

#endif /* BZERO */
\Rogue\Monster\
else
  echo "will not over write ./macbin.c"
fi
echo "Finished archive 1 of 1"
exit
-- 
+===== Jim Budler ==== Advanced Micro Devices, Inc. ==== (408) 749-5806 =====+
|  Compuserve: 72415,1200; Delphi: JIMBUDLER;  Usenet: jimb@amdcad.AMD.COM   |
+==== Disclaimer: My company wouldn't let ME speak for them, would they? ====+
Who needs four?