Skip to content

Commit

Permalink
libc: cleanup, some missing things
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed Sep 18, 2021
1 parent 33848f8 commit 8bca4ee
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 9 deletions.
1 change: 1 addition & 0 deletions base/usr/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 4 additions & 6 deletions base/usr/include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
15 changes: 15 additions & 0 deletions base/usr/include/utime.h
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions base/usr/include/wchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions libc/time/clock_gettime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions libc/unistd/getpgrp.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <unistd.h>

int getpgrp() {
/* XXX */
return getgid();
return getpgid(0);
}
8 changes: 8 additions & 0 deletions libc/unistd/ttyname.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion libc/unistd/utime.c → libc/utime/utime.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <unistd.h>
#include <utime.h>
#include <errno.h>

int utime(const char *filename, const struct utimbuf *times) {
Expand Down
11 changes: 11 additions & 0 deletions libc/wchar/wcscmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 95 additions & 0 deletions libc/wchar/wcstol.c
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 8bca4ee

Please sign in to comment.