initial check-in, version 0.5.0

This commit is contained in:
Rich Felker
2011-02-12 00:22:29 -05:00
commit 0b44a0315b
1021 changed files with 45711 additions and 0 deletions

35
src/conf/fpathconf.c Normal file
View File

@ -0,0 +1,35 @@
#include <unistd.h>
#include <limits.h>
#include <errno.h>
long fpathconf(int fd, int name)
{
static const short values[] = {
[_PC_LINK_MAX] = _POSIX_LINK_MAX,
[_PC_MAX_CANON] = _POSIX_MAX_CANON,
[_PC_MAX_INPUT] = _POSIX_MAX_INPUT,
[_PC_NAME_MAX] = NAME_MAX,
[_PC_PATH_MAX] = PATH_MAX,
[_PC_PIPE_BUF] = PIPE_BUF,
[_PC_CHOWN_RESTRICTED] = 1,
[_PC_NO_TRUNC] = 1,
[_PC_VDISABLE] = 0,
[_PC_SYNC_IO] = 0,
[_PC_ASYNC_IO] = 0,
[_PC_PRIO_IO] = 0,
[_PC_SOCK_MAXBUF] = -1,
[_PC_FILESIZEBITS] = sizeof(off_t),
[_PC_REC_INCR_XFER_SIZE] = PAGE_SIZE,
[_PC_REC_MAX_XFER_SIZE] = PAGE_SIZE,
[_PC_REC_MIN_XFER_SIZE] = PAGE_SIZE,
[_PC_REC_XFER_ALIGN] = PAGE_SIZE,
[_PC_ALLOC_SIZE_MIN] = PAGE_SIZE,
[_PC_SYMLINK_MAX] = SYMLINK_MAX,
[_PC_2_SYMLINKS] = 1
};
if (name > sizeof(values)/sizeof(values[0])) {
errno = EINVAL;
return -1;
}
return values[name];
}

6
src/conf/pathconf.c Normal file
View File

@ -0,0 +1,6 @@
#include <unistd.h>
long pathconf(const char *path, int name)
{
return fpathconf(-1, name);
}

222
src/conf/sysconf.c Normal file
View File

@ -0,0 +1,222 @@
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#define VER (-2)
#define OFLOW (-3)
long sysconf(int name)
{
static const short values[] = {
[_SC_ARG_MAX] = OFLOW,
[_SC_CHILD_MAX] = -1,
[_SC_CLK_TCK] = 100,
[_SC_NGROUPS_MAX] = 32,
[_SC_OPEN_MAX] = 1024,
[_SC_STREAM_MAX] = -1,
[_SC_TZNAME_MAX] = TZNAME_MAX,
[_SC_JOB_CONTROL] = 1,
[_SC_SAVED_IDS] = 1,
[_SC_REALTIME_SIGNALS] = 1,
[_SC_PRIORITY_SCHEDULING] = -1,
[_SC_TIMERS] = VER,
[_SC_ASYNCHRONOUS_IO] = VER,
[_SC_PRIORITIZED_IO] = -1,
[_SC_SYNCHRONIZED_IO] = -1,
[_SC_FSYNC] = -1,
[_SC_MAPPED_FILES] = VER,
[_SC_MEMLOCK] = VER,
[_SC_MEMLOCK_RANGE] = VER,
[_SC_MEMORY_PROTECTION] = VER,
[_SC_MESSAGE_PASSING] = -1,
[_SC_SEMAPHORES] = VER,
[_SC_SHARED_MEMORY_OBJECTS] = -1,
[_SC_AIO_LISTIO_MAX] = -1,
[_SC_AIO_MAX] = -1,
[_SC_AIO_PRIO_DELTA_MAX] = 0, /* ?? */
[_SC_DELAYTIMER_MAX] = _POSIX_DELAYTIMER_MAX,
[_SC_MQ_OPEN_MAX] = -1,
[_SC_MQ_PRIO_MAX] = _POSIX_MQ_PRIO_MAX,
[_SC_VERSION] = VER,
[_SC_PAGE_SIZE] = PAGE_SIZE,
[_SC_RTSIG_MAX] = 63, /* ?? */
[_SC_SEM_NSEMS_MAX] = _POSIX_SEM_NSEMS_MAX,
[_SC_SEM_VALUE_MAX] = _POSIX_SEM_VALUE_MAX,
[_SC_SIGQUEUE_MAX] = -1,
[_SC_TIMER_MAX] = -1,
[_SC_BC_BASE_MAX] = _POSIX2_BC_BASE_MAX,
[_SC_BC_DIM_MAX] = _POSIX2_BC_DIM_MAX,
[_SC_BC_SCALE_MAX] = _POSIX2_BC_SCALE_MAX,
[_SC_BC_STRING_MAX] = _POSIX2_BC_STRING_MAX,
[_SC_COLL_WEIGHTS_MAX] = COLL_WEIGHTS_MAX,
[_SC_EQUIV_CLASS_MAX] = -1, /* ?? */
[_SC_EXPR_NEST_MAX] = -1,
[_SC_LINE_MAX] = -1,
[_SC_RE_DUP_MAX] = RE_DUP_MAX,
[_SC_CHARCLASS_NAME_MAX] = -1, /* ?? */
[_SC_2_VERSION] = VER,
[_SC_2_C_BIND] = VER,
[_SC_2_C_DEV] = -1,
[_SC_2_FORT_DEV] = -1,
[_SC_2_FORT_RUN] = -1,
[_SC_2_SW_DEV] = -1,
[_SC_2_LOCALEDEF] = -1,
[_SC_PII] = -1, /* ????????? */
[_SC_PII_XTI] = -1,
[_SC_PII_SOCKET] = -1,
[_SC_PII_INTERNET] = -1,
[_SC_PII_OSI] = -1,
[_SC_POLL] = 1,
[_SC_SELECT] = 1,
[_SC_IOV_MAX] = IOV_MAX,
[_SC_PII_INTERNET_STREAM] = -1,
[_SC_PII_INTERNET_DGRAM] = -1,
[_SC_PII_OSI_COTS] = -1,
[_SC_PII_OSI_CLTS] = -1,
[_SC_PII_OSI_M] = -1,
[_SC_T_IOV_MAX] = -1,
[_SC_THREADS] = VER,
[_SC_THREAD_SAFE_FUNCTIONS] = VER,
[_SC_GETGR_R_SIZE_MAX] = -1,
[_SC_GETPW_R_SIZE_MAX] = -1,
[_SC_LOGIN_NAME_MAX] = 256,
[_SC_TTY_NAME_MAX] = TTY_NAME_MAX,
[_SC_THREAD_DESTRUCTOR_ITERATIONS] = _POSIX_THREAD_DESTRUCTOR_ITERATIONS,
[_SC_THREAD_KEYS_MAX] = -1,
[_SC_THREAD_STACK_MIN] = 2*PAGE_SIZE,
[_SC_THREAD_THREADS_MAX] = -1,
[_SC_THREAD_ATTR_STACKADDR] = -1,
[_SC_THREAD_ATTR_STACKSIZE] = VER,
[_SC_THREAD_PRIORITY_SCHEDULING] = -1,
[_SC_THREAD_PRIO_INHERIT] = -1,
[_SC_THREAD_PRIO_PROTECT] = -1,
[_SC_THREAD_PROCESS_SHARED] = VER,
[_SC_NPROCESSORS_CONF] = -1,
[_SC_NPROCESSORS_ONLN] = -1,
[_SC_PHYS_PAGES] = -1,
[_SC_AVPHYS_PAGES] = -1,
[_SC_ATEXIT_MAX] = -1,
[_SC_PASS_MAX] = -1,
[_SC_XOPEN_VERSION] = _XOPEN_VERSION,
[_SC_XOPEN_XCU_VERSION] = _XOPEN_VERSION,
[_SC_XOPEN_UNIX] = -1,
[_SC_XOPEN_CRYPT] = -1,
[_SC_XOPEN_ENH_I18N] = 1,
[_SC_XOPEN_SHM] = 1,
[_SC_2_CHAR_TERM] = -1,
[_SC_2_C_VERSION] = -1,
[_SC_2_UPE] = -1,
[_SC_XOPEN_XPG2] = -1,
[_SC_XOPEN_XPG3] = -1,
[_SC_XOPEN_XPG4] = -1,
[_SC_CHAR_BIT] = -1,
[_SC_CHAR_MAX] = -1,
[_SC_CHAR_MIN] = -1,
[_SC_INT_MAX] = -1,
[_SC_INT_MIN] = -1,
[_SC_LONG_BIT] = -1,
[_SC_WORD_BIT] = -1,
[_SC_MB_LEN_MAX] = -1,
[_SC_NZERO] = NZERO,
[_SC_SSIZE_MAX] = -1,
[_SC_SCHAR_MAX] = -1,
[_SC_SCHAR_MIN] = -1,
[_SC_SHRT_MAX] = -1,
[_SC_SHRT_MIN] = -1,
[_SC_UCHAR_MAX] = -1,
[_SC_UINT_MAX] = -1,
[_SC_ULONG_MAX] = -1,
[_SC_USHRT_MAX] = -1,
[_SC_NL_ARGMAX] = -1,
[_SC_NL_LANGMAX] = -1,
[_SC_NL_MSGMAX] = -1,
[_SC_NL_NMAX] = -1,
[_SC_NL_SETMAX] = -1,
[_SC_NL_TEXTMAX] = -1,
[_SC_XBS5_ILP32_OFF32] = -1,
[_SC_XBS5_ILP32_OFFBIG] = 2*(sizeof(long)==4)-1,
[_SC_XBS5_LP64_OFF64] = 2*(sizeof(long)==8)-1,
[_SC_XBS5_LPBIG_OFFBIG] = 2*(sizeof(long)>=8)-1,
[_SC_XOPEN_LEGACY] = -1,
[_SC_XOPEN_REALTIME] = -1,
[_SC_XOPEN_REALTIME_THREADS] = -1,
[_SC_ADVISORY_INFO] = -1,
[_SC_BARRIERS] = VER,
[_SC_BASE] = -1,
[_SC_C_LANG_SUPPORT] = -1,
[_SC_C_LANG_SUPPORT_R] = -1,
[_SC_CLOCK_SELECTION] = VER,
[_SC_CPUTIME] = VER,
[_SC_THREAD_CPUTIME] = -1,
[_SC_DEVICE_IO] = -1,
[_SC_DEVICE_SPECIFIC] = -1,
[_SC_DEVICE_SPECIFIC_R] = -1,
[_SC_FD_MGMT] = -1,
[_SC_FIFO] = -1,
[_SC_PIPE] = -1,
[_SC_FILE_ATTRIBUTES] = -1,
[_SC_FILE_LOCKING] = -1,
[_SC_FILE_SYSTEM] = -1,
[_SC_MONOTONIC_CLOCK] = VER,
[_SC_MULTI_PROCESS] = -1,
[_SC_SINGLE_PROCESS] = -1,
[_SC_NETWORKING] = -1,
[_SC_READER_WRITER_LOCKS] = VER,
[_SC_SPIN_LOCKS] = VER,
[_SC_REGEXP] = 1,
[_SC_REGEX_VERSION] = -1,
[_SC_SHELL] = 1,
[_SC_SIGNALS] = -1,
[_SC_SPAWN] = -1,
[_SC_SPORADIC_SERVER] = -1,
[_SC_THREAD_SPORADIC_SERVER] = -1,
[_SC_SYSTEM_DATABASE] = -1,
[_SC_SYSTEM_DATABASE_R] = -1,
[_SC_TIMEOUTS] = VER,
[_SC_TYPED_MEMORY_OBJECTS] = -1,
[_SC_USER_GROUPS] = -1,
[_SC_USER_GROUPS_R] = -1,
[_SC_2_PBS] = -1,
[_SC_2_PBS_ACCOUNTING] = -1,
[_SC_2_PBS_LOCATE] = -1,
[_SC_2_PBS_MESSAGE] = -1,
[_SC_2_PBS_TRACK] = -1,
[_SC_SYMLOOP_MAX] = SYMLOOP_MAX,
[_SC_STREAMS] = 0,
[_SC_2_PBS_CHECKPOINT] = -1,
[_SC_V6_ILP32_OFF32] = -1,
[_SC_V6_ILP32_OFFBIG] = 2*(sizeof(long)==4)-1,
[_SC_V6_LP64_OFF64] = 2*(sizeof(long)==8)-1,
[_SC_V6_LPBIG_OFFBIG] = 2*(sizeof(long)>=8)-1,
[_SC_HOST_NAME_MAX] = HOST_NAME_MAX,
[_SC_TRACE] = -1,
[_SC_TRACE_EVENT_FILTER] = -1,
[_SC_TRACE_INHERIT] = -1,
[_SC_TRACE_LOG] = -1,
[_SC_IPV6] = VER,
[_SC_RAW_SOCKETS] = VER,
[_SC_V7_ILP32_OFF32] = -1,
[_SC_V7_ILP32_OFFBIG] = 2*(sizeof(long)==4)-1,
[_SC_V7_LP64_OFF64] = 2*(sizeof(long)==8)-1,
[_SC_V7_LPBIG_OFFBIG] = 2*(sizeof(long)>=8)-1,
[_SC_SS_REPL_MAX] = -1,
[_SC_TRACE_EVENT_NAME_MAX] = -1,
[_SC_TRACE_NAME_MAX] = -1,
[_SC_TRACE_SYS_MAX] = -1,
[_SC_TRACE_USER_EVENT_MAX] = -1,
[_SC_XOPEN_STREAMS] = 0,
[_SC_THREAD_ROBUST_PRIO_INHERIT] = -1,
[_SC_THREAD_ROBUST_PRIO_PROTECT] = -1,
};
if (name > sizeof(values)/sizeof(values[0])) {
errno = EINVAL;
return -1;
} else if (values[name] == VER) {
return _POSIX_VERSION;
} else if (values[name] == OFLOW) {
return ARG_MAX;
} else {
return values[name];
}
}

View File

@ -0,0 +1,6 @@
#include <stdlib.h>
size_t __ctype_get_mb_cur_max()
{
return 4;
}

6
src/ctype/isalnum.c Normal file
View File

@ -0,0 +1,6 @@
#include <ctype.h>
int isalnum(int c)
{
return isalpha(c) || isdigit(c);
}

7
src/ctype/isalpha.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isalpha
int isalpha(int c)
{
return ((unsigned)c|32)-'a' < 26;
}

6
src/ctype/isascii.c Normal file
View File

@ -0,0 +1,6 @@
#include <ctype.h>
int isascii(int c)
{
return !(c&~0x7f);
}

6
src/ctype/isblank.c Normal file
View File

@ -0,0 +1,6 @@
#include <ctype.h>
int isblank(int c)
{
return (c == ' ' || c == '\t');
}

6
src/ctype/iscntrl.c Normal file
View File

@ -0,0 +1,6 @@
#include <ctype.h>
int iscntrl(int c)
{
return (unsigned)c < 0x20 || c == 0x7f;
}

7
src/ctype/isdigit.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isdigit
int isdigit(int c)
{
return (unsigned)c-'0' < 10;
}

4
src/ctype/isgraph.c Normal file
View File

@ -0,0 +1,4 @@
int isgraph(int c)
{
return (unsigned)c-0x21 < 0x5e;
}

7
src/ctype/islower.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef islower
int islower(int c)
{
return (unsigned)c-'a' < 26;
}

4
src/ctype/isprint.c Normal file
View File

@ -0,0 +1,4 @@
int isprint(int c)
{
return (unsigned)c-0x20 < 0x5f;
}

6
src/ctype/ispunct.c Normal file
View File

@ -0,0 +1,6 @@
#include <ctype.h>
int ispunct(int c)
{
return isgraph(c) && !isalnum(c);
}

6
src/ctype/isspace.c Normal file
View File

@ -0,0 +1,6 @@
#include <ctype.h>
int isspace(int c)
{
return c == ' ' || (unsigned)c-'\t' < 5;
}

7
src/ctype/isupper.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
#undef isupper
int isupper(int c)
{
return (unsigned)c-'A' < 26;
}

9
src/ctype/iswalnum.c Normal file
View File

@ -0,0 +1,9 @@
#include <wchar.h>
#include <wctype.h>
#undef iswalnum
int iswalnum(wint_t wc)
{
return (unsigned)wc-'0' < 10 || iswalpha(wc);
}

6
src/ctype/iswalpha.c Normal file
View File

@ -0,0 +1,6 @@
#include <wctype.h>
int iswalpha(wint_t wc)
{
return (32U|wc)-'a'<26;
}

8
src/ctype/iswblank.c Normal file
View File

@ -0,0 +1,8 @@
#include <wchar.h>
#include <wctype.h>
#include <ctype.h>
int iswblank(wint_t wc)
{
return isblank(wc);
}

10
src/ctype/iswcntrl.c Normal file
View File

@ -0,0 +1,10 @@
#include <wchar.h>
#include <wctype.h>
int iswcntrl(wint_t wc)
{
return (unsigned)wc < 32
|| (unsigned)(wc-0x7f) < 33
|| (unsigned)(wc-0x2028) < 2
|| (unsigned)(wc-0xfff9) < 3;
}

63
src/ctype/iswctype.c Normal file
View File

@ -0,0 +1,63 @@
#include <wchar.h>
#include <wctype.h>
#include <string.h>
#define WCTYPE_ALNUM 1
#define WCTYPE_ALPHA 2
#define WCTYPE_BLANK 3
#define WCTYPE_CNTRL 4
#define WCTYPE_DIGIT 5
#define WCTYPE_GRAPH 6
#define WCTYPE_LOWER 7
#define WCTYPE_PRINT 8
#define WCTYPE_PUNCT 9
#define WCTYPE_SPACE 10
#define WCTYPE_UPPER 11
#define WCTYPE_XDIGIT 12
int iswctype(wint_t wc, wctype_t type)
{
switch (type) {
case WCTYPE_ALNUM:
return iswalnum(wc);
case WCTYPE_ALPHA:
return iswalpha(wc);
case WCTYPE_BLANK:
return iswblank(wc);
case WCTYPE_CNTRL:
return iswcntrl(wc);
case WCTYPE_DIGIT:
return iswdigit(wc);
case WCTYPE_GRAPH:
return iswgraph(wc);
case WCTYPE_LOWER:
return iswlower(wc);
case WCTYPE_PRINT:
return iswprint(wc);
case WCTYPE_PUNCT:
return iswpunct(wc);
case WCTYPE_SPACE:
return iswspace(wc);
case WCTYPE_UPPER:
return iswupper(wc);
case WCTYPE_XDIGIT:
return iswxdigit(wc);
}
return 0;
}
wctype_t wctype(const char *s)
{
int i;
const char *p;
/* order must match! */
static const char names[] =
"alnum\0" "alpha\0" "blank\0"
"cntrl\0" "digit\0" "graph\0"
"lower\0" "print\0" "punct\0"
"space\0" "upper\0" "xdigit";
for (i=1, p=names; *p; i++, p+=6)
if (*s == *p && !strcmp(s, p))
return i;
return 0;
}

9
src/ctype/iswdigit.c Normal file
View File

@ -0,0 +1,9 @@
#include <wchar.h>
#include <wctype.h>
#undef iswdigit
int iswdigit(wint_t wc)
{
return (unsigned)wc-'0' < 10;
}

7
src/ctype/iswgraph.c Normal file
View File

@ -0,0 +1,7 @@
#include <wctype.h>
int iswgraph(wint_t wc)
{
/* ISO C defines this function as: */
return !iswspace(wc) && iswprint(wc);
}

6
src/ctype/iswlower.c Normal file
View File

@ -0,0 +1,6 @@
#include <wctype.h>
int iswlower(wint_t wc)
{
return towupper(wc) != wc;
}

10
src/ctype/iswprint.c Normal file
View File

@ -0,0 +1,10 @@
#include <wctype.h>
int iswprint(wint_t wc)
{
unsigned c = wc;
/* assume any non-control, non-illegal codepoint is printable */
if (c>0x10ffff || c-0xd800<0x800 || (c&0xfffe)==0xfffe || iswcntrl(c))
return 0;
return 1;
}

138
src/ctype/iswpunct.c Normal file
View File

@ -0,0 +1,138 @@
#include <wctype.h>
#include <inttypes.h>
/* The below data is derived from classes (P.|Sm) plus Pattern_Syntax */
#define R(a,b) { (b), (b)-(a) }
static const struct range {
uint32_t base:20;
uint32_t len:12;
} ranges[] = {
R(0x21, 0x2f),
R(0x3a, 0x40),
R(0x5b, 0x60),
R(0x7b, 0x7e),
R(0xa1, 0xa7),
R(0xa9, 0xa9),
R(0xab, 0xac),
R(0xae, 0xae),
R(0xb0, 0xb1),
R(0xb6, 0xb7),
R(0xbb, 0xbb),
R(0xbf, 0xbf),
R(0xd7, 0xd7),
R(0xf7, 0xf7),
R(0x37e, 0x37e),
R(0x387, 0x387),
R(0x3f6, 0x3f6),
R(0x55a, 0x55f),
R(0x589, 0x58a),
R(0x5be, 0x5be),
R(0x5c0, 0x5c0),
R(0x5c3, 0x5c3),
R(0x5c6, 0x5c6),
R(0x5f3, 0x5f4),
R(0x606, 0x60a),
R(0x60c, 0x60d),
R(0x61b, 0x61b),
R(0x61e, 0x61f),
R(0x66a, 0x66d),
R(0x6d4, 0x6d4),
R(0x700, 0x70d),
R(0x7f7, 0x7f9),
R(0x964, 0x965),
R(0x970, 0x970),
R(0xdf4, 0xdf4),
R(0xe4f, 0xe4f),
R(0xe5a, 0xe5b),
R(0xf04, 0xf12),
R(0xf3a, 0xf3d),
R(0xf85, 0xf85),
R(0xfd0, 0xfd4),
R(0x104a, 0x104f),
R(0x10fb, 0x10fb),
R(0x1361, 0x1368),
R(0x166d, 0x166e),
R(0x1680, 0x1680),
R(0x169b, 0x169c),
R(0x16eb, 0x16ed),
R(0x1735, 0x1736),
R(0x17d4, 0x17d6),
R(0x17d8, 0x17da),
R(0x1800, 0x180a),
R(0x180e, 0x180e),
R(0x1944, 0x1945),
R(0x19de, 0x19df),
R(0x1a1e, 0x1a1f),
R(0x1b5a, 0x1b60),
R(0x1c3b, 0x1c3f),
R(0x1c7e, 0x1c7f),
R(0x2010, 0x2027),
R(0x2030, 0x205e),
R(0x207a, 0x207e),
R(0x208a, 0x208e),
R(0x2140, 0x2144),
R(0x214b, 0x214b),
R(0x2190, 0x245f),
R(0x2500, 0x2775),
R(0x2794, 0x2bff),
R(0x2cf9, 0x2cfc),
R(0x2cfe, 0x2cff),
R(0x2e00, 0x2e7f),
R(0x3001, 0x3003),
R(0x3008, 0x3020),
R(0x3030, 0x3030),
R(0x303d, 0x303d),
R(0x30a0, 0x30a0),
R(0x30fb, 0x30fb),
R(0xa60d, 0xa60f),
R(0xa874, 0xa877),
R(0xa8ce, 0xa8cf),
R(0xa92e, 0xa92f),
R(0xa95f, 0xa95f),
R(0xfb29, 0xfb29),
R(0xfd3e, 0xfd3f),
R(0xfe10, 0xfe19),
R(0xfe30, 0xfe52),
R(0xfe54, 0xfe66),
R(0xfe68, 0xfe68),
R(0xfe6a, 0xfe6b),
R(0xff01, 0xff03),
R(0xff05, 0xff0f),
R(0xff1a, 0xff20),
R(0xff3b, 0xff3d),
R(0xff3f, 0xff3f),
R(0xff5b, 0xff65),
R(0xffe2, 0xffe2),
R(0xffe9, 0xffec),
R(0x10100, 0x10101),
R(0x1039f, 0x1039f),
R(0x103d0, 0x103d0),
R(0x1091f, 0x1091f),
R(0x1093f, 0x1093f),
R(0x10a50, 0x10a58),
R(0x12470, 0x12473),
R(0x1d6c1, 0x1d6c1),
R(0x1d6db, 0x1d6db),
R(0x1d6fb, 0x1d6fb),
R(0x1d715, 0x1d715),
R(0x1d735, 0x1d735),
R(0x1d74f, 0x1d74f),
R(0x1d76f, 0x1d76f),
R(0x1d789, 0x1d789),
R(0x1d7a9, 0x1d7a9),
R(0x1d7c3, 0x1d7c3),
};
int iswpunct(wint_t wc)
{
unsigned c = wc;
int a = 0;
int n = sizeof ranges / sizeof ranges[0];
do {
n >>= 1;
a += n+1 & (signed)(ranges[a+n].base-c)>>31;
} while (n);
return ranges[a].base-c <= ranges[a].len;
}

15
src/ctype/iswspace.c Normal file
View File

