[comp.sys.ibm.pc] Fix to UUENCODE for MSDOS

dick@plx.UUCP (Dick Flanagan) (10/29/87)

Two recent posters of UUENCODE for MSDOS share a common short-coming.  While
both open the input file to UUENCODE in binary mode, neither of them allow
that stdin may also be used as the input source and it, too, needs to be
conditioned for binary mode.  The following code segment from UUENCODE will
solve this problem:

/*
 * uuencode [input] output
 *
 * Encode a file so it can be mailed to a remote system.
 */
#include <stdio.h>
#include <fcntl.h>                    /* this is required for setmode() */
#include <sys/types.h>
#include <sys/stat.h>

/* ENC is the basic 1 character encoding function to make a char printing */
#define ENC(c) (((c) & 077) + ' ')

main(argc, argv)
char **argv;
{
	FILE *in;
	struct stat sbuf;
	int mode;

	/* optional 1st argument */
	if (argc > 2) {
		if ((in = fopen(argv[1], "r")) == NULL) {
			perror(argv[1]);
			exit(1);
		}
		argv++; argc--;
	} else
		in = stdin;

	if (argc != 2) {
		printf("Usage: uuencode [infile] remotefile\n");
		exit(2);
	}

#ifdef MSDOS
	setmode(fileno(in), O_BINARY);      /* set input file to binary mode */
#endif /* MSDOS */

	/* figure out the input file mode */
	fstat(fileno(in), &sbuf);
	mode = sbuf.st_mode & 0777;
	printf("begin %o %s\n", mode, argv[1]);

	encode(in, stdout);

	printf("end\n");
	exit(0);
}
-- 
Dick Flanagan, W6OLD                          I'll take a drug test when
UUCP:  ...!ucbvax!sun!plx!dick                Reagan takes an IQ test.
GEnie: FLANAGAN