www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/03/05/23:33:04

Date: Sun, 5 Mar 95 22:30:31 EST
From: weslager AT me DOT udel DOT edu (Clint)
Organization: Mind Blast Innovations
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Neophyte Meets C, and Loses!

I'm sorry for making this message so large. However, I'm
trying to attach the problematic source code...

I've been trying to compile a piece of C code I didn't
write. I don't even program in C, yet, but I would appreciate
any comments youse all can give. I have all the up-to-date
files in the dj distribution (except, perhaps, m4 - I'm not
completely certain).

The error I am receiving is something along the lines of a
redeclaration of three variables: Ceor, Csum, and Crot.
>From my perusal of the code, it looks like it comes from the
'extern' lines.

Much to my chagrin, this code compiles quietly on the Sun's gcc.
I was hoping that it would do so with djgpp also!

I have been compiling with the command "gcc -c -O btoa.c".
When I used the -traditional flag, I didn't receive any btoa.c
errors, but I did get several stdio.h ones. *ppbt*

Unless you deem it necessary, there's not much reason to cc
the mailing list. It seems more a C thing than djgpp-related.

Clint

P.S. Just noticed the inclusion of that header file. I'll
attach the header file after the c code.

***

/* btoa.c */

/* Written by Paul Rutter, Joe Orost & Stefan Parmark. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "btoa.h"
#if USE_MACROS
#include "chksum.h"
#endif USE_MACROS

#define VERSION  "5.2"

LONG Ceor, Csum, Crot;  /* Checksums to verify archive validity. */
BYTE new_version, openoutput, buffer[BUFSIZE];
FILE *outfile;


void main(argc, argv)
int argc;
BYTE **argv;
{
  register BYTE openinput, error, ch, a_to_b, diagnosis, repair;
  register BYTE *infilename, *text;
  register FILE *infile;
  extern BYTE new_version, openoutput;
  extern FILE *outfile;
#ifdef AMIGA
  extern int _bufsiz;

  /* Change file buffer size. */
  _bufsiz = 10240;
#endif AMIGA

  error = openinput = openoutput = a_to_b = diagnosis = repair = FALSE;
  new_version = TRUE;
  infilename = NULL;

  /* Scan for '-' options. The rest must be file names. */
  while (!error && argc > 1 && *argv[1] == '-')
  {
    text = &argv[1][1];
    while (!error && (ch = *text++) != 0)
    {
      switch(ch)
      {
        case 'a' : /* Activate atob. */
                   a_to_b = TRUE;
                   break;
        case 'd' : /* Extract missing part from undamaged archive. */
                   diagnosis = TRUE;
                   break;
        case 'h' : /* Print help and abort execution. */
                   error = TRUE;
                   break;
        case 'o' : /* Use old btoa format. */
                   new_version = FALSE;
                   break;
        case 'r' : /* Repair damaged archive. */
                   repair = TRUE;
                   break;
        default  : error = TRUE;
      }
    }
    argv++;
    argc--;
  }

  if (argc > 3)
    error = TRUE;

  if (error)
    printhelp();
  else
  {
    /* If file name was given, try to open file. Otherwise use stdin. */
    if (argc > 1)
    {
      infilename = argv[1];
      if ((infile = fopen_read(infilename)) == NULL)
        error = TRUE;
      else
        openinput = TRUE;
    }
    else
      infile = stdin;
  }

  if (!error)
  {
    /* If file name was given, try to open file. Otherwise use stdout. */
    if (argc > 2 && !diagnosis && !repair)
    {
      if ((outfile = fopen_write(argv[2])) == NULL)
        error = TRUE;
      else
        openoutput = TRUE;
    }
    else
      outfile = stdout;
  }

  if (!error)
  {
    if (diagnosis)
      error = producerepair(infile);
    else if (repair)
      error = performrepair(infile);
    else if (a_to_b)
      error = atob(infile);
    else
      error = btoa(infile, infilename);
  }

  /* Close all opened files. */
  if (openinput)
    fclose(infile);
  if (openoutput)
    fclose(outfile);

  if (error)
    exit(1);
}