@ -0,0 +1,15 @@
#include <wchar.h>
#include <wctype.h>
#include <ctype.h>
int iswspace(wint_t wc)
{
static const wchar_t spaces[] = {
' ', '\t', '\n', '\r', 11, 12, 0x0085,
0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005,
0x2006, 0x2008, 0x2009, 0x200a, 0x200b,
0x2028, 0x2029, 0x2050, 0x3000, 0
};
if (wcschr(spaces, wc)) return 1;
return 0;
}

6
src/ctype/iswupper.c Normal file
View File

@ -0,0 +1,6 @@
#include <wctype.h>
int iswupper(wint_t wc)
{
return towlower(wc) != wc;
}

7
src/ctype/iswxdigit.c Normal file
View File

@ -0,0 +1,7 @@
#include <wchar.h>
#include <wctype.h>
int iswxdigit(wint_t wc)
{
return (unsigned)(wc-'0') < 10 || (unsigned)((wc|32)-'a') < 6;
}

6
src/ctype/isxdigit.c Normal file
View File

@ -0,0 +1,6 @@
#include <ctype.h>
int isxdigit(int c)
{
return isdigit(c) || ((unsigned)c|32)-'a' < 6;
}

7
src/ctype/toascii.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
/* nonsense function that should NEVER be used! */
int toascii(int c)
{
return c & 0x7f;
}

7
src/ctype/tolower.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
int tolower(int c)
{
if (isupper(c)) return c | 32;
return c;
}

7
src/ctype/toupper.c Normal file
View File

@ -0,0 +1,7 @@
#include <ctype.h>
int toupper(int c)
{
if (islower(c)) return c & 0x5f;
return c;
}

246
src/ctype/towctrans.c Normal file
View File

@ -0,0 +1,246 @@
#include <wchar.h>
#include <wctype.h>
#include <stdio.h>
#define CASEMAP(u1,u2,l) { (u1), (l)-(u1), (u2)-(u1)+1 }
#define CASELACE(u1,u2) CASEMAP((u1),(u2),(u1)+1)
static const struct {
unsigned short upper;
signed char lower;
unsigned char len;
} casemaps[] = {
CASEMAP('A','Z','a'),
CASEMAP(0xc0,0xde,0xe0),
CASELACE(0x0100,0x012e),
CASELACE(0x0132,0x0136),
CASELACE(0x0139,0x0147),
CASELACE(0x014a,0x0176),
CASELACE(0x0179,0x017d),
CASELACE(0x370,0x372),
CASEMAP(0x391,0x3a1,0x3b1),
CASEMAP(0x3a3,0x3ab,0x3c3),
CASEMAP(0x400,0x40f,0x450),
CASEMAP(0x410,0x42f,0x430),
CASELACE(0x460,0x480),
CASELACE(0x48a,0x4be),
CASELACE(0x4c1,0x4cd),
CASELACE(0x4d0,0x50e),
CASEMAP(0x531,0x556,0x561),
CASELACE(0x01a0,0x01a4),
CASELACE(0x01b3,0x01b5),
CASELACE(0x01cd,0x01db),
CASELACE(0x01de,0x01ee),
CASELACE(0x01f8,0x021e),
CASELACE(0x0222,0x0232),
CASELACE(0x03d8,0x03ee),
CASELACE(0x1e00,0x1e94),
CASELACE(0x1ea0,0x1efe),
CASEMAP(0x1f08,0x1f0f,0x1f00),
CASEMAP(0x1f18,0x1f1d,0x1f10),
CASEMAP(0x1f28,0x1f2f,0x1f20),
CASEMAP(0x1f38,0x1f3f,0x1f30),
CASEMAP(0x1f48,0x1f4d,0x1f40),
CASEMAP(0x1f68,0x1f6f,0x1f60),
CASEMAP(0x1f88,0x1f8f,0x1f80),
CASEMAP(0x1f98,0x1f9f,0x1f90),
CASEMAP(0x1fa8,0x1faf,0x1fa0),
CASEMAP(0x1fb8,0x1fb9,0x1fb0),
CASEMAP(0x1fba,0x1fbb,0x1f70),
CASEMAP(0x1fc8,0x1fcb,0x1f72),
CASEMAP(0x1fd8,0x1fd9,0x1fd0),
CASEMAP(0x1fda,0x1fdb,0x1f76),
CASEMAP(0x1fe8,0x1fe9,0x1fe0),
CASEMAP(0x1fea,0x1feb,0x1f7a),
CASEMAP(0x1ff8,0x1ff9,0x1f78),
CASEMAP(0x1ffa,0x1ffb,0x1f7c),
CASELACE(0x246,0x24e),
CASELACE(0x510,0x512),
CASEMAP(0x2160,0x216f,0x2170),
CASEMAP(0x2c00,0x2c2e,0x2c30),
CASELACE(0x2c67,0x2c6b),
CASELACE(0x2c80,0x2ce2),
CASELACE(0xa722,0xa72e),
CASELACE(0xa732,0xa76e),
CASELACE(0xa779,0xa77b),
CASELACE(0xa77e,0xa786),
CASEMAP(0xff21,0xff3a,0xff41),
{ 0,0,0 }
};
static const unsigned short pairs[][2] = {
{ 'I', 0x0131 },
{ 'S', 0x017f },
{ 0x0130, 'i' },
{ 0x0178, 0x00ff },
{ 0x0181, 0x0253 },
{ 0x0182, 0x0183 },
{ 0x0184, 0x0185 },
{ 0x0186, 0x0254 },
{ 0x0187, 0x0188 },
{ 0x0189, 0x0256 },
{ 0x018a, 0x0257 },
{ 0x018b, 0x018c },
{ 0x018e, 0x01dd },
{ 0x018f, 0x0259 },
{ 0x0190, 0x025b },
{ 0x0191, 0x0192 },
{ 0x0193, 0x0260 },
{ 0x0194, 0x0263 },
{ 0x0196, 0x0269 },
{ 0x0197, 0x0268 },
{ 0x0198, 0x0199 },
{ 0x019c, 0x026f },
{ 0x019d, 0x0272 },
{ 0x019f, 0x0275 },
{ 0x01a6, 0x0280 },
{ 0x01a7, 0x01a8 },
{ 0x01a9, 0x0283 },
{ 0x01ac, 0x01ad },
{ 0x01ae, 0x0288 },
{ 0x01af, 0x01b0 },
{ 0x01b1, 0x028a },
{ 0x01b2, 0x028b },
{ 0x01b7, 0x0292 },
{ 0x01b8, 0x01b9 },
{ 0x01bc, 0x01bd },
{ 0x01c4, 0x01c6 },
{ 0x01c4, 0x01c5 },
{ 0x01c5, 0x01c6 },
{ 0x01c7, 0x01c9 },
{ 0x01c7, 0x01c8 },
{ 0x01c8, 0x01c9 },
{ 0x01ca, 0x01cc },
{ 0x01ca, 0x01cb },
{ 0x01cb, 0x01cc },
{ 0x01f1, 0x01f3 },
{ 0x01f1, 0x01f2 },
{ 0x01f2, 0x01f3 },
{ 0x01f4, 0x01f5 },
{ 0x01f6, 0x0195 },
{ 0x01f7, 0x01bf },
{ 0x0220, 0x019e },
{ 0x0386, 0x03ac },
{ 0x0388, 0x03ad },
{ 0x0389, 0x03ae },
{ 0x038a, 0x03af },
{ 0x038c, 0x03cc },
{ 0x038e, 0x03cd },
{ 0x038f, 0x03ce },
{ 0x0399, 0x0345 },
{ 0x0399, 0x1fbe },
{ 0x03a3, 0x03c2 },
{ 0x03f7, 0x03f8 },
{ 0x03fa, 0x03fb },
{ 0x1e60, 0x1e9b },
{ 0x1f59, 0x1f51 },
{ 0x1f5b, 0x1f53 },
{ 0x1f5d, 0x1f55 },
{ 0x1f5f, 0x1f57 },
{ 0x1fbc, 0x1fb3 },
{ 0x1fcc, 0x1fc3 },
{ 0x1fec, 0x1fe5 },
{ 0x1ffc, 0x1ff3 },
{ 0x23a, 0x2c65 },
{ 0x23b, 0x23c },
{ 0x23d, 0x19a },
{ 0x23e, 0x2c66 },
{ 0x241, 0x242 },
{ 0x243, 0x180 },
{ 0x244, 0x289 },
{ 0x245, 0x28c },
{ 0x3f4, 0x3b8 },
{ 0x3f9, 0x3f2 },
{ 0x3fd, 0x37b },
{ 0x3fe, 0x37c },
{ 0x3ff, 0x37d },
{ 0x4c0, 0x4cf },
{ 0x2126, 0x3c9 },
{ 0x212a, 'k' },
{ 0x212b, 0xe5 },
{ 0x2132, 0x214e },
{ 0x2183, 0x2184 },
{ 0x2c60, 0x2c61 },
{ 0x2c62, 0x26b },
{ 0x2c63, 0x1d7d },
{ 0x2c64, 0x27d },
{ 0x2c6d, 0x251 },
{ 0x2c6e, 0x271 },
{ 0x2c6f, 0x250 },
{ 0x2c72, 0x2c73 },
{ 0x2c75, 0x2c76 },
{ 0xa77d, 0x1d79 },
/* bogus greek 'symbol' letters */
{ 0x376, 0x377 },
{ 0x39c, 0xb5 },
{ 0x392, 0x3d0 },
{ 0x398, 0x3d1 },
{ 0x3a6, 0x3d5 },
{ 0x3a0, 0x3d6 },
{ 0x39a, 0x3f0 },
{ 0x3a1, 0x3f1 },
{ 0x395, 0x3f5 },
{ 0x3cf, 0x3d7 },
{ 0,0 }
};
static wchar_t __towcase(wchar_t wc, int lower)
{
int i;
int lmul = 2*lower-1;
int lmask = lower-1;
if ((unsigned)wc - 0x10400 < 0x50)
return wc + lmul*0x28;
/* no letters with case in these large ranges */
if (!iswalpha(wc)
|| (unsigned)wc - 0x0600 <= 0x0fff-0x0600
|| (unsigned)wc - 0x2e00 <= 0xa6ff-0x2e00
|| (unsigned)wc - 0xa800 <= 0xfeff-0xa800)
return wc;
/* special case because the diff between upper/lower is too big */
if ((unsigned)wc - 0x10a0 < 0x26 || (unsigned)wc - 0x2d00 < 0x26)
return wc + lmul*(0x2d00-0x10a0);
for (i=0; casemaps[i].len; i++) {
int base = casemaps[i].upper + (lmask & casemaps[i].lower);
if ((unsigned)wc-base < casemaps[i].len) {
if (casemaps[i].lower == 1)
return wc + lower - ((wc-casemaps[i].upper)&1);
return wc + lmul*casemaps[i].lower;
}
}
for (i=0; pairs[i][1-lower]; i++) {
if (pairs[i][1-lower] == wc)
return pairs[i][lower];
}
if ((unsigned)wc - 0x10428 + (lower<<5) + (lower<<3) < 0x28)
return wc - 0x28 + (lower<<10) + (lower<<6);
return wc;
}
wint_t towupper(wint_t wc)
{
return __towcase(wc, 0);
}
wint_t towlower(wint_t wc)
{
return __towcase(wc, 1);
}

8
src/ctype/wcswidth.c Normal file
View File

@ -0,0 +1,8 @@
#include <wchar.h>
int wcswidth(const wchar_t *wcs, size_t n)
{
int l=0, k=0;
for (; n-- && *wcs && (k = wcwidth(*wcs)) >= 0; l+=k, wcs++);
return (k < 0) ? k : l;
}

16
src/ctype/wctrans.c Normal file
View File

@ -0,0 +1,16 @@
#include <wctype.h>
#include <string.h>
wctrans_t wctrans(const char *class)
{
if (!strcmp(class, "toupper")) return 1;
if (!strcmp(class, "tolower")) return 2;
return 0;
}
wint_t towctrans(wint_t wc, wctrans_t trans)
{
if (trans == 1) return towupper(wc);
if (trans == 2) return towlower(wc);
return wc;
}

185
src/ctype/wcwidth.c Normal file
View File

