libc: cleanup, some missing things

This commit is contained in:
K. Lange 2021-09-18 13:30:47 +09:00
parent 33848f8a8e
commit 8bca4ee6c8
10 changed files with 153 additions and 9 deletions

View File

@ -49,5 +49,6 @@ typedef int clockid_t;
#define CLOCK_MONOTONIC 1
extern int clock_gettime(clockid_t clk_id, struct timespec *tp);
extern int clock_getres(clockid_t clk_id, struct timespec *res);
_End_C_Header

View File

@ -66,15 +66,12 @@ extern int optind, opterr, optopt;
extern int unlink(const char * pathname);
/* Unimplemented stubs */
struct utimbuf {
time_t actime;
time_t modtime;
};
extern char * ttyname(int fd);
extern int utime(const char *filename, const struct utimbuf *times);
extern int rmdir(const char *pathname); /* TODO rm probably just works */
extern int chown(const char * pathname, uid_t owner, gid_t group);
extern char * getlogin(void);
extern char * ttyname(int fd);
extern int ttyname_r(int fd, char * buf, size_t buflen);
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
@ -95,6 +92,7 @@ extern int sethostname(const char * name, size_t len);
extern pid_t setsid(void);
extern int setpgid(pid_t, pid_t);
extern pid_t getpgid(pid_t);
extern pid_t getpgrp(void);
extern unsigned int alarm(unsigned int seconds);

15
base/usr/include/utime.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <_cheader.h>
#include <sys/types.h>
_Begin_C_Header
struct utimbuf {
time_t actime;
time_t modtime;
};
extern int utime(const char *filename, const struct utimbuf *times);
_End_C_Header

View File

@ -15,6 +15,12 @@ extern wchar_t *wcspbrk(const wchar_t *wcs, const wchar_t *accept);
extern wchar_t * wcschr(const wchar_t *wcs, wchar_t wc);
extern wchar_t * wcsrchr(const wchar_t *wcs, wchar_t wc);
extern wchar_t * wcsncat(wchar_t *dest, const wchar_t * src, size_t n);
extern int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n);
extern wchar_t * wcscpy(wchar_t * dest, const wchar_t * src);
extern unsigned long int wcstoul(const wchar_t *nptr, wchar_t **endptr, int base);
extern unsigned long long int wcstoull(const char *nptr, wchar_t **endptr, int base);
extern long int wcstol(const wchar_t *nptr, wchar_t **endptr, int base);
extern long long int wcstoll(const wchar_t *nptr, wchar_t **endptr, int base);
typedef unsigned int wint_t;
_End_C_Header

View File

@ -2,6 +2,17 @@
#include <errno.h>
#include <sys/time.h>
int clock_getres(clockid_t clk_id, struct timespec *res) {
if (clk_id < 0 || clk_id > 1) {
errno = EINVAL;
return -1;
}
res->tv_sec = 0;
res->tv_nsec = 1000;
return 0;
}
int clock_gettime(clockid_t clk_id, struct timespec *tp) {
if (clk_id < 0 || clk_id > 1) {
errno = EINVAL;

View File

@ -1,6 +1,5 @@
#include <unistd.h>
int getpgrp() {
/* XXX */
return getgid();
return getpgid(0);
}

View File

@ -1,3 +1,4 @@
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
@ -15,3 +16,10 @@ char * ttyname(int fd) {
return _tty_name;
}
int ttyname_r(int fd, char * buf, size_t buflen) {
if (!isatty(fd)) return ENOTTY;
if (buflen < 30) return ERANGE;
ioctl(fd, IOCTLTTYNAME, buf);
return 0;
}

View File

@ -1,4 +1,4 @@
#include <unistd.h>
#include <utime.h>
#include <errno.h>
int utime(const char *filename, const struct utimbuf *times) {

View File

@ -4,3 +4,14 @@ int wcscmp(const wchar_t *l, const wchar_t *r) {
for (; *l == *r && *l; l++, r++);
return *(unsigned int *)l - *(unsigned int *)r;
}
int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n) {
if (n == 0) return 0;
while (n-- && *s1 == *s2) {
if (!n || !*s1) break;
s1++;
s2++;
}
return (*s1) - (*s2);
}

95
libc/wchar/wcstol.c Normal file
View File

@ -0,0 +1,95 @@
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <wchar.h>
#include <errno.h>
static int is_valid(int base, wchar_t c) {
if (c < '0') return 0;
if (base <= 10) {
return c < ('0' + base);
}
if (c >= 'a' && c < 'a' + (base - 10)) return 1;
if (c >= 'A' && c < 'A' + (base - 10)) return 1;
if (c >= '0' && c <= '9') return 1;
return 0;
}
static int convert_digit(wchar_t c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'a' && c <= 'z') {
return c - 'a' + 0xa;
}
if (c >= 'A' && c <= 'Z') {
return c - 'A' + 0xa;
}
return 0;
}
#define strtox(max, type) \
if (base < 0 || base == 1 || base > 36) { \
errno = EINVAL; \
return max; \
} \
while (*nptr && isspace(*nptr)) nptr++; \
int sign = 1; \
if (*nptr == '-') { \
sign = -1; \
nptr++; \
} else if (*nptr == '+') { \
nptr++; \
} \
if (base == 16) { \
if (*nptr == '0') { \
nptr++; \
if (*nptr == 'x') { \
nptr++; \
} \
} \
} \
if (base == 0) { \
if (*nptr == '0') { \
base = 8; \
nptr++; \
if (*nptr == 'x') { \
base = 16; \
nptr++; \
} \
} else { \
base = 10; \
} \
} \
type val = 0; \
while (is_valid(base, *nptr)) { \
val *= base; \
val += convert_digit(*nptr); \
nptr++; \
} \
if (endptr) { \
*endptr = (wchar_t *)nptr; \
} \
if (sign == -1) { \
return -val; \
} else { \
return val; \
}
unsigned long int wcstoul(const wchar_t *nptr, wchar_t **endptr, int base) {
strtox(ULONG_MAX, unsigned long int);
}
unsigned long long int wcstoull(const char *nptr, wchar_t **endptr, int base) {
strtox(ULLONG_MAX, unsigned long int);
}
long int wcstol(const wchar_t *nptr, wchar_t **endptr, int base) {
strtox(LONG_MAX, unsigned long int);
}
long long int wcstoll(const wchar_t *nptr, wchar_t **endptr, int base) {
strtox(LLONG_MAX, unsigned long long int);
}