BYTE btoa(infile, infilename)
register FILE *infile;
register BYTE *infilename;
{
  register LONG codeword, filesize;
  register int ch1, ch2, ch3, ch4, readbytes;
  extern FILE *outfile;
  extern BYTE new_version, buffer[BUFSIZE];
  extern LONG Ceor, Csum, Crot;

  Ceor = Csum = Crot = 0;

  /* Write archive header. */
  if (new_version)
  {
    fprintf(outfile, "xbtoa5 %d %s Begin\n", MAXPERLINE,
        (infilename == NULL) ? "-" : truncname(infilename));
  }
  else
    fprintf(outfile, "xbtoa Begin\n");

  /* Encode entire input file. */
  filesize = 0;
  do
  {
    readbytes = fread(buffer, 1, 4, infile);

    if (readbytes < 4)
    {
      ch1 = (readbytes > 0) ? ((int)buffer[0] & 0xFF) : 0;
      ch2 = (readbytes > 1) ? ((int)buffer[1] & 0xFF) : 0;
      ch3 = (readbytes > 2) ? ((int)buffer[2] & 0xFF) : 0;
      ch4 = 0;
    }
    else
    {
      ch1 = (int)buffer[0] & 0xFF;
      ch2 = (int)buffer[1] & 0xFF;
      ch3 = (int)buffer[2] & 0xFF;
      ch4 = (int)buffer[3] & 0xFF;
    }

    if (readbytes > 0)
    {
      if (!new_version)
      {
        calcchecksum(ch1);
        calcchecksum(ch2);
        calcchecksum(ch3);
        calcchecksum(ch4);
      }

      codeword = (ch1 << 8) | ch2;
      codeword = (((codeword << 8) | ch3) << 8) | ch4;
      wordout(codeword);

      filesize += readbytes;
    }
  }
  while (readbytes == 4);

  asciiout(EOF);  /* Flush buffer. */

  /* Filesize is written twice as crude cross check. */
  fprintf(outfile, "xbtoa End N %ld %lx E %lx S %lx R %lx\n",
        filesize, filesize, Ceor, Csum, Crot);

  return(FALSE);  /* No errors discovered. */
}


/* Print help on how to use btoa. */
void printhelp()
{
  fprintf(stderr, "              Btoa version %s\n", VERSION);
  fprintf(stderr, "Written by Paul Rutter, Joe Orost & Stefan Parmark.\n");

  fprintf(stderr, "\nUsage: btoa [-{adhor}] [input file] [output file]\n");

  fprintf(stderr, "\nOptions:\n");
  fprintf(stderr, "-h  Shows this help list.\n");
  fprintf(stderr, "-a  Use atob rather than btoa.\n");
  fprintf(stderr, "-o  Use old version of btoa.\n");
  fprintf(stderr, "-d  Extract repair file from diagnosis file.\n");
  fprintf(stderr, "-r  Repair archive from repair file.\n");

  fprintf(stderr, "\nExamples:\n");
  fprintf(stderr, "  btoa -h\n");
  fprintf(stderr, "  btoa [input binary file] [output archive file]\n");
  fprintf(stderr, "  btoa -o [input binary file] [output archive file]\n");
  fprintf(stderr, "  btoa -a [input archive file] [output binary file]\n");
  fprintf(stderr, "  btoa -d [undamaged archive file]\n");
  fprintf(stderr, "  btoa -r [damaged archive file]\n");
}


#if !USE_MACROS
/* Update file checksums. */
void calcchecksum(ch)
register int ch;
{
  extern LONG Ceor, Csum, Crot;

  Ceor ^= ch;
  Csum += ch + 1;

  if (Crot & 0x80000000L)
    ch ++;
  Crot <<= 1;
  Crot += ch;
}
#endif !USE_MACROS


/* Encode 4 binary bytes to 5 ascii bytes. */
void wordout(codeword)
register LONG codeword;
{
  register int tmp, quote;
  extern BYTE new_version;

  if (codeword == 0)
    /* Encode 4 zeros as a 'z'. */
    asciiout('z');
  else if (new_version && codeword == 0x20202020)
    /* Encode 4 spaces as a 'y'. */
    asciiout('y');
  else
  {
    tmp = 0;

    /* Extra calculations because some machines don't support */
    /* unsigned longwords.                                    */
    if (codeword < 0)
    {
      tmp = 32;
      codeword -= (LONG)(85L * 85 * 85 * 85 * 32);
    }
    if (codeword < 0)
    {
      tmp = 64;
      codeword -= (LONG)(85L * 85 * 85 * 85 * 32);
    }

    /* Write 5 ascii bytes representing 4 binary bytes. */

    quote = codeword / (LONG)(85L * 85 * 85 * 85);
    codeword -= quote * (LONG)(85L * 85 * 85 * 85);
    asciiout(ENCODE(quote + tmp));

    quote = codeword / (LONG)(85L * 85 * 85);
    codeword -= quote * (LONG)(85L * 85 * 85);
    asciiout(ENCODE(quote));

    quote = codeword / (LONG)(85L * 85);
    codeword -= quote * (LONG)(85L * 85);
    asciiout(ENCODE(quote));

    quote = (int)codeword / 85;
    codeword -= quote * 85;
    asciiout(ENCODE(quote));

    asciiout(ENCODE((int)codeword));
  }
}


