Skip to content
Snippets Groups Projects
Commit 05521dc5 authored by Christoph Hellwig's avatar Christoph Hellwig
Browse files

add misc routines from musl

parent e72713ff
No related branches found
No related tags found
No related merge requests found
libc := libc :=
libc += getopt.o
libc += ctype/__ctype_b_loc.o libc += ctype/__ctype_b_loc.o
libc += ctype/__ctype_get_mb_cur_max.o libc += ctype/__ctype_get_mb_cur_max.o
libc += ctype/__ctype_tolower_loc.o libc += ctype/__ctype_tolower_loc.o
...@@ -99,6 +97,19 @@ libc += locale/wcsxfrm_l.o ...@@ -99,6 +97,19 @@ libc += locale/wcsxfrm_l.o
libc += locale/wctrans_l.o libc += locale/wctrans_l.o
libc += locale/wctype_l.o libc += locale/wctype_l.o
libc += misc/a64l.o
libc += misc/basename.o
libc += misc/dirname.o
libc += misc/ffs.o
libc += misc/get_current_dir_name.o
libc += misc/gethostid.o
libc += misc/getopt.o
libc += misc/getopt_long.o
libc += misc/getresuid.o
libc += misc/getresgid.o
libc += misc/getsubopt.o
libc += misc/setdomainname.o
libc += multibyte/btowc.o libc += multibyte/btowc.o
libc += multibyte/internal.o libc += multibyte/internal.o
libc += multibyte/mblen.o libc += multibyte/mblen.o
...@@ -224,4 +235,3 @@ libc += time.o ...@@ -224,4 +235,3 @@ libc += time.o
libc += signal.o libc += signal.o
libc += mman.o libc += mman.o
libc += qsort.o libc += qsort.o
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
static const char digits[] =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
long a64l(const char *s)
{
int e;
uint32_t x = 0;
for (e=0; e<36 && *s; e+=6, s++)
x |= (strchr(digits, *s)-digits)<<e;
return x;
}
char *l64a(long x0)
{
static char s[7];
char *p;
uint32_t x = x0;
for (p=s; x; p++, x>>=6)
*p = digits[x&63];
*p = 0;
return s;
}
#include <string.h>
#include "libc.h"
char *basename(char *s)
{
size_t i;
if (!s || !*s) return ".";
i = strlen(s)-1;
for (; i&&s[i]=='/'; i--) s[i] = 0;
for (; i&&s[i-1]!='/'; i--);
return s+i;
}
weak_alias(basename, __xpg_basename);
#include <string.h>
#include <libgen.h>
char *dirname(char *s)
{
size_t i;
if (!s || !*s) return ".";
i = strlen(s)-1;
for (; s[i]=='/'; i--) if (!i) return "/";
for (; s[i]!='/'; i--) if (!i) return ".";
for (; s[i]=='/'; i--) if (!i) return "/";
s[i+1] = 0;
return s;
}
#include <strings.h>
#include "atomic.h"
int ffs(int i)
{
return i ? a_ctz_l(i)+1 : 0;
}
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
char *get_current_dir_name(void) {
struct stat a, b;
char buf[PATH_MAX];
char *res = getenv("PWD");
if (res && *res && !stat(res, &a) && !stat(".", &b)
&& (a.st_dev == b.st_dev) && (a.st_ino == b.st_ino))
return strdup(res);
if(!getcwd(buf, sizeof(buf))) return NULL;
return strdup(buf);
}
long gethostid()
{
return 0;
}
#include <unistd.h>
#include <wchar.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include "libc.h"
char *optarg;
int optind=1, opterr=1, optopt, __optpos, __optreset=0;
#define optpos __optpos
weak_alias(__optreset, optreset);
int getopt(int argc, char * const argv[], const char *optstring)
{
int i;
wchar_t c, d;
int k, l;
char *optchar;
if (!optind || __optreset) {
__optreset = 0;
__optpos = 0;
optind = 1;
}
if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
return -1;
if (argv[optind][1] == '-' && !argv[optind][2])
return optind++, -1;
if (!optpos) optpos++;
if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
k = 1;
c = 0xfffd; /* replacement char */
}
optchar = argv[optind]+optpos;
optopt = c;
optpos += k;
if (!argv[optind][optpos]) {
optind++;
optpos = 0;
}
for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1);
if (d != c) {
if (optstring[0] != ':' && opterr) {
write(2, argv[0], strlen(argv[0]));
write(2, ": illegal option: ", 18);
write(2, optchar, k);
write(2, "\n", 1);
}
return '?';
}
if (optstring[i+1] == ':') {
if (optind >= argc) {
if (optstring[0] == ':') return ':';
if (opterr) {
write(2, argv[0], strlen(argv[0]));
write(2, ": option requires an argument: ", 31);
write(2, optchar, k);
write(2, "\n", 1);
}
return '?';
}
optarg = argv[optind++] + optpos;
optpos = 0;
}
return c;
}
#define _GNU_SOURCE
#include <stddef.h>
#include <getopt.h>
#include <stdio.h>
extern int __optpos, __optreset;
static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
{
if (!optind || __optreset) {
__optreset = 0;
__optpos = 0;
optind = 1;
}
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++;
for (; *name && *name == *opt; 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);
}
#define _GNU_SOURCE
#include <unistd.h>
#include "syscall.h"
int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
{
return syscall(SYS_getresgid, rgid, egid, sgid);
}
#define _GNU_SOURCE
#include <unistd.h>
#include "syscall.h"
int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
{
return syscall(SYS_getresuid, ruid, euid, suid);
}
#include <stdlib.h>
#include <string.h>
int getsubopt(char **opt, char *const *keys, char **val)
{
char *s = *opt;
int i;
*val = NULL;
*opt = strchr(s, ',');
if (*opt) *(*opt)++ = 0;
else *opt = s + strlen(s);
for (i=0; keys[i]; i++) {
size_t l = strlen(keys[i]);
if (strncmp(keys[i], s, l)) continue;
if (s[l] == '=')
*val = s + l;
else if (s[l]) continue;
return i;
}
return -1;
}
#define _GNU_SOURCE
#include <unistd.h>
#include "syscall.h"
int setdomainname(const char *name, size_t len)
{
return syscall(SYS_setdomainname, name, len);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment