1 /* word_io.c -- word oriented I/O
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
29 # define _(Text) gettext (Text)
32 #define textdomain(Domain)
33 #define bindtextdomain(Package, Directory)
36 # define N_(String) gettext_noop (String)
38 /* We used to use (String) instead of just String, but apparently ISO C
39 * doesn't allow this (at least, that's what HP said when someone reported
40 * this as a compiler bug). This is HP case number 1205608192. See
41 * also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11250 (which references
42 * ANSI 3.5.7p14-15). The Intel icc compiler also rejects constructs
43 * like: static const char buf[] = ("string");
45 # define N_(String) String
49 /* Swap bytes in 32 bit value. This code is taken from glibc-2.3.3. */
51 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
52 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
55 decode_value(const unsigned char data
[],
57 int *endian_state_flag
,
62 if (*endian_state_flag
= GetwordEndianStateInitial
)
64 int testflag
= GetwordEndianStateNative
;
66 val
= decode_value(data
, limit
, &testflag
, filename
);
73 testflag
= GetwordEndianStateSwab
;
74 val
= decode_value(data
, limit
, &testflag
, filename
);
77 /* Aha, now we know we have to byte-swap. */
79 _("Warning: locate database %s was built with a different byte order"),
80 quotearg_n_style(0, locale_quoting_style
, filename
));
81 *endian_state_flag
= testflag
;
93 if (*endian_state_flag
== GetwordEndianStateSwab
)
104 const char *filename
,
107 int *endian_state_flag
)
109 enum { WORDBYTES
=4 };
110 unsigned char data
[4];
114 bytes_read
= fread(data
, WORDBYTES
, 1, fp
);
117 const char * quoted_name
= quotearg_n_style(0, locale_quoting_style
,
119 /* Distinguish between a truncated database and an I/O error.
120 * Either condition is fatal.
123 error(1, 0, _("Unexpected EOF in %s"), quoted_name
);
125 error(1, errno
, "error reading a word from %s", quoted_name
);
129 return decode_value(data
, maxvalue
, endian_state_flag
, filename
);