bf.c
3.55 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* This file is part of the GNU Mailutils testsuite.
Copyright (C) 2017 Free Software Foundation, Inc.
GNU Mailutils 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, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include <inttypes.h>
typedef int (*ACTION) (FILE *, char *);
int
seek_file (FILE *fp, char *arg)
{
long off;
int whence;
switch (arg[0])
{
case '+':
whence = SEEK_CUR;
arg++;
break;
case '-':
whence = SEEK_CUR;
break;
case '$':
whence = SEEK_END;
arg++;
break;
default:
whence = SEEK_SET;
}
if (sscanf (arg, "%ld", &off) != 1)
{
fprintf (stderr, "bad offset: %s\n", arg);
abort ();
}
return fseek (fp, off, whence);
}
int
write_string (FILE *fp, char *arg)
{
size_t n = strlen (arg);
return fwrite (arg, n, 1, fp) != 1;
}
int
write_byte (FILE *fp, char *arg)
{
int c;
if (strlen (arg) == 3
&& (arg[0] == '\'' || arg[0] == '"'))
c = arg[1];
else
{
char *p;
unsigned long n = strtoul (arg, &p, 0);
if (*p || n > UCHAR_MAX)
{
errno = EINVAL;
return -1;
}
c = n;
}
return fwrite (&c, 1, 1, fp) != 1;
}
int
write_short (FILE *fp, char *arg)
{
uint16_t val;
char *p;
unsigned long n = strtoul (arg, &p, 0);
if (*p || n > UINT16_MAX)
{
errno = EINVAL;
return -1;
}
val = n;
return fwrite (&val, sizeof(val), 1, fp) != 1;
}
int
write_int (FILE *fp, char *arg)
{
uint32_t val;
char *p;
unsigned long n = strtoul (arg, &p, 0);
if (*p || n > UINT32_MAX)
{
errno = EINVAL;
return -1;
}
val = n;
return fwrite (&val, sizeof(val), 1, fp) != 1;
}
static struct dispatch {
char *opt;
int (*act) (FILE *, char *);
} dispatch[] = {
{ "-seek", seek_file },
{ "-string", write_string },
{ "-byte", write_byte },
{ "-short", write_short },
{ "-int", write_int },
{ NULL }
};
static ACTION
find_action (char const *opt)
{
struct dispatch *p;
for (p = dispatch; p->opt; p++)
if (strcmp (p->opt, opt) == 0)
return p->act;
return NULL;
}
/*
-seek OFF
-string STRING
-byte BYTE
-short SHORT
-int INT
-repeat N
*/
int
main (int argc, char **argv)
{
char *name;
FILE *fp;
ACTION action;
char **pp;
assert (argc > 1);
name = argv[1];
fp = fopen (name, "w");
if (!fp)
{
perror (name);
abort ();
}
pp = argv + 2;
while (*pp)
{
char *opt = *pp++, *arg;
if (opt[0] != '-')
{
fprintf (stderr, "not an option: %s\n", opt);
abort ();
}
action = find_action (opt);
if (!action)
{
fprintf (stderr, "unknown action: %s\n", opt);
abort ();
}
if (!*pp)
{
fprintf (stderr, "argument to %s missing\n", opt);
abort ();
}
arg = *pp++;
if (action (fp, arg))
{
fprintf (stderr, "%s %s: %s\n", opt, arg, strerror (errno));
abort ();
}
}
fclose (fp);
return 0;
}