1 /* endian.c -- A trick for determining the byte order of a machine. */
3 /* Copyright (C) 1993 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <sys/types.h>
25 /* The name of this program, as taken from argv[0]. */
28 /* The name of the source file that this code is made from. */
29 char source_name[256];
31 /* The name of the define. Either "BIG_ENDIAN" or "LITTLE_ENDIAN". */
37 /* Stuff "1234" into a long, and compare it against a character string
38 "1234". If the results are EQ, the machine is big endian like a 68000
39 or Sparc, otherwise it is little endian, like a Vax, or 386. */
44 #if defined (__STDC__)
48 #endif /* !__STDC__ */
49 FILE *stream = (FILE *)NULL;
50 char *stream_name = "stdout";
53 char s[sizeof (long)];
58 for (i = strlen (progname); i > 0; i--)
59 if (progname[i] == '/')
61 progname = progname + i + 1;
65 strcpy (source_name, progname);
66 for (i = strlen (source_name); i > 0; i--)
67 if (source_name[i] == '.')
69 source_name[i] = '\0';
73 strcat (source_name, ".c");
77 stream_name = "stdout";
82 stream_name = argv[1];
83 stream = fopen (stream_name, "w");
87 fprintf (stderr, "Usage: %s [output-file]\n", progname);
93 fprintf (stderr, "%s: %s Cannot be opened or written to.\n",
94 progname, stream_name);
98 if (sizeof (long int) == 4)
101 (void) strcpy (string, "4321");
103 else if (sizeof (long int) == 8)
105 #if defined (__GNUC__)
106 unsigned long fake_out_gcc;
108 fake_out_gcc = (0x08070605L << 31);
109 fake_out_gcc = (fake_out_gcc << 1);
110 u.l = fake_out_gcc | 0x04030201L;
112 u.l = (0x08070605L << 32) | 0x04030201L;
113 #endif /* !__GNUC__ */
114 (void) strcpy (string, "87654321");
119 "%s: sizeof (long int) = %d, which isn't handled here.\n",
120 progname, sizeof (long int));
124 for (i = 0; i < sizeof (long); i++)
125 nstring[i] = u.s[i] + '0';
128 if (strcmp (nstring, string) == 0)
129 endian_define = "BIG_ENDIAN";
131 endian_define = "LITTLE_ENDIAN";
133 fprintf (stream, "/* %s - Define BIG or LITTLE endian. */\n\n", stream_name);
135 "/* This file was automatically created by `%s'. You shouldn't\n\
136 edit this file, because your changes will be overwritten. Instead,\n\
137 edit the source code file `%s'. */\n\n",
138 progname, source_name);
140 fprintf (stream, "#if !defined (%s)\n", endian_define);
141 fprintf (stream, "# define %s\n", endian_define);
142 fprintf (stream, "#endif /* %s */\n", endian_define);
144 if (stream != stdout)