diff options
| author | Linus Nielsen Feltzing <linus@haxx.se> | 2003-05-04 02:04:31 +0000 |
|---|---|---|
| committer | Linus Nielsen Feltzing <linus@haxx.se> | 2003-05-04 02:04:31 +0000 |
| commit | 75b41a88f6bfdc478d16d0f3ad9341a6fac459d8 (patch) | |
| tree | b3f1b4f5831be936a47ab970f5c3f532650ad2fa /apps/misc.c | |
| parent | 93e47b922f38b2ab7a6b485361943b8566e56629 (diff) | |
| download | rockbox-75b41a88f6bfdc478d16d0f3ad9341a6fac459d8.zip rockbox-75b41a88f6bfdc478d16d0f3ad9341a6fac459d8.tar.gz rockbox-75b41a88f6bfdc478d16d0f3ad9341a6fac459d8.tar.bz2 rockbox-75b41a88f6bfdc478d16d0f3ad9341a6fac459d8.tar.xz | |
Moved read_line() to misc.c
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@3647 a1c6a512-1295-4272-9138-f99709370657
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 a49739f..69cb7d3 100644 --- a/apps/misc.c +++ b/apps/misc.c @@ -21,6 +21,9 @@ #include "file.h" #include "lcd.h" #include "sprintf.h" +#include "errno.h" +#include "system.h" + #define ONE_KILOBYTE 1024 #define ONE_MEGABYTE (1024*1024) @@ -49,6 +52,42 @@ char *num2max5(unsigned int bytes, char *max5) return max5; } +/* Read (up to) a line of text from fd into buffer and return number of bytes + * read (which may be larger than the number of bytes stored in buffer). If + * an error occurs, -1 is returned (and buffer contains whatever could be + * read). A line is terminated by a LF char. Neither LF nor CR chars are + * stored in buffer. + */ +int read_line(int fd, char* buffer, int buffer_size) +{ + int count = 0; + int num_read = 0; + + errno = 0; + + while (count < buffer_size) + { + unsigned char c; + + if (1 != read(fd, &c, 1)) + break; + + num_read++; + + if ( c == '\n' ) + break; + + if ( c == '\r' ) + continue; + + buffer[count++] = c; + } + + buffer[MIN(count, buffer_size - 1)] = 0; + + return errno ? -1 : num_read; +} + #ifdef TEST_MAX5 int main(int argc, char **argv) { |