This source file includes following definitions.
- main
- usage
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 #include <sys/types.h>
43 #include <sys/mman.h>
44 #include <err.h>
45 #include <ctype.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51
52 int main(int argc, char *argv[]);
53 void usage(void);
54
55 int
56 main(int argc, char *argv[])
57 {
58 off_t in_len;
59 u_char *in_ptr;
60 FILE *out_file;
61 char *include_name, *cp;
62 int i;
63
64 if (argc != 3)
65 usage();
66
67 i = open(argv[1], O_RDONLY, 0644);
68 if (i < 0)
69 err(1, "unable to open %s", argv[1]);
70
71 out_file = fopen(argv[2], "w+");
72 if (out_file == NULL)
73 err(1, "unable to create %s", argv[2]);
74
75
76
77
78
79 include_name = strdup(argv[2]);
80 if (include_name == NULL)
81 err(1, "unable to allocate include name");
82
83 for (cp = include_name; *cp != '\0'; cp++) {
84 if (isalpha(*cp))
85 *cp = toupper(*cp);
86 else if (*cp == '.')
87 *cp = '_';
88 }
89
90 in_len = lseek(i, 0, SEEK_END);
91 if (in_len == (off_t) -1)
92 err(1, "unable to determine length of input file");
93
94 in_ptr = mmap(NULL, in_len, PROT_READ, MAP_FILE|MAP_SHARED,
95 i, (off_t) 0);
96 if (in_ptr == MAP_FAILED)
97 err(1, "unable to mmap input file");
98 (void) close(i);
99
100 fprintf(out_file, "/*\t$NetBSD: cyzfirm2h.c,v 1.1 2000/05/17 17:58:10 thorpej Exp $\t*/\n\n");
101 fprintf(out_file, "\
102 /*
103 * Firmware for Cyclades Z series multiport serial boards.
104 * Automatically generated from:
105 *
106 * %s
107 */\n\n", argv[1]);
108 fprintf(out_file, "#ifndef _%s_\n", include_name);
109 fprintf(out_file, "#define\t_%s_\n\n", include_name);
110
111 fprintf(out_file, "static const u_int8_t cycladesz_firmware[] = {\n");
112
113 i = 0;
114 while (in_len != 0) {
115 if (i == 0)
116 fprintf(out_file, "\t");
117 fprintf(out_file, "0x%02x, ", *in_ptr);
118 in_ptr++;
119 in_len--;
120 i++;
121 if (i == 10) {
122 fprintf(out_file, "\n");
123 i = 0;
124 }
125 }
126 fprintf(out_file, "\n};\n\n");
127
128 fprintf(out_file, "#endif /* _%s_ */\n", include_name);
129 }
130
131 void
132 usage()
133 {
134 extern char *__progname;
135
136 fprintf(stderr, "usage: %s infile outfile\n", __progname);
137 exit(1);
138 }