@ -0,0 +1,185 @@
#include <inttypes.h>
#include <wchar.h>
#define R(a,b,w) { (b), (w)/2, (b)-(a) }
static const struct range {
uint32_t base:20;
uint32_t width:1;
uint32_t len:11;
} ranges[] = {
R(0x0300, 0x036F, 0),
R(0x0483, 0x0486, 0),
R(0x0488, 0x0489, 0),
R(0x0591, 0x05BD, 0),
R(0x05BF, 0x05BF, 0),
R(0x05C1, 0x05C2, 0),
R(0x05C4, 0x05C5, 0),
R(0x05C7, 0x05C7, 0),
R(0x0600, 0x0603, 0),
R(0x0610, 0x0615, 0),
R(0x064B, 0x065E, 0),
R(0x0670, 0x0670, 0),
R(0x06D6, 0x06E4, 0),
R(0x06E7, 0x06E8, 0),
R(0x06EA, 0x06ED, 0),
R(0x070F, 0x070F, 0),
R(0x0711, 0x0711, 0),
R(0x0730, 0x074A, 0),
R(0x07A6, 0x07B0, 0),
R(0x07EB, 0x07F3, 0),
R(0x0901, 0x0902, 0),
R(0x093C, 0x093C, 0),
R(0x0941, 0x0948, 0),
R(0x094D, 0x094D, 0),
R(0x0951, 0x0954, 0),
R(0x0962, 0x0963, 0),
R(0x0981, 0x0981, 0),
R(0x09BC, 0x09BC, 0),
R(0x09C1, 0x09C4, 0),
R(0x09CD, 0x09CD, 0),
R(0x09E2, 0x09E3, 0),
R(0x0A01, 0x0A02, 0),
R(0x0A3C, 0x0A3C, 0),
R(0x0A41, 0x0A42, 0),
R(0x0A47, 0x0A48, 0),
R(0x0A4B, 0x0A4D, 0),
R(0x0A70, 0x0A71, 0),
R(0x0A81, 0x0A82, 0),
R(0x0ABC, 0x0ABC, 0),
R(0x0AC1, 0x0AC5, 0),
R(0x0AC7, 0x0AC8, 0),
R(0x0ACD, 0x0ACD, 0),
R(0x0AE2, 0x0AE3, 0),
R(0x0B01, 0x0B01, 0),
R(0x0B3C, 0x0B3C, 0),
R(0x0B3F, 0x0B3F, 0),
R(0x0B41, 0x0B43, 0),
R(0x0B4D, 0x0B4D, 0),
R(0x0B56, 0x0B56, 0),
R(0x0B82, 0x0B82, 0),
R(0x0BC0, 0x0BC0, 0),
R(0x0BCD, 0x0BCD, 0),
R(0x0C3E, 0x0C40, 0),
R(0x0C46, 0x0C48, 0),
R(0x0C4A, 0x0C4D, 0),
R(0x0C55, 0x0C56, 0),
R(0x0CBC, 0x0CBC, 0),
R(0x0CBF, 0x0CBF, 0),
R(0x0CC6, 0x0CC6, 0),
R(0x0CCC, 0x0CCD, 0),
R(0x0CE2, 0x0CE3, 0),
R(0x0D41, 0x0D43, 0),
R(0x0D4D, 0x0D4D, 0),
R(0x0DCA, 0x0DCA, 0),
R(0x0DD2, 0x0DD4, 0),
R(0x0DD6, 0x0DD6, 0),
R(0x0E31, 0x0E31, 0),
R(0x0E34, 0x0E3A, 0),
R(0x0E47, 0x0E4E, 0),
R(0x0EB1, 0x0EB1, 0),
R(0x0EB4, 0x0EB9, 0),
R(0x0EBB, 0x0EBC, 0),
R(0x0EC8, 0x0ECD, 0),
R(0x0F18, 0x0F19, 0),
R(0x0F35, 0x0F35, 0),
R(0x0F37, 0x0F37, 0),
R(0x0F39, 0x0F39, 0),
R(0x0F71, 0x0F7E, 0),
R(0x0F80, 0x0F84, 0),
R(0x0F86, 0x0F87, 0),
R(0x0F90, 0x0F97, 0),
R(0x0F99, 0x0FBC, 0),
R(0x0FC6, 0x0FC6, 0),
R(0x102D, 0x1030, 0),
R(0x1032, 0x1032, 0),
R(0x1036, 0x1037, 0),
R(0x1039, 0x1039, 0),
R(0x1058, 0x1059, 0),
R(0x1100, 0x115F, 2),
R(0x1160, 0x11FF, 0),
R(0x135F, 0x135F, 0),
R(0x1712, 0x1714, 0),
R(0x1732, 0x1734, 0),
R(0x1752, 0x1753, 0),
R(0x1772, 0x1773, 0),
R(0x17B4, 0x17B5, 0),
R(0x17B7, 0x17BD, 0),
R(0x17C6, 0x17C6, 0),
R(0x17C9, 0x17D3, 0),
R(0x17DD, 0x17DD, 0),
R(0x180B, 0x180D, 0),
R(0x18A9, 0x18A9, 0),
R(0x1920, 0x1922, 0),
R(0x1927, 0x1928, 0),
R(0x1932, 0x1932, 0),
R(0x1939, 0x193B, 0),
R(0x1A17, 0x1A18, 0),
R(0x1B00, 0x1B03, 0),
R(0x1B34, 0x1B34, 0),
R(0x1B36, 0x1B3A, 0),
R(0x1B3C, 0x1B3C, 0),
R(0x1B42, 0x1B42, 0),
R(0x1B6B, 0x1B73, 0),
R(0x1DC0, 0x1DCA, 0),
R(0x1DFE, 0x1DFF, 0),
R(0x200B, 0x200F, 0),
R(0x202A, 0x202E, 0),
R(0x2060, 0x2063, 0),
R(0x206A, 0x206F, 0),
R(0x20D0, 0x20EF, 0),
R(0x2329, 0x232A, 2),
R(0x2E80, 0x3029, 2),
R(0x302A, 0x302F, 0),
R(0x3030, 0x303E, 2),
R(0x3099, 0x309A, 0),
R(0xA806, 0xA806, 0),
R(0xA80B, 0xA80B, 0),
R(0xA825, 0xA826, 0),
R(0xF900, 0xFAFF, 2),
R(0xFB1E, 0xFB1E, 0),
R(0xFE00, 0xFE0F, 0),
R(0xFE20, 0xFE23, 0),
R(0xFE30, 0xFE6F, 2),
R(0xFEFF, 0xFEFF, 0),
R(0xFF00, 0xFF60, 2),
R(0xFFE0, 0xFFE6, 2),
R(0x10A01, 0x10A03, 0),
R(0x10A05, 0x10A06, 0),
R(0x10A0C, 0x10A0F, 0),
R(0x10A38, 0x10A3A, 0),
R(0x10A3F, 0x10A3F, 0),
R(0x1D167, 0x1D169, 0),
R(0x1D173, 0x1D182, 0),
R(0x1D185, 0x1D18B, 0),
R(0x1D1AA, 0x1D1AD, 0),
R(0x1D242, 0x1D244, 0),
R(0xE0001, 0xE0001, 0),
R(0xE0020, 0xE007F, 0),
R(0xE0100, 0xE01EF, 0),
};
/* Note: because the len field is only 10 bits, we must special-case
* the two huge ranges of full width characters and exclude them
* from the binary search table. */
int wcwidth(wchar_t wc)
{
int a, n;
uint32_t c = wc;
if (c-0x20 < 0x5f) return 1;
if (!iswprint(c)) return wc ? -1 : 0;
if (c-0x20000 < 0x20000) return 2;
/* The following code is a branchless binary search. */
a = 0;
n = sizeof ranges / sizeof ranges[0];
do {
n >>= 1;
a += n+1 & (signed)(ranges[a+n].base-c)>>31;
} while (n);
if (ranges[a].base-c <= ranges[a].len)
return 2*ranges[a].width;
return 1 + (c-0x3040 < 0xd800-0x3040);
}

9
src/dirent/__dirent.h Normal file
View File

@ -0,0 +1,9 @@
struct __DIR_s
{
int lock;
int fd;
off_t tell;
int buf_pos;
int buf_end;
char buf[2048];
};

12
src/dirent/__getdents.c Normal file
View File

@ -0,0 +1,12 @@
#include <dirent.h>
#include "syscall.h"
#include "libc.h"
int __getdents(int fd, struct dirent *buf, size_t len)
{
return syscall3(__NR_getdents64, fd, (long)buf, len);
}
weak_alias(__getdents, getdents);
LFS64(getdents);

10
src/dirent/alphasort.c Normal file
View File

@ -0,0 +1,10 @@
#include <string.h>
#include <dirent.h>
#include "libc.h"
int alphasort(const struct dirent **a, const struct dirent **b)
{
return strcoll((*a)->d_name, (*b)->d_name);
}
LFS64(alphasort);

11
src/dirent/closedir.c Normal file
View File

@ -0,0 +1,11 @@
#include <dirent.h>
#include <unistd.h>
#include "__dirent.h"
#include "libc.h"
int closedir(DIR *dir)
{
int ret = close(dir->fd);
free(dir);
return ret;
}

7
src/dirent/dirfd.c Normal file
View File

@ -0,0 +1,7 @@
#include <dirent.h>
#include "__dirent.h"
int dirfd(DIR *d)
{
return d->fd;
}

26
src/dirent/fdopendir.c Normal file
View File

@ -0,0 +1,26 @@
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "__dirent.h"
DIR *fdopendir(int fd)
{
DIR *dir;
struct stat st;
if (fstat(fd, &st) < 0 || !S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return 0;
}
if (!(dir = calloc(1, sizeof *dir))) {
return 0;
}
fcntl(fd, F_SETFD, FD_CLOEXEC);
dir->fd = fd;
return dir;
}

25
src/dirent/opendir.c Normal file
View File

@ -0,0 +1,25 @@
#define _GNU_SOURCE
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "__dirent.h"
DIR *opendir(const char *name)
{
int fd;
DIR *dir;
if ((fd = open(name, O_RDONLY|O_DIRECTORY)) < 0)
return 0;
fcntl(fd, F_SETFD, FD_CLOEXEC);
if (!(dir = calloc(1, sizeof *dir))) {
close(fd);
return 0;
}
dir->fd = fd;
return dir;
}

32
src/dirent/readdir.c Normal file
View File

@ -0,0 +1,32 @@
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include "__dirent.h"
#include "syscall.h"
#include "libc.h"
int __getdents(int, struct dirent *, size_t);
struct dirent *readdir(DIR *dir)
{
struct dirent *de;
if (dir->buf_pos >= dir->buf_end) {
int len = __getdents(dir->fd, (void *)dir->buf, sizeof dir->buf);
if (len < 0) {
dir->lock = 0;
return NULL;
} else if (len == 0) return 0;
dir->buf_end = len;
dir->buf_pos = 0;
}
de = (void *)(dir->buf + dir->buf_pos);
dir->buf_pos += de->d_reclen;
dir->tell = de->d_off;
return de;
}
LFS64(readdir);

30
src/dirent/readdir_r.c Normal file
View File

@ -0,0 +1,30 @@
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "__dirent.h"
#include "libc.h"
int readdir_r(DIR *dir, struct dirent *buf, struct dirent **result)
{
struct dirent *de;
int errno_save = errno;
int ret;
LOCK(&dir->lock);
errno = 0;
de = readdir(dir);
if ((ret = errno)) {
UNLOCK(&dir->lock);
return ret;
}
errno = errno_save;
if (de) memcpy(buf, de, de->d_reclen);
else buf = NULL;
UNLOCK(&dir->lock);
*result = buf;
return 0;
}
LFS64_2(readdir_r, readdir64_r);

13
src/dirent/rewinddir.c Normal file
View File

@ -0,0 +1,13 @@
#include <dirent.h>
#include <unistd.h>
#include "__dirent.h"
#include "libc.h"
void rewinddir(DIR *dir)
{
LOCK(&dir->lock);
lseek(dir->fd, 0, SEEK_SET);
dir->buf_pos = dir->buf_end = 0;
dir->tell = 0;
UNLOCK(&dir->lock);
}

50
src/dirent/scandir.c Normal file
View File

@ -0,0 +1,50 @@
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <stddef.h>
#include <libc.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, size;
int old_errno = errno;
if (!d) 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;
}
size = offsetof(struct dirent,d_name) + strlen(de->d_name) + 1;
names[cnt] = malloc(size);
if (!names[cnt]) break;
memcpy(names[cnt++], de, size);
}
closedir(d);
if (errno) {
old_errno = errno;
if (names) while (cnt-->0) free(names[cnt]);
free(names);
errno = old_errno;
return -1;
}
if (cmp) qsort(names, cnt, sizeof *names, (int (*)(const void *, const void *))cmp);
*res = names;
return cnt;
}
LFS64(scandir);

12
src/dirent/seekdir.c Normal file
View File

