Skip to content

Commit

Permalink
[libc] Add implementation of getchar
Browse files Browse the repository at this point in the history
added getchar and getchar_unlocked which are just wrappers getc and getc_unlocked respectively.

Reviewed By: sivachandra, lntue, michaelrj

Differential Revision: https://reviews.llvm.org/D147919
  • Loading branch information
michaelrj-google committed Apr 14, 2023
1 parent 5e53e1b commit 1c261e3
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 1 deletion.
2 changes: 2 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fwrite
libc.src.stdio.fwrite_unlocked
libc.src.stdio.fprintf
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
libc.src.stdio.printf
libc.src.stdio.putc
libc.src.stdio.putchar
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/riscv64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fprintf
libc.src.stdio.getc
libc.src.stdio.getc_unlocked
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
libc.src.stdio.printf
libc.src.stdio.sscanf
libc.src.stdio.scanf
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.stdio.fwrite_unlocked
libc.src.stdio.getc
libc.src.stdio.getc_unlocked
libc.src.stdio.getchar
libc.src.stdio.getchar_unlocked
libc.src.stdio.sscanf
libc.src.stdio.scanf
libc.src.stdio.fscanf
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/stdio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Function Name Available
============= =========
(f)getc |check|
fgets |check|
getchar
getchar |check|
fread |check|
(f)putc |check|
(f)puts |check|
Expand Down
5 changes: 5 additions & 0 deletions libc/spec/posix.td
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,11 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<IntType>,
[ArgSpec<FILEPtr>]
>,
FunctionSpec<
"getchar_unlocked",
RetValSpec<IntType>,
[ArgSpec<VoidType>]
>,
]
>;

Expand Down
5 changes: 5 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ def StdC : StandardSpec<"stdc"> {
RetValSpec<IntType>,
[ArgSpec<FILEPtr>]
>,
FunctionSpec<
"getchar",
RetValSpec<IntType>,
[ArgSpec<VoidType>]
>,
FunctionSpec<
"putc",
RetValSpec<IntType>,
Expand Down
26 changes: 26 additions & 0 deletions libc/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,32 @@ add_entrypoint_object(
libc.src.__support.File.platform_file
)

add_entrypoint_object(
getchar
SRCS
getchar.cpp
HDRS
getchar.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
getchar_unlocked
SRCS
getc_unlocked.cpp
HDRS
getc_unlocked.h
DEPENDS
libc.src.errno.errno
libc.include.stdio
libc.src.__support.File.file
libc.src.__support.File.platform_file
)

add_entrypoint_object(
fgets
SRCS
Expand Down
28 changes: 28 additions & 0 deletions libc/src/stdio/getchar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- Implementation of getchar -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdio/getchar.h"
#include "src/__support/File/file.h"

#include "src/errno/libc_errno.h"
#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, getchar, ()) {
unsigned char c;
auto result = stdin->read(&c, 1);
if (result.has_error())
libc_errno = result.error;

if (result.value != 1)
return EOF;
return c;
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/stdio/getchar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header of getchar ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDIO_GETCHAR_H
#define LLVM_LIBC_SRC_STDIO_GETCHAR_H

namespace __llvm_libc {

int getchar();

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_GETCHAR_H
28 changes: 28 additions & 0 deletions libc/src/stdio/getchar_unlocked.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- Implementation of getchar_unlocked --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdio/getchar_unlocked.h"
#include "src/__support/File/file.h"

#include "src/errno/libc_errno.h"
#include <stdio.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, getchar_unlocked, ()) {
unsigned char c;
auto result = stdin->read_unlocked(&c, 1);
if (result.has_error())
libc_errno = result.error;

if (result.value != 1)
return EOF;
return c;
}

} // namespace __llvm_libc
18 changes: 18 additions & 0 deletions libc/src/stdio/getchar_unlocked.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header of getchar_unlocked ---------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H
#define LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H

namespace __llvm_libc {

int getchar_unlocked();

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H

0 comments on commit 1c261e3

Please sign in to comment.