1Received: from CU-ARPA.CS.CORNELL.EDU by loki.cs.cornell.edu (5.61/I-1.91f)
2	id AA25874; Wed, 18 Jul 90 12:02:22 -0400
3Message-Id: <9007181320.AA24810@cu-arpa.cs.cornell.edu>
4Received: from CORNELLC.CIT.CORNELL.EDU by cu-arpa.cs.cornell.edu (5.61+2/1.91d)
5	id AA24810; Wed, 18 Jul 90 09:20:21 -0400
6Received: from CORNELLC by CORNELLC.cit.cornell.edu (IBM VM SMTP R1.2.1MX) with BSMTP id 6769; Wed, 18 Jul 90 09:18:46 EDT
7Received: from CAS.BITNET (MAILER) by CORNELLC (Mailer R2.05X) with BSMTP id
8 5378; Wed, 18 Jul 90 09:18:38 EDT
9From: swl26%CAS.BITNET@CORNELLC.cit.cornell.edu
10Date: Wed, 18 Jul 1990 09:16 EDT
11Subject: Re(2): diffs for mvs port of flex-2.3
12In-Reply-To: Your message of Tue, 17 Jul 90 17:42:3
13To: vern@cs.cornell.edu
14
15Sorry about the trailing blank problem.  It's farily common with data sent
16through bitnet paths, but ever the optimist ...
17
18>I think there should be an 'M' at the beginning of the second line.
19
20This isn't a problem.  I believe that the first byte of the line indicates
21it's length (in some manner).
22
23Rather than re-send the data, how about a uudecode that compensates for
24the trailing blank problem?  I manually mangled the uuencoded file and ran
25the following decode, and it seemed to work.
26
27#! /bin/sh
28# This is a shell archive.  Remove anything before this line, then feed it
29# into a shell via "sh file" or similar.  To overwrite existing files,
30# type "sh file -c".
31# The tool that generated this appeared in the comp.sources.unix newsgroup;
32# send mail to comp-sources-unix@uunet.uu.net if you want that tool.
33# If this archive is complete, you will see the following message at the end:
34#               "End of shell archive."
35# Contents:  uudecode.c
36# Wrapped by swl26@swl26aws on Wed Jul 18 08:59:24 1990
37PATH=/bin:/usr/bin:/usr/ucb ; export PATH
38if test -f 'uudecode.c' -a "${1}" != "-c" ; then
39  echo shar: Will not clobber existing file \"'uudecode.c'\"
40else
41echo shar: Extracting \"'uudecode.c'\" \(6418 characters\)
42sed "s/^X//" >'uudecode.c' <<'END_OF_FILE'
43X/* #ifndef lint
44Xstatic char sccsid[] = "@(#)uudecode.c  5.3-1 (Berkeley) 9/1/87";
45X#endif */
46X
47X/* Written by Mark Horton */
48X/* Modified by ajr (Alan J Rosenthatl,flaps@utcsri.UUCP) to use checksums */
49X/* Modified by fnf (Fred Fish,well!fnf) to use Keith Pyle's suggestion for
50X   compatibility */
51X/* Modified by bcn (Bryce Nesbitt,ucbvax!cogsci!bryce) to fix a misleading
52X   error message on the Amiga port, to fix a bug that prevented decoding
53X   certain files, to work even if trailing spaces have been removed from a
54X   file, to check the filesize (if present), to add some error checking, to
55X   loop for multiple decodes from a single file, and to handle common
56X   BITNET mangling.  Also kludged around a missing string function in Aztec
57X   C */
58X
59X/*
60X * uudecode [input]
61X *
62X * Decode a file encoded with uuencode.  WIll extract multiple encoded
63X * modules from a single file. Can deal with most mangled files, including
64X * BITNET.
65X */
66X
67X#include <stdio.h>
68X#include <ctype.h>
69X
70X#ifdef AMIGA
71X#define AMIGA_LATTICE      /* Set for Amiga Lattice C */
72X#define MCH_AMIGA
73X#define MPU68000
74X#endif
75X
76X#ifdef unix
77X#include <pwd.h>
78X#include <sys/types.h>
79X#include <sys/stat.h>
80X#endif
81X
82X#define SUMSIZE 64
83X#define DEC(c) (((c) - ' ') & 077)    /* single character decode */
84X
85Xmain(argc, argv)
86Xchar **argv;
87X{
88XFILE   *in, *out;
89Xint    through_loop=0; /* Dejavu indicator */
90Xint    mode;           /* file's mode (from header) */
91Xlong   filesize;       /* theoretical file size (from header) */
92Xchar   dest[128];
93Xchar   buf[80];
94X
95X#ifdef AMIGA_LATTICE
96Xextern int Enable_Abort;
97X       Enable_Abort=1;
98X#endif
99X
100X    /* A filename can be specified to be uudecoded, or nothing can
101X    be specified, and the input will come from STDIN */
102X
103X    switch (argc)
104X       {
105X       case 1:
106X       in=stdin;
107X       break;
108X
109X       case 2:
110X       if ((in = fopen(argv[1], "r")) == NULL)
111X           {
112X           fprintf(stderr, "ERROR: can't find %s\n", argv[1]);
113X           fprintf(stderr, "USAGE: uudecode [infile]\n");
114X           exit(10);
115X           }
116X       break;
117X
118X       default:
119X       fprintf(stderr, "USAGE: uudecode [infile]\n");
120X       exit(11);
121X       break;
122X       }
123X
124X    /* Loop through file, searching for headers.  Decode anything with a
125X       header, complain if there where no headers. */
126X
127Xfor (;;)
128X{
129X    /* search file for header line */
130X    for (;;)
131X       {
132X       if (fgets(buf, sizeof buf, in) == NULL)
133X           {
134X           if (!through_loop)
135X               {
136X               fprintf(stderr, "ERROR: no `begin' line!\n");
137X               exit(12);
138X               }
139X           else
140X               {
141X               exit(0);
142X               }
143X           }
144X       if (strncmp(buf, "begin ", 6) == 0)
145X           break;
146X       }
147X    sscanf(buf, "begin %o %s", &mode, dest);
148X
149X#ifdef unix
150X    /* handle ~user/file format */
151X    if (dest[0] == '~')
152X       {
153X       char *sl;
154X       struct passwd *getpwnam();
155X       char *index();
156X       struct passwd *user;
157X       char dnbuf[100];
158X
159X       sl = index(dest, '/');
160X       if (sl == NULL)
161X           {
162X           fprintf(stderr, "Illegal ~user\n");
163X               exit(13);
164X           }
165X       *sl++ = 0;
166X       user = getpwnam(dest+1);
167X       if (user == NULL)
168X           {
169X           fprintf(stderr, "No such user as %s\n", dest);
170X           exit(14);
171X           }
172X       strcpy(dnbuf, user->pw_dir);
173X       strcat(dnbuf, "/");
174X       strcat(dnbuf, sl);
175X       strcpy(dest, dnbuf);
176X       }
177X#endif
178X
179X    /* create output file */
180X    if ((out = fopen(dest, "w")) == NULL)
181X       {
182X       fprintf(stderr, "ERROR: can't open output file %s\n", dest);
183X       exit(15);
184X       }
185X#ifdef unix
186X    chmod(dest, mode);
187X#endif
188X
189X    decode(in, out, dest);
190X
191X    if (fgets(buf, sizeof buf, in) == NULL || strncmp(buf,"end",3))
192X       {              /* don't be overly picky about newline ^ */
193X       fprintf(stderr, "ERROR: no `end' line\n");
194X       exit(16);
195X       }
196X
197X    if (!(fgets(buf,sizeof buf,in) == NULL || strncmp(buf,"size ",3)))
198X       {
199X       sscanf(buf, "size %ld", &filesize);
200X       if (ftell(out) != filesize)
201X           {
202X           fprintf(stderr, "ERROR: file should have been %ld bytes long but was
203X           exit(17);
204X           }
205X       }
206X    through_loop = 1;
207X}   /* forever */
208X}   /* main */
209X
210X/*
211X * Copy from in to out, decoding as you go.
212X * If a return or newline is encountered too early in a line, it is
213X * assumed that means that some editor has truncated trailing spaces.
214X */
215Xdecode(in, out, dest)
216XFILE *in;
217XFILE *out;
218Xchar *dest;
219X{
220Xchar buf[81];
221Xchar *bp;
222Xint nosum=0;
223X#ifndef unix
224Xextern errno;
225X#endif
226Xregister int j;
227Xregister int n;
228Xint checksum, line;
229X
230X    for (line = 1; ; line++)   /* for each input line */
231X       {
232X       if (fgets(buf, sizeof buf, in) == NULL)
233X           {
234X           fprintf(stderr, "ERROR: input ended unexpectedly!\n");
235X           exit(18);
236X           }
237X
238X       /* Pad end of lines in case some editor truncated trailing
239X          spaces */
240X
241X       for (n=0;n<79;n++)  /* search for first \r, \n or \000 */
242X           {
243X           if (buf[n]=='\176')     /* If BITNET made a twiddle, */
244X               buf[n]='\136';     /* we make a caret           */
245X           if (buf[n]=='\r'||buf[n]=='\n'||buf[n]=='\000')
246X               break;
247X           }
248X       for (;n<79;n++)     /* when found, fill rest of line with space */
249X           {
250X           buf[n]=' ';
251X           }
252X       buf[79]=0;          /* terminate new string */
253X
254X       checksum = 0;
255X       n = DEC(buf[0]);
256X       if (n <= 0)
257X           break;      /* 0 bytes on a line??  Must be the last line */
258X
259X       bp = &buf[1];
260X
261X       /* FOUR input characters go into each THREE output charcters */
262X
263X       while (n >= 4)
264X           {
265X           j = DEC(bp[0]) << 2 | DEC(bp[1]) >> 4; putc(j, out); checksum += j;
266X           j = DEC(bp[1]) << 4 | DEC(bp[2]) >> 2; putc(j, out); checksum += j;
267X           j = DEC(bp[2]) << 6 | DEC(bp[3]);      putc(j, out); checksum += j;
268X           checksum = checksum % SUMSIZE;
269X           bp += 4;
270X           n -= 3;
271X           }
272X
273X           j = DEC(bp[0]) << 2 | DEC(bp[1]) >> 4;
274X               checksum += j;
275X               if (n >= 1)
276X                   putc(j, out);
277X           j = DEC(bp[1]) << 4 | DEC(bp[2]) >> 2;
278X               checksum += j;
279X               if (n >= 2)
280X                   putc(j, out);
281X           j = DEC(bp[2]) << 6 | DEC(bp[3]);
282X               checksum += j;
283X               if (n >= 3)
284X                   putc(j, out);
285X           checksum = checksum % SUMSIZE;
286X           bp += 4;
287X           n -= 3;
288X
289X#ifndef unix
290X        /* Error checking under UNIX??? You must be kidding... */
291X        /* Check if an error occured while writing to that last line */
292X       if (errno)
293X           {
294X           fprintf(stderr, "ERROR: error writing to %s\n",dest);
295X           exit(19);
296X           }
297X#endif
298X
299X       /* The line has been decoded; now check that sum */
300X
301X       nosum |= !isspace(*bp);
302X       if (nosum)                      /* Is there a checksum at all?? */
303X           {
304X           if (checksum != DEC(*bp))   /* Does that checksum match? */
305X               {
306X               fprintf(stderr, "ERROR: checksum mismatch decoding %s, line %d.\
307X               }
308X           }   /* sum */
309X    }  /* line */
310X}   /* function */
311X
312X#ifdef unix
313X/*
314X * Return the ptr in sp at which the character c appears;
315X * 0 if not found
316X */
317Xchar *
318Xindex(sp, c)
319Xregister char *sp, c;
320X{
321X    do
322X       {
323X       if (*sp == c)
324X           return(sp);
325X       }
326X    while (*sp++);
327X
328X    return(0);
329X}
330X#endif unix
331X
332
333END_OF_FILE
334echo shar: NEWLINE appended to \"'uudecode.c'\"
335if test 6419 -ne `wc -c <'uudecode.c'`; then
336    echo shar: \"'uudecode.c'\" unpacked with wrong size!
337fi
338# end of 'uudecode.c'
339fi
340echo shar: End of shell archive.
341exit 0
342