Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
322 changes: 322 additions & 0 deletions doc/spec_pkcl.md

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions foundation/include/pkcl-nolibc/errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef PKCL_ERRNO_H
#define PKCL_ERRNO_H

/* pkcl の errno(doc/spec_pkcl.md §5)。値は Newlib の sys/errno.h と同じで、
* 後から Newlib を載せても数値が食い違わない。単一のグローバルで、スレッドごとではない。 */
extern int errno;

#define EPERM 1
#define ENOENT 2
#define EINTR 4
#define EIO 5
#define ENOEXEC 8
#define EBADF 9
#define ECHILD 10
#define EAGAIN 11
#define EWOULDBLOCK EAGAIN
#define ENOMEM 12
#define EACCES 13
#define EFAULT 14
#define EBUSY 16
#define EEXIST 17
#define EXDEV 18
#define ENODEV 19
#define ENOTDIR 20
#define EISDIR 21
#define EINVAL 22
#define ENFILE 23
#define EMFILE 24
#define ENOTTY 25
#define EFBIG 27
#define ENOSPC 28
#define ESPIPE 29
#define EROFS 30
#define EPIPE 32
#define EDOM 33
#define ERANGE 34
#define ENOSYS 88
#define ENOTEMPTY 90
#define ENAMETOOLONG 91
#define ELOOP 92
#define EPROTO 71
#define ENOTSUP 134
#define EOVERFLOW 139
#define ETIMEDOUT 116

#endif
29 changes: 29 additions & 0 deletions foundation/include/pkcl-nolibc/fcntl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef PKCL_FCNTL_H
#define PKCL_FCNTL_H
#include <sys/types.h>

/* 値は Newlib の sys/_default_fcntl.h と同じ。 */
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_ACCMODE 3
#define O_APPEND 0x0008
#define O_CREAT 0x0200
#define O_TRUNC 0x0400
#define O_EXCL 0x0800
#define O_NONBLOCK 0x4000
#define O_CLOEXEC 0x40000
#define O_DIRECTORY 0x200000

#define F_DUPFD 0
#define F_GETFD 1
#define F_SETFD 2
#define F_GETFL 3
#define F_SETFL 4
#define FD_CLOEXEC 1

int open(const char *path, int flags, ...);
int creat(const char *path, mode_t mode);
int fcntl(int fd, int cmd, ...);

#endif
55 changes: 55 additions & 0 deletions foundation/include/pkcl-nolibc/sys/stat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef PKCL_SYS_STAT_H
#define PKCL_SYS_STAT_H
#include <sys/types.h>

#define S_IFMT 0170000
#define S_IFDIR 0040000
#define S_IFCHR 0020000
#define S_IFREG 0100000
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISBLK(m) 0
#define S_ISFIFO(m) 0
#define S_ISLNK(m) 0
#define S_ISSOCK(m) 0

#define S_IRUSR 0400
#define S_IWUSR 0200
#define S_IXUSR 0100
#define S_IRWXU 0700
#define S_IRGRP 0040
#define S_IWGRP 0020
#define S_IXGRP 0010
#define S_IRWXG 0070
#define S_IROTH 0004
#define S_IWOTH 0002
#define S_IXOTH 0001
#define S_IRWXO 0007

/* FS2 に inode 番号・リンク数・GID・atime はない。st_ino は 0、st_nlink は 1、
* st_gid は 0、st_rdev は 0、st_blksize は 512、st_atime は st_mtime と同じ(doc/spec_pkcl.md §6.3)。 */
struct stat {
dev_t st_dev;
ino_t st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
dev_t st_rdev;
off_t st_size;
time_t st_atime;
time_t st_mtime;
time_t st_ctime;
int32_t st_blksize;
int32_t st_blocks;
};

int stat(const char *path, struct stat *st);
int lstat(const char *path, struct stat *st);
int fstat(int fd, struct stat *st);
int mkdir(const char *path, mode_t mode);
int chmod(const char *path, mode_t mode);
mode_t umask(mode_t mask);

