diff options
Diffstat (limited to 'apps/misc.c')
-rw-r--r-- | apps/misc.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/apps/misc.c b/apps/misc.c index a49739f92d..69cb7d3174 100644 --- a/apps/misc.c +++ b/apps/misc.c | |||
@@ -21,6 +21,9 @@ | |||
21 | #include "file.h" | 21 | #include "file.h" |
22 | #include "lcd.h" | 22 | #include "lcd.h" |
23 | #include "sprintf.h" | 23 | #include "sprintf.h" |
24 | #include "errno.h" | ||
25 | #include "system.h" | ||
26 | |||
24 | #define ONE_KILOBYTE 1024 | 27 | #define ONE_KILOBYTE 1024 |
25 | #define ONE_MEGABYTE (1024*1024) | 28 | #define ONE_MEGABYTE (1024*1024) |
26 | 29 | ||
@@ -49,6 +52,42 @@ char *num2max5(unsigned int bytes, char *max5) | |||
49 | return max5; | 52 | return max5; |
50 | } | 53 | } |
51 | 54 | ||
55 | /* Read (up to) a line of text from fd into buffer and return number of bytes | ||
56 | * read (which may be larger than the number of bytes stored in buffer). If | ||
57 | * an error occurs, -1 is returned (and buffer contains whatever could be | ||
58 | * read). A line is terminated by a LF char. Neither LF nor CR chars are | ||
59 | * stored in buffer. | ||
60 | */ | ||
61 | int read_line(int fd, char* buffer, int buffer_size) | ||
62 | { | ||
63 | int count = 0; | ||
64 | int num_read = 0; | ||
65 | |||
66 | errno = 0; | ||
67 | |||
68 | while (count < buffer_size) | ||
69 | { | ||
70 | unsigned char c; | ||
71 | |||
72 | if (1 != read(fd, &c, 1)) | ||
73 | break; | ||
74 | |||
75 | num_read++; | ||
76 | |||
77 | if ( c == '\n' ) | ||
78 | break; | ||
79 | |||
80 | if ( c == '\r' ) | ||
81 | continue; | ||
82 | |||
83 | buffer[count++] = c; | ||
84 | } | ||
85 | |||
86 | buffer[MIN(count, buffer_size - 1)] = 0; | ||
87 | |||
88 | return errno ? -1 : num_read; | ||
89 | } | ||
90 | |||
52 | #ifdef TEST_MAX5 | 91 | #ifdef TEST_MAX5 |
53 | int main(int argc, char **argv) | 92 | int main(int argc, char **argv) |
54 | { | 93 | { |