Compare commits

..

6 Commits

Author SHA1 Message Date
e54f033578 libc: Add wcslen (with a test) and wcscat 2022-11-12 14:16:00 +01:00
35829a6998 libc: Remove unnecessary casts in strcat() and strncat() 2022-11-12 14:15:38 +01:00
6ab246a05e libc: Add a basic implementation of pathconf() 2022-11-12 14:15:21 +01:00
9e8a57cec7 libc: Add getdtablesize()
No system call for this, since the limit is fixed right now
2022-11-12 14:15:02 +01:00
f46831f459 libc: Define PATH_MAX
This restriction is actually not enforced by the kernel. It should be.
2022-11-12 14:13:28 +01:00
5fa8569ff9 libc: Add creat() 2022-11-12 14:12:49 +01:00
11 changed files with 111 additions and 8 deletions

View File

@ -1,6 +1,8 @@
#ifndef _FCNTL_H #ifndef _FCNTL_H
#define _FCNTL_H #define _FCNTL_H
#include <sys/types.h>
/* Open for reading only. */ /* Open for reading only. */
#define O_RDONLY 1 #define O_RDONLY 1
/* Open for writing only. */ /* Open for writing only. */
@ -42,6 +44,10 @@ extern "C"
/* Opens the file specified by pathname. Returns a file descriptor on success, or -1 on error. */ /* Opens the file specified by pathname. Returns a file descriptor on success, or -1 on error. */
int open(const char* pathname, int flags, ...); int open(const char* pathname, int flags, ...);
/* Opens the file specified by pathname, creating it if it doesn't exist or overwriting it otherwise. Calling this
* function is equivalent to calling open(pathname, O_WRONLY|O_CREAT|O_TRUNC, mode) */
int creat(const char* pathname, mode_t mode);
/* Performs an operation on the file descriptor fd determined by cmd. */ /* Performs an operation on the file descriptor fd determined by cmd. */
int fcntl(int fd, int cmd, ...); int fcntl(int fd, int cmd, ...);

View File

@ -5,6 +5,7 @@
#define ATEXIT_MAX 32 #define ATEXIT_MAX 32
#define NAME_MAX 64 #define NAME_MAX 64
#define PATH_MAX 4096
#define PAGESIZE 4096 #define PAGESIZE 4096
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096

View File

@ -6,15 +6,20 @@
#include <luna/os-limits.h> #include <luna/os-limits.h>
#include <sys/types.h> #include <sys/types.h>
// Standard IO streams.
#define STDIN_FILENO 0 // The standard input stream. #define STDIN_FILENO 0 // The standard input stream.
#define STDOUT_FILENO 1 // The standard output stream. #define STDOUT_FILENO 1 // The standard output stream.
#define STDERR_FILENO 2 // The standard error stream. #define STDERR_FILENO 2 // The standard error stream.
// Possible arguments to access()
#define F_OK 0 // Check for a file's existence. #define F_OK 0 // Check for a file's existence.
#define R_OK 1 // Check whether a file is readable. #define R_OK 1 // Check whether a file is readable.
#define W_OK 2 // Check whether a file is writable. #define W_OK 2 // Check whether a file is writable.
#define X_OK 4 // Check whether a file is executable. #define X_OK 4 // Check whether a file is executable.
// Name values for pathconf()
#define _PC_PATH_MAX 0 // Maximum length of a path relative to the provided name.
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
@ -103,6 +108,12 @@ extern "C"
int chdir(const char* path); // Not implemented. int chdir(const char* path); // Not implemented.
int pipe(int fd[2]); // Not implemented. int pipe(int fd[2]); // Not implemented.
/* Returns a configuration value for the file at path. */
long pathconf(const char* path, int name);
/* Returns the maximum number of file descriptors a process can have open at the same time. */
int getdtablesize(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -1,11 +1,24 @@
#ifndef _WCHAR_H #ifndef _WCHAR_H
#define _WCHAR_H #define _WCHAR_H
#include <bits/macros.h>
#include <stddef.h> #include <stddef.h>
#ifndef __WINT_TYPE__ typedef unsigned int wint_t;
#define __WINT_TYPE__ unsigned int
#ifdef __cplusplus
extern "C"
{
#endif
/* Return the length of a wide-character string. */
size_t wcslen(const wchar_t* str);
/* Concatenates the wide-character string src into dest. */
__lc_is_deprecated wchar_t* wcscat(wchar_t* dest, const wchar_t* src);
#ifdef __cplusplus
}
#endif #endif
typedef __WINT_TYPE__ wint_t;
#endif #endif

View File

@ -15,6 +15,13 @@ extern "C"
return (int)result; return (int)result;
} }
int creat(const char* pathname, mode_t mode)
{
return (int)__lc_fast_syscall3(SYS_open, pathname, O_WRONLY | O_CREAT | O_TRUNC,
mode); // we don't need to pass this through to open(), we can avoid variadic
// stuff since we're sure mode exists.
}
int fcntl(int fd, int cmd, ...) int fcntl(int fd, int cmd, ...)
{ {
va_list ap; va_list ap;

View File

@ -203,9 +203,9 @@ extern "C"
size_t dest_len = strlen(dest); size_t dest_len = strlen(dest);
size_t i; size_t i;
for (i = 0; *(src + i); i++) *(char*)(dest + dest_len + i) = *(const char*)(src + i); for (i = 0; *(src + i); i++) *(dest + dest_len + i) = *(src + i);
*(char*)(dest + dest_len + i) = '\0'; *(dest + dest_len + i) = '\0';
return dest; return dest;
} }
@ -215,9 +215,9 @@ extern "C"
size_t dest_len = strlen(dest); size_t dest_len = strlen(dest);
size_t i; size_t i;
for (i = 0; i < max && *(src + i); i++) *(char*)(dest + dest_len + i) = *(const char*)(src + i); for (i = 0; i < max && *(src + i); i++) *(dest + dest_len + i) = *(src + i);
*(char*)(dest + dest_len + i) = '\0'; *(dest + dest_len + i) = '\0';
return dest; return dest;
} }