@ -0,0 +1,12 @@
#include <dirent.h>
#include <unistd.h>
#include "__dirent.h"
#include "libc.h"
void seekdir(DIR *dir, long off)
{
LOCK(&dir->lock);
dir->tell = lseek(dir->fd, off, SEEK_SET);
dir->buf_pos = dir->buf_end = 0;
UNLOCK(&dir->lock);
}

7
src/dirent/telldir.c Normal file
View File

@ -0,0 +1,7 @@
#include <dirent.h>
#include "__dirent.h"
long telldir(DIR *dir)
{
return dir->tell;
}

7
src/env/__environ.c vendored Normal file
View File

@ -0,0 +1,7 @@
#include "libc.h"
#undef environ
char **___environ = 0;
weak_alias(___environ, __environ);
weak_alias(___environ, _environ);
weak_alias(___environ, environ);

26
src/env/__libc_start_main.c vendored Normal file
View File

@ -0,0 +1,26 @@
#include "libc.h"
/* Any use of __environ/environ will override this symbol. */
char **__dummy_environ = (void *)-1;
weak_alias(__dummy_environ, ___environ);
int __libc_start_main(
int (*main)(int, char **, char **), int argc, char **argv,
int (*init)(int, char **, char **), void (*fini)(void),
void (*ldso_fini)(void))
{
/* Save the environment if it may be used by libc/application */
char **envp = argv+argc+1;
if (___environ != (void *)-1) ___environ = envp;
/* Avoid writing 0 and triggering unnecessary COW */
if (ldso_fini) libc.ldso_fini = ldso_fini;
if (fini) libc.fini = fini;
/* Execute constructors (static) linked into the application */
if (init) init(argc, argv, envp);
/* Pass control to to application */
exit(main(argc, argv, envp));
return 0;
}

9
src/env/clearenv.c vendored Normal file
View File

@ -0,0 +1,9 @@
#include <stdlib.h>
extern char **__environ;
int clearenv()
{
__environ[0] = 0;
return 0;
}

14
src/env/getenv.c vendored Normal file
View File

@ -0,0 +1,14 @@
#include <stdlib.h>
#include <string.h>
#include "libc.h"
char *getenv(const char *name)
{
int i;
size_t l = strlen(name);
if (!__environ || !*name || strchr(name, '=')) return NULL;
for (i=0; __environ[i] && (strncmp(name, __environ[i], l)
|| __environ[i][l] != '='); i++);
if (__environ[i]) return __environ[i] + l+1;
return NULL;
}

59
src/env/putenv.c vendored Normal file
View File

@ -0,0 +1,59 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
extern char **__environ;
char **__env_map;
int __putenv(char *s, int a)
{
int i=0, j=0;
char *end = strchr(s, '=');
size_t l = end-s+1;
char **newenv = 0;
char **newmap = 0;
static char **oldenv;
if (!end || l == 1) return -1;
for (; __environ[i] && memcmp(s, __environ[i], l); i++);
if (a) {
if (!__env_map) {
__env_map = calloc(2, sizeof(char *));
if (__env_map) __env_map[0] = s;
} else {
for (; __env_map[j] && __env_map[j] != __environ[i]; j++);
if (!__env_map[j]) {
newmap = realloc(__env_map, sizeof(char *)*(j+2));
if (newmap) {
__env_map = newmap;
__env_map[j] = s;
__env_map[j+1] = NULL;
}
} else {
free(__env_map[j]);
}
}
}
if (!__environ[i]) {
newenv = malloc(sizeof(char *)*(i+2));
if (!newenv) {
if (a && __env_map) __env_map[j] = 0;
return -1;
}
memcpy(newenv, __environ, sizeof(char *)*i);
newenv[i] = s;
newenv[i+1] = 0;
__environ = newenv;
free(oldenv);
oldenv = __environ;
}
__environ[i] = s;
return 0;
}
int putenv(char *s)
{
return __putenv(s, 0);
}

31
src/env/setenv.c vendored Normal file
View File

@ -0,0 +1,31 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int __putenv(char *s, int a);
int setenv(const char *var, const char *value, int overwrite)
{
char *s;
int l1, l2;
if (strchr(var, '=')) {
errno = EINVAL;
return -1;
}
if (!overwrite && getenv(var)) return 0;
l1 = strlen(var);
l2 = strlen(value);
s = malloc(l1+l2+2);
memcpy(s, var, l1);
s[l1] = '=';
memcpy(s+l1+1, value, l2);
s[l1+l2+1] = 0;
if (__putenv(s, 1)) {
free(s);
errno = ENOMEM;
return -1;
}
return 0;
}

32
src/env/unsetenv.c vendored Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
extern char **__environ;
extern char **__env_map;
int unsetenv(const char *name)
{
int i, j;
size_t l = strlen(name);
if (!*name || strchr(name, '=')) {
errno = EINVAL;
return -1;
}
again:
for (i=0; __environ[i] && (memcmp(name, __environ[i], l) || __environ[i][l] != '='); i++);
if (__environ[i]) {
if (__env_map) {
for (j=0; __env_map[j] && __env_map[j] != __environ[i]; j++);
free (__env_map[j]);
for (; __env_map[j]; j++)
__env_map[j] = __env_map[j+1];
}
for (; __environ[i]; i++)
__environ[i] = __environ[i+1];
goto again;
}
return 0;
}

View File

@ -0,0 +1,11 @@
#include <errno.h>
#include "libc.h"
#undef errno
int errno;
int *__errno_location(void)
{
if (libc.errno_location) return libc.errno_location();
return &errno;
}

101
src/errno/__strerror.h Normal file
View File

@ -0,0 +1,101 @@
/* This file is sorted such that 'errors' which represent exceptional
* conditions under which a correct program may fail come first, followed
* by messages that indicate an incorrect program or system failure. The
* macro E() along with double-inclusion is used to ensure that ordering
* of the strings remains synchronized. */
E(EILSEQ, "Illegal byte sequence")
E(EDOM, "Argument outside domain")
E(ERANGE, "Result not representable")
E(ENOTTY, "Not a tty")
E(EACCES, "Permission denied")
E(EPERM, "Operation not permitted")
E(ENOENT, "No such file or directory")
E(ESRCH, "No such process")
E(EEXIST, "File exists")
E(EOVERFLOW, "Value too large for defined data type")
E(ENOSPC, "No space left on device")
E(ENOMEM, "Out of memory")
E(EBUSY, "Device or resource busy")
E(EINTR, "Interrupted system call")
E(EAGAIN, "Operation would block")
E(ESPIPE, "Illegal seek")
E(EXDEV, "Cross-device link")
E(EROFS, "Read-only file system")
E(ENOTEMPTY, "Directory not empty")
E(ECONNRESET, "Connection reset by peer")
E(ETIMEDOUT, "Connection timed out")
E(ECONNREFUSED, "Connection refused")
E(EHOSTDOWN, "Host is down")
E(EHOSTUNREACH, "No route to host")
E(EADDRINUSE, "Address already in use")
E(EPIPE, "Broken pipe")
E(EIO, "I/O error")
E(ENXIO, "No such device or address")
E(ENOTBLK, "Block device required")
E(ENODEV, "No such device")
E(ENOTDIR, "Not a directory")
E(EISDIR, "Is a directory")
E(ETXTBSY, "Text file busy")
E(ENOEXEC, "Exec format error")
E(EINVAL, "Invalid argument")
E(E2BIG, "Argument list too long")
E(ELOOP, "Too many levels of symbolic links")
E(ENAMETOOLONG, "Filename too long")
E(ENFILE, "File table overflow")
E(EMFILE, "Too many open files")
E(EBADF, "Bad file number")
E(ECHILD, "No child processes")
E(EFAULT, "Bad address")
E(EFBIG, "File too large")
E(EMLINK, "Too many links")
E(ENOLCK, "No record locks available")
E(EDEADLK, "Resource deadlock would occur")
E(ENOSYS, "Function not supported")
E(ENOMSG, "No message of desired type")
E(EIDRM, "Identifier removed")
E(ENOSTR, "Device not a stream")
E(ENODATA, "No data available")
E(ETIME, "Timer expired")
E(ENOSR, "Out of streams resources")
E(ENOLINK, "Link has been severed")
E(EPROTO, "Protocol error")
E(EBADMSG, "Not a data message")
E(EBADFD, "File descriptor in bad state")
E(ENOTSOCK, "Socket operation on non-socket")
E(EDESTADDRREQ, "Destination address required")
E(EMSGSIZE, "Message too long")
E(EPROTOTYPE, "Protocol wrong type for socket")
E(ENOPROTOOPT, "Protocol not available")
E(EPROTONOSUPPORT,"Protocol not supported")
E(ESOCKTNOSUPPORT,"Socket type not supported")
E(EOPNOTSUPP, "Operation not supported on socket")
E(EPFNOSUPPORT, "Protocol family not supported")
E(EAFNOSUPPORT, "Address family not supported by protocol")
E(EADDRNOTAVAIL,"Cannot assign requested address")
E(ENETDOWN, "Network is down")
E(ENETUNREACH, "Network is unreachable")
E(ENETRESET, "Network dropped connection because of reset")
E(ECONNABORTED, "Software caused connection abort")
E(ENOBUFS, "No buffer space available")
E(EISCONN, "Socket is connected")
E(ENOTCONN, "Socket is not connected")
E(ESHUTDOWN, "Cannot send after socket shutdown")
E(EALREADY, "Operation already in progress")
E(EINPROGRESS, "Operation now in progress")
E(ESTALE, "Stale NFS file handle")
E(EREMOTEIO, "Remote I/O error")
E(EDQUOT, "Quota exceeded")
E(ENOMEDIUM, "No medium found")
E(EMEDIUMTYPE, "Wrong medium type")
E(0, "Invalid error number")

22
src/errno/strerror.c Normal file
View File

@ -0,0 +1,22 @@
#include <errno.h>
#include <string.h>
#define E(a,b) a,
static const unsigned char errid[] = {
#include "__strerror.h"
};
#undef E
#define E(a,b) b "\0"
static const char errmsg[] =
#include "__strerror.h"
;
char *strerror(int e)
{
const char *s;
int i;
for (i=0; errid[i] && errid[i] != e; i++);
for (s=errmsg; i; s++, i--) for (; *s; s++);
return (char *)s;
}

9
src/exit/_Exit.c Normal file
View File

@ -0,0 +1,9 @@
#include <stdlib.h>
#define SYSCALL_NORETURN
#include "syscall.h"
void _Exit(int ec)
{
syscall1(__NR_exit_group, ec);
syscall1(__NR_exit, ec);
}

8
src/exit/abort.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdlib.h>
#include <signal.h>
void abort(void)
{
raise(SIGABRT);
for (;;);
}

9
src/exit/assert.c Normal file
View File

@ -0,0 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
void __assert_fail(const char *expr, const char *file, int line, const char *func)
{
fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line);
fflush(NULL);
abort();
}

57
src/exit/atexit.c Normal file
View File

@ -0,0 +1,57 @@
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>
#include "libc.h"
/* Ensure that at least 32 atexit handlers can be registered without malloc */
#define COUNT 32
static struct fl
{
struct fl *next;
void (*f[COUNT])(void);
} builtin, *head;
static int run_atexit_functions(void)
{
int i;
for (; head; head=head->next) {
for (i=COUNT-1; i>=0 && !head->f[i]; i--);
for (; i>=0; i--) head->f[i]();
}
return 0;
}
int (*const __funcs_on_exit)(void) = run_atexit_functions;
int atexit(void (*func)(void))
{
static int lock;
int i;
/* Hook for atexit extensions */
if (libc.atexit) return libc.atexit(func);
LOCK(&lock);
/* Defer initialization of head so it can be in BSS */
if (!head) head = &builtin;
/* If the current function list is full, add a new one */
if (head->f[COUNT-1]) {
struct fl *new_fl = calloc(sizeof(struct fl), 1);
if (!new_fl) {
UNLOCK(&lock);
return -1;
}
new_fl->next = head;
head = new_fl;
}
/* Append function to the list. */
for (i=0; i<COUNT && head->f[i]; i++);
head->f[i] = func;
UNLOCK(&lock);
return 0;
}

28
src/exit/exit.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include "libc.h"
/* __overflow.c and atexit.c override these */
static int (*const dummy)() = 0;
weak_alias(dummy, __funcs_on_exit);
weak_alias(dummy, __fflush_on_exit);
void exit(int code)
{
static int lock;
/* If more than one thread calls exit, hang until _Exit ends it all */
LOCK(&lock);
/* Only do atexit & stdio flush if they were actually used */
if (__funcs_on_exit) __funcs_on_exit();
if (__fflush_on_exit) __fflush_on_exit(0);
/* Destructor s**t is kept separate from atexit to avoid bloat */
if (libc.fini) libc.fini();
if (libc.ldso_fini) libc.ldso_fini();
_Exit(code);
for(;;);
}

