OpenCores
URL https://opencores.org/ocsvn/tinyvliw8/tinyvliw8/trunk

Subversion Repositories tinyvliw8

[/] [tinyvliw8/] [trunk/] [tools/] [objcopy/] [src/] [main.c] - Blame information for rev 5

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 steckol
#include <sys/stat.h>
2
#include <stdio.h>
3
#include <string.h>
4
#include <unistd.h>
5
#include <fcntl.h>
6
#include <stdlib.h>
7
#include <errno.h>
8
 
9
typedef enum _bfd_e {
10
        _bfd_ascii = 0
11
} _bfd_t;
12
 
13
static unsigned char  _mem_width = 4;
14
static char          *_infile    = NULL;
15
static char          *_outfile   = NULL;
16
static _bfd_t         _bfdname   = _bfd_ascii;
17
 
18
static int _parse_args(int argc, char *argv[])
19
{
20
        int err, i;
21
 
22
        err = 0;
23
        for (i = 1; 0 == err && i < argc; i++) {
24
                if ('-' != *argv[i]) {
25
                        if ((i + 1) == argc) {
26
                                _infile = argv[i];
27
                                break;
28
                        }
29
 
30
                        fprintf(stderr, "\tERROR! bad option <%s>\n", argv[i]);
31
                        err = -EINVAL;
32
 
33
                        break;
34
                }
35
 
36
                switch (*(argv[i] + 1)) {
37
                case 'O':
38
                        if ((i + 1) < argc) {
39
                                i++;
40
 
41
                                if (0 == strcmp("ascii", argv[i])) {
42
                                        _bfdname = _bfd_ascii;
43
                                }
44
 
45
                                break;
46
                        }
47
 
48
                        fprintf(stderr, "\tERROR! option <%s> requires an argument\n",
49
                                argv[i]);
50
                        err = -EINVAL;
51
 
52
                        break;
53
                case 'o':
54
                        if ((i + 1) < argc) {
55
                                i++;
56
                                _outfile = argv[i];
57
 
58
                                break;
59
                        }
60
 
61
                        fprintf(stderr, "\tERROR! option <%s> requires an argument\n",
62
                                argv[i]);
63
                        err = -EINVAL;
64
 
65
                        break;
66
                case 'w':
67
                        if ((i + 1) < argc) {
68
                                long num;
69
 
70
                                i++;
71
                                num = strtol(argv[i], NULL, 0);
72
                                switch (num) {
73
                                case 8:
74
                                        _mem_width = 1;
75
                                        break;
76
                                case 16:
77
                                        _mem_width = 2;
78
                                        break;
79
                                case 32:
80
                                        _mem_width = 4;
81
                                        break;
82
                                default:
83
                                        fprintf(stderr, "\tERROR! invalid memory width\n");
84
                                        err = -ERANGE;
85
                                }
86
                        } else
87
                                err = -EINVAL;
88
 
89
                        break;
90
                case 'h':
91
                        printf("%s [option] <input>\n", argv[0]);
92
                        printf("\t-O <bfdname> output object format (ascii)\n");
93
                        printf("\t-w [8|16|32] memory width in bit (default: 32)\n");
94
                        printf("\t-o <file>    output filename (default: stdout)\n");
95
                        printf("\t-h           useful help\n");
96
 
97
                        exit(0);
98
                }
99
        }
100
 
101
        if (0 == err && NULL == _infile) {
102
                fprintf(stderr, "\tERROR! no input file given\n");
103
                err = -EINVAL;
104
        }
105
 
106
        return err;
107
}
108
 
