working on it

This commit is contained in:
Dawid Sobczak 2025-04-05 10:55:40 +01:00
parent 56a6e78765
commit 35a88970c2
1094 changed files with 51093 additions and 51 deletions

View file

@ -0,0 +1,17 @@
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
size_t confstr(int name, char *buf, size_t len)
{
const char *s = "";
if (!name) {
s = "/bin:/usr/bin";
} else if ((name&~4U)!=1 && name-_CS_POSIX_V6_ILP32_OFF32_CFLAGS>31U) {
errno = EINVAL;
return 0;
}
// snprintf is overkill but avoid wasting code size to implement
// this completely useless function and its truncation semantics
return snprintf(buf, len, "%s", s);
}

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];
}

View file

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

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;
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

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);
}

View file

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

View file

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

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;
}

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;
}

View file

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

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);
}

View file

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

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;
}

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;
}

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;
}

View file

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

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;
}

View file

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

View file

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

View file

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

View file

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

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);
}

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;
}

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;
}

View file

@ -0,0 +1,186 @@
#include <inttypes.h>
#include <wchar.h>
#include <wctype.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);
}

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];
};

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_getdents, fd, (long)buf, len);
}
weak_alias(__getdents, getdents);
LFS64(getdents);

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);

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;
}

View file

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

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;
}

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;
}

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);

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);

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);
}

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);

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);
}

View file

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

7
05/musl-final/src/env/__environ.c vendored Normal file
View file

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

View file

@ -0,0 +1,30 @@
#include "libc.h"
#if 0
/* Any use of __environ/environ will override this symbol. */
char **__dummy_environ = (void *)-1;
weak_alias(__dummy_environ, ___environ);
#else
extern char **___environ;
#endif
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
05/musl-final/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
05/musl-final/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
05/musl-final/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
05/musl-final/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
05/musl-final/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;
}

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")

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;
}

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);
}

View file

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

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();
}

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;
}

View file

@ -0,0 +1,33 @@
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include "libc.h"
#if 0
/* __overflow.c and atexit.c override these */
static int (*const dummy)() = 0;
weak_alias(dummy, __funcs_on_exit);
weak_alias(dummy, __fflush_on_exit);
#else
extern int (*const __funcs_on_exit)(void);
extern int (*const __fflush_on_exit)(FILE *);
#endif
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(;;);
}

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);

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);

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);

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);

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

View file

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

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,79 @@
#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;
unsigned long tlsdesc[4];
pid_t tid, pid;
int tsd_used, errno_val, *errno_ptr;
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;
struct __ptcb *cancelbuf;
void **tsd;
pthread_attr_t attr;
};
#define __SU (sizeof(size_t)/sizeof(int))
#define _a_stacksize __u.__s[0]
#define _a_guardsize __u.__s[1]
#define _a_detach __u.__i[2*__SU+0]
#define _m_type __u.__i[0]
#define _m_lock __u.__i[1]
#define _m_waiters __u.__i[2]
#define _m_owner __u.__i[3]
#define _c_block __u.__i[0]
#define _rw_wrlock __u.__i[0]
#define _rw_readers __u.__i[1]
#define _rw_waiters __u.__i[2]
#define _rw_owner __u.__i[3]
#define _b_count __u.__i[0]
#define _b_limit __u.__i[1]
#define _b_left __u.__i[2]
#define _b_waiters __u.__i[3]
#include "pthread_arch.h"
#define SIGCANCEL 32
#define SIGSYSCALL 33
#define SIGTIMER 32 /* ?? */
int __set_thread_area(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

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

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;
}

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));
}

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

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
}

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
}

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
}

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 (void *)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

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
}

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
}

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
}

View file

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

View file

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

View file

@ -0,0 +1,32 @@
#include <fcntl.h>
#include <unistd.h>
int daemon(int nochdir, int noclose)
{
if (!nochdir && chdir("/"))
return -1;
if (!noclose) {
int fd, failed = 0;
if ((fd = open("/dev/null", O_RDWR)) < 0) return -1;
if (dup2(fd, 0) < 0 || dup2(fd, 1) < 0 || dup2(fd, 2) < 0)
failed++;
if (fd > 2) close(fd);
if (failed) return -1;
}
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);
}
return 0;
}

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);
}

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);
}

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);
}

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;
}

View file

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

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);
}

View file

@ -0,0 +1,7 @@
#include <unistd.h>
#include <limits.h>
int getpagesize(void)
{
return PAGE_SIZE;
}

View file

@ -0,0 +1,39 @@
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
char *getpass(const char *prompt)
{
int fd;
struct termios s, t;
ssize_t l;
static char password[128];
if ((fd = open("/dev/tty", O_RDONLY|O_NOCTTY)) < 0) fd = 0;
tcgetattr(fd, &t);
s = t;
t.c_lflag &= ~(ECHO|ISIG);
t.c_lflag |= ICANON;
t.c_iflag &= ~(INLCR|IGNCR);
t.c_iflag |= ICRNL;
tcsetattr(fd, TCSAFLUSH, &t);
tcdrain(fd);
fputs(prompt, stderr);
fflush(stderr);
l = read(fd, password, sizeof password);
if (l >= 0) {
if (l > 0 && password[l-1] == '\n') l--;
password[l] = 0;
}
tcsetattr(fd, TCSAFLUSH, &s);
if (fd > 2) close(fd);
return password;
}

View file

@ -0,0 +1,15 @@
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <limits.h>
int getgrouplist(const char *, gid_t, gid_t *, int *);
int setgroups(size_t, const gid_t *);
int initgroups(const char *user, gid_t gid)
{
gid_t groups[NGROUPS_MAX];
int count;
if (getgrouplist(user, gid, groups, &count) < 0) return -1;
return setgroups(count, groups);
}

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