9
src/fcntl/creat.c Normal file
View File

@ -0,0 +1,9 @@
#include <fcntl.h>
#include "libc.h"
int creat(const char *filename, mode_t mode)
{
return open(filename, O_CREAT|O_WRONLY|O_TRUNC, mode);
}
LFS64(creat);

22
src/fcntl/fcntl.c Normal file
View File

@ -0,0 +1,22 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include "syscall.h"
#include "libc.h"
int fcntl(int fd, int cmd, ...)
{
int r;
long arg;
va_list ap;
va_start(ap, cmd);
arg = va_arg(ap, long);
va_end(ap);
if (cmd == F_SETFL) arg |= O_LARGEFILE;
if (cmd == F_SETLKW) CANCELPT_BEGIN;
r = __syscall_fcntl(fd, cmd, arg);
if (cmd == F_SETLKW) CANCELPT_END;
return r;
}
LFS64(fcntl);

21
src/fcntl/open.c Normal file
View File

@ -0,0 +1,21 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include "syscall.h"
#include "libc.h"
int open(const char *filename, int flags, ...)
{
int r;
mode_t mode;
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
CANCELPT_BEGIN;
r = __syscall_open(filename, flags, mode);
CANCELPT_END;
return r;
}
LFS64(open);

21
src/fcntl/openat.c Normal file
View File

@ -0,0 +1,21 @@
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include "syscall.h"
#include "libc.h"
int openat(int fd, const char *filename, int flags, ...)
{
int r;
mode_t mode;
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
CANCELPT_BEGIN;
r = syscall4(__NR_openat, fd, (long)filename, flags|O_LARGEFILE, mode);
CANCELPT_END;
return r;
}
LFS64(openat);

110
src/internal/atomic.h Normal file
View File

@ -0,0 +1,110 @@
#ifndef _INTERNAA_ATOMIC_H
#define _INTERNAA_ATOMIC_H
#include <stdint.h>
static inline int a_ctz_64(uint64_t x)
{
int r;
__asm__( "bsf %1,%0 ; jnz 1f ; bsf %2,%0 ; addl $32,%0\n1:"
: "=r"(r) : "r"((unsigned)x), "r"((unsigned)(x>>32)) );
return r;
}
static inline void a_and_64(volatile uint64_t *p, uint64_t v)
{
__asm__( "lock ; andl %1, (%0) ; lock ; andl %2, 4(%0)"
: : "r"((long *)p), "r"((unsigned)v), "r"((unsigned)(v>>32)) );
}
static inline void a_or_64(volatile uint64_t *p, uint64_t v)
{
__asm__( "lock ; orl %1, (%0) ; lock ; orl %2, 4(%0)"
: : "r"((long *)p), "r"((unsigned)v), "r"((unsigned)(v>>32)) );
}
static inline void a_store_l(volatile void *p, long x)
{
__asm__( "movl %1, %0" : "=m"(*(long *)p) : "r"(x) : "memory" );
}
static inline void a_or_l(volatile void *p, long v)
{
__asm__( "lock ; orl %1, %0"
: "=m"(*(long *)p) : "r"(v) );
}
static inline void *a_cas_p(volatile void *p, void *t, void *s)
{
__asm__( "lock ; cmpxchg %3, %1"
: "=a"(t), "=m"(*(long *)p) : "a"(t), "r"(s) );
return t;
}
static inline long a_cas_l(volatile void *p, long t, long s)
{
__asm__( "lock ; cmpxchg %3, %1"
: "=a"(t), "=m"(*(long *)p) : "a"(t), "r"(s) );
return t;
}
static inline void *a_swap_p(void *volatile *x, void *v)
{
__asm__( "xchg %0, %1" : "=r"(v), "=m"(*(void **)x) : "0"(v) );
return v;
}
static inline long a_swap_l(volatile void *x, long v)
{
__asm__( "xchg %0, %1" : "=r"(v), "=m"(*(long *)x) : "0"(v) );
return v;
}
static inline void a_or(volatile void *p, int v)
{
__asm__( "lock ; orl %1, %0"
: "=m"(*(int *)p) : "r"(v) );
}
static inline void a_and(volatile void *p, int v)
{
__asm__( "lock ; andl %1, %0"
: "=m"(*(int *)p) : "r"(v) );
}
static inline int a_swap(volatile int *x, int v)
{
__asm__( "xchg %0, %1" : "=r"(v), "=m"(*x) : "0"(v) );
return v;
}
#define a_xchg a_swap
static inline int a_fetch_add(volatile int *x, int v)
{
__asm__( "lock ; xadd %0, %1" : "=r"(v), "=m"(*x) : "0"(v) );
return v;
}
static inline void a_inc(volatile int *x)
{
__asm__( "lock ; incl %0" : "=m"(*x) : "m"(*x) );
}
static inline void a_dec(volatile int *x)
{
__asm__( "lock ; decl %0" : "=m"(*x) : "m"(*x) );
}
static inline void a_store(volatile int *p, int x)
{
__asm__( "movl %1, %0" : "=m"(*p) : "r"(x) : "memory" );
}
static inline void a_spin()
{
__asm__ __volatile__( "pause" : : : "memory" );
}
#endif

22
src/internal/clone.h Normal file
View File

@ -0,0 +1,22 @@
#define CLONE_VM 0x00000100
#define CLONE_FS 0x00000200
#define CLONE_FILES 0x00000400
#define CLONE_SIGHAND 0x00000800
#define CLONE_PTRACE 0x00002000
#define CLONE_VFORK 0x00004000
#define CLONE_PARENT 0x00008000
#define CLONE_THREAD 0x00010000
#define CLONE_NEWNS 0x00020000
#define CLONE_SYSVSEM 0x00040000
#define CLONE_SETTLS 0x00080000
#define CLONE_PARENT_SETTID 0x00100000
#define CLONE_CHILD_CLEARTID 0x00200000
#define CLONE_DETACHED 0x00400000
#define CLONE_UNTRACED 0x00800000
#define CLONE_CHILD_SETTID 0x01000000
#define CLONE_NEWUTS 0x04000000
#define CLONE_NEWIPC 0x08000000
#define CLONE_NEWUSER 0x10000000
#define CLONE_NEWPID 0x20000000
#define CLONE_NEWNET 0x40000000
#define CLONE_IO 0x80000000

16
src/internal/futex.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef _INTERNAL_FUTEX_H
#define _INTERNAL_FUTEX_H
#define FUTEX_WAIT 0
#define FUTEX_WAKE 1
#define FUTEX_FD 2
#define FUTEX_REQUEUE 3
#define FUTEX_CMP_REQUEUE 4
#define FUTEX_WAKE_OP 5
#define FUTEX_LOCK_PI 6
#define FUTEX_UNLOCK_PI 7
#define FUTEX_TRYLOCK_PI 8
int __futex(volatile int *, int, int, void *);
#endif

3
src/internal/libc.c Normal file
View File

@ -0,0 +1,3 @@
#include "libc.h"
struct libc libc;