View File

@ -163,4 +163,18 @@ extern "C"
{ {
NOT_IMPLEMENTED("pipe"); NOT_IMPLEMENTED("pipe");
} }
long pathconf(const char* path, int name)
{
switch (name)
{
case _PC_PATH_MAX: return PATH_MAX - (strlen(path) + 1);
default: errno = EINVAL; return -1;
}
}
int getdtablesize(void)
{
return OPEN_MAX;
}
} }

24
libs/libc/src/wchar.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <wchar.h>
extern "C"
{
size_t wcslen(const wchar_t* str)
{
const wchar_t* i = str;
for (; *i; ++i)
;
return (i - str);
}
wchar_t* wcscat(wchar_t* dest, const wchar_t* src)
{
size_t dest_len = wcslen(dest);
size_t i;
for (i = 0; *(src + i); i++) *(dest + dest_len + i) = *(src + i);
*(dest + dest_len + i) = L'\0';
return dest;
}
}

View File

@ -4,7 +4,7 @@ DESTDIR := $(LUNA_ROOT)/initrd/bin
build: build:
@mkdir -p $(TESTDIR)/bin @mkdir -p $(TESTDIR)/bin
$(LUNA_ROOT)/tools/sync-libc.sh $(LUNA_ROOT)/tools/sync-libc.sh
$(CC) $(TESTDIR)/string.c $(TESTDIR)/stdlib.c $(TESTDIR)/Test.c -I$(LUNA_ROOT)/tests -o $(TESTDIR)/bin/test-libc -Wall -Wextra -Wno-deprecated-declarations -Wno-stringop-overread -Werror $(CC) $(TESTDIR)/string.c $(TESTDIR)/stdlib.c $(TESTDIR)/wchar.c $(TESTDIR)/Test.c -I$(LUNA_ROOT)/tests -o $(TESTDIR)/bin/test-libc -Wall -Wextra -Wno-deprecated-declarations -Wno-stringop-overread -Werror
install: install:
$(LUNA_ROOT)/tools/clean.sh $(LUNA_ROOT)/tools/clean.sh

View File

@ -30,6 +30,9 @@ DEFINE_TEST(mktemp);
DEFINE_TEST(qsort); DEFINE_TEST(qsort);
DEFINE_TEST(bsearch); DEFINE_TEST(bsearch);
// wchar.h
DEFINE_TEST(wcslen);
int main() int main()
{ {
START_TEST_CASE(string.h); START_TEST_CASE(string.h);
@ -61,4 +64,7 @@ int main()
RUN_TEST(mktemp); RUN_TEST(mktemp);
RUN_TEST(qsort); RUN_TEST(qsort);
RUN_TEST(bsearch); RUN_TEST(bsearch);
START_TEST_CASE(wchar.h);
RUN_TEST(wcslen);
} }

21
tests/libc/wchar.c Normal file
View File

@ -0,0 +1,21 @@
#include "Test.h"
#include <wchar.h>
DEFINE_TEST(wcslen)
{
START_TEST(wcslen);
const wchar_t* str = L"Hello, World!";
size_t len = wcslen(str);
EXPECT_EQ(len, 13);
wchar_t null[] = {L'\0'};
len = wcslen(null);
EXPECT_EQ(len, 0);
TEST_SUCCESS();
}