#endif
19 changes: 19 additions & 0 deletions foundation/include/pkcl-nolibc/sys/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef PKCL_SYS_TYPES_H
#define PKCL_SYS_TYPES_H
#include <stddef.h>
#include <stdint.h>

/* off_t は 32 bit(2 GiB 未満)。BusyBox は CONFIG_LFS=n で組む。
* 大きなオフセットは EOVERFLOW になる(doc/spec_pkcl.md §4)。 */
typedef int32_t ssize_t;
typedef int32_t off_t;
typedef uint32_t mode_t;
typedef uint32_t uid_t;
typedef uint32_t gid_t;
typedef int32_t pid_t;
typedef uint32_t dev_t;
typedef uint32_t ino_t;
typedef uint32_t nlink_t;
typedef int64_t time_t;

#endif
41 changes: 41 additions & 0 deletions foundation/include/pkcl-nolibc/unistd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef PKCL_UNISTD_H
#define PKCL_UNISTD_H
#include <sys/types.h>

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2

#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2

#define F_OK 0
#define X_OK 1
#define W_OK 2
#define R_OK 4

int close(int fd);
ssize_t read(int fd, void *buf, size_t len);
ssize_t write(int fd, const void *buf, size_t len);
off_t lseek(int fd, off_t offset, int whence);
int dup(int fd);
int dup2(int fd, int newfd);
int isatty(int fd);
int ftruncate(int fd, off_t length);
int truncate(const char *path, off_t length);
int fsync(int fd);
int access(const char *path, int mode);
int unlink(const char *path);
int rmdir(const char *path);
int rename(const char *old_path, const char *new_path);
int chdir(const char *path);
char *getcwd(char *buf, size_t size);
void _exit(int code) __attribute__((noreturn));

uid_t getuid(void);
uid_t geteuid(void);
gid_t getgid(void);
gid_t getegid(void);

#endif
10 changes: 10 additions & 0 deletions foundation/include/pkcl-posix/arpa/inet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef PKCL_ARPA_INET_H
#define PKCL_ARPA_INET_H
#include <netinet/in.h>

int inet_aton(const char *text, struct in_addr *addr);
char *inet_ntoa(struct in_addr addr);
int inet_pton(int family, const char *text, void *dst);
const char *inet_ntop(int family, const void *src, char *dst, socklen_t size);

#endif
9 changes: 9 additions & 0 deletions foundation/include/pkcl-posix/byteswap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef PKCL_BYTESWAP_H
#define PKCL_BYTESWAP_H
#include <stdint.h>

#define bswap_16(x) ((uint16_t)__builtin_bswap16((uint16_t)(x)))
#define bswap_32(x) ((uint32_t)__builtin_bswap32((uint32_t)(x)))
#define bswap_64(x) ((uint64_t)__builtin_bswap64((uint64_t)(x)))

#endif
32 changes: 32 additions & 0 deletions foundation/include/pkcl-posix/dirent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef PKCL_DIRENT_H
#define PKCL_DIRENT_H
#include <sys/types.h>

/* Directory streams over fnd_dir_open / fnd_dir_next (doc/spec_pkcl.md §6.2).
* Newlib's own <dirent.h> is an #error on a target without one. */
#define DT_UNKNOWN 0
#define DT_CHR 2
#define DT_DIR 4
#define DT_REG 8

#define NAME_MAX 1024
#ifndef MAXNAMLEN
#define MAXNAMLEN NAME_MAX
#endif

struct dirent {
ino_t d_ino;
unsigned char d_type;
char d_name[NAME_MAX + 1];
};

typedef struct pkcl_dir DIR;

DIR *opendir(const char *path);
struct dirent *readdir(DIR *dir);
int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result);
void rewinddir(DIR *dir);
int closedir(DIR *dir);
int dirfd(DIR *dir);

#endif
12 changes: 12 additions & 0 deletions foundation/include/pkcl-posix/endian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef PKCL_ENDIAN_H
#define PKCL_ENDIAN_H
#include <machine/endian.h>

