Skip to content

Commit

Permalink
Merge pull request #62 from afxgroup/beta6
Browse files Browse the repository at this point in the history
Beta6
  • Loading branch information
afxgroup authored Aug 17, 2022
2 parents 33f863d + cb320a6 commit a9583f1
Show file tree
Hide file tree
Showing 28 changed files with 809 additions and 655 deletions.
1 change: 1 addition & 0 deletions libc.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ C_DIRENT := \
dirent/dirfd.o \
dirent/fdopendir.o \
dirent/opendir.o \
dirent/scandir.o \
dirent/readdir.o \
dirent/readdir_r.o \
dirent/readdir64_r.o \
Expand Down
58 changes: 58 additions & 0 deletions library/dirent/scandir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* $Id: dirent_scandir.c,v 1.0 2022-08-17 12:04:22 clib2devs Exp $
*/

#ifndef _DIRENT_HEADERS_H
#include "dirent_headers.h"
#endif /* _DIRENT_HEADERS_H */

#ifndef _STDLIB_MEMORY_H
#include "stdlib_memory.h"
#endif /* _STDLIB_MEMORY_H */

int scandir(const char *path, struct dirent ***res,
int (*sel)(const struct dirent *),
int (*cmp)(const struct dirent **, const struct dirent **)) {
DIR *d = opendir(path);
struct dirent *de, **names = 0, **tmp;
size_t cnt = 0, len = 0;
int old_errno = errno;

if (!d) {
__set_errno(EACCES);
return -1;
}

while ((errno = 0), (de = readdir(d))) {
if (sel && !sel(de))
continue;
if (cnt >= len) {
len = 2 * len + 1;
if (len > SIZE_MAX / sizeof *names)
break;
tmp = realloc(names, len * sizeof *names);
if (!tmp)
break;
names = tmp;
}
names[cnt] = malloc(de->d_reclen);
if (!names[cnt])
break;
memcpy(names[cnt++], de, de->d_reclen);
}

closedir(d);

if (errno) {
if (names)
while (cnt-- > 0) free(names[cnt]);
free(names);
return -1;
}
__set_errno(old_errno);

if (cmp)
qsort(names, cnt, sizeof *names, (int (*)(const void *, const void *)) cmp);
*res = names;
return cnt;
}
2 changes: 1 addition & 1 deletion library/fcntl/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ open(const char *path_name, int open_flag, ... /* mode_t mode */) {
fd = __fd[fd_slot_number];

if (is_directory || FLAG_IS_SET(open_flag, O_PATH))
__initialize_fd(fd, __fd_hook_entry, dir_lock, 0, fd_lock);
__initialize_fd(fd, __fd_hook_entry, dir_lock, 0, fd_lock); // TODO - Create a new dir hook
else
__initialize_fd(fd, __fd_hook_entry, handle, 0, fd_lock);

Expand Down
9 changes: 5 additions & 4 deletions library/include/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ struct dirent {
};

extern DIR *opendir(const char * path_name);
extern struct dirent * readdir(DIR * directory_pointer);
extern struct dirent *readdir(DIR * directory_pointer);
extern void rewinddir(DIR * directory_pointer);
extern int closedir(DIR * directory_pointer);
extern int closedir(DIR * directory_pointer);
extern DIR *fdopendir(int);
extern int alphasort(const struct dirent **a, const struct dirent **b);
extern int dirfd(DIR *dirp);
extern int alphasort(const struct dirent **a, const struct dirent **b);
extern int dirfd(DIR *dirp);
extern int scandir(const char *, struct dirent ***, int (*)(const struct dirent *), int (*)(const struct dirent **, const struct dirent **));

extern int readdir_r(DIR *dir, struct dirent *buf, struct dirent **result);
extern int readdir64_r(DIR *dir, struct dirent *buf, struct dirent **result);
Expand Down
6 changes: 3 additions & 3 deletions library/include/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,9 @@
# define __USE_EXTERN_INLINES 1
#endif

#define weak __attribute__((__weak__))
#define hidden __attribute__((__visibility__("hidden")))
#define weak_alias(old, new) \
#define WEAK __attribute__((__weak__))
#define HIDDEN __attribute__((__visibility__("hidden")))
#define WEAK_ALIAS(old, new) \
extern __typeof(old) new __attribute__((__weak__, __alias__(#old)))


Expand Down
82 changes: 41 additions & 41 deletions library/include/termios.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,57 +51,57 @@ struct termios

/* c_iflag */

#define IGNBRK 0x00001 /* Ignore VINTR */
#define BRKINT 0x00002 /* Unimpl. */
#define IGNPAR 0x00004 /* Unimpl. */
#define IGNBRK 0x00001 /* Ignore VINTR */
#define BRKINT 0x00002 /* Unimpl. */
#define IGNPAR 0x00004 /* Unimpl. */
#define IMAXBEL 0x00008 /* Unimpl. */
#define INPCK 0x00010 /* Unimpl. */
#define ISTRIP 0x00020 /* 7-bit data (strip high bit) */
#define INLCR 0x00040 /* Map NL->CR */
#define IGNCR 0x00080 /* Map CR->nothing */
#define ICRNL 0x00100 /* Map CR->NL */
#define IXON 0x00400 /* Unimpl. Enable XON/XOFF for output. */
#define IXOFF 0x01000 /* Unimpl. Enable XON/XOFF for input. */
#define IUCLC 0x04000 /* Unimpl. */
#define IXANY 0x08000 /* Unimpl. */
#define PARMRK 0x10000 /* Unimpl. */
#define IUTF8 0x20000 /* Unimpl. */
#define INPCK 0x00010 /* Unimpl. */
#define ISTRIP 0x00020 /* 7-bit data (strip high bit) */
#define INLCR 0x00040 /* Map NL->CR */
#define IGNCR 0x00080 /* Map CR->nothing */
#define ICRNL 0x00100 /* Map CR->NL */
#define IXON 0x00400 /* Unimpl. Enable XON/XOFF for output. */
#define IXOFF 0x01000 /* Unimpl. Enable XON/XOFF for input. */
#define IUCLC 0x04000 /* Unimpl. */
#define IXANY 0x08000 /* Unimpl. */
#define PARMRK 0x10000 /* Unimpl. */
#define IUTF8 0x20000 /* Unimpl. */

/* c_oflag */

#define OPOST (1 << 0L) /* Enable output processing. */
#define ONLCR (1 << 1L) /* Map NL->CR+NL */
#define OCRNL (1 << 2L) /* Map CR->NL */
#define ONOCR (1 << 3L) /* Map CR->nothing, but only in column 0. */
#define ONLRET (1 << 4L) /* Map CR->nothing */
#define OPOST (1 << 0L) /* Enable output processing. */
#define ONLCR (1 << 1L) /* Map NL->CR+NL */
#define OCRNL (1 << 2L) /* Map CR->NL */
#define ONOCR (1 << 3L) /* Map CR->nothing, but only in column 0. */
#define ONLRET (1 << 4L) /* Map CR->nothing */

/* c_cflag */

#define CSIZE (0x07) /* Bit-width mask. */
#define CS5 (0x01) /* 5-bits */
#define CS6 (0x02) /* 6-bits */
#define CS7 (0x03) /* 7-bits */
#define CS8 (0x04) /* 8-bits */
#define CSTOPB (1 << 3L) /* Use 2 stop bits. */
#define CREAD (1 << 4L) /* Enable reading/receiving. */
#define PARENB (1 << 5L) /* Enable parity generation/checking. */
#define PARODD (1 << 6L) /* Parity is odd. */
#define HUPCL (1 << 7L) /* Hangup on close (when the device is closed). */
#define CLOCAL (1 << 8L) /* Ignore modem control lines (i.e. a null-modem) */
#define CSIZE (0x07) /* Bit-width mask. */
#define CS5 (0x01) /* 5-bits */
#define CS6 (0x02) /* 6-bits */
#define CS7 (0x03) /* 7-bits */
#define CS8 (0x04) /* 8-bits */
#define CSTOPB (1 << 3L) /* Use 2 stop bits. */
#define CREAD (1 << 4L) /* Enable reading/receiving. */
#define PARENB (1 << 5L) /* Enable parity generation/checking. */
#define PARODD (1 << 6L) /* Parity is odd. */
#define HUPCL (1 << 7L) /* Hangup on close (when the device is closed). */
#define CLOCAL (1 << 8L) /* Ignore modem control lines (i.e. a null-modem) */

/* c_lflag */

#define ISIG 0x0001
#define ICANON 0x0002
#define ECHO 0x0004
#define ECHOE 0x0008
#define ECHOK 0x0010
#define ECHONL 0x0020
#define NOFLSH 0x0040
#define TOSTOP 0x0080
#define IEXTEN 0x0100
#define FLUSHO 0x0200
#define ECHOKE 0x0400
#define ISIG 0x0001
#define ICANON 0x0002
#define ECHO 0x0004
#define ECHOE 0x0008
#define ECHOK 0x0010
#define ECHONL 0x0020
#define NOFLSH 0x0040
#define TOSTOP 0x0080
#define IEXTEN 0x0100
#define FLUSHO 0x0200
#define ECHOKE 0x0400
#define ECHOCTL 0x0800

/* Speeds */
Expand Down
39 changes: 34 additions & 5 deletions library/libc_init_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
#include <proto/elf.h>
#include <fenv.h>

static APTR
hook_function(struct Hook *hook, APTR userdata, struct Process *process) {
uint32 pid = (uint32) userdata;
(void) (hook);

if (process->pr_ProcessID == pid) {
return process;
}

return 0;
}

/* random table */
static uint32_t _random_init[] = {
0x00000000, 0x5851f42d, 0xc0b18ccf, 0xcbb5f646,
Expand All @@ -45,9 +57,7 @@ static uint32_t _random_init[] = {
extern struct Library *__ElfBase;
extern struct ElfIFace *__IElf;

struct _clib2 NOCOMMON
*
__global_clib2;
struct _clib2 NOCOMMON* __global_clib2;

void
reent_init() {
Expand Down Expand Up @@ -213,8 +223,27 @@ reent_init() {

/* Remove timer tasks */
if (__global_clib2->tmr_real_task != NULL) {
Signal((struct Task *) __global_clib2->tmr_real_task, SIGBREAKF_CTRL_F);
WaitForChildExit(__global_clib2->tmr_real_task->pr_ProcessID);
struct Hook h = {{NULL, NULL}, (HOOKFUNC) hook_function, NULL, NULL};
int32 pid, process;

/* Block SIGALRM signal from raise */
sigblock(SIGALRM);
/* Get itimer process ID */
pid = __global_clib2->tmr_real_task->pr_ProcessID;

Forbid();
/* Scan for process */
process = ProcessScan(&h, (CONST_APTR) pid, 0);
/* If we find the process send a signal to kill it */
while (process > 0) {
/* Send a SIGBREAKF_CTRL_F signal until the timer task return to Wait state
* and can get the signal */
Signal((struct Task *) __global_clib2->tmr_real_task, SIGBREAKF_CTRL_F);
process = ProcessScan(&h, (CONST_APTR) pid, 0);
usleep(100);
}
Permit();
WaitForChildExit(pid);
__global_clib2->tmr_real_task = NULL;
}

Expand Down
36 changes: 0 additions & 36 deletions library/posix/checkabort.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,11 @@
#include "stdio_headers.h"
#endif /* _STDIO_HEADERS_H */

static APTR
hook_function(struct Hook *hook, APTR userdata, struct Process *process) {
uint32 pid = (uint32) userdata;
(void) (hook);

if (process->pr_ProcessID == pid) {
return process;
}

return 0;
}

void
__check_abort(void) {
ENTER();

if (__check_abort_enabled && FLAG_IS_SET(SetSignal(0, __break_signal_mask), __break_signal_mask)) {
if (__global_clib2->tmr_real_task != NULL) {
struct Hook h = {{NULL, NULL}, (HOOKFUNC) hook_function, NULL, NULL};
int32 pid, process;

/* Block SIGALRM signal from raise */
sigblock(SIGALRM);
/* Get itimer process ID */
pid = __global_clib2->tmr_real_task->pr_ProcessID;

Forbid();
/* Scan for process */
process = ProcessScan(&h, (CONST_APTR) pid, 0);
/* If we find the process send a signal to kill it */
while (process > 0) {
/* Send a SIGBREAKF_CTRL_F signal until the timer task return to Wait state
* and can get the signal */
Signal((struct Task *) __global_clib2->tmr_real_task, SIGBREAKF_CTRL_F);
process = ProcessScan(&h, (CONST_APTR) pid, 0);
usleep(100);
}
Permit();
WaitForChildExit(pid);
__global_clib2->tmr_real_task = NULL;
}
raise(SIGINT);
}

Expand Down
55 changes: 49 additions & 6 deletions library/posix/raise.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
#include "signal_headers.h"
#endif /* _SIGNAL_HEADERS_H */

#ifndef _STDIO_HEADERS_H
#include "stdio_headers.h"
#endif /* _STDIO_HEADERS_H */

static APTR
hook_function(struct Hook *hook, APTR userdata, struct Process *process) {
uint32 pid = (uint32) userdata;
(void) (hook);

if (process->pr_ProcessID == pid) {
return process;
}

return 0;
}

/* This table holds pointers to all signal handlers configured at a time. */
signal_handler_t NOCOMMON __signal_handler_table[NSIG] = {
SIG_DFL, /* SIGHUP */
Expand Down Expand Up @@ -82,6 +98,32 @@ raise(int sig) {
SHOWMSG("this is the default handler");

if (sig == SIGINT || sig == SIGTERM) {
/* Check ig we have timer terminal running. If so let's kill it */
if (__global_clib2->tmr_real_task != NULL) {
struct Hook h = {{NULL, NULL}, (HOOKFUNC) hook_function, NULL, NULL};
int32 pid, process;

/* Block SIGALRM signal from raise */
sigblock(SIGALRM);
/* Get itimer process ID */
pid = __global_clib2->tmr_real_task->pr_ProcessID;

Forbid();
/* Scan for process */
process = ProcessScan(&h, (CONST_APTR) pid, 0);
/* If we find the process send a signal to kill it */
while (process > 0) {
/* Send a SIGBREAKF_CTRL_F signal until the timer task return to Wait state
* and can get the signal */
Signal((struct Task *) __global_clib2->tmr_real_task, SIGBREAKF_CTRL_F);
process = ProcessScan(&h, (CONST_APTR) pid, 0);
usleep(100);
}
Permit();
WaitForChildExit(pid);
__global_clib2->tmr_real_task = NULL;
}

char break_string[80];

/* Turn off ^C checking for good. */
Expand All @@ -95,12 +137,7 @@ raise(int sig) {
SHOWMSG("bye, bye...");
}
/* If we have a SIGALRM without associated handler don't call abort but exit directly */
else if (sig != SIGALRM) {
/* Drop straight into abort(), which might call signal()
again but is otherwise guaranteed to eventually
land us in _exit(). */
abort();
} else if (sig == SIGALRM) {
if (sig == SIGALRM) {
__print_termination_message("Alarm Clock");

/* Block SIGALRM signal from raise again */
Expand All @@ -109,6 +146,12 @@ raise(int sig) {
/* Since we got a signal we interrrupt every sleep function like nanosleep */
Signal((struct Task *) __global_clib2->self, SIGBREAKF_CTRL_E);
}
else {
/* Drop straight into abort(), which might call signal()
again but is otherwise guaranteed to eventually
land us in _exit(). */
abort();
}
}
else if (handler == SIG_ERR) {
__set_errno(EINVAL);
Expand Down
Loading

0 comments on commit a9583f1

Please sign in to comment.