43
src/internal/libc.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef LIBC_H
#define LIBC_H
#include <stdlib.h>
#include <stdio.h>
#define libc __libc
extern struct libc {
void (*lock)(volatile int *);
void (*cancelpt)(int);
int (*atexit)(void (*)(void));
void (*fini)(void);
void (*ldso_fini)(void);
int *(*errno_location)(void);
volatile int threads_minus_1;
int (*rsyscall)(int, long, long, long, long, long, long);
void (**tsd_keys)(void *);
} libc;
/* Designed to avoid any overhead in non-threaded processes */
void __lock(volatile int *);
#define LOCK(x) (libc.threads_minus_1 ? (__lock(x),1) : ((void)(x),1))
#define UNLOCK(x) (*(x)=0)
#define CANCELPT(x) (libc.cancelpt ? libc.cancelpt((x)),0 : (void)(x),0)
#define CANCELPT_BEGIN CANCELPT(1)
#define CANCELPT_END CANCELPT(0)
extern char **__environ;
#define environ __environ
#undef weak_alias
#define weak_alias(old, new) \
extern __typeof(old) new __attribute__((weak, alias(#old)))
#undef LFS64_2
//#define LFS64_2(x, y) weak_alias(x, y)
#define LFS64_2(x, y) extern __typeof(x) y
#undef LFS64
#define LFS64(x) LFS64_2(x, x##64)
#endif

View File

@ -0,0 +1,5 @@
#include <locale.h>
struct __locale {
int dummy;
};

View File

@ -0,0 +1,68 @@
#ifndef _PTHREAD_IMPL_H
#define _PTHREAD_IMPL_H
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <limits.h>
#include <inttypes.h>
#include <setjmp.h>
#include <string.h>
#include <time.h>
#include "libc.h"
#include "syscall.h"
#include "atomic.h"
#include "futex.h"
#define pthread __pthread
struct pthread {
struct pthread *self, *join;
int errno_val;
pid_t tid, pid;
volatile int canceldisable, cancelasync, cancelpoint, cancel;
unsigned char *map_base;
size_t map_size;
void *start_arg;
void *(*start)(void *);
void *result;
jmp_buf exit_jmp_buf;
int detached;
int exitlock;
unsigned long tlsdesc[4];
struct __ptcb *cancelbuf;
void **tsd;
int tsd_used;
pthread_attr_t attr;
int *errno_ptr;
};
static inline struct pthread *__pthread_self()
{
struct pthread *self;
__asm__ ("movl %%gs:0,%0" : "=r" (self) );
return self;
}
#define SIGCANCEL 32
#define SIGSYSCALL 33
#define SIGTIMER 32 /* ?? */
int __set_thread_area(unsigned long *);
int __set_pthread_self(void *);
int __libc_sigaction(int, const struct sigaction *, struct sigaction *);
int __libc_sigprocmask(int, const sigset_t *, sigset_t *);
void __lock(volatile int *);
void __unmapself(void *, size_t);
int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int);
void __wait(volatile int *, volatile int *, int, int);
void __wake(volatile int *, int, int);
#define DEFAULT_STACK_SIZE (16384-PAGE_SIZE)
#define DEFAULT_GUARD_SIZE PAGE_SIZE
#endif

100
src/internal/stdio_impl.h Normal file
View File

@ -0,0 +1,100 @@
#ifndef _STDIO_IMPL_H
#define _STDIO_IMPL_H
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include <wchar.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <sys/wait.h>
#include <math.h>
#include <float.h>
#include "syscall.h"
#include "libc.h"
#define UNGET 4
#define FLOCK(f) LOCK(&f->lock)
#define FUNLOCK(f) UNLOCK(&f->lock)
#define F_PERM 1
#define F_NORD 4
#define F_NOWR 8
#define F_EOF 16
#define F_ERR 32
struct __FILE_s {
unsigned flags;
unsigned char *rpos, *rstop;
unsigned char *rend, *wend;
unsigned char *wpos, *wstop;
unsigned char *wbase;
unsigned char *dummy01[3];
unsigned char *buf;
size_t buf_size;
FILE *prev, *next;
int fd;
int pipe_pid;
long dummy2;
short dummy3;
char dummy4;
signed char lbf;
int lock;
int lockcount;
void *owner;
off_t off;
int (*flush)(FILE *);
void **wide_data; /* must be NULL */
size_t (*read)(FILE *, unsigned char *, size_t);
size_t (*write)(FILE *, const unsigned char *, size_t);
off_t (*seek)(FILE *, off_t, int);
int mode;
int (*close)(FILE *);
};
size_t __stdio_read(FILE *, unsigned char *, size_t);
size_t __stdio_write(FILE *, const unsigned char *, size_t);
off_t __stdio_seek(FILE *, off_t, int);
int __stdio_close(FILE *);
int __overflow(FILE *, int);
int __oflow(FILE *);
int __uflow(FILE *);
int __underflow(FILE *);
int __fseeko(FILE *, off_t, int);
int __fseeko_unlocked(FILE *, off_t, int);
off_t __ftello(FILE *);
off_t __ftello_unlocked(FILE *);
size_t __fwritex(const unsigned char *, size_t, FILE *);
int __putc_unlocked(int, FILE *);
FILE *__fdopen(int, const char *);
extern struct ofl
{
FILE *head;
int lock;
} __ofl;
#define OFLLOCK() LOCK(&__ofl.lock)
#define OFLUNLOCK() UNLOCK(&__ofl.lock)
#define ofl_head (__ofl.head)
#define feof(f) ((f)->flags & F_EOF)
#define ferror(f) ((f)->flags & F_ERR)
/* Caller-allocated FILE * operations */
FILE *__fopen_rb_ca(const char *, FILE *, unsigned char *, size_t);
int __fclose_ca(FILE *);
#endif

11
src/internal/syscall.c Normal file
View File

@ -0,0 +1,11 @@
#include <errno.h>
#include <unistd.h>
long __syscall_ret(unsigned long r)
{
if (r >= (unsigned long)-1 - 4096) {
errno = -(long)r;
return -1;
}
return (long)r;
}

469
src/internal/syscall.h Normal file
View File

@ -0,0 +1,469 @@
#ifndef _SYSCALL_H
#define _SYSCALL_H
#define SYSCALL_LL(x) \
((union { long long ll; long l[2]; }){ .ll = x }).l[0], \
((union { long long ll; long l[2]; }){ .ll = x }).l[1]
#define SYSCALL_SIGSET_SIZE 8
#if defined(SYSCALL_STANDALONE)
#include <errno.h>
static inline long __syscall_ret(unsigned long r)
{
if (r >= (unsigned long)-1 - 4096) {
errno = -(long)r;
return -1;
}
return (long)r;
}
#elif defined(SYSCALL_NORETURN)
static inline long __syscall_ret(unsigned long r)
{
for(;;);
return 0;
}
#elif defined(SYSCALL_RETURN_ERRNO)
static inline long __syscall_ret(unsigned long r)
{
return -r;
}
#else
extern long __syscall_ret(unsigned long);
#endif
#define SYSCALL0 "int $128"
#ifdef __PIC__
#define SYSCALL "xchgl %%ebx,%2\n\t" SYSCALL0 "\n\txchgl %%ebx,%2"
#define EBX "m"
#else
#define SYSCALL SYSCALL0
#define EBX "b"
#endif
static inline long syscall0(long n)
{
unsigned long ret;
__asm__ __volatile__ (SYSCALL0 : "=a"(ret) : "a"(n) : "memory");
return __syscall_ret(ret);
}
static inline long syscall1(long n, long a1)
{
unsigned long ret;
__asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1) : "memory");
return __syscall_ret(ret);
}
static inline long syscall2(long n, long a1, long a2)
{
unsigned long ret;
__asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1), "c"(a2) : "memory");
return __syscall_ret(ret);
}
static inline long syscall3(long n, long a1, long a2, long a3)
{
unsigned long ret;
__asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1), "c"(a2), "d"(a3) : "memory");
return __syscall_ret(ret);
}
static inline long syscall4(long n, long a1, long a2, long a3, long a4)
{
unsigned long ret;
__asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1), "c"(a2), "d"(a3), "S"(a4) : "memory");
return __syscall_ret(ret);
}
static inline long syscall5(long n, long a1, long a2, long a3, long a4, long a5)
{
unsigned long ret;
__asm__ __volatile__ (SYSCALL : "=a"(ret) : "a"(n), EBX(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5) : "memory");
return __syscall_ret(ret);
}
#ifdef __PIC__
/* note: it's probably only safe to use this when a6 is on the stack */
static inline long syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6)
{
unsigned long ret;
__asm__ __volatile__ ("xchgl %%ebx,%2 ; pushl %1 ; pushl %%ebp ; movl %%eax,%%ebp ; movl 4(%%esp),%%eax ; int $128 ; popl %%ebp ; popl %%ecx ; xchgl %%ebx,%2"
: "=a"(ret) : "g"(n), EBX(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5), "a"(a6) : "memory");
return __syscall_ret(ret);
}
#else
static inline long syscall6(long n, long a1, long a2, long a3, long a4, long a5, long a6)
{
unsigned long ret;
__asm__ __volatile__ ("pushl %%ebp ; mov %1, %%ebp ; xchg %%ebp, %7 ; int $128 ; popl %%ebp"
: "=a"(ret) : "g"(n), EBX(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5), "a"(a6) : "memory");
return __syscall_ret(ret);
}
#endif
#define __NR_restart_syscall 0
#define __NR_exit 1
#define __NR_fork 2
#define __NR_read 3
#define __NR_write 4
#define __NR_open 5
#define __NR_close 6
#define __NR_waitpid 7
#define __NR_creat 8
#define __NR_link 9
#define __NR_unlink 10
#define __NR_execve 11
#define __NR_chdir 12
#define __NR_time 13
#define __NR_mknod 14
#define __NR_chmod 15
#define __NR_lchown 16
#define __NR_break 17
#define __NR_oldstat 18
#define __NR_lseek 19
#define __NR_getpid 20
#define __NR_mount 21
#define __NR_umount 22
#define __NR_setuid 23
#define __NR_getuid 24
#define __NR_stime 25
#define __NR_ptrace 26
#define __NR_alarm 27
#define __NR_oldfstat 28
#define __NR_pause 29
#define __NR_utime 30
#define __NR_stty 31
#define __NR_gtty 32
#define __NR_access 33
#define __NR_nice 34
#define __NR_ftime 35
#define __NR_sync 36
#define __NR_kill 37
#define __NR_rename 38
#define __NR_mkdir 39
#define __NR_rmdir 40
#define __NR_dup 41
#define __NR_pipe 42
#define __NR_times 43
#define __NR_prof 44
#define __NR_brk 45
#define __NR_setgid 46
#define __NR_getgid 47
#define __NR_signal 48
#define __NR_geteuid 49
#define __NR_getegid 50
#define __NR_acct 51
#define __NR_umount2 52
#define __NR_lock 53
#define __NR_ioctl 54
#define __NR_fcntl 55
#define __NR_mpx 56
#define __NR_setpgid 57
#define __NR_ulimit 58
#define __NR_oldolduname 59
#define __NR_umask 60
#define __NR_chroot 61
#define __NR_ustat 62
#define __NR_dup2 63
#define __NR_getppid 64
#define __NR_getpgrp 65
#define __NR_setsid 66
#define __NR_sigaction 67
#define __NR_sgetmask 68
#define __NR_ssetmask 69
#define __NR_setreuid 70
#define __NR_setregid 71
#define __NR_sigsuspend 72
#define __NR_sigpending 73
#define __NR_sethostname 74
#define __NR_setrlimit 75
#define __NR_getrlimit 76 /* Back compatible 2Gig limited rlimit */
#define __NR_getrusage 77
#define __NR_gettimeofday 78
#define __NR_settimeofday 79
#define __NR_getgroups 80
#define __NR_setgroups 81
#define __NR_select 82
#define __NR_symlink 83
#define __NR_oldlstat 84
#define __NR_readlink 85
#define __NR_uselib 86
#define __NR_swapon 87
#define __NR_reboot 88
#define __NR_readdir 89
#define __NR_mmap 90
#define __NR_munmap 91
#define __NR_truncate 92
#define __NR_ftruncate 93
#define __NR_fchmod 94
#define __NR_fchown 95
#define __NR_getpriority 96
#define __NR_setpriority 97
#define __NR_profil 98
#define __NR_statfs 99
#define __NR_fstatfs 100
#define __NR_ioperm 101
#define __NR_socketcall 102
#define __NR_syslog 103
#define __NR_setitimer 104
#define __NR_getitimer 105
#define __NR_stat 106
#define __NR_lstat 107
#define __NR_fstat 108
#define __NR_olduname 109
#define __NR_iopl 110
#define __NR_vhangup 111
#define __NR_idle 112
#define __NR_vm86old 113
#define __NR_wait4 114
#define __NR_swapoff 115
#define __NR_sysinfo 116
#define __NR_ipc 117
#define __NR_fsync 118
#define __NR_sigreturn 119
#define __NR_clone 120
#define __NR_setdomainname 121
#define __NR_uname 122
#define __NR_modify_ldt 123
#define __NR_adjtimex 124
#define __NR_mprotect 125
#define __NR_sigprocmask 126
#define __NR_create_module 127
#define __NR_init_module 128
#define __NR_delete_module 129
#define __NR_get_kernel_syms 130
#define __NR_quotactl 131
#define __NR_getpgid 132
#define __NR_fchdir 133
#define __NR_bdflush 134
#define __NR_sysfs 135
#define __NR_personality 136
#define __NR_afs_syscall 137
#define __NR_setfsuid 138
#define __NR_setfsgid 139
#define __NR__llseek 140
#define __NR_getdents 141
#define __NR__newselect 142
#define __NR_flock 143
#define __NR_msync 144
#define __NR_readv 145
#define __NR_writev 146
#define __NR_getsid 147
#define __NR_fdatasync 148
#define __NR__sysctl 149
#define __NR_mlock 150
#define __NR_munlock 151
#define __NR_mlockall 152
#define __NR_munlockall 153
#define __NR_sched_setparam 154
#define __NR_sched_getparam 155
#define __NR_sched_setscheduler 156
#define __NR_sched_getscheduler 157
#define __NR_sched_yield 158
#define __NR_sched_get_priority_max 159
#define __NR_sched_get_priority_min 160
#define __NR_sched_rr_get_interval 161
#define __NR_nanosleep 162
#define __NR_mremap 163
#define __NR_setresuid 164
#define __NR_getresuid 165
#define __NR_vm86 166
#define __NR_query_module 167
#define __NR_poll 168
#define __NR_nfsservctl 169
#define __NR_setresgid 170
#define __NR_getresgid 171
#define __NR_prctl 172
#define __NR_rt_sigreturn 173
#define __NR_rt_sigaction 174
#define __NR_rt_sigprocmask 175
#define __NR_rt_sigpending 176
#define __NR_rt_sigtimedwait 177
#define __NR_rt_sigqueueinfo 178
#define __NR_rt_sigsuspend 179
#define __NR_pread64 180
#define __NR_pwrite64 181
#define __NR_chown 182
#define __NR_getcwd 183
#define __NR_capget 184
#define __NR_capset 185
#define __NR_sigaltstack 186
#define __NR_sendfile 187
#define __NR_getpmsg 188
#define __NR_putpmsg 189
#define __NR_vfork 190
#define __NR_ugetrlimit 191
#define __NR_mmap2 192
#define __NR_truncate64 193
#define __NR_ftruncate64 194
#define __NR_stat64 195
#define __NR_lstat64 196
#define __NR_fstat64 197
#define __NR_lchown32 198
#define __NR_getuid32 199
#define __NR_getgid32 200
#define __NR_geteuid32 201
#define __NR_getegid32 202
#define __NR_setreuid32 203
#define __NR_setregid32 204
#define __NR_getgroups32 205
#define __NR_setgroups32 206
#define __NR_fchown32 207
#define __NR_setresuid32 208
#define __NR_getresuid32 209
#define __NR_setresgid32 210
#define __NR_getresgid32 211
#define __NR_chown32 212
#define __NR_setuid32 213
#define __NR_setgid32 214
#define __NR_setfsuid32 215
#define __NR_setfsgid32 216
#define __NR_pivot_root 217
#define __NR_mincore 218
#define __NR_madvise 219
#define __NR_madvise1 219
#define __NR_getdents64 220
#define __NR_fcntl64 221
/* 223 is unused */
#define __NR_gettid 224
#define __NR_readahead 225
#define __NR_setxattr 226
#define __NR_lsetxattr 227
#define __NR_fsetxattr 228
#define __NR_getxattr 229
#define __NR_lgetxattr 230
#define __NR_fgetxattr 231
#define __NR_listxattr 232
#define __NR_llistxattr 233
#define __NR_flistxattr 234
#define __NR_removexattr 235
#define __NR_lremovexattr 236
#define __NR_fremovexattr 237
#define __NR_tkill 238
#define __NR_sendfile64 239
#define __NR_futex 240
#define __NR_sched_setaffinity 241
#define __NR_sched_getaffinity 242
#define __NR_set_thread_area 243
#define __NR_get_thread_area 244
#define __NR_io_setup 245
#define __NR_io_destroy 246
#define __NR_io_getevents 247
#define __NR_io_submit 248
#define __NR_io_cancel 249
#define __NR_fadvise64 250
/* 251 is available for reuse (was briefly sys_set_zone_reclaim) */
#define __NR_exit_group 252
#define __NR_lookup_dcookie 253
#define __NR_epoll_create 254
#define __NR_epoll_ctl 255
#define __NR_epoll_wait 256
#define __NR_remap_file_pages 257
#define __NR_set_tid_address 258
#define __NR_timer_create 259
#define __NR_timer_settime (__NR_timer_create+1)
#define __NR_timer_gettime (__NR_timer_create+2)
#define __NR_timer_getoverrun (__NR_timer_create+3)
#define __NR_timer_delete (__NR_timer_create+4)
#define __NR_clock_settime (__NR_timer_create+5)
#define __NR_clock_gettime (__NR_timer_create+6)
#define __NR_clock_getres (__NR_timer_create+7)
#define __NR_clock_nanosleep (__NR_timer_create+8)
#define __NR_statfs64 268
#define __NR_fstatfs64 269
#define __NR_tgkill 270
#define __NR_utimes 271
#define __NR_fadvise64_64 272
#define __NR_vserver 273
#define __NR_mbind 274
#define __NR_get_mempolicy 275
#define __NR_set_mempolicy 276
#define __NR_mq_open 277
#define __NR_mq_unlink (__NR_mq_open+1)
#define __NR_mq_timedsend (__NR_mq_open+2)
#define __NR_mq_timedreceive (__NR_mq_open+3)
#define __NR_mq_notify (__NR_mq_open+4)
#define __NR_mq_getsetattr (__NR_mq_open+5)
#define __NR_kexec_load 283
#define __NR_waitid 284
/* #define __NR_sys_setaltroot 285 */
#define __NR_add_key 286
#define __NR_request_key 287
#define __NR_keyctl 288
#define __NR_ioprio_set 289
#define __NR_ioprio_get 290
#define __NR_inotify_init 291
#define __NR_inotify_add_watch 292
#define __NR_inotify_rm_watch 293
#define __NR_migrate_pages 294
#define __NR_openat 295
#define __NR_mkdirat 296
#define __NR_mknodat 297
#define __NR_fchownat 298
#define __NR_futimesat 299
#define __NR_fstatat64 300
#define __NR_unlinkat 301
#define __NR_renameat 302
#define __NR_linkat 303
#define __NR_symlinkat 304
#define __NR_readlinkat 305
#define __NR_fchmodat 306
#define __NR_faccessat 307
#define __NR_pselect6 308
#define __NR_ppoll 309
#define __NR_unshare 310
#define __NR_set_robust_list 311
#define __NR_get_robust_list 312
#define __NR_splice 313
#define __NR_sync_file_range 314
#define __NR_tee 315
#define __NR_vmsplice 316
#define __NR_move_pages 317
#define __NR_getcpu 318
#define __NR_epoll_pwait 319
#define __NR_utimensat 320
#define __NR_signalfd 321
#define __NR_timerfd_create 322
#define __NR_eventfd 323
#define __NR_fallocate 324
#define __NR_timerfd_settime 325
#define __NR_timerfd_gettime 326
#define __NR_signalfd4 327
#define __NR_eventfd2 328
#define __NR_epoll_create1 329
#define __NR_dup3 330
#define __NR_pipe2 331
#define __NR_inotify_init1 332
#define __NR_preadv 333
#define __NR_pwritev 334
#undef O_LARGEFILE
#define O_LARGEFILE 0100000
/* the following are needed for iso c functions to use */
#define __syscall_open(filename, flags, mode) syscall3(__NR_open, (long)(filename), (flags)|O_LARGEFILE, (mode))
#define __syscall_read(fd, buf, len) syscall3(__NR_read, (fd), (long)(buf), (len))
#define __syscall_write(fd, buf, len) syscall3(__NR_write, (fd), (long)(buf), (len))
#define __syscall_close(fd) syscall1(__NR_close, (fd))
#define __syscall_fcntl(fd, cmd, arg) syscall3(__NR_fcntl64, (fd), (cmd), (long)(arg))
#define __syscall_dup2(old, new) syscall2(__NR_dup2, (old), (new))
#define __syscall_unlink(path) syscall1(__NR_unlink, (long)(path))
#define __syscall_getpid() syscall0(__NR_getpid)
#define __syscall_kill(pid,sig) syscall2(__NR_kill, (pid), (sig))
#define __syscall_sigaction(sig,new,old) syscall4(__NR_rt_sigaction, (sig), (long)(new), (long)(old), SYSCALL_SIGSET_SIZE)
#define __syscall_ioctl(fd,ioc,arg) syscall3(__NR_ioctl, (fd), (ioc), (long)(arg))
#define __syscall_exit(code) syscall1(__NR_exit, code)
#define __NEED_off_t
#include <bits/alltypes.h>
static inline off_t __syscall_lseek(int fd, off_t offset, int whence)
{
off_t result;
return syscall5(__NR__llseek, fd, offset>>32, offset, (long)&result, whence) ? -1 : result;
}
#endif

