[net.sources.mac] xbin->MacBinary converter

jimb@amdcad.UUCP (Jim Budler) (10/17/86)

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	README
#	makefile
#	macbin.c
# This archive created: Thu Oct 16 16:57:13 1986
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'README'
then
	echo shar: "will not over-write existing file 'README'"
else
sed 's/^X//' << \SHAR_EOF > 'README'
XA new version of Unix macbin.
X
XChanges:
X
X	'% macbin *' will convert all *.{info,data,rsrc} trios in the
X	current directory to basename.bin.
X
X	'% macbin foo fee fum' will attempt to convert foo.{data,rsrc,info},
X	fee.{data,rsrc,info}, and fum.{data,info,rsrc}.
X
X	'% macbin -v <whatever>' will be a little noisier about it.
SHAR_EOF
if test 322 -ne "`wc -c < 'README'`"
then
	echo shar: "error transmitting 'README'" '(should have been 322 characters)'
fi
fi
if test -f 'makefile'
then
	echo shar: "will not over-write existing file 'makefile'"
else
sed 's/^X//' << \SHAR_EOF > 'makefile'
X# CFLAGS=-O -Drindex=strrchr	-DBZERO		# for sysV I think
XCFLAGS=-O
X
Xmacbin:	macbin.c makefile
X	cc $(CFLAGS) -s -o macbin macbin.c
X	
SHAR_EOF
if test 132 -ne "`wc -c < 'makefile'`"
then
	echo shar: "error transmitting 'makefile'" '(should have been 132 characters)'
fi
fi
if test -f 'macbin.c'
then
	echo shar: "will not over-write existing file 'macbin.c'"
else
sed 's/^X//' << \SHAR_EOF > 'macbin.c'
X/*--------------------------------------------------------------------* 
X | Version    : 2.0                                                   | 
X | Author     : Jim Budler                                            | 
X | Copyright Oct 13, 1986                                             | 
X | Placed in public domain Oct 14, 1986                               |
X | Enjoy                                                              | 
X *--------------------------------------------------------------------*/
X
X#include <stdio.h>
X#include <errno.h>
X
Xextern int errno;
X
Xmain(argc,argv)
Xint argc;
Xchar **argv;
X{
X/*	structure of the info file - from MacBin.C
X	char zero1;
X	char nlen;
X	char name[63];
X	char type[4];		65	0101
X	char creator[4];	69
X	char flags;		73
X	char zero2;		74	0112
X	char location[6];	80
X	char protected;		81	0121
X	char zero3;		82	0122
X	char dflen[4];
X	char rflen[4];
X	char cdate[4];
X	char mdate[4];
X*/
X	FILE *fd, *ofd;
X	char bname[128];
X	char iname[128];
X	char dname[128];
X	char rname[128];
X	char oname[128];
X	char buf[128];
X	char * charp, * cmd;
X	char * rindex();
X	int verbose = 0;
X	if ((cmd = rindex(argv[0], '/')) != NULL)
X		++cmd;
X	else
X		cmd = argv[0];
X	if (argc > 1)
X		if (!strcmp(argv[1], "-v")) {
X			verbose = 1;
X			--argc;
X			++argv;
X		}
X	if (argc < 2) {
X		fprintf(stderr,"Usage:\n\t%s [-v] filename(s)\n",cmd);
X		exit(EINVAL);
X	}
X	while ( argc > 1 ) {
X		(void) bzero(buf,128);
X		if (( charp = rindex (argv[1], '.')) == NULL) 
X			strcpy(bname, argv[1]);
X		else {
X			*charp = '\0';
X			if ( strcmp(++charp,"data") && strcmp (charp, "info")
X			&& strcmp (charp, "rsrc")) {
X				*(--charp) = '.'; /* put it back */
X				strcpy(bname, argv[1]);
X			} else {
X				strcpy(bname,argv[1]);
X				--argc;
X				++argv;
X			}
X		}
X		strcpy(oname,bname);
X		strcat(oname,".bin");
X		strcpy(iname,bname);
X		strcat(iname,".info");
X		strcpy(dname,bname);
X		strcat(dname,".data");
X		strcpy(rname,bname);
X		strcat(rname,".rsrc");
X		if (verbose)
X			fprintf (stderr, "Converting %s\n", bname);
X		while ( argc > 1 && (!strcmp(argv[1], iname) ||
X		!strcmp(argv[1], dname) || !strcmp(argv[1], rname))) {
X			--argc;
X			++argv;
X		}
X		if ((ofd = fopen( oname, "w")) == NULL){
X			perror(oname);
X			--argc;
X			++argv;
X			continue;
X		}
X		if ((fd = fopen(iname,"r")) == NULL){
X			if ( verbose )
X				fprintf ( stderr, "No %s\n", iname);
X			fclose(ofd);
X			unlink(oname);
X			--argc;
X			++argv;
X			continue;
X		}
X		if (fread(buf, sizeof(*buf), 128, fd) > 0){
X			if (buf[74] & 0x40) buf[81] = '\1'; /* protected */
X			buf[74] = '\0'; /* clear zero2 */
X			fwrite(buf, sizeof(*buf),128, ofd);
X			(void) bzero(buf,128);
X		}
X		fclose(fd);
X		if ((fd = fopen(dname,"r")) == NULL){
X			if ( verbose )
X				fprintf ( stderr, "No %s\n", dname);
X			fclose(ofd);
X			unlink(oname);
X			--argc;
X			++argv;
X			continue;
X		}
X		while (fread(buf, sizeof(*buf),128, fd) > 0){
X			fwrite(buf, sizeof(*buf),128, ofd);
X			(void) bzero(buf,128);
X		}
X		fclose(fd);
X		if ((fd = fopen(rname,"r")) == NULL){
X			if ( verbose )
X				fprintf ( stderr, "No %s\n", rname);
X			fclose(ofd);
X			unlink(oname);
X			--argc;
X			++argv;
X			continue;
X		}
X		while (fread(buf, sizeof(*buf),128, fd) > 0){
X			fwrite(buf, sizeof(*buf),128, ofd);
X			(void) bzero(buf,128);
X		}
X		fclose(fd);
X		fclose(ofd);
X	}
X}
X
X#ifdef BZERO
X/*  File   : bzero.c
X    Author : Richard A. O'Keefe.
X    Updated: 23 April 1984
X    Defines: bzero()
X
X    bzero(dst, len) moves "len" 0 bytes to "dst".
X    Thus to clear a disc buffer to 0s do bzero(buffer, BUFSIZ).
X
X    Note: the "b" routines are there to exploit certain VAX order codes,
X    but the MOVC5 instruction will only move 65535 characters.   The asm
X    code is presented for your interest and amusement.
X*/
X
X#include "strings.h"
X
X#if	VaxAsm
X
Xvoid bzero(dst, len)
X    char *dst;
X    int len;
X    {
X	asm("movc5 $0,*4(ap),$0,8(ap),*4(ap)");
X    }
X
X#else  ~VaxAsm
X
Xvoid bzero(dst, len)
X    register char *dst;
X    register int len;
X    {
X	while (--len >= 0) *dst++ = 0;
X    }
X
X#endif	VaxAsm
X
X#endif /* BZERO */
SHAR_EOF
if test 3976 -ne "`wc -c < 'macbin.c'`"
then
	echo shar: "error transmitting 'macbin.c'" '(should have been 3976 characters)'
