Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: mbt@ast.cam.ac.uk (Mark Taylor)
Subject: Re: Reading sun binaries on DEC-Alpha f77?
Date: 1998/08/20	[thread overview]
Message-ID: <6rh3nm$bre$1@pegasus.csx.cam.ac.uk> (raw)
In-Reply-To: r74g1f2tkrr.fsf@caesar.ifm.uni-kiel.de

Joerg Lippmann (jlippmann@ifm.uni-kiel.de) wrote:

: Hi!

: Is there an easy way to read sun binary data files with DEC-Alpha
: f77-programs?

I knocked up the following short C program to convert files from 
little-endian to big-endian or the other way round.
Note it will only work if the file has nothing but 4-byte words in
(e.g. all REALs or INTEGERs).

#include <stdio.h>


 /*  This is a very simple program to swap 4-byte words from 
  *  big-endian to little-endian (or vice versa).
  *  Useful, e.g., for moving REALs or INTEGERS between Suns and DECs.
  *  Note that this only works in virtue of the fact that both
  *  use the same representation for REALs and INTEGERs - at least 
  *  this is true at Digital Unix 4.0c and Solaris 2.6.  
  *  However there is no guarantee that this will hold for arbitrary
  *  operating systems with differing byte-order.  For a more general
  *  solution one should use XDR or something.
  */

int main (int argc, char *argv[]) {
   char buf[4], buf2[4];
   size_t b;
   FILE *in, *out;


   if ((argc > 3) || (argc > 1 && *argv[1] == '-')) {
      printf ("Usage:  %s [ infile [ outfile ] ]\n", argv[0]);
      exit (1);
   }

   in  = (argc >= 2) ? fopen (argv[1], "rb") : fdopen (0, "rb");
   if (!in) {
      perror (argv[0]);
      exit (1);
   }

   out = (argc == 3) ? fopen (argv[2], "wb") : fdopen (1, "wb");
   if (!out) {
      perror (argv[0]);
      exit (1);
   }

   while (fread (buf, 4, 1, in) == 1) {
      buf2[0] = buf[3];
      buf2[1] = buf[2];
      buf2[2] = buf[1];
      buf2[3] = buf[0];
      if (fwrite (buf2, 4, 1, out) != 1) {
          printf ("%s:  Error writing output file\n", argv[0]);
          exit (1);
      }
   }
   if (ferror(in) != 0) {
      printf ("%s:  Error reading input file\n", argv[0]);
      exit(1);
   }
}

---------------------------------------------------------------------------
| Mark Taylor                       Institute of Astronomy, Cambridge, UK |
| mbt@ast.cam.ac.uk    http://dougal.chm.bris.ac.uk/~mbt/    01223 337535 |
---------------------------------------------------------------------------



  parent reply	other threads:[~1998-08-20  4:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-08-12  4:00 Reading sun binaries on DEC-Alpha f77? Joerg Lippmann
1998-08-19  4:00 ` James W. Caldwell
1998-08-20  4:00   ` Mike Iglesias
1998-08-20  4:00 ` Mark Taylor [this message]
1998-08-20  4:00 ` Petrie L M
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox