summaryrefslogtreecommitdiff
path: root/apps/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/misc.c')
-rw-r--r--apps/misc.c74
1 files changed, 38 insertions, 36 deletions
diff --git a/apps/misc.c b/apps/misc.c
index f9c6116205..eebcc9aebc 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -21,6 +21,7 @@
21#include <stdlib.h> 21#include <stdlib.h>
22#include <ctype.h> 22#include <ctype.h>
23#include <string.h> 23#include <string.h>
24#include <errno.h>
24#include "config.h" 25#include "config.h"
25#include "misc.h" 26#include "misc.h"
26#include "lcd.h" 27#include "lcd.h"
@@ -33,7 +34,6 @@
33#include "lang.h" 34#include "lang.h"
34#include "dir.h" 35#include "dir.h"
35#include "lcd-remote.h" 36#include "lcd-remote.h"
36#include "errno.h"
37#include "system.h" 37#include "system.h"
38#include "timefuncs.h" 38#include "timefuncs.h"
39#include "screens.h" 39#include "screens.h"
@@ -158,41 +158,6 @@ bool warn_on_pl_erase(void)
158 return true; 158 return true;
159} 159}
160 160
161/* Read (up to) a line of text from fd into buffer and return number of bytes
162 * read (which may be larger than the number of bytes stored in buffer). If
163 * an error occurs, -1 is returned (and buffer contains whatever could be
164 * read). A line is terminated by a LF char. Neither LF nor CR chars are
165 * stored in buffer.
166 */
167int read_line(int fd, char* buffer, int buffer_size)
168{
169 int count = 0;
170 int num_read = 0;
171
172 errno = 0;
173
174 while (count < buffer_size)
175 {
176 unsigned char c;
177
178 if (1 != read(fd, &c, 1))
179 break;
180
181 num_read++;
182
183 if ( c == '\n' )
184 break;
185
186 if ( c == '\r' )
187 continue;
188
189 buffer[count++] = c;
190 }
191
192 buffer[MIN(count, buffer_size - 1)] = 0;
193
194 return errno ? -1 : num_read;
195}
196 161
197/* Performance optimized version of the previous function. */ 162/* Performance optimized version of the previous function. */
198int fast_readline(int fd, char *buf, int buf_size, void *parameters, 163int fast_readline(int fd, char *buf, int buf_size, void *parameters,
@@ -841,6 +806,43 @@ char *strip_extension(char* buffer, int buffer_size, const char *filename)
841} 806}
842#endif /* !defined(__PCTOOL__) */ 807#endif /* !defined(__PCTOOL__) */
843 808
809/* Read (up to) a line of text from fd into buffer and return number of bytes
810 * read (which may be larger than the number of bytes stored in buffer). If
811 * an error occurs, -1 is returned (and buffer contains whatever could be
812 * read). A line is terminated by a LF char. Neither LF nor CR chars are
813 * stored in buffer.
814 */
815int read_line(int fd, char* buffer, int buffer_size)
816{
817 int count = 0;
818 int num_read = 0;
819
820 errno = 0;
821
822 while (count < buffer_size)
823 {
824 unsigned char c;
825
826 if (1 != read(fd, &c, 1))
827 break;
828
829 num_read++;
830
831 if ( c == '\n' )
832 break;
833
834 if ( c == '\r' )
835 continue;
836
837 buffer[count++] = c;
838 }
839
840 buffer[MIN(count, buffer_size - 1)] = 0;
841
842 return errno ? -1 : num_read;
843}
844
845
844char* skip_whitespace(char* const str) 846char* skip_whitespace(char* const str)
845{ 847{
846 char *s = str; 848 char *s = str;