Skip to content

Commit 1c261e3

Browse files
[libc] Add implementation of getchar
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
1 parent 5e53e1b commit 1c261e3

File tree

11 files changed

+135
-1
lines changed

11 files changed

+135
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ if(LLVM_LIBC_FULL_BUILD)
396396
libc.src.stdio.fwrite
397397
libc.src.stdio.fwrite_unlocked
398398
libc.src.stdio.fprintf
399+
libc.src.stdio.getchar
400+
libc.src.stdio.getchar_unlocked
399401
libc.src.stdio.printf
400402
libc.src.stdio.putc
401403
libc.src.stdio.putchar

libc/config/linux/riscv64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ if(LLVM_LIBC_FULL_BUILD)
409409
libc.src.stdio.fprintf
410410
libc.src.stdio.getc
411411
libc.src.stdio.getc_unlocked
412+
libc.src.stdio.getchar
413+
libc.src.stdio.getchar_unlocked
412414
libc.src.stdio.printf
413415
libc.src.stdio.sscanf
414416
libc.src.stdio.scanf

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ if(LLVM_LIBC_FULL_BUILD)
423423
libc.src.stdio.fwrite_unlocked
424424
libc.src.stdio.getc
425425
libc.src.stdio.getc_unlocked
426+
libc.src.stdio.getchar
427+
libc.src.stdio.getchar_unlocked
426428
libc.src.stdio.sscanf
427429
libc.src.stdio.scanf
428430
libc.src.stdio.fscanf

libc/docs/stdio.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Function Name Available
8383
============= =========
8484
(f)getc |check|
8585
fgets |check|
86-
getchar
86+
getchar |check|
8787
fread |check|
8888
(f)putc |check|
8989
(f)puts |check|

libc/spec/posix.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,11 @@ def POSIX : StandardSpec<"POSIX"> {
10771077
RetValSpec<IntType>,
10781078
[ArgSpec<FILEPtr>]
10791079
>,
1080+
FunctionSpec<
1081+
"getchar_unlocked",
1082+
RetValSpec<IntType>,
1083+
[ArgSpec<VoidType>]
1084+
>,
10801085
]
10811086
>;
10821087

libc/spec/stdc.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@ def StdC : StandardSpec<"stdc"> {
580580
RetValSpec<IntType>,
581581
[ArgSpec<FILEPtr>]
582582
>,
583+
FunctionSpec<
584+
"getchar",
585+
RetValSpec<IntType>,
586+
[ArgSpec<VoidType>]
587+
>,
583588
FunctionSpec<
584589
"putc",
585590
RetValSpec<IntType>,

libc/src/stdio/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,32 @@ add_entrypoint_object(
154154
libc.src.__support.File.platform_file
155155
)
156156

157+
add_entrypoint_object(
158+
getchar
159+
SRCS
160+
getchar.cpp
161+
HDRS
162+
getchar.h
163+
DEPENDS
164+
libc.src.errno.errno
165+
libc.include.stdio
166+
libc.src.__support.File.file
167+
libc.src.__support.File.platform_file
168+
)
169+
170+
add_entrypoint_object(
171+
getchar_unlocked
172+
SRCS
173+
getc_unlocked.cpp
174+
HDRS
175+
getc_unlocked.h
176+
DEPENDS
177+
libc.src.errno.errno
178+
libc.include.stdio
179+
libc.src.__support.File.file
180+
libc.src.__support.File.platform_file
181+
)
182+
157183
add_entrypoint_object(
158184
fgets
159185
SRCS

libc/src/stdio/getchar.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Implementation of getchar -----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdio/getchar.h"
10+
#include "src/__support/File/file.h"
11+
12+
#include "src/errno/libc_errno.h"
13+
#include <stdio.h>
14+
15+
namespace __llvm_libc {
16+
17+
LLVM_LIBC_FUNCTION(int, getchar, ()) {
18+
unsigned char c;
19+
auto result = stdin->read(&c, 1);
20+
if (result.has_error())
21+
libc_errno = result.error;
22+
23+
if (result.value != 1)
24+
return EOF;
25+
return c;
26+
}
27+
28+
} // namespace __llvm_libc

libc/src/stdio/getchar.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header of getchar ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDIO_GETCHAR_H
10+
#define LLVM_LIBC_SRC_STDIO_GETCHAR_H
11+
12+
namespace __llvm_libc {
13+
14+
int getchar();
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_STDIO_GETCHAR_H

libc/src/stdio/getchar_unlocked.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Implementation of getchar_unlocked --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdio/getchar_unlocked.h"
10+
#include "src/__support/File/file.h"
11+
12+
#include "src/errno/libc_errno.h"
13+
#include <stdio.h>
14+
15+
namespace __llvm_libc {
16+
17+
LLVM_LIBC_FUNCTION(int, getchar_unlocked, ()) {
18+
unsigned char c;
19+
auto result = stdin->read_unlocked(&c, 1);
20+
if (result.has_error())
21+
libc_errno = result.error;
22+
23+
if (result.value != 1)
24+
return EOF;
25+
return c;
26+
}
27+
28+
} // namespace __llvm_libc

libc/src/stdio/getchar_unlocked.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header of getchar_unlocked ---------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H
10+
#define LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H
11+
12+
namespace __llvm_libc {
13+
14+
int getchar_unlocked();
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H

0 commit comments

Comments
 (0)