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