109
static int _load_file(const char *filename, unsigned char **buf_ptr)
110
{
111
        unsigned char *buf;
112
        int err;
113
 
114
        buf = NULL;
115
 
116
        err = open(filename, O_RDONLY);
117
        if (0 <= err) {
118
                struct stat s;
119
                int fd;
120
 
121
                fd = err;
122
                err = 0;
123
 
124
                err = fstat(fd, &s);
125
                if (0 == err) {
126
                        buf = malloc(s.st_size);
127
                        if (buf) {
128
                                unsigned int r;
129
 
130
                                r = 0;
131
                                do {
132
                                        err = read(fd, buf + r, s.st_size - r);
133
                                        if (0 > err) {
134
                                                err = -errno;
135
                                                if (-EINTR != err)
136
                                                        break;
137
 
138
                                                err = 0;
139
                                        }
140
 
141
                                        r += err;
142
                                        err = 0;
143
                                } while (r < s.st_size);
144
 
145
                                if (0 == err) {
146
                                        *buf_ptr = buf;
147
                                        err = s.st_size;
148
                                }
149
                        } else
150
                                err = -ENOMEM;
151
                }
152
 
153
                close(fd);
154
        } else
155
                err = -errno;
156
 
157
        if (0 > err)
158
                fprintf(stderr, "load file failed, error %d\n", err);
159
 
160
        return err;
161
}
162
 
163
static const char *_get_filename(const char *filename)
164
{
165
        int i;
166
 
167
        i = strlen(filename);
168
        while (0 != i) {
169
                if ('/' == filename[i]) {
170
                        i++;
171
                        break;
172
                }
173
 
174
                i--;
175
        }
176
 
177
        return &filename[i];
178
}
179
 
180
static int _convert_to_ascii(const char *filename, const unsigned char *buf,
181
                             int size)
182
{
183
        char name[64];
184
        int err;
185
 
186
        err = snprintf(&name[0], 64, "%s.asc", _get_filename(filename));
187
        if (64 > err) {
188
                err = open(&name[0], O_WRONLY | O_CREAT | O_TRUNC, 00644);
189
                if (0 <= err) {
190
                        int offset, fd;
191
 
192
                        fd = err;
193
                        err = 0;
194
 
195
                        offset = 0;
196
                        while (offset < size) {
197
                                unsigned char ob[34];
198
                                int i;
199
 
200
                                memset(&ob[0], 0x00, sizeof(ob));
201
                                for (i = 0; i < _mem_width; i++) {
202
                                        unsigned char c;
203
                                        int j;
204
 
205
                                        if ((i + offset) < size) {
206
                                                c = buf[i + offset];
207
                                        } else
208
                                                c = 0x00;
209
 
210
                                        for (j = 0; j < 8; j++) {
211
                                                if (0 != (c & (0x80 >> j)))
212
                                                        ob[(i << 3) + j] = '1';
213
                                                else
214
                                                        ob[(i << 3) + j] = '0';
215
                                        }
216
                                }
217
 
218
                                ob[(i << 3)] = '\n';
219
                                err = write(fd, &ob[0], (i << 3) + 1);
220
                                if (0 > err) {
221
                                        err = -errno;
222
                                        break;
223
                                }
224
 
225
                                offset += i;
226
 
227
                                err = 0;
228
                        }
229
 
230
                        close(fd);
231
                } else
232
                        err = -errno;
233
        } else
234
                err = -ENAMETOOLONG;
235
 
236
        if (0 != err)
237
                fprintf(stderr, "write output file failed, error %d\n", err);
238
 
239
        return err;
240
}
241
 
242
int main(int argc, char *argv[])
243
{
244
        int err;
245
 
246
        err = _parse_args(argc, argv);
247
        if (0 == err) {
248
                unsigned char *inbuf;
249
 
250
                err = _load_file(_infile, &inbuf);
251
                if (0 < err) {
252
                        int len;
253
 
254
                        len = err;
255
                        switch (_bfdname) {
256
                        case _bfd_ascii:
257
                                err = _convert_to_ascii(_infile, inbuf, len);
258
                                break;
259
                        }
260
 
261
                        free(inbuf);
262
                }
263
        }
264
 
265
        return err;
266
}
267
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.