]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_FAT_SL_and_CLI_Windows_Simulator/File-Releated-CLI-commands.c
6f5a1c1e356087d3ca56e8e765f46a374428ef3a
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_FAT_SL_and_CLI_Windows_Simulator / File-Releated-CLI-commands.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /* FreeRTOS includes. */\r
97 #include "FreeRTOS.h"\r
98 #include "task.h"\r
99 \r
100 /* Standard includes. */\r
101 #include <stdint.h>\r
102 #include <stdio.h>\r
103 #include <stdlib.h>\r
104 \r
105 /* FreeRTOS+CLI includes. */\r
106 #include "FreeRTOS_CLI.h"\r
107 \r
108 /* File system includes. */\r
109 #include "fat_sl.h"\r
110 #include "api_mdriver_ram.h"\r
111 #include "test.h"\r
112 \r
113 #ifdef _WINDOWS_\r
114         #define snprintf _snprintf\r
115 #endif\r
116 \r
117 #define cliNEW_LINE             "\r\n"\r
118 \r
119 /*******************************************************************************\r
120  * See the URL in the comments within main.c for the location of the online\r
121  * documentation.\r
122  ******************************************************************************/\r
123 \r
124 /*\r
125  * Print out information on a single file.\r
126  */\r
127 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct );\r
128 \r
129 /*\r
130  * Copies an existing file into a newly created file.\r
131  */\r
132 static BaseType_t prvPerformCopy( const char *pcSourceFile,\r
133                                                         int32_t lSourceFileLength,\r
134                                                         const char *pcDestinationFile,\r
135                                                         char *pxWriteBuffer,\r
136                                                         size_t xWriteBufferLen );\r
137 \r
138 /*\r
139  * Implements the DIR command.\r
140  */\r
141 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
142 \r
143 /*\r
144  * Implements the CD command.\r
145  */\r
146 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
147 \r
148 /*\r
149  * Implements the DEL command.\r
150  */\r
151 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
152 \r
153 /*\r
154  * Implements the TYPE command.\r
155  */\r
156 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
157 \r
158 /*\r
159  * Implements the COPY command.\r
160  */\r
161 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
162 \r
163 /*\r
164  * Implements the TEST command.\r
165  */\r
166 static BaseType_t prvTESTFSCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
167 \r
168 /* Structure that defines the DIR command line command, which lists all the\r
169 files in the current directory. */\r
170 static const CLI_Command_Definition_t xDIR =\r
171 {\r
172         "dir", /* The command string to type. */\r
173         "\r\ndir:\r\n Lists the files in the current directory\r\n",\r
174         prvDIRCommand, /* The function to run. */\r
175         0 /* No parameters are expected. */\r
176 };\r
177 \r
178 /* Structure that defines the CD command line command, which changes the\r
179 working directory. */\r
180 static const CLI_Command_Definition_t xCD =\r
181 {\r
182         "cd", /* The command string to type. */\r
183         "\r\ncd <dir name>:\r\n Changes the working directory\r\n",\r
184         prvCDCommand, /* The function to run. */\r
185         1 /* One parameter is expected. */\r
186 };\r
187 \r
188 /* Structure that defines the TYPE command line command, which prints the\r
189 contents of a file to the console. */\r
190 static const CLI_Command_Definition_t xTYPE =\r
191 {\r
192         "type", /* The command string to type. */\r
193         "\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",\r
194         prvTYPECommand, /* The function to run. */\r
195         1 /* One parameter is expected. */\r
196 };\r
197 \r
198 /* Structure that defines the DEL command line command, which deletes a file. */\r
199 static const CLI_Command_Definition_t xDEL =\r
200 {\r
201         "del", /* The command string to type. */\r
202         "\r\ndel <filename>:\r\n deletes a file or directory\r\n",\r
203         prvDELCommand, /* The function to run. */\r
204         1 /* One parameter is expected. */\r
205 };\r
206 \r
207 /* Structure that defines the COPY command line command, which deletes a file. */\r
208 static const CLI_Command_Definition_t xCOPY =\r
209 {\r
210         "copy", /* The command string to type. */\r
211         "\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",\r
212         prvCOPYCommand, /* The function to run. */\r
213         2 /* Two parameters are expected. */\r
214 };\r
215 \r
216 /* Structure that defines the TEST command line command, which executes some\r
217 file system driver tests. */\r
218 static const CLI_Command_Definition_t xTEST_FS =\r
219 {\r
220         "test-fs", /* The command string to type. */\r
221         "\r\ntest-fs:\r\n Executes file system tests.  ALL FILES WILL BE DELETED!!!\r\n",\r
222         prvTESTFSCommand, /* The function to run. */\r
223         0 /* No parameters are expected. */\r
224 };\r
225 \r
226 /*-----------------------------------------------------------*/\r
227 \r
228 void vRegisterFileSystemCLICommands( void )\r
229 {\r
230         /* Register all the command line commands defined immediately above. */\r
231         FreeRTOS_CLIRegisterCommand( &xDIR );\r
232         FreeRTOS_CLIRegisterCommand( &xCD );\r
233         FreeRTOS_CLIRegisterCommand( &xTYPE );\r
234         FreeRTOS_CLIRegisterCommand( &xDEL );\r
235         FreeRTOS_CLIRegisterCommand( &xCOPY );\r
236         FreeRTOS_CLIRegisterCommand( &xTEST_FS );\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
241 {\r
242 const char *pcParameter;\r
243 BaseType_t xParameterStringLength, xReturn = pdTRUE;\r
244 static F_FILE *pxFile = NULL;\r
245 int iChar;\r
246 size_t xByte;\r
247 size_t xColumns = 50U;\r
248 \r
249         /* Ensure there is always a null terminator after each character written. */\r
250         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
251 \r
252         /* Ensure the buffer leaves space for the \r\n. */\r
253         configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );\r
254         xWriteBufferLen -= strlen( cliNEW_LINE );\r
255 \r
256         if( xWriteBufferLen < xColumns )\r
257         {\r
258                 /* Ensure the loop that uses xColumns as an end condition does not\r
259                 write off the end of the buffer. */\r
260                 xColumns = xWriteBufferLen;\r
261         }\r
262 \r
263         if( pxFile == NULL )\r
264         {\r
265                 /* The file has not been opened yet.  Find the file name. */\r
266                 pcParameter = FreeRTOS_CLIGetParameter\r
267                                                 (\r
268                                                         pcCommandString,                /* The command string itself. */\r
269                                                         1,                                              /* Return the first parameter. */\r
270                                                         &xParameterStringLength /* Store the parameter string length. */\r
271                                                 );\r
272 \r
273                 /* Sanity check something was returned. */\r
274                 configASSERT( pcParameter );\r
275 \r
276                 /* Attempt to open the requested file. */\r
277                 pxFile = f_open( pcParameter, "r" );\r
278         }\r
279 \r
280         if( pxFile != NULL )\r
281         {\r
282                 /* Read the next chunk of data from the file. */\r
283                 for( xByte = 0; xByte < xColumns; xByte++ )\r
284                 {\r
285                         iChar = f_getc( pxFile );\r
286 \r
287                         if( iChar == -1 )\r
288                         {\r
289                                 /* No more characters to return. */\r
290                                 f_close( pxFile );\r
291                                 pxFile = NULL;\r
292                                 break;\r
293                         }\r
294                         else\r
295                         {\r
296                                 pcWriteBuffer[ xByte ] = ( char ) iChar;\r
297                         }\r
298                 }\r
299         }\r
300 \r
301         if( pxFile == NULL )\r
302         {\r
303                 /* Either the file was not opened, or all the data from the file has\r
304                 been returned and the file is now closed. */\r
305                 xReturn = pdFALSE;\r
306         }\r
307 \r
308         strcat( pcWriteBuffer, cliNEW_LINE );\r
309 \r
310         return xReturn;\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
315 {\r
316 const char *pcParameter;\r
317 BaseType_t xParameterStringLength;\r
318 unsigned char ucReturned;\r
319 size_t xStringLength;\r
320 \r
321         /* Obtain the parameter string. */\r
322         pcParameter = FreeRTOS_CLIGetParameter\r
323                                         (\r
324                                                 pcCommandString,                /* The command string itself. */\r
325                                                 1,                                              /* Return the first parameter. */\r
326                                                 &xParameterStringLength /* Store the parameter string length. */\r
327                                         );\r
328 \r
329         /* Sanity check something was returned. */\r
330         configASSERT( pcParameter );\r
331 \r
332         /* Attempt to move to the requested directory. */\r
333         ucReturned = f_chdir( pcParameter );\r
334 \r
335         if( ucReturned == F_NO_ERROR )\r
336         {\r
337                 sprintf( pcWriteBuffer, "In: " );\r
338                 xStringLength = strlen( pcWriteBuffer );\r
339                 f_getcwd( &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );\r
340         }\r
341         else\r
342         {\r
343                 sprintf( pcWriteBuffer, "Error" );\r
344         }\r
345 \r
346         strcat( pcWriteBuffer, cliNEW_LINE );\r
347 \r
348         return pdFALSE;\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
353 {\r
354 static F_FIND *pxFindStruct = NULL;\r
355 unsigned char ucReturned;\r
356 BaseType_t xReturn = pdFALSE;\r
357 \r
358         /* This assumes pcWriteBuffer is long enough. */\r
359         ( void ) pcCommandString;\r
360 \r
361         /* Ensure the buffer leaves space for the \r\n. */\r
362         configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );\r
363         xWriteBufferLen -= strlen( cliNEW_LINE );\r
364 \r
365         if( pxFindStruct == NULL )\r
366         {\r
367                 /* This is the first time this function has been executed since the Dir\r
368                 command was run.  Create the find structure. */\r
369                 pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) );\r
370 \r
371                 if( pxFindStruct != NULL )\r
372                 {\r
373                         ucReturned = f_findfirst( "*.*", pxFindStruct );\r
374 \r
375                         if( ucReturned == F_NO_ERROR )\r
376                         {\r
377                                 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );\r
378                                 xReturn = pdPASS;\r
379                         }\r
380                         else\r
381                         {\r
382                                 snprintf( pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );\r
383                         }\r
384                 }\r
385                 else\r
386                 {\r
387                         snprintf( pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );\r
388                 }\r
389         }\r
390         else\r
391         {\r
392                 /* The find struct has already been created.  Find the next file in\r
393                 the directory. */\r
394                 ucReturned = f_findnext( pxFindStruct );\r
395 \r
396                 if( ucReturned == F_NO_ERROR )\r
397                 {\r
398                         prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );\r
399                         xReturn = pdPASS;\r
400                 }\r
401                 else\r
402                 {\r
403                         /* There are no more files.  Free the find structure. */\r
404                         vPortFree( pxFindStruct );\r
405                         pxFindStruct = NULL;\r
406 \r
407                         /* No string to return. */\r
408                         pcWriteBuffer[ 0 ] = 0x00;\r
409                 }\r
410         }\r
411 \r
412         strcat( pcWriteBuffer, cliNEW_LINE );\r
413 \r
414         return xReturn;\r
415 }\r
416 /*-----------------------------------------------------------*/\r
417 \r
418 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
419 {\r
420 const char *pcParameter;\r
421 BaseType_t xParameterStringLength;\r
422 unsigned char ucReturned;\r
423 \r
424         /* This function assumes xWriteBufferLen is large enough! */\r
425         ( void ) xWriteBufferLen;\r
426 \r
427         /* Obtain the parameter string. */\r
428         pcParameter = FreeRTOS_CLIGetParameter\r
429                                         (\r
430                                                 pcCommandString,                /* The command string itself. */\r
431                                                 1,                                              /* Return the first parameter. */\r
432                                                 &xParameterStringLength /* Store the parameter string length. */\r
433                                         );\r
434 \r
435         /* Sanity check something was returned. */\r
436         configASSERT( pcParameter );\r
437 \r
438         /* Attempt to delete the file. */\r
439         ucReturned = f_delete( pcParameter );\r
440 \r
441         if( ucReturned == F_NO_ERROR )\r
442         {\r
443                 sprintf( pcWriteBuffer, "%s was deleted", pcParameter );\r
444         }\r
445         else\r
446         {\r
447                 sprintf( pcWriteBuffer, "Error" );\r
448         }\r
449 \r
450         strcat( pcWriteBuffer, cliNEW_LINE );\r
451 \r
452         return pdFALSE;\r
453 }\r
454 /*-----------------------------------------------------------*/\r
455 \r
456 static BaseType_t prvTESTFSCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
457 {\r
458 UBaseType_t uxOriginalPriority;\r
459 \r
460         /* Avoid compiler warnings. */\r
461         ( void ) xWriteBufferLen;\r
462         ( void ) pcCommandString;\r
463 \r
464         /* Limitations in the interaction with the Windows TCP/IP stack require\r
465         the command console to run at the idle priority.  Raise the priority for\r
466         the duration of the tests to ensure there are not multiple switches to the\r
467         idle task as in the simulated environment the idle task hook function may\r
468         include a (relatively) long delay. */\r
469         uxOriginalPriority = uxTaskPriorityGet( NULL );\r
470         vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 );\r
471 \r
472         f_dotest( 0 );\r
473 \r
474         /* Reset back to the original priority. */\r
475         vTaskPrioritySet( NULL, uxOriginalPriority );\r
476 \r
477         sprintf( pcWriteBuffer, "%s", "Test results were sent to Windows console" );\r
478 \r
479         return pdFALSE;\r
480 }\r
481 /*-----------------------------------------------------------*/\r
482 \r
483 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
484 {\r
485 char *pcSourceFile;\r
486 const char *pcDestinationFile;\r
487 BaseType_t xParameterStringLength;\r
488 long lSourceLength, lDestinationLength = 0;\r
489 \r
490         /* Obtain the name of the destination file. */\r
491         pcDestinationFile = FreeRTOS_CLIGetParameter\r
492                                                 (\r
493                                                         pcCommandString,                /* The command string itself. */\r
494                                                         2,                                              /* Return the second parameter. */\r
495                                                         &xParameterStringLength /* Store the parameter string length. */\r
496                                                 );\r
497 \r
498         /* Sanity check something was returned. */\r
499         configASSERT( pcDestinationFile );\r
500 \r
501         /* Obtain the name of the source file. */\r
502         pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter\r
503                                                                 (\r
504                                                                         pcCommandString,                /* The command string itself. */\r
505                                                                         1,                                              /* Return the first parameter. */\r
506                                                                         &xParameterStringLength /* Store the parameter string length. */\r
507                                                                 );\r
508 \r
509         /* Sanity check something was returned. */\r
510         configASSERT( pcSourceFile );\r
511 \r
512         /* Terminate the string. */\r
513         pcSourceFile[ xParameterStringLength ] = 0x00;\r
514 \r
515         /* See if the source file exists, obtain its length if it does. */\r
516         lSourceLength = f_filelength( pcSourceFile );\r
517 \r
518         if( lSourceLength == 0 )\r
519         {\r
520                 sprintf( pcWriteBuffer, "Source file does not exist" );\r
521         }\r
522         else\r
523         {\r
524                 /* See if the destination file exists. */\r
525                 lDestinationLength = f_filelength( pcDestinationFile );\r
526 \r
527                 if( lDestinationLength != 0 )\r
528                 {\r
529                         sprintf( pcWriteBuffer, "Error: Destination file already exists" );\r
530                 }\r
531         }\r
532 \r
533         /* Continue only if the source file exists and the destination file does\r
534         not exist. */\r
535         if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )\r
536         {\r
537                 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )\r
538                 {\r
539                         sprintf( pcWriteBuffer, "Copy made" );\r
540                 }\r
541                 else\r
542                 {\r
543                         sprintf( pcWriteBuffer, "Error during copy" );\r
544                 }\r
545         }\r
546 \r
547         strcat( pcWriteBuffer, cliNEW_LINE );\r
548 \r
549         return pdFALSE;\r
550 }\r
551 /*-----------------------------------------------------------*/\r
552 \r
553 static BaseType_t prvPerformCopy( const char *pcSourceFile,\r
554                                                                         int32_t lSourceFileLength,\r
555                                                                         const char *pcDestinationFile,\r
556                                                                         char *pxWriteBuffer,\r
557                                                                         size_t xWriteBufferLen )\r
558 {\r
559 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;\r
560 F_FILE *pxFile;\r
561 BaseType_t xReturn = pdPASS;\r
562 \r
563         /* NOTE:  Error handling has been omitted for clarity. */\r
564 \r
565         while( lBytesRead < lSourceFileLength )\r
566         {\r
567                 /* How many bytes are left? */\r
568                 lBytesRemaining = lSourceFileLength - lBytesRead;\r
569 \r
570                 /* How many bytes should be read this time around the loop.  Can't\r
571                 read more bytes than will fit into the buffer. */\r
572                 if( lBytesRemaining > ( long ) xWriteBufferLen )\r
573                 {\r
574                         lBytesToRead = ( long ) xWriteBufferLen;\r
575                 }\r
576                 else\r
577                 {\r
578                         lBytesToRead = lBytesRemaining;\r
579                 }\r
580 \r
581                 /* Open the source file, seek past the data that has already been\r
582                 read from the file, read the next block of data, then close the\r
583                 file again so the destination file can be opened. */\r
584                 pxFile = f_open( pcSourceFile, "r" );\r
585                 if( pxFile != NULL )\r
586                 {\r
587                         f_seek( pxFile, lBytesRead, F_SEEK_SET );\r
588                         f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );\r
589                         f_close( pxFile );\r
590                 }\r
591                 else\r
592                 {\r
593                         xReturn = pdFAIL;\r
594                         break;\r
595                 }\r
596 \r
597                 /* Open the destination file and write the block of data to the end of\r
598                 the file. */\r
599                 pxFile = f_open( pcDestinationFile, "a" );\r
600                 if( pxFile != NULL )\r
601                 {\r
602                         f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );\r
603                         f_close( pxFile );\r
604                 }\r
605                 else\r
606                 {\r
607                         xReturn = pdFAIL;\r
608                         break;\r
609                 }\r
610 \r
611                 lBytesRead += lBytesToRead;\r
612         }\r
613 \r
614         return xReturn;\r
615 }\r
616 /*-----------------------------------------------------------*/\r
617 \r
618 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct )\r
619 {\r
620 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";\r
621 const char * pcAttrib;\r
622 \r
623         /* Point pcAttrib to a string that describes the file. */\r
624         if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )\r
625         {\r
626                 pcAttrib = pcDirectory;\r
627         }\r
628         else if( pxFindStruct->attr & F_ATTR_READONLY )\r
629         {\r
630                 pcAttrib = pcReadOnlyFile;\r
631         }\r
632         else\r
633         {\r
634                 pcAttrib = pcWritableFile;\r
635         }\r
636 \r
637         /* Create a string that includes the file name, the file size and the\r
638         attributes string. */\r
639         sprintf( pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, pxFindStruct->filesize );\r
640 }\r