Skip to content

Commit dc2b01b

Browse files
author
Siva Chandra Reddy
committed
[libc] Add POSIX close, fsync, open, read and write functions.
They are implemented as simple syscall wrappers. The file creation macros have been put in a header file as a temporary solution until we have a cleaner approach to listing platform relevant macros. Reviewed By: abrachet Differential Revision: https://reviews.llvm.org/D118396
1 parent 0cf75aa commit dc2b01b

28 files changed

+507
-33
lines changed

libc/config/linux/api.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def AssertAPI : PublicAPI<"assert.h"> {
6262
def CTypeAPI : PublicAPI<"ctype.h"> {
6363
}
6464

65+
def FCntlAPI : PublicAPI<"fcntl.h"> {
66+
let Types = ["mode_t"];
67+
}
68+
6569
def IntTypesAPI : PublicAPI<"inttypes.h"> {
6670
let Types = ["imaxdiv_t"];
6771
}

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ set(TARGET_LIBC_ENTRYPOINTS
2020
# errno.h entrypoints
2121
libc.src.errno.__errno_location
2222

23+
# fcntl.h entrypoints
24+
libc.src.fcntl.open
25+
2326
# string.h entrypoints
2427
libc.src.string.bcmp
2528
libc.src.string.bzero
@@ -81,6 +84,9 @@ set(TARGET_LIBC_ENTRYPOINTS
8184
libc.src.sys.mman.munmap
8285

8386
# unistd.h entrypoints
87+
libc.src.unistd.close
88+
libc.src.unistd.fsync
89+
libc.src.unistd.read
8490
libc.src.unistd.write
8591
)
8692

libc/include/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(llvm-libc-macros)
12
add_subdirectory(llvm-libc-types)
23

34
add_header(
@@ -14,6 +15,16 @@ add_gen_header(
1415
.llvm_libc_common_h
1516
)
1617

18+
add_gen_header(
19+
fcntl
20+
DEF_FILE fcntl.h.def
21+
GEN_HDR fcntl.h
22+
DEPENDS
23+
.llvm_libc_common_h
24+
.llvm-libc-macros.fcntl_macros
25+
.llvm-libc-types.mode_t
26+
)
27+
1728
add_gen_header(
1829
fenv
1930
DEF_FILE fenv.h.def

libc/include/fcntl.h.def

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- C standard library header fcntl.h ---------------------------------===//
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_FCNTL_H
10+
#define LLVM_LIBC_FCNTL_H
11+
12+
#include <__llvm-libc-common.h>
13+
#include <llvm-libc-macros/fcntl-macros.h>
14+
15+
%%public_api()
16+
17+
#endif // LLVM_LIBC_FCNTL_H
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
add_subdirectory(linux)
2+
3+
add_header(
4+
fcntl_macros
5+
HDR
6+
fcntl-macros.h
7+
DEPENDS
8+
.linux.fcntl_macros
9+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef __LLVM_LIBC_MACROS_FCNTL_MACROS_H
2+
#define __LLVM_LIBC_MACROS_FCNTL_MACROS_H
3+
4+
#ifdef __unix__
5+
#include "linux/fcntl-macros.h"
6+
#endif
7+
8+
#endif // __LLVM_LIBC_MACROS_FCNTL_MACROS_H
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_header(
2+
fcntl_macros
3+
HDR
4+
fcntl-macros.h
5+
)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===-- Definition of macros from fcntl.h ---------------------------------===//
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_MACROS_LINUX_FCNTL_MACROS_H
10+
#define __LLVM_LIBC_MACROS_LINUX_FCNTL_MACROS_H
11+
12+
// File creation flags
13+
#define O_CLOEXEC 02000000
14+
#define O_CREAT 00000100
15+
#define O_DIRECTORY 00200000
16+
#define O_EXCL 00000200
17+
#define O_NOCTTY 00000400
18+
#define O_NOFOLLOW 00400000
19+
#define O_TRUNC 00001000
20+
#define O_TMPFILE (020000000 | O_DIRECTORY)
21+
22+
// File status flags
23+
#define O_APPEND 00002000
24+
#define O_DSYNC 00010000
25+
#define O_NONBLOCK 00004000
26+
#define O_SYNC 04000000 | O_DSYNC
27+
28+
// File access mode mask
29+
#define O_ACCMODE 00000003
30+
31+
// File access mode flags
32+
#define O_RDONLY 00000000
33+
#define O_RDWR 00000002
34+
#define O_WRONLY 00000001
35+
36+
// File mode flags
37+
#define S_IRWXU 0700
38+
#define S_IRUSR 0400
39+
#define S_IWUSR 0200
40+
#define S_IXUSR 0100
41+
#define S_IRWXG 070
42+
#define S_IRGRP 040
43+
#define S_IWGRP 020
44+
#define S_IXGRP 010
45+
#define S_IRWXO 07
46+
#define S_IROTH 04
47+
#define S_IWOTH 02
48+
#define S_IXOTH 01
49+
#define S_ISUID 04000
50+
#define S_ISGID 02000
51+
52+
#endif // __LLVM_LIBC_MACROS_LINUX_FCNTL_MACROS_H

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_header(fenv_t HDR fenv_t.h)
1212
add_header(fexcept_t HDR fexcept_t.h)
1313
add_header(float_t HDR float_t.h)
1414
add_header(imaxdiv_t HDR imaxdiv_t.h)
15+
add_header(mode_t HDR mode_t.h)
1516
add_header(mtx_t HDR mtx_t.h)
1617
add_header(off_t HDR off_t.h)
1718
add_header(once_flag HDR once_flag.h)

libc/include/llvm-libc-types/mode_t.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Definition of mode_t type -----------------------------------------===//
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_TYPES_MODE_T_H
10+
#define __LLVM_LIBC_TYPES_MODE_T_H
11+
12+
typedef unsigned mode_t;
13+
14+
#endif // __LLVM_LIBC_TYPES_MODE_T_H

0 commit comments

Comments
 (0)