Finish portability support for AC_HEADER_STDBOOL configure macro.
[openocd.git] / src / helper / fileio.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "types.h"
31 #include "replacements.h"
32 #include "log.h"
33 #include "configuration.h"
34
35 #include "fileio.h"
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <sys/stat.h>
42 #include <errno.h>
43 #include <ctype.h>
44
45 static inline int fileio_open_local(fileio_t *fileio)
46 {
47 char access[4];
48
49 switch (fileio->access)
50 {
51 case FILEIO_READ:
52 strcpy(access, "r");
53 break;
54 case FILEIO_WRITE:
55 strcpy(access, "w");
56 break;
57 case FILEIO_READWRITE:
58 strcpy(access, "w+");
59 break;
60 case FILEIO_APPEND:
61 strcpy(access, "a");
62 break;
63 case FILEIO_APPENDREAD:
64 strcpy(access, "a+");
65 break;
66 default:
67 LOG_ERROR("BUG: access neither read, write nor readwrite");
68 return ERROR_INVALID_ARGUMENTS;
69 }
70
71 /* win32 always opens in binary mode */
72 #ifndef _WIN32
73 if (fileio->type == FILEIO_BINARY)
74 #endif
75 {
76 strcat(access, "b");
77 }
78
79 if (!(fileio->file = open_file_from_path (fileio->url, access)))
80 {
81 LOG_ERROR("couldn't open %s", fileio->url);
82 return ERROR_FILEIO_OPERATION_FAILED;
83 }
84
85 if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
86 {
87 /* NB! Here we use fseek() instead of stat(), since stat is a
88 * more advanced operation that might not apply to e.g. a disk path
89 * that refers to e.g. a tftp client */
90 int result, result2;
91
92 result = fseek(fileio->file, 0, SEEK_END);
93
94 fileio->size = ftell(fileio->file);
95
96 result2 = fseek(fileio->file, 0, SEEK_SET);
97
98 if ((fileio->size<0)||(result<0)||(result2<0))
99 {
100 fileio_close(fileio);
101 return ERROR_FILEIO_OPERATION_FAILED;
102 }
103 }
104 else
105 {
106 fileio->size = 0x0;
107 }
108
109 return ERROR_OK;
110 }
111
112 int fileio_open(fileio_t *fileio, const char *url, enum fileio_access access, enum fileio_type type)
113 {
114 int retval = ERROR_OK;
115
116 fileio->type = type;
117 fileio->access = access;
118 fileio->url = strdup(url);
119
120 retval = fileio_open_local(fileio);
121
122 return retval;
123 }
124
125 static inline int fileio_close_local(fileio_t *fileio)
126 {
127 int retval;
128 if ((retval = fclose(fileio->file)) != 0)
129 {
130 if (retval == EBADF)
131 {
132 LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
133 }
134 else
135 {
136 LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
137 }
138
139 return ERROR_FILEIO_OPERATION_FAILED;
140 }
141
142 return ERROR_OK;
143 }
144
145 int fileio_close(fileio_t *fileio)
146 {
147 int retval;
148
149 retval = fileio_close_local(fileio);
150
151 free(fileio->url);
152 fileio->url = NULL;
153
154 return retval;
155 }
156
157 int fileio_seek(fileio_t *fileio, u32 position)
158 {
159 int retval;
160 if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
161 {
162 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
163 return ERROR_FILEIO_OPERATION_FAILED;
164 }
165
166 return ERROR_OK;
167 }
168
169 static inline int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
170 {
171 *size_read = fread(buffer, 1, size, fileio->file);
172
173 return ERROR_OK;
174 }
175
176 int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
177 {
178 return fileio_local_read(fileio, size, buffer, size_read);
179 }
180
181 int fileio_read_u32(fileio_t *fileio, u32 *data)
182 {
183 u8 buf[4];
184 u32 size_read;
185 int retval;
186
187 if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
188 return retval;
189 *data = be_to_h_u32(buf);
190
191 return ERROR_OK;
192 }
193
194 static inline int fileio_local_fgets(fileio_t *fileio, u32 size, char *buffer)
195 {
196 if( fgets(buffer, size, fileio->file) == NULL)
197 return ERROR_FILEIO_OPERATION_FAILED;
198
199 return ERROR_OK;
200 }
201
202 int fileio_fgets(fileio_t *fileio, u32 size, char *buffer)
203 {
204 return fileio_local_fgets(fileio, size, buffer);
205 }
206
207 static inline int fileio_local_write(fileio_t *fileio, u32 size, const u8 *buffer, u32 *size_written)
208 {
209 *size_written = fwrite(buffer, 1, size, fileio->file);
210
211 return ERROR_OK;
212 }
213
214 int fileio_write(fileio_t *fileio, u32 size, const u8 *buffer, u32 *size_written)
215 {
216 int retval;
217
218 retval = fileio_local_write(fileio, size, buffer, size_written);
219
220 if (retval == ERROR_OK)
221 fileio->size += *size_written;
222
223 return retval;;
224 }
225
226 int fileio_write_u32(fileio_t *fileio, u32 data)
227 {
228 u8 buf[4];
229 u32 size_written;
230 int retval;
231
232 h_u32_to_be(buf, data);
233
234 if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)
235 return retval;
236
237 return ERROR_OK;
238 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)