[BACK]Return to io.c CVS log [TXT][DIR] Up to [contributed] / dgamelaunch-openbsd

Annotation of dgamelaunch-openbsd/io.c, Revision 1.1.1.1

1.1       rubenllo    1: /*
                      2:  * Copyright (c) 2000 Satoru Takabayashi <satoru@namazu.org>
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *     This product includes software developed by the University of
                     16:  *     California, Berkeley and its contributors.
                     17:  * 4. Neither the name of the University nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  */
                     33:
                     34: #include <stdio.h>
                     35: #include <stdlib.h>
                     36: #include <assert.h>
                     37: #include <string.h>
                     38: #include <errno.h>
                     39:
                     40: #include "ttyrec.h"
                     41:
                     42: #define SWAP_ENDIAN(val) ((unsigned int) ( \
                     43:     (((unsigned int) (val) & (unsigned int) 0x000000ffU) << 24) | \
                     44:     (((unsigned int) (val) & (unsigned int) 0x0000ff00U) <<  8) | \
                     45:     (((unsigned int) (val) & (unsigned int) 0x00ff0000U) >>  8) | \
                     46:     (((unsigned int) (val) & (unsigned int) 0xff000000U) >> 24)))
                     47:
                     48: static int
                     49: is_little_endian ()
                     50: {
                     51:   static int retval = -1;
                     52:
                     53:   if (retval == -1)
                     54:     {
                     55:       int n = 1;
                     56:       char *p = (char *) &n;
                     57:       char x[] = { 1, 0, 0, 0 };
                     58:
                     59:       assert (sizeof (int) == 4);
                     60:
                     61:       if (memcmp (p, x, 4) == 0)
                     62:         {
                     63:           retval = 1;
                     64:         }
                     65:       else
                     66:         {
                     67:           retval = 0;
                     68:         }
                     69:     }
                     70:
                     71:   return retval;
                     72: }
                     73:
                     74: static int
                     75: convert_to_little_endian (int x)
                     76: {
                     77:   if (is_little_endian ())
                     78:     {
                     79:       return x;
                     80:     }
                     81:   else
                     82:     {
                     83:       return SWAP_ENDIAN (x);
                     84:     }
                     85: }
                     86:
                     87: int
                     88: read_header (FILE * fp, Header * h)
                     89: {
                     90:   int buf[3];
                     91:   int numread;
                     92:   long offset;
                     93:
                     94:   offset = ftell (fp);
                     95:   numread = fread (buf, sizeof (int), 3, fp);
                     96:
                     97:   /* odd but possible race condition when full header isn't read because
                     98:    * it isn't there yet. */
                     99:   if (numread < 3)
                    100:     {
                    101:       fseek (fp, offset, SEEK_SET);
                    102:       return 0;
                    103:     }
                    104:
                    105:   h->tv.tv_sec = convert_to_little_endian (buf[0]);
                    106:   h->tv.tv_usec = convert_to_little_endian (buf[1]);
                    107:   h->len = convert_to_little_endian (buf[2]);
                    108:
                    109:   return 1;
                    110: }
                    111:
                    112: int
                    113: write_header (FILE * fp, Header * h)
                    114: {
                    115:   int buf[3];
                    116:
                    117:   buf[0] = convert_to_little_endian (h->tv.tv_sec);
                    118:   buf[1] = convert_to_little_endian (h->tv.tv_usec);
                    119:   buf[2] = convert_to_little_endian (h->len);
                    120:
                    121:   if (fwrite (buf, sizeof (int), 3, fp) == 0)
                    122:     {
                    123:       return 0;
                    124:     }
                    125:
                    126:   return 1;
                    127: }
                    128:
                    129: static char *progname = "";
                    130: void
                    131: set_progname (const char *name)
                    132: {
                    133:   progname = strdup (name);
                    134: }
                    135:
                    136: FILE *
                    137: efopen (const char *path, const char *mode)
                    138: {
                    139:   FILE *fp = fopen (path, mode);
                    140:   if (fp == NULL)
                    141:     {
                    142:       fprintf (stderr, "%s: %s: %s\n", progname, path, strerror (errno));
                    143:       exit (EXIT_FAILURE);
                    144:     }
                    145:   return fp;
                    146: }

CVSweb