Add configure check for sys/types.h; include in our types.h.
[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/types.h>
42 #include <sys/stat.h>
43 #include <errno.h>
44 #include <ctype.h>
45
46 static inline int fileio_open_local(fileio_t *fileio)
47 {
48 char access[4];
49
50 switch (fileio->access)
51 {
52 case FILEIO_READ:
53 strcpy(access, "r");
54 break;
55 case FILEIO_WRITE:
56 strcpy(access, "w");
57 break;
58 case FILEIO_READWRITE:
59 strcpy(access, "w+");
60 break;
61 case FILEIO_APPEND:
62 strcpy(access, "a");
63 break;
64 case FILEIO_APPENDREAD:
65 strcpy(access, "a+");
66 break;
67 default:
68 LOG_ERROR("BUG: access neither read, write nor readwrite");
69 return ERROR_INVALID_ARGUMENTS;
70 }
71
72 /* win32 always opens in binary mode */
73 #ifndef _WIN32
74 if (fileio->type == FILEIO_BINARY)
75 #endif
76 {
77 strcat(access, "b");
78 }
79
80 if (!(fileio->file = open_file_from_path (fileio->url, access)))
81 {
82 LOG_ERROR("couldn't open %s", fileio->url);
83 return ERROR_FILEIO_OPERATION_FAILED;
84 }
85
86 if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
87 {
88 /* NB! Here we use fseek() instead of stat(), since stat is a
89 * more advanced operation that might not apply to e.g. a disk path
90 * that refers to e.g. a tftp client */
91 int result, result2;
92
93 result = fseek(fileio->file, 0, SEEK_END);
94
95 fileio->size = ftell(fileio->file);
96
97 result2 = fseek(fileio->file, 0, SEEK_SET);
98
99 if ((fileio->size<0)||(result<0)||(result2<0))
100 {
101 fileio_close(fileio);
102 return ERROR_FILEIO_OPERATION_FAILED;
103 }
104 }
105 else
106 {
107 fileio->size = 0x0;
108 }
109
110 return ERROR_OK;
111 }
112
113 int fileio_open(fileio_t *fileio, const char *url, enum fileio_access access, enum fileio_type type)
114 {
115 int retval = ERROR_OK;
116
117 fileio->type = type;
118 fileio->access = access;
119 fileio->url = strdup(url);
120
121 retval = fileio_open_local(fileio);
122
123 return retval;
124 }
125
126 static inline int fileio_close_local(fileio_t *fileio)
127 {
128 int retval;
129 if ((retval = fclose(fileio->file)) != 0)
130 {
131 if (retval == EBADF)
132 {
133 LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
134 }
135 else
136 {
137 LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
138 }
139
140 return ERROR_FILEIO_OPERATION_FAILED;
141 }
142
143 return ERROR_OK;
144 }
145
146 int fileio_close(fileio_t *fileio)
147 {
148 int retval;
149
150 retval = fileio_close_local(fileio);
151
152 free(fileio->url);
153 fileio->url = NULL;
154
155 return retval;
156 }
157
158 int fileio_seek(fileio_t *fileio, u32 position)
159 {
160 int retval;
161 if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
162 {
163 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
164 return ERROR_FILEIO_OPERATION_FAILED;
165 }
166
167 return ERROR_OK;
168 }
169
170 static inline int fileio_local_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
171 {
172 *size_read = fread(buffer, 1, size, fileio->file);
173
174 return ERROR_OK;
175 }
176
177 int fileio_read(fileio_t *fileio, u32 size, u8 *buffer, u32 *size_read)
178 {
179 return fileio_local_read(fileio, size, buffer, size_read);
180 }
181
182 int fileio_read_u32(fileio_t *fileio, u32 *data)
183 {
184 u8 buf[4];
185 u32 size_read;
186 int retval;
187
188 if ((retval = fileio_local_read(fileio, 4, buf, &size_read)) != ERROR_OK)
189 return retval;
190 *data = be_to_h_u32(buf);
191
192 return ERROR_OK;
193 }
194
195 static inline int fileio_local_fgets(fileio_t *fileio, u32 size, char *buffer)
196 {
197 if( fgets(buffer, size, fileio->file) == NULL)
198 return ERROR_FILEIO_OPERATION_FAILED;
199
200 return ERROR_OK;
201 }
202
203 int fileio_fgets(fileio_t *fileio, u32 size, char *buffer)
204 {
205 return fileio_local_fgets(fileio, size, buffer);
206 }
207
208 static inline int fileio_local_write(fileio_t *fileio, u32 size, const u8 *buffer, u32 *size_written)
209 {
210 *size_written = fwrite(buffer, 1, size, fileio->file);
211
212 return ERROR_OK;
213 }
214
215 int fileio_write(fileio_t *fileio, u32 size, const u8 *buffer, u32 *size_written)
216 {
217 int retval;
218
219 retval = fileio_local_write(fileio, size, buffer, size_written);
220
221 if (retval == ERROR_OK)
222 fileio->size += *size_written;
223
224 return retval;;
225 }
226
227 int fileio_write_u32(fileio_t *fileio, u32 data)
228 {
229 u8 buf[4];
230 u32 size_written;
231 int retval;
232
233 h_u32_to_be(buf, data);
234
235 if ((retval = fileio_local_write(fileio, 4, buf, &size_written)) != ERROR_OK)
236 return retval;
237
238 return ERROR_OK;
239 }

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)