diff options
| author | Roger A. Faulkner <Roger.Faulkner@Oracle.COM> | 2010-08-12 14:55:22 -0700 |
|---|---|---|
| committer | Roger A. Faulkner <Roger.Faulkner@Oracle.COM> | 2010-08-12 14:55:22 -0700 |
| commit | 23a1ccea6aac035f084a7a4cdc968687d1b02daf (patch) | |
| tree | 6ed5e310ce6dd96f997b0c0f9735805d513d898a /usr/src/lib/libc/port/gen | |
| parent | 29c3196fe2acc65721d8b9b5ea708d3a87facde0 (diff) | |
| download | illumos-joyent-23a1ccea6aac035f084a7a4cdc968687d1b02daf.tar.gz | |
PSARC 2010/299 GNU/Linux/BSD compatibility functions
6960818 add get_nprocs(), getline(), strdupa(), strndup() to libc
6901783 strndup would be nice
6824404 libc should provide ffsl() & ffsll()
6793969 RFE: Add|stpcpy|to libc
6735446 Want a __progname symbol for BSD-style source compatibility
6421095 Solaris should provide strcasestr
6275498 Provide string compare functions wcscasecmp,wcsncasecmp in solaris like linux
--HG--
rename : usr/src/lib/libc/port/gen/strcasecmp.c => usr/src/lib/libc/port/gen/ascii_strcasecmp.c
rename : usr/src/lib/libc/port/gen/strncasecmp.c => usr/src/lib/libc/port/gen/ascii_strncasecmp.c
rename : usr/src/lib/libc/sparc/gen/strcasecmp.s => usr/src/lib/libc/sparc/gen/ascii_strcasecmp.s
rename : usr/src/lib/libc/sparcv9/gen/strcasecmp.s => usr/src/lib/libc/sparcv9/gen/ascii_strcasecmp.s
Diffstat (limited to 'usr/src/lib/libc/port/gen')
20 files changed, 701 insertions, 121 deletions
diff --git a/usr/src/lib/libc/port/gen/strcasecmp.c b/usr/src/lib/libc/port/gen/ascii_strcasecmp.c index c8e7ba62ac..b04f562c65 100644 --- a/usr/src/lib/libc/port/gen/strcasecmp.c +++ b/usr/src/lib/libc/port/gen/ascii_strcasecmp.c @@ -20,8 +20,7 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ @@ -32,8 +31,6 @@ * under license from the Regents of the University of California. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include "lint.h" #include <sys/types.h> #include <strings.h> @@ -47,7 +44,7 @@ extern const char strcase_charmap[]; int -strcasecmp(const char *s1, const char *s2) +ascii_strcasecmp(const char *s1, const char *s2) { const unsigned char *cm = (const unsigned char *)strcase_charmap; const unsigned char *us1 = (const unsigned char *)s1; diff --git a/usr/src/lib/libc/port/gen/strncasecmp.c b/usr/src/lib/libc/port/gen/ascii_strncasecmp.c index d06b7ac9c4..33a8baf173 100644 --- a/usr/src/lib/libc/port/gen/strncasecmp.c +++ b/usr/src/lib/libc/port/gen/ascii_strncasecmp.c @@ -20,8 +20,7 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ @@ -32,8 +31,6 @@ * under license from the Regents of the University of California. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include "lint.h" #include <sys/types.h> #include <strings.h> @@ -47,7 +44,7 @@ extern const char strcase_charmap[]; int -strncasecmp(const char *s1, const char *s2, size_t n) +ascii_strncasecmp(const char *s1, const char *s2, size_t n) { const unsigned char *cm = (const unsigned char *)strcase_charmap; const unsigned char *us1 = (const unsigned char *)s1; diff --git a/usr/src/lib/libc/port/gen/err.c b/usr/src/lib/libc/port/gen/err.c index 016d5e0b89..7c3695e785 100644 --- a/usr/src/lib/libc/port/gen/err.c +++ b/usr/src/lib/libc/port/gen/err.c @@ -20,15 +20,13 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include "lint.h" #include "file64.h" #include "mtlib.h" +#include "thr_uberdata.h" #include <sys/types.h> #include <err.h> #include <stdio.h> @@ -36,11 +34,51 @@ #include <stdarg.h> #include <string.h> #include <errno.h> +#include <dlfcn.h> #include "stdiom.h" /* Function exit/warning functions and global variables. */ -static const char *progname; +const char *__progname; /* GNU/Linux/BSD compatibility */ + +#define PROGNAMESIZE 128 /* buffer size for __progname */ + +const char * +getprogname(void) +{ + return (__progname); +} + +void +setprogname(const char *argv0) +{ + uberdata_t *udp = curthread->ul_uberdata; + const char *progname; + + if ((progname = strrchr(argv0, '/')) == NULL) + progname = argv0; + else + progname++; + + if (udp->progname == NULL) + udp->progname = lmalloc(PROGNAMESIZE); + (void) strlcpy(udp->progname, progname, PROGNAMESIZE); + __progname = udp->progname; +} + +/* called only from libc_init() */ +void +init_progname(void) +{ + Dl_argsinfo_t args; + const char *argv0; + + if (dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &args) < 0) + argv0 = "UNKNOWN"; + else + argv0 = args.dla_argv[0]; + setprogname(argv0); +} /* * warncore() is the workhorse of these functions. Everything else has @@ -49,22 +87,12 @@ static const char *progname; static rmutex_t * warncore(FILE *fp, const char *fmt, va_list args) { - const char *execname; rmutex_t *lk; FLOCKFILE(lk, fp); - if (progname == NULL) { - execname = getexecname(); - if ((execname != NULL) && - ((progname = strrchr(execname, '/')) != NULL)) - progname++; - else - progname = execname; - } - - if (progname != NULL) - (void) fprintf(fp, "%s: ", progname); + if (__progname != NULL) + (void) fprintf(fp, "%s: ", __progname); if (fmt != NULL) { (void) vfprintf(fp, fmt, args); diff --git a/usr/src/lib/libc/port/gen/ffs.c b/usr/src/lib/libc/port/gen/ffs.c new file mode 100644 index 0000000000..ff787de122 --- /dev/null +++ b/usr/src/lib/libc/port/gen/ffs.c @@ -0,0 +1,83 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + */ + +#include "lint.h" +#include <string.h> +#include <strings.h> +#include <sys/types.h> + +static int +ffs_impl(uint64_t bits) +{ + int i = 1; + uint32_t bits32; + + if (bits == 0) + return (0); + + if ((bits32 = (uint32_t)bits) == 0) { + bits32 = (uint32_t)(bits >> 32); + i += 32; + } + + if ((bits32 & 0xffff) == 0) { + bits32 >>= 16; + i += 16; + } + if ((bits32 & 0xff) == 0) { + bits32 >>= 8; + i += 8; + } + if ((bits32 & 0xf) == 0) { + bits32 >>= 4; + i += 4; + } + if ((bits32 & 0x3) == 0) { + bits32 >>= 2; + i += 2; + } + if ((bits32 & 0x1) == 0) + i += 1; + + return (i); +} + +int +ffs(int bits) +{ + return (ffs_impl((uint64_t)(uint_t)bits)); +} + +int +ffsl(long bits) +{ + return (ffs_impl((uint64_t)(ulong_t)bits)); +} + +int +ffsll(long long bits) +{ + return (ffs_impl((uint64_t)(u_longlong_t)bits)); +} diff --git a/usr/src/lib/libc/port/gen/fls.c b/usr/src/lib/libc/port/gen/fls.c new file mode 100644 index 0000000000..e685451c3e --- /dev/null +++ b/usr/src/lib/libc/port/gen/fls.c @@ -0,0 +1,83 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + */ + +#include "lint.h" +#include <string.h> +#include <strings.h> +#include <sys/types.h> + +static int +fls_impl(uint64_t bits) +{ + int i = 1; + uint32_t bits32; + + if (bits == 0) + return (0); + + if ((bits32 = (uint32_t)(bits >> 32)) != 0) + i += 32; + else + bits32 = (uint32_t)bits; + + if ((bits32 & 0xffff0000) != 0) { + bits32 >>= 16; + i += 16; + } + if ((bits32 & 0xff00) != 0) { + bits32 >>= 8; + i += 8; + } + if ((bits32 & 0xf0) != 0) { + bits32 >>= 4; + i += 4; + } + if ((bits32 & 0xc) != 0) { + bits32 >>= 2; + i += 2; + } + if ((bits32 & 0x2) != 0) + i += 1; + + return (i); +} + +int +fls(int bits) +{ + return (fls_impl((uint64_t)(uint_t)bits)); +} + +int +flsl(long bits) +{ + return (fls_impl((uint64_t)(ulong_t)bits)); +} + +int +flsll(long long bits) +{ + return (fls_impl((uint64_t)(u_longlong_t)bits)); +} diff --git a/usr/src/lib/libc/port/gen/get_nprocs.c b/usr/src/lib/libc/port/gen/get_nprocs.c new file mode 100644 index 0000000000..555dceafae --- /dev/null +++ b/usr/src/lib/libc/port/gen/get_nprocs.c @@ -0,0 +1,45 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + */ + +#include "lint.h" +#include <unistd.h> + +/* + * get_nprocs(3C) - return the number of online processors + */ +int +get_nprocs(void) +{ + return ((int)sysconf(_SC_NPROCESSORS_ONLN)); +} + +/* + * get_nprocs_conf(3C) - return the number of configured processors + */ +int +get_nprocs_conf(void) +{ + return ((int)sysconf(_SC_NPROCESSORS_CONF)); +} diff --git a/usr/src/lib/libc/port/gen/getenv.c b/usr/src/lib/libc/port/gen/getenv.c index 464632ac6c..c345226d0c 100644 --- a/usr/src/lib/libc/port/gen/getenv.c +++ b/usr/src/lib/libc/port/gen/getenv.c @@ -20,15 +20,12 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 1988 AT&T */ /* All Rights Reserved */ -#pragma ident "%Z%%M% %I% %E% SMI" - #pragma weak _putenv = putenv #include "lint.h" @@ -445,6 +442,30 @@ unsetenv(const char *name) } /* + * Dump entire environment. + */ +int +clearenv(void) +{ + /* + * Just drop the entire environment list on the floor, as it + * would be non-trivial to try and free the used memory. + */ + static const char *nullp = NULL; + + lmutex_lock(&update_lock); + _environ = &nullp; + my_environ = NULL; + environ_base = NULL; + environ_size = 0; + environ_gen++; + membar_producer(); + lmutex_unlock(&update_lock); + + return (0); +} + +/* * At last, a lockless implementation of getenv()! */ char * diff --git a/usr/src/lib/libc/port/gen/getmntent.c b/usr/src/lib/libc/port/gen/getmntent.c index af342cdfd4..ef63aacf85 100644 --- a/usr/src/lib/libc/port/gen/getmntent.c +++ b/usr/src/lib/libc/port/gen/getmntent.c @@ -20,8 +20,7 @@ */ /* - * Copyright 2009 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 1988 AT&T */ @@ -342,7 +341,7 @@ resetmnttab(FILE *fp) * an ioctl() call. */ static int -getline(char *lp, FILE *fp) +getaline(char *lp, FILE *fp) { char *cp; @@ -372,7 +371,7 @@ getmntent_compat(FILE *fp, struct mnttab *mp) } /* skip leading spaces and comments */ - if ((ret = getline(line, fp)) != 0) + if ((ret = getaline(line, fp)) != 0) return (ret); /* split up each field */ diff --git a/usr/src/lib/libc/port/gen/getvfsent.c b/usr/src/lib/libc/port/gen/getvfsent.c index bf6cf11c1a..13ce57d19f 100644 --- a/usr/src/lib/libc/port/gen/getvfsent.c +++ b/usr/src/lib/libc/port/gen/getvfsent.c @@ -20,15 +20,12 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 1988 AT&T */ /* All Rights Reserved */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include "lint.h" #include <mtlib.h> #include <stdio.h> @@ -66,7 +63,7 @@ static const char sepstr[] = " \t\n"; static const char dash[] = "-"; -static int getline(char *, FILE *); +static int getaline(char *, FILE *); int getvfsspec(FILE *fd, struct vfstab *vgetp, char *special) @@ -78,8 +75,8 @@ getvfsspec(FILE *fd, struct vfstab *vgetp, char *special) if (special && stat64(special, &statb) == 0 && - ((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK || - bmode == S_IFCHR)) { + ((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK || + bmode == S_IFCHR)) { bstat = 1; brdev = statb.st_rdev; } else @@ -119,12 +116,12 @@ getvfsany(FILE *fd, struct vfstab *vgetp, struct vfstab *vrefp) /* Match by straight strcmp */ while ((ret = getvfsent(fd, vgetp)) == 0 && - (DIFF(vfs_special) || DIFF(vfs_fsckdev) || - DIFF(vfs_mountp) || - DIFF(vfs_fstype) || - DIFF(vfs_fsckpass) || - DIFF(vfs_automnt) || - DIFF(vfs_mntopts))) + (DIFF(vfs_special) || DIFF(vfs_fsckdev) || + DIFF(vfs_mountp) || + DIFF(vfs_fstype) || + DIFF(vfs_fsckpass) || + DIFF(vfs_automnt) || + DIFF(vfs_mntopts))) ; /* If something other than EOF, return it */ @@ -139,31 +136,31 @@ getvfsany(FILE *fd, struct vfstab *vgetp, struct vfstab *vrefp) (void) fseeko64(fd, start, SEEK_SET); if (vrefp->vfs_special && stat64(vrefp->vfs_special, &statb) == 0 && - ((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK || - bmode == S_IFCHR)) { + ((bmode = (statb.st_mode & S_IFMT)) == S_IFBLK || + bmode == S_IFCHR)) { bstat = 1; brdev = statb.st_rdev; } else bstat = 0; if (vrefp->vfs_fsckdev && stat64(vrefp->vfs_fsckdev, &statb) == 0 && - ((cmode = (statb.st_mode & S_IFMT)) == S_IFBLK || - cmode == S_IFCHR)) { + ((cmode = (statb.st_mode & S_IFMT)) == S_IFBLK || + cmode == S_IFCHR)) { cstat = 1; crdev = statb.st_rdev; } else cstat = 0; while ((ret = getvfsent(fd, vgetp)) == 0 && - ((bstat == 0 && DIFF(vfs_special)) || - (bstat == 1 && SDIFF(vfs_special, bmode, brdev)) || - (cstat == 0 && DIFF(vfs_fsckdev)) || - (cstat == 1 && SDIFF(vfs_fsckdev, cmode, crdev)) || - DIFF(vfs_mountp) || - DIFF(vfs_fstype) || - DIFF(vfs_fsckpass) || - DIFF(vfs_automnt) || - DIFF(vfs_mntopts))) + ((bstat == 0 && DIFF(vfs_special)) || + (bstat == 1 && SDIFF(vfs_special, bmode, brdev)) || + (cstat == 0 && DIFF(vfs_fsckdev)) || + (cstat == 1 && SDIFF(vfs_fsckdev, cmode, crdev)) || + DIFF(vfs_mountp) || + DIFF(vfs_fstype) || + DIFF(vfs_fsckpass) || + DIFF(vfs_automnt) || + DIFF(vfs_mntopts))) ; return (ret); } @@ -179,7 +176,7 @@ getvfsent(FILE *fd, struct vfstab *vp) return (0); /* skip leading spaces and comments */ - if ((ret = getline(line, fd)) != 0) + if ((ret = getaline(line, fd)) != 0) return (ret); /* split up each field */ @@ -199,7 +196,7 @@ getvfsent(FILE *fd, struct vfstab *vp) } static int -getline(char *lp, FILE *fd) +getaline(char *lp, FILE *fd) { char *cp; diff --git a/usr/src/lib/libc/port/gen/memmem.c b/usr/src/lib/libc/port/gen/memmem.c new file mode 100644 index 0000000000..a0f396e7c4 --- /dev/null +++ b/usr/src/lib/libc/port/gen/memmem.c @@ -0,0 +1,87 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + */ + +/* + * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <string.h> + +/* + * Find the first occurrence of the byte string s in byte string l. + */ + +void * +memmem(const void *l, size_t l_len, const void *s, size_t s_len) +{ + char *cur, *last; + const char *cl = (const char *)l; + const char *cs = (const char *)s; + + /* we need something to compare */ + if (l_len == 0 || s_len == 0) + return (NULL); + + /* "s" must be smaller or equal to "l" */ + if (l_len < s_len) + return (NULL); + + /* special case where s_len == 1 */ + if (s_len == 1) + return (memchr(l, (int)*cs, l_len)); + + /* the last position where its possible to find "s" in "l" */ + last = (char *)cl + l_len - s_len; + + for (cur = (char *)cl; cur <= last; cur++) + if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) + return (cur); + + return (NULL); +} diff --git a/usr/src/lib/libc/port/gen/mkdtemp.c b/usr/src/lib/libc/port/gen/mkdtemp.c index 37e60c2b96..abe0afedc6 100644 --- a/usr/src/lib/libc/port/gen/mkdtemp.c +++ b/usr/src/lib/libc/port/gen/mkdtemp.c @@ -20,18 +20,14 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. */ -#pragma ident "%Z%%M% %I% %E% SMI" - /* * mkdtemp(3C) - create a directory with a unique name. */ #include "lint.h" -#include <alloca.h> #include <errno.h> #include <stdlib.h> #include <string.h> @@ -40,11 +36,11 @@ char * mkdtemp(char *template) { - char *t = alloca(strlen(template) + 1); + char *t; char *r; /* Save template */ - (void) strcpy(t, template); + t = strdupa(template); for (;;) { r = mktemp(template); diff --git a/usr/src/lib/libc/port/gen/mktemp.c b/usr/src/lib/libc/port/gen/mktemp.c index e8e7105d39..5f58501e64 100644 --- a/usr/src/lib/libc/port/gen/mktemp.c +++ b/usr/src/lib/libc/port/gen/mktemp.c @@ -20,15 +20,12 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 1988 AT&T */ /* All Rights Reserved */ -#pragma ident "%Z%%M% %I% %E% SMI" - /* * mktemp() expects a string with up to six trailing 'X's. * These will be overlaid with letters, digits and symbols from @@ -97,29 +94,6 @@ chars[64] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_', }; -/* - * Find highest one bit set. - * Returns bit number of highest bit that is set. - * Low order bit is number 0, high order bit is number 31. - */ -static int -highbit(uint_t i) -{ - int h = 0; - - if (i & 0xffff0000) - h += 16, i >>= 16; - if (i & 0xff00) - h += 8, i >>= 8; - if (i & 0xf0) - h += 4, i >>= 4; - if (i & 0xc) - h += 2, i >>= 2; - if (i & 0x2) - h += 1; - return (h); -} - char * libc_mktemps(char *as, int slen) { @@ -162,7 +136,7 @@ libc_mktemps(char *as, int slen) /* for all possible values of pid, 0 <= pid < (1 << pidshift) */ if (pidshift == 0) /* one-time initialization */ - pidshift = highbit((uint_t)MAXPID) + 1; + pidshift = fls((uint_t)MAXPID); /* high bit number */ /* count the X's */ xcnt = 0; diff --git a/usr/src/lib/libc/port/gen/poll.c b/usr/src/lib/libc/port/gen/poll.c index e186a30b12..e23b103f3b 100644 --- a/usr/src/lib/libc/port/gen/poll.c +++ b/usr/src/lib/libc/port/gen/poll.c @@ -20,12 +20,9 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. */ -#pragma ident "%Z%%M% %I% %E% SMI" - #pragma weak _poll = poll #include "lint.h" @@ -34,6 +31,14 @@ #include "libc.h" int +ppoll(struct pollfd *_RESTRICT_KYWD fds, nfds_t nfd, + const struct timespec *_RESTRICT_KYWD tsp, + const sigset_t *_RESTRICT_KYWD sigmask) +{ + return (_pollsys(fds, nfd, tsp, sigmask)); +} + +int poll(struct pollfd *fds, nfds_t nfd, int timeout) { timespec_t ts; diff --git a/usr/src/lib/libc/port/gen/realpath.c b/usr/src/lib/libc/port/gen/realpath.c index 08223f8f5c..a6b116c250 100644 --- a/usr/src/lib/libc/port/gen/realpath.c +++ b/usr/src/lib/libc/port/gen/realpath.c @@ -20,15 +20,12 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 1988 AT&T */ /* All Rights Reserved */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include "lint.h" #include <sys/types.h> #include <dirent.h> @@ -42,18 +39,18 @@ /* * Canonicalize the path given in file_name, resolving away all symbolic link * components. Store the result into the buffer named by resolved_name, which - * must be long enough (MAXPATHLEN bytes will suffice). Returns NULL + * must be long enough (PATH_MAX bytes will suffice). Returns NULL * on failure and resolved_name on success. On failure, to maintain * compatibility with the past, the contents of file_name will be copied * into resolved_name. */ -char * -realpath(const char *file_name, char *resolved_name) +static char * +realpath_impl(const char *file_name, char *resolved_name) { char cwd[PATH_MAX]; int len; - if (file_name == NULL || resolved_name == NULL) { + if (file_name == NULL) { errno = EINVAL; return (NULL); } @@ -121,3 +118,32 @@ realpath(const char *file_name, char *resolved_name) (void) strcpy(resolved_name, cwd); return (resolved_name); } + +/* + * Canonicalize the path given in file_name, resolving away all symbolic link + * components. If resolved_name is a null pointer, return a malloc()d + * buffer containing the result, else store the result into resolved_name + * and return resolved_name. Return NULL on failure. + */ +char * +realpath(const char *file_name, char *resolved_name) +{ + char buffer[PATH_MAX]; + + if (resolved_name != NULL) + return (realpath_impl(file_name, resolved_name)); + + if (realpath_impl(file_name, buffer) != NULL) + return (strdup(buffer)); + + return (NULL); +} + +/* + * GNU extension. + */ +char * +canonicalize_file_name(const char *path) +{ + return (realpath(path, NULL)); +} diff --git a/usr/src/lib/libc/port/gen/stpcpy.c b/usr/src/lib/libc/port/gen/stpcpy.c new file mode 100644 index 0000000000..ee1439e61c --- /dev/null +++ b/usr/src/lib/libc/port/gen/stpcpy.c @@ -0,0 +1,42 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + */ + +/* Copyright (c) 1988 AT&T */ +/* All Rights Reserved */ + +#include "lint.h" +#include <string.h> +#include <sys/types.h> + +/* + * Copy string s2 to s1. s1 must be large enough. + * return a pointer to the terminating null character of s1. + */ +char * +stpcpy(char *_RESTRICT_KYWD s1, const char *_RESTRICT_KYWD s2) +{ + (void) strcpy(s1, s2); + return (s1 + strlen(s1)); +} diff --git a/usr/src/lib/libc/port/gen/stpncpy.c b/usr/src/lib/libc/port/gen/stpncpy.c new file mode 100644 index 0000000000..5a69e370e5 --- /dev/null +++ b/usr/src/lib/libc/port/gen/stpncpy.c @@ -0,0 +1,46 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + */ + +/* Copyright (c) 1988 AT&T */ +/* All Rights Reserved */ + +#include "lint.h" +#include <string.h> +#include <sys/types.h> + +/* + * Copy s2 to s1, truncating or null-padding to always copy n bytes + * return a pointer to the terminating null byte in s1, or, + * if s1 is not null-terminated, s1 + n. + */ +char * +stpncpy(char *_RESTRICT_KYWD s1, const char *_RESTRICT_KYWD s2, size_t n) +{ + size_t len = strnlen(s2, n); + + (void) strncpy(s1, s2, n); + + return ((len < n) ? s1 + len : s1 + n); +} diff --git a/usr/src/lib/libc/port/gen/strchrnul.c b/usr/src/lib/libc/port/gen/strchrnul.c new file mode 100644 index 0000000000..3355ec4d93 --- /dev/null +++ b/usr/src/lib/libc/port/gen/strchrnul.c @@ -0,0 +1,37 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + */ + +#include <string.h> + +char * +strchrnul(const char *s, int c) +{ + char *ptr = strchr(s, c); + + if (ptr == NULL) + ptr = (char *)s + strlen(s); + + return (ptr); +} diff --git a/usr/src/lib/libc/port/gen/strndup.c b/usr/src/lib/libc/port/gen/strndup.c new file mode 100644 index 0000000000..a31f60827c --- /dev/null +++ b/usr/src/lib/libc/port/gen/strndup.c @@ -0,0 +1,44 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + */ + +#include "lint.h" +#include <string.h> +#include <stdlib.h> +#include <sys/types.h> + +/* + * Create a copy of string s, but only duplicate the first n bytes. + * Return NULL if the new string can't be allocated. + */ +char * +strndup(const char *s1, size_t n) +{ + char *s2; + + n = strnlen(s1, n); + if ((s2 = malloc(n + 1)) != NULL) + (void) strlcpy(s2, s1, n + 1); + return (s2); +} diff --git a/usr/src/lib/libc/port/gen/strstr.c b/usr/src/lib/libc/port/gen/strstr.c index a8d8d655af..1f87034ada 100644 --- a/usr/src/lib/libc/port/gen/strstr.c +++ b/usr/src/lib/libc/port/gen/strstr.c @@ -20,15 +20,12 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 1988 AT&T */ /* All Rights Reserved */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include "lint.h" #include <string.h> #include <stddef.h> @@ -37,9 +34,9 @@ /* * strstr() locates the first occurrence in the string as1 of * the sequence of characters (excluding the terminating null - * character) in the string as2. strstr() returns a pointer + * character) in the string as2. strstr() returns a pointer * to the located string, or a null pointer if the string is - * not found. If as2 is "", the function returns as1. + * not found. If as2 is empty, the function returns as1. */ char * @@ -54,19 +51,61 @@ strstr(const char *as1, const char *as2) if (s2 == NULL || *s2 == '\0') return ((char *)s1); + c = *s2; + while (*s1 != '\0') { + if (c == *s1++) { + tptr = s1; + while ((c = *++s2) == *s1++ && c != '\0') + continue; + if (c == '\0') + return ((char *)tptr - 1); + s1 = tptr; + s2 = as2; + c = *s2; + } + } - while (*s1) - if (*s1++ == c) { + return (NULL); +} + +/* + * strnstr() locates the first occurrence in the string as1 of + * the sequence of characters (excluding the terminating null + * character) in the string as2, where not more than n characters + * from the string as1 are searched. strnstr() returns a pointer + * to the located string, or a null pointer if the string is + * not found. If as2 is empty, the function returns as1. + */ + +char * +strnstr(const char *as1, const char *as2, size_t n) +{ + const char *s1, *s2; + const char *tptr; + size_t k; + char c; + + s1 = as1; + s2 = as2; + + if (s2 == NULL || *s2 == '\0') + return ((char *)s1); + + c = *s2; + while (*s1 != '\0' && n--) { + if (c == *s1++) { + k = n; tptr = s1; - while ((c = *++s2) == *s1++ && c) - ; - if (c == 0) + while ((c = *++s2) == *s1++ && c != '\0' && k--) + continue; + if (c == '\0') return ((char *)tptr - 1); s1 = tptr; s2 = as2; c = *s2; } + } return (NULL); } diff --git a/usr/src/lib/libc/port/gen/tls_data.c b/usr/src/lib/libc/port/gen/tls_data.c new file mode 100644 index 0000000000..b0cf94470a --- /dev/null +++ b/usr/src/lib/libc/port/gen/tls_data.c @@ -0,0 +1,34 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE + * or http://www.opensolaris.org/os/licensing. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + */ + +#include "lint.h" +#include <string.h> + +/* + * Thread-local variables. + * Needed by strdupa() and strndupa() macros when compiling with Studio C. + */ +__thread char *__strdupa_str; +__thread size_t __strdupa_len; |
