-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsh_fsutils.c
419 lines (352 loc) · 11.2 KB
/
nsh_fsutils.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/****************************************************************************
* apps/nshlib/nsh_fsutils.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <dirent.h>
#include "nsh.h"
#include "nsh_console.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nsh_catfile
*
* Description:
* Dump the contents of a file to the current NSH terminal.
*
* Input Paratemets:
* vtbl - session vtbl
* cmd - NSH command name to use in error reporting
* filepath - The full path to the file to be dumped
*
* Returned Value:
* Zero (OK) on success; -1 (ERROR) on failure.
*
****************************************************************************/
#ifdef NSH_HAVE_CATFILE
int nsh_catfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
FAR const char *filepath)
{
FAR char *buffer;
int fd;
int ret = OK;
/* Open the file for reading */
fd = open(filepath, O_RDONLY);
if (fd < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
return ERROR;
}
buffer = (FAR char *)malloc(IOBUFFERSIZE);
if(buffer == NULL)
{
close(fd);
nsh_error(vtbl, g_fmtcmdfailed, cmd, "malloc", NSH_ERRNO);
return ERROR;
}
/* And just dump it byte for byte into stdout */
for (;;)
{
int nbytesread = read(fd, buffer, IOBUFFERSIZE);
/* Check for read errors */
if (nbytesread < 0)
{
int errval = errno;
/* EINTR is not an error (but will stop stop the cat) */
if (errval == EINTR)
{
nsh_error(vtbl, g_fmtsignalrecvd, cmd);
}
else
{
nsh_error(vtbl, g_fmtcmdfailed, cmd, "read", NSH_ERRNO_OF(errval));
}
ret = ERROR;
break;
}
/* Check for data successfully read */
else if (nbytesread > 0)
{
int nbyteswritten = 0;
while (nbyteswritten < nbytesread)
{
ssize_t n = nsh_write(vtbl, buffer + nbyteswritten, nbytesread - nbyteswritten);
if (n < 0)
{
int errcode = errno;
/* EINTR is not an error (but will stop stop the cat) */
if (errcode == EINTR)
{
nsh_error(vtbl, g_fmtsignalrecvd, cmd);
}
else
{
nsh_error(vtbl, g_fmtcmdfailed, cmd, "write",
NSH_ERRNO_OF(errcode));
}
ret = ERROR;
break;
}
else
{
nbyteswritten += n;
}
}
}
/* Otherwise, it is the end of file */
else
{
break;
}
}
/* NOTE that the following NSH prompt may appear on the same line as file
* content. The IEEE Std requires that "The standard output shall
* contain the sequence of bytes read from the input files. Nothing else
* shall be written to the standard output." Reference:
* https://pubs.opengroup.org/onlinepubs/009695399/utilities/cat.html.
*/
/* Close the input file and return the result */
close(fd);
free(buffer);
return ret;
}
#endif
/****************************************************************************
* Name: nsh_readfile
*
* Description:
* Read a small file into a user-provided buffer. The data is assumed to
* be a string and is guaranteed to be NUL-termined. An error occurs if
* the file content (+terminator) will not fit into the provided 'buffer'.
*
* Input Parameters:
* vtbl - The console vtable
* filepath - The full path to the file to be read
* buffer - The user-provided buffer into which the file is read.
* buflen - The size of the user provided buffer
*
* Returned Value:
* Zero (OK) is returned on success; -1 (ERROR) is returned on any
* failure to read the fil into the buffer.
*
****************************************************************************/
#ifdef NSH_HAVE_READFILE
int nsh_readfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
FAR const char *filepath, FAR char *buffer, size_t buflen)
{
FAR char *bufptr;
size_t remaining;
ssize_t nread;
ssize_t ntotal;
int fd;
int ret;
/* Open the file */
fd = open(filepath, O_RDONLY);
if (fd < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
return ERROR;
}
/* Read until we hit the end of the file, until we have exhausted the
* buffer space, or until some irrecoverable error occurs
*/
ntotal = 0; /* No bytes read yet */
*buffer = '\0'; /* NUL terminate the empty buffer */
bufptr = buffer; /* Working pointer */
remaining = buflen - 1; /* Reserve one byte for a NUL terminator */
ret = ERROR; /* Assume failure */
do
{
nread = read(fd, bufptr, remaining);
if (nread < 0)
{
/* Read error */
int errcode = errno;
DEBUGASSERT(errcode > 0);
/* EINTR is not a read error. It simply means that a signal was
* received while waiting for the read to complete.
*/
if (errcode != EINTR)
{
/* Fatal error */
nsh_error(vtbl, g_fmtcmdfailed, cmd, "read", NSH_ERRNO);
break;
}
}
else if (nread == 0)
{
/* End of file */
ret = OK;
break;
}
else
{
/* Successful read. Make sure that the buffer is null terminated */
DEBUGASSERT(nread <= remaining);
ntotal += nread;
buffer[ntotal] = '\0';
/* Bump up the read count and continuing reading to the end of
* file.
*/
bufptr += nread;
remaining -= nread;
}
}
while (buflen > 0);
/* Close the file and return. */
close(fd);
return ret;
}
#endif
/****************************************************************************
* Name: nsh_foreach_direntry
*
* Description:
* Call the provided 'handler' for each entry found in the directory at
* 'dirpath'.
*
* Input Parameters
* vtbl - The console vtable
* cmd - NSH command name to use in error reporting
* dirpath - The full path to the directory to be traversed
* handler - The handler to be called for each entry of the directory
* pvarg - User provided argument to be passed to the 'handler'
*
* Returned Value:
* Zero (OK) returned on success; -1 (ERROR) returned on failure.
*
****************************************************************************/
#ifdef NSH_HAVE_FOREACH_DIRENTRY
int nsh_foreach_direntry(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
FAR const char *dirpath,
nsh_direntry_handler_t handler, void *pvarg)
{
DIR *dirp;
int ret = OK;
/* Open the directory */
dirp = opendir(dirpath);
if (dirp == NULL)
{
/* Failed to open the directory */
nsh_error(vtbl, g_fmtnosuch, cmd, "directory", dirpath);
return ERROR;
}
/* Read each directory entry */
for (; ; )
{
FAR struct dirent *entryp = readdir(dirp);
if (entryp == NULL)
{
/* Finished with this directory */
break;
}
/* Call the handler with this directory entry */
if (handler(vtbl, dirpath, entryp, pvarg) < 0)
{
/* The handler reported a problem */
ret = ERROR;
break;
}
}
closedir(dirp);
return ret;
}
#endif
/****************************************************************************
* Name: nsh_trimdir
*
* Description:
* Skip any trailing '/' characters (unless it is also the leading '/')
*
* Input Parameters:
* dirpath - The directory path to be trimmed. May be modified!
*
* Returned value:
* None
*
****************************************************************************/
#ifdef NSH_HAVE_TRIMDIR
void nsh_trimdir(FAR char *dirpath)
{
/* Skip any trailing '/' characters (unless it is also the leading '/') */
int len = strlen(dirpath) - 1;
while (len > 0 && dirpath[len] == '/')
{
dirpath[len] = '\0';
len--;
}
}
#endif
/****************************************************************************
* Name: nsh_trimspaces
*
* Description:
* Trim any leading or trailing spaces from a string.
*
* Input Parameters:
* str - The string to be trimmed. May be modified!
*
* Returned value:
* The new string pointer.
*
****************************************************************************/
#ifdef NSH_HAVE_TRIMSPACES
FAR char *nsh_trimspaces(FAR char *str)
{
FAR char *trimmed;
int ndx;
/* Strip leading whitespace from the value */
for (trimmed = str;
*trimmed != '\0' && isspace(*trimmed);
trimmed++);
/* Strip trailing whitespace from the value */
for (ndx = strlen(trimmed) - 1;
ndx >= 0 && isspace(trimmed[ndx]);
ndx--)
{
trimmed[ndx] = '\0';
}
return trimmed;
}
#endif