fi
fi
exit 0
#	End of shell archive
-- 

 Jim Budler
 Advanced Micro Devices, Inc.
 (408) 749-5806
 Usenet: {ucbvax,decwrl,ihnp4}!amdcad!jimb
 Compuserve:	72415,1200

dwd@sfsup.UUCP (David W. Dougherty) (10/21/86)

> X/*--------------------------------------------------------------------* 
> X | Version    : 2.0                                                   | 
> X | Author     : Jim Budler                                            | 
> X | Copyright Oct 13, 1986                                             | 
> X | Placed in public domain Oct 14, 1986                               |
> X | Enjoy                                                              | 
> X *--------------------------------------------------------------------*/

*** YOUR MESSAGE *** :-)

Great!  But where is the function rindex()???  Whoops!

jimb@amdcad.UUCP (Jim Budler) (10/23/86)

In article <552@sfsup.UUCP> dwd@sfsup.UUCP (David W. Dougherty) writes:
>> X/*--------------------------------------------------------------------* 
>> X | Version    : 2.0                                                   | 
>> X | Author     : Jim Budler                                            | 
>> X | Copyright Oct 13, 1986                                             | 
>> X | Placed in public domain Oct 14, 1986                               |
>> X | Enjoy                                                              | 
>> X *--------------------------------------------------------------------*/
>
>*** YOUR MESSAGE *** :-)
>
>Great!  But where is the function rindex()???  Whoops!

In the makefile, sort of:
+---------------
| # CFLAGS=-O -Drindex=strrchr	-DBZERO		# for sysV I think
| CFLAGS=-O
| 
| macbin:	macbin.c makefile
| 	cc $(CFLAGS) -s -o macbin macbin.c
+---------------
The makefile was included in the posting.
Change the comments and try it.

While I don't promise this makefile will work, I have included this time,
unlike in the first version, rindex(), (by way of the define in the makefile),
and bzero(), by way of the source ifdef'ed out by the BZERO define.
These are the only two Berkeleyisms in the code. That's the best I can do.
The bzero() source is from the stringlib package available from mod.sources.
It is not mine.

The bzero() is not absolutely necessary for the function of the program. If
removed, the last XMODEM packet may contain up to 128 bytes of the previous
packet. The mac will place the EOF at the right place.

Jim
-- 

 Jim Budler
 Advanced Micro Devices, Inc.
 (408) 749-5806
 Usenet: {ucbvax,decwrl,ihnp4}!amdcad!jimb
 Compuserve:	72415,1200