-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmachdep.c
94 lines (80 loc) · 1.61 KB
/
machdep.c
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
/*
// machdep.c
//
// Machine dependencies checker
//
// (C) R.P.Bellis 1993
*/
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
void byte_order(FILE *f)
{
union {
struct {
uint8_t field1;
uint8_t field2;
} bytes;
uint16_t value;
} tmp;
tmp.value = 0;
tmp.bytes.field1 = 1;
if (tmp.value == 0x0001) {
fprintf(f, "#define MACH_BYTE_ORDER_LSB_FIRST\n");
} else if (tmp.value == 0x0100) {
fprintf(f, "#define MACH_BYTE_ORDER_MSB_FIRST\n");
} else {
fprintf(stderr, "cannot determine byte order\n");
exit(EXIT_FAILURE);
}
}
void bitfield_order(FILE *f)
{
union {
struct {
unsigned int field1 : 1;
unsigned int dummys : 6;
unsigned int field2 : 1;
} bits;
unsigned int value;
} tmp;
tmp.value = 0;
tmp.bits.field1 = 1;
if (tmp.value == 1) {
fprintf(f, "#define MACH_BITFIELDS_LSB_FIRST\n");
} else {
fprintf(f, "#define MACH_BITFIELDS_MSB_FIRST\n");
}
}
int main(int argc, char *argv[])
{
FILE *f;
char *path;
time_t tp;
if (argc != 2) {
fprintf(stderr, "usage: machdep <outfile>\n");
return EXIT_FAILURE;
}
path = argv[1];
/* Open output stream */
if ((f = fopen(path, "w")) == NULL) {
perror("fopen");
return EXIT_FAILURE;
}
/* Add head of output file */
tp = time(NULL);
fprintf(f, "/*\n");
fprintf(f, " *\tmachdep.h generated by machdep at %s", ctime(&tp));
fprintf(f, " */\n\n");
fprintf(f, "#pragma once\n\n");
fprintf(f, "#define USIM_MACHDEP_H\n");
/* Call the determination functions */
byte_order(f);
bitfield_order(f);
/* and clean up */
fclose(f);
return EXIT_SUCCESS;
}