5
src/internal/util.h Normal file
View File

@ -0,0 +1,5 @@
#ifndef _INTERNAL_UTIL_H
#define _INTERNAL_UTIL_H
#endif

10
src/ipc/ftok.c Normal file
View File

@ -0,0 +1,10 @@
#include <sys/ipc.h>
#include <sys/stat.h>
key_t ftok(const char *path, int id)
{
struct stat st;
if (stat(path, &st) < 0) return -1;
return ((st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16) | ((id & 0xff) << 24));
}

13
src/ipc/ipc.h Normal file
View File

@ -0,0 +1,13 @@
#define IPCOP_semop 1
#define IPCOP_semget 2
#define IPCOP_semctl 3
#define IPCOP_msgsnd 11
#define IPCOP_msgrcv 12
#define IPCOP_msgget 13
#define IPCOP_msgctl 14
#define IPCOP_shmat 21
#define IPCOP_shmdt 22
#define IPCOP_shmget 23
#define IPCOP_shmctl 24
#define IPC_MODERN 0x100

18
src/ipc/semctl.c Normal file
View File

@ -0,0 +1,18 @@
#include <sys/sem.h>
#include <stdarg.h>
#include "syscall.h"
#include "ipc.h"
int semctl(int id, int num, int cmd, ...)
{
long arg;
va_list ap;
va_start(ap, cmd);
arg = va_arg(ap, long);
va_end(ap);
#ifdef __NR_semctl
return syscall4(__NR_semctl, id, num, cmd, arg);
#else
return syscall5(__NR_ipc, IPCOP_semctl, id, num, cmd | 0x100, (long)&arg);
#endif
}

12
src/ipc/semget.c Normal file
View File

@ -0,0 +1,12 @@
#include <sys/sem.h>
#include "syscall.h"
#include "ipc.h"
int semget(key_t key, int n, int fl)
{
#ifdef __NR_semget
return syscall3(__NR_semget, key, n, fl);
#else
return syscall4(__NR_ipc, IPCOP_semget, key, n, fl);
#endif
}

12
src/ipc/semop.c Normal file
View File

@ -0,0 +1,12 @@
#include <sys/sem.h>
#include "syscall.h"
#include "ipc.h"
int semop(int id, struct sembuf *buf, size_t n)
{
#ifdef __NR_semop
return syscall3(__NR_semop, id, (long)buf, n);
#else
return syscall5(__NR_ipc, IPCOP_semop, id, n, 0, (long)buf);
#endif
}

17
src/ipc/shmat.c Normal file
View File

@ -0,0 +1,17 @@
#include <sys/shm.h>
#include "syscall.h"
#include "ipc.h"
#ifdef __NR_shmat
void *shmat(int id, const void *addr, int flag)
{
return syscall3(__NR_shmat, id, (long)addr, flag);
}
#else
void *shmat(int id, const void *addr, int flag)
{
unsigned long ret;
ret = syscall5(__NR_ipc, IPCOP_shmat, id, flag, (long)&addr, (long)addr);
return (ret > -(unsigned long)SHMLBA) ? (void *)ret : (void *)addr;
}
#endif

12
src/ipc/shmctl.c Normal file
View File

@ -0,0 +1,12 @@
#include <sys/shm.h>
#include "syscall.h"
#include "ipc.h"
int shmctl(int id, int cmd, struct shmid_ds *buf)
{
#ifdef __NR_shmctl
return syscall3(__NR_shmctl, id, cmd, (long)buf);
#else
return syscall4(__NR_ipc, IPCOP_shmctl, id, cmd | IPC_MODERN, (long)buf);
#endif
}

12
src/ipc/shmdt.c Normal file
View File

@ -0,0 +1,12 @@
#include <sys/shm.h>
#include "syscall.h"
#include "ipc.h"
int shmdt(const void *addr)
{
#ifdef __NR_shmdt
return syscall1(__NR_shmdt, (long)addr);
#else
return syscall2(__NR_ipc, IPCOP_shmdt, (long)addr);
#endif
}

12
src/ipc/shmget.c Normal file
View File

@ -0,0 +1,12 @@
#include <sys/shm.h>
#include "syscall.h"
#include "ipc.h"
int shmget(key_t key, size_t size, int flag)
{
#ifdef __NR_shmget
return syscall3(__NR_shmget, key, size, flag);
#else
return syscall4(__NR_ipc, IPCOP_shmget, key, size, flag);
#endif
}

6
src/linux/brk.c Normal file
View File

@ -0,0 +1,6 @@
#include "syscall.h"
int brk(void *end)
{
return -(syscall1(__NR_brk, (long)end) == -1);
}

8
src/linux/chroot.c Normal file
View File

@ -0,0 +1,8 @@
#include <unistd.h>
#define SYSCALL_STANDALONE
#include "syscall.h"
int chroot(const char *path)
{
return syscall1(__NR_chroot, (long)path);
}

31
src/linux/daemon.c Normal file
View File

@ -0,0 +1,31 @@
#include <fcntl.h>
#include <unistd.h>
int daemon(int nochdir, int noclose)
{
int fd;
switch(fork()) {
case 0: break;
case -1: return -1;
default: _exit(0);
}
if (setsid() < 0) return -1;
switch(fork()) {
case 0: break;
case -1: return -1;
default: _exit(0);
}
if (!nochdir) chdir("/");
if (!noclose && (fd = open("/dev/null", O_RDWR)) >= 0) {
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2) close(fd);
}
return 0;
}

7
src/linux/epoll_create.c Normal file
View File

@ -0,0 +1,7 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_create(int size)
{
return syscall1(__NR_epoll_create, size);
}

View File

@ -0,0 +1,7 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_create1(int flags)
{
return syscall1(__NR_epoll_create1, flags);
}

7
src/linux/epoll_ctl.c Normal file
View File

@ -0,0 +1,7 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_ctl(int fd, int op, int fd2, struct epoll_event *ev)
{
return syscall4(__NR_epoll_ctl, fd, op, fd2, (long)ev);
}

7
src/linux/epoll_pwait.c Normal file
View File

@ -0,0 +1,7 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_pwait(int fd, struct epoll_event *ev, int cnt, int to, const sigset_t *sigs)
{
return syscall6(__NR_epoll_pwait, fd, (long)ev, cnt, to, (long)sigs, 8);
}

7
src/linux/epoll_wait.c Normal file
View File

@ -0,0 +1,7 @@
#include <sys/epoll.h>
#include "syscall.h"
int epoll_wait(int fd, struct epoll_event *ev, int cnt, int to)
{
return syscall4(__NR_epoll_wait, fd, (long)ev, cnt, to);
}

View File

@ -0,0 +1,9 @@
#include <limits.h>
#include <sys/resource.h>
int getdtablesize(void)
{
struct rlimit rl;
getrlimit(RLIMIT_NOFILE, &rl);
return rl.rlim_max < INT_MAX ? rl.rlim_max : INT_MAX;
}

4
src/linux/gethostid.c Normal file
View File

@ -0,0 +1,4 @@
long gethostid()
{
return 0;
}

52
src/linux/getopt_long.c Normal file
View File

@ -0,0 +1,52 @@
#define _GNU_SOURCE
#include <stddef.h>
#include <getopt.h>
#include <stdio.h>
static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
{
if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1;
if ((longonly && argv[optind][1]) ||
(argv[optind][1] == '-' && argv[optind][2]))
{
int i;
for (i=0; longopts[i].name; i++) {
const char *name = longopts[i].name;
char *opt = argv[optind]+1;
if (*opt == '-') opt++;
while (*name && *name++ == *opt++);
if (*name || (*opt && *opt != '=')) continue;
if (*opt == '=') {
if (!longopts[i].has_arg) continue;
optarg = opt+1;
} else {
if (longopts[i].has_arg == required_argument) {
if (!(optarg = argv[++optind]))
return ':';
} else optarg = NULL;
}
optind++;
if (idx) *idx = i;
if (longopts[i].flag) {
*longopts[i].flag = longopts[i].val;
return 0;
}
return longopts[i].val;
}
if (argv[optind][1] == '-') {
optind++;
return '?';
}
}
return getopt(argc, argv, optstring);
}
int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
{
return __getopt_long(argc, argv, optstring, longopts, idx, 0);
}
int getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
{
return __getopt_long(argc, argv, optstring, longopts, idx, 1);
}

Some files were not shown because too many files have changed in this diff Show More