/* Write ch to outfile. Write '\n' for every line. */
void asciiout(ch)
register int ch;
{
  static WORD linepos = 0;
  extern FILE *outfile;
  extern LONG Csum;
  extern BYTE new_version;

  if (ch == EOF)  /* Signal to flush buffer. */
  {
    /* Linepos == 0 means '\n' just written in asciiout(). This */
    /* avoids bug in BITNET, which changes blank line to spaces. */
    if (linepos != 0)
    {
      if (new_version)
        fputc(ENCODE(Csum % 85), outfile); /* Checksum for every line. */
      fputc('\n', outfile);
    }
  }
  else
  {
    fputc(ch, outfile);
    linepos ++;

    if (new_version)
    {
      calcchecksum(ch);
      if (linepos >= (MAXPERLINE-1))
      {
        fputc(ENCODE(Csum % 85), outfile); /* Checksum for every line. */
        fputc('\n', outfile);
        linepos = 0;
      }
    }
    else  /* Old version */
      if (linepos >= MAXPERLINE)
      {
        fputc('\n', outfile);
        linepos = 0;
      }

  }
}


/* Remove paths from a file name. */
BYTE *truncname(name)
register BYTE *name;
{
  register BYTE ch, *newname;

  newname = name;
  while ((ch = *name++) != 0)
    if (ch == '/' || ch == ':')
      newname = name;

  return(newname);
}

***

/* btoa.h */

#define MAXPERLINE      78
#define BUFSIZE         100
#define TRUE            1
#define FALSE           0
#define USE_MACROS      TRUE

#define BYTE            char
#define WORD            short
#define LONG            long

#define ENCODE(ch)      ( (int) ((ch) + '!') )
#define DECODE(ch)      ( (int) ((ch) - '!') )

struct Diagnosis
{
  LONG startpos, endpos;  /* Line before and after erroneous area */
  struct Diagnosis *next, *last;
};


/*
Following functions have been converted to macros:
  calcchecksum()
*/


#if LATTICE  /* Prototypes for Lattice C */

void asciiout(int), exit(int),
     intodiagnosislist(struct Diagnosis *, LONG, LONG),
     outdiagnosislist(struct Diagnosis *, LONG *, LONG *), printhelp(void),
     producediagnosis(struct Diagnosis *, FILE *), wordout(LONG);

BYTE atob(FILE *), btoa(FILE *, BYTE *), copyfile(FILE *, FILE *, BYTE *),
     decode_line(BYTE *, int), new_decodefile(FILE *, LONG *, LONG, int),
     old_decodefile(FILE *, LONG *), performrepair(FILE *),
     producerepair(FILE *), readbuffer(BYTE *, BYTE *, FILE *),
     *truncname(BYTE *);

int  nextbyte(FILE *);

FILE *fopen_read(BYTE *), *fopen_write(BYTE *);

#if USE_MACROS
void calcchecksum(int);
#else
#include "chksum.h"
#endif USE_MACROS

#else !LATTICE  /* For compilers which don't know about prototypes. */

void asciiout(), exit(), intodiagnosislist(), outdiagnosislist(),
     printhelp(), producediagnosis(), wordout();

BYTE atob(), btoa(), copyfile(), decode_line(), new_decodefile(),
     old_decodefile(), performrepair(), producerepair(), readbuffer(),
     *truncname();

int  nextbyte();

FILE *fopen_read(), *fopen_write();

#if USE_MACROS
void calcchecksum();
#else
#include "chksum.h"
#endif USE_MACROS

#endif LATTICE

***
end attachments

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019