/* Newlib spells these with one underscore; glibc-style code (BusyBox) tests the double one. */
#ifndef __BYTE_ORDER
#define __BYTE_ORDER _BYTE_ORDER
#define __BIG_ENDIAN _BIG_ENDIAN
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
#endif

#endif
1 change: 1 addition & 0 deletions foundation/include/pkcl-posix/features.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <sys/features.h>
7 changes: 7 additions & 0 deletions foundation/include/pkcl-posix/glob.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef PKCL_GLOB_H
#define PKCL_GLOB_H
#include_next <glob.h>
#ifndef GLOB_NOMATCH
#define GLOB_NOMATCH (-3)
#endif
#endif
19 changes: 19 additions & 0 deletions foundation/include/pkcl-posix/mntent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef PKCL_MNTENT_H
#define PKCL_MNTENT_H
#include <stdio.h>

/* Foundation has no mount table to read (doc/spec_pkcl.md §6.8). */
struct mntent {
char *mnt_fsname;
char *mnt_dir;
char *mnt_type;
char *mnt_opts;
int mnt_freq;
int mnt_passno;
};

FILE *setmntent(const char *file, const char *mode);
struct mntent *getmntent(FILE *stream);
int endmntent(FILE *stream);

#endif
4 changes: 4 additions & 0 deletions foundation/include/pkcl-posix/net/if.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef PKCL_NET_IF_H
#define PKCL_NET_IF_H
#define IF_NAMESIZE 16
#endif
50 changes: 50 additions & 0 deletions foundation/include/pkcl-posix/netdb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef PKCL_NETDB_H
#define PKCL_NETDB_H
#include <sys/socket.h>

/* No resolver (doc/spec_pkcl.md §6.8): declarations only. */
struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};

struct addrinfo {
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
socklen_t ai_addrlen;
struct sockaddr *ai_addr;
char *ai_canonname;
struct addrinfo *ai_next;
};

#define AI_PASSIVE 1
#define AI_CANONNAME 2
#define AI_NUMERICHOST 4
#define NI_NUMERICHOST 1
#define NI_NUMERICSERV 2
#define NI_NAMEREQD 8
extern int h_errno;
#define HOST_NOT_FOUND 1
#define TRY_AGAIN 2
#define NO_RECOVERY 3
#define NO_DATA 4
#define EAI_NONAME (-2)
#define EAI_FAIL (-4)
#define EAI_SYSTEM (-11)

struct servent { char *s_name; char **s_aliases; int s_port; char *s_proto; };
struct servent *getservbyname(const char *name, const char *proto);
const char *hstrerror(int err);
struct hostent *gethostbyname(const char *name);
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
void freeaddrinfo(struct addrinfo *res);
const char *gai_strerror(int code);
int getnameinfo(const struct sockaddr *addr, socklen_t len, char *host, socklen_t hostlen,
char *service, socklen_t servicelen, int flags);

#endif
45 changes: 45 additions & 0 deletions foundation/include/pkcl-posix/netinet/in.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef PKCL_NETINET_IN_H
#define PKCL_NETINET_IN_H
#include <stdint.h>
#include <sys/socket.h>

typedef uint16_t in_port_t;
typedef uint32_t in_addr_t;

struct in_addr {
in_addr_t s_addr;
};

struct in6_addr {
uint8_t s6_addr[16];
};

struct sockaddr_in {
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
unsigned char sin_zero[8];
};

struct sockaddr_in6 {
sa_family_t sin6_family;
in_port_t sin6_port;
uint32_t sin6_flowinfo;
struct in6_addr sin6_addr;
uint32_t sin6_scope_id;
};

#define INADDR_ANY ((in_addr_t)0)
#define INADDR_LOOPBACK ((in_addr_t)0x7f000001)
#define IPPROTO_TCP 6
#define IPPROTO_UDP 17
#define INET_ADDRSTRLEN 16
#define INET6_ADDRSTRLEN 46

/* SRC32 is big endian: network order is host order. */
#define htons(x) ((uint16_t)(x))
#define ntohs(x) ((uint16_t)(x))
#define htonl(x) ((uint32_t)(x))
#define ntohl(x) ((uint32_t)(x))

#endif
Loading
Loading