aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrei Homescu <ahomescu@google.com>2022-11-01 04:56:38 +0000
committerAndrei Homescu <ahomescu@google.com>2023-04-13 21:54:45 +0000
commit4904bc8d0c0ba5c31ee80b1c478dc63d7691d84a (patch)
tree3708e7b71361db57fe981ce4b5ffc5bf0ad15f90 /lib
parent45845c6e5070e25a8661a721985e3f49aa63f6aa (diff)
downloadcommon-4904bc8d0c0ba5c31ee80b1c478dc63d7691d84a.tar.gz
lib/libc: Use lk stdio instead of musl
Update lk stdio for use instead of musl's implementation in libc-trusty. Bug: 230134581 Change-Id: Icdd15292e6197446999333dd252cf079b04f67a0
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/io_handle.c6
-rw-r--r--lib/libc/stdio.c27
2 files changed, 21 insertions, 12 deletions
diff --git a/lib/libc/io_handle.c b/lib/libc/io_handle.c
index ed911496..d42a2355 100644
--- a/lib/libc/io_handle.c
+++ b/lib/libc/io_handle.c
@@ -21,17 +21,19 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
+#include <lib/io.h>
#include <stdio.h>
+#include <trusty/io_handle.h>
io_handle_t* fd_io_handle(int fd) {
- if ((fd == 1) || (fd == 2)) {
+ if ((fd == 0) || (fd == 1) || (fd == 2)) {
return &console_io;
}
return NULL;
}
io_handle_t* file_io_handle(FILE* file) {
- if ((file == stdout) || (file == stderr)) {
+ if ((file == stdin) || (file == stdout) || (file == stderr)) {
return &console_io;
}
return NULL;
diff --git a/lib/libc/stdio.c b/lib/libc/stdio.c
index 8b41bf6c..49671d4f 100644
--- a/lib/libc/stdio.c
+++ b/lib/libc/stdio.c
@@ -20,11 +20,14 @@
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
+#include <printf.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
+#include <trusty/io_handle.h>
+#if LK_LIBC_IMPLEMENTATION_IS_LK
#define DEFINE_STDIO_DESC(id) \
[(id)] = { \
.io = &console_io, \
@@ -36,14 +39,16 @@ __WEAK FILE __stdio_FILEs[3] = {
DEFINE_STDIO_DESC(2), /* stderr */
};
#undef DEFINE_STDIO_DESC
+#endif
static size_t lock_write_commit_unlock(FILE *fp, const char* s, size_t length)
{
size_t bytes_written;
- io_lock(fp->io);
- bytes_written = io_write(fp->io, s, length);
- io_write_commit(fp->io);
- io_unlock(fp->io);
+ io_handle_t *io = file_io_handle(fp);
+ io_lock(io);
+ bytes_written = io_write(io, s, length);
+ io_write_commit(io);
+ io_unlock(io);
return bytes_written;
}
@@ -91,7 +96,8 @@ size_t fwrite(const void *ptr, size_t size, size_t count, FILE *fp)
int getc(FILE *fp)
{
char c;
- ssize_t ret = io_read(fp->io, &c, sizeof(c));
+ io_handle_t *io = file_io_handle(fp);
+ ssize_t ret = io_read(io, &c, sizeof(c));
return (ret > 0) ? c : ret;
}
@@ -103,17 +109,18 @@ int getchar(void)
static int _fprintf_output_func(const char *str, size_t len, void *state)
{
- FILE *fp = (FILE *)state;
+ io_handle_t *io = file_io_handle((FILE *)state);
- return io_write(fp->io, str, len);
+ return io_write(io, str, len);
}
int vfprintf_worker(FILE *fp, const char *fmt, va_list ap, int filtered_on_release)
{
- io_lock(fp->io);
+ io_handle_t *io = file_io_handle(fp);
+ io_lock(io);
int result = _printf_engine(&_fprintf_output_func, (void *)fp, fmt, ap);
- io_write_commit(fp->io);
- io_unlock(fp->io);
+ io_write_commit(io);
+ io_unlock(io);
return result;
}