diff options
Diffstat (limited to 'firmware/common/fdprintf.c')
-rw-r--r-- | firmware/common/fdprintf.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/firmware/common/fdprintf.c b/firmware/common/fdprintf.c new file mode 100644 index 0000000000..21d0a72e58 --- /dev/null +++ b/firmware/common/fdprintf.c | |||
@@ -0,0 +1,56 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by Felix Arends | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include <limits.h> | ||
22 | #include "file.h" | ||
23 | #include "vuprintf.h" | ||
24 | |||
25 | struct for_fprintf { | ||
26 | int fd; /* where to store it */ | ||
27 | int rem; /* amount remaining */ | ||
28 | }; | ||
29 | |||
30 | static int fprfunc(void *pr, int letter) | ||
31 | { | ||
32 | struct for_fprintf *fpr = (struct for_fprintf *)pr; | ||
33 | |||
34 | /* TODO: add a small buffer to reduce write() calls */ | ||
35 | if (write(fpr->fd, &(char){ letter }, 1) > 0) { | ||
36 | return --fpr->rem; | ||
37 | } | ||
38 | |||
39 | return -1; | ||
40 | } | ||
41 | |||
42 | int fdprintf(int fd, const char *fmt, ...) | ||
43 | { | ||
44 | int bytes; | ||
45 | struct for_fprintf fpr; | ||
46 | va_list ap; | ||
47 | |||
48 | fpr.fd = fd; | ||
49 | fpr.rem = INT_MAX; | ||
50 | |||
51 | va_start(ap, fmt); | ||
52 | bytes = vuprintf(fprfunc, &fpr, fmt, ap); | ||
53 | va_end(ap); | ||
54 | |||
55 | return bytes; | ||
56 | } | ||