This source file includes following definitions.
- status
- checkbe
- xhtons
- main
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 #include <errno.h>
27 #include <inttypes.h>
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #if defined(_WIN32)
35 # include <fcntl.h>
36 # include <io.h>
37 #endif
38
39 #define VERSION_MAJ 1
40 #define VERSION_MIN 1
41 #define VERSION_PAT 1
42
43 #define MBUFSIZ (1024 * 64)
44
45 static uint64_t wcnt, block, reccnt, mark;
46 static bool isbe;
47
48 static void
49 status (void)
50 {
51 int64_t ft = ftell (stdin);
52
53 if (-1 == ft)
54 {
55 (void)fprintf (stderr, "Error reading input: %s\r\n",
56 strerror (errno));
57 exit (EXIT_FAILURE);
58 }
59
60 (void)fprintf (stderr,
61 "Position %-11" PRId64 " Mark %-8" PRIu64 " Block %-6" PRIu64
62 " %5" PRIu64 " record%s\r\n",
63 ft, mark, block, reccnt, reccnt > 1 ? "s" : "");
64 }
65
66 static void
67 checkbe (void)
68 {
69 int tmp = 1;
70 isbe = (*((char *)&tmp) ? false : true);
71 }
72
73 static uint16_t
74 xhtons (uint16_t x)
75 {
76 if (isbe)
77 {
78 unsigned char *s = (unsigned char *)&x;
79 return (uint16_t)(s[0] << 8 | s[1]);
80 }
81 else
82 {
83 return x;
84 }
85 }
86
87 int
88 main (int argc, char *argv[])
89 {
90 uint64_t bread;
91 uint8_t bc[4] = { 0 };
92 uint8_t buf[MBUFSIZ];
93 const bool forever = true;
94
95 if (1 < argc)
96 {
97 if (0 == strcmp (argv[1], "-h") ||
98 0 == strcmp (argv[1], "-H") ||
99 0 == strcmp (argv[1], "--help"))
100 {
101 (void)fprintf (stdout, "Usage:\r\n");
102 (void)fprintf (stdout, " tap2raw < infile.tap > outfile.raw\r\n");
103 (void)fprintf (stdout, " tap2raw [ -v | --version ]\r\n");
104 (void)fprintf (stdout, " tap2raw [ -h | --help ]\r\n");
105 return EXIT_SUCCESS;
106 }
107 else if (0 == strcmp (argv[1], "-v") ||
108 0 == strcmp (argv[1], "-V") ||
109 0 == strcmp (argv[1], "--version"))
110 {
111 (void)fprintf (stdout, "tap2raw %d.%d.%d\r\n",
112 VERSION_MAJ, VERSION_MIN, VERSION_PAT);
113 return EXIT_SUCCESS;
114 } else {
115 (void)fprintf (stderr, "Error: Invalid option; try '-h'\r\n");
116 return EXIT_FAILURE;
117 }
118 }
119
120 checkbe ();
121
122 #if defined(_WIN32)
123 _setmode (_fileno (stdin), O_BINARY);
124 _setmode (_fileno (stdout), O_BINARY);
125 #endif
126
127 block = 1;
128 reccnt = mark = 0;
129 do
130 {
131 (void)memset (buf, 0, sizeof (char) * MBUFSIZ);
132 bread = fread (bc, sizeof (char), 4, stdin);
133 if (0 == bread)
134 {
135 break;
136 }
137
138 wcnt = ((xhtons ((uint32_t)bc[0] | ((uint32_t)bc[1] << 8)) + 1) & ~1UL);
139 if (wcnt)
140 {
141 (void)fread (buf, sizeof (char), wcnt, stdin);
142 (void)fwrite (buf, sizeof (char), wcnt, stdout);
143 (void)fread (bc, sizeof (char), 4, stdin);
144 reccnt++;
145 }
146 else
147 {
148 if (reccnt)
149 {
150 mark++;
151 status ();
152 }
153
154 block++;
155 reccnt = 0;
156 }
157 }
158 while (forever);
159
160 mark++;
161 status ();
162
163 return EXIT_SUCCESS;
164 }