summaryrefslogtreecommitdiff
path: root/support/endian.c
blob: 3c92ae8fbd9c285c67f4fbd6523a628d1d4ac679 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* endian.c -- A trick for determining the byte order of a machine. */

/* Copyright (C) 1993 Free Software Foundation, Inc.

   This file is part of GNU Bash, the Bourne Again SHell.

   Bash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Bash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <sys/types.h>
#include <stdio.h>
#include "bashansi.h"

/* The name of this program, as taken from argv[0]. */
char *progname;

/* The name of the source file that this code is made from. */
char source_name[256];

/* The name of the define.  Either "BIG_ENDIAN" or "LITTLE_ENDIAN". */
char *endian_define;

char string[9];
char nstring[9];

/* Stuff "1234" into a long, and compare it against a character string
   "1234".  If the results are EQ, the machine is big endian like a 68000
   or Sparc, otherwise it is little endian, like a Vax, or 386. */
main (argc, argv)
     int argc;
     char **argv;
{
#if defined (__STDC__)
  register size_t i;
#else
  register int i;
#endif /* !__STDC__ */
  FILE *stream = (FILE *)NULL;
  char *stream_name = "stdout";
  union {
      unsigned long l;
      char s[sizeof (long)];
  } u;

  progname = argv[0];

  for (i = strlen (progname); i > 0; i--)
    if (progname[i] == '/')
      {
	progname = progname + i + 1;
	break;
      }

  strcpy (source_name, progname);
  for (i = strlen (source_name); i > 0; i--)
    if (source_name[i] == '.')
      {
	source_name[i] = '\0';
	break;
      }

  strcat (source_name, ".c");

  if (argc == 1)
    {
      stream_name = "stdout";
      stream = stdout;
    }
  else if (argc == 2)
    {
      stream_name = argv[1];
      stream = fopen (stream_name, "w");
    }
  else
    {
      fprintf (stderr, "Usage: %s [output-file]\n", progname);
      exit (1);
    }

  if (!stream)
    {
      fprintf (stderr, "%s: %s Cannot be opened or written to.\n",
	       progname, stream_name);
      exit (2);
    }

  if (sizeof (long int) == 4)
    {
      u.l = 0x04030201L;
      (void) strcpy (string, "4321");
    }
  else if (sizeof (long int) == 8)
    {
#if defined (__GNUC__)
      unsigned long fake_out_gcc;

      fake_out_gcc = (0x08070605L << 31);
      fake_out_gcc = (fake_out_gcc << 1);
      u.l = fake_out_gcc | 0x04030201L;
#else
      u.l = (0x08070605L << 32) | 0x04030201L;
#endif /* !__GNUC__ */
      (void) strcpy (string, "87654321");
    }
  else
    {
      fprintf (stderr,
	       "%s: sizeof (long int) = %d, which isn't handled here.\n",
	       progname, sizeof (long int));
      exit (2);
    }

  for (i = 0; i < sizeof (long); i++)
    nstring[i] = u.s[i] + '0';
  nstring[i] = '\0';

  if (strcmp (nstring, string) == 0)
    endian_define = "BIG_ENDIAN";
  else
    endian_define = "LITTLE_ENDIAN";

  fprintf (stream, "/* %s - Define BIG or LITTLE endian. */\n\n", stream_name);
  fprintf (stream,
"/* This file was automatically created by `%s'.  You shouldn't\n\
   edit this file, because your changes will be overwritten.  Instead,\n\
   edit the source code file `%s'. */\n\n",
	   progname, source_name);

  fprintf (stream, "#if !defined (%s)\n", endian_define);
  fprintf (stream, "#  define %s\n", endian_define);
  fprintf (stream, "#endif /* %s */\n", endian_define);

  if (stream != stdout)
    fclose (stream);

  exit (0);
}