diff options
Diffstat (limited to 'lib/libc/gen')
206 files changed, 30943 insertions, 10 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc new file mode 100644 index 0000000..58de374 --- /dev/null +++ b/lib/libc/gen/Makefile.inc @@ -0,0 +1,121 @@ +# @(#)Makefile.inc 8.6 (Berkeley) 5/4/95 +# $FreeBSD$ + +# machine-independent gen sources +.PATH: ${.CURDIR}/../libc/${MACHINE_ARCH}/gen ${.CURDIR}/../libc/gen + +SRCS+= _rand48.c _spinlock_stub.c alarm.c arc4random.c assert.c \ + clock.c closedir.c confstr.c \ + crypt.c ctermid.c daemon.c devname.c disklabel.c \ + dlfcn.c drand48.c erand48.c err.c errlst.c \ + exec.c fnmatch.c fstab.c ftok.c fts.c getbootfile.c getbsize.c \ + getcap.c getcwd.c getdomainname.c getgrent.c getgrouplist.c \ + gethostname.c getloadavg.c getlogin.c getmntinfo.c getnetgrent.c \ + getobjformat.c getosreldate.c getpagesize.c \ + getpass.c getpwent.c getttyent.c \ + getusershell.c getvfsbyname.c getvfsent.c glob.c \ + initgroups.c isatty.c jrand48.c lcong48.c \ + lockf.c lrand48.c mrand48.c msgctl.c \ + msgget.c msgrcv.c msgsnd.c nice.c \ + nlist.c nrand48.c ntp_gettime.c opendir.c \ + pause.c popen.c psignal.c pwcache.c raise.c readdir.c rewinddir.c \ + scandir.c seed48.c seekdir.c semconfig.c semctl.c semget.c semop.c \ + setdomainname.c sethostname.c setjmperr.c setmode.c shmat.c \ + shmctl.c shmdt.c shmget.c siginterrupt.c siglist.c signal.c \ + sigsetops.c sleep.c srand48.c stringlist.c \ + sysconf.c sysctl.c sysctlbyname.c \ + syslog.c telldir.c termios.c time.c times.c timezone.c ttyname.c \ + ttyslot.c ualarm.c uname.c unvis.c usleep.c utime.c valloc.c vis.c \ + wait.c wait3.c waitpid.c + +# machine-dependent gen sources +.include "${.CURDIR}/../libc/${MACHINE_ARCH}/gen/Makefile.inc" + +.if ${LIB} == "c" +MAN3+= alarm.3 arc4random.3 clock.3 \ + confstr.3 crypt.3 ctermid.3 daemon.3 \ + devname.3 directory.3 dladdr.3 dlopen.3 \ + err.3 exec.3 fnmatch.3 frexp.3 ftok.3 fts.3 \ + getbootfile.3 getbsize.3 getcap.3 getcwd.3 \ + getdiskbyname.3 getdomainname.3 getfsent.3 \ + getgrent.3 getgrouplist.3 gethostname.3 getloadavg.3 \ + getmntinfo.3 getnetgrent.3 getobjformat.3 \ + getpagesize.3 getpass.3 getpwent.3 \ + getttyent.3 getusershell.3 getvfsbyname.3 getvfsent.3 \ + glob.3 initgroups.3 isinf.3 \ + ldexp.3 lockf.3 modf.3 msgctl.3 msgget.3 msgrcv.3 msgsnd.3 \ + nice.3 nlist.3 pause.3 popen.3 psignal.3 pwcache.3 \ + raise.3 rand48.3 scandir.3 setjmp.3 setmode.3 siginterrupt.3 \ + signal.3 sigsetops.3 sleep.3 stringlist.3 \ + sysconf.3 sysctl.3 syslog.3 tcgetpgrp.3 \ + tcsendbreak.3 tcsetattr.3 tcsetpgrp.3 time.3 times.3 timezone.3 \ + ttyname.3 tzset.3 ualarm.3 uname.3 unvis.3 usleep.3 utime.3 \ + valloc.3 vis.3 + +MLINKS+=arc4random.3 arc4random_addrandom.3 arc4random.3 arc4random_stir.3 +MLINKS+=crypt.3 des_cipher.3 crypt.3 des_setkey.3 crypt.3 encrypt.3 \ + crypt.3 setkey.3 +MLINKS+=directory.3 closedir.3 directory.3 dirfd.3 directory.3 opendir.3 \ + directory.3 readdir.3 directory.3 rewinddir.3 directory.3 seekdir.3 \ + directory.3 telldir.3 +MLINKS+=dlopen.3 dlclose.3 dlopen.3 dlerror.3 dlopen.3 dlsym.3 +MLINKS+=err.3 err_set_exit.3 err.3 err_set_file.3 err.3 errc.3 err.3 errx.3 \ + err.3 verr.3 err.3 verrc.3 err.3 verrx.3 err.3 vwarn.3 err.3 vwarnc.3 \ + err.3 vwarnx.3 err.3 warnc.3 err.3 warn.3 err.3 warnx.3 +MLINKS+=exec.3 execl.3 exec.3 execle.3 exec.3 execlp.3 exec.3 exect.3 \ + exec.3 execv.3 exec.3 execvp.3 +MLINKS+=fts.3 fts_children.3 fts.3 fts_close.3 fts.3 fts_open.3 \ + fts.3 fts_read.3 fts.3 fts_set.3 +MLINKS+=getcap.3 cgetcap.3 getcap.3 cgetclose.3 getcap.3 cgetent.3 \ + getcap.3 cgetfirst.3 getcap.3 cgetmatch.3 getcap.3 cgetnext.3 \ + getcap.3 cgetnum.3 getcap.3 cgetset.3 getcap.3 cgetstr.3 \ + getcap.3 cgetustr.3 +MLINKS+=getcwd.3 getwd.3 +MLINKS+=getdomainname.3 setdomainname.3 +MLINKS+=getfsent.3 endfsent.3 getfsent.3 getfsfile.3 getfsent.3 getfsspec.3 \ + getfsent.3 getfstype.3 getfsent.3 setfsent.3 +MLINKS+=getgrent.3 endgrent.3 getgrent.3 getgrgid.3 getgrent.3 getgrnam.3 \ + getgrent.3 setgrent.3 getgrent.3 setgroupent.3 +MLINKS+=gethostname.3 sethostname.3 +MLINKS+=getnetgrent.3 endnetgrent.3 getnetgrent.3 innetgr.3 \ + getnetgrent.3 setnetgrent.3 +MLINKS+=getpwent.3 endpwent.3 getpwent.3 getpwnam.3 getpwent.3 getpwuid.3 \ + getpwent.3 setpassent.3 getpwent.3 setpwent.3 getpwent.3 setpwfile.3 +MLINKS+=getttyent.3 endttyent.3 getttyent.3 getttynam.3 \ + getttyent.3 isdialuptty.3 getttyent.3 isnettty.3 \ + getttyent.3 setttyent.3 +MLINKS+=getusershell.3 endusershell.3 getusershell.3 setusershell.3 +MLINKS+=getvfsent.3 endvfsent.3 getvfsent.3 getvfsbytype.3 \ + getvfsent.3 setvfsent.3 getvfsent.3 vfsisloadable.3 \ + getvfsent.3 vfsload.3 +MLINKS+=glob.3 globfree.3 +MLINKS+=isinf.3 isnan.3 +MLINKS+=popen.3 pclose.3 +MLINKS+=psignal.3 sys_siglist.3 psignal.3 sys_signame.3 +MLINKS+=psignal.3 strsignal.3 psignal.3 sys_siglist.3 psignal.3 sys_signame.3 +MLINKS+=pwcache.3 group_from_gid.3 pwcache.3 user_from_uid.3 +MLINKS+=rand48.3 _rand48.3 rand48.3 drand48.3 rand48.3 erand48.3 \ + rand48.3 jrand48.3 rand48.3 lcong48.3 rand48.3 lrand48.3 \ + rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 seed48.3 \ + rand48.3 srand48.3 +MLINKS+=scandir.3 alphasort.3 +MLINKS+=setjmp.3 _longjmp.3 setjmp.3 _setjmp.3 setjmp.3 longjmp.3 \ + setjmp.3 longjmperr.3 setjmp.3 longjmperror.3 \ + setjmp.3 siglongjmp.3 setjmp.3 sigsetjmp.3 +MLINKS+=setmode.3 getmode.3 +MLINKS+=sigsetops.3 sigaddset.3 sigsetops.3 sigdelset.3 \ + sigsetops.3 sigemptyset.3 sigsetops.3 sigfillset.3 \ + sigsetops.3 sigismember.3 +MLINKS+=sysctl.3 sysctlbyname.3 +MLINKS+=syslog.3 closelog.3 syslog.3 openlog.3 syslog.3 setlogmask.3 \ + syslog.3 vsyslog.3 +MLINKS+=tcsendbreak.3 tcdrain.3 tcsendbreak.3 tcflow.3 tcsendbreak.3 tcflush.3 +MLINKS+=tcsetattr.3 cfgetispeed.3 tcsetattr.3 cfgetospeed.3 \ + tcsetattr.3 cfmakeraw.3 tcsetattr.3 cfsetispeed.3 \ + tcsetattr.3 cfsetospeed.3 tcsetattr.3 cfsetspeed.3 \ + tcsetattr.3 tcgetattr.3 +MLINKS+=ttyname.3 isatty.3 ttyname.3 ttyslot.3 +MLINKS+=tzset.3 tzsetwall.3 +MLINKS+=unvis.3 strunvis.3 +MLINKS+=vis.3 strvis.3 vis.3 strvisx.3 +.endif diff --git a/lib/libc/gen/__xuname.c b/lib/libc/gen/__xuname.c new file mode 100644 index 0000000..944c2dc --- /dev/null +++ b/lib/libc/gen/__xuname.c @@ -0,0 +1,125 @@ +/*- + * Copyright (c) 1994 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +/*static char sccsid[] = "From: @(#)uname.c 8.1 (Berkeley) 1/4/94";*/ +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> +#include <sys/utsname.h> +#include <errno.h> + +int +uname(name) + struct utsname *name; +{ + int mib[2], rval; + size_t len; + char *p; + int oerrno; + + rval = 0; + + mib[0] = CTL_KERN; + mib[1] = KERN_OSTYPE; + len = sizeof(name->sysname); + oerrno = errno; + if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1) { + if(errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->sysname[sizeof(name->sysname) - 1] = '\0'; + + mib[0] = CTL_KERN; + mib[1] = KERN_HOSTNAME; + len = sizeof(name->nodename); + oerrno = errno; + if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1) { + if(errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->nodename[sizeof(name->nodename) - 1] = '\0'; + + mib[0] = CTL_KERN; + mib[1] = KERN_OSRELEASE; + len = sizeof(name->release); + oerrno = errno; + if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1) { + if(errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->release[sizeof(name->release) - 1] = '\0'; + + /* The version may have newlines in it, turn them into spaces. */ + mib[0] = CTL_KERN; + mib[1] = KERN_VERSION; + len = sizeof(name->version); + oerrno = errno; + if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1) { + if (errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->version[sizeof(name->version) - 1] = '\0'; + for (p = name->version; len--; ++p) { + if (*p == '\n' || *p == '\t') { + if (len > 1) + *p = ' '; + else + *p = '\0'; + } + } + + mib[0] = CTL_HW; + mib[1] = HW_MACHINE; + len = sizeof(name->machine); + oerrno = errno; + if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1) { + if (errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->machine[sizeof(name->machine) - 1] = '\0'; + return (rval); +} diff --git a/lib/libc/gen/_rand48.c b/lib/libc/gen/_rand48.c new file mode 100644 index 0000000..990e2c8 --- /dev/null +++ b/lib/libc/gen/_rand48.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +unsigned short _rand48_seed[3] = { + RAND48_SEED_0, + RAND48_SEED_1, + RAND48_SEED_2 +}; +unsigned short _rand48_mult[3] = { + RAND48_MULT_0, + RAND48_MULT_1, + RAND48_MULT_2 +}; +unsigned short _rand48_add = RAND48_ADD; + +void +_dorand48(unsigned short xseed[3]) +{ + unsigned long accu; + unsigned short temp[2]; + + accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] + + (unsigned long) _rand48_add; + temp[0] = (unsigned short) accu; /* lower 16 bits */ + accu >>= sizeof(unsigned short) * 8; + accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] + + (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0]; + temp[1] = (unsigned short) accu; /* middle 16 bits */ + accu >>= sizeof(unsigned short) * 8; + accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0]; + xseed[0] = temp[0]; + xseed[1] = temp[1]; + xseed[2] = (unsigned short) accu; +} diff --git a/lib/libc/gen/_spinlock_stub.c b/lib/libc/gen/_spinlock_stub.c new file mode 100644 index 0000000..e3a800d --- /dev/null +++ b/lib/libc/gen/_spinlock_stub.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>. + * All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by John Birrell. + * 4. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL 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 REGENTS 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. + * + * $FreeBSD$ + * + */ + +#include <stdio.h> + +/* Don't build these stubs into libc_r: */ +#ifndef _THREAD_SAFE +#include "spinlock.h" + +/* + * Declare weak references in case the application is not linked + * with libpthread. + */ +#pragma weak _spinlock=_spinlock_stub +#pragma weak _spinlock_debug=_spinlock_debug_stub + +/* + * This function is a stub for the spinlock function in libpthread. + */ +void +_spinlock_stub(spinlock_t *lck) +{ +} + +/* + * This function is a stub for the debug spinlock function in libpthread. + */ +void +_spinlock_debug_stub(spinlock_t *lck, char *fname, int lineno) +{ +} +#endif diff --git a/lib/libc/gen/alarm.3 b/lib/libc/gen/alarm.3 new file mode 100644 index 0000000..0265180 --- /dev/null +++ b/lib/libc/gen/alarm.3 @@ -0,0 +1,96 @@ +.\" Copyright (c) 1980, 1991, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)alarm.3 8.2 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd April 19, 1994 +.Dt ALARM 3 +.Os BSD 4 +.Sh NAME +.Nm alarm +.Nd set signal timer alarm +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft unsigned int +.Fn alarm "unsigned int seconds" +.Sh DESCRIPTION +.Bf -symbolic +This interface is made obsolete by setitimer(2). +.Ef +.Pp +The +.Fn alarm +function sets a timer to deliver the signal +.Dv SIGALRM +to the calling process after the specified number of +.Fa seconds . +If an alarm has already been set with +.Fn alarm +but has not been delivered, another call to +.Fn alarm +will supersede the prior call. +The request +.Fn alarm "0" +voids the current +alarm and the signal SIGALRM will not be delivered. +.Pp +Due to +.Xr setitimer 2 +restriction the maximum number of +.Ar seconds +allowed is 100000000. +.Sh RETURN VALUES +.Pp +The return value of +.Fn alarm +is the amount of time left on the timer from a previous call to +.Fn alarm . +If no alarm is currently set, the return value is 0. +.Sh SEE ALSO +.Xr setitimer 2 , +.Xr sigaction 2 , +.Xr sigpause 2 , +.Xr sigvec 2 , +.Xr signal 3 , +.Xr sleep 3 , +.Xr ualarm 3 , +.Xr usleep 3 +.\" .Sh STANDARDS +.\" The +.\" .Fn alarm +.\" function conforms to +.\" .St -p1003.1-90 . +.Sh HISTORY +An +.Fn alarm +function appeared in +.At v7 . diff --git a/lib/libc/gen/alarm.c b/lib/libc/gen/alarm.c new file mode 100644 index 0000000..7a7dc68 --- /dev/null +++ b/lib/libc/gen/alarm.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)alarm.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +/* + * Backwards compatible alarm. + */ +#include <sys/time.h> +#include <unistd.h> + +unsigned int +alarm(secs) + unsigned int secs; +{ + struct itimerval it, oitv; + register struct itimerval *itp = ⁢ + + timerclear(&itp->it_interval); + itp->it_value.tv_sec = secs; + itp->it_value.tv_usec = 0; + if (setitimer(ITIMER_REAL, itp, &oitv) < 0) + return (-1); + if (oitv.it_value.tv_usec) + oitv.it_value.tv_sec++; + return (oitv.it_value.tv_sec); +} diff --git a/lib/libc/gen/arc4random.3 b/lib/libc/gen/arc4random.3 new file mode 100644 index 0000000..334bb7b --- /dev/null +++ b/lib/libc/gen/arc4random.3 @@ -0,0 +1,84 @@ +.\" $OpenBSD: arc4random.3,v 1.2 1997/04/27 22:40:25 angelos Exp $ +.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> +.\" All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by Niels Provos. +.\" 4. 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 ``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 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. +.\" +.\" Manual page, using -mandoc macros +.\" $FreeBSD$ +.\" +.Dd April 15, 1997 +.Dt ARC4RANDOM 3 +.Os +.Sh NAME +.Nm arc4random , +.Nm arc4random_stir , +.Nm arc4random_addrandom +.Nd arc4 random number generator. +.Sh SYNOPSIS +.Fd #include <stdlib.h> +.Ft u_int32_t +.Fn arc4random "void" +.Ft void +.Fn arc4random_stir "void" +.Ft void +.Fn arc4random_addrandom "unsigned char *dat" "int datlen" +.Sh DESCRIPTION +The +.Fn arc4random +function uses the key stream generator employed by the +arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes +can be in about +.if t 2\u\s71700\s10\d +.if n (2**1700) +states. +.Pp +The +.Fn arc4random_stir +function reads data from +.Pa /dev/urandom +and uses it to permute the S-Boxes via +.Fn arc4random_addrandom . +.Pp +There is no need to call +.Fn arc4random_stir +before using +.Fn arc4random , +since +.Fn arc4random +automatically initializes itself. +.Sh SEE ALSO +.Xr rand 3 , +.Xr random 3 , +.Xr srandomdev 3 +.Sh HISTORY +.Pa RC4 +has been designed by RSA Data Security, Inc. It was posted anonymously +to the USENET and was confirmed to be equivalent by several sources who +had access to the original cipher. Since +.Pa RC4 +used to be a trade secret, the cipher is now referred to as +.Pa ARC4 . diff --git a/lib/libc/gen/arc4random.c b/lib/libc/gen/arc4random.c new file mode 100644 index 0000000..f331687 --- /dev/null +++ b/lib/libc/gen/arc4random.c @@ -0,0 +1,172 @@ +/* $FreeBSD$ */ + +/* + * Arc4 random number generator for OpenBSD. + * Copyright 1996 David Mazieres <dm@lcs.mit.edu>. + * + * Modification and redistribution in source and binary forms is + * permitted provided that due credit is given to the author and the + * OpenBSD project (for instance by leaving this copyright notice + * intact). + */ + +/* + * This code is derived from section 17.1 of Applied Cryptography, + * second edition, which describes a stream cipher allegedly + * compatible with RSA Labs "RC4" cipher (the actual description of + * which is a trade secret). The same algorithm is used as a stream + * cipher called "arcfour" in Tatu Ylonen's ssh package. + * + * Here the stream cipher has been modified always to include the time + * when initializing the state. That makes it impossible to + * regenerate the same random sequence twice, so this can't be used + * for encryption, but will generate good random numbers. + * + * RC4 is a registered trademark of RSA Laboratories. + */ + +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/time.h> + +struct arc4_stream { + u_int8_t i; + u_int8_t j; + u_int8_t s[256]; +}; + +static int rs_initialized; +static struct arc4_stream rs; + +static inline void +arc4_init(as) + struct arc4_stream *as; +{ + int n; + + for (n = 0; n < 256; n++) + as->s[n] = n; + as->i = 0; + as->j = 0; +} + +static inline void +arc4_addrandom(as, dat, datlen) + struct arc4_stream *as; + u_char *dat; + int datlen; +{ + int n; + u_int8_t si; + + as->i--; + for (n = 0; n < 256; n++) { + as->i = (as->i + 1); + si = as->s[as->i]; + as->j = (as->j + si + dat[n % datlen]); + as->s[as->i] = as->s[as->j]; + as->s[as->j] = si; + } +} + +static void +arc4_stir(as) + struct arc4_stream *as; +{ + int fd; + struct { + struct timeval tv; + pid_t pid; + u_int8_t rnd[128 - sizeof(struct timeval) - sizeof(pid_t)]; + } rdat; + + gettimeofday(&rdat.tv, NULL); + rdat.pid = getpid(); + fd = open("/dev/urandom", O_RDONLY, 0); + if (fd >= 0) { + (void) read(fd, rdat.rnd, sizeof(rdat.rnd)); + close(fd); + } + /* fd < 0? Ah, what the heck. We'll just take whatever was on the + * stack... */ + + arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); +} + +static inline u_int8_t +arc4_getbyte(as) + struct arc4_stream *as; +{ + u_int8_t si, sj; + + as->i = (as->i + 1); + si = as->s[as->i]; + as->j = (as->j + si); + sj = as->s[as->j]; + as->s[as->i] = sj; + as->s[as->j] = si; + return (as->s[(si + sj) & 0xff]); +} + +static inline u_int32_t +arc4_getword(as) + struct arc4_stream *as; +{ + u_int32_t val; + val = arc4_getbyte(as) << 24; + val |= arc4_getbyte(as) << 16; + val |= arc4_getbyte(as) << 8; + val |= arc4_getbyte(as); + return val; +} + +void +arc4random_stir() +{ + if (!rs_initialized) { + arc4_init(&rs); + rs_initialized = 1; + } + arc4_stir(&rs); +} + +void +arc4random_addrandom(dat, datlen) + u_char *dat; + int datlen; +{ + if (!rs_initialized) + arc4random_stir(); + arc4_addrandom(&rs, dat, datlen); +} + +u_int32_t +arc4random() +{ + if (!rs_initialized) + arc4random_stir(); + return arc4_getword(&rs); +} + +#if 0 +/*-------- Test code for i386 --------*/ +#include <stdio.h> +#include <machine/pctr.h> +int +main(int argc, char **argv) +{ + const int iter = 1000000; + int i; + pctrval v; + + v = rdtsc(); + for (i = 0; i < iter; i++) + arc4random(); + v = rdtsc() - v; + v /= iter; + + printf("%qd cycles\n", v); +} +#endif diff --git a/lib/libc/gen/assert.c b/lib/libc/gen/assert.c new file mode 100644 index 0000000..eb2748c --- /dev/null +++ b/lib/libc/gen/assert.c @@ -0,0 +1,52 @@ +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)assert.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +void +__assert(file, line, failedexpr) + const char *file, *failedexpr; + int line; +{ + (void)fprintf(stderr, + "assertion \"%s\" failed: file \"%s\", line %d\n", + failedexpr, file, line); + abort(); + /* NOTREACHED */ +} diff --git a/lib/libc/gen/clock.3 b/lib/libc/gen/clock.3 new file mode 100644 index 0000000..1d0f8fa --- /dev/null +++ b/lib/libc/gen/clock.3 @@ -0,0 +1,69 @@ +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)clock.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt CLOCK 3 +.Os +.Sh NAME +.Nm clock +.Nd determine processor time used +.Sh SYNOPSIS +.Fd #include <time.h> +.Ft clock_t +.Fn clock void +.Sh DESCRIPTION +The +.Fn clock +function +determines the amount of processor time used since the invocation of the +calling process, measured in +.Dv CLOCKS_PER_SEC Ns 's +of a second. +.Sh RETURN VALUES +The +.Fn clock +function returns the amount of time used unless an error occurs, in which +case the return value is \-1. +.Sh SEE ALSO +.Xr getrusage 2 , +.Xr clocks 7 +.Sh STANDARDS +The +.Fn clock +function conforms to +.St -ansiC . diff --git a/lib/libc/gen/clock.c b/lib/libc/gen/clock.c new file mode 100644 index 0000000..fe867c2 --- /dev/null +++ b/lib/libc/gen/clock.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)clock.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/time.h> +#include <sys/resource.h> + +/* + * Convert usec to clock ticks; could do (usec * CLOCKS_PER_SEC) / 1000000, + * but this would overflow if we switch to nanosec. + */ +#define CONVTCK(r) ((r).tv_sec * CLOCKS_PER_SEC \ + + (r).tv_usec / (1000000 / CLOCKS_PER_SEC)) + +clock_t +clock() +{ + struct rusage ru; + + if (getrusage(RUSAGE_SELF, &ru)) + return ((clock_t) -1); + return((clock_t)((CONVTCK(ru.ru_utime) + CONVTCK(ru.ru_stime)))); +} diff --git a/lib/libc/gen/closedir.c b/lib/libc/gen/closedir.c new file mode 100644 index 0000000..6c46e70 --- /dev/null +++ b/lib/libc/gen/closedir.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 1983, 1993 + * Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)closedir.c 8.1 (Berkeley) 6/10/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <dirent.h> +#include <stdlib.h> +#include <unistd.h> + +extern void _reclaim_telldir __P(( const DIR * )); + +/* + * close a directory. + */ +int +closedir(dirp) + register DIR *dirp; +{ + int fd; + + seekdir(dirp, dirp->dd_rewind); /* free seekdir storage */ + fd = dirp->dd_fd; + dirp->dd_fd = -1; + dirp->dd_loc = 0; + free((void *)dirp->dd_buf); + free((void *)dirp); + _reclaim_telldir(dirp); + return(close(fd)); +} diff --git a/lib/libc/gen/confstr.3 b/lib/libc/gen/confstr.3 new file mode 100644 index 0000000..6ba8e7c --- /dev/null +++ b/lib/libc/gen/confstr.3 @@ -0,0 +1,126 @@ +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)confstr.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt CONFSTR 3 +.Os BSD 4 +.Sh NAME +.Nm confstr +.Nd get string-valued configurable variables +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft size_t +.Fn confstr "int name" "char *buf" "size_t len" +.Sh DESCRIPTION +.Bf -symbolic +This interface is obsoleted by +.Xr sysctl 3 . +.Ef +.Pp +The +.Fn confstr +function provides a method for applications to get configuration +defined string values. +.Pp +The +.Fa name +argument specifies the system variable to be queried. +Symbolic constants for each name value are found in the include file +.Li <unistd.h> . +The +.Fa len +argument specifies the size of the buffer referenced by the +argument +.Fa buf . +If +.Fa len +is non-zero, +.Fa buf +is a non-null pointer, and +.Fa name +has a value, up to +.Fa len +\- 1 bytes of the value are copied into the buffer +.Fa buf . +The copied value is always null terminated. +.Pp +The available values are as follows: +.Pp +.Bl -tag -width "123456" +.Pp +.It Li _CS_PATH +Return a value for the +.Ev PATH +environment variable that finds all the standard utilities. +.El +.Sh RETURN VALUES +If the call to +.Fn confstr +is not successful, \-1 is returned and +.Va errno +is set appropriately. +Otherwise, if the variable does not have a configuration defined value, +0 is returned and +.Va errno +is not modified. +Otherwise, the buffer size needed to hold the entire configuration-defined +value is returned. +If this size is greater than the argument +.Fa len , +the string in +.Fa buf +was truncated. +.Sh ERRORS +The +.Fn confstr +function may fail and set +.Va error +for any of the errors specified for the library functions +.Xr malloc 3 +and +.Xr sysctl 3 . +.Pp +In addition, the following errors may be reported: +.Bl -tag -width Er +.It Bq Er EINVAL +The value of the +.Fa name +argument is invalid. +.Sh SEE ALSO +.Xr sysctl 3 +.Sh HISTORY +The +.Fn confstr +function first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/confstr.c b/lib/libc/gen/confstr.c new file mode 100644 index 0000000..0a7078e --- /dev/null +++ b/lib/libc/gen/confstr.c @@ -0,0 +1,86 @@ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)confstr.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> + +#include <errno.h> +#include <paths.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +size_t +confstr(name, buf, len) + int name; + char *buf; + size_t len; +{ + size_t tlen; + int mib[2], sverrno; + char *p; + + switch (name) { + case _CS_PATH: + mib[0] = CTL_USER; + mib[1] = USER_CS_PATH; + if (sysctl(mib, 2, NULL, &tlen, NULL, 0) == -1) + return (-1); + if (len != 0 && buf != NULL) { + if ((p = malloc(tlen)) == NULL) + return (-1); + if (sysctl(mib, 2, p, &tlen, NULL, 0) == -1) { + sverrno = errno; + free(p); + errno = sverrno; + return (-1); + } + /* + * POSIX 1003.2 requires partial return of + * the string -- that should be *real* useful. + */ + (void)strncpy(buf, p, len - 1); + buf[len - 1] = '\0'; + free(p); + } + return (tlen + 1); + default: + errno = EINVAL; + return (0); + } + /* NOTREACHED */ +} diff --git a/lib/libc/gen/crypt.3 b/lib/libc/gen/crypt.3 new file mode 100644 index 0000000..4a111f8 --- /dev/null +++ b/lib/libc/gen/crypt.3 @@ -0,0 +1,297 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)crypt.3 8.2 (Berkeley) 12/11/93 +.\" $FreeBSD$ +.\" +.Dd December 11, 1993 +.Dt CRYPT 3 +.Os +.Sh NAME +.Nm crypt , +.Nm setkey , +.Nm encrypt , +.Nm des_setkey , +.Nm des_cipher +.Nd DES encryption +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft char * +.Fn crypt "const char *key" "const char *setting" +.Ft int +.Fn setkey "const char *key" +.Ft int +.Fn encrypt "char *block" "int flag" +.Ft int +.Fn des_setkey "const char *key" +.Ft int +.Fn des_cipher "const char *in" "char *out" "long salt" "int count" +.Sh DESCRIPTION +The +.Fn crypt +function +performs password encryption. +It is derived from the +.Tn NBS +Data Encryption Standard. +Additional code has been added to deter +key search attempts. +The first argument to +.Nm crypt +is +a +.Dv NUL Ns -terminated +string (normally a password typed by a user). +The second is a character array, 9 bytes in length, consisting of an +underscore (``_'') followed by 4 bytes of iteration count and 4 bytes +of salt. +Both the iteration +.Fa count +and the +.Fa salt +are encoded with 6 bits per character, least significant bits first. +The values 0 to 63 are encoded by the characters ``./0-9A-Za-z'', +respectively. +.Pp +The +.Fa salt +is used to induce disorder in to the +.Tn DES +algorithm +in one of 16777216 +possible ways +(specifically, if bit +.Em i +of the +.Ar salt +is set then bits +.Em i +and +.Em i+24 +are swapped in the +.Tn DES +``E'' box output). +The +.Ar key +is divided into groups of 8 characters (a short final group is null-padded) +and the low-order 7 bits of each character (56 bits per group) are +used to form the DES key as follows: the first group of 56 bits becomes the +initial DES key. +For each additional group, the XOR of the group bits and the encryption of +the DES key with itself becomes the next DES key. +Then the final DES key is used to perform +.Ar count +cumulative encryptions of a 64-bit constant. +The value returned is a +.Dv NUL Ns -terminated +string, 20 bytes in length, consisting +of the +.Ar setting +followed by the encoded 64-bit encryption. +.Pp +For compatibility with historical versions of +.Xr crypt 3 , +the +.Ar setting +may consist of 2 bytes of salt, encoded as above, in which case an +iteration +.Ar count +of 25 is used, fewer perturbations of +.Tn DES +are available, at most 8 +characters of +.Ar key +are used, and the returned value is a +.Dv NUL Ns -terminated +string 13 bytes in length. +.Pp +The +functions, +.Fn encrypt , +.Fn setkey , +.Fn des_setkey +and +.Fn des_cipher +allow limited access to the +.Tn DES +algorithm itself. +The +.Ar key +argument to +.Fn setkey +is a 64 character array of +binary values (numeric 0 or 1). +A 56-bit key is derived from this array by dividing the array +into groups of 8 and ignoring the last bit in each group. +.Pp +The +.Fn encrypt +argument +.Fa block +is also a 64 character array of +binary values. +If the value of +.Fa flag +is 0, +the argument +.Fa block +is encrypted, otherwise it +is decrypted. +The encryption or decryption is returned in the original +array +.Fa block +after using the +key specified +by +.Fn setkey +to process it. +.Pp +The +.Fn des_setkey +and +.Fn des_cipher +functions are faster but less portable than +.Fn setkey +and +.Fn encrypt . +The argument to +.Fn des_setkey +is a character array of length 8. +The +.Em least +significant bit in each character is ignored and the next 7 bits of each +character are concatenated to yield a 56-bit key. +The function +.Fn des_cipher +encrypts (or decrypts if +.Fa count +is negative) the 64-bits stored in the 8 characters at +.Fa in +using +.Xr abs 3 +of +.Fa count +iterations of +.Tn DES +and stores the 64-bit result in the 8 characters at +.Fa out . +The +.Fa salt +specifies perturbations to +.Tn DES +as described above. +.Pp +The function +.Fn crypt +returns a pointer to the encrypted value on success and NULL on failure. +The functions +.Fn setkey , +.Fn encrypt , +.Fn des_setkey , +and +.Fn des_cipher +return 0 on success and 1 on failure. +Historically, the functions +.Fn setkey +and +.Fn encrypt +did not return any value. +They have been provided return values primarily to distinguish +implementations where hardware support is provided but not +available or where the DES encryption is not available due to the +usual political silliness. +.Pp +Use of +.Fn crypt +requires linking with the +.Nm libcrypt +library. The +.Fn setkey , +.Fn encrypt , +.Fn des_setkey +and +.Fn des_cipher +can be found in the +.Nm libcipher +library (the standard C library, +.Nm libc , +only contains stubs to these routines). +.Sh SEE ALSO +.Xr login 1 , +.Xr passwd 1 , +.Xr getpass 3 , +.Xr passwd 5 +.Rs +.%T "Mathematical Cryptology for Computer Scientists and Mathematicians" +.%A Wayne Patterson +.%D 1987 +.%N ISBN 0-8476-7438-X +.Re +.Rs +.%T "Password Security: A Case History" +.%A R. Morris +.%A Ken Thompson +.%J "Communications of the ACM" +.%V vol. 22 +.%P pp. 594-597 +.%D Nov. 1979 +.Re +.Rs +.%T "DES will be Totally Insecure within Ten Years" +.%A M.E. Hellman +.%J "IEEE Spectrum" +.%V vol. 16 +.%P pp. 32-39 +.%D July 1979 +.Re +.Sh HISTORY +A rotor-based +.Fn crypt +function appeared in +.At v6 . +The current style +.Fn crypt +first appeared in +.At v7 . +.Sh BUGS +Dropping the +.Em least +significant bit in each character of the argument to +.Fn des_setkey +is ridiculous. +.Pp +The +.Fn crypt +function leaves its result in an internal static object and returns +a pointer to that object. +Subsequent calls to +.Fn crypt +will modify the same object. diff --git a/lib/libc/gen/crypt.c b/lib/libc/gen/crypt.c new file mode 100644 index 0000000..8012b06 --- /dev/null +++ b/lib/libc/gen/crypt.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 1989 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Tom Truscott. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +/* from static char sccsid[] = "@(#)crypt.c 5.11 (Berkeley) 6/25/91"; */ +static char rcsid[] = "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <unistd.h> +#include <stdio.h> +#include <string.h> + +/* + * UNIX password, and DES, encryption. + * + * since this is non-exportable, this is just a dummy. if you want real + * encryption, make sure you've got libcrypt.a around. + */ + +__warn_references(des_setkey, + "WARNING! des_setkey(3) not present in the system!"); + +int +des_setkey(key) + register const char *key; +{ + fprintf(stderr, "WARNING! des_setkey(3) not present in the system!\n"); + return (0); +} + +__warn_references(des_cipher, + "WARNING! des_cipher(3) not present in the system!"); + +int +des_cipher(in, out, salt, num_iter) + const char *in; + char *out; + long salt; + int num_iter; +{ + fprintf(stderr, "WARNING! des_cipher(3) not present in the system!\n"); + bcopy(in, out, 8); + return (0); +} + +__warn_references(setkey, + "WARNING! setkey(3) not present in the system!"); + +int +setkey(key) + register const char *key; +{ + fprintf(stderr, "WARNING! setkey(3) not present in the system!\n"); + return (0); +} + +__warn_references(encrypt, + "WARNING! encrypt(3) not present in the system!"); + +int +encrypt(block, flag) + register char *block; + int flag; +{ + fprintf(stderr, "WARNING! encrypt(3) not present in the system!\n"); + return (0); +} diff --git a/lib/libc/gen/ctermid.3 b/lib/libc/gen/ctermid.3 new file mode 100644 index 0000000..3b1668f --- /dev/null +++ b/lib/libc/gen/ctermid.3 @@ -0,0 +1,107 @@ +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)ctermid.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt CTERMID 3 +.Os +.Sh NAME +.Nm ctermid +.Nd generate terminal pathname +.Sh SYNOPSIS +.Fd #include <stdio.h> +.Ft char * +.Fn ctermid "char *buf" +.Ft char * +.Fn ctermid_r "char *buf" +.Sh DESCRIPTION +The +.Fn ctermid +function generates a string, that, when used as a pathname, refers to +the current controlling terminal of the calling process. +.Pp +If +.Ar buf +is the +.Dv NULL +pointer, a pointer to a static area is returned. +Otherwise, the pathname is copied into the memory referenced by +.Ar buf . +The argument +.Ar buf +is assumed to be at least +.Dv L_ctermid +(as defined in the include +file +.Aq Pa stdio.h ) +bytes long. +.Pp +.Fn ctermid_r +provides the same functionality as +.Fn ctermid +except that if +.Ar buf +is a +.Dv NULL +pointer, +.Dv NULL +is returned. +.Pp +The current implementation simply returns +.Ql /dev/tty . +.Sh RETURN VALUES +Upon successful completion, a +.Pf non- Dv NULL +pointer is returned. +Otherwise, a +.Dv NULL +pointer is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +The current implementation detects no error conditions. +.Sh SEE ALSO +.Xr ttyname 3 +.Sh STANDARDS +The +.Fn ctermid +function conforms to +.St -p1003.1-88 . +.Sh BUGS +By default the +.Fn ctermid +function +writes all information to an internal static object. +Subsequent calls to +.Fn ctermid +will modify the same object. diff --git a/lib/libc/gen/ctermid.c b/lib/libc/gen/ctermid.c new file mode 100644 index 0000000..e43d6ab --- /dev/null +++ b/lib/libc/gen/ctermid.c @@ -0,0 +1,61 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)ctermid.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <stdio.h> +#include <paths.h> +#include <string.h> + +char * +ctermid(char *s) +{ + static char def[] = _PATH_TTY; + + if (s) { + bcopy(def, s, sizeof(_PATH_TTY)); + return(s); + } + return(def); +} + + +char * +ctermid_r(char *s) +{ + return (s) ? ctermid(s) : NULL; +} diff --git a/lib/libc/gen/daemon.3 b/lib/libc/gen/daemon.3 new file mode 100644 index 0000000..fa423d6 --- /dev/null +++ b/lib/libc/gen/daemon.3 @@ -0,0 +1,80 @@ +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)daemon.3 8.1 (Berkeley) 6/9/93 +.\" $FreeBSD$ +.\" +.Dd June 9, 1993 +.Dt DAEMON 3 +.Os +.Sh NAME +.Nm daemon +.Nd run in the background +.Sh SYNOPSIS +.Fd #include <stdlib.h> +.Ft int +.Fn daemon "int nochdir" "int noclose" +.Sh DESCRIPTION +.Pp +The +.Fn daemon +function is for programs wishing to detach themselves from the +controlling terminal and run in the background as system daemons. +.Pp +Unless the argument +.Fa nochdir +is non-zero, +.Fn daemon +changes the current working directory to the root (``/''). +.Pp +Unless the argument +.Fa noclose +is non-zero, +.Fn daemon +will redirect standard input, standard output and standard error +to ``/dev/null''. +.Sh ERRORS +If an error occurs, +.Fn daemon +returns -1 and sets the global variable +.Va errno +to any of the errors specified for the library functions +.Xr fork 2 +and +.Xr setsid 2 . +.Sh SEE ALSO +.Xr fork 2 , +.Xr setsid 2 +.Sh HISTORY +The +.Fn daemon +function first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/daemon.c b/lib/libc/gen/daemon.c new file mode 100644 index 0000000..6b3409c --- /dev/null +++ b/lib/libc/gen/daemon.c @@ -0,0 +1,71 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)daemon.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <fcntl.h> +#include <paths.h> +#include <unistd.h> + +int +daemon(nochdir, noclose) + int nochdir, noclose; +{ + int fd; + + switch (fork()) { + case -1: + return (-1); + case 0: + break; + default: + _exit(0); + } + + if (setsid() == -1) + return (-1); + + if (!nochdir) + (void)chdir("/"); + + if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { + (void)dup2(fd, STDIN_FILENO); + (void)dup2(fd, STDOUT_FILENO); + (void)dup2(fd, STDERR_FILENO); + if (fd > 2) + (void)close (fd); + } + return (0); +} diff --git a/lib/libc/gen/devname.3 b/lib/libc/gen/devname.3 new file mode 100644 index 0000000..2030125 --- /dev/null +++ b/lib/libc/gen/devname.3 @@ -0,0 +1,69 @@ +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)devname.3 8.2 (Berkeley) 4/29/95 +.\" $FreeBSD$ +.\" +.Dd April 29, 1995 +.Dt DEVNAME 3 +.Os BSD 4.4 +.Sh NAME +.Nm devname +.Nd get device name +.Sh SYNOPSIS +.Fd #include <stdlib.h> +.Ft char * +.Fn devname "dev_t dev" "mode_t type" +.Sh DESCRIPTION +The +.Fn devname +function returns a pointer to the name of the block or character +device in +.Dq Pa /dev +with a device number of +.Fa dev , +and a file type matching the one encoded in +.Fa type +which must be one of S_IFBLK or S_IFCHR. +If no device matches the specified values, or no information is +available, NULL is returned. +.Pp +The traditional display for applications when no device is +found is the string +.Dq ?? . +.Sh SEE ALSO +.Xr stat 2 , +.Xr dev_mkdb 8 +.Sh HISTORY +The +.Fn devname +function call appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/devname.c b/lib/libc/gen/devname.c new file mode 100644 index 0000000..898c8e8 --- /dev/null +++ b/lib/libc/gen/devname.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)devname.c 8.2 (Berkeley) 4/29/95"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> + +#include <db.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <paths.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> + +static char * +xdevname(dev, type) + dev_t dev; + mode_t type; +{ + struct { + mode_t type; + dev_t dev; + } bkey; + static DB *db; + static int failure; + DBT data, key; + + if (!db && !failure && + !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) { + warn("warning: %s", _PATH_DEVDB); + failure = 1; + } + if (failure) + return (NULL); + + /* + * Keys are a mode_t followed by a dev_t. The former is the type of + * the file (mode & S_IFMT), the latter is the st_rdev field. Be + * sure to clear any padding that may be found in bkey. + */ + memset(&bkey, 0, sizeof(bkey)); + bkey.dev = dev; + bkey.type = type; + key.data = &bkey; + key.size = sizeof(bkey); + return ((db->get)(db, &key, &data, 0) ? NULL : (char *)data.data); +} + +char * +devname(dev, type) + dev_t dev; + mode_t type; +{ + static char buf[20]; + char *r; + + r = xdevname(dev, type); + if (!r) { + r = buf; + if (minor(dev) > 255) { + sprintf(buf, "#%c%d:0x%x", + (type & S_IFMT) == S_IFCHR ? 'C' : 'B', + major(dev), minor(dev)); + } else { + sprintf(buf, "#%c%d:%d", + (type & S_IFMT) == S_IFCHR ? 'C' : 'B', + major(dev), minor(dev)); + } + } + return (r); +} diff --git a/lib/libc/gen/directory.3 b/lib/libc/gen/directory.3 new file mode 100644 index 0000000..6334d38 --- /dev/null +++ b/lib/libc/gen/directory.3 @@ -0,0 +1,198 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)directory.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt DIRECTORY 3 +.Os BSD 4.2 +.Sh NAME +.Nm opendir , +.Nm readdir , +.Nm readdir_r , +.Nm telldir , +.Nm seekdir , +.Nm rewinddir , +.Nm closedir , +.Nm dirfd +.Nd directory operations +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <dirent.h> +.Ft DIR * +.Fn opendir "const char *filename" +.Ft struct dirent * +.Fn readdir "DIR *dirp" +.Ft int +.Fn readdir_r "DIR *dirp" "struct dirent *entry" "struct dirent **result" +.Ft long +.Fn telldir "const DIR *dirp" +.Ft void +.Fn seekdir "DIR *dirp" "long loc" +.Ft void +.Fn rewinddir "DIR *dirp" +.Ft int +.Fn closedir "DIR *dirp" +.Ft int +.Fn dirfd "DIR *dirp" +.Sh DESCRIPTION +The +.Fn opendir +function +opens the directory named by +.Fa filename , +associates a +.Em directory stream +with it +and +returns a pointer to be used to identify the +.Em directory stream +in subsequent operations. The pointer +.Dv NULL +is returned if +.Fa filename +cannot be accessed, or if it cannot +.Xr malloc 3 +enough memory to hold the whole thing. +.Pp +The +.Fn readdir +function +returns a pointer to the next directory entry. It returns +.Dv NULL +upon reaching the end of the directory or detecting an invalid +.Fn seekdir +operation. +.Pp +.Fn readdir_r +provides the same functionality as +.Fn readdir , +but the caller must provide a directory +.Fa entry +buffer to store the results in. If the read succeeds, +.Fa result +is pointed at the +.Fa entry ; +upon reaching the end of the directory +.Fa result +is set to +.Dv NULL . +.Fn readdir_r +returns 0 on success or an error number to indicate failure. +.Pp +The +.Fn telldir +function +returns the current location associated with the named +.Em directory stream . +Values returned by +.Fn telldir +are good only for the lifetime of the +.Dv DIR +pointer, +.Fa dirp , +from which they are derived. If the directory is closed and then +reopened, prior values returned by +.Fn telldir +will no longer be valid. +.Pp +The +.Fn seekdir +function +sets the position of the next +.Fn readdir +operation on the +.Em directory stream . +The new position reverts to the one associated with the +.Em directory stream +when the +.Fn telldir +operation was performed. +.Pp +The +.Fn rewinddir +function +resets the position of the named +.Em directory stream +to the beginning of the directory. +.Pp +The +.Fn closedir +function +closes the named +.Em directory stream +and frees the structure associated with the +.Fa dirp +pointer, +returning 0 on success. +On failure, \-1 is returned and the global variable +.Va errno +is set to indicate the error. +.Pp +The +.Fn dirfd +function +returns the integer file descriptor associated with the named +.Em directory stream , +see +.Xr open 2 . +.Pp +Sample code which searches a directory for entry ``name'' is: +.Bd -literal -offset indent +len = strlen(name); +dirp = opendir("."); +while ((dp = readdir(dirp)) != NULL) + if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { + (void)closedir(dirp); + return FOUND; + } +(void)closedir(dirp); +return NOT_FOUND; +.Ed +.Sh SEE ALSO +.Xr close 2 , +.Xr lseek 2 , +.Xr open 2 , +.Xr read 2 , +.Xr dir 5 +.Sh HISTORY +The +.Fn opendir , +.Fn readdir , +.Fn telldir , +.Fn seekdir , +.Fn rewinddir , +.Fn closedir , +and +.Fn dirfd +functions appeared in +.Bx 4.2 . diff --git a/lib/libc/gen/disklabel.c b/lib/libc/gen/disklabel.c new file mode 100644 index 0000000..c7ac0b4 --- /dev/null +++ b/lib/libc/gen/disklabel.c @@ -0,0 +1,177 @@ +/* + * Copyright (c) 1983, 1987, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 5/3/95"; +#endif +static const char rcsid[] = + "$FreeBSD$"; +#endif /* not lint */ + +#include <sys/param.h> +#define DKTYPENAMES +#include <sys/disklabel.h> +#include <ufs/ufs/dinode.h> +#include <ufs/ffs/fs.h> + +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +static int gettype __P((char *, char **)); + +struct disklabel * +getdiskbyname(name) + const char *name; +{ + static struct disklabel disk; + register struct disklabel *dp = &disk; + register struct partition *pp; + char *buf; + char *db_array[2] = { _PATH_DISKTAB, 0 }; + char *cp, *cq; /* can't be register */ + char p, max, psize[3], pbsize[3], + pfsize[3], poffset[3], ptype[3]; + u_int32_t *dx; + + if (cgetent(&buf, db_array, (char *) name) < 0) + return NULL; + + bzero((char *)&disk, sizeof(disk)); + /* + * typename + */ + cq = dp->d_typename; + cp = buf; + while (cq < dp->d_typename + sizeof(dp->d_typename) - 1 && + (*cq = *cp) && *cq != '|' && *cq != ':') + cq++, cp++; + *cq = '\0'; + /* + * boot name (optional) xxboot, bootxx + */ + cgetstr(buf, "b0", &dp->d_boot0); + cgetstr(buf, "b1", &dp->d_boot1); + + if (cgetstr(buf, "ty", &cq) > 0 && strcmp(cq, "removable") == 0) + dp->d_flags |= D_REMOVABLE; + else if (cq && strcmp(cq, "simulated") == 0) + dp->d_flags |= D_RAMDISK; + if (cgetcap(buf, "sf", ':') != NULL) + dp->d_flags |= D_BADSECT; + +#define getnumdflt(field, dname, dflt) \ + { long f; (field) = (cgetnum(buf, dname, &f) == -1) ? (dflt) : f; } + + getnumdflt(dp->d_secsize, "se", DEV_BSIZE); + getnumdflt(dp->d_ntracks, "nt", 0); + getnumdflt(dp->d_nsectors, "ns", 0); + getnumdflt(dp->d_ncylinders, "nc", 0); + + if (cgetstr(buf, "dt", &cq) > 0) + dp->d_type = gettype(cq, dktypenames); + else + getnumdflt(dp->d_type, "dt", 0); + getnumdflt(dp->d_secpercyl, "sc", dp->d_nsectors * dp->d_ntracks); + getnumdflt(dp->d_secperunit, "su", dp->d_secpercyl * dp->d_ncylinders); + getnumdflt(dp->d_rpm, "rm", 3600); + getnumdflt(dp->d_interleave, "il", 1); + getnumdflt(dp->d_trackskew, "sk", 0); + getnumdflt(dp->d_cylskew, "cs", 0); + getnumdflt(dp->d_headswitch, "hs", 0); + getnumdflt(dp->d_trkseek, "ts", 0); + getnumdflt(dp->d_bbsize, "bs", BBSIZE); + getnumdflt(dp->d_sbsize, "sb", SBSIZE); + strcpy(psize, "px"); + strcpy(pbsize, "bx"); + strcpy(pfsize, "fx"); + strcpy(poffset, "ox"); + strcpy(ptype, "tx"); + max = 'a' - 1; + pp = &dp->d_partitions[0]; + for (p = 'a'; p < 'a' + MAXPARTITIONS; p++, pp++) { + long l; + psize[1] = pbsize[1] = pfsize[1] = poffset[1] = ptype[1] = p; + if (cgetnum(buf, psize, &l) == -1) + pp->p_size = 0; + else { + pp->p_size = l; + cgetnum(buf, poffset, &l); + pp->p_offset = l; + getnumdflt(pp->p_fsize, pfsize, 0); + if (pp->p_fsize) { + long bsize; + + if (cgetnum(buf, pbsize, &bsize) == 0) + pp->p_frag = bsize / pp->p_fsize; + else + pp->p_frag = 8; + } + getnumdflt(pp->p_fstype, ptype, 0); + if (pp->p_fstype == 0 && cgetstr(buf, ptype, &cq) > 0) + pp->p_fstype = gettype(cq, fstypenames); + max = p; + } + } + dp->d_npartitions = max + 1 - 'a'; + (void)strcpy(psize, "dx"); + dx = dp->d_drivedata; + for (p = '0'; p < '0' + NDDATA; p++, dx++) { + psize[1] = p; + getnumdflt(*dx, psize, 0); + } + dp->d_magic = DISKMAGIC; + dp->d_magic2 = DISKMAGIC; + free(buf); + return (dp); +} + +static int +gettype(t, names) + char *t; + char **names; +{ + register char **nm; + + for (nm = names; *nm; nm++) + if (strcasecmp(t, *nm) == 0) + return (nm - names); + if (isdigit((unsigned char)*t)) + return (atoi(t)); + return (0); +} diff --git a/lib/libc/gen/dladdr.3 b/lib/libc/gen/dladdr.3 new file mode 100644 index 0000000..9938a7a --- /dev/null +++ b/lib/libc/gen/dladdr.3 @@ -0,0 +1,121 @@ +.\" +.\" Copyright (c) 1998 John D. Polstra +.\" All rights reserved. +.\" +.\" 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. +.\" +.\" 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. +.\" +.\" $FreeBSD$ +.\" +.Dd February 5, 1998 +.Os FreeBSD +.Dt DLADDR 3 +.Sh NAME +.Nm dladdr +.Nd find the shared object containing a given address +.Sh SYNOPSIS +.Fd #include <dlfcn.h> +.Ft int +.Fn dladdr "const void *addr" "Dl_info *info" +.Sh DESCRIPTION +.Nm +queries the dynamic linker for information about the shared object +containing the address +.Fa addr . +The information is returned in the structure specified by +.Fa info . +The structure contains at least the following members: +.Bl -tag -width "XXXconst char *dli_fname" +.It Li "const char *dli_fname" +The pathname of the shared object containing the address. +.It Li "void *dli_fbase" +The base address at which the shared object is mapped into the +address space of the calling process. +.It Li "const char *dli_sname" +The name of the nearest run-time symbol with a value less than or +equal to +.Fa addr . +When possible, the symbol name is returned as it would appear in C +source code. +.Pp +If no symbol with a suitable value is found, both this field and +.Va dli_saddr +are set to +.Dv NULL . +.It Li "void *dli_saddr" +The value of the symbol returned in +.Li dli_sname . +.El +.Pp +.Nm +is available only in dynamically linked programs. +.Sh ERRORS +If a mapped shared object containing +.Fa addr +cannot be found, +.Nm +returns 0. +In that case, a message detailing the failure can be retrieved by +calling +.Fn dlerror . +.Pp +On success, a non-zero value is returned. +.Sh SEE ALSO +.Xr rtld 1 , +.Xr dlopen 3 +.Sh HISTORY +The +.Nm +function first appeared in the Solaris operating system. +.Sh BUGS +This implementation is bug-compatible with the Solaris +implementation. In particular, the following bugs are present: +.Bl -bullet +.It +If +.Fa addr +lies in the main executable rather than in a shared library, the +pathname returned in +.Va dli_fname +may not be correct. The pathname is taken directly from +.Va argv[0] +of the calling process. When executing a program specified by its +full pathname, most shells set +.Va argv[0] +to the pathname. But this is not required of shells or guaranteed +by the operating system. +.It +If +.Fa addr +is of the form +.Va &func , +where +.Va func +is a global function, its value may be an unpleasant surprise. In +dynamically linked programs, the address of a global function is +considered to point to its program linkage table entry, rather than to +the entry point of the function itself. This causes most global +functions to appear to be defined within the main executable, rather +than in the shared libraries where the actual code resides. +.It +Returning 0 as an indication of failure goes against long-standing +Unix tradition. +.El diff --git a/lib/libc/gen/dlfcn.c b/lib/libc/gen/dlfcn.c new file mode 100644 index 0000000..ff77b79 --- /dev/null +++ b/lib/libc/gen/dlfcn.c @@ -0,0 +1,168 @@ +/*- + * Copyright (c) 1998 John D. Polstra + * All rights reserved. + * + * 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. + * + * 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. + * + * $FreeBSD$ + */ + +/* + * Linkage to services provided by the dynamic linker. These are + * implemented differently in ELF and a.out, because the dynamic + * linkers have different interfaces. + */ + +#ifdef __ELF__ + +#include <dlfcn.h> +#include <stddef.h> + +static const char sorry[] = "Service unavailable"; + +/* + * For ELF, the dynamic linker directly resolves references to its + * services to functions inside the dynamic linker itself. These + * weak-symbol stubs are necessary so that "ld" won't complain about + * undefined symbols. The stubs are executed only when the program is + * linked statically, or when a given service isn't implemented in the + * dynamic linker. They must return an error if called, and they must + * be weak symbols so that the dynamic linker can override them. + */ + +#pragma weak _rtld_error +void +_rtld_error(const char *fmt, ...) +{ +} + +#pragma weak dladdr +int +dladdr(const void *addr, Dl_info *dlip) +{ + _rtld_error(sorry); + return 0; +} + +#pragma weak dlclose +int +dlclose(void *handle) +{ + _rtld_error(sorry); + return -1; +} + +#pragma weak dlerror +const char * +dlerror(void) +{ + return sorry; +} + +#pragma weak dlopen +void * +dlopen(const char *name, int mode) +{ + _rtld_error(sorry); + return NULL; +} + +#pragma weak dlsym +void * +dlsym(void *handle, const char *name) +{ + _rtld_error(sorry); + return NULL; +} + +#else /* a.out format */ + +#include <sys/types.h> +#include <nlist.h> /* XXX - Required by link.h */ +#include <dlfcn.h> +#include <link.h> +#include <stddef.h> + +/* + * For a.out, entry to the dynamic linker is via these trampolines. + * They enter the dynamic linker through the ld_entry struct that was + * passed back from the dynamic linker at startup time. + */ + +/* GCC is needed because we use its __builtin_return_address construct. */ + +#ifndef __GNUC__ +#error "GCC is needed to compile this file" +#endif + +/* + * These variables are set by code in crt0.o. For compatibility with + * old executables, they must be common, not extern. + */ +struct ld_entry *__ldso_entry; /* Entry points to dynamic linker */ +int __ldso_version; /* Dynamic linker version number */ + +int +dladdr(const void *addr, Dl_info *dlip) +{ + if (__ldso_entry == NULL || __ldso_version < LDSO_VERSION_HAS_DLADDR) + return 0; + return (__ldso_entry->dladdr)(addr, dlip); +} + +int +dlclose(void *handle) +{ + if (__ldso_entry == NULL) + return -1; + return (__ldso_entry->dlclose)(handle); +} + +const char * +dlerror(void) +{ + if (__ldso_entry == NULL) + return "Service unavailable"; + return (__ldso_entry->dlerror)(); +} + +void * +dlopen(const char *name, int mode) +{ + if (__ldso_entry == NULL) + return NULL; + return (__ldso_entry->dlopen)(name, mode); +} + +void * +dlsym(void *handle, const char *name) +{ + if (__ldso_entry == NULL) + return NULL; + if (__ldso_version >= LDSO_VERSION_HAS_DLSYM3) { + void *retaddr = __builtin_return_address(0); /* __GNUC__ only */ + return (__ldso_entry->dlsym3)(handle, name, retaddr); + } else + return (__ldso_entry->dlsym)(handle, name); +} + +#endif /* __ELF__ */ diff --git a/lib/libc/gen/dlopen.3 b/lib/libc/gen/dlopen.3 new file mode 100644 index 0000000..6485e75 --- /dev/null +++ b/lib/libc/gen/dlopen.3 @@ -0,0 +1,230 @@ +.\" This source code is a product of Sun Microsystems, Inc. and is provided +.\" for unrestricted use provided that this legend is included on all tape +.\" media and as a part of the software program in whole or part. Users +.\" may copy or modify this source code without charge, but are not authorized +.\" to license or distribute it to anyone else except as part of a product or +.\" program developed by the user. +.\" +.\" THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC. +.\" SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY +.\" OF SUCH SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT +.\" EXPRESS OR IMPLIED WARRANTY OF ANY KIND. SUN MICROSYSTEMS, INC. DISCLAIMS +.\" ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED +.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN +.\" NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT, +.\" INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING +.\" FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY. +.\" +.\" This source code is provided with no support and without any obligation on +.\" the part of Sun Microsystems, Inc. to assist in its use, correction, +.\" modification or enhancement. +.\" +.\" SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE +.\" INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS +.\" SOURCE CODE OR ANY PART THEREOF. +.\" +.\" Sun Microsystems, Inc. +.\" 2550 Garcia Avenue +.\" Mountain View, California 94043 +.\" +.\" Copyright (c) 1991 Sun Microsystems, Inc. +.\" +.\" @(#) dlopen.3 1.6 90/01/31 SMI +.\" $FreeBSD$ +.\" +.Dd September 24, 1989 +.Os FreeBSD +.Dt DLOPEN 3 +.Sh NAME +.Nm dlopen, dlsym, dlerror, dlclose +.Nd programmatic interface to the dynamic linker +.Sh SYNOPSIS +.Fd #include <dlfcn.h> +.Ft void * +.Fn dlopen "const char *path" "int mode" +.Ft void * +.Fn dlsym "void *handle" "const char *symbol" +.Ft const char * +.Fn dlerror "void" +.Ft int +.Fn dlclose "void *handle" +.Sh DESCRIPTION +These functions provide a simple programmatic interface to the services of the +dynamic linker. +Operations are provided to add new shared objects to a +program's address space, to obtain the address bindings of symbols +defined by such +objects, and to remove such objects when their use is no longer required. +.Pp +.Fn dlopen +provides access to the shared object in +.Fa path , +returning a descriptor that can be used for later +references to the object in calls to +.Fn dlsym +and +.Fn dlclose . +If +.Fa path +was not in the address space prior to the call to +.Fn dlopen , +it is placed in the address space. +When an object is first loaded into the address space in this way, its +function +.Fn _init , +if any, is called by the dynamic linker. +If +.Fa path +has already been placed in the address space in a previous call to +.Fn dlopen , +it is not added a second time, although a reference count of +.Fn dlopen +operations on +.Fa path +is maintained. +A null pointer supplied for +.Fa path +is interpreted as a reference to the main +executable of the process. +.Fa mode +controls the way in which external function references from the +loaded object are bound to their referents. +It must contain one of the following values: +.Bl -tag -width RTLD_LAZYX +.It Dv RTLD_LAZY +Each external function reference is resolved when the function is first +called. +.It Dv RTLD_NOW +All external function references are bound immediately by +.Fn dlopen . +.El +.Pp +.Dv RTLD_LAZY +is normally preferred, for reasons of efficiency. +However, +.Dv RTLD_NOW +is useful to ensure that any undefined symbols are discovered during the +call to +.Fn dlopen . +If +.Fn dlopen +fails, it returns a null pointer, and sets an error condition which may +be interrogated with +.Fn dlerror . +.Pp +.Fn dlsym +returns the address binding of the symbol described in the null-terminated +character string +.Fa symbol , +as it occurs in the shared object identified by +.Fa handle . +The symbols exported by objects added to the address space by +.Fn dlopen +can be accessed only through calls to +.Fn dlsym . +Such symbols do not supersede any definition of those symbols already present +in the address space when the object is loaded, nor are they available to +satisfy normal dynamic linking references. +A null pointer supplied as the value of +.Fa handle +is interpreted as a reference to the executable from which the call to +.Fn dlsym +is being made. Thus a shared object can reference its own symbols. +.Fn dlsym +returns a null pointer if the symbol cannot be found, and sets an error +condition which may be queried with +.Fn dlerror . +.Pp +If +.Fn dlsym +is called with the special +.Fa handle +.Dv RTLD_NEXT , +then the search for the symbol is limited to the shared objects +which were loaded after the one issuing the call to +.Fn dlsym . +Thus, if the function is called from the main program, all +the shared libraries are searched. +If it is called from a shared library, all subsequent shared +libraries are searched. +.Dv RTLD_NEXT +is useful for implementing wrappers around library functions. +For example, a wrapper function +.Fn getpid +could access the +.Dq real +.Fn getpid +with +.Li dlsym(RTLD_NEXT, \&"getpid\&") . +.Pp +.Fn dlerror +returns a null-terminated character string describing the last error that +occurred during a call to +.Fn dlopen , +.Fn dlsym , +or +.Fn dlclose . +If no such error has occurred, +.Fn dlerror +returns a null pointer. +At each call to +.Fn dlerror , +the error indication is reset. Thus in the case of two calls +to +.Fn dlerror , +where the second call follows the first immediately, the second call +will always return a null pointer. +.Pp +.Fn dlclose +deletes a reference to the shared object referenced by +.Fa handle . +If the reference count drops to 0, the object is removed from the +address space, and +.Fa handle +is rendered invalid. +Just before removing a shared object in this way, the dynamic linker +calls the object's +.Fn _fini +function, if such a function is defined by the object. +If +.Fn dlclose +is successful, it returns a value of 0. +Otherwise it returns -1, and sets an error condition that can be +interrogated with +.Fn dlerror . +.Pp +The object-intrinsic functions +.Fn _init +and +.Fn _fini +are called with no arguments, and are not expected to return values. +.Sh NOTES +ELF executables need to be linked +using the +.Fl export-dynamic +option to +.Xr ld 1 +for symbols defined in the executable to become visible to +.Fn dlsym . +.Pp +In previous implementations, it was necessary to prepend an underscore +to all external symbols in order to gain symbol +compatibility with object code compiled from the C language. This is +still the case when using the (obsolete) +.Fl aout +option to the C language compiler. +.Sh ERRORS +.Fn dlopen +and +.Fn dlsym +return the null pointer in the event of errors. +.Fn dlclose +returns 0 on success, or -1 if an error occurred. +Whenever an error has been detected, a message detailing it can be +retrieved via a call to +.Fn dlerror . +.Sh SEE ALSO +.Xr ld 1 , +.Xr rtld 1 , +.Xr link 5 + diff --git a/lib/libc/gen/drand48.c b/lib/libc/gen/drand48.c new file mode 100644 index 0000000..cec04a6 --- /dev/null +++ b/lib/libc/gen/drand48.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +extern unsigned short _rand48_seed[3]; + +double +drand48(void) +{ + return erand48(_rand48_seed); +} diff --git a/lib/libc/gen/erand48.c b/lib/libc/gen/erand48.c new file mode 100644 index 0000000..286904c --- /dev/null +++ b/lib/libc/gen/erand48.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +double +erand48(unsigned short xseed[3]) +{ + _dorand48(xseed); + return ldexp((double) xseed[0], -48) + + ldexp((double) xseed[1], -32) + + ldexp((double) xseed[2], -16); +} diff --git a/lib/libc/gen/err.3 b/lib/libc/gen/err.3 new file mode 100644 index 0000000..468fd1f --- /dev/null +++ b/lib/libc/gen/err.3 @@ -0,0 +1,204 @@ +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" From: @(#)err.3 8.1 (Berkeley) 6/9/93 +.\" $FreeBSD$ +.\" +.Dd Mar 6, 1999 +.Dt ERR 3 +.Os BSD 4 +.Sh NAME +.Nm err , +.Nm verr , +.Nm errc , +.Nm verrc , +.Nm errx , +.Nm verrx , +.Nm warn , +.Nm vwarn , +.Nm warnc , +.Nm vwarnc , +.Nm warnx , +.Nm vwarnx , +.Nm err_set_exit , +.Nm err_set_file +.Nd formatted error messages +.Sh SYNOPSIS +.Fd #include <err.h> +.Ft void +.Fn err "int eval" "const char *fmt" "..." +.Ft void +.Fn err_set_exit "void (*exitf)(int)" +.Ft void +.Fn err_set_file "void *vfp" +.Ft void +.Fn errc "int eval" "int code" "const char *fmt" "..." +.Ft void +.Fn errx "int eval" "const char *fmt" "..." +.Ft void +.Fn warn "const char *fmt" "..." +.Ft void +.Fn warnc "int code" "const char *fmt" "..." +.Ft void +.Fn warnx "const char *fmt" "..." +.Fd #include <stdarg.h> +.Ft void +.Fn verr "int eval" "const char *fmt" "va_list args" +.Ft void +.Fn verrc "int eval" "int code" "const char *fmt" "va_list args" +.Ft void +.Fn verrx "int eval" "const char *fmt" "va_list args" +.Ft void +.Fn vwarn "const char *fmt" "va_list args" +.Ft void +.Fn vwarnc "int code" "const char *fmt" "va_list args" +.Ft void +.Fn vwarnx "const char *fmt" "va_list args" +.Sh DESCRIPTION +The +.Fn err +and +.Fn warn +family of functions display a formatted error message on the standard +error output, or on another file specified using the +.Fn err_set_file +function. +In all cases, the last component of the program name, a colon character, +and a space are output. +If the +.Va fmt +argument is not NULL, the formatted error message is output. +In the case of the +.Fn errc , +.Fn verrc , +.Fn warnc , +and +.Fn vwarnc +functions, +the error message string affiliated with the +.Va code +argument is also output, +preceded by another colon and space if necessary. +In all cases, the output is followed by a newline character. +.Pp +The +.Fn err , +.Fn verr , +.Fn warn , +and +.Fn vwarn +functions use the global variable +.Va errno +rather than the +.Va code +argument of the +.Fn errc +family +.Pp +The +.Fn err , +.Fn verr , +.Fn errc , +.Fn verrc , +.Fn errx , +and +.Fn verrx +functions do not return, but exit with the value of the argument +.Fa eval . +The +.Fn err_set_exit +function can be used to specify a function which is called before +.Xr exit 3 +to perform any necessary cleanup; passing a null function pointer for +.Va exitf +resets the hook to do nothing. +The +.Fn err_set_file +function sets the output stream used by the other functions. +Its +.Va vfp +argument must be either a pointer to an open stream +(possibly already converted to void *) +or a null pointer +(in which case the output stream is set to standard error). +.Sh EXAMPLES +Display the current errno information string and exit: +.Bd -literal -offset indent +if ((p = malloc(size)) == NULL) + err(1, NULL); +if ((fd = open(file_name, O_RDONLY, 0)) == -1) + err(1, "%s", file_name); +.Ed +.Pp +Display an error message and exit: +.Bd -literal -offset indent +if (tm.tm_hour < START_TIME) + errx(1, "too early, wait until %s", start_time_string); +.Ed +.Pp +Warn of an error: +.Bd -literal -offset indent +if ((fd = open(raw_device, O_RDONLY, 0)) == -1) + warnx("%s: %s: trying the block device", + raw_device, strerror(errno)); +if ((fd = open(block_device, O_RDONLY, 0)) == -1) + err(1, "%s", block_device); +.Ed +.Pp +Warn of an error without using the global variable +.Va errno : +.Bd -literal -offset indent +error = my_function(); /* returns a value from <errno.h> */ +if (error != 0) + warnc(error, "my_function"); +.Ed +.Sh SEE ALSO +.Xr exit 3 , +.Xr strerror 3 +.Sh HISTORY +The +.Fn err +and +.Fn warn +functions first appeared in +.Bx 4.4 . +The +.Fn err_set_exit +and +.Fn err_set_file +functions first appeared in +.Fx 2.1 . +The +.Fn errc +and +.Fn warnc +functions first appeared in +.Fx 3.0 . diff --git a/lib/libc/gen/err.c b/lib/libc/gen/err.c new file mode 100644 index 0000000..6d1766c --- /dev/null +++ b/lib/libc/gen/err.c @@ -0,0 +1,208 @@ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * From: @(#)err.c 8.1 (Berkeley) 6/4/93 + */ + +#if defined(LIBC_RCS) && !defined(lint) +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_RCS and not lint */ + +#include <err.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <stdarg.h> + +extern char *__progname; /* Program name, from crt0. */ + +static FILE *err_file; /* file to use for error output */ +static void (*err_exit)(int); + +/* + * This is declared to take a `void *' so that the caller is not required + * to include <stdio.h> first. However, it is really a `FILE *', and the + * manual page documents it as such. + */ +void +err_set_file(void *fp) +{ + if (fp) + err_file = fp; + else + err_file = stderr; +} + +void +err_set_exit(void (*ef)(int)) +{ + err_exit = ef; +} + +void +err(int eval, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verrc(eval, errno, fmt, ap); + va_end(ap); +} + +void +verr(eval, fmt, ap) + int eval; + const char *fmt; + va_list ap; +{ + verrc(eval, errno, fmt, ap); +} + +void +errc(int eval, int code, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verrc(eval, code, fmt, ap); + va_end(ap); +} + +void +verrc(eval, code, fmt, ap) + int eval; + int code; + const char *fmt; + va_list ap; +{ + if (err_file == 0) + err_set_file((FILE *)0); + fprintf(err_file, "%s: ", __progname); + if (fmt != NULL) { + vfprintf(err_file, fmt, ap); + fprintf(err_file, ": "); + } + fprintf(err_file, "%s\n", strerror(code)); + if (err_exit) + err_exit(eval); + exit(eval); +} + +void +errx(int eval, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verrx(eval, fmt, ap); + va_end(ap); +} + +void +verrx(eval, fmt, ap) + int eval; + const char *fmt; + va_list ap; +{ + if (err_file == 0) + err_set_file((FILE *)0); + fprintf(err_file, "%s: ", __progname); + if (fmt != NULL) + vfprintf(err_file, fmt, ap); + fprintf(err_file, "\n"); + if (err_exit) + err_exit(eval); + exit(eval); +} + +void +warn(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnc(errno, fmt, ap); + va_end(ap); +} + +void +vwarn(fmt, ap) + const char *fmt; + va_list ap; +{ + vwarnc(errno, fmt, ap); +} + +void +warnc(int code, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnc(code, fmt, ap); + va_end(ap); +} + +void +vwarnc(code, fmt, ap) + int code; + const char *fmt; + va_list ap; +{ + if (err_file == 0) + err_set_file((FILE *)0); + fprintf(err_file, "%s: ", __progname); + if (fmt != NULL) { + vfprintf(err_file, fmt, ap); + fprintf(err_file, ": "); + } + fprintf(err_file, "%s\n", strerror(code)); +} + +void +warnx(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); +} + +void +vwarnx(fmt, ap) + const char *fmt; + va_list ap; +{ + if (err_file == 0) + err_set_file((FILE *)0); + fprintf(err_file, "%s: ", __progname); + if (fmt != NULL) + vfprintf(err_file, fmt, ap); + fprintf(err_file, "\n"); +} diff --git a/lib/libc/gen/errlst.c b/lib/libc/gen/errlst.c new file mode 100644 index 0000000..a643794 --- /dev/null +++ b/lib/libc/gen/errlst.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 1982, 1985, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)errlst.c 8.2 (Berkeley) 11/16/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <stdio.h> + +const char *const sys_errlist[] = { + "Undefined error: 0", /* 0 - ENOERROR */ + "Operation not permitted", /* 1 - EPERM */ + "No such file or directory", /* 2 - ENOENT */ + "No such process", /* 3 - ESRCH */ + "Interrupted system call", /* 4 - EINTR */ + "Input/output error", /* 5 - EIO */ + "Device not configured", /* 6 - ENXIO */ + "Argument list too long", /* 7 - E2BIG */ + "Exec format error", /* 8 - ENOEXEC */ + "Bad file descriptor", /* 9 - EBADF */ + "No child processes", /* 10 - ECHILD */ + "Resource deadlock avoided", /* 11 - EDEADLK */ + "Cannot allocate memory", /* 12 - ENOMEM */ + "Permission denied", /* 13 - EACCES */ + "Bad address", /* 14 - EFAULT */ + "Block device required", /* 15 - ENOTBLK */ + "Device busy", /* 16 - EBUSY */ + "File exists", /* 17 - EEXIST */ + "Cross-device link", /* 18 - EXDEV */ + "Operation not supported by device", /* 19 - ENODEV */ + "Not a directory", /* 20 - ENOTDIR */ + "Is a directory", /* 21 - EISDIR */ + "Invalid argument", /* 22 - EINVAL */ + "Too many open files in system", /* 23 - ENFILE */ + "Too many open files", /* 24 - EMFILE */ + "Inappropriate ioctl for device", /* 25 - ENOTTY */ + "Text file busy", /* 26 - ETXTBSY */ + "File too large", /* 27 - EFBIG */ + "No space left on device", /* 28 - ENOSPC */ + "Illegal seek", /* 29 - ESPIPE */ + "Read-only file system", /* 30 - EROFS */ + "Too many links", /* 31 - EMLINK */ + "Broken pipe", /* 32 - EPIPE */ + +/* math software */ + "Numerical argument out of domain", /* 33 - EDOM */ + "Result too large", /* 34 - ERANGE */ + +/* non-blocking and interrupt i/o */ + "Resource temporarily unavailable", /* 35 - EAGAIN */ + /* 35 - EWOULDBLOCK */ + "Operation now in progress", /* 36 - EINPROGRESS */ + "Operation already in progress", /* 37 - EALREADY */ + +/* ipc/network software -- argument errors */ + "Socket operation on non-socket", /* 38 - ENOTSOCK */ + "Destination address required", /* 39 - EDESTADDRREQ */ + "Message too long", /* 40 - EMSGSIZE */ + "Protocol wrong type for socket", /* 41 - EPROTOTYPE */ + "Protocol not available", /* 42 - ENOPROTOOPT */ + "Protocol not supported", /* 43 - EPROTONOSUPPORT */ + "Socket type not supported", /* 44 - ESOCKTNOSUPPORT */ + "Operation not supported", /* 45 - EOPNOTSUPP */ + "Protocol family not supported", /* 46 - EPFNOSUPPORT */ + /* 47 - EAFNOSUPPORT */ + "Address family not supported by protocol family", + "Address already in use", /* 48 - EADDRINUSE */ + "Can't assign requested address", /* 49 - EADDRNOTAVAIL */ + +/* ipc/network software -- operational errors */ + "Network is down", /* 50 - ENETDOWN */ + "Network is unreachable", /* 51 - ENETUNREACH */ + "Network dropped connection on reset", /* 52 - ENETRESET */ + "Software caused connection abort", /* 53 - ECONNABORTED */ + "Connection reset by peer", /* 54 - ECONNRESET */ + "No buffer space available", /* 55 - ENOBUFS */ + "Socket is already connected", /* 56 - EISCONN */ + "Socket is not connected", /* 57 - ENOTCONN */ + "Can't send after socket shutdown", /* 58 - ESHUTDOWN */ + "Too many references: can't splice", /* 59 - ETOOMANYREFS */ + "Operation timed out", /* 60 - ETIMEDOUT */ + "Connection refused", /* 61 - ECONNREFUSED */ + + "Too many levels of symbolic links", /* 62 - ELOOP */ + "File name too long", /* 63 - ENAMETOOLONG */ + +/* should be rearranged */ + "Host is down", /* 64 - EHOSTDOWN */ + "No route to host", /* 65 - EHOSTUNREACH */ + "Directory not empty", /* 66 - ENOTEMPTY */ + +/* quotas & mush */ + "Too many processes", /* 67 - EPROCLIM */ + "Too many users", /* 68 - EUSERS */ + "Disc quota exceeded", /* 69 - EDQUOT */ + +/* Network File System */ + "Stale NFS file handle", /* 70 - ESTALE */ + "Too many levels of remote in path", /* 71 - EREMOTE */ + "RPC struct is bad", /* 72 - EBADRPC */ + "RPC version wrong", /* 73 - ERPCMISMATCH */ + "RPC prog. not avail", /* 74 - EPROGUNAVAIL */ + "Program version wrong", /* 75 - EPROGMISMATCH */ + "Bad procedure for program", /* 76 - EPROCUNAVAIL */ + + "No locks available", /* 77 - ENOLCK */ + "Function not implemented", /* 78 - ENOSYS */ + "Inappropriate file type or format", /* 79 - EFTYPE */ + "Authentication error", /* 80 - EAUTH */ + "Need authenticator", /* 81 - ENEEDAUTH */ + "Identifier removed", /* 82 - EIDRM */ + "No message of desired type", /* 83 - ENOMSG */ + "Value too large to be stored in data type", /* 84 - EOVERFLOW */ + "Operation canceled", /* 85 - ECANCELED */ + "Illegal byte sequence", /* 86 - EILSEQ */ +}; +int errno; +const int sys_nerr = sizeof(sys_errlist) / sizeof(sys_errlist[0]); diff --git a/lib/libc/gen/exec.3 b/lib/libc/gen/exec.3 new file mode 100644 index 0000000..8da1247 --- /dev/null +++ b/lib/libc/gen/exec.3 @@ -0,0 +1,291 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)exec.3 8.3 (Berkeley) 1/24/94 +.\" $FreeBSD$ +.\" +.Dd January 24, 1994 +.Dt EXEC 3 +.Os +.Sh NAME +.Nm execl , +.Nm execlp , +.Nm execle , +.Nm exect , +.Nm execv , +.Nm execvp +.Nd execute a file +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Vt extern char **environ; +.Ft int +.Fn execl "const char *path" "const char *arg" ... +.Ft int +.Fn execlp "const char *file" "const char *arg" ... +.Ft int +.Fn execle "const char *path" "const char *arg" ... +.Ft int +.Fn exect "const char *path" "char *const argv[]" "char *const envp[]" +.Ft int +.Fn execv "const char *path" "char *const argv[]" +.Ft int +.Fn execvp "const char *file" "char *const argv[]" +.Sh DESCRIPTION +The +.Nm exec +family of functions replaces the current process image with a +new process image. +The functions described in this manual page are front-ends for the function +.Xr execve 2 . +(See the manual page for +.Xr execve 2 +for detailed information about the replacement of the current process.) +.Pp +The initial argument for these functions is the pathname of a file which +is to be executed. +.Pp +The +.Fa "const char *arg" +and subsequent ellipses in the +.Fn execl , +.Fn execlp , +and +.Fn execle +functions can be thought of as +.Em arg0 , +.Em arg1 , +\&..., +.Em argn . +Together they describe a list of one or more pointers to null-terminated +strings that represent the argument list available to the executed program. +The first argument, by convention, should point to the file name associated +with the file being executed. +The list of arguments +.Em must +be terminated by a +.Dv NULL +pointer. +.Pp +The +.Fn exect , +.Fn execv , +and +.Fn execvp +functions provide an array of pointers to null-terminated strings that +represent the argument list available to the new program. +The first argument, by convention, should point to the file name associated +with the file begin executed. +The array of pointers +.Sy must +be terminated by a +.Dv NULL +pointer. +.Pp +The +.Fn execle +and +.Fn exect +functions also specify the environment of the executed process by following +the +.Dv NULL +pointer that terminates the list of arguments in the parameter list +or the pointer to the argv array with an additional parameter. +This additional parameter is an array of pointers to null-terminated strings +and +.Em must +be terminated by a +.Dv NULL +pointer. +The other functions take the environment for the new process image from the +external variable +.Va environ +in the current process. +.Pp +Some of these functions have special semantics. +.Pp +The functions +.Fn execlp +and +.Fn execvp +will duplicate the actions of the shell in searching for an executable file +if the specified file name does not contain a slash +.Dq Li / +character. +The search path is the path specified in the environment by +.Dq Ev PATH +variable. +If this variable isn't specified, the default path +.Dq Ev /bin:/usr/bin: +is +used. +In addition, certain errors are treated specially. +.Pp +If an error is ambiguous (for simplicity, we shall consider all +errors except +.Er ENOEXEC +as being ambiguous here, although only the critical error +.Er EACCES +is really ambiguous), +then these functions will act as if they stat the file to determine +whether the file exists and has suitable execute permissions. +If it does, they will return immediately with the global variable +.Va errno +restored to the value set by +.Fn execve . +Otherwise, the search will be continued. +If the search completes without performing a successful +.Fn execve +or terminating due to an error, +these functions will return with the global variable +.Va errno +set to +.Er EACCES +or +.Er ENOENT +according to whether at least one file with suitable execute permissions +was found. +.Pp +If the header of a file isn't recognized (the attempted +.Fn execve +returned +.Er ENOEXEC ) , +these functions will execute the shell with the path of +the file as its first argument. +(If this attempt fails, no further searching is done.) +.Pp +The function +.Fn exect +executes a file with the program tracing facilities enabled (see +.Xr ptrace 2 ) . +.Sh RETURN VALUES +If any of the +.Fn exec +functions returns, an error will have occurred. +The return value is \-1, and the global variable +.Va errno +will be set to indicate the error. +.Sh FILES +.Bl -tag -width /bin/sh -compact +.It Pa /bin/sh +The shell. +.El +.Sh ERRORS +.Fn Execl , +.Fn execle , +.Fn execlp +and +.Fn execvp +may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr execve 2 +and +.Xr malloc 3 . +.Pp +.Fn Exect +and +.Fn execv +may fail and set +.Va errno +for any of the errors specified for the library function +.Xr execve 2 . +.Sh SEE ALSO +.Xr sh 1 , +.Xr execve 2 , +.Xr fork 2 , +.Xr ktrace 2 , +.Xr ptrace 2 , +.Xr environ 7 . +.Sh COMPATIBILITY +Historically, the default path for the +.Fn execlp +and +.Fn execvp +functions was +.Dq Pa :/bin:/usr/bin . +This was changed to place the current directory last to enhance system +security. +.Pp +The behavior of +.Fn execlp +and +.Fn execvp +when errors occur while attempting to execute the file is not quite historic +practice, and has not traditionally been documented and is not specified +by the +.Tn POSIX +standard. +.Pp +Traditionally, the functions +.Fn execlp +and +.Fn execvp +ignored all errors except for the ones described above and +.Er ETXTBSY , +upon which they retried after sleeping for several seconds, and +.Er ENOMEM +and +.Er E2BIG , +upon which they returned. +They now return for +.Er ETXTBSY , +and determine existence and executability more carefully. +In particular, +.Er EACCES +for inaccessible directories in the path prefix is no longer +confused with +.Er EACCES +for files with unsuitable execute permissions. +In +.Bx 4.4 , +they returned upon all errors except +.Er EACCES , +.Er ENOENT , +.Er ENOEXEC +and +.Er ETXTBSY . +This was inferior to the traditional error handling, +since it it breaks the ignoring of errors for path prefixes +and only improves the handling of the unusual ambiguous error +.Er EFAULT +and the unusual error +.Er EIO . +The behaviour was changed to match the behaviour of +.Xr sh 1 . +.Sh STANDARDS +.Fn Execl , +.Fn execv , +.Fn execle , +.Fn execlp +and +.Fn execvp +conform to +.St -p1003.1-88 . diff --git a/lib/libc/gen/exec.c b/lib/libc/gen/exec.c new file mode 100644 index 0000000..af313a4 --- /dev/null +++ b/lib/libc/gen/exec.c @@ -0,0 +1,315 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 6/4/93"; +#endif +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <paths.h> + +#if __STDC__ +#include <stdarg.h> +#else +#include <varargs.h> +#endif + +extern char **environ; + +int +#if __STDC__ +execl(const char *name, const char *arg, ...) +#else +execl(name, arg, va_alist) + const char *name; + const char *arg; + va_dcl +#endif +{ + va_list ap; + char **argv; + int n; + +#if __STDC__ + va_start(ap, arg); +#else + va_start(ap); +#endif + n = 1; + while (va_arg(ap, char *) != NULL) + n++; + va_end(ap); + argv = alloca((n + 1) * sizeof(*argv)); + if (argv == NULL) { + errno = ENOMEM; + return (-1); + } +#if __STDC__ + va_start(ap, arg); +#else + va_start(ap); +#endif + n = 1; + argv[0] = (char *)arg; + while ((argv[n] = va_arg(ap, char *)) != NULL) + n++; + va_end(ap); + return (execve(name, argv, environ)); +} + +int +#if __STDC__ +execle(const char *name, const char *arg, ...) +#else +execle(name, arg, va_alist) + const char *name; + const char *arg; + va_dcl +#endif +{ + va_list ap; + char **argv, **envp; + int n; + +#if __STDC__ + va_start(ap, arg); +#else + va_start(ap); +#endif + n = 1; + while (va_arg(ap, char *) != NULL) + n++; + va_end(ap); + argv = alloca((n + 1) * sizeof(*argv)); + if (argv == NULL) { + errno = ENOMEM; + return (-1); + } +#if __STDC__ + va_start(ap, arg); +#else + va_start(ap); +#endif + n = 1; + argv[0] = (char *)arg; + while ((argv[n] = va_arg(ap, char *)) != NULL) + n++; + envp = va_arg(ap, char **); + va_end(ap); + return (execve(name, argv, envp)); +} + +int +#if __STDC__ +execlp(const char *name, const char *arg, ...) +#else +execlp(name, arg, va_alist) + const char *name; + const char *arg; + va_dcl +#endif +{ + va_list ap; + int sverrno; + char **argv; + int n; + +#if __STDC__ + va_start(ap, arg); +#else + va_start(ap); +#endif + n = 1; + while (va_arg(ap, char *) != NULL) + n++; + va_end(ap); + argv = alloca((n + 1) * sizeof(*argv)); + if (argv == NULL) { + errno = ENOMEM; + return (-1); + } +#if __STDC__ + va_start(ap, arg); +#else + va_start(ap); +#endif + n = 1; + argv[0] = (char *)arg; + while ((argv[n] = va_arg(ap, char *)) != NULL) + n++; + va_end(ap); + return (execvp(name, argv)); +} + +int +execv(name, argv) + const char *name; + char * const *argv; +{ + (void)execve(name, argv, environ); + return (-1); +} + +int +execvp(name, argv) + const char *name; + char * const *argv; +{ + char **memp; + register int cnt, lp, ln; + register char *p; + int eacces, save_errno; + char *bp, *cur, *path, buf[MAXPATHLEN]; + struct stat sb; + + eacces = 0; + + /* If it's an absolute or relative path name, it's easy. */ + if (index(name, '/')) { + bp = (char *)name; + cur = path = NULL; + goto retry; + } + bp = buf; + + /* If it's an empty path name, fail in the usual POSIX way. */ + if (*name == '\0') { + errno = ENOENT; + return (-1); + } + + /* Get the path we're searching. */ + if (!(path = getenv("PATH"))) + path = _PATH_DEFPATH; + cur = alloca(strlen(path) + 1); + if (cur == NULL) { + errno = ENOMEM; + return (-1); + } + strcpy(cur, path); + path = cur; + while ( (p = strsep(&cur, ":")) ) { + /* + * It's a SHELL path -- double, leading and trailing colons + * mean the current directory. + */ + if (!*p) { + p = "."; + lp = 1; + } else + lp = strlen(p); + ln = strlen(name); + + /* + * If the path is too long complain. This is a possible + * security issue; given a way to make the path too long + * the user may execute the wrong program. + */ + if (lp + ln + 2 > sizeof(buf)) { + (void)write(STDERR_FILENO, "execvp: ", 8); + (void)write(STDERR_FILENO, p, lp); + (void)write(STDERR_FILENO, ": path too long\n", 16); + continue; + } + bcopy(p, buf, lp); + buf[lp] = '/'; + bcopy(name, buf + lp + 1, ln); + buf[lp + ln + 1] = '\0'; + +retry: (void)execve(bp, argv, environ); + switch(errno) { + case E2BIG: + goto done; + case ELOOP: + case ENAMETOOLONG: + case ENOENT: + break; + case ENOEXEC: + for (cnt = 0; argv[cnt]; ++cnt) + ; + memp = alloca((cnt + 2) * sizeof(char *)); + if (memp == NULL) { + /* errno = ENOMEM; XXX override ENOEXEC? */ + goto done; + } + memp[0] = "sh"; + memp[1] = bp; + bcopy(argv + 1, memp + 2, cnt * sizeof(char *)); + (void)execve(_PATH_BSHELL, memp, environ); + goto done; + case ENOMEM: + goto done; + case ENOTDIR: + break; + case ETXTBSY: + /* + * We used to retry here, but sh(1) doesn't. + */ + goto done; + default: + /* + * EACCES may be for an inaccessible directory or + * a non-executable file. Call stat() to decide + * which. This also handles ambiguities for EFAULT + * and EIO, and undocumented errors like ESTALE. + * We hope that the race for a stat() is unimportant. + */ + save_errno = errno; + if (stat(bp, &sb) != 0) + break; + if (save_errno == EACCES) { + eacces = 1; + continue; + } + errno = save_errno; + goto done; + } + } + if (eacces) + errno = EACCES; + else + errno = ENOENT; +done: + return (-1); +} diff --git a/lib/libc/gen/fnmatch.3 b/lib/libc/gen/fnmatch.3 new file mode 100644 index 0000000..8b556b5 --- /dev/null +++ b/lib/libc/gen/fnmatch.3 @@ -0,0 +1,149 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Guido van Rossum. +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)fnmatch.3 8.3 (Berkeley) 4/28/95 +.\" $FreeBSD$ +.\" +.Dd April 28, 1995 +.Dt FNMATCH 3 +.Os +.Sh NAME +.Nm fnmatch +.Nd match filename or pathname +.Sh SYNOPSIS +.Fd #include <fnmatch.h> +.Ft int +.Fn fnmatch "const char *pattern" "const char *string" "int flags" +.Sh DESCRIPTION +The +.Fn fnmatch +function +matches patterns according to the rules used by the shell. +It checks the string specified by the +.Fa string +argument to see if it matches the pattern specified by the +.Fa pattern +argument. +.Pp +The +.Fa flags +argument modifies the interpretation of +.Fa pattern +and +.Fa string . +The value of +.Fa flags +is the bitwise inclusive +.Tn OR +of any of the following +constants, which are defined in the include file +.Pa fnmatch.h . +.Bl -tag -width FNM_PATHNAME +.It Dv FNM_NOESCAPE +Normally, every occurrence of a backslash +.Pq Ql \e +followed by a character in +.Fa pattern +is replaced by that character. +This is done to negate any special meaning for the character. +If the +.Dv FNM_NOESCAPE +flag is set, a backslash character is treated as an ordinary character. +.It Dv FNM_PATHNAME +Slash characters in +.Fa string +must be explicitly matched by slashes in +.Fa pattern . +If this flag is not set, then slashes are treated as regular characters. +.It Dv FNM_PERIOD +Leading periods in +.Fa string +must be explicitly matched by periods in +.Fa pattern . +If this flag is not set, then leading periods are treated as regular +characters. +The definition of +.Dq leading +is related to the specification of +.Dv FNM_PATHNAME . +A period is always +.Dq leading +if it is the first character in +.Ar string . +Additionally, if +.Dv FNM_PATHNAME +is set, +a period is +leading +if it immediately follows a slash. +.It Dv FNM_LEADING_DIR +Ignore +.Nm /* +rest after successful +.Fa pattern +matching. +.It Dv FNM_CASEFOLD +Ignore case distinctions in both the +.Fa pattern +and the +.Fa string . +.El +.Sh RETURN VALUES +The +.Fn fnmatch +function returns zero if +.Fa string +matches the pattern specified by +.Fa pattern , +otherwise, it returns the value +.Dv FNM_NOMATCH . +.Sh SEE ALSO +.Xr sh 1 , +.Xr glob 3 , +.Xr regex 3 +.Sh STANDARDS +The +.Fn fnmatch +function conforms to +.St -p1003.2 . +.Sh HISTORY +The +.Fn fnmatch +function first appeared in +.Bx 4.4 . +.Sh BUGS +The pattern +.Ql * +matches the empty string, even if +.Dv FNM_PATHNAME +is specified. diff --git a/lib/libc/gen/fnmatch.c b/lib/libc/gen/fnmatch.c new file mode 100644 index 0000000..c3312b4 --- /dev/null +++ b/lib/libc/gen/fnmatch.c @@ -0,0 +1,231 @@ +/* + * Copyright (c) 1989, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Guido van Rossum. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; +#endif /* LIBC_SCCS and not lint */ + +/* + * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. + * Compares a filename or pathname to a pattern. + */ + +#include <ctype.h> +#include <fnmatch.h> +#include <string.h> +#include <stdio.h> + +#include "collate.h" + +#define EOS '\0' + +#define RANGE_MATCH 1 +#define RANGE_NOMATCH 0 +#define RANGE_ERROR (-1) + +static int rangematch __P((const char *, char, int, char **)); + +int +fnmatch(pattern, string, flags) + const char *pattern, *string; + int flags; +{ + const char *stringstart; + char *newp; + char c, test; + + for (stringstart = string;;) + switch (c = *pattern++) { + case EOS: + if ((flags & FNM_LEADING_DIR) && *string == '/') + return (0); + return (*string == EOS ? 0 : FNM_NOMATCH); + case '?': + if (*string == EOS) + return (FNM_NOMATCH); + if (*string == '/' && (flags & FNM_PATHNAME)) + return (FNM_NOMATCH); + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + ++string; + break; + case '*': + c = *pattern; + /* Collapse multiple stars. */ + while (c == '*') + c = *++pattern; + + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + + /* Optimize for pattern with * at end or before /. */ + if (c == EOS) + if (flags & FNM_PATHNAME) + return ((flags & FNM_LEADING_DIR) || + strchr(string, '/') == NULL ? + 0 : FNM_NOMATCH); + else + return (0); + else if (c == '/' && flags & FNM_PATHNAME) { + if ((string = strchr(string, '/')) == NULL) + return (FNM_NOMATCH); + break; + } + + /* General case, use recursion. */ + while ((test = *string) != EOS) { + if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) + return (0); + if (test == '/' && flags & FNM_PATHNAME) + break; + ++string; + } + return (FNM_NOMATCH); + case '[': + if (*string == EOS) + return (FNM_NOMATCH); + if (*string == '/' && (flags & FNM_PATHNAME)) + return (FNM_NOMATCH); + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + + switch (rangematch(pattern, *string, flags, &newp)) { + case RANGE_ERROR: + goto norm; + case RANGE_MATCH: + pattern = newp; + break; + case RANGE_NOMATCH: + return (FNM_NOMATCH); + } + ++string; + break; + case '\\': + if (!(flags & FNM_NOESCAPE)) { + if ((c = *pattern++) == EOS) { + c = '\\'; + --pattern; + } + } + /* FALLTHROUGH */ + default: + norm: + if (c == *string) + ; + else if ((flags & FNM_CASEFOLD) && + (tolower((unsigned char)c) == + tolower((unsigned char)*string))) + ; + else + return (FNM_NOMATCH); + string++; + break; + } + /* NOTREACHED */ +} + +static int +rangematch(pattern, test, flags, newp) + const char *pattern; + char test; + int flags; + char **newp; +{ + int negate, ok; + char c, c2; + + /* + * A bracket expression starting with an unquoted circumflex + * character produces unspecified results (IEEE 1003.2-1992, + * 3.13.2). This implementation treats it like '!', for + * consistency with the regular expression syntax. + * J.T. Conklin (conklin@ngai.kaleida.com) + */ + if ( (negate = (*pattern == '!' || *pattern == '^')) ) + ++pattern; + + if (flags & FNM_CASEFOLD) + test = tolower((unsigned char)test); + + /* + * A right bracket shall lose its special meaning and represent + * itself in a bracket expression if it occurs first in the list. + * -- POSIX.2 2.8.3.2 + */ + ok = 0; + c = *pattern++; + do { + if (c == '\\' && !(flags & FNM_NOESCAPE)) + c = *pattern++; + if (c == EOS) + return (RANGE_ERROR); + + if (c == '/' && (flags & FNM_PATHNAME)) + return (RANGE_NOMATCH); + + if (flags & FNM_CASEFOLD) + c = tolower((unsigned char)c); + + if (*pattern == '-' + && (c2 = *(pattern+1)) != EOS && c2 != ']') { + pattern += 2; + if (c2 == '\\' && !(flags & FNM_NOESCAPE)) + c2 = *pattern++; + if (c2 == EOS) + return (RANGE_ERROR); + + if (flags & FNM_CASEFOLD) + c2 = tolower((unsigned char)c2); + + if (__collate_load_error ? + c <= test && test <= c2 : + __collate_range_cmp(c, test) <= 0 + && __collate_range_cmp(test, c2) <= 0 + ) + ok = 1; + } else if (c == test) + ok = 1; + } while ((c = *pattern++) != ']'); + + *newp = (char *)pattern; + return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); +} diff --git a/lib/libc/gen/frexp.3 b/lib/libc/gen/frexp.3 new file mode 100644 index 0000000..f2a6b5c --- /dev/null +++ b/lib/libc/gen/frexp.3 @@ -0,0 +1,86 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)frexp.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt FREXP 3 +.Os +.Sh NAME +.Nm frexp +.Nd convert floating-point number to fractional and integral components +.Sh SYNOPSIS +.Fd #include <math.h> +.Ft double +.Fn frexp "double value" "int *exp" +.Sh DESCRIPTION +The +.Fn frexp +function breaks a floating-point number into a normalized +fraction and an integral power of 2. +It stores the integer in the +.Em int +object pointed to by +.Fa exp . +.Sh RETURN VALUES +The +.Fn frexp +function returns the value +.Em x , +such that +.Em x +is a +.Em double +with magnitude in the interval +.Bq 1/2 , 1 +or zero, and +.Fa value +equals +.Em x +times 2 raised to the power +.Fa *exp . +If +.Fa value +is zero, both parts of the result are zero. +.Sh SEE ALSO +.Xr ldexp 3 , +.Xr math 3 , +.Xr modf 3 +.Sh STANDARDS +The +.Fn frexp +function conforms to +.St -ansiC . diff --git a/lib/libc/gen/fstab.c b/lib/libc/gen/fstab.c new file mode 100644 index 0000000..65b02c1 --- /dev/null +++ b/lib/libc/gen/fstab.c @@ -0,0 +1,256 @@ +/* + * Copyright (c) 1980, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/mount.h> +#include <sys/stat.h> + +#include <errno.h> +#include <fstab.h> +#include <paths.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static FILE *_fs_fp; +static struct fstab _fs_fstab; +static int LineNo = 0; + +static void error __P((int)); +static void fixfsfile __P((void)); +static int fstabscan __P((void)); + +static void +fixfsfile() +{ + static char buf[sizeof(_PATH_DEV) + MNAMELEN]; + struct stat sb; + struct statfs sf; + + if (strcmp(_fs_fstab.fs_file, "/") != 0) + return; + if (statfs("/", &sf) != 0) + return; + if (sf.f_mntfromname[0] == '/') + buf[0] = '\0'; + else + strcpy(buf, _PATH_DEV); + strcat(buf, sf.f_mntfromname); + if (stat(buf, &sb) != 0 || !S_ISBLK(sb.st_mode)) + return; + _fs_fstab.fs_spec = buf; +} + +static int +fstabscan() +{ + char *cp, *p; +#define MAXLINELENGTH 1024 + static char line[MAXLINELENGTH]; + char subline[MAXLINELENGTH]; + int typexx; + + for (;;) { + + if (!(p = fgets(line, sizeof(line), _fs_fp))) + return(0); +/* OLD_STYLE_FSTAB */ + ++LineNo; + if (*line == '#' || *line == '\n') + continue; + if (!strpbrk(p, " \t")) { + _fs_fstab.fs_spec = strsep(&p, ":\n"); + _fs_fstab.fs_file = strsep(&p, ":\n"); + fixfsfile(); + _fs_fstab.fs_type = strsep(&p, ":\n"); + if (_fs_fstab.fs_type) { + if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) + continue; + _fs_fstab.fs_mntops = _fs_fstab.fs_type; + _fs_fstab.fs_vfstype = + strcmp(_fs_fstab.fs_type, FSTAB_SW) ? + "ufs" : "swap"; + if ((cp = strsep(&p, ":\n")) != NULL) { + _fs_fstab.fs_freq = atoi(cp); + if ((cp = strsep(&p, ":\n")) != NULL) { + _fs_fstab.fs_passno = atoi(cp); + return(1); + } + } + } + goto bad; + } +/* OLD_STYLE_FSTAB */ + while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') + ; + _fs_fstab.fs_spec = cp; + if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') + continue; + while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') + ; + _fs_fstab.fs_file = cp; + fixfsfile(); + while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') + ; + _fs_fstab.fs_vfstype = cp; + while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') + ; + _fs_fstab.fs_mntops = cp; + if (_fs_fstab.fs_mntops == NULL) + goto bad; + _fs_fstab.fs_freq = 0; + _fs_fstab.fs_passno = 0; + while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') + ; + if (cp != NULL) { + _fs_fstab.fs_freq = atoi(cp); + while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') + ; + if (cp != NULL) + _fs_fstab.fs_passno = atoi(cp); + } + strcpy(subline, _fs_fstab.fs_mntops); + p = subline; + for (typexx = 0, cp = strsep(&p, ","); cp; + cp = strsep(&p, ",")) { + if (strlen(cp) != 2) + continue; + if (!strcmp(cp, FSTAB_RW)) { + _fs_fstab.fs_type = FSTAB_RW; + break; + } + if (!strcmp(cp, FSTAB_RQ)) { + _fs_fstab.fs_type = FSTAB_RQ; + break; + } + if (!strcmp(cp, FSTAB_RO)) { + _fs_fstab.fs_type = FSTAB_RO; + break; + } + if (!strcmp(cp, FSTAB_SW)) { + _fs_fstab.fs_type = FSTAB_SW; + break; + } + if (!strcmp(cp, FSTAB_XX)) { + _fs_fstab.fs_type = FSTAB_XX; + typexx++; + break; + } + } + if (typexx) + continue; + if (cp != NULL) + return(1); + +bad: /* no way to distinguish between EOF and syntax error */ + error(EFTYPE); + } + /* NOTREACHED */ +} + +struct fstab * +getfsent() +{ + if ((!_fs_fp && !setfsent()) || !fstabscan()) + return((struct fstab *)NULL); + return(&_fs_fstab); +} + +struct fstab * +getfsspec(name) + register const char *name; +{ + if (setfsent()) + while (fstabscan()) + if (!strcmp(_fs_fstab.fs_spec, name)) + return(&_fs_fstab); + return((struct fstab *)NULL); +} + +struct fstab * +getfsfile(name) + register const char *name; +{ + if (setfsent()) + while (fstabscan()) + if (!strcmp(_fs_fstab.fs_file, name)) + return(&_fs_fstab); + return((struct fstab *)NULL); +} + +int +setfsent() +{ + if (_fs_fp) { + rewind(_fs_fp); + LineNo = 0; + return(1); + } + if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) { + LineNo = 0; + return(1); + } + error(errno); + return(0); +} + +void +endfsent() +{ + if (_fs_fp) { + (void)fclose(_fs_fp); + _fs_fp = NULL; + } +} + +static void +error(err) + int err; +{ + char *p; + char num[30]; + + (void)write(STDERR_FILENO, "fstab: ", 7); + (void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1); + (void)write(STDERR_FILENO, ":", 1); + sprintf(num, "%d: ", LineNo); + (void)write(STDERR_FILENO, num, strlen(num)); + p = strerror(err); + (void)write(STDERR_FILENO, p, strlen(p)); + (void)write(STDERR_FILENO, "\n", 1); +} diff --git a/lib/libc/gen/ftok.3 b/lib/libc/gen/ftok.3 new file mode 100644 index 0000000..611bf30 --- /dev/null +++ b/lib/libc/gen/ftok.3 @@ -0,0 +1,83 @@ +.\" Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com> +.\" All rights reserved. +.\" +.\" 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 ``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 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. +.\" +.\" $FreeBSD$ +.Dd June 24, 1994 +.Os +.Dt FTOK 3 +.Sh NAME +.Nm ftok +.Nd create IPC identifier from path name +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <sys/ipc.h> +.Ft key_t +.Fn ftok "const char *path" "int id" +.Sh DESCRIPTION +.Bf -symbolic +This function is available from the compatibility library, libcompat. +.Ef +The +.Fn ftok +function attempts to create a unique key suitable for use with the +.Xr msgget 3 , +.Xr semget 2 +and +.Xr shmget 2 +functions given the +.Fa path +of an existing file and a user-selectable +.Fa id . +.Pp +The specified +.Fa path +must specify an existing file that is accessible to the calling process +or the call will fail. Also, note that links to files will return the +same key, given the same +.Fa id . +.Sh RETURN VALUES +The +.Fn ftok +function will return -1 if +.Fa path +does not exist or if it cannot be accessed by the calling process. +.Sh SEE ALSO +.Xr semget 2 , +.Xr shmget 2 , +.Xr msgget 3 +.Sh HISTORY +The +.Fn ftok +function originates with System V and is typically used by programs +that use the System V IPC routines. +.Sh AUTHORS +.An Thorsten Lockert Aq tholo@sigmasoft.com +.Sh BUGS +The returned key is computed based on the device minor number and inode of the +specified +.Fa path +in combination with the lower 8 bits of the given +.Fa id . +Thus it is quite possible for the routine to return duplicate keys. diff --git a/lib/libc/gen/ftok.c b/lib/libc/gen/ftok.c new file mode 100644 index 0000000..24cfe7d --- /dev/null +++ b/lib/libc/gen/ftok.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com> + * All rights reserved. + * + * 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 ``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 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/ipc.h> + +key_t +ftok(path, id) + const char *path; + char id; +{ + struct stat st; + + if (stat(path, &st) < 0) + return (key_t)-1; + + return (key_t) (id << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 0xffff)); +} diff --git a/lib/libc/gen/fts-compat.c b/lib/libc/gen/fts-compat.c new file mode 100644 index 0000000..0ad03ca --- /dev/null +++ b/lib/libc/gen/fts-compat.c @@ -0,0 +1,1112 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $OpenBSD: fts.c,v 1.22 1999/10/03 19:22:22 millert Exp $ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; +#else +static char rcsid[] = "$FreeBSD$"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <fts.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static FTSENT *fts_alloc __P((FTS *, char *, int)); +static FTSENT *fts_build __P((FTS *, int)); +static void fts_lfree __P((FTSENT *)); +static void fts_load __P((FTS *, FTSENT *)); +static size_t fts_maxarglen __P((char * const *)); +static void fts_padjust __P((FTS *, FTSENT *)); +static int fts_palloc __P((FTS *, size_t)); +static FTSENT *fts_sort __P((FTS *, FTSENT *, int)); +static u_short fts_stat __P((FTS *, FTSENT *, int)); +static int fts_safe_changedir __P((FTS *, FTSENT *, int)); + +#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) + +#define CLR(opt) (sp->fts_options &= ~(opt)) +#define ISSET(opt) (sp->fts_options & (opt)) +#define SET(opt) (sp->fts_options |= (opt)) + +#define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) +#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) + +/* fts_build flags */ +#define BCHILD 1 /* fts_children */ +#define BNAMES 2 /* fts_children, names only */ +#define BREAD 3 /* fts_read */ + +FTS * +fts_open(argv, options, compar) + char * const *argv; + register int options; + int (*compar) __P((const FTSENT **, const FTSENT **)); +{ + register FTS *sp; + register FTSENT *p, *root; + register int nitems; + FTSENT *parent, *tmp; + int len; + + /* Options check. */ + if (options & ~FTS_OPTIONMASK) { + errno = EINVAL; + return (NULL); + } + + /* Allocate/initialize the stream */ + if ((sp = malloc((u_int)sizeof(FTS))) == NULL) + return (NULL); + memset(sp, 0, sizeof(FTS)); + sp->fts_compar = compar; + sp->fts_options = options; + + /* Shush, GCC. */ + tmp = NULL; + + /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ + if (ISSET(FTS_LOGICAL)) + SET(FTS_NOCHDIR); + + /* + * Start out with 1K of path space, and enough, in any case, + * to hold the user's paths. + */ + if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) + goto mem1; + + /* Allocate/initialize root's parent. */ + if ((parent = fts_alloc(sp, "", 0)) == NULL) + goto mem2; + parent->fts_level = FTS_ROOTPARENTLEVEL; + + /* Allocate/initialize root(s). */ + for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) { + /* Don't allow zero-length paths. */ + if ((len = strlen(*argv)) == 0) { + errno = ENOENT; + goto mem3; + } + + p = fts_alloc(sp, *argv, len); + p->fts_level = FTS_ROOTLEVEL; + p->fts_parent = parent; + p->fts_accpath = p->fts_name; + p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); + + /* Command-line "." and ".." are real directories. */ + if (p->fts_info == FTS_DOT) + p->fts_info = FTS_D; + + /* + * If comparison routine supplied, traverse in sorted + * order; otherwise traverse in the order specified. + */ + if (compar) { + p->fts_link = root; + root = p; + } else { + p->fts_link = NULL; + if (root == NULL) + tmp = root = p; + else { + tmp->fts_link = p; + tmp = p; + } + } + } + if (compar && nitems > 1) + root = fts_sort(sp, root, nitems); + + /* + * Allocate a dummy pointer and make fts_read think that we've just + * finished the node before the root(s); set p->fts_info to FTS_INIT + * so that everything about the "current" node is ignored. + */ + if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) + goto mem3; + sp->fts_cur->fts_link = root; + sp->fts_cur->fts_info = FTS_INIT; + + /* + * If using chdir(2), grab a file descriptor pointing to dot to ensure + * that we can get back here; this could be avoided for some paths, + * but almost certainly not worth the effort. Slashes, symbolic links, + * and ".." are all fairly nasty problems. Note, if we can't get the + * descriptor we run anyway, just more slowly. + */ + if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0) + SET(FTS_NOCHDIR); + + return (sp); + +mem3: fts_lfree(root); + free(parent); +mem2: free(sp->fts_path); +mem1: free(sp); + return (NULL); +} + +static void +fts_load(sp, p) + FTS *sp; + register FTSENT *p; +{ + register int len; + register char *cp; + + /* + * Load the stream structure for the next traversal. Since we don't + * actually enter the directory until after the preorder visit, set + * the fts_accpath field specially so the chdir gets done to the right + * place and the user can access the first node. From fts_open it's + * known that the path will fit. + */ + len = p->fts_pathlen = p->fts_namelen; + memmove(sp->fts_path, p->fts_name, len + 1); + if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { + len = strlen(++cp); + memmove(p->fts_name, cp, len + 1); + p->fts_namelen = len; + } + p->fts_accpath = p->fts_path = sp->fts_path; + sp->fts_dev = p->fts_dev; +} + +int +fts_close(sp) + FTS *sp; +{ + register FTSENT *freep, *p; + int saved_errno; + + /* + * This still works if we haven't read anything -- the dummy structure + * points to the root list, so we step through to the end of the root + * list which has a valid parent pointer. + */ + if (sp->fts_cur) { + for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { + freep = p; + p = p->fts_link ? p->fts_link : p->fts_parent; + free(freep); + } + free(p); + } + + /* Free up child linked list, sort array, path buffer. */ + if (sp->fts_child) + fts_lfree(sp->fts_child); + if (sp->fts_array) + free(sp->fts_array); + free(sp->fts_path); + + /* Return to original directory, save errno if necessary. */ + if (!ISSET(FTS_NOCHDIR)) { + saved_errno = fchdir(sp->fts_rfd) ? errno : 0; + (void)close(sp->fts_rfd); + + /* Set errno and return. */ + if (saved_errno != 0) { + /* Free up the stream pointer. */ + free(sp); + errno = saved_errno; + return (-1); + } + } + + /* Free up the stream pointer. */ + free(sp); + return (0); +} + +/* + * Special case of "/" at the end of the path so that slashes aren't + * appended which would cause paths to be written as "....//foo". + */ +#define NAPPEND(p) \ + (p->fts_path[p->fts_pathlen - 1] == '/' \ + ? p->fts_pathlen - 1 : p->fts_pathlen) + +FTSENT * +fts_read(sp) + register FTS *sp; +{ + register FTSENT *p, *tmp; + register int instr; + register char *t; + int saved_errno; + + /* If finished or unrecoverable error, return NULL. */ + if (sp->fts_cur == NULL || ISSET(FTS_STOP)) + return (NULL); + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* Save and zero out user instructions. */ + instr = p->fts_instr; + p->fts_instr = FTS_NOINSTR; + + /* Any type of file may be re-visited; re-stat and re-turn. */ + if (instr == FTS_AGAIN) { + p->fts_info = fts_stat(sp, p, 0); + return (p); + } + + /* + * Following a symlink -- SLNONE test allows application to see + * SLNONE and recover. If indirecting through a symlink, have + * keep a pointer to current location. If unable to get that + * pointer, follow fails. + */ + if (instr == FTS_FOLLOW && + (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + return (p); + } + + /* Directory in pre-order. */ + if (p->fts_info == FTS_D) { + /* If skipped or crossed mount point, do post-order visit. */ + if (instr == FTS_SKIP || + (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { + if (p->fts_flags & FTS_SYMFOLLOW) + (void)close(p->fts_symfd); + if (sp->fts_child) { + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + p->fts_info = FTS_DP; + return (p); + } + + /* Rebuild if only read the names and now traversing. */ + if (sp->fts_child && ISSET(FTS_NAMEONLY)) { + CLR(FTS_NAMEONLY); + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + + /* + * Cd to the subdirectory. + * + * If have already read and now fail to chdir, whack the list + * to make the names come out right, and set the parent errno + * so the application will eventually get an error condition. + * Set the FTS_DONTCHDIR flag so that when we logically change + * directories back to the parent we don't do a chdir. + * + * If haven't read do so. If the read fails, fts_build sets + * FTS_STOP or the fts_info field of the node. + */ + if (sp->fts_child) { + if (fts_safe_changedir(sp, p, -1)) { + p->fts_errno = errno; + p->fts_flags |= FTS_DONTCHDIR; + for (p = sp->fts_child; p; p = p->fts_link) + p->fts_accpath = + p->fts_parent->fts_accpath; + } + } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { + if (ISSET(FTS_STOP)) + return (NULL); + return (p); + } + p = sp->fts_child; + sp->fts_child = NULL; + goto name; + } + + /* Move to the next node on this level. */ +next: tmp = p; + if ((p = p->fts_link)) { + free(tmp); + + /* + * If reached the top, return to the original directory (or + * the root of the tree), and load the paths for the next root. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + fts_load(sp, p); + return (sp->fts_cur = p); + } + + /* + * User may have called fts_set on the node. If skipped, + * ignore. If followed, get a file descriptor so we can + * get back if necessary. + */ + if (p->fts_instr == FTS_SKIP) + goto next; + if (p->fts_instr == FTS_FOLLOW) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = + open(".", O_RDONLY, 0)) < 0) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + p->fts_instr = FTS_NOINSTR; + } + +name: t = sp->fts_path + NAPPEND(p->fts_parent); + *t++ = '/'; + memmove(t, p->fts_name, p->fts_namelen + 1); + return (sp->fts_cur = p); + } + + /* Move up to the parent node. */ + p = tmp->fts_parent; + free(tmp); + + if (p->fts_level == FTS_ROOTPARENTLEVEL) { + /* + * Done; free everything up and set errno to 0 so the user + * can distinguish between error and EOF. + */ + free(p); + errno = 0; + return (sp->fts_cur = NULL); + } + + /* NUL terminate the pathname. */ + sp->fts_path[p->fts_pathlen] = '\0'; + + /* + * Return to the parent directory. If at a root node or came through + * a symlink, go back through the file descriptor. Otherwise, cd up + * one directory. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + } else if (p->fts_flags & FTS_SYMFOLLOW) { + if (FCHDIR(sp, p->fts_symfd)) { + saved_errno = errno; + (void)close(p->fts_symfd); + errno = saved_errno; + SET(FTS_STOP); + return (NULL); + } + (void)close(p->fts_symfd); + } else if (!(p->fts_flags & FTS_DONTCHDIR)) { + if (CHDIR(sp, "..")) { + SET(FTS_STOP); + return (NULL); + } + } + p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; + return (sp->fts_cur = p); +} + +/* + * Fts_set takes the stream as an argument although it's not used in this + * implementation; it would be necessary if anyone wanted to add global + * semantics to fts using fts_set. An error return is allowed for similar + * reasons. + */ +/* ARGSUSED */ +int +fts_set(sp, p, instr) + FTS *sp; + FTSENT *p; + int instr; +{ + if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && + instr != FTS_NOINSTR && instr != FTS_SKIP) { + errno = EINVAL; + return (1); + } + p->fts_instr = instr; + return (0); +} + +FTSENT * +fts_children(sp, instr) + register FTS *sp; + int instr; +{ + register FTSENT *p; + int fd; + + if (instr && instr != FTS_NAMEONLY) { + errno = EINVAL; + return (NULL); + } + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* + * Errno set to 0 so user can distinguish empty directory from + * an error. + */ + errno = 0; + + /* Fatal errors stop here. */ + if (ISSET(FTS_STOP)) + return (NULL); + + /* Return logical hierarchy of user's arguments. */ + if (p->fts_info == FTS_INIT) + return (p->fts_link); + + /* + * If not a directory being visited in pre-order, stop here. Could + * allow FTS_DNR, assuming the user has fixed the problem, but the + * same effect is available with FTS_AGAIN. + */ + if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) + return (NULL); + + /* Free up any previous child list. */ + if (sp->fts_child) + fts_lfree(sp->fts_child); + + if (instr == FTS_NAMEONLY) { + SET(FTS_NAMEONLY); + instr = BNAMES; + } else + instr = BCHILD; + + /* + * If using chdir on a relative path and called BEFORE fts_read does + * its chdir to the root of a traversal, we can lose -- we need to + * chdir into the subdirectory, and we don't know where the current + * directory is, so we can't get back so that the upcoming chdir by + * fts_read will work. + */ + if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || + ISSET(FTS_NOCHDIR)) + return (sp->fts_child = fts_build(sp, instr)); + + if ((fd = open(".", O_RDONLY, 0)) < 0) + return (NULL); + sp->fts_child = fts_build(sp, instr); + if (fchdir(fd)) + return (NULL); + (void)close(fd); + return (sp->fts_child); +} + +/* + * This is the tricky part -- do not casually change *anything* in here. The + * idea is to build the linked list of entries that are used by fts_children + * and fts_read. There are lots of special cases. + * + * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is + * set and it's a physical walk (so that symbolic links can't be directories), + * we can do things quickly. First, if it's a 4.4BSD file system, the type + * of the file is in the directory entry. Otherwise, we assume that the number + * of subdirectories in a node is equal to the number of links to the parent. + * The former skips all stat calls. The latter skips stat calls in any leaf + * directories and for any files after the subdirectories in the directory have + * been found, cutting the stat calls by about 2/3. + */ +static FTSENT * +fts_build(sp, type) + register FTS *sp; + int type; +{ + register struct dirent *dp; + register FTSENT *p, *head; + register int nitems; + FTSENT *cur, *tail; + DIR *dirp; + void *oldaddr; + int cderrno, descend, len, level, maxlen, nlinks, oflag, saved_errno, + nostat, doadjust; + char *cp; + + /* Set current node pointer. */ + cur = sp->fts_cur; + + /* + * Open the directory for reading. If this fails, we're done. + * If being called from fts_read, set the fts_info field. + */ +#ifdef FTS_WHITEOUT + if (ISSET(FTS_WHITEOUT)) + oflag = DTF_NODUP|DTF_REWIND; + else + oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND; +#else +#define __opendir2(path, flag) opendir(path) +#endif + if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { + if (type == BREAD) { + cur->fts_info = FTS_DNR; + cur->fts_errno = errno; + } + return (NULL); + } + + /* + * Nlinks is the number of possible entries of type directory in the + * directory if we're cheating on stat calls, 0 if we're not doing + * any stat calls at all, -1 if we're doing stats on everything. + */ + if (type == BNAMES) { + nlinks = 0; + /* Be quiet about nostat, GCC. */ + nostat = 0; + } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { + nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); + nostat = 1; + } else { + nlinks = -1; + nostat = 0; + } + +#ifdef notdef + (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); + (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", + ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); +#endif + /* + * If we're going to need to stat anything or we want to descend + * and stay in the directory, chdir. If this fails we keep going, + * but set a flag so we don't chdir after the post-order visit. + * We won't be able to stat anything, but we can still return the + * names themselves. Note, that since fts_read won't be able to + * chdir into the directory, it will have to return different path + * names than before, i.e. "a/b" instead of "b". Since the node + * has already been visited in pre-order, have to wait until the + * post-order visit to return the error. There is a special case + * here, if there was nothing to stat then it's not an error to + * not be able to stat. This is all fairly nasty. If a program + * needed sorted entries or stat information, they had better be + * checking FTS_NS on the returned nodes. + */ + cderrno = 0; + if (nlinks || type == BREAD) { + if (fts_safe_changedir(sp, cur, dirfd(dirp))) { + if (nlinks && type == BREAD) + cur->fts_errno = errno; + cur->fts_flags |= FTS_DONTCHDIR; + descend = 0; + cderrno = errno; + (void)closedir(dirp); + dirp = NULL; + } else + descend = 1; + } else + descend = 0; + + /* + * Figure out the max file name length that can be stored in the + * current path -- the inner loop allocates more path as necessary. + * We really wouldn't have to do the maxlen calculations here, we + * could do them in fts_read before returning the path, but it's a + * lot easier here since the length is part of the dirent structure. + * + * If not changing directories set a pointer so that can just append + * each new name into the path. + */ + len = NAPPEND(cur); + if (ISSET(FTS_NOCHDIR)) { + cp = sp->fts_path + len; + *cp++ = '/'; + } else { + /* GCC, you're too verbose. */ + cp = NULL; + } + len++; + maxlen = sp->fts_pathlen - len; + + level = cur->fts_level + 1; + + /* Read the directory, attaching each entry to the `link' pointer. */ + doadjust = 0; + for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) { + if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) + continue; + + if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL) + goto mem1; + if (dp->d_namlen >= maxlen) { /* include space for NUL */ + oldaddr = sp->fts_path; + if (fts_palloc(sp, dp->d_namlen +len + 1)) { + /* + * No more memory for path or structures. Save + * errno, free up the current structure and the + * structures already allocated. + */ +mem1: saved_errno = errno; + if (p) + free(p); + fts_lfree(head); + (void)closedir(dirp); + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = saved_errno; + return (NULL); + } + /* Did realloc() change the pointer? */ + if (oldaddr != sp->fts_path) { + doadjust = 1; + if (ISSET(FTS_NOCHDIR)) + cp = sp->fts_path + len; + } + maxlen = sp->fts_pathlen - len; + } + + if (len + dp->d_namlen >= USHRT_MAX) { + /* + * In an FTSENT, fts_pathlen is a u_short so it is + * possible to wraparound here. If we do, free up + * the current structure and the structures already + * allocated, then error out with ENAMETOOLONG. + */ + free(p); + fts_lfree(head); + (void)closedir(dirp); + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = ENAMETOOLONG; + return (NULL); + } + p->fts_level = level; + p->fts_parent = sp->fts_cur; + p->fts_pathlen = len + dp->d_namlen; + +#ifdef FTS_WHITEOUT + if (dp->d_type == DT_WHT) + p->fts_flags |= FTS_ISW; +#endif + + if (cderrno) { + if (nlinks) { + p->fts_info = FTS_NS; + p->fts_errno = cderrno; + } else + p->fts_info = FTS_NSOK; + p->fts_accpath = cur->fts_accpath; + } else if (nlinks == 0 +#ifdef DT_DIR + || (nostat && + dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) +#endif + ) { + p->fts_accpath = + ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; + p->fts_info = FTS_NSOK; + } else { + /* Build a file name for fts_stat to stat. */ + if (ISSET(FTS_NOCHDIR)) { + p->fts_accpath = p->fts_path; + memmove(cp, p->fts_name, p->fts_namelen + 1); + } else + p->fts_accpath = p->fts_name; + /* Stat it. */ + p->fts_info = fts_stat(sp, p, 0); + + /* Decrement link count if applicable. */ + if (nlinks > 0 && (p->fts_info == FTS_D || + p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) + --nlinks; + } + + /* We walk in directory order so "ls -f" doesn't get upset. */ + p->fts_link = NULL; + if (head == NULL) + head = tail = p; + else { + tail->fts_link = p; + tail = p; + } + ++nitems; + } + if (dirp) + (void)closedir(dirp); + + /* + * If realloc() changed the address of the path, adjust the + * addresses for the rest of the tree and the dir list. + */ + if (doadjust) + fts_padjust(sp, head); + + /* + * If not changing directories, reset the path back to original + * state. + */ + if (ISSET(FTS_NOCHDIR)) { + if (len == sp->fts_pathlen || nitems == 0) + --cp; + *cp = '\0'; + } + + /* + * If descended after called from fts_children or after called from + * fts_read and nothing found, get back. At the root level we use + * the saved fd; if one of fts_open()'s arguments is a relative path + * to an empty directory, we wind up here with no other way back. If + * can't get back, we're done. + */ + if (descend && (type == BCHILD || !nitems) && + (cur->fts_level == FTS_ROOTLEVEL ? + FCHDIR(sp, sp->fts_rfd) : CHDIR(sp, ".."))) { + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + return (NULL); + } + + /* If didn't find anything, return NULL. */ + if (!nitems) { + if (type == BREAD) + cur->fts_info = FTS_DP; + return (NULL); + } + + /* Sort the entries. */ + if (sp->fts_compar && nitems > 1) + head = fts_sort(sp, head, nitems); + return (head); +} + +static u_short +fts_stat(sp, p, follow) + FTS *sp; + register FTSENT *p; + int follow; +{ + register FTSENT *t; + register dev_t dev; + register ino_t ino; + struct stat *sbp, sb; + int saved_errno; + + /* If user needs stat info, stat buffer already allocated. */ + sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; + +#ifdef FTS_WHITEOUT + /* check for whiteout */ + if (p->fts_flags & FTS_ISW) { + if (sbp != &sb) { + memset(sbp, '\0', sizeof (*sbp)); + sbp->st_mode = S_IFWHT; + } + return (FTS_W); + } +#endif + + /* + * If doing a logical walk, or application requested FTS_FOLLOW, do + * a stat(2). If that fails, check for a non-existent symlink. If + * fail, set the errno from the stat call. + */ + if (ISSET(FTS_LOGICAL) || follow) { + if (stat(p->fts_accpath, sbp)) { + saved_errno = errno; + if (!lstat(p->fts_accpath, sbp)) { + errno = 0; + return (FTS_SLNONE); + } + p->fts_errno = saved_errno; + goto err; + } + } else if (lstat(p->fts_accpath, sbp)) { + p->fts_errno = errno; +err: memset(sbp, 0, sizeof(struct stat)); + return (FTS_NS); + } + + if (S_ISDIR(sbp->st_mode)) { + /* + * Set the device/inode. Used to find cycles and check for + * crossing mount points. Also remember the link count, used + * in fts_build to limit the number of stat calls. It is + * understood that these fields are only referenced if fts_info + * is set to FTS_D. + */ + dev = p->fts_dev = sbp->st_dev; + ino = p->fts_ino = sbp->st_ino; + p->fts_nlink = sbp->st_nlink; + + if (ISDOT(p->fts_name)) + return (FTS_DOT); + + /* + * Cycle detection is done by brute force when the directory + * is first encountered. If the tree gets deep enough or the + * number of symbolic links to directories is high enough, + * something faster might be worthwhile. + */ + for (t = p->fts_parent; + t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) + if (ino == t->fts_ino && dev == t->fts_dev) { + p->fts_cycle = t; + return (FTS_DC); + } + return (FTS_D); + } + if (S_ISLNK(sbp->st_mode)) + return (FTS_SL); + if (S_ISREG(sbp->st_mode)) + return (FTS_F); + return (FTS_DEFAULT); +} + +static FTSENT * +fts_sort(sp, head, nitems) + FTS *sp; + FTSENT *head; + register int nitems; +{ + register FTSENT **ap, *p; + + /* + * Construct an array of pointers to the structures and call qsort(3). + * Reassemble the array in the order returned by qsort. If unable to + * sort for memory reasons, return the directory entries in their + * current order. Allocate enough space for the current needs plus + * 40 so don't realloc one entry at a time. + */ + if (nitems > sp->fts_nitems) { + struct _ftsent **a; + + sp->fts_nitems = nitems + 40; + if ((a = realloc(sp->fts_array, + sp->fts_nitems * sizeof(FTSENT *))) == NULL) { + if (sp->fts_array) + free(sp->fts_array); + sp->fts_array = NULL; + sp->fts_nitems = 0; + return (head); + } + sp->fts_array = a; + } + for (ap = sp->fts_array, p = head; p; p = p->fts_link) + *ap++ = p; + qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar); + for (head = *(ap = sp->fts_array); --nitems; ++ap) + ap[0]->fts_link = ap[1]; + ap[0]->fts_link = NULL; + return (head); +} + +static FTSENT * +fts_alloc(sp, name, namelen) + FTS *sp; + char *name; + register int namelen; +{ + register FTSENT *p; + size_t len; + + /* + * The file name is a variable length array and no stat structure is + * necessary if the user has set the nostat bit. Allocate the FTSENT + * structure, the file name and the stat structure in one chunk, but + * be careful that the stat structure is reasonably aligned. Since the + * fts_name field is declared to be of size 1, the fts_name pointer is + * namelen + 2 before the first possible address of the stat structure. + */ + len = sizeof(FTSENT) + namelen; + if (!ISSET(FTS_NOSTAT)) + len += sizeof(struct stat) + ALIGNBYTES; + if ((p = malloc(len)) == NULL) + return (NULL); + + /* Copy the name and guarantee NUL termination. */ + memmove(p->fts_name, name, namelen); + p->fts_name[namelen] = '\0'; + + if (!ISSET(FTS_NOSTAT)) + p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2); + p->fts_namelen = namelen; + p->fts_path = sp->fts_path; + p->fts_errno = 0; + p->fts_flags = 0; + p->fts_instr = FTS_NOINSTR; + p->fts_number = 0; + p->fts_pointer = NULL; + return (p); +} + +static void +fts_lfree(head) + register FTSENT *head; +{ + register FTSENT *p; + + /* Free a linked list of structures. */ + while ((p = head)) { + head = head->fts_link; + free(p); + } +} + +/* + * Allow essentially unlimited paths; find, rm, ls should all work on any tree. + * Most systems will allow creation of paths much longer than MAXPATHLEN, even + * though the kernel won't resolve them. Add the size (not just what's needed) + * plus 256 bytes so don't realloc the path 2 bytes at a time. + */ +static int +fts_palloc(sp, more) + FTS *sp; + size_t more; +{ + char *p; + + sp->fts_pathlen += more + 256; + /* + * Check for possible wraparound. In an FTS, fts_pathlen is + * a signed int but in an FTSENT it is an unsigned short. + * We limit fts_pathlen to USHRT_MAX to be safe in both cases. + */ + if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) { + if (sp->fts_path) + free(sp->fts_path); + sp->fts_path = NULL; + errno = ENAMETOOLONG; + return (1); + } + p = realloc(sp->fts_path, sp->fts_pathlen); + if (p == NULL) { + if (sp->fts_path) + free(sp->fts_path); + sp->fts_path = NULL; + return (1); + } + sp->fts_path = p; + return (0); +} + +/* + * When the path is realloc'd, have to fix all of the pointers in structures + * already returned. + */ +static void +fts_padjust(sp, head) + FTS *sp; + FTSENT *head; +{ + FTSENT *p; + char *addr = sp->fts_path; + +#define ADJUST(p) { \ + if ((p)->fts_accpath != (p)->fts_name) { \ + (p)->fts_accpath = \ + (char *)addr + ((p)->fts_accpath - (p)->fts_path); \ + } \ + (p)->fts_path = addr; \ +} + /* Adjust the current set of children. */ + for (p = sp->fts_child; p; p = p->fts_link) + ADJUST(p); + + /* Adjust the rest of the tree, including the current level. */ + for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { + ADJUST(p); + p = p->fts_link ? p->fts_link : p->fts_parent; + } +} + +static size_t +fts_maxarglen(argv) + char * const *argv; +{ + size_t len, max; + + for (max = 0; *argv; ++argv) + if ((len = strlen(*argv)) > max) + max = len; + return (max + 1); +} + +/* + * Change to dir specified by fd or p->fts_accpath without getting + * tricked by someone changing the world out from underneath us. + * Assumes p->fts_dev and p->fts_ino are filled in. + */ +static int +fts_safe_changedir(sp, p, fd) + FTS *sp; + FTSENT *p; + int fd; +{ + int ret, oerrno, newfd; + struct stat sb; + + newfd = fd; + if (ISSET(FTS_NOCHDIR)) + return (0); + if (fd < 0 && (newfd = open(p->fts_accpath, O_RDONLY, 0)) < 0) + return (-1); + if (fstat(newfd, &sb)) { + ret = -1; + goto bail; + } + if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) { + errno = ENOENT; /* disinformation */ + ret = -1; + goto bail; + } + ret = fchdir(newfd); +bail: + oerrno = errno; + if (fd < 0) + (void)close(newfd); + errno = oerrno; + return (ret); +} diff --git a/lib/libc/gen/fts-compat.h b/lib/libc/gen/fts-compat.h new file mode 100644 index 0000000..4fa4a3a --- /dev/null +++ b/lib/libc/gen/fts-compat.h @@ -0,0 +1,128 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#)fts.h 8.3 (Berkeley) 8/14/94 + */ + +#ifndef _FTS_H_ +#define _FTS_H_ + +typedef struct { + struct _ftsent *fts_cur; /* current node */ + struct _ftsent *fts_child; /* linked list of children */ + struct _ftsent **fts_array; /* sort array */ + dev_t fts_dev; /* starting device # */ + char *fts_path; /* path for this descent */ + int fts_rfd; /* fd for root */ + int fts_pathlen; /* sizeof(path) */ + int fts_nitems; /* elements in the sort array */ + int (*fts_compar)(); /* compare function */ + +#define FTS_COMFOLLOW 0x001 /* follow command line symlinks */ +#define FTS_LOGICAL 0x002 /* logical walk */ +#define FTS_NOCHDIR 0x004 /* don't change directories */ +#define FTS_NOSTAT 0x008 /* don't get stat info */ +#define FTS_PHYSICAL 0x010 /* physical walk */ +#define FTS_SEEDOT 0x020 /* return dot and dot-dot */ +#define FTS_XDEV 0x040 /* don't cross devices */ +#define FTS_WHITEOUT 0x080 /* return whiteout information */ +#define FTS_OPTIONMASK 0x0ff /* valid user option mask */ + +#define FTS_NAMEONLY 0x100 /* (private) child names only */ +#define FTS_STOP 0x200 /* (private) unrecoverable error */ + int fts_options; /* fts_open options, global flags */ +} FTS; + +typedef struct _ftsent { + struct _ftsent *fts_cycle; /* cycle node */ + struct _ftsent *fts_parent; /* parent directory */ + struct _ftsent *fts_link; /* next file in directory */ + long fts_number; /* local numeric value */ + void *fts_pointer; /* local address value */ + char *fts_accpath; /* access path */ + char *fts_path; /* root path */ + int fts_errno; /* errno for this node */ + int fts_symfd; /* fd for symlink */ + u_short fts_pathlen; /* strlen(fts_path) */ + u_short fts_namelen; /* strlen(fts_name) */ + + ino_t fts_ino; /* inode */ + dev_t fts_dev; /* device */ + nlink_t fts_nlink; /* link count */ + +#define FTS_ROOTPARENTLEVEL -1 +#define FTS_ROOTLEVEL 0 + short fts_level; /* depth (-1 to N) */ + +#define FTS_D 1 /* preorder directory */ +#define FTS_DC 2 /* directory that causes cycles */ +#define FTS_DEFAULT 3 /* none of the above */ +#define FTS_DNR 4 /* unreadable directory */ +#define FTS_DOT 5 /* dot or dot-dot */ +#define FTS_DP 6 /* postorder directory */ +#define FTS_ERR 7 /* error; errno is set */ +#define FTS_F 8 /* regular file */ +#define FTS_INIT 9 /* initialized only */ +#define FTS_NS 10 /* stat(2) failed */ +#define FTS_NSOK 11 /* no stat(2) requested */ +#define FTS_SL 12 /* symbolic link */ +#define FTS_SLNONE 13 /* symbolic link without target */ +#define FTS_W 14 /* whiteout object */ + u_short fts_info; /* user flags for FTSENT structure */ + +#define FTS_DONTCHDIR 0x01 /* don't chdir .. to the parent */ +#define FTS_SYMFOLLOW 0x02 /* followed a symlink to get here */ +#define FTS_ISW 0x04 /* this is a whiteout object */ + u_short fts_flags; /* private flags for FTSENT structure */ + +#define FTS_AGAIN 1 /* read node again */ +#define FTS_FOLLOW 2 /* follow symbolic link */ +#define FTS_NOINSTR 3 /* no instructions */ +#define FTS_SKIP 4 /* discard node */ + u_short fts_instr; /* fts_set() instructions */ + + struct stat *fts_statp; /* stat(2) information */ + char fts_name[1]; /* file name */ +} FTSENT; + +#include <sys/cdefs.h> + +__BEGIN_DECLS +FTSENT *fts_children __P((FTS *, int)); +int fts_close __P((FTS *)); +FTS *fts_open __P((char * const *, int, + int (*)(const FTSENT **, const FTSENT **))); +FTSENT *fts_read __P((FTS *)); +int fts_set __P((FTS *, FTSENT *, int)); +__END_DECLS + +#endif /* !_FTS_H_ */ diff --git a/lib/libc/gen/fts.3 b/lib/libc/gen/fts.3 new file mode 100644 index 0000000..ba21c89 --- /dev/null +++ b/lib/libc/gen/fts.3 @@ -0,0 +1,755 @@ +.\" Copyright (c) 1989, 1991, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)fts.3 8.5 (Berkeley) 4/16/94 +.\" $FreeBSD$ +.\" +.Dd April 16, 1994 +.Dt FTS 3 +.Os +.Sh NAME +.Nm fts +.Nd traverse a file hierarchy +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <sys/stat.h> +.Fd #include <fts.h> +.Ft FTS * +.Fn fts_open "char * const *path_argv" "int options" "int (*compar)(const FTSENT **, const FTSENT **)" +.Ft FTSENT * +.Fn fts_read "FTS *ftsp" +.Ft FTSENT * +.Fn fts_children "FTS *ftsp" "int options" +.Ft int +.Fn fts_set "FTS *ftsp" "FTSENT *f" "int options" +.Ft int +.Fn fts_close "FTS *ftsp" +.Sh DESCRIPTION +The +.Nm fts +functions are provided for traversing +.Tn UNIX +file hierarchies. +A simple overview is that the +.Fn fts_open +function returns a +.Dq handle +on a file hierarchy, which is then supplied to +the other +.Nm fts +functions. +The function +.Fn fts_read +returns a pointer to a structure describing one of the files in the file +hierarchy. +The function +.Fn fts_children +returns a pointer to a linked list of structures, each of which describes +one of the files contained in a directory in the hierarchy. +In general, directories are visited two distinguishable times; in pre-order +(before any of their descendants are visited) and in post-order (after all +of their descendants have been visited). +Files are visited once. +It is possible to walk the hierarchy +.Dq logically +(ignoring symbolic links) +or physically (visiting symbolic links), order the walk of the hierarchy or +prune and/or re-visit portions of the hierarchy. +.Pp +Two structures are defined (and typedef'd) in the include file +.Aq Pa fts.h . +The first is +.Fa FTS , +the structure that represents the file hierarchy itself. +The second is +.Fa FTSENT , +the structure that represents a file in the file +hierarchy. +Normally, an +.Fa FTSENT +structure is returned for every file in the file +hierarchy. +In this manual page, +.Dq file +and +.Dq Fa FTSENT No structure +are generally +interchangeable. +The +.Fa FTSENT +structure contains at least the following fields, which are +described in greater detail below: +.Bd -literal +typedef struct _ftsent { + u_short fts_info; /* flags for FTSENT structure */ + char *fts_accpath; /* access path */ + char *fts_path; /* root path */ + u_short fts_pathlen; /* strlen(fts_path) */ + char *fts_name; /* file name */ + u_short fts_namelen; /* strlen(fts_name) */ + short fts_level; /* depth (\-1 to N) */ + int fts_errno; /* file errno */ + long fts_number; /* local numeric value */ + void *fts_pointer; /* local address value */ + struct ftsent *fts_parent; /* parent directory */ + struct ftsent *fts_link; /* next file structure */ + struct ftsent *fts_cycle; /* cycle structure */ + struct stat *fts_statp; /* stat(2) information */ +} FTSENT; +.Ed +.Pp +These fields are defined as follows: +.Bl -tag -width "fts_namelen" +.It Fa fts_info +One of the following values describing the returned +.Fa FTSENT +structure and +the file it represents. +With the exception of directories without errors +.Pq Dv FTS_D , +all of these +entries are terminal, that is, they will not be revisited, nor will any +of their descendants be visited. +.Bl -tag -width FTS_DEFAULT +.It Dv FTS_D +A directory being visited in pre-order. +.It Dv FTS_DC +A directory that causes a cycle in the tree. +(The +.Fa fts_cycle +field of the +.Fa FTSENT +structure will be filled in as well.) +.It Dv FTS_DEFAULT +Any +.Fa FTSENT +structure that represents a file type not explicitly described +by one of the other +.Fa fts_info +values. +.It Dv FTS_DNR +A directory which cannot be read. +This is an error return, and the +.Fa fts_errno +field will be set to indicate what caused the error. +.It Dv FTS_DOT +A file named +.Ql \&. +or +.Ql .. +which was not specified as a file name to +.Fn fts_open +(see +.Dv FTS_SEEDOT ) . +.It Dv FTS_DP +A directory being visited in post-order. +The contents of the +.Fa FTSENT +structure will be unchanged from when +it was returned in pre-order, i.e. with the +.Fa fts_info +field set to +.Dv FTS_D . +.It Dv FTS_ERR +This is an error return, and the +.Fa fts_errno +field will be set to indicate what caused the error. +.It Dv FTS_F +A regular file. +.It Dv FTS_NS +A file for which no +.Xr stat 2 +information was available. +The contents of the +.Fa fts_statp +field are undefined. +This is an error return, and the +.Fa fts_errno +field will be set to indicate what caused the error. +.It Dv FTS_NSOK +A file for which no +.Xr stat 2 +information was requested. +The contents of the +.Fa fts_statp +field are undefined. +.It Dv FTS_SL +A symbolic link. +.It Dv FTS_SLNONE +A symbolic link with a non-existent target. +The contents of the +.Fa fts_statp +field reference the file characteristic information for the symbolic link +itself. +.El +.It Fa fts_accpath +A path for accessing the file from the current directory. +.It Fa fts_path +The path for the file relative to the root of the traversal. +This path contains the path specified to +.Fn fts_open +as a prefix. +.It Fa fts_pathlen +The length of the string referenced by +.Fa fts_path . +.It Fa fts_name +The name of the file. +.It Fa fts_namelen +The length of the string referenced by +.Fa fts_name . +.It Fa fts_level +The depth of the traversal, numbered from \-1 to N, where this file +was found. +The +.Fa FTSENT +structure representing the parent of the starting point (or root) +of the traversal is numbered \-1, and the +.Fa FTSENT +structure for the root +itself is numbered 0. +.It Fa fts_errno +Upon return of a +.Fa FTSENT +structure from the +.Fn fts_children +or +.Fn fts_read +functions, with its +.Fa fts_info +field set to +.Dv FTS_DNR , +.Dv FTS_ERR +or +.Dv FTS_NS , +the +.Fa fts_errno +field contains the value of the external variable +.Va errno +specifying the cause of the error. +Otherwise, the contents of the +.Fa fts_errno +field are undefined. +.It Fa fts_number +This field is provided for the use of the application program and is +not modified by the +.Nm fts +functions. +It is initialized to 0. +.It Fa fts_pointer +This field is provided for the use of the application program and is +not modified by the +.Nm fts +functions. +It is initialized to +.Dv NULL . +.It Fa fts_parent +A pointer to the +.Fa FTSENT +structure referencing the file in the hierarchy +immediately above the current file, i.e. the directory of which this +file is a member. +A parent structure for the initial entry point is provided as well, +however, only the +.Fa fts_level , +.Fa fts_number +and +.Fa fts_pointer +fields are guaranteed to be initialized. +.It Fa fts_link +Upon return from the +.Fn fts_children +function, the +.Fa fts_link +field points to the next structure in the NULL-terminated linked list of +directory members. +Otherwise, the contents of the +.Fa fts_link +field are undefined. +.It Fa fts_cycle +If a directory causes a cycle in the hierarchy (see +.Dv FTS_DC ) , +either because +of a hard link between two directories, or a symbolic link pointing to a +directory, the +.Fa fts_cycle +field of the structure will point to the +.Fa FTSENT +structure in the hierarchy that references the same file as the current +.Fa FTSENT +structure. +Otherwise, the contents of the +.Fa fts_cycle +field are undefined. +.It Fa fts_statp +A pointer to +.Xr stat 2 +information for the file. +.El +.Pp +A single buffer is used for all of the paths of all of the files in the +file hierarchy. +Therefore, the +.Fa fts_path +and +.Fa fts_accpath +fields are guaranteed to be +.Dv NUL Ns -terminated +.Em only +for the file most recently returned by +.Fn fts_read . +To use these fields to reference any files represented by other +.Fa FTSENT +structures will require that the path buffer be modified using the +information contained in that +.Fa FTSENT +structure's +.Fa fts_pathlen +field. +Any such modifications should be undone before further calls to +.Fn fts_read +are attempted. +The +.Fa fts_name +field is always +.Dv NUL Ns -terminated. +.Sh FTS_OPEN +The +.Fn fts_open +function takes a pointer to an array of character pointers naming one +or more paths which make up a logical file hierarchy to be traversed. +The array must be terminated by a +.Dv NULL +pointer. +.Pp +There are +a number of options, at least one of which (either +.Dv FTS_LOGICAL +or +.Dv FTS_PHYSICAL ) +must be specified. +The options are selected by +.Em or Ns 'ing +the following values: +.Bl -tag -width "FTS_PHYSICAL" +.It Dv FTS_COMFOLLOW +This option causes any symbolic link specified as a root path to be +followed immediately whether or not +.Dv FTS_LOGICAL +is also specified. +.It Dv FTS_LOGICAL +This option causes the +.Nm fts +routines to return +.Fa FTSENT +structures for the targets of symbolic links +instead of the symbolic links themselves. +If this option is set, the only symbolic links for which +.Fa FTSENT +structures +are returned to the application are those referencing non-existent files. +Either +.Dv FTS_LOGICAL +or +.Dv FTS_PHYSICAL +.Em must +be provided to the +.Fn fts_open +function. +.It Dv FTS_NOCHDIR +As a performance optimization, the +.Nm fts +functions change directories as they walk the file hierarchy. +This has the side-effect that an application cannot rely on being +in any particular directory during the traversal. +The +.Dv FTS_NOCHDIR +option turns off this optimization, and the +.Nm fts +functions will not change the current directory. +Note that applications should not themselves change their current directory +and try to access files unless +.Dv FTS_NOCHDIR +is specified and absolute +pathnames were provided as arguments to +.Fn fts_open . +.It Dv FTS_NOSTAT +By default, returned +.Fa FTSENT +structures reference file characteristic information (the +.Fa statp +field) for each file visited. +This option relaxes that requirement as a performance optimization, +allowing the +.Nm fts +functions to set the +.Fa fts_info +field to +.Dv FTS_NSOK +and leave the contents of the +.Fa statp +field undefined. +.It Dv FTS_PHYSICAL +This option causes the +.Nm fts +routines to return +.Fa FTSENT +structures for symbolic links themselves instead +of the target files they point to. +If this option is set, +.Fa FTSENT +structures for all symbolic links in the +hierarchy are returned to the application. +Either +.Dv FTS_LOGICAL +or +.Dv FTS_PHYSICAL +.Em must +be provided to the +.Fn fts_open +function. +.It Dv FTS_SEEDOT +By default, unless they are specified as path arguments to +.Fn fts_open , +any files named +.Ql \&. +or +.Ql .. +encountered in the file hierarchy are ignored. +This option causes the +.Nm fts +routines to return +.Fa FTSENT +structures for them. +.It Dv FTS_XDEV +This option prevents +.Nm fts +from descending into directories that have a different device number +than the file from which the descent began. +.El +.Pp +The argument +.Fn compar +specifies a user-defined function which may be used to order the traversal +of the hierarchy. +It +takes two pointers to pointers to +.Fa FTSENT +structures as arguments and +should return a negative value, zero, or a positive value to indicate +if the file referenced by its first argument comes before, in any order +with respect to, or after, the file referenced by its second argument. +The +.Fa fts_accpath , +.Fa fts_path +and +.Fa fts_pathlen +fields of the +.Fa FTSENT +structures may +.Em never +be used in this comparison. +If the +.Fa fts_info +field is set to +.Dv FTS_NS +or +.Dv FTS_NSOK , +the +.Fa fts_statp +field may not either. +If the +.Fn compar +argument is +.Dv NULL , +the directory traversal order is in the order listed in +.Fa path_argv +for the root paths, and in the order listed in the directory for +everything else. +.Sh FTS_READ +The +.Fn fts_read +function returns a pointer to an +.Fa FTSENT +structure describing a file in +the hierarchy. +Directories (that are readable and do not cause cycles) are visited at +least twice, once in pre-order and once in post-order. +All other files are visited at least once. +(Hard links between directories that do not cause cycles or symbolic +links to symbolic links may cause files to be visited more than once, +or directories more than twice.) +.Pp +If all the members of the hierarchy have been returned, +.Fn fts_read +returns +.Dv NULL +and sets the external variable +.Va errno +to 0. +If an error unrelated to a file in the hierarchy occurs, +.Fn fts_read +returns +.Dv NULL +and sets +.Va errno +appropriately. +If an error related to a returned file occurs, a pointer to an +.Fa FTSENT +structure is returned, and +.Va errno +may or may not have been set (see +.Fa fts_info ) . +.Pp +The +.Fa FTSENT +structures returned by +.Fn fts_read +may be overwritten after a call to +.Fn fts_close +on the same file hierarchy stream, or, after a call to +.Fn fts_read +on the same file hierarchy stream unless they represent a file of type +directory, in which case they will not be overwritten until after a call to +.Fn fts_read +after the +.Fa FTSENT +structure has been returned by the function +.Fn fts_read +in post-order. +.Sh FTS_CHILDREN +The +.Fn fts_children +function returns a pointer to an +.Fa FTSENT +structure describing the first entry in a NULL-terminated linked list of +the files in the directory represented by the +.Fa FTSENT +structure most recently returned by +.Fn fts_read . +The list is linked through the +.Fa fts_link +field of the +.Fa FTSENT +structure, and is ordered by the user-specified comparison function, if any. +Repeated calls to +.Fn fts_children +will recreate this linked list. +.Pp +As a special case, if +.Fn fts_read +has not yet been called for a hierarchy, +.Fn fts_children +will return a pointer to the files in the logical directory specified to +.Fn fts_open , +i.e. the arguments specified to +.Fn fts_open . +Otherwise, if the +.Fa FTSENT +structure most recently returned by +.Fn fts_read +is not a directory being visited in pre-order, +or the directory does not contain any files, +.Fn fts_children +returns +.Dv NULL +and sets +.Va errno +to zero. +If an error occurs, +.Fn fts_children +returns +.Dv NULL +and sets +.Va errno +appropriately. +.Pp +The +.Fa FTSENT +structures returned by +.Fn fts_children +may be overwritten after a call to +.Fn fts_children , +.Fn fts_close +or +.Fn fts_read +on the same file hierarchy stream. +.Pp +.Em Option +may be set to the following value: +.Bl -tag -width FTS_NAMEONLY +.It Dv FTS_NAMEONLY +Only the names of the files are needed. +The contents of all the fields in the returned linked list of structures +are undefined with the exception of the +.Fa fts_name +and +.Fa fts_namelen +fields. +.El +.Sh FTS_SET +The function +.Fn fts_set +allows the user application to determine further processing for the +file +.Fa f +of the stream +.Fa ftsp . +The +.Fn fts_set +function +returns 0 on success, and \-1 if an error occurs. +.Em Option +must be set to one of the following values: +.Bl -tag -width FTS_PHYSICAL +.It Dv FTS_AGAIN +Re-visit the file; any file type may be re-visited. +The next call to +.Fn fts_read +will return the referenced file. +The +.Fa fts_stat +and +.Fa fts_info +fields of the structure will be reinitialized at that time, +but no other fields will have been changed. +This option is meaningful only for the most recently returned +file from +.Fn fts_read . +Normal use is for post-order directory visits, where it causes the +directory to be re-visited (in both pre and post-order) as well as all +of its descendants. +.It Dv FTS_FOLLOW +The referenced file must be a symbolic link. +If the referenced file is the one most recently returned by +.Fn fts_read , +the next call to +.Fn fts_read +returns the file with the +.Fa fts_info +and +.Fa fts_statp +fields reinitialized to reflect the target of the symbolic link instead +of the symbolic link itself. +If the file is one of those most recently returned by +.Fn fts_children , +the +.Fa fts_info +and +.Fa fts_statp +fields of the structure, when returned by +.Fn fts_read , +will reflect the target of the symbolic link instead of the symbolic link +itself. +In either case, if the target of the symbolic link does not exist the +fields of the returned structure will be unchanged and the +.Fa fts_info +field will be set to +.Dv FTS_SLNONE . +.Pp +If the target of the link is a directory, the pre-order return, followed +by the return of all of its descendants, followed by a post-order return, +is done. +.It Dv FTS_SKIP +No descendants of this file are visited. +The file may be one of those most recently returned by either +.Fn fts_children +or +.Fn fts_read . +.El +.Sh FTS_CLOSE +The +.Fn fts_close +function closes a file hierarchy stream +.Fa ftsp +and restores the current directory to the directory from which +.Fn fts_open +was called to open +.Fa ftsp . +The +.Fn fts_close +function +returns 0 on success, and \-1 if an error occurs. +.Sh ERRORS +The function +.Fn fts_open +may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr open 2 +and +.Xr malloc 3 . +.Pp +The function +.Fn fts_close +may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr chdir 2 +and +.Xr close 2 . +.Pp +The functions +.Fn fts_read +and +.Fn fts_children +may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr chdir 2 , +.Xr malloc 3 , +.Xr opendir 3 , +.Xr readdir 3 +and +.Xr stat 2 . +.Pp +In addition, +.Fn fts_children , +.Fn fts_open +and +.Fn fts_set +may fail and set +.Va errno +as follows: +.Bl -tag -width Er +.It Bq Er EINVAL +The options were invalid. +.El +.Sh SEE ALSO +.Xr find 1 , +.Xr chdir 2 , +.Xr stat 2 , +.Xr qsort 3 +.Sh STANDARDS +The +.Nm fts +utility is expected to be included in a future +.St -p1003.1-88 +revision. diff --git a/lib/libc/gen/fts.c b/lib/libc/gen/fts.c new file mode 100644 index 0000000..0ad03ca --- /dev/null +++ b/lib/libc/gen/fts.c @@ -0,0 +1,1112 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $OpenBSD: fts.c,v 1.22 1999/10/03 19:22:22 millert Exp $ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; +#else +static char rcsid[] = "$FreeBSD$"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <fts.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static FTSENT *fts_alloc __P((FTS *, char *, int)); +static FTSENT *fts_build __P((FTS *, int)); +static void fts_lfree __P((FTSENT *)); +static void fts_load __P((FTS *, FTSENT *)); +static size_t fts_maxarglen __P((char * const *)); +static void fts_padjust __P((FTS *, FTSENT *)); +static int fts_palloc __P((FTS *, size_t)); +static FTSENT *fts_sort __P((FTS *, FTSENT *, int)); +static u_short fts_stat __P((FTS *, FTSENT *, int)); +static int fts_safe_changedir __P((FTS *, FTSENT *, int)); + +#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) + +#define CLR(opt) (sp->fts_options &= ~(opt)) +#define ISSET(opt) (sp->fts_options & (opt)) +#define SET(opt) (sp->fts_options |= (opt)) + +#define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) +#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) + +/* fts_build flags */ +#define BCHILD 1 /* fts_children */ +#define BNAMES 2 /* fts_children, names only */ +#define BREAD 3 /* fts_read */ + +FTS * +fts_open(argv, options, compar) + char * const *argv; + register int options; + int (*compar) __P((const FTSENT **, const FTSENT **)); +{ + register FTS *sp; + register FTSENT *p, *root; + register int nitems; + FTSENT *parent, *tmp; + int len; + + /* Options check. */ + if (options & ~FTS_OPTIONMASK) { + errno = EINVAL; + return (NULL); + } + + /* Allocate/initialize the stream */ + if ((sp = malloc((u_int)sizeof(FTS))) == NULL) + return (NULL); + memset(sp, 0, sizeof(FTS)); + sp->fts_compar = compar; + sp->fts_options = options; + + /* Shush, GCC. */ + tmp = NULL; + + /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ + if (ISSET(FTS_LOGICAL)) + SET(FTS_NOCHDIR); + + /* + * Start out with 1K of path space, and enough, in any case, + * to hold the user's paths. + */ + if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) + goto mem1; + + /* Allocate/initialize root's parent. */ + if ((parent = fts_alloc(sp, "", 0)) == NULL) + goto mem2; + parent->fts_level = FTS_ROOTPARENTLEVEL; + + /* Allocate/initialize root(s). */ + for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) { + /* Don't allow zero-length paths. */ + if ((len = strlen(*argv)) == 0) { + errno = ENOENT; + goto mem3; + } + + p = fts_alloc(sp, *argv, len); + p->fts_level = FTS_ROOTLEVEL; + p->fts_parent = parent; + p->fts_accpath = p->fts_name; + p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); + + /* Command-line "." and ".." are real directories. */ + if (p->fts_info == FTS_DOT) + p->fts_info = FTS_D; + + /* + * If comparison routine supplied, traverse in sorted + * order; otherwise traverse in the order specified. + */ + if (compar) { + p->fts_link = root; + root = p; + } else { + p->fts_link = NULL; + if (root == NULL) + tmp = root = p; + else { + tmp->fts_link = p; + tmp = p; + } + } + } + if (compar && nitems > 1) + root = fts_sort(sp, root, nitems); + + /* + * Allocate a dummy pointer and make fts_read think that we've just + * finished the node before the root(s); set p->fts_info to FTS_INIT + * so that everything about the "current" node is ignored. + */ + if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) + goto mem3; + sp->fts_cur->fts_link = root; + sp->fts_cur->fts_info = FTS_INIT; + + /* + * If using chdir(2), grab a file descriptor pointing to dot to ensure + * that we can get back here; this could be avoided for some paths, + * but almost certainly not worth the effort. Slashes, symbolic links, + * and ".." are all fairly nasty problems. Note, if we can't get the + * descriptor we run anyway, just more slowly. + */ + if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = open(".", O_RDONLY, 0)) < 0) + SET(FTS_NOCHDIR); + + return (sp); + +mem3: fts_lfree(root); + free(parent); +mem2: free(sp->fts_path); +mem1: free(sp); + return (NULL); +} + +static void +fts_load(sp, p) + FTS *sp; + register FTSENT *p; +{ + register int len; + register char *cp; + + /* + * Load the stream structure for the next traversal. Since we don't + * actually enter the directory until after the preorder visit, set + * the fts_accpath field specially so the chdir gets done to the right + * place and the user can access the first node. From fts_open it's + * known that the path will fit. + */ + len = p->fts_pathlen = p->fts_namelen; + memmove(sp->fts_path, p->fts_name, len + 1); + if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { + len = strlen(++cp); + memmove(p->fts_name, cp, len + 1); + p->fts_namelen = len; + } + p->fts_accpath = p->fts_path = sp->fts_path; + sp->fts_dev = p->fts_dev; +} + +int +fts_close(sp) + FTS *sp; +{ + register FTSENT *freep, *p; + int saved_errno; + + /* + * This still works if we haven't read anything -- the dummy structure + * points to the root list, so we step through to the end of the root + * list which has a valid parent pointer. + */ + if (sp->fts_cur) { + for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { + freep = p; + p = p->fts_link ? p->fts_link : p->fts_parent; + free(freep); + } + free(p); + } + + /* Free up child linked list, sort array, path buffer. */ + if (sp->fts_child) + fts_lfree(sp->fts_child); + if (sp->fts_array) + free(sp->fts_array); + free(sp->fts_path); + + /* Return to original directory, save errno if necessary. */ + if (!ISSET(FTS_NOCHDIR)) { + saved_errno = fchdir(sp->fts_rfd) ? errno : 0; + (void)close(sp->fts_rfd); + + /* Set errno and return. */ + if (saved_errno != 0) { + /* Free up the stream pointer. */ + free(sp); + errno = saved_errno; + return (-1); + } + } + + /* Free up the stream pointer. */ + free(sp); + return (0); +} + +/* + * Special case of "/" at the end of the path so that slashes aren't + * appended which would cause paths to be written as "....//foo". + */ +#define NAPPEND(p) \ + (p->fts_path[p->fts_pathlen - 1] == '/' \ + ? p->fts_pathlen - 1 : p->fts_pathlen) + +FTSENT * +fts_read(sp) + register FTS *sp; +{ + register FTSENT *p, *tmp; + register int instr; + register char *t; + int saved_errno; + + /* If finished or unrecoverable error, return NULL. */ + if (sp->fts_cur == NULL || ISSET(FTS_STOP)) + return (NULL); + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* Save and zero out user instructions. */ + instr = p->fts_instr; + p->fts_instr = FTS_NOINSTR; + + /* Any type of file may be re-visited; re-stat and re-turn. */ + if (instr == FTS_AGAIN) { + p->fts_info = fts_stat(sp, p, 0); + return (p); + } + + /* + * Following a symlink -- SLNONE test allows application to see + * SLNONE and recover. If indirecting through a symlink, have + * keep a pointer to current location. If unable to get that + * pointer, follow fails. + */ + if (instr == FTS_FOLLOW && + (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + return (p); + } + + /* Directory in pre-order. */ + if (p->fts_info == FTS_D) { + /* If skipped or crossed mount point, do post-order visit. */ + if (instr == FTS_SKIP || + (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { + if (p->fts_flags & FTS_SYMFOLLOW) + (void)close(p->fts_symfd); + if (sp->fts_child) { + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + p->fts_info = FTS_DP; + return (p); + } + + /* Rebuild if only read the names and now traversing. */ + if (sp->fts_child && ISSET(FTS_NAMEONLY)) { + CLR(FTS_NAMEONLY); + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + + /* + * Cd to the subdirectory. + * + * If have already read and now fail to chdir, whack the list + * to make the names come out right, and set the parent errno + * so the application will eventually get an error condition. + * Set the FTS_DONTCHDIR flag so that when we logically change + * directories back to the parent we don't do a chdir. + * + * If haven't read do so. If the read fails, fts_build sets + * FTS_STOP or the fts_info field of the node. + */ + if (sp->fts_child) { + if (fts_safe_changedir(sp, p, -1)) { + p->fts_errno = errno; + p->fts_flags |= FTS_DONTCHDIR; + for (p = sp->fts_child; p; p = p->fts_link) + p->fts_accpath = + p->fts_parent->fts_accpath; + } + } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { + if (ISSET(FTS_STOP)) + return (NULL); + return (p); + } + p = sp->fts_child; + sp->fts_child = NULL; + goto name; + } + + /* Move to the next node on this level. */ +next: tmp = p; + if ((p = p->fts_link)) { + free(tmp); + + /* + * If reached the top, return to the original directory (or + * the root of the tree), and load the paths for the next root. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + fts_load(sp, p); + return (sp->fts_cur = p); + } + + /* + * User may have called fts_set on the node. If skipped, + * ignore. If followed, get a file descriptor so we can + * get back if necessary. + */ + if (p->fts_instr == FTS_SKIP) + goto next; + if (p->fts_instr == FTS_FOLLOW) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = + open(".", O_RDONLY, 0)) < 0) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + p->fts_instr = FTS_NOINSTR; + } + +name: t = sp->fts_path + NAPPEND(p->fts_parent); + *t++ = '/'; + memmove(t, p->fts_name, p->fts_namelen + 1); + return (sp->fts_cur = p); + } + + /* Move up to the parent node. */ + p = tmp->fts_parent; + free(tmp); + + if (p->fts_level == FTS_ROOTPARENTLEVEL) { + /* + * Done; free everything up and set errno to 0 so the user + * can distinguish between error and EOF. + */ + free(p); + errno = 0; + return (sp->fts_cur = NULL); + } + + /* NUL terminate the pathname. */ + sp->fts_path[p->fts_pathlen] = '\0'; + + /* + * Return to the parent directory. If at a root node or came through + * a symlink, go back through the file descriptor. Otherwise, cd up + * one directory. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + } else if (p->fts_flags & FTS_SYMFOLLOW) { + if (FCHDIR(sp, p->fts_symfd)) { + saved_errno = errno; + (void)close(p->fts_symfd); + errno = saved_errno; + SET(FTS_STOP); + return (NULL); + } + (void)close(p->fts_symfd); + } else if (!(p->fts_flags & FTS_DONTCHDIR)) { + if (CHDIR(sp, "..")) { + SET(FTS_STOP); + return (NULL); + } + } + p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; + return (sp->fts_cur = p); +} + +/* + * Fts_set takes the stream as an argument although it's not used in this + * implementation; it would be necessary if anyone wanted to add global + * semantics to fts using fts_set. An error return is allowed for similar + * reasons. + */ +/* ARGSUSED */ +int +fts_set(sp, p, instr) + FTS *sp; + FTSENT *p; + int instr; +{ + if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && + instr != FTS_NOINSTR && instr != FTS_SKIP) { + errno = EINVAL; + return (1); + } + p->fts_instr = instr; + return (0); +} + +FTSENT * +fts_children(sp, instr) + register FTS *sp; + int instr; +{ + register FTSENT *p; + int fd; + + if (instr && instr != FTS_NAMEONLY) { + errno = EINVAL; + return (NULL); + } + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* + * Errno set to 0 so user can distinguish empty directory from + * an error. + */ + errno = 0; + + /* Fatal errors stop here. */ + if (ISSET(FTS_STOP)) + return (NULL); + + /* Return logical hierarchy of user's arguments. */ + if (p->fts_info == FTS_INIT) + return (p->fts_link); + + /* + * If not a directory being visited in pre-order, stop here. Could + * allow FTS_DNR, assuming the user has fixed the problem, but the + * same effect is available with FTS_AGAIN. + */ + if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) + return (NULL); + + /* Free up any previous child list. */ + if (sp->fts_child) + fts_lfree(sp->fts_child); + + if (instr == FTS_NAMEONLY) { + SET(FTS_NAMEONLY); + instr = BNAMES; + } else + instr = BCHILD; + + /* + * If using chdir on a relative path and called BEFORE fts_read does + * its chdir to the root of a traversal, we can lose -- we need to + * chdir into the subdirectory, and we don't know where the current + * directory is, so we can't get back so that the upcoming chdir by + * fts_read will work. + */ + if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || + ISSET(FTS_NOCHDIR)) + return (sp->fts_child = fts_build(sp, instr)); + + if ((fd = open(".", O_RDONLY, 0)) < 0) + return (NULL); + sp->fts_child = fts_build(sp, instr); + if (fchdir(fd)) + return (NULL); + (void)close(fd); + return (sp->fts_child); +} + +/* + * This is the tricky part -- do not casually change *anything* in here. The + * idea is to build the linked list of entries that are used by fts_children + * and fts_read. There are lots of special cases. + * + * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is + * set and it's a physical walk (so that symbolic links can't be directories), + * we can do things quickly. First, if it's a 4.4BSD file system, the type + * of the file is in the directory entry. Otherwise, we assume that the number + * of subdirectories in a node is equal to the number of links to the parent. + * The former skips all stat calls. The latter skips stat calls in any leaf + * directories and for any files after the subdirectories in the directory have + * been found, cutting the stat calls by about 2/3. + */ +static FTSENT * +fts_build(sp, type) + register FTS *sp; + int type; +{ + register struct dirent *dp; + register FTSENT *p, *head; + register int nitems; + FTSENT *cur, *tail; + DIR *dirp; + void *oldaddr; + int cderrno, descend, len, level, maxlen, nlinks, oflag, saved_errno, + nostat, doadjust; + char *cp; + + /* Set current node pointer. */ + cur = sp->fts_cur; + + /* + * Open the directory for reading. If this fails, we're done. + * If being called from fts_read, set the fts_info field. + */ +#ifdef FTS_WHITEOUT + if (ISSET(FTS_WHITEOUT)) + oflag = DTF_NODUP|DTF_REWIND; + else + oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND; +#else +#define __opendir2(path, flag) opendir(path) +#endif + if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { + if (type == BREAD) { + cur->fts_info = FTS_DNR; + cur->fts_errno = errno; + } + return (NULL); + } + + /* + * Nlinks is the number of possible entries of type directory in the + * directory if we're cheating on stat calls, 0 if we're not doing + * any stat calls at all, -1 if we're doing stats on everything. + */ + if (type == BNAMES) { + nlinks = 0; + /* Be quiet about nostat, GCC. */ + nostat = 0; + } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { + nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); + nostat = 1; + } else { + nlinks = -1; + nostat = 0; + } + +#ifdef notdef + (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); + (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", + ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); +#endif + /* + * If we're going to need to stat anything or we want to descend + * and stay in the directory, chdir. If this fails we keep going, + * but set a flag so we don't chdir after the post-order visit. + * We won't be able to stat anything, but we can still return the + * names themselves. Note, that since fts_read won't be able to + * chdir into the directory, it will have to return different path + * names than before, i.e. "a/b" instead of "b". Since the node + * has already been visited in pre-order, have to wait until the + * post-order visit to return the error. There is a special case + * here, if there was nothing to stat then it's not an error to + * not be able to stat. This is all fairly nasty. If a program + * needed sorted entries or stat information, they had better be + * checking FTS_NS on the returned nodes. + */ + cderrno = 0; + if (nlinks || type == BREAD) { + if (fts_safe_changedir(sp, cur, dirfd(dirp))) { + if (nlinks && type == BREAD) + cur->fts_errno = errno; + cur->fts_flags |= FTS_DONTCHDIR; + descend = 0; + cderrno = errno; + (void)closedir(dirp); + dirp = NULL; + } else + descend = 1; + } else + descend = 0; + + /* + * Figure out the max file name length that can be stored in the + * current path -- the inner loop allocates more path as necessary. + * We really wouldn't have to do the maxlen calculations here, we + * could do them in fts_read before returning the path, but it's a + * lot easier here since the length is part of the dirent structure. + * + * If not changing directories set a pointer so that can just append + * each new name into the path. + */ + len = NAPPEND(cur); + if (ISSET(FTS_NOCHDIR)) { + cp = sp->fts_path + len; + *cp++ = '/'; + } else { + /* GCC, you're too verbose. */ + cp = NULL; + } + len++; + maxlen = sp->fts_pathlen - len; + + level = cur->fts_level + 1; + + /* Read the directory, attaching each entry to the `link' pointer. */ + doadjust = 0; + for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) { + if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) + continue; + + if ((p = fts_alloc(sp, dp->d_name, (int)dp->d_namlen)) == NULL) + goto mem1; + if (dp->d_namlen >= maxlen) { /* include space for NUL */ + oldaddr = sp->fts_path; + if (fts_palloc(sp, dp->d_namlen +len + 1)) { + /* + * No more memory for path or structures. Save + * errno, free up the current structure and the + * structures already allocated. + */ +mem1: saved_errno = errno; + if (p) + free(p); + fts_lfree(head); + (void)closedir(dirp); + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = saved_errno; + return (NULL); + } + /* Did realloc() change the pointer? */ + if (oldaddr != sp->fts_path) { + doadjust = 1; + if (ISSET(FTS_NOCHDIR)) + cp = sp->fts_path + len; + } + maxlen = sp->fts_pathlen - len; + } + + if (len + dp->d_namlen >= USHRT_MAX) { + /* + * In an FTSENT, fts_pathlen is a u_short so it is + * possible to wraparound here. If we do, free up + * the current structure and the structures already + * allocated, then error out with ENAMETOOLONG. + */ + free(p); + fts_lfree(head); + (void)closedir(dirp); + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = ENAMETOOLONG; + return (NULL); + } + p->fts_level = level; + p->fts_parent = sp->fts_cur; + p->fts_pathlen = len + dp->d_namlen; + +#ifdef FTS_WHITEOUT + if (dp->d_type == DT_WHT) + p->fts_flags |= FTS_ISW; +#endif + + if (cderrno) { + if (nlinks) { + p->fts_info = FTS_NS; + p->fts_errno = cderrno; + } else + p->fts_info = FTS_NSOK; + p->fts_accpath = cur->fts_accpath; + } else if (nlinks == 0 +#ifdef DT_DIR + || (nostat && + dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) +#endif + ) { + p->fts_accpath = + ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; + p->fts_info = FTS_NSOK; + } else { + /* Build a file name for fts_stat to stat. */ + if (ISSET(FTS_NOCHDIR)) { + p->fts_accpath = p->fts_path; + memmove(cp, p->fts_name, p->fts_namelen + 1); + } else + p->fts_accpath = p->fts_name; + /* Stat it. */ + p->fts_info = fts_stat(sp, p, 0); + + /* Decrement link count if applicable. */ + if (nlinks > 0 && (p->fts_info == FTS_D || + p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) + --nlinks; + } + + /* We walk in directory order so "ls -f" doesn't get upset. */ + p->fts_link = NULL; + if (head == NULL) + head = tail = p; + else { + tail->fts_link = p; + tail = p; + } + ++nitems; + } + if (dirp) + (void)closedir(dirp); + + /* + * If realloc() changed the address of the path, adjust the + * addresses for the rest of the tree and the dir list. + */ + if (doadjust) + fts_padjust(sp, head); + + /* + * If not changing directories, reset the path back to original + * state. + */ + if (ISSET(FTS_NOCHDIR)) { + if (len == sp->fts_pathlen || nitems == 0) + --cp; + *cp = '\0'; + } + + /* + * If descended after called from fts_children or after called from + * fts_read and nothing found, get back. At the root level we use + * the saved fd; if one of fts_open()'s arguments is a relative path + * to an empty directory, we wind up here with no other way back. If + * can't get back, we're done. + */ + if (descend && (type == BCHILD || !nitems) && + (cur->fts_level == FTS_ROOTLEVEL ? + FCHDIR(sp, sp->fts_rfd) : CHDIR(sp, ".."))) { + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + return (NULL); + } + + /* If didn't find anything, return NULL. */ + if (!nitems) { + if (type == BREAD) + cur->fts_info = FTS_DP; + return (NULL); + } + + /* Sort the entries. */ + if (sp->fts_compar && nitems > 1) + head = fts_sort(sp, head, nitems); + return (head); +} + +static u_short +fts_stat(sp, p, follow) + FTS *sp; + register FTSENT *p; + int follow; +{ + register FTSENT *t; + register dev_t dev; + register ino_t ino; + struct stat *sbp, sb; + int saved_errno; + + /* If user needs stat info, stat buffer already allocated. */ + sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; + +#ifdef FTS_WHITEOUT + /* check for whiteout */ + if (p->fts_flags & FTS_ISW) { + if (sbp != &sb) { + memset(sbp, '\0', sizeof (*sbp)); + sbp->st_mode = S_IFWHT; + } + return (FTS_W); + } +#endif + + /* + * If doing a logical walk, or application requested FTS_FOLLOW, do + * a stat(2). If that fails, check for a non-existent symlink. If + * fail, set the errno from the stat call. + */ + if (ISSET(FTS_LOGICAL) || follow) { + if (stat(p->fts_accpath, sbp)) { + saved_errno = errno; + if (!lstat(p->fts_accpath, sbp)) { + errno = 0; + return (FTS_SLNONE); + } + p->fts_errno = saved_errno; + goto err; + } + } else if (lstat(p->fts_accpath, sbp)) { + p->fts_errno = errno; +err: memset(sbp, 0, sizeof(struct stat)); + return (FTS_NS); + } + + if (S_ISDIR(sbp->st_mode)) { + /* + * Set the device/inode. Used to find cycles and check for + * crossing mount points. Also remember the link count, used + * in fts_build to limit the number of stat calls. It is + * understood that these fields are only referenced if fts_info + * is set to FTS_D. + */ + dev = p->fts_dev = sbp->st_dev; + ino = p->fts_ino = sbp->st_ino; + p->fts_nlink = sbp->st_nlink; + + if (ISDOT(p->fts_name)) + return (FTS_DOT); + + /* + * Cycle detection is done by brute force when the directory + * is first encountered. If the tree gets deep enough or the + * number of symbolic links to directories is high enough, + * something faster might be worthwhile. + */ + for (t = p->fts_parent; + t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) + if (ino == t->fts_ino && dev == t->fts_dev) { + p->fts_cycle = t; + return (FTS_DC); + } + return (FTS_D); + } + if (S_ISLNK(sbp->st_mode)) + return (FTS_SL); + if (S_ISREG(sbp->st_mode)) + return (FTS_F); + return (FTS_DEFAULT); +} + +static FTSENT * +fts_sort(sp, head, nitems) + FTS *sp; + FTSENT *head; + register int nitems; +{ + register FTSENT **ap, *p; + + /* + * Construct an array of pointers to the structures and call qsort(3). + * Reassemble the array in the order returned by qsort. If unable to + * sort for memory reasons, return the directory entries in their + * current order. Allocate enough space for the current needs plus + * 40 so don't realloc one entry at a time. + */ + if (nitems > sp->fts_nitems) { + struct _ftsent **a; + + sp->fts_nitems = nitems + 40; + if ((a = realloc(sp->fts_array, + sp->fts_nitems * sizeof(FTSENT *))) == NULL) { + if (sp->fts_array) + free(sp->fts_array); + sp->fts_array = NULL; + sp->fts_nitems = 0; + return (head); + } + sp->fts_array = a; + } + for (ap = sp->fts_array, p = head; p; p = p->fts_link) + *ap++ = p; + qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar); + for (head = *(ap = sp->fts_array); --nitems; ++ap) + ap[0]->fts_link = ap[1]; + ap[0]->fts_link = NULL; + return (head); +} + +static FTSENT * +fts_alloc(sp, name, namelen) + FTS *sp; + char *name; + register int namelen; +{ + register FTSENT *p; + size_t len; + + /* + * The file name is a variable length array and no stat structure is + * necessary if the user has set the nostat bit. Allocate the FTSENT + * structure, the file name and the stat structure in one chunk, but + * be careful that the stat structure is reasonably aligned. Since the + * fts_name field is declared to be of size 1, the fts_name pointer is + * namelen + 2 before the first possible address of the stat structure. + */ + len = sizeof(FTSENT) + namelen; + if (!ISSET(FTS_NOSTAT)) + len += sizeof(struct stat) + ALIGNBYTES; + if ((p = malloc(len)) == NULL) + return (NULL); + + /* Copy the name and guarantee NUL termination. */ + memmove(p->fts_name, name, namelen); + p->fts_name[namelen] = '\0'; + + if (!ISSET(FTS_NOSTAT)) + p->fts_statp = (struct stat *)ALIGN(p->fts_name + namelen + 2); + p->fts_namelen = namelen; + p->fts_path = sp->fts_path; + p->fts_errno = 0; + p->fts_flags = 0; + p->fts_instr = FTS_NOINSTR; + p->fts_number = 0; + p->fts_pointer = NULL; + return (p); +} + +static void +fts_lfree(head) + register FTSENT *head; +{ + register FTSENT *p; + + /* Free a linked list of structures. */ + while ((p = head)) { + head = head->fts_link; + free(p); + } +} + +/* + * Allow essentially unlimited paths; find, rm, ls should all work on any tree. + * Most systems will allow creation of paths much longer than MAXPATHLEN, even + * though the kernel won't resolve them. Add the size (not just what's needed) + * plus 256 bytes so don't realloc the path 2 bytes at a time. + */ +static int +fts_palloc(sp, more) + FTS *sp; + size_t more; +{ + char *p; + + sp->fts_pathlen += more + 256; + /* + * Check for possible wraparound. In an FTS, fts_pathlen is + * a signed int but in an FTSENT it is an unsigned short. + * We limit fts_pathlen to USHRT_MAX to be safe in both cases. + */ + if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) { + if (sp->fts_path) + free(sp->fts_path); + sp->fts_path = NULL; + errno = ENAMETOOLONG; + return (1); + } + p = realloc(sp->fts_path, sp->fts_pathlen); + if (p == NULL) { + if (sp->fts_path) + free(sp->fts_path); + sp->fts_path = NULL; + return (1); + } + sp->fts_path = p; + return (0); +} + +/* + * When the path is realloc'd, have to fix all of the pointers in structures + * already returned. + */ +static void +fts_padjust(sp, head) + FTS *sp; + FTSENT *head; +{ + FTSENT *p; + char *addr = sp->fts_path; + +#define ADJUST(p) { \ + if ((p)->fts_accpath != (p)->fts_name) { \ + (p)->fts_accpath = \ + (char *)addr + ((p)->fts_accpath - (p)->fts_path); \ + } \ + (p)->fts_path = addr; \ +} + /* Adjust the current set of children. */ + for (p = sp->fts_child; p; p = p->fts_link) + ADJUST(p); + + /* Adjust the rest of the tree, including the current level. */ + for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { + ADJUST(p); + p = p->fts_link ? p->fts_link : p->fts_parent; + } +} + +static size_t +fts_maxarglen(argv) + char * const *argv; +{ + size_t len, max; + + for (max = 0; *argv; ++argv) + if ((len = strlen(*argv)) > max) + max = len; + return (max + 1); +} + +/* + * Change to dir specified by fd or p->fts_accpath without getting + * tricked by someone changing the world out from underneath us. + * Assumes p->fts_dev and p->fts_ino are filled in. + */ +static int +fts_safe_changedir(sp, p, fd) + FTS *sp; + FTSENT *p; + int fd; +{ + int ret, oerrno, newfd; + struct stat sb; + + newfd = fd; + if (ISSET(FTS_NOCHDIR)) + return (0); + if (fd < 0 && (newfd = open(p->fts_accpath, O_RDONLY, 0)) < 0) + return (-1); + if (fstat(newfd, &sb)) { + ret = -1; + goto bail; + } + if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) { + errno = ENOENT; /* disinformation */ + ret = -1; + goto bail; + } + ret = fchdir(newfd); +bail: + oerrno = errno; + if (fd < 0) + (void)close(newfd); + errno = oerrno; + return (ret); +} diff --git a/lib/libc/gen/getbootfile.3 b/lib/libc/gen/getbootfile.3 new file mode 100644 index 0000000..1ad2b8e --- /dev/null +++ b/lib/libc/gen/getbootfile.3 @@ -0,0 +1,70 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" From: @(#)gethostname.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd September 23, 1994 +.Dt GETBOOTFILE 3 +.Os +.Sh NAME +.Nm getbootfile +.Nd get kernel boot file name +.Sh SYNOPSIS +.Fd #include <paths.h> +.Ft const char * +.Fn getbootfile void +.Sh DESCRIPTION +The +.Fn getbootfile +function retrieves the full pathname of the file from which the +current kernel was loaded, and returns a static pointer to the name. +A read/write interface to this information is available via the +.Xr sysctl 3 +MIB variable +.Dq Li kern.bootfile . +.Sh RETURN VALUES +If the call succeeds a string giving the pathname is returned. If it +fails, a a null pointer is returned and an error code is +placed in the global location +.Va errno . +.Sh SEE ALSO +.Xr sysctl 3 +.Sh BUGS +If the boot blocks have not been modified to pass this information into +the kernel at boot time, the static string +.Dq Pa /kernel +is returned instead of the real boot file. +.Sh HISTORY +The +.Fn getbootfile +function call appeared in +.Fx 2.0 . diff --git a/lib/libc/gen/getbootfile.c b/lib/libc/gen/getbootfile.c new file mode 100644 index 0000000..c2d1558 --- /dev/null +++ b/lib/libc/gen/getbootfile.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +/*static char sccsid[] = "From: @(#)gethostname.c 8.1 (Berkeley) 6/4/93";*/ +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> + +const char * +getbootfile(void) +{ + static char name[MAXPATHLEN]; + size_t size = sizeof name; + int mib[2]; + + mib[0] = CTL_KERN; + mib[1] = KERN_BOOTFILE; + if (sysctl(mib, 2, name, &size, NULL, 0) == -1) + return ("/kernel"); + return (name); +} diff --git a/lib/libc/gen/getbsize.3 b/lib/libc/gen/getbsize.3 new file mode 100644 index 0000000..00b09c6 --- /dev/null +++ b/lib/libc/gen/getbsize.3 @@ -0,0 +1,80 @@ +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getbsize.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt GETBSIZE 3 +.Os BSD 4.4 +.Sh NAME +.Nm getbsize +.Nd get user block size +.Sh SYNOPSIS +.Fd #include <stdlib.h> +.Ft char * +.Fn getbsize "int *headerlenp" "long *blocksizep" +.Sh DESCRIPTION +The +.Fn getbsize +function determines the user's preferred block size based on the value of the +.Dq BLOCKSIZE +environment variable; see +.Xr environ 7 +for details on its use and format. +.Pp +The +.Fn getbsize +function returns a pointer to a null-terminated string describing +the block size, something like +.Dq 1K-blocks . +The memory referenced by +.Fa headerlenp +is filled in with the length of the string (not including the +terminating null). +The memory referenced by +.Fa blocksizep +is filled in with block size, in bytes. +.Pp +If the user's block size is unreasonable, a warning message is +written to standard error and the returned information reflects +a block size of 512 bytes. +.Sh SEE ALSO +.Xr df 1 , +.Xr du 1 , +.Xr ls 1 , +.Xr systat 1 , +.Xr environ 7 +.Sh HISTORY +The +.Fn getbsize +function first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/getbsize.c b/lib/libc/gen/getbsize.c new file mode 100644 index 0000000..6f951e7 --- /dev/null +++ b/lib/libc/gen/getbsize.c @@ -0,0 +1,106 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +static char sccsid[] = "@(#)getbsize.c 8.1 (Berkeley) 6/4/93"; +#endif /* not lint */ + +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +char * +getbsize(headerlenp, blocksizep) + int *headerlenp; + long *blocksizep; +{ + static char header[20]; + long n, max, mul, blocksize; + char *ep, *p, *form; + +#define KB (1024L) +#define MB (1024L * 1024L) +#define GB (1024L * 1024L * 1024L) +#define MAXB GB /* No tera, peta, nor exa. */ + form = ""; + if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') { + if ((n = strtol(p, &ep, 10)) < 0) + goto underflow; + if (n == 0) + n = 1; + if (*ep && ep[1]) + goto fmterr; + switch (*ep) { + case 'G': case 'g': + form = "G"; + max = MAXB / GB; + mul = GB; + break; + case 'K': case 'k': + form = "K"; + max = MAXB / KB; + mul = KB; + break; + case 'M': case 'm': + form = "M"; + max = MAXB / MB; + mul = MB; + break; + case '\0': + max = MAXB; + mul = 1; + break; + default: +fmterr: warnx("%s: unknown blocksize", p); + n = 512; + mul = 1; + break; + } + if (n > max) { + warnx("maximum blocksize is %ldG", MAXB / GB); + n = max; + } + if ((blocksize = n * mul) < 512) { +underflow: warnx("minimum blocksize is 512"); + form = ""; + blocksize = n = 512; + } + } else + blocksize = n = 512; + + (void)snprintf(header, sizeof(header), "%ld%s-blocks", n, form); + *headerlenp = strlen(header); + *blocksizep = blocksize; + return (header); +} diff --git a/lib/libc/gen/getcap.3 b/lib/libc/gen/getcap.3 new file mode 100644 index 0000000..0e6a3da --- /dev/null +++ b/lib/libc/gen/getcap.3 @@ -0,0 +1,529 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Casey Leedom of Lawrence Livermore National Laboratory. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getcap.3 8.4 (Berkeley) 5/13/94 +.\" $FreeBSD$ +.\" +.Dd May 13, 1994 +.Dt GETCAP 3 +.Os +.Sh NAME +.Nm cgetent , +.Nm cgetset , +.Nm cgetmatch , +.Nm cgetcap , +.Nm cgetnum , +.Nm cgetstr , +.Nm cgetustr , +.Nm cgetfirst , +.Nm cgetnext , +.Nm cgetclose +.Nd capability database access routines +.Sh SYNOPSIS +.Fd #include <stdlib.h> +.Ft int +.Fn cgetent "char **buf" "char **db_array" "char *name" +.Ft int +.Fn cgetset "char *ent" +.Ft int +.Fn cgetmatch "char *buf" "char *name" +.Ft char * +.Fn cgetcap "char *buf" "char *cap" "int type" +.Ft int +.Fn cgetnum "char *buf" "char *cap" "long *num" +.Ft int +.Fn cgetstr "char *buf" "char *cap" "char **str" +.Ft int +.Fn cgetustr "char *buf" "char *cap" "char **str" +.Ft int +.Fn cgetfirst "char **buf" "char **db_array" +.Ft int +.Fn cgetnext "char **buf" "char **db_array" +.Ft int +.Fn cgetclose "void" +.Sh DESCRIPTION +.Fn Cgetent +extracts the capability +.Fa name +from the database specified by the +.Dv NULL +terminated file array +.Fa db_array +and returns a pointer to a +.Xr malloc Ns \&'d +copy of it in +.Fa buf . +The +.Fn cgetent +function will first look for files ending in +.Nm .db +(see +.Xr cap_mkdb 1) +before accessing the ASCII file. +.Fa Buf +must be retained through all subsequent calls to +.Fn cgetmatch , +.Fn cgetcap , +.Fn cgetnum , +.Fn cgetstr , +and +.Fn cgetustr , +but may then be +.Xr free 3 Ns \&'d. +On success 0 is returned, 1 if the returned +record contains an unresolved +.Nm tc +expansion, +\-1 if the requested record couldn't be found, +\-2 if a system error was encountered (couldn't open/read a file, etc.) also +setting +.Va errno , +and \-3 if a potential reference loop is detected (see +.Ic tc= +comments below). +.Pp +The +.Fn cgetset +function enables the addition of a character buffer containing a single capability +record entry +to the capability database. +Conceptually, the entry is added as the first ``file'' in the database, and +is therefore searched first on the call to +.Fn cgetent . +The entry is passed in +.Fa ent . +If +.Fa ent +is +.Dv NULL , +the current entry is removed from the database. +A call to +.Fn cgetset +must precede the database traversal. It must be called before the +.Fn cgetent +call. If a sequential access is being performed (see below), it must be called +before the first sequential access call ( +.Fn cgetfirst +or +.Fn cgetnext +), or be directly preceded by a +.Fn cgetclose +call. +On success 0 is returned and \-1 on failure. +.Pp +The +.Fn cgetmatch +function will return 0 if +.Fa name +is one of the names of the capability record +.Fa buf , +\-1 if +not. +.Pp +The +.Fn cgetcap +function searches the capability record +.Fa buf +for the capability +.Fa cap +with type +.Fa type . +A +.Fa type +is specified using any single character. If a colon (`:') is used, an +untyped capability will be searched for (see below for explanation of +types). A pointer to the value of +.Fa cap +in +.Fa buf +is returned on success, +.Dv NULL +if the requested capability couldn't be +found. The end of the capability value is signaled by a `:' or +.Tn ASCII +.Dv NUL +(see below for capability database syntax). +.Pp +The +.Fn cgetnum +function retrieves the value of the numeric capability +.Fa cap +from the capability record pointed to by +.Fa buf . +The numeric value is returned in the +.Ft long +pointed to by +.Fa num . +0 is returned on success, \-1 if the requested numeric capability couldn't +be found. +.Pp +The +.Fn cgetstr +function retrieves the value of the string capability +.Fa cap +from the capability record pointed to by +.Fa buf . +A pointer to a decoded, +.Dv NUL +terminated, +.Xr malloc Ns \&'d +copy of the string is returned in the +.Ft char * +pointed to by +.Fa str . +The number of characters in the decoded string not including the trailing +.Dv NUL +is returned on success, \-1 if the requested string capability couldn't +be found, \-2 if a system error was encountered (storage allocation +failure). +.Pp +The +.Fn cgetustr +function is identical to +.Fn cgetstr +except that it does not expand special characters, but rather returns each +character of the capability string literally. +.Pp +The +.Fn cgetfirst +and +.Fn cgetnext +functions comprise a function group that provides for sequential +access of the +.Dv NULL +pointer terminated array of file names, +.Fa db_array . +The +.Fn cgetfirst +function returns the first record in the database and resets the access +to the first record. +The +.Fn cgetnext +function returns the next record in the database with respect to the +record returned by the previous +.Fn cgetfirst +or +.Fn cgetnext +call. If there is no such previous call, the first record in the database is +returned. +Each record is returned in a +.Xr malloc Ns \&'d +copy pointed to by +.Fa buf . +.Ic Tc +expansion is done (see +.Ic tc= +comments below). +Upon completion of the database 0 is returned, 1 is returned upon successful +return of record with possibly more remaining (we haven't reached the end of +the database yet), 2 is returned if the record contains an unresolved +.Nm tc +expansion, \-1 is returned if an system error occurred, and \-2 +is returned if a potential reference loop is detected (see +.Ic tc= +comments below). +Upon completion of database (0 return) the database is closed. +.Pp +The +.Fn cgetclose +function closes the sequential access and frees any memory and file descriptors +being used. Note that it does not erase the buffer pushed by a call to +.Fn cgetset . +.Sh CAPABILITY DATABASE SYNTAX +Capability databases are normally +.Tn ASCII +and may be edited with standard +text editors. Blank lines and lines beginning with a `#' are comments +and are ignored. Lines ending with a `\|\e' indicate that the next line +is a continuation of the current line; the `\|\e' and following newline +are ignored. Long lines are usually continued onto several physical +lines by ending each line except the last with a `\|\e'. +.Pp +Capability databases consist of a series of records, one per logical +line. Each record contains a variable number of `:'-separated fields +(capabilities). Empty fields consisting entirely of white space +characters (spaces and tabs) are ignored. +.Pp +The first capability of each record specifies its names, separated by `|' +characters. These names are used to reference records in the database. +By convention, the last name is usually a comment and is not intended as +a lookup tag. For example, the +.Em vt100 +record from the +.Nm termcap +database begins: +.Pp +.Dl "d0\||\|vt100\||\|vt100-am\||\|vt100am\||\|dec vt100:" +.Pp +giving four names that can be used to access the record. +.Pp +The remaining non-empty capabilities describe a set of (name, value) +bindings, consisting of a names optionally followed by a typed values: +.Bl -column "nameTvalue" +.It name Ta "typeless [boolean] capability" +.Em name No "is present [true]" +.It name Ns Em \&T Ns value Ta capability +.Pq Em name , \&T +has value +.Em value +.It name@ Ta "no capability" Em name No exists +.It name Ns Em T Ns \&@ Ta capability +.Pq Em name , T +does not exist +.El +.Pp +Names consist of one or more characters. Names may contain any character +except `:', but it's usually best to restrict them to the printable +characters and avoid use of graphics like `#', `=', `%', `@', etc. Types +are single characters used to separate capability names from their +associated typed values. Types may be any character except a `:'. +Typically, graphics like `#', `=', `%', etc. are used. Values may be any +number of characters and may contain any character except `:'. +.Sh CAPABILITY DATABASE SEMANTICS +Capability records describe a set of (name, value) bindings. Names may +have multiple values bound to them. Different values for a name are +distinguished by their +.Fa types . +The +.Fn cgetcap +function will return a pointer to a value of a name given the capability +name and the type of the value. +.Pp +The types `#' and `=' are conventionally used to denote numeric and +string typed values, but no restriction on those types is enforced. The +functions +.Fn cgetnum +and +.Fn cgetstr +can be used to implement the traditional syntax and semantics of `#' +and `='. +Typeless capabilities are typically used to denote boolean objects with +presence or absence indicating truth and false values respectively. +This interpretation is conveniently represented by: +.Pp +.Dl "(getcap(buf, name, ':') != NULL)" +.Pp +A special capability, +.Ic tc= name , +is used to indicate that the record specified by +.Fa name +should be substituted for the +.Ic tc +capability. +.Ic Tc +capabilities may interpolate records which also contain +.Ic tc +capabilities and more than one +.Ic tc +capability may be used in a record. A +.Ic tc +expansion scope (i.e., where the argument is searched for) contains the +file in which the +.Ic tc +is declared and all subsequent files in the file array. +.Pp +When a database is searched for a capability record, the first matching +record in the search is returned. When a record is scanned for a +capability, the first matching capability is returned; the capability +.Ic :nameT@: +will hide any following definition of a value of type +.Em T +for +.Fa name ; +and the capability +.Ic :name@: +will prevent any following values of +.Fa name +from being seen. +.Pp +These features combined with +.Ic tc +capabilities can be used to generate variations of other databases and +records by either adding new capabilities, overriding definitions with new +definitions, or hiding following definitions via `@' capabilities. +.Sh EXAMPLES +.Bd -unfilled -offset indent +example\||\|an example of binding multiple values to names:\e + :foo%bar:foo^blah:foo@:\e + :abc%xyz:abc^frap:abc$@:\e + :tc=more: +.Ed +.Pp +The capability foo has two values bound to it (bar of type `%' and blah of +type `^') and any other value bindings are hidden. The capability abc +also has two values bound but only a value of type `$' is prevented from +being defined in the capability record more. +.Pp +.Bd -unfilled -offset indent +file1: + new\||\|new_record\||\|a modification of "old":\e + :fript=bar:who-cares@:tc=old:blah:tc=extensions: +file2: + old\||\|old_record\||\|an old database record:\e + :fript=foo:who-cares:glork#200: +.Ed +.Pp +The records are extracted by calling +.Fn cgetent +with file1 preceding file2. +In the capability record new in file1, fript=bar overrides the definition +of fript=foo interpolated from the capability record old in file2, +who-cares@ prevents the definition of any who-cares definitions in old +from being seen, glork#200 is inherited from old, and blah and anything +defined by the record extensions is added to those definitions in old. +Note that the position of the fript=bar and who-cares@ definitions before +tc=old is important here. If they were after, the definitions in old +would take precedence. +.Sh CGETNUM AND CGETSTR SYNTAX AND SEMANTICS +Two types are predefined by +.Fn cgetnum +and +.Fn cgetstr : +.Bl -column "nameXnumber" +.Sm off +.It Em name No \&# Em number Ta numeric +capability +.Em name +has value +.Em number +.It Em name No = Em string Ta "string capability" +.Em name +has value +.Em string +.It Em name No \&#@ Ta "the numeric capability" +.Em name +does not exist +.It Em name No \&=@ Ta "the string capability" +.Em name +does not exist +.El +.Pp +Numeric capability values may be given in one of three numeric bases. +If the number starts with either +.Ql 0x +or +.Ql 0X +it is interpreted as a hexadecimal number (both upper and lower case a-f +may be used to denote the extended hexadecimal digits). +Otherwise, if the number starts with a +.Ql 0 +it is interpreted as an octal number. +Otherwise the number is interpreted as a decimal number. +.Pp +String capability values may contain any character. Non-printable +.Dv ASCII +codes, new lines, and colons may be conveniently represented by the use +of escape sequences: +.Bl -column "\e\|X,X\e\|X" "(ASCII octal nnn)" +^X ('X' & 037) control-X +\e\|b, \e\|B (ASCII 010) backspace +\e\|t, \e\|T (ASCII 011) tab +\e\|n, \e\|N (ASCII 012) line feed (newline) +\e\|f, \e\|F (ASCII 014) form feed +\e\|r, \e\|R (ASCII 015) carriage return +\e\|e, \e\|E (ASCII 027) escape +\e\|c, \e\|C (:) colon +\e\|\e (\e\|) back slash +\e\|^ (^) caret +\e\|nnn (ASCII octal nnn) +.El +.Pp +A `\|\e' may be followed by up to three octal digits directly specifies +the numeric code for a character. The use of +.Tn ASCII +.Dv NUL Ns s , +while easily +encoded, causes all sorts of problems and must be used with care since +.Dv NUL Ns s +are typically used to denote the end of strings; many applications +use `\e\|200' to represent a +.Dv NUL . +.Sh DIAGNOSTICS +.Fn Cgetent , +.Fn cgetset , +.Fn cgetmatch , +.Fn cgetnum , +.Fn cgetstr , +.Fn cgetustr , +.Fn cgetfirst , +and +.Fn cgetnext +return a value greater than or equal to 0 on success and a value less +than 0 on failure. +The +.Fn cgetcap +function returns a character pointer on success and a +.Dv NULL +on failure. +.Pp +The +.Fn cgetent , +and +.Fn cgetseq +functions may fail and set +.Va errno +for any of the errors specified for the library functions: +.Xr fopen 3 , +.Xr fclose 3 , +.Xr open 2 , +and +.Xr close 2 . +.Pp +The +.Fn cgetent , +.Fn cgetset , +.Fn cgetstr , +and +.Fn cgetustr +may fail and set +.Va errno +as follows: +.Bl -tag -width Er +.It Bq Er ENOMEM +No memory to allocate. +.El +.Sh SEE ALSO +.Xr cap_mkdb 1 , +.Xr malloc 3 +.Sh BUGS +Colons (`:') can't be used in names, types, or values. +.Pp +There are no checks for +.Ic tc= name +loops in +.Fn cgetent . +.Pp +The buffer added to the database by a call to +.Fn cgetset +is not unique to the database but is rather prepended to any database used. diff --git a/lib/libc/gen/getcap.c b/lib/libc/gen/getcap.c new file mode 100644 index 0000000..d1e33b4 --- /dev/null +++ b/lib/libc/gen/getcap.c @@ -0,0 +1,1046 @@ +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Casey Leedom of Lawrence Livermore National Laboratory. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> + +#include <ctype.h> +#include <db.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define BFRAG 1024 +#define BSIZE 1024 +#define ESC ('[' & 037) /* ASCII ESC */ +#define MAX_RECURSION 32 /* maximum getent recursion */ +#define SFRAG 100 /* cgetstr mallocs in SFRAG chunks */ + +#define RECOK (char)0 +#define TCERR (char)1 +#define SHADOW (char)2 + +static size_t topreclen; /* toprec length */ +static char *toprec; /* Additional record specified by cgetset() */ +static int gottoprec; /* Flag indicating retrieval of toprecord */ + +static int cdbget __P((DB *, char **, char *)); +static int getent __P((char **, u_int *, char **, int, char *, int, char *)); +static int nfcmp __P((char *, char *)); + +/* + * Cgetset() allows the addition of a user specified buffer to be added + * to the database array, in effect "pushing" the buffer on top of the + * virtual database. 0 is returned on success, -1 on failure. + */ +int +cgetset(ent) + char *ent; +{ + if (ent == NULL) { + if (toprec) + free(toprec); + toprec = NULL; + topreclen = 0; + return (0); + } + topreclen = strlen(ent); + if ((toprec = malloc (topreclen + 1)) == NULL) { + errno = ENOMEM; + return (-1); + } + gottoprec = 0; + (void)strcpy(toprec, ent); + return (0); +} + +/* + * Cgetcap searches the capability record buf for the capability cap with + * type `type'. A pointer to the value of cap is returned on success, NULL + * if the requested capability couldn't be found. + * + * Specifying a type of ':' means that nothing should follow cap (:cap:). + * In this case a pointer to the terminating ':' or NUL will be returned if + * cap is found. + * + * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator) + * return NULL. + */ +char * +cgetcap(buf, cap, type) + char *buf, *cap; + int type; +{ + register char *bp, *cp; + + bp = buf; + for (;;) { + /* + * Skip past the current capability field - it's either the + * name field if this is the first time through the loop, or + * the remainder of a field whose name failed to match cap. + */ + for (;;) + if (*bp == '\0') + return (NULL); + else + if (*bp++ == ':') + break; + + /* + * Try to match (cap, type) in buf. + */ + for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++) + continue; + if (*cp != '\0') + continue; + if (*bp == '@') + return (NULL); + if (type == ':') { + if (*bp != '\0' && *bp != ':') + continue; + return(bp); + } + if (*bp != type) + continue; + bp++; + return (*bp == '@' ? NULL : bp); + } + /* NOTREACHED */ +} + +/* + * Cgetent extracts the capability record name from the NULL terminated file + * array db_array and returns a pointer to a malloc'd copy of it in buf. + * Buf must be retained through all subsequent calls to cgetcap, cgetnum, + * cgetflag, and cgetstr, but may then be free'd. 0 is returned on success, + * -1 if the requested record couldn't be found, -2 if a system error was + * encountered (couldn't open/read a file, etc.), and -3 if a potential + * reference loop is detected. + */ +int +cgetent(buf, db_array, name) + char **buf, **db_array, *name; +{ + u_int dummy; + + return (getent(buf, &dummy, db_array, -1, name, 0, NULL)); +} + +/* + * Getent implements the functions of cgetent. If fd is non-negative, + * *db_array has already been opened and fd is the open file descriptor. We + * do this to save time and avoid using up file descriptors for tc= + * recursions. + * + * Getent returns the same success/failure codes as cgetent. On success, a + * pointer to a malloc'ed capability record with all tc= capabilities fully + * expanded and its length (not including trailing ASCII NUL) are left in + * *cap and *len. + * + * Basic algorithm: + * + Allocate memory incrementally as needed in chunks of size BFRAG + * for capability buffer. + * + Recurse for each tc=name and interpolate result. Stop when all + * names interpolated, a name can't be found, or depth exceeds + * MAX_RECURSION. + */ +static int +getent(cap, len, db_array, fd, name, depth, nfield) + char **cap, **db_array, *name, *nfield; + u_int *len; + int fd, depth; +{ + DB *capdbp; + register char *r_end, *rp, **db_p; + int myfd, eof, foundit, retval, clen; + char *record, *cbuf; + int tc_not_resolved; + char pbuf[_POSIX_PATH_MAX]; + + /* + * Return with ``loop detected'' error if we've recursed more than + * MAX_RECURSION times. + */ + if (depth > MAX_RECURSION) + return (-3); + + /* + * Check if we have a top record from cgetset(). + */ + if (depth == 0 && toprec != NULL && cgetmatch(toprec, name) == 0) { + if ((record = malloc (topreclen + BFRAG)) == NULL) { + errno = ENOMEM; + return (-2); + } + (void)strcpy(record, toprec); + myfd = 0; + db_p = db_array; + rp = record + topreclen + 1; + r_end = rp + BFRAG; + goto tc_exp; + } + /* + * Allocate first chunk of memory. + */ + if ((record = malloc(BFRAG)) == NULL) { + errno = ENOMEM; + return (-2); + } + r_end = record + BFRAG; + foundit = 0; + /* + * Loop through database array until finding the record. + */ + + for (db_p = db_array; *db_p != NULL; db_p++) { + eof = 0; + + /* + * Open database if not already open. + */ + + if (fd >= 0) { + (void)lseek(fd, (off_t)0, SEEK_SET); + myfd = 0; + } else { + (void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p); + if ((capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0)) + != NULL) { + free(record); + retval = cdbget(capdbp, &record, name); + if (retval < 0) { + /* no record available */ + (void)capdbp->close(capdbp); + return (retval); + } + /* save the data; close frees it */ + clen = strlen(record); + cbuf = malloc(clen + 1); + memcpy(cbuf, record, clen + 1); + if (capdbp->close(capdbp) < 0) { + free(cbuf); + return (-2); + } + *len = clen; + *cap = cbuf; + return (retval); + } else { + fd = open(*db_p, O_RDONLY, 0); + if (fd < 0) + continue; + myfd = 1; + } + } + /* + * Find the requested capability record ... + */ + { + char buf[BUFSIZ]; + register char *b_end, *bp; + register int c; + + /* + * Loop invariants: + * There is always room for one more character in record. + * R_end always points just past end of record. + * Rp always points just past last character in record. + * B_end always points just past last character in buf. + * Bp always points at next character in buf. + */ + b_end = buf; + bp = buf; + for (;;) { + + /* + * Read in a line implementing (\, newline) + * line continuation. + */ + rp = record; + for (;;) { + if (bp >= b_end) { + int n; + + n = read(fd, buf, sizeof(buf)); + if (n <= 0) { + if (myfd) + (void)close(fd); + if (n < 0) { + free(record); + return (-2); + } else { + fd = -1; + eof = 1; + break; + } + } + b_end = buf+n; + bp = buf; + } + + c = *bp++; + if (c == '\n') { + if (rp > record && *(rp-1) == '\\') { + rp--; + continue; + } else + break; + } + *rp++ = c; + + /* + * Enforce loop invariant: if no room + * left in record buffer, try to get + * some more. + */ + if (rp >= r_end) { + u_int pos; + size_t newsize; + + pos = rp - record; + newsize = r_end - record + BFRAG; + record = reallocf(record, newsize); + if (record == NULL) { + errno = ENOMEM; + if (myfd) + (void)close(fd); + return (-2); + } + r_end = record + newsize; + rp = record + pos; + } + } + /* loop invariant let's us do this */ + *rp++ = '\0'; + + /* + * If encountered eof check next file. + */ + if (eof) + break; + + /* + * Toss blank lines and comments. + */ + if (*record == '\0' || *record == '#') + continue; + + /* + * See if this is the record we want ... + */ + if (cgetmatch(record, name) == 0) { + if (nfield == NULL || !nfcmp(nfield, record)) { + foundit = 1; + break; /* found it! */ + } + } + } + } + if (foundit) + break; + } + + if (!foundit) + return (-1); + + /* + * Got the capability record, but now we have to expand all tc=name + * references in it ... + */ +tc_exp: { + register char *newicap, *s; + register int newilen; + u_int ilen; + int diff, iret, tclen; + char *icap, *scan, *tc, *tcstart, *tcend; + + /* + * Loop invariants: + * There is room for one more character in record. + * R_end points just past end of record. + * Rp points just past last character in record. + * Scan points at remainder of record that needs to be + * scanned for tc=name constructs. + */ + scan = record; + tc_not_resolved = 0; + for (;;) { + if ((tc = cgetcap(scan, "tc", '=')) == NULL) + break; + + /* + * Find end of tc=name and stomp on the trailing `:' + * (if present) so we can use it to call ourselves. + */ + s = tc; + for (;;) + if (*s == '\0') + break; + else + if (*s++ == ':') { + *(s - 1) = '\0'; + break; + } + tcstart = tc - 3; + tclen = s - tcstart; + tcend = s; + + iret = getent(&icap, &ilen, db_p, fd, tc, depth+1, + NULL); + newicap = icap; /* Put into a register. */ + newilen = ilen; + if (iret != 0) { + /* an error */ + if (iret < -1) { + if (myfd) + (void)close(fd); + free(record); + return (iret); + } + if (iret == 1) + tc_not_resolved = 1; + /* couldn't resolve tc */ + if (iret == -1) { + *(s - 1) = ':'; + scan = s - 1; + tc_not_resolved = 1; + continue; + + } + } + /* not interested in name field of tc'ed record */ + s = newicap; + for (;;) + if (*s == '\0') + break; + else + if (*s++ == ':') + break; + newilen -= s - newicap; + newicap = s; + + /* make sure interpolated record is `:'-terminated */ + s += newilen; + if (*(s-1) != ':') { + *s = ':'; /* overwrite NUL with : */ + newilen++; + } + + /* + * Make sure there's enough room to insert the + * new record. + */ + diff = newilen - tclen; + if (diff >= r_end - rp) { + u_int pos, tcpos, tcposend; + size_t newsize; + + pos = rp - record; + newsize = r_end - record + diff + BFRAG; + tcpos = tcstart - record; + tcposend = tcend - record; + record = reallocf(record, newsize); + if (record == NULL) { + errno = ENOMEM; + if (myfd) + (void)close(fd); + free(icap); + return (-2); + } + r_end = record + newsize; + rp = record + pos; + tcstart = record + tcpos; + tcend = record + tcposend; + } + + /* + * Insert tc'ed record into our record. + */ + s = tcstart + newilen; + bcopy(tcend, s, rp - tcend); + bcopy(newicap, tcstart, newilen); + rp += diff; + free(icap); + + /* + * Start scan on `:' so next cgetcap works properly + * (cgetcap always skips first field). + */ + scan = s-1; + } + + } + /* + * Close file (if we opened it), give back any extra memory, and + * return capability, length and success. + */ + if (myfd) + (void)close(fd); + *len = rp - record - 1; /* don't count NUL */ + if (r_end > rp) + if ((record = + reallocf(record, (size_t)(rp - record))) == NULL) { + errno = ENOMEM; + return (-2); + } + + *cap = record; + if (tc_not_resolved) + return (1); + return (0); +} + +static int +cdbget(capdbp, bp, name) + DB *capdbp; + char **bp, *name; +{ + DBT key, data; + + key.data = name; + key.size = strlen(name); + + for (;;) { + /* Get the reference. */ + switch(capdbp->get(capdbp, &key, &data, 0)) { + case -1: + return (-2); + case 1: + return (-1); + } + + /* If not an index to another record, leave. */ + if (((char *)data.data)[0] != SHADOW) + break; + + key.data = (char *)data.data + 1; + key.size = data.size - 1; + } + + *bp = (char *)data.data + 1; + return (((char *)(data.data))[0] == TCERR ? 1 : 0); +} + +/* + * Cgetmatch will return 0 if name is one of the names of the capability + * record buf, -1 if not. + */ +int +cgetmatch(buf, name) + char *buf, *name; +{ + register char *np, *bp; + + /* + * Start search at beginning of record. + */ + bp = buf; + for (;;) { + /* + * Try to match a record name. + */ + np = name; + for (;;) + if (*np == '\0') + if (*bp == '|' || *bp == ':' || *bp == '\0') + return (0); + else + break; + else + if (*bp++ != *np++) + break; + + /* + * Match failed, skip to next name in record. + */ + bp--; /* a '|' or ':' may have stopped the match */ + for (;;) + if (*bp == '\0' || *bp == ':') + return (-1); /* match failed totally */ + else + if (*bp++ == '|') + break; /* found next name */ + } +} + + + + + +int +cgetfirst(buf, db_array) + char **buf, **db_array; +{ + (void)cgetclose(); + return (cgetnext(buf, db_array)); +} + +static FILE *pfp; +static int slash; +static char **dbp; + +int +cgetclose() +{ + if (pfp != NULL) { + (void)fclose(pfp); + pfp = NULL; + } + dbp = NULL; + gottoprec = 0; + slash = 0; + return(0); +} + +/* + * Cgetnext() gets either the first or next entry in the logical database + * specified by db_array. It returns 0 upon completion of the database, 1 + * upon returning an entry with more remaining, and -1 if an error occurs. + */ +int +cgetnext(bp, db_array) + register char **bp; + char **db_array; +{ + size_t len; + int status, i, done; + char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE]; + u_int dummy; + + if (dbp == NULL) + dbp = db_array; + + if (pfp == NULL && (pfp = fopen(*dbp, "r")) == NULL) { + (void)cgetclose(); + return (-1); + } + for(;;) { + if (toprec && !gottoprec) { + gottoprec = 1; + line = toprec; + } else { + line = fgetln(pfp, &len); + if (line == NULL && pfp) { + (void)fclose(pfp); + if (ferror(pfp)) { + (void)cgetclose(); + return (-1); + } else { + if (*++dbp == NULL) { + (void)cgetclose(); + return (0); + } else if ((pfp = + fopen(*dbp, "r")) == NULL) { + (void)cgetclose(); + return (-1); + } else + continue; + } + } else + line[len - 1] = '\0'; + if (len == 1) { + slash = 0; + continue; + } + if (isspace((unsigned char)*line) || + *line == ':' || *line == '#' || slash) { + if (line[len - 2] == '\\') + slash = 1; + else + slash = 0; + continue; + } + if (line[len - 2] == '\\') + slash = 1; + else + slash = 0; + } + + + /* + * Line points to a name line. + */ + i = 0; + done = 0; + np = nbuf; + for (;;) { + for (cp = line; *cp != '\0'; cp++) { + if (*cp == ':') { + *np++ = ':'; + done = 1; + break; + } + if (*cp == '\\') + break; + *np++ = *cp; + } + if (done) { + *np = '\0'; + break; + } else { /* name field extends beyond the line */ + line = fgetln(pfp, &len); + if (line == NULL && pfp) { + (void)fclose(pfp); + if (ferror(pfp)) { + (void)cgetclose(); + return (-1); + } + } else + line[len - 1] = '\0'; + } + } + rp = buf; + for(cp = nbuf; *cp != '\0'; cp++) + if (*cp == '|' || *cp == ':') + break; + else + *rp++ = *cp; + + *rp = '\0'; + /* + * XXX + * Last argument of getent here should be nbuf if we want true + * sequential access in the case of duplicates. + * With NULL, getent will return the first entry found + * rather than the duplicate entry record. This is a + * matter of semantics that should be resolved. + */ + status = getent(bp, &dummy, db_array, -1, buf, 0, NULL); + if (status == -2 || status == -3) + (void)cgetclose(); + + return (status + 1); + } + /* NOTREACHED */ +} + +/* + * Cgetstr retrieves the value of the string capability cap from the + * capability record pointed to by buf. A pointer to a decoded, NUL + * terminated, malloc'd copy of the string is returned in the char * + * pointed to by str. The length of the string not including the trailing + * NUL is returned on success, -1 if the requested string capability + * couldn't be found, -2 if a system error was encountered (storage + * allocation failure). + */ +int +cgetstr(buf, cap, str) + char *buf, *cap; + char **str; +{ + register u_int m_room; + register char *bp, *mp; + int len; + char *mem; + + /* + * Find string capability cap + */ + bp = cgetcap(buf, cap, '='); + if (bp == NULL) + return (-1); + + /* + * Conversion / storage allocation loop ... Allocate memory in + * chunks SFRAG in size. + */ + if ((mem = malloc(SFRAG)) == NULL) { + errno = ENOMEM; + return (-2); /* couldn't even allocate the first fragment */ + } + m_room = SFRAG; + mp = mem; + + while (*bp != ':' && *bp != '\0') { + /* + * Loop invariants: + * There is always room for one more character in mem. + * Mp always points just past last character in mem. + * Bp always points at next character in buf. + */ + if (*bp == '^') { + bp++; + if (*bp == ':' || *bp == '\0') + break; /* drop unfinished escape */ + if (*bp == '?') { + *mp++ = '\177'; + bp++; + } else + *mp++ = *bp++ & 037; + } else if (*bp == '\\') { + bp++; + if (*bp == ':' || *bp == '\0') + break; /* drop unfinished escape */ + if ('0' <= *bp && *bp <= '7') { + register int n, i; + + n = 0; + i = 3; /* maximum of three octal digits */ + do { + n = n * 8 + (*bp++ - '0'); + } while (--i && '0' <= *bp && *bp <= '7'); + *mp++ = n; + } + else switch (*bp++) { + case 'b': case 'B': + *mp++ = '\b'; + break; + case 't': case 'T': + *mp++ = '\t'; + break; + case 'n': case 'N': + *mp++ = '\n'; + break; + case 'f': case 'F': + *mp++ = '\f'; + break; + case 'r': case 'R': + *mp++ = '\r'; + break; + case 'e': case 'E': + *mp++ = ESC; + break; + case 'c': case 'C': + *mp++ = ':'; + break; + default: + /* + * Catches '\', '^', and + * everything else. + */ + *mp++ = *(bp-1); + break; + } + } else + *mp++ = *bp++; + m_room--; + + /* + * Enforce loop invariant: if no room left in current + * buffer, try to get some more. + */ + if (m_room == 0) { + size_t size = mp - mem; + + if ((mem = reallocf(mem, size + SFRAG)) == NULL) + return (-2); + m_room = SFRAG; + mp = mem + size; + } + } + *mp++ = '\0'; /* loop invariant let's us do this */ + m_room--; + len = mp - mem - 1; + + /* + * Give back any extra memory and return value and success. + */ + if (m_room != 0) + if ((mem = reallocf(mem, (size_t)(mp - mem))) == NULL) + return (-2); + *str = mem; + return (len); +} + +/* + * Cgetustr retrieves the value of the string capability cap from the + * capability record pointed to by buf. The difference between cgetustr() + * and cgetstr() is that cgetustr does not decode escapes but rather treats + * all characters literally. A pointer to a NUL terminated malloc'd + * copy of the string is returned in the char pointed to by str. The + * length of the string not including the trailing NUL is returned on success, + * -1 if the requested string capability couldn't be found, -2 if a system + * error was encountered (storage allocation failure). + */ +int +cgetustr(buf, cap, str) + char *buf, *cap, **str; +{ + register u_int m_room; + register char *bp, *mp; + int len; + char *mem; + + /* + * Find string capability cap + */ + if ((bp = cgetcap(buf, cap, '=')) == NULL) + return (-1); + + /* + * Conversion / storage allocation loop ... Allocate memory in + * chunks SFRAG in size. + */ + if ((mem = malloc(SFRAG)) == NULL) { + errno = ENOMEM; + return (-2); /* couldn't even allocate the first fragment */ + } + m_room = SFRAG; + mp = mem; + + while (*bp != ':' && *bp != '\0') { + /* + * Loop invariants: + * There is always room for one more character in mem. + * Mp always points just past last character in mem. + * Bp always points at next character in buf. + */ + *mp++ = *bp++; + m_room--; + + /* + * Enforce loop invariant: if no room left in current + * buffer, try to get some more. + */ + if (m_room == 0) { + size_t size = mp - mem; + + if ((mem = reallocf(mem, size + SFRAG)) == NULL) + return (-2); + m_room = SFRAG; + mp = mem + size; + } + } + *mp++ = '\0'; /* loop invariant let's us do this */ + m_room--; + len = mp - mem - 1; + + /* + * Give back any extra memory and return value and success. + */ + if (m_room != 0) + if ((mem = reallocf(mem, (size_t)(mp - mem))) == NULL) + return (-2); + *str = mem; + return (len); +} + +/* + * Cgetnum retrieves the value of the numeric capability cap from the + * capability record pointed to by buf. The numeric value is returned in + * the long pointed to by num. 0 is returned on success, -1 if the requested + * numeric capability couldn't be found. + */ +int +cgetnum(buf, cap, num) + char *buf, *cap; + long *num; +{ + register long n; + register int base, digit; + register char *bp; + + /* + * Find numeric capability cap + */ + bp = cgetcap(buf, cap, '#'); + if (bp == NULL) + return (-1); + + /* + * Look at value and determine numeric base: + * 0x... or 0X... hexadecimal, + * else 0... octal, + * else decimal. + */ + if (*bp == '0') { + bp++; + if (*bp == 'x' || *bp == 'X') { + bp++; + base = 16; + } else + base = 8; + } else + base = 10; + + /* + * Conversion loop ... + */ + n = 0; + for (;;) { + if ('0' <= *bp && *bp <= '9') + digit = *bp - '0'; + else if ('a' <= *bp && *bp <= 'f') + digit = 10 + *bp - 'a'; + else if ('A' <= *bp && *bp <= 'F') + digit = 10 + *bp - 'A'; + else + break; + + if (digit >= base) + break; + + n = n * base + digit; + bp++; + } + + /* + * Return value and success. + */ + *num = n; + return (0); +} + + +/* + * Compare name field of record. + */ +static int +nfcmp(nf, rec) + char *nf, *rec; +{ + char *cp, tmp; + int ret; + + for (cp = rec; *cp != ':'; cp++) + ; + + tmp = *(cp + 1); + *(cp + 1) = '\0'; + ret = strcmp(nf, rec); + *(cp + 1) = tmp; + + return (ret); +} diff --git a/lib/libc/gen/getcwd.3 b/lib/libc/gen/getcwd.3 new file mode 100644 index 0000000..357fdf3 --- /dev/null +++ b/lib/libc/gen/getcwd.3 @@ -0,0 +1,156 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getcwd.3 8.2 (Berkeley) 12/11/93 +.\" $FreeBSD$ +.\" +.Dd November 24, 1997 +.Dt GETCWD 3 +.Os BSD 4.2 +.Sh NAME +.Nm getcwd , +.Nm getwd +.Nd get working directory pathname +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft char * +.Fn getcwd "char *buf" "size_t size" +.Ft char * +.Fn getwd "char *buf" +.Sh DESCRIPTION +The +.Fn getcwd +function copies the absolute pathname of the current working directory +into the memory referenced by +.Fa buf +and returns a pointer to +.Fa buf . +The +.Fa size +argument is the size, in bytes, of the array referenced by +.Fa buf . +.Pp +If +.Fa buf +is +.Dv NULL , +space is allocated as necessary to store the pathname. +This space may later be +.Xr free 3 Ns 'd. +.Pp +The function +.Fn getwd +is a compatibility routine which calls +.Fn getcwd +with its +.Fa buf +argument and a size of +.Dv MAXPATHLEN +(as defined in the include +file +.Aq Pa sys/param.h ) . +Obviously, +.Fa buf +should be at least +.Dv MAXPATHLEN +bytes in length. +.Pp +These routines have traditionally been used by programs to save the +name of a working directory for the purpose of returning to it. +A much faster and less error-prone method of accomplishing this is to +open the current directory +.Pq Ql \&. +and use the +.Xr fchdir 2 +function to return. +.Sh RETURN VALUES +Upon successful completion, a pointer to the pathname is returned. +Otherwise a +.Dv NULL +pointer is returned and the global variable +.Va errno +is set to indicate the error. +In addition, +.Fn getwd +copies the error message associated with +.Va errno +into the memory referenced by +.Fa buf . +.Sh ERRORS +The +.Fn getcwd +function +will fail if: +.Bl -tag -width [EACCES] +.It Bq Er EACCES +Read or search permission was denied for a component of the pathname. +.It Bq Er EINVAL +The +.Fa size +argument is zero. +.It Bq Er ENOENT +A component of the pathname no longer exists. +.It Bq Er ENOMEM +Insufficient memory is available. +.It Bq Er ERANGE +The +.Fa size +argument is greater than zero but smaller than the length of the pathname +plus 1. +.El +.Sh SEE ALSO +.Xr chdir 2 , +.Xr fchdir 2 , +.Xr malloc 3 , +.Xr strerror 3 +.Sh STANDARDS +The +.Fn getcwd +function +conforms to +.St -ansiC . +The ability to specify a +.Dv NULL +pointer and have +.Fn getcwd +allocate memory as necessary is an extension. +.Sh HISTORY +The +.Fn getwd +function appeared in +.Bx 4.0 . +.Sh BUGS +The +.Fn getwd +function +does not do sufficient error checking and is not able to return very +long, but valid, paths. +It is provided for compatibility. diff --git a/lib/libc/gen/getcwd.c b/lib/libc/gen/getcwd.c new file mode 100644 index 0000000..9b1ccf3 --- /dev/null +++ b/lib/libc/gen/getcwd.c @@ -0,0 +1,288 @@ +/* + * Copyright (c) 1989, 1991, 1993, 1995 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <signal.h> + +#define ISDOT(dp) \ + (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \ + (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) + +static int have__getcwd = 1; /* 0 = no, 1 = perhaps, 2 = yes */ + +char * +getcwd(pt, size) + char *pt; + size_t size; +{ + register struct dirent *dp; + register DIR *dir = NULL; + register dev_t dev; + register ino_t ino; + register int first; + register char *bpt, *bup; + struct stat s; + dev_t root_dev; + ino_t root_ino; + size_t ptsize, upsize; + int save_errno; + char *ept, *eup, *up, c; + + /* + * If no buffer specified by the user, allocate one as necessary. + * If a buffer is specified, the size has to be non-zero. The path + * is built from the end of the buffer backwards. + */ + if (pt) { + ptsize = 0; + if (!size) { + errno = EINVAL; + return (NULL); + } + if (size == 1) { + errno = ERANGE; + return (NULL); + } + ept = pt + size; + } else { + if ((pt = malloc(ptsize = 1024 - 4)) == NULL) + return (NULL); + ept = pt + ptsize; + } +#if defined(__NETBSD_SYSCALLS) + have__getcwd = 0; +#else + if (have__getcwd) { + struct sigaction sa, osa; + int sigsys_installed = 0; + int ret; + + if (have__getcwd == 1) { /* unsure? */ + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = SIG_IGN; + if (sigaction(SIGSYS, &sa, &osa) >= 0) + sigsys_installed = 1; + } + ret = __getcwd(pt, ept - pt); + if (sigsys_installed == 1) { + int oerrno = errno; + sigaction(SIGSYS, &osa, NULL); + errno = oerrno; + } + /* XXX a bogus syscall seems to return EINVAL(!) */ + if (ret < 0 && (errno == ENOSYS || errno == EINVAL)) + have__getcwd = 0; + else if (have__getcwd == 1) + have__getcwd = 2; /* yep, remember we have it */ + if (ret == 0) { + if (*pt != '/') { + bpt = pt; + ept = pt + strlen(pt) - 1; + while (bpt < ept) { + c = *bpt; + *bpt++ = *ept; + *ept-- = c; + } + } + return (pt); + } + } +#endif + bpt = ept - 1; + *bpt = '\0'; + + /* + * Allocate bytes (1024 - malloc space) for the string of "../"'s. + * Should always be enough (it's 340 levels). If it's not, allocate + * as necessary. Special case the first stat, it's ".", not "..". + */ + if ((up = malloc(upsize = 1024 - 4)) == NULL) + goto err; + eup = up + MAXPATHLEN; + bup = up; + up[0] = '.'; + up[1] = '\0'; + + /* Save root values, so know when to stop. */ + if (stat("/", &s)) + goto err; + root_dev = s.st_dev; + root_ino = s.st_ino; + + errno = 0; /* XXX readdir has no error return. */ + + for (first = 1;; first = 0) { + /* Stat the current level. */ + if (lstat(up, &s)) + goto err; + + /* Save current node values. */ + ino = s.st_ino; + dev = s.st_dev; + + /* Check for reaching root. */ + if (root_dev == dev && root_ino == ino) { + *--bpt = '/'; + /* + * It's unclear that it's a requirement to copy the + * path to the beginning of the buffer, but it's always + * been that way and stuff would probably break. + */ + bcopy(bpt, pt, ept - bpt); + free(up); + return (pt); + } + + /* + * Build pointer to the parent directory, allocating memory + * as necessary. Max length is 3 for "../", the largest + * possible component name, plus a trailing NULL. + */ + if (bup + 3 + MAXNAMLEN + 1 >= eup) { + if ((up = reallocf(up, upsize *= 2)) == NULL) + goto err; + bup = up; + eup = up + upsize; + } + *bup++ = '.'; + *bup++ = '.'; + *bup = '\0'; + + /* Open and stat parent directory. */ + if (!(dir = opendir(up)) || fstat(dirfd(dir), &s)) + goto err; + + /* Add trailing slash for next directory. */ + *bup++ = '/'; + *bup = '\0'; + + /* + * If it's a mount point, have to stat each element because + * the inode number in the directory is for the entry in the + * parent directory, not the inode number of the mounted file. + */ + save_errno = 0; + if (s.st_dev == dev) { + for (;;) { + if (!(dp = readdir(dir))) + goto notfound; + if (dp->d_fileno == ino) + break; + } + } else + for (;;) { + if (!(dp = readdir(dir))) + goto notfound; + if (ISDOT(dp)) + continue; + bcopy(dp->d_name, bup, dp->d_namlen + 1); + + /* Save the first error for later. */ + if (lstat(up, &s)) { + if (!save_errno) + save_errno = errno; + errno = 0; + continue; + } + if (s.st_dev == dev && s.st_ino == ino) + break; + } + + /* + * Check for length of the current name, preceding slash, + * leading slash. + */ + if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) { + size_t len, off; + + if (!ptsize) { + errno = ERANGE; + goto err; + } + off = bpt - pt; + len = ept - bpt; + if ((pt = realloc(pt, ptsize *= 2)) == NULL) + goto err; + bpt = pt + off; + ept = pt + ptsize; + bcopy(bpt, ept - len, len); + bpt = ept - len; + } + if (!first) + *--bpt = '/'; + bpt -= dp->d_namlen; + bcopy(dp->d_name, bpt, dp->d_namlen); + (void) closedir(dir); + dir = NULL; + + /* Truncate any file name. */ + *bup = '\0'; + } + +notfound: + /* + * If readdir set errno, use it, not any saved error; otherwise, + * didn't find the current directory in its parent directory, set + * errno to ENOENT. + */ + if (!errno) + errno = save_errno ? save_errno : ENOENT; + /* FALLTHROUGH */ +err: + save_errno = errno; + + if (ptsize) + free(pt); + if (dir) + (void) closedir(dir); + free(up); + + errno = save_errno; + return (NULL); +} diff --git a/lib/libc/gen/getdiskbyname.3 b/lib/libc/gen/getdiskbyname.3 new file mode 100644 index 0000000..14d8824 --- /dev/null +++ b/lib/libc/gen/getdiskbyname.3 @@ -0,0 +1,65 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getdiskbyname.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt GETDISKBYNAME 3 +.Os BSD 4.2 +.Sh NAME +.Nm getdiskbyname +.Nd get generic disk description by its name +.Sh SYNOPSIS +.Fd #include <sys/disklabel.h> +.Ft struct disklabel * +.Fn getdiskbyname "const char *name" +.Sh DESCRIPTION +The +.Fn getdiskbyname +function +takes a disk name (e.g. +.Ql rm03 ) +and returns a prototype disk label +describing its geometry information and the standard +disk partition tables. All information is obtained from +the +.Xr disktab 5 +file. +.Sh SEE ALSO +.Xr disklabel 5 , +.Xr disktab 5 , +.Xr disklabel 8 +.Sh HISTORY +The +.Fn getdiskbyname +function appeared in +.Bx 4.3 . diff --git a/lib/libc/gen/getdomainname.3 b/lib/libc/gen/getdomainname.3 new file mode 100644 index 0000000..60a8099 --- /dev/null +++ b/lib/libc/gen/getdomainname.3 @@ -0,0 +1,99 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)gethostname.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd May 6, 1994 +.Dt GETDOMAINNAME 3 +.Os BSD 4.2 +.Sh NAME +.Nm getdomainname , +.Nm setdomainname +.Nd get/set domain name of current host +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft int +.Fn getdomainname "char *name" "int namelen" +.Ft int +.Fn setdomainname "const char *name" "int namelen" +.Sh DESCRIPTION +.Fn Getdomainname +returns the standard domain name for the current processor, as +previously set by +.Fn setdomainname . +The parameter +.Fa namelen +specifies the size of the +.Fa name +array. The returned name is null-terminated unless insufficient +space is provided. +.Pp +.Fn Setdomainname +sets the domain name of the host machine to be +.Fa name , +which has length +.Fa namelen . +This call is restricted to the super-user and +is normally used only when the system is bootstrapped. +.Sh RETURN VALUES +If the call succeeds a value of 0 is returned. If the call +fails, a value of -1 is returned and an error code is +placed in the global location +.Va errno . +.Sh ERRORS +The following errors may be returned by these calls: +.Bl -tag -width Er +.It Bq Er EFAULT +The +.Fa name +or +.Fa namelen +parameter gave an +invalid address. +.It Bq Er EPERM +The caller tried to set the hostname and was not the super-user. +.El +.Sh SEE ALSO +.Xr gethostid 3 , +.Xr gethostname 3 , +.Xr sysctl 3 +.Sh BUGS +Domain names are limited to +.Dv MAXHOSTNAMELEN +(from +.Ao Pa sys/param.h Ac ) +characters, currently 256. +.Sh HISTORY +The +.Fn getdomainname +function call appeared in +.Bx 4.2 . diff --git a/lib/libc/gen/getdomainname.c b/lib/libc/gen/getdomainname.c new file mode 100644 index 0000000..aafb010 --- /dev/null +++ b/lib/libc/gen/getdomainname.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +/* +static char sccsid[] = "From: @(#)gethostname.c 8.1 (Berkeley) 6/4/93"; +*/ +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> + +#include <unistd.h> + +int +getdomainname(name, namelen) + char *name; + int namelen; +{ + int mib[2]; + size_t size; + + mib[0] = CTL_KERN; + mib[1] = KERN_NISDOMAINNAME; + size = namelen; + if (sysctl(mib, 2, name, &size, NULL, 0) == -1) + return (-1); + return (0); +} diff --git a/lib/libc/gen/getfsent.3 b/lib/libc/gen/getfsent.3 new file mode 100644 index 0000000..98064fc --- /dev/null +++ b/lib/libc/gen/getfsent.3 @@ -0,0 +1,151 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getfsent.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt GETFSENT 3 +.Os BSD 4 +.Sh NAME +.Nm getfsent , +.Nm getfsspec , +.Nm getfsfile , +.Nm setfsent , +.Nm endfsent +.Nd get file system descriptor file entry +.Sh SYNOPSIS +.Fd #include <fstab.h> +.Ft struct fstab * +.Fn getfsent void +.Ft struct fstab * +.Fn getfsspec "const char *spec" +.Ft struct fstab * +.Fn getfsfile "const char *file" +.Ft int +.Fn setfsent void +.Ft void +.Fn endfsent void +.Sh DESCRIPTION +The +.Fn getfsent , +.Fn getfsspec , +and +.Fn getfsfile +functions +each return a pointer to an object with the following structure +containing the broken-out fields of a line in the file system +description file, +.Aq Pa fstab.h . +.Bd -literal -offset indent +struct fstab { + char *fs_spec; /* block special device name */ + char *fs_file; /* file system path prefix */ + char *fs_vfstype; /* File system type, ufs, nfs */ + char *fs_mntops; /* Mount options ala -o */ + char *fs_type; /* FSTAB_* from fs_mntops */ + int fs_freq; /* dump frequency, in days */ + int fs_passno; /* pass number on parallel fsck */ +}; +.Ed +.Pp +The fields have meanings described in +.Xr fstab 5 . +.Pp +The +.Fn setfsent +function +opens the file (closing any previously opened file) or rewinds it +if it is already open. +.Pp +The +.Fn endfsent +function +closes the file. +.Pp +The +.Fn getfsspec +and +.Fn getfsfile +functions +search the entire file (opening it if necessary) for a matching special +file name or file system file name. +.Pp +For programs wishing to read the entire database, +.Fn getfsent +reads the next entry (opening the file if necessary). +.Pp +All entries in the file with a type field equivalent to +.Dv FSTAB_XX +are ignored. +.Sh RETURN VALUES +The +.Fn getfsent , +.Fn getfsspec , +and +.Fn getfsfile +functions +return a +.Dv NULL +pointer on +.Dv EOF +or error. +The +.Fn setfsent +function +returns 0 on failure, 1 on success. +The +.Fn endfsent +function +returns nothing. +.Sh FILES +.Bl -tag -width /etc/fstab -compact +.It Pa /etc/fstab +.El +.Sh SEE ALSO +.Xr fstab 5 +.Sh HISTORY +The +.Fn getfsent +function appeared in +.Bx 4.0 ; +the +.Fn endfsent , +.Fn getfsfile , +.Fn getfsspec , +and +.Fn setfsent +functions appeared in +.Bx 4.3 . +.Sh BUGS +These functions use static data storage; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. diff --git a/lib/libc/gen/getgrent.3 b/lib/libc/gen/getgrent.3 new file mode 100644 index 0000000..bb3cfb5 --- /dev/null +++ b/lib/libc/gen/getgrent.3 @@ -0,0 +1,205 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" From: @(#)getgrent.3 8.2 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd September 29, 1994 +.Dt GETGRENT 3 +.Os +.Sh NAME +.Nm getgrent , +.Nm getgrnam , +.Nm getgrgid , +.Nm setgroupent , +.\" .Nm setgrfile , +.Nm setgrent , +.Nm endgrent +.Nd group database operations +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <grp.h> +.Ft struct group * +.Fn getgrent void +.Ft struct group * +.Fn getgrnam "const char *name" +.Ft struct group * +.Fn getgrgid "gid_t gid" +.Ft int +.Fn setgroupent "int stayopen" +.\" .Ft void +.\" .Fn setgrfile "const char *name" +.Ft int +.Fn setgrent void +.Ft void +.Fn endgrent void +.Sh DESCRIPTION +These functions operate on the group database file +.Pa /etc/group +which is described +in +.Xr group 5 . +Each line of the database is defined by the structure +.Ar group +found in the include +file +.Aq Pa grp.h : +.Bd -literal -offset indent +struct group { + char *gr_name; /* group name */ + char *gr_passwd; /* group password */ + int gr_gid; /* group id */ + char **gr_mem; /* group members */ +}; +.Ed +.Pp +The functions +.Fn getgrnam +and +.Fn getgrgid +search the group database for the given group name pointed to by +.Ar name +or the group id pointed to by +.Ar gid , +respectively, returning the first one encountered. Identical group +names or group gids may result in undefined behavior. +.Pp +The +.Fn getgrent +function +sequentially reads the group database and is intended for programs +that wish to step through the complete list of groups. +.Pp +All three routines will open the group file for reading, if necessary. +.Pp +The +.Fn setgroupent +function +opens the file, or rewinds it if it is already open. If +.Fa stayopen +is non-zero, file descriptors are left open, significantly speeding +functions subsequent calls. This functionality is unnecessary for +.Fn getgrent +as it doesn't close its file descriptors by default. It should also +be noted that it is dangerous for long-running programs to use this +functionality as the group file may be updated. +.Pp +The +.Fn setgrent +function +is identical to +.Fn setgroupent +with an argument of zero. +.Pp +The +.Fn endgrent +function +closes any open files. +.Sh YP/NIS INTERACTION +When the +.Xr yp 4 +group database is enabled, the +.Fn getgrnam +and +.Fn getgrgid +functions use the YP maps +.Dq Li group.byname +and +.Dq Li group.bygid , +respectively, if the requested group is not found in the local +.Pa /etc/group +file. The +.Fn getgrent +function will step through the YP map +.Dq Li group.byname +if the entire map is enabled as described in +.Xr group 5 . +.Sh RETURN VALUES +The functions +.Fn getgrent , +.Fn getgrnam , +and +.Fn getgrgid , +return a pointer to the group entry if successful; if end-of-file +is reached or an error occurs a null pointer is returned. +The functions +.Fn setgroupent +and +.Fn setgrent +return the value 1 if successful, otherwise the value +0 is returned. +The functions +.Fn endgrent +and +.Fn setgrfile +have no return value. +.Sh FILES +.Bl -tag -width /etc/group -compact +.It Pa /etc/group +group database file +.El +.Sh SEE ALSO +.Xr getpwent 3 , +.Xr yp 4 , +.Xr group 5 +.Sh HISTORY +The functions +.Fn endgrent , +.Fn getgrent , +.Fn getgrnam , +.Fn getgrgid , +and +.Fn setgrent +appeared in +.At v7 . +The functions +.Fn setgrfile +and +.Fn setgroupent +appeared in +.Bx 4.3 Reno . +.Sh COMPATIBILITY +The historic function +.Fn setgrfile , +which allowed the specification of alternate password databases, has +been deprecated and is no longer available. +.Sh BUGS +The functions +.Fn getgrent , +.Fn getgrnam , +.Fn getgrgid , +.Fn setgroupent +and +.Fn setgrent +leave their results in an internal static object and return +a pointer to that object. Subsequent calls to +the same function +will modify the same object. diff --git a/lib/libc/gen/getgrent.c b/lib/libc/gen/getgrent.c new file mode 100644 index 0000000..5b5c9da --- /dev/null +++ b/lib/libc/gen/getgrent.c @@ -0,0 +1,553 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <grp.h> + +static FILE *_gr_fp; +static struct group _gr_group; +static int _gr_stayopen; +static int grscan(), start_gr(); +#ifdef YP +#include <rpc/rpc.h> +#include <rpcsvc/yp_prot.h> +#include <rpcsvc/ypclnt.h> +static int _gr_stepping_yp; +static int _gr_yp_enabled; +static int _getypgroup(struct group *, const char *, char *); +static int _nextypgroup(struct group *); +#endif + +/* initial size for malloc and increase steps for realloc */ +#define MAXGRP 64 +#define MAXLINELENGTH 256 + +static char **members; /* list of group members */ +static int maxgrp; /* current length of **mebers */ +static char *line; /* temp buffer for group line */ +static int maxlinelength; /* current length of *line */ + +/* + * Lines longer than MAXLINELENGTHLIMIT will be count as an error. + * <= 0 disable check for maximum line length + * 256K is enough for 64,000 uids + */ +#define MAXLINELENGTHLIMIT (256 * 1024) +#define GROUP_IGNORE_COMMENTS 1 /* allow comments in /etc/group */ + +struct group * +getgrent() +{ + if (!_gr_fp && !start_gr()) { + return NULL; + } + +#ifdef YP + if (_gr_stepping_yp) { + if (_nextypgroup(&_gr_group)) + return(&_gr_group); + } +tryagain: +#endif + + if (!grscan(0, 0, NULL)) + return(NULL); +#ifdef YP + if(_gr_group.gr_name[0] == '+' && _gr_group.gr_name[1]) { + _getypgroup(&_gr_group, &_gr_group.gr_name[1], + "group.byname"); + } else if(_gr_group.gr_name[0] == '+') { + if (!_nextypgroup(&_gr_group)) + goto tryagain; + else + return(&_gr_group); + } +#endif + return(&_gr_group); +} + +struct group * +getgrnam(name) + const char *name; +{ + int rval; + + if (!start_gr()) + return(NULL); +#ifdef YP + tryagain: +#endif + rval = grscan(1, 0, name); +#ifdef YP + if(rval == -1 && (_gr_yp_enabled < 0 || (_gr_yp_enabled && + _gr_group.gr_name[0] == '+'))) { + if (!(rval = _getypgroup(&_gr_group, name, "group.byname"))) + goto tryagain; + } +#endif + if (!_gr_stayopen) + endgrent(); + return(rval ? &_gr_group : NULL); +} + +struct group * +#ifdef __STDC__ +getgrgid(gid_t gid) +#else +getgrgid(gid) + gid_t gid; +#endif +{ + int rval; + + if (!start_gr()) + return(NULL); +#ifdef YP + tryagain: +#endif + rval = grscan(1, gid, NULL); +#ifdef YP + if(rval == -1 && _gr_yp_enabled) { + char buf[16]; + snprintf(buf, sizeof buf, "%d", (unsigned)gid); + if (!(rval = _getypgroup(&_gr_group, buf, "group.bygid"))) + goto tryagain; + } +#endif + if (!_gr_stayopen) + endgrent(); + return(rval ? &_gr_group : NULL); +} + +static int +start_gr() +{ + if (_gr_fp) { + rewind(_gr_fp); + return(1); + } + _gr_fp = fopen(_PATH_GROUP, "r"); + if(!_gr_fp) return 0; +#ifdef YP + /* + * This is a disgusting hack, used to determine when YP is enabled. + * This would be easier if we had a group database to go along with + * the password database. + */ + { + char *line; + size_t linelen; + _gr_yp_enabled = 0; + while((line = fgetln(_gr_fp, &linelen)) != NULL) { + if(line[0] == '+') { + if(line[1] && line[1] != ':' && !_gr_yp_enabled) { + _gr_yp_enabled = 1; + } else { + _gr_yp_enabled = -1; + break; + } + } + } + rewind(_gr_fp); + } +#endif + + if (maxlinelength == 0) { + if ((line = (char *)malloc(sizeof(char) * + MAXLINELENGTH)) == NULL) + return(0); + maxlinelength += MAXLINELENGTH; + } + + if (maxgrp == 0) { + if ((members = (char **)malloc(sizeof(char **) * + MAXGRP)) == NULL) + return(0); + maxgrp += MAXGRP; + } + + return 1; +} + +int +setgrent() +{ + return(setgroupent(0)); +} + +int +setgroupent(stayopen) + int stayopen; +{ + if (!start_gr()) + return(0); + _gr_stayopen = stayopen; +#ifdef YP + _gr_stepping_yp = 0; +#endif + return(1); +} + +void +endgrent() +{ +#ifdef YP + _gr_stepping_yp = 0; +#endif + if (_gr_fp) { + (void)fclose(_gr_fp); + _gr_fp = NULL; + } +} + +static int +grscan(search, gid, name) + register int search, gid; + register char *name; +{ + register char *cp, **m; + char *bp; + + +#ifdef YP + int _ypfound; +#endif + for (;;) { +#ifdef YP + _ypfound = 0; +#endif + if (fgets(line, maxlinelength, _gr_fp) == NULL) + return(0); + + if (!index(line, '\n')) { + do { + if (feof(_gr_fp)) + return(0); + + /* don't allocate infinite memory */ + if (MAXLINELENGTHLIMIT > 0 && + maxlinelength >= MAXLINELENGTHLIMIT) + return(0); + + if ((line = (char *)reallocf(line, + sizeof(char) * + (maxlinelength + MAXLINELENGTH))) == NULL) + return(0); + + if (fgets(line + maxlinelength - 1, + MAXLINELENGTH + 1, _gr_fp) == NULL) + return(0); + + maxlinelength += MAXLINELENGTH; + } while (!index(line + maxlinelength - + MAXLINELENGTH - 1, '\n')); + } + +#ifdef GROUP_IGNORE_COMMENTS + /* + * Ignore comments: ^[ \t]*# + */ + for (cp = line; *cp != '\0'; cp++) + if (*cp != ' ' && *cp != '\t') + break; + if (*cp == '#' || *cp == '\0') + continue; +#endif + + bp = line; + + if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL) + break; +#ifdef YP + /* + * XXX We need to be careful to avoid proceeding + * past this point under certain circumstances or + * we risk dereferencing null pointers down below. + */ + if (_gr_group.gr_name[0] == '+') { + if (strlen(_gr_group.gr_name) == 1) { + switch(search) { + case 0: + return(1); + case 1: + return(-1); + default: + return(0); + } + } else { + cp = &_gr_group.gr_name[1]; + if (search && name != NULL) + if (strcmp(cp, name)) + continue; + if (!_getypgroup(&_gr_group, cp, + "group.byname")) + continue; + if (search && name == NULL) + if (gid != _gr_group.gr_gid) + continue; + /* We're going to override -- tell the world. */ + _ypfound++; + } + } +#else + if (_gr_group.gr_name[0] == '+') + continue; +#endif /* YP */ + if (search && name) { + if(strcmp(_gr_group.gr_name, name)) { + continue; + } + } +#ifdef YP + if ((cp = strsep(&bp, ":\n")) == NULL) + if (_ypfound) + return(1); + else + break; + if (strlen(cp) || !_ypfound) + _gr_group.gr_passwd = cp; +#else + if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL) + break; +#endif + if (!(cp = strsep(&bp, ":\n"))) +#ifdef YP + if (_ypfound) + return(1); + else +#endif + continue; +#ifdef YP + /* + * Hurm. Should we be doing this? We allow UIDs to + * be overridden -- what about GIDs? + */ + if (!_ypfound) +#endif + _gr_group.gr_gid = atoi(cp); + if (search && name == NULL && _gr_group.gr_gid != gid) + continue; + cp = NULL; + if (bp == NULL) /* !!! Must check for this! */ + break; +#ifdef YP + if ((cp = strsep(&bp, ":\n")) == NULL) + break; + + if (!strlen(cp) && _ypfound) + return(1); + else + members[0] = NULL; + bp = cp; + cp = NULL; +#endif + for (m = members; ; bp++) { + if (m == (members + maxgrp - 1)) { + if ((members = (char **) + reallocf(members, + sizeof(char **) * + (maxgrp + MAXGRP))) == NULL) + return(0); + m = members + maxgrp - 1; + maxgrp += MAXGRP; + } + if (*bp == ',') { + if (cp) { + *bp = '\0'; + *m++ = cp; + cp = NULL; + } + } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') { + if (cp) { + *bp = '\0'; + *m++ = cp; + } + break; + } else if (cp == NULL) + cp = bp; + + } + _gr_group.gr_mem = members; + *m = NULL; + return(1); + } + /* NOTREACHED */ + return (0); +} + +#ifdef YP + +static int +_gr_breakout_yp(struct group *gr, char *result) +{ + char *s, *cp; + char **m; + + /* + * XXX If 's' ends up being a NULL pointer, punt on this group. + * It means the NIS group entry is badly formatted and should + * be skipped. + */ + if ((s = strsep(&result, ":")) == NULL) return 0; /* name */ + gr->gr_name = s; + + if ((s = strsep(&result, ":")) == NULL) return 0; /* password */ + gr->gr_passwd = s; + + if ((s = strsep(&result, ":")) == NULL) return 0; /* gid */ + gr->gr_gid = atoi(s); + + if ((s = result) == NULL) return 0; + cp = 0; + + for (m = members; ; s++) { + if (m == members + maxgrp - 1) { + if ((members = (char **)reallocf(members, + sizeof(char **) * (maxgrp + MAXGRP))) == NULL) + return(0); + m = members + maxgrp - 1; + maxgrp += MAXGRP; + } + if (*s == ',') { + if (cp) { + *s = '\0'; + *m++ = cp; + cp = NULL; + } + } else if (*s == '\0' || *s == '\n' || *s == ' ') { + if (cp) { + *s = '\0'; + *m++ = cp; + } + break; + } else if (cp == NULL) { + cp = s; + } + } + _gr_group.gr_mem = members; + *m = NULL; + + return 1; +} + +static char *_gr_yp_domain; + +static int +_getypgroup(struct group *gr, const char *name, char *map) +{ + char *result, *s; + static char resultbuf[YPMAXRECORD + 2]; + int resultlen; + + if(!_gr_yp_domain) { + if(yp_get_default_domain(&_gr_yp_domain)) + return 0; + } + + if(yp_match(_gr_yp_domain, map, name, strlen(name), + &result, &resultlen)) + return 0; + + s = strchr(result, '\n'); + if(s) *s = '\0'; + + if(resultlen >= sizeof resultbuf) return 0; + strncpy(resultbuf, result, resultlen); + resultbuf[resultlen] = '\0'; + free(result); + return(_gr_breakout_yp(gr, resultbuf)); + +} + + +static int +_nextypgroup(struct group *gr) +{ + static char *key; + static int keylen; + char *lastkey, *result; + static char resultbuf[YPMAXRECORD + 2]; + int resultlen; + int rv; + + if(!_gr_yp_domain) { + if(yp_get_default_domain(&_gr_yp_domain)) + return 0; + } + + if(!_gr_stepping_yp) { + if(key) free(key); + rv = yp_first(_gr_yp_domain, "group.byname", + &key, &keylen, &result, &resultlen); + if(rv) { + return 0; + } + _gr_stepping_yp = 1; + goto unpack; + } else { +tryagain: + lastkey = key; + rv = yp_next(_gr_yp_domain, "group.byname", key, keylen, + &key, &keylen, &result, &resultlen); + free(lastkey); +unpack: + if(rv) { + _gr_stepping_yp = 0; + return 0; + } + + if(resultlen > sizeof(resultbuf)) { + free(result); + goto tryagain; + } + + strncpy(resultbuf, result, resultlen); + resultbuf[resultlen] = '\0'; + free(result); + if((result = strchr(resultbuf, '\n')) != NULL) + *result = '\0'; + if (_gr_breakout_yp(gr, resultbuf)) + return(1); + else + goto tryagain; + } +} + +#endif /* YP */ diff --git a/lib/libc/gen/getgrouplist.3 b/lib/libc/gen/getgrouplist.3 new file mode 100644 index 0000000..e85eaa5 --- /dev/null +++ b/lib/libc/gen/getgrouplist.3 @@ -0,0 +1,94 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getgrouplist.3 8.1 (Berkeley) 6/9/93 +.\" $FreeBSD$ +.\" +.Dd June 9, 1993 +.Dt GETGROUPLIST 3 +.Os +.Sh NAME +.Nm getgrouplist +.Nd calculate group access list +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft int +.Fn getgrouplist "const char *name" "int basegid" "int *groups" "int *ngroups" +.Sh DESCRIPTION +The +.Fn getgrouplist +function reads through the group file and calculates +the group access list for the user specified in +.Fa name . +The +.Fa basegid +is automatically included in the groups list. +Typically this value is given as +the group number from the password file. +.Pp +The resulting group list is returned in the integer array pointed to by +.Fa groups . +The caller specifies the size of the +.Fa groups +array in the integer pointed to by +.Fa ngroups ; +the actual number of groups found is returned in +.Fa ngroups . +.Sh RETURN VALUES +The +.Fn getgrouplist +function +returns \-1 if the size of the group list is too small to +hold all the user's groups. +Here, the group array will be filled with as many groups as will fit. +.Sh FILES +.Bl -tag -width /etc/group -compact +.It Pa /etc/group +group membership list +.El +.Sh SEE ALSO +.Xr setgroups 2 , +.Xr initgroups 3 +.Sh HISTORY +The +.Fn getgrouplist +function first appeared in +.Bx 4.4 . +.Sh BUGS +The +.Fn getgrouplist +function +uses the routines based on +.Xr getgrent 3 . +If the invoking program uses any of these routines, +the group structure will +be overwritten in the call to +.Fn getgrouplist . diff --git a/lib/libc/gen/getgrouplist.c b/lib/libc/gen/getgrouplist.c new file mode 100644 index 0000000..31f90b4 --- /dev/null +++ b/lib/libc/gen/getgrouplist.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getgrouplist.c 8.2 (Berkeley) 12/8/94"; +#endif /* LIBC_SCCS and not lint */ + +/* + * get credential + */ +#include <sys/types.h> +#include <string.h> +#include <grp.h> + +int +getgrouplist(uname, agroup, groups, grpcnt) + const char *uname; + int agroup; + register int *groups; + int *grpcnt; +{ + register struct group *grp; + register int i, ngroups; + int ret, maxgroups; + + ret = 0; + ngroups = 0; + maxgroups = *grpcnt; + /* + * When installing primary group, duplicate it; + * the first element of groups is the effective gid + * and will be overwritten when a setgid file is executed. + */ + groups[ngroups++] = agroup; + if (maxgroups > 1) + groups[ngroups++] = agroup; + /* + * Scan the group file to find additional groups. + */ + setgrent(); + while (grp = getgrent()) { + for (i = 0; i < ngroups; i++) { + if (grp->gr_gid == groups[i]) + goto skip; + } + for (i = 0; grp->gr_mem[i]; i++) { + if (!strcmp(grp->gr_mem[i], uname)) { + if (ngroups >= maxgroups) { + ret = -1; + break; + } + groups[ngroups++] = grp->gr_gid; + break; + } + } +skip: + } + endgrent(); + *grpcnt = ngroups; + return (ret); +} diff --git a/lib/libc/gen/gethostname.3 b/lib/libc/gen/gethostname.3 new file mode 100644 index 0000000..f4d2d04 --- /dev/null +++ b/lib/libc/gen/gethostname.3 @@ -0,0 +1,98 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)gethostname.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt GETHOSTNAME 3 +.Os BSD 4.2 +.Sh NAME +.Nm gethostname , +.Nm sethostname +.Nd get/set name of current host +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft int +.Fn gethostname "char *name" "int namelen" +.Ft int +.Fn sethostname "const char *name" "int namelen" +.Sh DESCRIPTION +.Fn Gethostname +returns the standard host name for the current processor, as +previously set by +.Fn sethostname . +The parameter +.Fa namelen +specifies the size of the +.Fa name +array. The returned name is null-terminated unless insufficient +space is provided. +.Pp +.Fn Sethostname +sets the name of the host machine to be +.Fa name , +which has length +.Fa namelen . +This call is restricted to the super-user and +is normally used only when the system is bootstrapped. +.Sh RETURN VALUES +If the call succeeds a value of 0 is returned. If the call +fails, a value of -1 is returned and an error code is +placed in the global location +.Va errno . +.Sh ERRORS +The following errors may be returned by these calls: +.Bl -tag -width Er +.It Bq Er EFAULT +The +.Fa name +or +.Fa namelen +parameter gave an +invalid address. +.It Bq Er EPERM +The caller tried to set the hostname and was not the super-user. +.El +.Sh SEE ALSO +.Xr gethostid 3 , +.Xr sysctl 3 +.Sh BUGS +Host names are limited to +.Dv MAXHOSTNAMELEN +(from +.Ao Pa sys/param.h Ac ) +characters, currently 256. +.Sh HISTORY +The +.Fn gethostname +function call appeared in +.Bx 4.2 . diff --git a/lib/libc/gen/gethostname.c b/lib/libc/gen/gethostname.c new file mode 100644 index 0000000..64bd64d --- /dev/null +++ b/lib/libc/gen/gethostname.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)gethostname.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> + +int +gethostname(name, namelen) + char *name; + int namelen; +{ + int mib[2]; + size_t size; + + mib[0] = CTL_KERN; + mib[1] = KERN_HOSTNAME; + size = namelen; + if (sysctl(mib, 2, name, &size, NULL, 0) == -1) + return (-1); + return (0); +} diff --git a/lib/libc/gen/getloadavg.3 b/lib/libc/gen/getloadavg.3 new file mode 100644 index 0000000..737f699 --- /dev/null +++ b/lib/libc/gen/getloadavg.3 @@ -0,0 +1,67 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getloadavg.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt GETLOADAVG 3 +.Os +.Sh NAME +.Nm getloadavg +.Nd get system load averages +.Sh SYNOPSIS +.Fd #include <stdlib.h> +.Ft int +.Fn getloadavg "double loadavg[]" "int nelem" +.Sh DESCRIPTION +The +.Fn getloadavg +function returns the number of processes in the system run queue +averaged over various periods of time. +Up to +.Fa nelem +samples are retrieved and assigned to successive elements of +.Fa loadavg Ns Bq . +The system imposes a maximum of 3 samples, representing averages +over the last 1, 5, and 15 minutes, respectively. +.Sh DIAGNOSTICS +If the load average was unobtainable, \-1 is returned; otherwise, +the number of samples actually retrieved is returned. +.Sh SEE ALSO +.Xr uptime 1 , +.Xr kvm_getloadavg 3 , +.Xr sysctl 3 +.Sh HISTORY +The +.Fn getloadavg +function appeared in +.Bx 4.3 Reno . diff --git a/lib/libc/gen/getloadavg.c b/lib/libc/gen/getloadavg.c new file mode 100644 index 0000000..811dbda --- /dev/null +++ b/lib/libc/gen/getloadavg.c @@ -0,0 +1,71 @@ +/*- + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getloadavg.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <sys/sysctl.h> +#include <vm/vm_param.h> + +#include <stdlib.h> + +/* + * getloadavg() -- Get system load averages. + * + * Put `nelem' samples into `loadavg' array. + * Return number of samples retrieved, or -1 on error. + */ +int +getloadavg(loadavg, nelem) + double loadavg[]; + int nelem; +{ + struct loadavg loadinfo; + int i, mib[2]; + size_t size; + + mib[0] = CTL_VM; + mib[1] = VM_LOADAVG; + size = sizeof(loadinfo); + if (sysctl(mib, 2, &loadinfo, &size, NULL, 0) < 0) + return (-1); + + nelem = MIN(nelem, sizeof(loadinfo.ldavg) / sizeof(fixpt_t)); + for (i = 0; i < nelem; i++) + loadavg[i] = (double) loadinfo.ldavg[i] / loadinfo.fscale; + return (nelem); +} diff --git a/lib/libc/gen/getlogin.c b/lib/libc/gen/getlogin.c new file mode 100644 index 0000000..044689d --- /dev/null +++ b/lib/libc/gen/getlogin.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getlogin.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <pwd.h> +#include <utmp.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int _logname_valid; /* known to setlogin() */ + +char * +getlogin() +{ + static char logname[MAXLOGNAME]; + + if (_logname_valid == 0) { +#ifdef __NETBSD_SYSCALLS + if (__getlogin(logname, sizeof(logname) - 1) < 0) +#else + if (_getlogin(logname, sizeof(logname)) < 0) +#endif + return ((char *)NULL); + _logname_valid = 1; + } + return (*logname ? logname : (char *)NULL); +} + + +char * +getlogin_r(char *logname, int namelen) +{ + if (_logname_valid == 0) { +#ifdef __NETBSD_SYSCALLS + if (__getlogin(logname, namelen - 1) < 0) +#else + if (_getlogin(logname, namelen) < 0) +#endif + return ((char *)NULL); + _logname_valid = 1; + } + return (*logname ? logname : (char *)NULL); +} diff --git a/lib/libc/gen/getmntinfo.3 b/lib/libc/gen/getmntinfo.3 new file mode 100644 index 0000000..d20fc5b --- /dev/null +++ b/lib/libc/gen/getmntinfo.3 @@ -0,0 +1,110 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getmntinfo.3 8.1 (Berkeley) 6/9/93 +.\" $FreeBSD$ +.\" +.Dd June 9, 1993 +.Dt GETMNTINFO 3 +.Os +.Sh NAME +.Nm getmntinfo +.Nd get information about mounted file systems +.Sh SYNOPSIS +.Fd #include <sys/param.h> +.Fd #include <sys/ucred.h> +.Fd #include <sys/mount.h> +.Ft int +.Fn getmntinfo "struct statfs **mntbufp" "int flags" +.Sh DESCRIPTION +The +.Fn getmntinfo +function +returns an array of +.Fn statfs +structures describing each currently mounted file system (see +.Xr statfs 2 ) . +.Pp +The +.Fn getmntinfo +function +passes its +.Fa flags +parameter transparently to +.Xr getfsstat 2 . +.Sh RETURN VALUES +On successful completion, +.Fn getmntinfo +returns a count of the number of elements in the array. +The pointer to the array is stored into +.Fa mntbufp . +.Pp +If an error occurs, zero is returned and the external variable +.Va errno +is set to indicate the error. +Although the pointer +.Fa mntbufp +will be unmodified, any information previously returned by +.Fn getmntinfo +will be lost. +.Sh ERRORS +The +.Fn getmntinfo +function +may fail and set errno for any of the errors specified for the library +routines +.Xr getfsstat 2 +or +.Xr malloc 3 . +.Sh SEE ALSO +.Xr getfsstat 2 , +.Xr mount 2 , +.Xr statfs 2 , +.Xr mount 8 +.Sh HISTORY +The +.Fn getmntinfo +function first appeared in +.Bx 4.4 . +.Sh BUGS +The +.Fn getmntinfo +function writes the array of structures to an internal static object +and returns +a pointer to that object. Subsequent calls to +.Fn getmntinfo +will modify the same object. +.Pp +The memory allocated by +.Fn getmntinfo +cannot be +.Xr free 3 Ns 'd +by the application. diff --git a/lib/libc/gen/getmntinfo.c b/lib/libc/gen/getmntinfo.c new file mode 100644 index 0000000..6e167a3 --- /dev/null +++ b/lib/libc/gen/getmntinfo.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getmntinfo.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/ucred.h> +#include <sys/mount.h> +#include <stdlib.h> + +/* + * Return information about mounted filesystems. + */ +int +getmntinfo(mntbufp, flags) + struct statfs **mntbufp; + int flags; +{ + static struct statfs *mntbuf; + static int mntsize; + static long bufsize; + + if (mntsize <= 0 && (mntsize = getfsstat(0, 0, MNT_NOWAIT)) < 0) + return (0); + if (bufsize > 0 && (mntsize = getfsstat(mntbuf, bufsize, flags)) < 0) + return (0); + while (bufsize <= mntsize * sizeof(struct statfs)) { + if (mntbuf) + free(mntbuf); + bufsize = (mntsize + 1) * sizeof(struct statfs); + if ((mntbuf = (struct statfs *)malloc(bufsize)) == 0) + return (0); + if ((mntsize = getfsstat(mntbuf, bufsize, flags)) < 0) + return (0); + } + *mntbufp = mntbuf; + return (mntsize); +} diff --git a/lib/libc/gen/getnetgrent.3 b/lib/libc/gen/getnetgrent.3 new file mode 100644 index 0000000..db8f550 --- /dev/null +++ b/lib/libc/gen/getnetgrent.3 @@ -0,0 +1,129 @@ +.\" Copyright (c) 1992, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getnetgrent.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt GETNETGRENT 3 +.Os +.Sh NAME +.Nm getnetgrent , +.Nm innetgr , +.Nm setnetgrent , +.Nm endnetgrent +.Nd netgroup database operations +.Sh SYNOPSIS +.Ft int +.Fn getnetgrent "char **host" "char **user" "char **domain" +.Ft int +.Fn innetgr "const char *netgroup" "const char *host" "const char *user" "const char *domain" +.Ft void +.Fn setnetgrent "const char *netgroup" +.Ft void +.Fn endnetgrent void +.Sh DESCRIPTION +These functions operate on the netgroup database file +.Pa /etc/netgroup +which is described +in +.Xr netgroup 5 . +The database defines a set of netgroups, each made up of one or more triples: +.Bd -literal -offset indent +(host, user, domain) +.Ed +that defines a combination of host, user and domain. +Any of the three fields may be specified as ``wildcards'' that match any +string. +.Pp +The function +.Fn getnetgrent +sets the three pointer arguments to the strings of the next member of the +current netgroup. +If any of the string pointers are +.Sy (char *)0 +that field is considered a wildcard. +.Pp +The functions +.Fn setnetgrent +and +.Fn endnetgrent +set the current netgroup and terminate the current netgroup respectively. +If +.Fn setnetgrent +is called with a different netgroup than the previous call, an implicit +.Fn endnetgrent +is implied. +.Fn Setnetgrent +also sets the offset to the first member of the netgroup. +.Pp +The function +.Fn innetgr +searches for a match of all fields within the specified group. +If any of the +.Sy host , +.Sy user , +or +.Sy domain +arguments are +.Sy (char *)0 +those fields will match any string value in the netgroup member. +.Sh RETURN VALUES +The function +.Fn getnetgrent +returns 0 for ``no more netgroup members'' and 1 otherwise. +The function +.Fn innetgr +returns 1 for a successful match and 0 otherwise. +The functions +.Fn setnetgrent +and +.Fn endnetgrent +have no return value. +.Sh FILES +.Bl -tag -width /etc/netgroup -compact +.It Pa /etc/netgroup +netgroup database file +.El +.Sh SEE ALSO +.Xr netgroup 5 +.Sh COMPATIBILITY +The netgroup members have three string fields to maintain compatibility +with other vendor implementations, however it is not obvious what use the +.Sy domain +string has within BSD. +.Sh BUGS +The function +.Fn getnetgrent +returns pointers to dynamically allocated data areas that are freed when +the function +.Fn endnetgrent +is called. diff --git a/lib/libc/gen/getnetgrent.c b/lib/libc/gen/getnetgrent.c new file mode 100644 index 0000000..a26c86c --- /dev/null +++ b/lib/libc/gen/getnetgrent.c @@ -0,0 +1,634 @@ +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Rick Macklem at The University of Guelph. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getnetgrent.c 8.2 (Berkeley) 4/27/95"; +#endif /* LIBC_SCCS and not lint */ + +#include <stdio.h> +#include <strings.h> +#include <stdlib.h> +#include <unistd.h> + +#ifdef YP +/* + * Notes: + * We want to be able to use NIS netgroups properly while retaining + * the ability to use a local /etc/netgroup file. Unfortunately, you + * can't really do both at the same time - at least, not efficiently. + * NetBSD deals with this problem by creating a netgroup database + * using Berkeley DB (just like the password database) that allows + * for lookups using netgroup, netgroup.byuser or netgroup.byhost + * searches. This is a neat idea, but I don't have time to implement + * something like that now. (I think ultimately it would be nice + * if we DB-fied the group and netgroup stuff all in one shot, but + * for now I'm satisfied just to have something that works well + * without requiring massive code changes.) + * + * Therefore, to still permit the use of the local file and maintain + * optimum NIS performance, we allow for the following conditions: + * + * - If /etc/netgroup does not exist and NIS is turned on, we use + * NIS netgroups only. + * + * - If /etc/netgroup exists but is empty, we use NIS netgroups + * only. + * + * - If /etc/netgroup exists and contains _only_ a '+', we use + * NIS netgroups only. + * + * - If /etc/netgroup exists, contains locally defined netgroups + * and a '+', we use a mixture of NIS and the local entries. + * This method should return the same NIS data as just using + * NIS alone, but it will be slower if the NIS netgroup database + * is large (innetgr() in particular will suffer since extra + * processing has to be done in order to determine memberships + * using just the raw netgroup data). + * + * - If /etc/netgroup exists and contains only locally defined + * netgroup entries, we use just those local entries and ignore + * NIS (this is the original, pre-NIS behavior). + */ + +#include <rpc/rpc.h> +#include <rpcsvc/yp_prot.h> +#include <rpcsvc/ypclnt.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/param.h> +#include <sys/errno.h> +static char *_netgr_yp_domain; +int _use_only_yp; +static int _netgr_yp_enabled; +static int _yp_innetgr; +#endif + +#ifndef _PATH_NETGROUP +#define _PATH_NETGROUP "/etc/netgroup" +#endif + +/* + * Static Variables and functions used by setnetgrent(), getnetgrent() and + * endnetgrent(). + * There are two linked lists: + * - linelist is just used by setnetgrent() to parse the net group file via. + * parse_netgrp() + * - netgrp is the list of entries for the current netgroup + */ +struct linelist { + struct linelist *l_next; /* Chain ptr. */ + int l_parsed; /* Flag for cycles */ + char *l_groupname; /* Name of netgroup */ + char *l_line; /* Netgroup entrie(s) to be parsed */ +}; + +struct netgrp { + struct netgrp *ng_next; /* Chain ptr */ + char *ng_str[3]; /* Field pointers, see below */ +}; +#define NG_HOST 0 /* Host name */ +#define NG_USER 1 /* User name */ +#define NG_DOM 2 /* and Domain name */ + +static struct linelist *linehead = (struct linelist *)0; +static struct netgrp *nextgrp = (struct netgrp *)0; +static struct { + struct netgrp *gr; + char *grname; +} grouphead = { + (struct netgrp *)0, + (char *)0, +}; +static FILE *netf = (FILE *)0; +static int parse_netgrp(); +static struct linelist *read_for_group(); +void setnetgrent(), endnetgrent(); +int getnetgrent(), innetgr(); + +#define LINSIZ 1024 /* Length of netgroup file line */ + +/* + * setnetgrent() + * Parse the netgroup file looking for the netgroup and build the list + * of netgrp structures. Let parse_netgrp() and read_for_group() do + * most of the work. + */ +void +setnetgrent(group) + char *group; +{ +#ifdef YP + struct stat _yp_statp; + char _yp_plus; +#endif + + /* Sanity check */ + + if (group == NULL || !strlen(group)) + return; + + if (grouphead.gr == (struct netgrp *)0 || + strcmp(group, grouphead.grname)) { + endnetgrent(); +#ifdef YP + /* Presumed guilty until proven innocent. */ + _use_only_yp = 0; + /* + * If /etc/netgroup doesn't exist or is empty, + * use NIS exclusively. + */ + if (((stat(_PATH_NETGROUP, &_yp_statp) < 0) && + errno == ENOENT) || _yp_statp.st_size == 0) + _use_only_yp = _netgr_yp_enabled = 1; + if ((netf = fopen(_PATH_NETGROUP,"r")) != NULL ||_use_only_yp){ + /* + * Icky: grab the first character of the netgroup file + * and turn on NIS if it's a '+'. rewind the stream + * afterwards so we don't goof up read_for_group() later. + */ + if (netf) { + fscanf(netf, "%c", &_yp_plus); + rewind(netf); + if (_yp_plus == '+') + _use_only_yp = _netgr_yp_enabled = 1; + } + /* + * If we were called specifically for an innetgr() + * lookup and we're in NIS-only mode, short-circuit + * parse_netgroup() and cut directly to the chase. + */ + if (_use_only_yp && _yp_innetgr) { + /* dohw! */ + if (netf != NULL) + fclose(netf); + return; + } +#else + if (netf = fopen(_PATH_NETGROUP, "r")) { +#endif + if (parse_netgrp(group)) + endnetgrent(); + else { + grouphead.grname = (char *) + malloc(strlen(group) + 1); + strcpy(grouphead.grname, group); + } + if (netf) + fclose(netf); + } + } + nextgrp = grouphead.gr; +} + +/* + * Get the next netgroup off the list. + */ +int +getnetgrent(hostp, userp, domp) + char **hostp, **userp, **domp; +{ +#ifdef YP + _yp_innetgr = 0; +#endif + + if (nextgrp) { + *hostp = nextgrp->ng_str[NG_HOST]; + *userp = nextgrp->ng_str[NG_USER]; + *domp = nextgrp->ng_str[NG_DOM]; + nextgrp = nextgrp->ng_next; + return (1); + } + return (0); +} + +/* + * endnetgrent() - cleanup + */ +void +endnetgrent() +{ + register struct linelist *lp, *olp; + register struct netgrp *gp, *ogp; + + lp = linehead; + while (lp) { + olp = lp; + lp = lp->l_next; + free(olp->l_groupname); + free(olp->l_line); + free((char *)olp); + } + linehead = (struct linelist *)0; + if (grouphead.grname) { + free(grouphead.grname); + grouphead.grname = (char *)0; + } + gp = grouphead.gr; + while (gp) { + ogp = gp; + gp = gp->ng_next; + if (ogp->ng_str[NG_HOST]) + free(ogp->ng_str[NG_HOST]); + if (ogp->ng_str[NG_USER]) + free(ogp->ng_str[NG_USER]); + if (ogp->ng_str[NG_DOM]) + free(ogp->ng_str[NG_DOM]); + free((char *)ogp); + } + grouphead.gr = (struct netgrp *)0; +#ifdef YP + _netgr_yp_enabled = 0; +#endif +} + +#ifdef YP +static int _listmatch(list, group, len) + char *list, *group; + int len; +{ + char *ptr = list, *cptr; + int glen = strlen(group); + + /* skip possible leading whitespace */ + while(isspace((unsigned char)*ptr)) + ptr++; + + while (ptr < list + len) { + cptr = ptr; + while(*ptr != ',' && *ptr != '\0' && !isspace((unsigned char)*ptr)) + ptr++; + if (strncmp(cptr, group, glen) == 0 && glen == (ptr - cptr)) + return(1); + while(*ptr == ',' || isspace((unsigned char)*ptr)) + ptr++; + } + + return(0); +} + +static int _buildkey(key, str, dom, rotation) +char *key, *str, *dom; +int *rotation; +{ + (*rotation)++; + if (*rotation > 4) + return(0); + switch(*rotation) { + case(1): sprintf((char *)key, "%s.%s", str, dom ? dom : "*"); + break; + case(2): sprintf((char *)key, "%s.*", str); + break; + case(3): sprintf((char *)key, "*.%s", dom ? dom : "*"); + break; + case(4): sprintf((char *)key, "*.*"); + break; + } + return(1); +} +#endif + +/* + * Search for a match in a netgroup. + */ +int +innetgr(group, host, user, dom) + const char *group, *host, *user, *dom; +{ + char *hst, *usr, *dm; +#ifdef YP + char *result; + int resultlen; + int rv; +#endif + /* Sanity check */ + + if (group == NULL || !strlen(group)) + return (0); + +#ifdef YP + _yp_innetgr = 1; +#endif + setnetgrent(group); +#ifdef YP + _yp_innetgr = 0; + /* + * If we're in NIS-only mode, do the search using + * NIS 'reverse netgroup' lookups. + */ + if (_use_only_yp) { + char _key[MAXHOSTNAMELEN]; + int rot = 0, y = 0; + + if(yp_get_default_domain(&_netgr_yp_domain)) + return(0); + while(_buildkey(_key, user ? user : host, dom, &rot)) { + y = yp_match(_netgr_yp_domain, user? "netgroup.byuser": + "netgroup.byhost", _key, strlen(_key), &result, + &resultlen); + if (y) { + /* + * If we get an error other than 'no + * such key in map' then something is + * wrong and we should stop the search. + */ + if (y != YPERR_KEY) + break; + } else { + rv = _listmatch(result, group, resultlen); + free(result); + if (rv) + return(1); + else + return(0); + } + } + /* + * Couldn't match using NIS-exclusive mode. If the error + * was YPERR_MAP, then the failure happened because there + * was no netgroup.byhost or netgroup.byuser map. The odds + * are we are talking to an Sun NIS+ server in YP emulation + * mode; if this is the case, then we have to do the check + * the 'old-fashioned' way by grovelling through the netgroup + * map and resolving memberships on the fly. + */ + if (y != YPERR_MAP) + return(0); + } + + setnetgrent(group); +#endif /* YP */ + + while (getnetgrent(&hst, &usr, &dm)) + if ((host == NULL || hst == NULL || !strcmp(host, hst)) && + (user == NULL || usr == NULL || !strcmp(user, usr)) && + ( dom == NULL || dm == NULL || !strcmp(dom, dm))) { + endnetgrent(); + return (1); + } + endnetgrent(); + return (0); +} + +/* + * Parse the netgroup file setting up the linked lists. + */ +static int +parse_netgrp(group) + char *group; +{ + register char *spos, *epos; + register int len, strpos; +#ifdef DEBUG + register int fields; +#endif + char *pos, *gpos; + struct netgrp *grp; + struct linelist *lp = linehead; + + /* + * First, see if the line has already been read in. + */ + while (lp) { + if (!strcmp(group, lp->l_groupname)) + break; + lp = lp->l_next; + } + if (lp == (struct linelist *)0 && + (lp = read_for_group(group)) == (struct linelist *)0) + return (1); + if (lp->l_parsed) { +#ifdef DEBUG + /* + * This error message is largely superflous since the + * code handles the error condition sucessfully, and + * spewing it out from inside libc can actually hose + * certain programs. + */ + fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname); +#endif + return (1); + } else + lp->l_parsed = 1; + pos = lp->l_line; + /* Watch for null pointer dereferences, dammit! */ + while (pos != NULL && *pos != '\0') { + if (*pos == '(') { + grp = (struct netgrp *)malloc(sizeof (struct netgrp)); + bzero((char *)grp, sizeof (struct netgrp)); + grp->ng_next = grouphead.gr; + grouphead.gr = grp; + pos++; + gpos = strsep(&pos, ")"); +#ifdef DEBUG + fields = 0; +#endif + for (strpos = 0; strpos < 3; strpos++) { + if ((spos = strsep(&gpos, ","))) { +#ifdef DEBUG + fields++; +#endif + while (*spos == ' ' || *spos == '\t') + spos++; + if ((epos = strpbrk(spos, " \t"))) { + *epos = '\0'; + len = epos - spos; + } else + len = strlen(spos); + if (len > 0) { + grp->ng_str[strpos] = (char *) + malloc(len + 1); + bcopy(spos, grp->ng_str[strpos], + len + 1); + } + } else { + /* + * All other systems I've tested + * return NULL for empty netgroup + * fields. It's up to user programs + * to handle the NULLs appropriately. + */ + grp->ng_str[strpos] = NULL; + } + } +#ifdef DEBUG + /* + * Note: on other platforms, malformed netgroup + * entries are not normally flagged. While we + * can catch bad entries and report them, we should + * stay silent by default for compatibility's sake. + */ + if (fields < 3) + fprintf(stderr, "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n", + grp->ng_str[NG_HOST] == NULL ? "" : grp->ng_str[NG_HOST], + grp->ng_str[NG_USER] == NULL ? "" : ",", + grp->ng_str[NG_USER] == NULL ? "" : grp->ng_str[NG_USER], + grp->ng_str[NG_DOM] == NULL ? "" : ",", + grp->ng_str[NG_DOM] == NULL ? "" : grp->ng_str[NG_DOM], + lp->l_groupname); +#endif + } else { + spos = strsep(&pos, ", \t"); + if (parse_netgrp(spos)) + continue; + } + if (pos == NULL) + break; + while (*pos == ' ' || *pos == ',' || *pos == '\t') + pos++; + } + return (0); +} + +/* + * Read the netgroup file and save lines until the line for the netgroup + * is found. Return 1 if eof is encountered. + */ +static struct linelist * +read_for_group(group) + char *group; +{ + register char *pos, *spos, *linep, *olinep; + register int len, olen; + int cont; + struct linelist *lp; + char line[LINSIZ + 2]; +#ifdef YP + char *result; + int resultlen; + + while (_netgr_yp_enabled || fgets(line, LINSIZ, netf) != NULL) { + if (_netgr_yp_enabled) { + if(!_netgr_yp_domain) + if(yp_get_default_domain(&_netgr_yp_domain)) + continue; + if (yp_match(_netgr_yp_domain, "netgroup", group, + strlen(group), &result, &resultlen)) { + free(result); + if (_use_only_yp) + return ((struct linelist *)0); + else { + _netgr_yp_enabled = 0; + continue; + } + } + snprintf(line, LINSIZ, "%s %s", group, result); + free(result); + } +#else + while (fgets(line, LINSIZ, netf) != NULL) { +#endif + pos = (char *)&line; +#ifdef YP + if (*pos == '+') { + _netgr_yp_enabled = 1; + continue; + } +#endif + if (*pos == '#') + continue; + while (*pos == ' ' || *pos == '\t') + pos++; + spos = pos; + while (*pos != ' ' && *pos != '\t' && *pos != '\n' && + *pos != '\0') + pos++; + len = pos - spos; + while (*pos == ' ' || *pos == '\t') + pos++; + if (*pos != '\n' && *pos != '\0') { + lp = (struct linelist *)malloc(sizeof (*lp)); + lp->l_parsed = 0; + lp->l_groupname = (char *)malloc(len + 1); + bcopy(spos, lp->l_groupname, len); + *(lp->l_groupname + len) = '\0'; + len = strlen(pos); + olen = 0; + + /* + * Loop around handling line continuations. + */ + do { + if (*(pos + len - 1) == '\n') + len--; + if (*(pos + len - 1) == '\\') { + len--; + cont = 1; + } else + cont = 0; + if (len > 0) { + linep = (char *)malloc(olen + len + 1); + if (olen > 0) { + bcopy(olinep, linep, olen); + free(olinep); + } + bcopy(pos, linep + olen, len); + olen += len; + *(linep + olen) = '\0'; + olinep = linep; + } + if (cont) { + if (fgets(line, LINSIZ, netf)) { + pos = line; + len = strlen(pos); + } else + cont = 0; + } + } while (cont); + lp->l_line = linep; + lp->l_next = linehead; + linehead = lp; + + /* + * If this is the one we wanted, we are done. + */ + if (!strcmp(lp->l_groupname, group)) + return (lp); + } + } +#ifdef YP + /* + * Yucky. The recursive nature of this whole mess might require + * us to make more than one pass through the netgroup file. + * This might be best left outside the #ifdef YP, but YP is + * defined by default anyway, so I'll leave it like this + * until I know better. + */ + rewind(netf); +#endif + return ((struct linelist *)0); +} diff --git a/lib/libc/gen/getobjformat.3 b/lib/libc/gen/getobjformat.3 new file mode 100644 index 0000000..3862d82 --- /dev/null +++ b/lib/libc/gen/getobjformat.3 @@ -0,0 +1,124 @@ +.\" Copyright (c) 1998 John D. Polstra +.\" All rights reserved. +.\" +.\" 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. +.\" +.\" 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. +.\" +.\" $FreeBSD$ +.\" +.Dd September 7, 1998 +.Dt GETOBJFORMAT 3 +.Os FreeBSD +.Sh NAME +.Nm getobjformat +.Nd get preferred object file format +.Sh SYNOPSIS +.Fd #include <objformat.h> +.Ft int +.Fn getobjformat "char *buf" "size_t bufsize" "int *argcp" "char **argv" +.Sh DESCRIPTION +.Fn getobjformat +queries several sources to determine the preferred object file +format, and copies its name into a buffer provided by the caller. +.Pp +The object file format is determined as follows. If +.Va argv +is +.No non- Ns Ev NULL +and an explicit command line argument such as +.Fl aout +or +.Fl elf +is present, then that determines the object file format. +.Pp +Otherwise, if the variable +.Ev OBJFORMAT +is set in the environment, the object file format is taken from its +value. +.Pp +Otherwise, if the file +.Pa /etc/objformat +is readable and contains a line of the form +.Ql OBJFORMAT=xxx , +the object file format is taken from there. +.Pp +Otherwise, a built-in system default object file format is returned. +.Pp +.Va buf +points to a user-supplied buffer into which the name of the object +file format is copied. +.Va bufsize +gives the size of the buffer in bytes. The string placed in +.Va buf +is always null-terminated. It is an error if the buffer is too +small to hold the null-terminated name. +.Pp +.Va argv +points to a +.Dv NULL Ns -terminated +argument vector to be scanned for object +format options. +.Va argv +may be +.Dv NULL , +in which case the argument vector is not scanned. +.Pp +If +.Va argcp +is non-NULL, any object format options are deleted from the +argument vector, and the updated argument count is stored into +the integer referenced by +.Va argcp . +If +.Va argcp +is +.Dv NULL , +the argument vector is left unchanged. +.Sh RETURN VALUES +On success, +.Fn getobjformat +returns the length of the object file format name, not counting the +null terminator. +If the supplied buffer is too small to hold the object file format +and its null terminator, +.Fn getobjformat +returns -1. In that case, the contents of the buffer and argument +vector supplied by the caller are indeterminate. +.Sh ENVIRONMENT +.Bl -tag -width OBJFORMAT +.It Ev OBJFORMAT +If the environment variable +.Ev OBJFORMAT +is set, it overrides the default object file format. +.Ev OBJFORMAT takes precedence over +.Pa /etc/objformat . +.Sh FILES +.Bl -tag -width /etc/objformat -compact +.It Pa /etc/objformat +If present, specifies the object file format to use. Syntax is +.Ql OBJFORMAT=xxx . +.Sh SEE ALSO +.Xr objformat 1 +.Sh HISTORY +The +.Fn getobjformat +function first appeared in +.Fx 3.0 . diff --git a/lib/libc/gen/getobjformat.c b/lib/libc/gen/getobjformat.c new file mode 100644 index 0000000..98e6ab0 --- /dev/null +++ b/lib/libc/gen/getobjformat.c @@ -0,0 +1,119 @@ +/*- + * Copyright (c) 1998 John D. Polstra + * All rights reserved. + * + * 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. + * + * 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. + * + * $FreeBSD$ + */ + +#include <sys/param.h> +#include <objformat.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define PATH_OBJFORMAT "/etc/objformat" + +static int copyformat(char *, const char *, size_t); + +static const char *known_formats[] = { OBJFORMAT_NAMES, NULL }; + +static int +copyformat(char *buf, const char *fmt, size_t bufsize) +{ + size_t len; + + len = strlen(fmt); + if (len > bufsize - 1) + return -1; + strcpy(buf, fmt); + return len; +} + +int +getobjformat(char *buf, size_t bufsize, int *argcp, char **argv) +{ + const char *fmt; + char **src, **dst; + const char *env; + FILE *fp; + + fmt = NULL; + + if (argv != NULL) { + /* + * Scan for arguments setting known formats, e.g., "-elf". + * If "argcp" is non-NULL, delete these arguments from the + * list and update the argument count in "*argcp". + */ + for (dst = src = argv; *src != NULL; src++) { + if ((*src)[0] == '-') { + const char **p; + + for (p = known_formats; *p != NULL; p++) + if (strcmp(*src + 1, *p) == 0) + break; + if (*p != NULL) { + fmt = *p; + if (argcp == NULL) /* Don't delete */ + *dst++ = *src; + } else + *dst++ = *src; + } else + *dst++ = *src; + } + *dst = NULL; + if (argcp != NULL) + *argcp -= src - dst; + if (fmt != NULL) + return copyformat(buf, fmt, bufsize); + } + + /* Check the OBJFORMAT environment variable. */ + if ((env = getenv("OBJFORMAT")) != NULL) + return copyformat(buf, env, bufsize); + + /* Take a look at "/etc/objformat". */ + if ((fp = fopen(PATH_OBJFORMAT, "r")) != NULL) { + char line[1024]; + int found; + int len; + + found = len = 0; + while (fgets(line, sizeof line, fp) != NULL) { + if (strncmp(line, "OBJFORMAT=", 10) == 0) { + char *p = &line[10]; + + p[strcspn(p, " \t\n")] = '\0'; + len = copyformat(buf, p, bufsize); + found = 1; + } + } + fclose(fp); + if (found) + return len; + } + + /* As a last resort, use the compiled in default. */ + return copyformat(buf, OBJFORMAT_DEFAULT, bufsize); +} diff --git a/lib/libc/gen/getosreldate.c b/lib/libc/gen/getosreldate.c new file mode 100644 index 0000000..b6e91f4 --- /dev/null +++ b/lib/libc/gen/getosreldate.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +/* From: +static char sccsid[] = "@(#)gethostid.c 8.1 (Berkeley) 6/2/93"; +*/ +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> + +#if __STDC__ +int +getosreldate(void) +#else +int +getosreldate() +#endif +{ + int mib[2]; + size_t size; + int value; + + mib[0] = CTL_KERN; + mib[1] = KERN_OSRELDATE; + size = sizeof value; + if (sysctl(mib, 2, &value, &size, NULL, 0) == -1) + return (-1); + return (value); +} diff --git a/lib/libc/gen/getpagesize.3 b/lib/libc/gen/getpagesize.3 new file mode 100644 index 0000000..549cfe7 --- /dev/null +++ b/lib/libc/gen/getpagesize.3 @@ -0,0 +1,61 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getpagesize.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt GETPAGESIZE 3 +.Os BSD 4.2 +.Sh NAME +.Nm getpagesize +.Nd get system page size +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft int +.Fn getpagesize void +.Sh DESCRIPTION +.Fn Getpagesize +returns the number of bytes in a page. +Page granularity is the granularity of many of the memory +management calls. +.Pp +The page size is a system +page size and may not be the same as the underlying +hardware page size. +.Sh SEE ALSO +.Xr pagesize 1 , +.Xr sbrk 2 +.Sh HISTORY +The +.Fn getpagesze +function call appeared in +.Bx 4.2 . diff --git a/lib/libc/gen/getpagesize.c b/lib/libc/gen/getpagesize.c new file mode 100644 index 0000000..556ff9e --- /dev/null +++ b/lib/libc/gen/getpagesize.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getpagesize.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> + +/* + * This is unlikely to change over the running time of any + * program, so we cache the result to save some syscalls. + */ + +int +getpagesize() +{ + int mib[2]; + static int value; + size_t size; + + if (!value) { + mib[0] = CTL_HW; + mib[1] = HW_PAGESIZE; + size = sizeof value; + if (sysctl(mib, 2, &value, &size, NULL, 0) == -1) + return (-1); + } + return (value); +} diff --git a/lib/libc/gen/getpass.3 b/lib/libc/gen/getpass.3 new file mode 100644 index 0000000..4fe2636 --- /dev/null +++ b/lib/libc/gen/getpass.3 @@ -0,0 +1,95 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getpass.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt GETPASS 3 +.Os +.Sh NAME +.Nm getpass +.Nd get a password +.Sh SYNOPSIS +.Fd #include <pwd.h> +.Fd #include <unistd.h> +.Ft char * +.Fn getpass "const char *prompt" +.Sh DESCRIPTION +The +.Fn getpass +function displays a prompt to, and reads in a password from, +.Pa /dev/tty . +If this file is not accessible, +.Fn getpass +displays the prompt on the standard error output and reads from the standard +input. +.Pp +The password may be up to _PASSWORD_LEN (currently 128) +characters in length. +Any additional +characters and the terminating newline character are discarded. +.Pp +The +.Fn getpass +function turns off character echoing while reading the password. +.Pp +.Sh RETURN VALUES +The +.Fn getpass +function returns a pointer to the null terminated password. +.Sh FILES +.Bl -tag -width /dev/tty -compact +.It Pa /dev/tty +.El +.Sh SEE ALSO +.Xr crypt 3 +.Sh HISTORY +A +.Fn getpass +function appeared in +.At v7 . +.Sh BUGS +The +.Fn getpass +function leaves its result in an internal static object and returns +a pointer to that object. +Subsequent calls to +.Fn getpass +will modify the same object. +.Pp +The calling process should zero the password as soon as possible to +avoid leaving the cleartext password visible in the process's address +space. +.Pp +Upon receipt of a SIGTSTP, the input buffer will be flushed, so any +partially typed password must be retyped when the process +continues. diff --git a/lib/libc/gen/getpass.c b/lib/libc/gen/getpass.c new file mode 100644 index 0000000..e87a5df --- /dev/null +++ b/lib/libc/gen/getpass.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getpass.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/termios.h> +#include <signal.h> + +#include <paths.h> +#include <pwd.h> +#include <stdio.h> +#include <unistd.h> + +static struct termios oterm, term; +static FILE *fp; + +char * +getpass(prompt) + const char *prompt; +{ + register int ch; + register char *p; + FILE *outfp; + static char buf[_PASSWORD_LEN + 1]; + sigset_t oset, nset; + + /* + * read and write to /dev/tty if possible; else read from + * stdin and write to stderr. + */ + if ((outfp = fp = fopen(_PATH_TTY, "w+")) == NULL) { + outfp = stderr; + fp = stdin; + } + + /* + * note - blocking signals isn't necessarily the + * right thing, but we leave it for now. + */ + sigemptyset(&nset); + sigaddset(&nset, SIGINT); + sigaddset(&nset, SIGTSTP); + (void)sigprocmask(SIG_BLOCK, &nset, &oset); + + (void)tcgetattr(fileno(fp), &oterm); + term = oterm; + term.c_lflag &= ~ECHO; + (void)tcsetattr(fileno(fp), TCSAFLUSH|TCSASOFT, &term); + (void)fputs(prompt, outfp); + rewind(outfp); /* implied flush */ + for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';) + if (p < buf + _PASSWORD_LEN) + *p++ = ch; + *p = '\0'; + (void)write(fileno(outfp), "\n", 1); + (void)tcsetattr(fileno(fp), TCSAFLUSH|TCSASOFT, &oterm); + + (void)sigprocmask(SIG_SETMASK, &oset, NULL); + + if (fp != stdin) + (void)fclose(fp); + return(buf); +} diff --git a/lib/libc/gen/getpwent.3 b/lib/libc/gen/getpwent.3 new file mode 100644 index 0000000..57803da --- /dev/null +++ b/lib/libc/gen/getpwent.3 @@ -0,0 +1,219 @@ +.\" Copyright (c) 1988, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" From: @(#)getpwent.3 8.2 (Berkeley) 12/11/93 +.\" $FreeBSD$ +.\" +.Dd September 20, 1994 +.Dt GETPWENT 3 +.Os +.Sh NAME +.Nm getpwent , +.Nm getpwnam , +.Nm getpwuid , +.Nm setpassent , +.Nm setpwent , +.Nm endpwent +.Nd password database operations +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <pwd.h> +.Ft struct passwd * +.Fn getpwent void +.Ft struct passwd * +.Fn getpwnam "const char *login" +.Ft struct passwd * +.Fn getpwuid "uid_t uid" +.Ft int +.Fn setpassent "int stayopen" +.Ft void +.Fn setpwent void +.Ft void +.Fn endpwent void +.Sh DESCRIPTION +These functions +operate on the password database file +which is described +in +.Xr passwd 5 . +Each entry in the database is defined by the structure +.Ar passwd +found in the include +file +.Aq Pa pwd.h : +.Bd -literal -offset indent +struct passwd { + char *pw_name; /* user name */ + char *pw_passwd; /* encrypted password */ + uid_t pw_uid; /* user uid */ + gid_t pw_gid; /* user gid */ + time_t pw_change; /* password change time */ + char *pw_class; /* user access class */ + char *pw_gecos; /* Honeywell login info */ + char *pw_dir; /* home directory */ + char *pw_shell; /* default shell */ + time_t pw_expire; /* account expiration */ + int pw_fields; /* internal: fields filled in */ +}; +.Ed +.Pp +The functions +.Fn getpwnam +and +.Fn getpwuid +search the password database for the given login name or user uid, +respectively, always returning the first one encountered. +.Pp +The +.Fn getpwent +function +sequentially reads the password database and is intended for programs +that wish to process the complete list of users. +.Pp +The +.Fn setpassent +function +accomplishes two purposes. +First, it causes +.Fn getpwent +to ``rewind'' to the beginning of the database. +Additionally, if +.Fa stayopen +is non-zero, file descriptors are left open, significantly speeding +up subsequent accesses for all of the routines. +(This latter functionality is unnecessary for +.Fn getpwent +as it doesn't close its file descriptors by default.) +.Pp +It is dangerous for long-running programs to keep the file descriptors +open as the database will become out of date if it is updated while the +program is running. +.Pp +The +.Fn setpwent +function +is identical to +.Fn setpassent +with an argument of zero. +.Pp +The +.Fn endpwent +function +closes any open files. +.Pp +These routines have been written to ``shadow'' the password file, e.g. +allow only certain programs to have access to the encrypted password. +If the process which calls them has an effective uid of 0, the encrypted +password will be returned, otherwise, the password field of the returned +structure will point to the string +.Ql * . +.Sh YP/NIS INTERACTION +When the +.Xr yp 4 +password database is enabled, the +.Fn getpwnam +and +.Fn getpwuid +functions use the YP maps +.Dq Li passwd.byname +and +.Dq Li passwd.byuid , +respectively, if the requested password entry is not found in the +local database. The +.Fn getpwent +function will step through the YP map +.Dq Li passwd.byname +if the entire map is enabled as described in +.Xr passwd 5 . +.Sh RETURN VALUES +The functions +.Fn getpwent , +.Fn getpwnam , +and +.Fn getpwuid , +return a valid pointer to a passwd structure on success +and a null pointer if end-of-file is reached or an error occurs. +The +.Fn setpassent +function returns 0 on failure and 1 on success. +The +.Fn endpwent +and +.Fn setpwent +functions +have no return value. +.Sh FILES +.Bl -tag -width /etc/master.passwd -compact +.It Pa /etc/pwd.db +The insecure password database file +.It Pa /etc/spwd.db +The secure password database file +.It Pa /etc/master.passwd +The current password file +.It Pa /etc/passwd +A Version 7 format password file +.El +.Sh SEE ALSO +.Xr getlogin 2 , +.Xr getgrent 3 , +.Xr yp 4 , +.Xr passwd 5 , +.Xr pwd_mkdb 8 , +.Xr vipw 8 +.Sh HISTORY +The +.Fn getpwent , +.Fn getpwnam , +.Fn getpwuid , +.Fn setpwent, +and +.Fn endpwent +functions appeared in +.At v7 . +The +.Fn setpassent +function appeared in +.Bx 4.3 Reno . +.Sh COMPATIBILITY +The historic function +.Xr setpwfile 3 , +which allowed the specification of alternate password databases, +has been deprecated and is no longer available. +.Sh BUGS +The functions +.Fn getpwent , +.Fn getpwnam , +and +.Fn getpwuid , +leave their results in an internal static object and return +a pointer to that object. Subsequent calls to +the same function +will modify the same object. diff --git a/lib/libc/gen/getpwent.c b/lib/libc/gen/getpwent.c new file mode 100644 index 0000000..90dbb4a --- /dev/null +++ b/lib/libc/gen/getpwent.c @@ -0,0 +1,860 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getpwent.c 8.2 (Berkeley) 4/27/95"; +#endif /* LIBC_SCCS and not lint */ + +#include <stdio.h> +#include <sys/param.h> +#include <fcntl.h> +#include <db.h> +#include <syslog.h> +#include <pwd.h> +#include <utmp.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <grp.h> + +extern void setnetgrent __P(( char * )); +extern int getnetgrent __P(( char **, char **, char ** )); +extern int innetgr __P(( const char *, const char *, const char *, const char * )); + +/* + * The lookup techniques and data extraction code here must be kept + * in sync with that in `pwd_mkdb'. + */ + +static struct passwd _pw_passwd; /* password structure */ +static DB *_pw_db; /* password database */ +static int _pw_keynum; /* key counter */ +static int _pw_stayopen; /* keep fd's open */ +#ifdef YP +#include <rpc/rpc.h> +#include <rpcsvc/yp_prot.h> +#include <rpcsvc/ypclnt.h> + +static struct passwd _pw_copy; +static DBT empty = { NULL, 0 }; +static DB *_ypcache = (DB *)NULL; +static int _yp_exclusions = 0; +static int _yp_enabled = -1; +static int _pw_stepping_yp; /* set true when stepping thru map */ +static char _ypnam[YPMAXRECORD]; +#define YP_HAVE_MASTER 2 +#define YP_HAVE_ADJUNCT 1 +#define YP_HAVE_NONE 0 +static int _gotmaster; +static char *_pw_yp_domain; +static inline int unwind __P(( char * )); +static void _ypinitdb __P(( void )); +static int _havemaster __P((char *)); +static int _getyppass __P((struct passwd *, const char *, const char * )); +static int _nextyppass __P((struct passwd *)); +static inline int lookup __P((const char *)); +static inline void store __P((const char *)); +static inline int ingr __P((const char *, const char*)); +static inline int verf __P((const char *)); +static char * _get_adjunct_pw __P((const char *)); +#endif +static int __hashpw(DBT *); +static int __initdb(void); + +struct passwd * +getpwent() +{ + DBT key; + char bf[sizeof(_pw_keynum) + 1]; + int rv; + + if (!_pw_db && !__initdb()) + return((struct passwd *)NULL); + +#ifdef YP + if(_pw_stepping_yp) { + _pw_passwd = _pw_copy; + if (unwind((char *)&_ypnam)) + return(&_pw_passwd); + } +#endif +tryagain: + + ++_pw_keynum; + bf[0] = _PW_KEYBYNUM; + bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum)); + key.data = (u_char *)bf; + key.size = sizeof(_pw_keynum) + 1; + rv = __hashpw(&key); + if(!rv) return (struct passwd *)NULL; +#ifdef YP + if(_pw_passwd.pw_name[0] == '+' || _pw_passwd.pw_name[0] == '-') { + if (_yp_enabled == -1) + _ypinitdb(); + bzero((char *)&_ypnam, sizeof(_ypnam)); + bcopy(_pw_passwd.pw_name, _ypnam, + strlen(_pw_passwd.pw_name)); + _pw_copy = _pw_passwd; + if (unwind((char *)&_ypnam) == 0) + goto tryagain; + else + return(&_pw_passwd); + } +#else + /* Ignore YP password file entries when YP is disabled. */ + if(_pw_passwd.pw_name[0] == '+' || _pw_passwd.pw_name[0] == '-') { + goto tryagain; + } +#endif + return(&_pw_passwd); +} + +struct passwd * +getpwnam(name) + const char *name; +{ + DBT key; + int len, rval; + char bf[UT_NAMESIZE + 2]; + + if (!_pw_db && !__initdb()) + return((struct passwd *)NULL); + + bf[0] = _PW_KEYBYNAME; + len = strlen(name); + if (len > UT_NAMESIZE) + return(NULL); + bcopy(name, bf + 1, len); + key.data = (u_char *)bf; + key.size = len + 1; + rval = __hashpw(&key); + +#ifdef YP + if (!rval) { + if (_yp_enabled == -1) + _ypinitdb(); + if (_yp_enabled) + rval = _getyppass(&_pw_passwd, name, "passwd.byname"); + } +#endif + /* + * Prevent login attempts when YP is not enabled but YP entries + * are in /etc/master.passwd. + */ + if (rval && (_pw_passwd.pw_name[0] == '+'|| + _pw_passwd.pw_name[0] == '-')) rval = 0; + + if (!_pw_stayopen) + endpwent(); + return(rval ? &_pw_passwd : (struct passwd *)NULL); +} + +struct passwd * +getpwuid(uid) + uid_t uid; +{ + DBT key; + int keyuid, rval; + char bf[sizeof(keyuid) + 1]; + + if (!_pw_db && !__initdb()) + return((struct passwd *)NULL); + + bf[0] = _PW_KEYBYUID; + keyuid = uid; + bcopy(&keyuid, bf + 1, sizeof(keyuid)); + key.data = (u_char *)bf; + key.size = sizeof(keyuid) + 1; + rval = __hashpw(&key); + +#ifdef YP + if (!rval) { + if (_yp_enabled == -1) + _ypinitdb(); + if (_yp_enabled) { + char ypbuf[16]; /* big enough for 32-bit uids */ + snprintf(ypbuf, sizeof ypbuf, "%u", (unsigned)uid); + rval = _getyppass(&_pw_passwd, ypbuf, "passwd.byuid"); + } + } +#endif + /* + * Prevent login attempts when YP is not enabled but YP entries + * are in /etc/master.passwd. + */ + if (rval && (_pw_passwd.pw_name[0] == '+'|| + _pw_passwd.pw_name[0] == '-')) rval = 0; + + if (!_pw_stayopen) + endpwent(); + return(rval ? &_pw_passwd : (struct passwd *)NULL); +} + +int +setpassent(stayopen) + int stayopen; +{ + _pw_keynum = 0; +#ifdef YP + _pw_stepping_yp = 0; + if (stayopen) + setgroupent(1); +#endif + _pw_stayopen = stayopen; + return(1); +} + +void +setpwent() +{ + (void)setpassent(0); +} + +void +endpwent() +{ + _pw_keynum = 0; +#ifdef YP + _pw_stepping_yp = 0; +#endif + if (_pw_db) { + (void)(_pw_db->close)(_pw_db); + _pw_db = (DB *)NULL; + } +#ifdef YP + if (_ypcache) { + (void)(_ypcache->close)(_ypcache); + _ypcache = (DB *)NULL; + _yp_exclusions = 0; + } + /* Fix for PR #12008 */ + _yp_enabled = -1; +#endif +} + +static int +__initdb() +{ + static int warned; + char *p; + + p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB; + _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL); + if (_pw_db) + return(1); + if (!warned++) + syslog(LOG_ERR, "%s: %m", p); + return(0); +} + +static int +__hashpw(key) + DBT *key; +{ + register char *p, *t; + static u_int max; + static char *line; + DBT data; + + if ((_pw_db->get)(_pw_db, key, &data, 0)) + return(0); + p = (char *)data.data; + + /* Increase buffer size for long lines if necessary. */ + if (data.size > max) { + max = data.size + 1024; + if (!(line = reallocf(line, max))) + return(0); + } + + /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */ + t = line; +#define EXPAND(e) e = t; while ( (*t++ = *p++) ); +#define SCALAR(v) memmove(&(v), p, sizeof v); p += sizeof v + EXPAND(_pw_passwd.pw_name); + EXPAND(_pw_passwd.pw_passwd); + SCALAR(_pw_passwd.pw_uid); + SCALAR(_pw_passwd.pw_gid); + SCALAR(_pw_passwd.pw_change); + EXPAND(_pw_passwd.pw_class); + EXPAND(_pw_passwd.pw_gecos); + EXPAND(_pw_passwd.pw_dir); + EXPAND(_pw_passwd.pw_shell); + SCALAR(_pw_passwd.pw_expire); + bcopy(p, (char *)&_pw_passwd.pw_fields, sizeof _pw_passwd.pw_fields); + p += sizeof _pw_passwd.pw_fields; + return(1); +} + +#ifdef YP + +static void +_ypinitdb() +{ + DBT key, data; + char buf[] = { _PW_KEYYPENABLED }; + key.data = buf; + key.size = 1; + _yp_enabled = 0; + if ((_pw_db->get)(_pw_db, &key, &data, 0) == 0) { + _yp_enabled = (int)*((char *)data.data) - 2; + /* Don't even bother with this if we aren't root. */ + if (!geteuid()) { + if (!_pw_yp_domain) + if (yp_get_default_domain(&_pw_yp_domain)) + return; + _gotmaster = _havemaster(_pw_yp_domain); + } else _gotmaster = YP_HAVE_NONE; + /* + * Create a DB hash database in memory. Bet you didn't know you + * could do a dbopen() with a NULL filename, did you. + */ + if (_ypcache == (DB *)NULL) + _ypcache = dbopen(NULL, O_RDWR, 600, DB_HASH, NULL); + } +} + +/* + * See if a user is in the blackballed list. + */ +static inline int +lookup(name) + const char *name; +{ + DBT key; + + if (!_yp_exclusions) + return(0); + + key.data = (char *)name; + key.size = strlen(name); + + if ((_ypcache->get)(_ypcache, &key, &empty, 0)) { + return(0); + } + + return(1); +} + +/* + * Store a blackballed user in an in-core hash database. + */ +static inline void +store(key) + const char *key; +{ + DBT lkey; +/* + if (lookup(key)) + return; +*/ + + _yp_exclusions = 1; + + lkey.data = (char *)key; + lkey.size = strlen(key); + + (void)(_ypcache->put)(_ypcache, &lkey, &empty, R_NOOVERWRITE); +} + +/* + * Parse the + entries in the password database and do appropriate + * NIS lookups. While ugly to look at, this is optimized to do only + * as many lookups as are absolutely necessary in any given case. + * Basically, the getpwent() function will feed us + and - lines + * as they appear in the database. For + lines, we do netgroup/group + * and user lookups to find all usernames that match the rule and + * extract them from the NIS passwd maps. For - lines, we save the + * matching names in a database and a) exlude them, and b) make sure + * we don't consider them when processing other + lines that appear + * later. + */ +static inline int +unwind(grp) + char *grp; +{ + char *user, *host, *domain; + static int latch = 0; + static struct group *gr = NULL; + int rv = 0; + + if (grp[0] == '+') { + if (strlen(grp) == 1) { + return(_nextyppass(&_pw_passwd)); + } + if (grp[1] == '@') { + _pw_stepping_yp = 1; +grpagain: + if (gr != NULL) { + if (*gr->gr_mem != NULL) { + if (lookup(*gr->gr_mem)) { + gr->gr_mem++; + goto grpagain; + } + rv = _getyppass(&_pw_passwd, + *gr->gr_mem, + "passwd.byname"); + gr->gr_mem++; + return(rv); + } else { + latch = 0; + _pw_stepping_yp = 0; + gr = NULL; + return(0); + } + } + if (!latch) { + setnetgrent(grp+2); + latch++; + } +again: + if (getnetgrent(&host, &user, &domain) == 0) { + if ((gr = getgrnam(grp+2)) != NULL) + goto grpagain; + latch = 0; + _pw_stepping_yp = 0; + return(0); + } else { + if (lookup(user)) + goto again; + if (_getyppass(&_pw_passwd, user, + "passwd.byname")) + return(1); + else + goto again; + } + } else { + if (lookup(grp+1)) + return(0); + return(_getyppass(&_pw_passwd, grp+1, "passwd.byname")); + } + } else { + if (grp[1] == '@') { + setnetgrent(grp+2); + rv = 0; + while(getnetgrent(&host, &user, &domain) != 0) { + store(user); + rv++; + } + if (!rv && (gr = getgrnam(grp+2)) != NULL) { + while(*gr->gr_mem) { + store(*gr->gr_mem); + gr->gr_mem++; + } + } + } else { + store(grp+1); + } + } + return(0); +} + +/* + * See if a user is a member of a particular group. + */ +static inline int +ingr(grp, name) + const char *grp; + const char *name; +{ + register struct group *gr; + + if ((gr = getgrnam(grp)) == NULL) + return(0); + + while(*gr->gr_mem) { + if (!strcmp(*gr->gr_mem, name)) + return(1); + gr->gr_mem++; + } + + return(0); +} + +/* + * Check a user against the +@netgroup/-@netgroup lines listed in + * the local password database. Also checks +user/-user lines. + * If no netgroup exists that matches +@netgroup/-@netgroup, + * try searching regular groups with the same name. + */ +static inline int +verf(name) + const char *name; +{ + DBT key; + char bf[sizeof(_pw_keynum) + 1]; + int keynum = 0; + +again: + ++keynum; + bf[0] = _PW_KEYYPBYNUM; + bcopy((char *)&keynum, bf + 1, sizeof(keynum)); + key.data = (u_char *)bf; + key.size = sizeof(keynum) + 1; + if (!__hashpw(&key)) { + /* Try again using old format */ + bf[0] = _PW_KEYBYNUM; + bcopy((char *)&keynum, bf + 1, sizeof(keynum)); + key.data = (u_char *)bf; + if (!__hashpw(&key)) + return(0); + } + if (_pw_passwd.pw_name[0] != '+' && (_pw_passwd.pw_name[0] != '-')) + goto again; + if (_pw_passwd.pw_name[0] == '+') { + if (strlen(_pw_passwd.pw_name) == 1) /* Wildcard */ + return(1); + if (_pw_passwd.pw_name[1] == '@') { + if ((innetgr(_pw_passwd.pw_name+2, NULL, name, + _pw_yp_domain) || + ingr(_pw_passwd.pw_name+2, name)) && !lookup(name)) + return(1); + else + goto again; + } else { + if (!strcmp(name, _pw_passwd.pw_name+1) && + !lookup(name)) + return(1); + else + goto again; + } + } + if (_pw_passwd.pw_name[0] == '-') { + /* Note that a minus wildcard is a no-op. */ + if (_pw_passwd.pw_name[1] == '@') { + if (innetgr(_pw_passwd.pw_name+2, NULL, name, + _pw_yp_domain) || + ingr(_pw_passwd.pw_name+2, name)) { + store(name); + return(0); + } else + goto again; + } else { + if (!strcmp(name, _pw_passwd.pw_name+1)) { + store(name); + return(0); + } else + goto again; + } + + } + return(0); +} + +static char * +_get_adjunct_pw(name) + const char *name; +{ + static char adjunctbuf[YPMAXRECORD+2]; + int rval; + char *result; + int resultlen; + char *map = "passwd.adjunct.byname"; + char *s; + + if ((rval = yp_match(_pw_yp_domain, map, name, strlen(name), + &result, &resultlen))) + return(NULL); + + strncpy(adjunctbuf, result, resultlen); + adjunctbuf[resultlen] = '\0'; + free(result); + result = (char *)&adjunctbuf; + + /* Don't care about the name. */ + if ((s = strsep(&result, ":")) == NULL) + return (NULL); /* name */ + if ((s = strsep(&result, ":")) == NULL) + return (NULL); /* password */ + + return(s); +} + +static int +_pw_breakout_yp(struct passwd *pw, char *res, int resultlen, int master) +{ + char *s, *result; + static char resbuf[YPMAXRECORD+2]; + + /* + * Be triple, ultra super-duper paranoid: reject entries + * that start with a + or -. yp_mkdb and /var/yp/Makefile + * are _both_ supposed to strip these out, but you never + * know. + */ + if (*res == '+' || *res == '-') + return 0; + + /* + * The NIS protocol definition limits the size of an NIS + * record to YPMAXRECORD bytes. We need to do a copy to + * a static buffer here since the memory pointed to by + * res will be free()ed when this function returns. + */ + strncpy((char *)&resbuf, res, resultlen); + resbuf[resultlen] = '\0'; + result = (char *)&resbuf; + + /* + * XXX Sanity check: make sure all fields are valid (no NULLs). + * If we find a badly formatted entry, we punt. + */ + if ((s = strsep(&result, ":")) == NULL) return 0; /* name */ + /* + * We don't care what pw_fields says: we _always_ want the + * username returned to us by NIS. + */ + pw->pw_name = s; + pw->pw_fields |= _PWF_NAME; + + if ((s = strsep(&result, ":")) == NULL) return 0; /* password */ + if(!(pw->pw_fields & _PWF_PASSWD)) { + /* SunOS passwd.adjunct hack */ + if (master == YP_HAVE_ADJUNCT && strstr(s, "##") != NULL) { + char *realpw; + realpw = _get_adjunct_pw(pw->pw_name); + if (realpw == NULL) + pw->pw_passwd = s; + else + pw->pw_passwd = realpw; + } else { + pw->pw_passwd = s; + } + pw->pw_fields |= _PWF_PASSWD; + } + + if ((s = strsep(&result, ":")) == NULL) return 0; /* uid */ + if(!(pw->pw_fields & _PWF_UID)) { + pw->pw_uid = atoi(s); + pw->pw_fields |= _PWF_UID; + } + + if ((s = strsep(&result, ":")) == NULL) return 0; /* gid */ + if(!(pw->pw_fields & _PWF_GID)) { + pw->pw_gid = atoi(s); + pw->pw_fields |= _PWF_GID; + } + + if (master == YP_HAVE_MASTER) { + if ((s = strsep(&result, ":")) == NULL) return 0; /* class */ + if(!(pw->pw_fields & _PWF_CLASS)) { + pw->pw_class = s; + pw->pw_fields |= _PWF_CLASS; + } + + if ((s = strsep(&result, ":")) == NULL) return 0; /* change */ + if(!(pw->pw_fields & _PWF_CHANGE)) { + pw->pw_change = atol(s); + pw->pw_fields |= _PWF_CHANGE; + } + + if ((s = strsep(&result, ":")) == NULL) return 0; /* expire */ + if(!(pw->pw_fields & _PWF_EXPIRE)) { + pw->pw_expire = atol(s); + pw->pw_fields |= _PWF_EXPIRE; + } + } + + if ((s = strsep(&result, ":")) == NULL) return 0; /* gecos */ + if(!(pw->pw_fields & _PWF_GECOS)) { + pw->pw_gecos = s; + pw->pw_fields |= _PWF_GECOS; + } + + if ((s = strsep(&result, ":")) == NULL) return 0; /* dir */ + if(!(pw->pw_fields & _PWF_DIR)) { + pw->pw_dir = s; + pw->pw_fields |= _PWF_DIR; + } + + if ((s = strsep(&result, ":")) == NULL) return 0; /* shell */ + if(!(pw->pw_fields & _PWF_SHELL)) { + pw->pw_shell = s; + pw->pw_fields |= _PWF_SHELL; + } + + /* Be consistent. */ + if ((s = strchr(pw->pw_shell, '\n'))) *s = '\0'; + + return 1; +} + +static int +_havemaster(char *_yp_domain) +{ + int order; + int rval; + + if (!(rval = yp_order(_yp_domain, "master.passwd.byname", &order))) + return(YP_HAVE_MASTER); + + /* + * NIS+ in YP compat mode doesn't support + * YPPROC_ORDER -- no point in continuing. + */ + if (rval == YPERR_YPERR) + return(YP_HAVE_NONE); + + /* master.passwd doesn't exist -- try passwd.adjunct */ + if (rval == YPERR_MAP) { + rval = yp_order(_yp_domain, "passwd.adjunct.byname", &order); + if (!rval) + return(YP_HAVE_ADJUNCT); + } + + return (YP_HAVE_NONE); +} + +static int +_getyppass(struct passwd *pw, const char *name, const char *map) +{ + char *result, *s; + int resultlen; + int rv; + char mastermap[YPMAXRECORD]; + + if(!_pw_yp_domain) { + if(yp_get_default_domain(&_pw_yp_domain)) + return 0; + } + + if (_gotmaster == YP_HAVE_MASTER) + sprintf(mastermap,"master.%s", map); + else + sprintf(mastermap,"%s",map); + + if(yp_match(_pw_yp_domain, (char *)&mastermap, name, strlen(name), + &result, &resultlen)) { + if (_gotmaster != YP_HAVE_MASTER) + return 0; + sprintf(mastermap,"%s",map); + if (yp_match(_pw_yp_domain, (char *)&mastermap, + name, strlen(name), &result, &resultlen)) + return 0; + _gotmaster = YP_HAVE_NONE; + } + + if (!_pw_stepping_yp) { + s = strchr(result, ':'); + if (s) { + *s = '\0'; + } else { + /* Must be a malformed entry if no colons. */ + free(result); + return(0); + } + + if (!verf(result)) { + *s = ':'; + free(result); + return(0); + } + + *s = ':'; /* Put back the colon we previously replaced with a NUL. */ + } + + rv = _pw_breakout_yp(pw, result, resultlen, _gotmaster); + free(result); + return(rv); +} + +static int +_nextyppass(struct passwd *pw) +{ + static char *key; + static int keylen; + char *lastkey, *result, *s; + int resultlen; + int rv; + char *map = "passwd.byname"; + + if(!_pw_yp_domain) { + if(yp_get_default_domain(&_pw_yp_domain)) + return 0; + } + + if (_gotmaster == YP_HAVE_MASTER) + map = "master.passwd.byname"; + + if(!_pw_stepping_yp) { + if(key) free(key); + rv = yp_first(_pw_yp_domain, map, + &key, &keylen, &result, &resultlen); + if(rv) { + return 0; + } + _pw_stepping_yp = 1; + goto unpack; + } else { +tryagain: + lastkey = key; + rv = yp_next(_pw_yp_domain, map, key, keylen, + &key, &keylen, &result, &resultlen); + free(lastkey); +unpack: + if(rv) { + _pw_stepping_yp = 0; + return 0; + } + + s = strchr(result, ':'); + if (s) { + *s = '\0'; + } else { + /* Must be a malformed entry if no colons. */ + free(result); + goto tryagain; + } + + if (lookup(result)) { + *s = ':'; + free(result); + goto tryagain; + } + + *s = ':'; /* Put back the colon we previously replaced with a NUL. */ + if (_pw_breakout_yp(pw, result, resultlen, _gotmaster)) { + free(result); + return(1); + } else { + free(result); + goto tryagain; + } + } +} + +#endif /* YP */ diff --git a/lib/libc/gen/getttyent.3 b/lib/libc/gen/getttyent.3 new file mode 100644 index 0000000..ab3ff1d --- /dev/null +++ b/lib/libc/gen/getttyent.3 @@ -0,0 +1,210 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getttyent.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd November 17, 1996 +.Dt GETTTYENT 3 +.Os BSD 4.3 +.Sh NAME +.Nm getttyent , +.Nm getttynam , +.Nm setttyent , +.Nm endttyent +.Nd get ttys file entry +.Nm isdialuptty , +.Nm isnettty +.Nd determine tty type from ttys file entry +.Sh SYNOPSIS +.Fd #include <ttyent.h> +.Ft struct ttyent * +.Fn getttyent void +.Ft struct ttyent * +.Fn getttynam "const char *name" +.Ft int +.Fn setttyent void +.Ft int +.Fn endttyent void +.Ft int +.Fn isdialuptty "const char *name" +.Ft int +.Fn isnettty "const char *name" +.Sh DESCRIPTION +The +.Fn getttyent , +and +.Fn getttynam +functions +each return a pointer to an object, with the following structure, +containing the broken-out fields of a line from the tty description +file. +.Bd -literal +struct ttyent { + char *ty_name; /* terminal device name */ + char *ty_getty; /* command to execute, usually getty */ + char *ty_type; /* terminal type for termcap */ +#define TTY_ON 0x01 /* enable logins (start ty_getty program) */ +#define TTY_SECURE 0x02 /* allow uid of 0 to login */ +#define TTY_DIALUP 0x04 /* is a dialup tty */ +#define TTY_NETWORK 0x08 /* is a network tty */ + int ty_status; /* status flags */ + char *ty_window; /* command to start up window manager */ + char *ty_comment; /* comment field */ + char *ty_group; /* tty group name */ +}; +.Ed +.Pp +The fields are as follows: +.Bl -tag -width ty_comment +.It Fa ty_name +The name of the character-special file. +.It Fa ty_getty +The name of the command invoked by +.Xr init 8 +to initialize tty line characteristics. +.It Fa ty_type +The name of the default terminal type connected to this tty line. +.It Fa ty_status +A mask of bit fields which indicate various actions allowed on this +tty line. +The possible flags are as follows: +.Bl -tag -width TTY_NETWORK +.It Dv TTY_ON +Enables logins (i.e., +.Xr init 8 +will start the command referenced by +.Fa ty_getty +on this entry). +.It Dv TTY_SECURE +Allow users with a uid of 0 to login on this terminal. +.It Dv TTY_DIALUP +Identifies a tty as a dialin line. +If this flag is set, then +.Fn isdialuptty +will return a non-zero value. +.It Dv TTY_NETWORK +Identifies a tty used for network connections. +If this flag is set, then +.Fn isnettty +will return a non-zero value. +.El +.It Fa ty_window +The command to execute for a window system associated with the line. +.It Fa ty_group +A group name to which the tty belongs. +If no group is specified in the ttys description file, +then the tty is placed in an anonymous group called "none". +.It Fa ty_comment +Any trailing comment field, with any leading hash marks (``#'') or +whitespace removed. +.El +.Pp +If any of the fields pointing to character strings are unspecified, +they are returned as null pointers. +The field +.Fa ty_status +will be zero if no flag values are specified. +.Pp +See +.Xr ttys 5 +for a more complete discussion of the meaning and usage of the +fields. +.Pp +The +.Fn getttyent +function +reads the next line from the ttys file, opening the file if necessary. +The +.Fn setttyent +function +rewinds the file if open, or opens the file if it is unopened. +The +.Fn endttyent +function +closes any open files. +.Pp +The +.Fn getttynam +function +searches from the beginning of the file until a matching +.Fa name +is found +(or until +.Dv EOF +is encountered). +.Sh RETURN VALUES +The routines +.Fn getttyent +and +.Fn getttynam +return a null pointer on +.Dv EOF +or error. +The +.Fn setttyent +function +and +.Fn endttyent +return 0 on failure and 1 on success. +.Pp +The routines +.Fn isdialuptty +and +.Fn isnettty +return non-zero if the dialup or network flag is set for the +tty entry relating to the tty named by the parameter, and +zero otherwise. +.Sh FILES +.Bl -tag -width /etc/ttys -compact +.It Pa /etc/ttys +.El +.Sh SEE ALSO +.Xr login 1 , +.Xr ttyslot 3 , +.Xr gettytab 5 , +.Xr termcap 5 , +.Xr ttys 5 , +.Xr getty 8 , +.Xr init 8 +.Sh HISTORY +The +.Fn getttyent , +.Fn getttynam , +.Fn setttyent , +and +.Fn endttyent +functions appeared in +.Bx 4.3 . +.Sh BUGS +These functions use static data storage; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. diff --git a/lib/libc/gen/getttyent.c b/lib/libc/gen/getttyent.c new file mode 100644 index 0000000..bfbe833 --- /dev/null +++ b/lib/libc/gen/getttyent.c @@ -0,0 +1,266 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <ttyent.h> +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> + +static char zapchar; +static FILE *tf; +static size_t lbsize; +static char *line; + +#define MALLOCCHUNK 100 + +static char *skip __P((char *)); +static char *value __P((char *)); + +struct ttyent * +getttynam(tty) + const char *tty; +{ + register struct ttyent *t; + + if (strncmp(tty, "/dev/", 5) == 0) + tty += 5; + setttyent(); + while ( (t = getttyent()) ) + if (!strcmp(tty, t->ty_name)) + break; + endttyent(); + return (t); +} + +struct ttyent * +getttyent() +{ + static struct ttyent tty; + register char *p; + register int c; + size_t i; + + if (!tf && !setttyent()) + return (NULL); + for (;;) { + if (!fgets(p = line, lbsize, tf)) + return (NULL); + /* extend buffer if line was too big, and retry */ + while (!index(p, '\n')) { + i = strlen(p); + lbsize += MALLOCCHUNK; + if ((p = realloc(line, lbsize)) == NULL) { + (void)endttyent(); + return (NULL); + } + line = p; + if (!fgets(&line[i], lbsize - i, tf)) + return (NULL); + } + while (isspace((unsigned char)*p)) + ++p; + if (*p && *p != '#') + break; + } + +#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1]) +#define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' + + zapchar = 0; + tty.ty_name = p; + p = skip(p); + if (!*(tty.ty_getty = p)) + tty.ty_getty = tty.ty_type = NULL; + else { + p = skip(p); + if (!*(tty.ty_type = p)) + tty.ty_type = NULL; + else { + /* compatibility kludge: handle network/dialup specially */ + if (scmp(_TTYS_DIALUP)) + tty.ty_status |= TTY_DIALUP; + else if (scmp(_TTYS_NETWORK)) + tty.ty_status |= TTY_NETWORK; + p = skip(p); + } + } + tty.ty_status = 0; + tty.ty_window = NULL; + tty.ty_group = _TTYS_NOGROUP; + + for (; *p; p = skip(p)) { + if (scmp(_TTYS_OFF)) + tty.ty_status &= ~TTY_ON; + else if (scmp(_TTYS_ON)) + tty.ty_status |= TTY_ON; + else if (scmp(_TTYS_SECURE)) + tty.ty_status |= TTY_SECURE; + else if (scmp(_TTYS_INSECURE)) + tty.ty_status &= ~TTY_SECURE; + else if (scmp(_TTYS_DIALUP)) + tty.ty_status |= TTY_DIALUP; + else if (scmp(_TTYS_NETWORK)) + tty.ty_status |= TTY_NETWORK; + else if (vcmp(_TTYS_WINDOW)) + tty.ty_window = value(p); + else if (vcmp(_TTYS_GROUP)) + tty.ty_group = value(p); + else + break; + } + + if (zapchar == '#' || *p == '#') + while ((c = *++p) == ' ' || c == '\t') + ; + tty.ty_comment = p; + if (*p == 0) + tty.ty_comment = 0; + if ( (p = index(p, '\n')) ) + *p = '\0'; + return (&tty); +} + +#define QUOTED 1 + +/* + * Skip over the current field, removing quotes, and return a pointer to + * the next field. + */ +static char * +skip(p) + register char *p; +{ + register char *t; + register int c, q; + + for (q = 0, t = p; (c = *p) != '\0'; p++) { + if (c == '"') { + q ^= QUOTED; /* obscure, but nice */ + continue; + } + if (q == QUOTED && *p == '\\' && *(p+1) == '"') + p++; + *t++ = *p; + if (q == QUOTED) + continue; + if (c == '#') { + zapchar = c; + *p = 0; + break; + } + if (c == '\t' || c == ' ' || c == '\n') { + zapchar = c; + *p++ = 0; + while ((c = *p) == '\t' || c == ' ' || c == '\n') + p++; + break; + } + } + *--t = '\0'; + return (p); +} + +static char * +value(p) + register char *p; +{ + + return ((p = index(p, '=')) ? ++p : NULL); +} + +int +setttyent() +{ + + if (line == NULL) { + if ((line = malloc(MALLOCCHUNK)) == NULL) + return (0); + lbsize = MALLOCCHUNK; + } + if (tf) { + rewind(tf); + return (1); + } else if ( (tf = fopen(_PATH_TTYS, "r")) ) + return (1); + return (0); +} + +int +endttyent() +{ + int rval; + + /* + * NB: Don't free `line' because getttynam() + * may still be referencing it + */ + if (tf) { + rval = (fclose(tf) != EOF); + tf = NULL; + return (rval); + } + return (1); +} + +static int +isttystat(tty, flag) + const char *tty; + int flag; +{ + register struct ttyent *t; + + return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag); +} + + +int +isdialuptty(tty) + const char *tty; +{ + + return isttystat(tty, TTY_DIALUP); +} + +int isnettty(tty) + const char *tty; +{ + + return isttystat(tty, TTY_NETWORK); +} diff --git a/lib/libc/gen/getusershell.3 b/lib/libc/gen/getusershell.3 new file mode 100644 index 0000000..62eaafa --- /dev/null +++ b/lib/libc/gen/getusershell.3 @@ -0,0 +1,99 @@ +.\" Copyright (c) 1985, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)getusershell.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt GETUSERSHELL 3 +.Os BSD 4.3 +.Sh NAME +.Nm getusershell , +.Nm setusershell , +.Nm endusershell +.Nd get legal user shells +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft char * +.Fn getusershell void +.Ft void +.Fn setusershell void +.Ft void +.Fn endusershell void +.Sh DESCRIPTION +The +.Fn getusershell +function +returns a pointer to a legal user shell as defined by the +system manager in the file +.Pa /etc/shells . +If +.Pa /etc/shells +is unreadable or does not exist, +.Fn getusershell +behaves as if +.Pa /bin/sh +and +.Pa /bin/csh +were listed in the file. +.Pp +The +.Fn getusershell +function +reads the next +line (opening the file if necessary); +.Fn setusershell +rewinds the file; +.Fn endusershell +closes it. +.Sh FILES +.Bl -tag -width /etc/shells -compact +.It Pa /etc/shells +.El +.Sh DIAGNOSTICS +The routine +.Fn getusershell +returns a null pointer (0) on +.Dv EOF . +.Sh SEE ALSO +.Xr shells 5 +.Sh HISTORY +The +.Fn getusershell +function appeared in +.Bx 4.3 . +.Sh BUGS +The +.Fn getusershell +function leaves its result in an internal static object and returns +a pointer to that object. Subsequent calls to +.Fn getusershell +will modify the same object. diff --git a/lib/libc/gen/getusershell.c b/lib/libc/gen/getusershell.c new file mode 100644 index 0000000..be1a77a --- /dev/null +++ b/lib/libc/gen/getusershell.c @@ -0,0 +1,139 @@ +/* + * Copyright (c) 1985, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getusershell.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/file.h> +#include <sys/stat.h> +#include <stdio.h> +#include <ctype.h> +#include <stdlib.h> +#include <unistd.h> +#include <paths.h> + +/* + * Local shells should NOT be added here. They should be added in + * /etc/shells. + */ + +static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; +static char **curshell, **shells, *strings; +static char **initshells __P((void)); + +/* + * Get a list of shells from _PATH_SHELLS, if it exists. + */ +char * +getusershell() +{ + char *ret; + + if (curshell == NULL) + curshell = initshells(); + ret = *curshell; + if (ret != NULL) + curshell++; + return (ret); +} + +void +endusershell() +{ + + if (shells != NULL) + free(shells); + shells = NULL; + if (strings != NULL) + free(strings); + strings = NULL; + curshell = NULL; +} + +void +setusershell() +{ + + curshell = initshells(); +} + +static char ** +initshells() +{ + register char **sp, *cp; + register FILE *fp; + struct stat statb; + + if (shells != NULL) + free(shells); + shells = NULL; + if (strings != NULL) + free(strings); + strings = NULL; + if ((fp = fopen(_PATH_SHELLS, "r")) == NULL) + return (okshells); + if (fstat(fileno(fp), &statb) == -1) { + (void)fclose(fp); + return (okshells); + } + if ((strings = malloc((u_int)statb.st_size)) == NULL) { + (void)fclose(fp); + return (okshells); + } + shells = calloc((unsigned)statb.st_size / 3, sizeof (char *)); + if (shells == NULL) { + (void)fclose(fp); + free(strings); + strings = NULL; + return (okshells); + } + sp = shells; + cp = strings; + while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) { + while (*cp != '#' && *cp != '/' && *cp != '\0') + cp++; + if (*cp == '#' || *cp == '\0') + continue; + *sp++ = cp; + while (!isspace((unsigned char)*cp) && *cp != '#' && *cp != '\0') + cp++; + *cp++ = '\0'; + } + *sp = NULL; + (void)fclose(fp); + return (shells); +} diff --git a/lib/libc/gen/getvfsbyname.3 b/lib/libc/gen/getvfsbyname.3 new file mode 100644 index 0000000..c33fbea --- /dev/null +++ b/lib/libc/gen/getvfsbyname.3 @@ -0,0 +1,98 @@ +.\" Copyright (c) 1995 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)kvm_getvfsbyname.3 8.3 (Berkeley) 5/4/95 +.\" $FreeBSD$ +.\" +.Dd May 4, 1995 +.Dt GETVFSBYNAME 3 +.Os +.Sh NAME +.Nm getvfsbyname +.Nd get information about a filesystem +.Sh SYNOPSIS +.Fd #include <sys/param.h> +.Fd #include <sys/mount.h> +.Ft int +.Fn getvfsbyname "const char *name" "struct vfsconf *vfc" +.Sh DESCRIPTION +The +.Fn getvfsbyname +function provides access to information about a +filesystem module that is configured in the kernel. +If successful, +the requested filesystem +.Fa vfsconf +is returned in the location pointed to by +.Fa vfc . +The fields in a +.Dq Li struct vfsconf +are defined as follows: +.Pp +.Bl -tag -compact -width vfc_refcount +.It vfc_name +the name of the filesystem +.It vfc_typenum +the filesystem type number assigned by the kernel +.It vfc_refcount +the number of active mount points using the filesystem +.It vfc_flags +flag bits that are used to initialize a new mount point +using the filesystem +.El +.Sh RETURN VALUES +If the call to +.Nm getvfsbyname +is successful, 0 is returned. +Otherwise \-1 is returned and +.Va errno +is set appropriately. +.Sh ERRORS +The following errors may be reported: +.Bl -tag -width Er +.It Bq Er EFAULT +The +.Fa vfc +pointer contains an invalid address. +.It Bq Er ENOENT +The +.Fa name +specifies a filesystem that is unknown or not configured in the kernel. +.Sh SEE ALSO +.Xr mount 2 , +.Xr sysctl 3 , +.Xr mount 8 , +.Xr sysctl 8 +.Sh HISTORY +A variant of the +.Fn getvfsbyname +function first appeared in +.Fx 2.0 . diff --git a/lib/libc/gen/getvfsbyname.c b/lib/libc/gen/getvfsbyname.c new file mode 100644 index 0000000..a8f6913 --- /dev/null +++ b/lib/libc/gen/getvfsbyname.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 1995 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)kvm_getvfsbyname.c 8.1 (Berkeley) 4/3/95"; +#endif +static const char rcsid[] = + "$FreeBSD$"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/mount.h> +#include <sys/sysctl.h> +#include <errno.h> + +/* + * Given a filesystem name, determine if it is resident in the kernel, + * and if it is resident, return its vfsconf structure. + */ +getvfsbyname(fsname, vfcp) + const char *fsname; + struct vfsconf *vfcp; +{ +#ifdef __NETBSD_SYSCALLS + errno = ENOSYS; +#else + int name[4], maxtypenum, cnt; + size_t buflen; + + name[0] = CTL_VFS; + name[1] = VFS_GENERIC; + name[2] = VFS_MAXTYPENUM; + buflen = 4; + if (sysctl(name, 3, &maxtypenum, &buflen, (void *)0, (size_t)0) < 0) + return (-1); + name[2] = VFS_CONF; + buflen = sizeof *vfcp; + for (cnt = 0; cnt < maxtypenum; cnt++) { + name[3] = cnt; + if (sysctl(name, 4, vfcp, &buflen, (void *)0, (size_t)0) < 0) { + if (errno != EOPNOTSUPP) + return (-1); + continue; + } + if (!strcmp(fsname, vfcp->vfc_name)) + return (0); + } + errno = ENOENT; +#endif + return (-1); +} diff --git a/lib/libc/gen/getvfsent.3 b/lib/libc/gen/getvfsent.3 new file mode 100644 index 0000000..127e0f3 --- /dev/null +++ b/lib/libc/gen/getvfsent.3 @@ -0,0 +1,163 @@ +.\" $FreeBSD$ +.\" Written by Garrett A. Wollman, September 1994. +.\" This manual page is in the public domain. +.\" +.Dd September 24, 1994 +.Dt GETVFSENT 3 +.Os +.Sh NAME +.Nm getvfsent , +.Nm setvfsent , +.Nm endvfsent , +.Nm vfsisloadable , +.Nm vfsload +.Nd manage virtual filesystem modules +.Sh SYNOPSIS +.Fd #include <sys/param.h> +.Fd #include <sys/mount.h> +.Ft struct ovfsconf * +.Fn getvfsent "void" +.Ft void +.Fn setvfsent "int cachelist" +.Ft void +.Fn endvfsent "void" +.Ft int +.Fn vfsisloadable "const char *name" +.Ft int +.Fn vfsload "const char *name" +.Sh DESCRIPTION +The +.Fn getvfsent +function provides convenient access to a list of installed virtual +filesystem modules managed by the kernel. It steps through the +list of filesystems one at a time. A null pointer is returned when +no more data is available. The fields in a +.Dq Li struct ovfsconf +are as follows: +.Pp +.Bl -tag -compact -width vfc_refcount +.It vfc_name +the name of the filesystem +.It vfc_index +the filesystem type number assigned by the kernel and used in calls to +.Xr mount 2 +.It vfc_refcount +the number of references to this filesystem +(usually the number of mounts, but one greater for filesystems which +cannot be unloaded or which are statically linked into the kernel) +.It vfc_flags +flag bits, of which none are currently defined +.El +.Pp +The +.Fn setvfsent +and +.Fn endvfsent +functions are used to control caching of the filesystem list, which is +obtained in toto from the kernel via +.Xr sysctl 3 . +If the +.Fa cachelist +parameter to +.Fn setvfsent +is non-zero, the list will be retrieved only once, upon the first call +to one of the retrieval functions, until +.Fn endvfsent +is called to clear the cache. In general, +.Fn setvfsent 1 +should be called by programs using the +.Fn getvfsent +function, and +.Fn setvfsent 0 +(which is also the default state) +should be called by programs using the +.Fn vfsload +function. +.Pp +The +.Fn vfsisloadable +function returns a non-zero value if a later call to +.Fn vfsload name +is likely to succeed. We say +.Dq likely +because +.Fn vfsisloadable +does not check any of the conditions necessary for +.Fn vfsload +to succeed. +.Pp +The +.Fn vfsload +function attempts to load a kernel module implementing filesystem +.Fa name . +It returns zero if the filesystem module was successfully located and +loaded, or non-zero otherwise. It should only be called in the +following circumstances: +.Bl -enum +.It +.Fn getvfsbyname +has been called and returned a non-zero value. +.It +.Fn vfsisloadable +has been called and returned a non-zero value. +.El +.Pp +Here is an example, taken from the source to +.Xr mount_cd9660 8 : +.Bd -literal -offset indent + +struct vfsconf *vfc; +int error; + +/* setup code here */ + +error = getvfsbyname("cd9660", &vfc); +if (error && vfsisloadable("cd9660")) { + if (vfsload("cd9660")) + err(EX_OSERR, "vfsload(cd9660)"); + endvfsent(); /* flush cache */ + error = getvfsbyname("cd9660", &vfc); +} +if (error) + errx(1, "cd9660 filesystem is not available"); + +if (mount(vfc.vfc_name, dir, mntflags, &args) < 0) + err(1, NULL); + +.Ed +.Sh RETURN VALUES +The +.Fn getvfsent +routine returns a pointer to a static data structure when +it succeeds, and returns a null pointer when it fails. On failure, +.Va errno +may be set to one of the values documented for +.Xr sysctl 3 +or +.Xr malloc 3 , +if a failure of that function was the cause; otherwise +.Va errno +will be unmodified. +.Pp +The +.Fn vfsload +function returns a non-zero value on failure, or zero on success. If +.Fn vfsload +fails, +.Va errno +may be set to one of the values documented for +.Xr kldload 2 . +.Sh SEE ALSO +.Xr kldload 2 , +.Xr mount 2 , +.Xr mount 8 +.Sh AUTHORS +The loadable filesystem support was written by +.An Garrett A. Wollman , +based on generic loadable kernel module support by +.An Terry Lambert . +.Sh HISTORY +The +.Fn getvfsent +family of functions first appeared in +.Fx 2.0 . diff --git a/lib/libc/gen/getvfsent.c b/lib/libc/gen/getvfsent.c new file mode 100644 index 0000000..bf3ca20 --- /dev/null +++ b/lib/libc/gen/getvfsent.c @@ -0,0 +1,177 @@ +/* + * getvfsent.c - get a listing of installed filesystems + * Written September 1994 by Garrett A. Wollman + * This file is in the public domain. + */ + +#include <sys/param.h> +#include <sys/types.h> +#include <sys/mount.h> +#include <sys/sysctl.h> +#include <unistd.h> +#include <sys/file.h> +#include <sys/wait.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> +#include <paths.h> + +/* XXX hide some compatibility problems. */ +#undef getvfsbyname +#define vfsconf ovfsconf + +static struct vfsconf *_vfslist = 0; +static struct vfsconf _vfsconf; +static size_t _vfslistlen = 0; +static int _vfs_keeplist = 0; +static int _vfs_index = 0; + +static int +initvfs(void) +{ + int mib[2] = { CTL_VFS, VFS_VFSCONF }; + size_t size = 0; + int rv; + + rv = sysctl(mib, 2, (void *)0, &size, (void *)0, (size_t)0); + if(rv < 0) + return 0; + + if(_vfslist) + free(_vfslist); + _vfslist = malloc(size); + if(!_vfslist) + return 0; + + rv = sysctl(mib, 2, _vfslist, &size, (void *)0, (size_t)0); + if(rv < 0) { + free(_vfslist); + _vfslist = 0; + return 0; + } + + _vfslistlen = size / sizeof _vfslist[0]; + return 1; +} + +struct vfsconf * +getvfsent(void) +{ + if(!_vfslist && !initvfs()) { + return 0; + } + + do { + if(_vfs_index >= _vfslistlen) { + return 0; + } + + _vfsconf = _vfslist[_vfs_index++]; + } while(!_vfsconf.vfc_vfsops); + + if(!_vfs_keeplist) { + free(_vfslist); + _vfslist = 0; + } + return &_vfsconf; +} + +struct vfsconf * +getvfsbyname(const char *name) +{ + int i; + + if(!_vfslist && !initvfs()) { + return 0; + } + + for(i = 0; i < _vfslistlen; i++) { + if( ! strcmp(_vfslist[i].vfc_name, name) ) + break; + } + + if(i < _vfslistlen) { + _vfsconf = _vfslist[i]; + } + + if(!_vfs_keeplist) { + free(_vfslist); + _vfslist = 0; + } + + if(i < _vfslistlen) { + return &_vfsconf; + } else { + return 0; + } +} + +struct vfsconf * +getvfsbytype(int type) +{ + int i; + + if(!_vfslist && !initvfs()) { + return 0; + } + + for(i = 0; i < _vfslistlen; i++) { + if(_vfslist[i].vfc_index == type) + break; + } + + if(i < _vfslistlen) { + _vfsconf = _vfslist[i]; + } + + if(!_vfs_keeplist) { + free(_vfslist); + _vfslist = 0; + } + + if(i < _vfslistlen) { + return &_vfsconf; + } else { + return 0; + } +} + +void +setvfsent(int keep) +{ + if(_vfslist && !keep) { + free(_vfslist); + _vfslist = 0; + } + + _vfs_keeplist = keep; + _vfs_index = 0; +} + +void +endvfsent(void) +{ + if(_vfslist) { + free(_vfslist); + _vfslist = 0; + } + + _vfs_index = 0; +} + +int +vfsisloadable(const char *name) +{ + return 1; +} + +int +vfsload(const char *name) +{ + int status; + + status = kldload(name); + return status == -1 ? status : 0; +} + diff --git a/lib/libc/gen/glob.3 b/lib/libc/gen/glob.3 new file mode 100644 index 0000000..d99dc9f --- /dev/null +++ b/lib/libc/gen/glob.3 @@ -0,0 +1,447 @@ +.\" Copyright (c) 1989, 1991, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Guido van Rossum. +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)glob.3 8.3 (Berkeley) 4/16/94 +.\" $FreeBSD$ +.\" +.Dd April 16, 1994 +.Dt GLOB 3 +.Os +.Sh NAME +.Nm glob , +.Nm globfree +.Nd generate pathnames matching a pattern +.Sh SYNOPSIS +.Fd #include <glob.h> +.Ft int +.Fn glob "const char *pattern" "int flags" "int (*errfunc)(const char *, int)" "glob_t *pglob" +.Ft void +.Fn globfree "glob_t *pglob" +.Sh DESCRIPTION +The +.Fn glob +function +is a pathname generator that implements the rules for file name pattern +matching used by the shell. +.Pp +The include file +.Pa glob.h +defines the structure type +.Fa glob_t , +which contains at least the following fields: +.Bd -literal +typedef struct { + int gl_pathc; /* count of total paths so far */ + int gl_matchc; /* count of paths matching pattern */ + int gl_offs; /* reserved at beginning of gl_pathv */ + int gl_flags; /* returned flags */ + char **gl_pathv; /* list of paths matching pattern */ +} glob_t; +.Ed +.Pp +The argument +.Fa pattern +is a pointer to a pathname pattern to be expanded. +The +.Fn glob +argument +matches all accessible pathnames against the pattern and creates +a list of the pathnames that match. +In order to have access to a pathname, +.Fn glob +requires search permission on every component of a path except the last +and read permission on each directory of any filename component of +.Fa pattern +that contains any of the special characters +.Ql * , +.Ql ? +or +.Ql [ . +.Pp +The +.Fn glob +argument +stores the number of matched pathnames into the +.Fa gl_pathc +field, and a pointer to a list of pointers to pathnames into the +.Fa gl_pathv +field. +The first pointer after the last pathname is +.Dv NULL . +If the pattern does not match any pathnames, the returned number of +matched paths is set to zero. +.Pp +It is the caller's responsibility to create the structure pointed to by +.Fa pglob . +The +.Fn glob +function allocates other space as needed, including the memory pointed +to by +.Fa gl_pathv . +.Pp +The argument +.Fa flags +is used to modify the behavior of +.Fn glob . +The value of +.Fa flags +is the bitwise inclusive +.Tn OR +of any of the following +values defined in +.Pa glob.h : +.Bl -tag -width GLOB_ALTDIRFUNC +.It Dv GLOB_APPEND +Append pathnames generated to the ones from a previous call (or calls) +to +.Fn glob . +The value of +.Fa gl_pathc +will be the total matches found by this call and the previous call(s). +The pathnames are appended to, not merged with the pathnames returned by +the previous call(s). +Between calls, the caller must not change the setting of the +.Dv GLOB_DOOFFS +flag, nor change the value of +.Fa gl_offs +when +.Dv GLOB_DOOFFS +is set, nor (obviously) call +.Fn globfree +for +.Fa pglob . +.It Dv GLOB_DOOFFS +Make use of the +.Fa gl_offs +field. +If this flag is set, +.Fa gl_offs +is used to specify how many +.Dv NULL +pointers to prepend to the beginning +of the +.Fa gl_pathv +field. +In other words, +.Fa gl_pathv +will point to +.Fa gl_offs +.Dv NULL +pointers, +followed by +.Fa gl_pathc +pathname pointers, followed by a +.Dv NULL +pointer. +.It Dv GLOB_ERR +Causes +.Fn glob +to return when it encounters a directory that it cannot open or read. +Ordinarily, +.Fn glob +continues to find matches. +.It Dv GLOB_MARK +Each pathname that is a directory that matches +.Fa pattern +has a slash +appended. +.It Dv GLOB_NOCHECK +If +.Fa pattern +does not match any pathname, then +.Fn glob +returns a list +consisting of only +.Fa pattern , +with the number of total pathnames is set to 1, and the number of matched +pathnames set to 0. +If +.Dv GLOB_QUOTE +is set, its effect is present in the pattern returned. +.It Dv GLOB_NOSORT +By default, the pathnames are sorted in ascending +.Tn ASCII +order; +this flag prevents that sorting (speeding up +.Fn glob ) . +.El +.Pp +The following values may also be included in +.Fa flags , +however, they are non-standard extensions to +.St -p1003.2 . +.Bl -tag -width GLOB_ALTDIRFUNC +.It Dv GLOB_ALTDIRFUNC +The following additional fields in the pglob structure have been +initialized with alternate functions for glob to use to open, read, +and close directories and to get stat information on names found +in those directories. +.Bd -literal +void *(*gl_opendir)(const char * name); +struct dirent *(*gl_readdir)(void *); +void (*gl_closedir)(void *); +int (*gl_lstat)(const char *name, struct stat *st); +int (*gl_stat)(const char *name, struct stat *st); +.Ed +.Pp +This extension is provided to allow programs such as +.Xr restore 8 +to provide globbing from directories stored on tape. +.It Dv GLOB_BRACE +Pre-process the pattern string to expand +.Ql {pat,pat,...} +strings like +.Xr csh 1 . +The pattern +.Ql {} +is left unexpanded for historical reasons (and +.Xr csh 1 +does the same thing to +ease typing +of +.Xr find 1 +patterns). +.It Dv GLOB_MAGCHAR +Set by the +.Fn glob +function if the pattern included globbing characters. +See the description of the usage of the +.Fa gl_matchc +structure member for more details. +.It Dv GLOB_NOMAGIC +Is the same as +.Dv GLOB_NOCHECK +but it only appends the +.Fa pattern +if it does not contain any of the special characters ``*'', ``?'' or ``[''. +.Dv GLOB_NOMAGIC +is provided to simplify implementing the historic +.Xr csh 1 +globbing behavior and should probably not be used anywhere else. +.It Dv GLOB_QUOTE +Use the backslash +.Pq Ql \e +character for quoting: every occurrence of +a backslash followed by a character in the pattern is replaced by that +character, avoiding any special interpretation of the character. +.It Dv GLOB_TILDE +Expand patterns that start with +.Ql ~ +to user name home directories. +.El +.Pp +If, during the search, a directory is encountered that cannot be opened +or read and +.Fa errfunc +is +.Pf non- Dv NULL , +.Fn glob +calls +.Fa (*errfunc)(path, errno) . +This may be unintuitive: a pattern like +.Ql */Makefile +will try to +.Xr stat 2 +.Ql foo/Makefile +even if +.Ql foo +is not a directory, resulting in a +call to +.Fa errfunc . +The error routine can suppress this action by testing for +.Dv ENOENT +and +.Dv ENOTDIR ; +however, the +.Dv GLOB_ERR +flag will still cause an immediate +return when this happens. +.Pp +If +.Fa errfunc +returns non-zero, +.Fn glob +stops the scan and returns +.Dv GLOB_ABEND +after setting +.Fa gl_pathc +and +.Fa gl_pathv +to reflect any paths already matched. +This also happens if an error is encountered and +.Dv GLOB_ERR +is set in +.Fa flags , +regardless of the return value of +.Fa errfunc , +if called. +If +.Dv GLOB_ERR +is not set and either +.Fa errfunc +is +.Dv NULL +or +.Fa errfunc +returns zero, the error is ignored. +.Pp +The +.Fn globfree +function frees any space associated with +.Fa pglob +from a previous call(s) to +.Fn glob . +.Sh RETURN VALUES +On successful completion, +.Fn glob +returns zero. +In addition the fields of +.Fa pglob +contain the values described below: +.Bl -tag -width GLOB_NOCHECK +.It Fa gl_pathc +contains the total number of matched pathnames so far. +This includes other matches from previous invocations of +.Fn glob +if +.Dv GLOB_APPEND +was specified. +.It Fa gl_matchc +contains the number of matched pathnames in the current invocation of +.Fn glob . +.It Fa gl_flags +contains a copy of the +.Fa flags +parameter with the bit +.Dv GLOB_MAGCHAR +set if +.Fa pattern +contained any of the special characters ``*'', ``?'' or ``['', cleared +if not. +.It Fa gl_pathv +contains a pointer to a +.Dv NULL Ns -terminated +list of matched pathnames. +However, if +.Fa gl_pathc +is zero, the contents of +.Fa gl_pathv +are undefined. +.El +.Pp +If +.Fn glob +terminates due to an error, it sets errno and returns one of the +following non-zero constants, which are defined in the include +file +.Aq Pa glob.h : +.Bl -tag -width GLOB_NOCHECK +.It Dv GLOB_NOSPACE +An attempt to allocate memory failed. +.It Dv GLOB_ABEND +The scan was stopped because an error was encountered and either +.Dv GLOB_ERR +was set or +.Fa (*errfunc)() +returned non-zero. +.El +.Pp +The arguments +.Fa pglob\->gl_pathc +and +.Fa pglob\->gl_pathv +are still set as specified above. +.Sh EXAMPLE +A rough equivalent of +.Ql "ls -l *.c *.h" +can be obtained with the +following code: +.Bd -literal -offset indent +glob_t g; + +g.gl_offs = 2; +glob("*.c", GLOB_DOOFFS, NULL, &g); +glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g); +g.gl_pathv[0] = "ls"; +g.gl_pathv[1] = "-l"; +execvp("ls", g.gl_pathv); +.Ed +.Sh SEE ALSO +.Xr sh 1 , +.Xr fnmatch 3 , +.Xr regexp 3 +.Sh STANDARDS +The +.Fn glob +function is expected to be +.St -p1003.2 +compatible with the exception +that the flags +.Dv GLOB_ALTDIRFUNC, +.Dv GLOB_BRACE +.Dv GLOB_MAGCHAR, +.Dv GLOB_NOMAGIC, +.Dv GLOB_QUOTE, +and +.Dv GLOB_TILDE, +and the fields +.Fa gl_matchc +and +.Fa gl_flags +should not be used by applications striving for strict +.Tn POSIX +conformance. +.Sh HISTORY +The +.Fn glob +and +.Fn globfree +functions first appeared in +.Bx 4.4 . +.Sh BUGS +Patterns longer than +.Dv MAXPATHLEN +may cause unchecked errors. +.Pp +The +.Fn glob +argument +may fail and set errno for any of the errors specified for the +library routines +.Xr stat 2 , +.Xr closedir 3 , +.Xr opendir 3 , +.Xr readdir 3 , +.Xr malloc 3 , +and +.Xr free 3 . diff --git a/lib/libc/gen/glob.c b/lib/libc/gen/glob.c new file mode 100644 index 0000000..09dfd7a --- /dev/null +++ b/lib/libc/gen/glob.c @@ -0,0 +1,864 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Guido van Rossum. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; +#endif /* LIBC_SCCS and not lint */ + +/* + * glob(3) -- a superset of the one defined in POSIX 1003.2. + * + * The [!...] convention to negate a range is supported (SysV, Posix, ksh). + * + * Optional extra services, controlled by flags not defined by POSIX: + * + * GLOB_QUOTE: + * Escaping convention: \ inhibits any special meaning the following + * character might have (except \ at end of string is retained). + * GLOB_MAGCHAR: + * Set in gl_flags if pattern contained a globbing character. + * GLOB_NOMAGIC: + * Same as GLOB_NOCHECK, but it will only append pattern if it did + * not contain any magic characters. [Used in csh style globbing] + * GLOB_ALTDIRFUNC: + * Use alternately specified directory access functions. + * GLOB_TILDE: + * expand ~user/foo to the /home/dir/of/user/foo + * GLOB_BRACE: + * expand {1,2}{a,b} to 1a 1b 2a 2b + * gl_matchc: + * Number of matches in the current invocation of glob. + */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <ctype.h> +#include <dirent.h> +#include <errno.h> +#include <glob.h> +#include <pwd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "collate.h" + +#define DOLLAR '$' +#define DOT '.' +#define EOS '\0' +#define LBRACKET '[' +#define NOT '!' +#define QUESTION '?' +#define QUOTE '\\' +#define RANGE '-' +#define RBRACKET ']' +#define SEP '/' +#define STAR '*' +#define TILDE '~' +#define UNDERSCORE '_' +#define LBRACE '{' +#define RBRACE '}' +#define SLASH '/' +#define COMMA ',' + +#ifndef DEBUG + +#define M_QUOTE 0x8000 +#define M_PROTECT 0x4000 +#define M_MASK 0xffff +#define M_ASCII 0x00ff + +typedef u_short Char; + +#else + +#define M_QUOTE 0x80 +#define M_PROTECT 0x40 +#define M_MASK 0xff +#define M_ASCII 0x7f + +typedef char Char; + +#endif + + +#define CHAR(c) ((Char)((c)&M_ASCII)) +#define META(c) ((Char)((c)|M_QUOTE)) +#define M_ALL META('*') +#define M_END META(']') +#define M_NOT META('!') +#define M_ONE META('?') +#define M_RNG META('-') +#define M_SET META('[') +#define ismeta(c) (((c)&M_QUOTE) != 0) + + +static int compare __P((const void *, const void *)); +static void g_Ctoc __P((const Char *, char *)); +static int g_lstat __P((Char *, struct stat *, glob_t *)); +static DIR *g_opendir __P((Char *, glob_t *)); +static Char *g_strchr __P((Char *, int)); +#ifdef notdef +static Char *g_strcat __P((Char *, const Char *)); +#endif +static int g_stat __P((Char *, struct stat *, glob_t *)); +static int glob0 __P((const Char *, glob_t *)); +static int glob1 __P((Char *, glob_t *)); +static int glob2 __P((Char *, Char *, Char *, glob_t *)); +static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *)); +static int globextend __P((const Char *, glob_t *)); +static const Char * globtilde __P((const Char *, Char *, size_t, glob_t *)); +static int globexp1 __P((const Char *, glob_t *)); +static int globexp2 __P((const Char *, const Char *, glob_t *, int *)); +static int match __P((Char *, Char *, Char *)); +#ifdef DEBUG +static void qprintf __P((const char *, Char *)); +#endif + +int +glob(pattern, flags, errfunc, pglob) + const char *pattern; + int flags, (*errfunc) __P((const char *, int)); + glob_t *pglob; +{ + const u_char *patnext; + int c; + Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; + + patnext = (u_char *) pattern; + if (!(flags & GLOB_APPEND)) { + pglob->gl_pathc = 0; + pglob->gl_pathv = NULL; + if (!(flags & GLOB_DOOFFS)) + pglob->gl_offs = 0; + } + pglob->gl_flags = flags & ~GLOB_MAGCHAR; + pglob->gl_errfunc = errfunc; + pglob->gl_matchc = 0; + + bufnext = patbuf; + bufend = bufnext + MAXPATHLEN; + if (flags & GLOB_QUOTE) { + /* Protect the quoted characters. */ + while (bufnext < bufend && (c = *patnext++) != EOS) + if (c == QUOTE) { + if ((c = *patnext++) == EOS) { + c = QUOTE; + --patnext; + } + *bufnext++ = c | M_PROTECT; + } + else + *bufnext++ = c; + } + else + while (bufnext < bufend && (c = *patnext++) != EOS) + *bufnext++ = c; + *bufnext = EOS; + + if (flags & GLOB_BRACE) + return globexp1(patbuf, pglob); + else + return glob0(patbuf, pglob); +} + +/* + * Expand recursively a glob {} pattern. When there is no more expansion + * invoke the standard globbing routine to glob the rest of the magic + * characters + */ +static int globexp1(pattern, pglob) + const Char *pattern; + glob_t *pglob; +{ + const Char* ptr = pattern; + int rv; + + /* Protect a single {}, for find(1), like csh */ + if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) + return glob0(pattern, pglob); + + while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL) + if (!globexp2(ptr, pattern, pglob, &rv)) + return rv; + + return glob0(pattern, pglob); +} + + +/* + * Recursive brace globbing helper. Tries to expand a single brace. + * If it succeeds then it invokes globexp1 with the new pattern. + * If it fails then it tries to glob the rest of the pattern and returns. + */ +static int globexp2(ptr, pattern, pglob, rv) + const Char *ptr, *pattern; + glob_t *pglob; + int *rv; +{ + int i; + Char *lm, *ls; + const Char *pe, *pm, *pl; + Char patbuf[MAXPATHLEN + 1]; + + /* copy part up to the brace */ + for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) + continue; + ls = lm; + + /* Find the balanced brace */ + for (i = 0, pe = ++ptr; *pe; pe++) + if (*pe == LBRACKET) { + /* Ignore everything between [] */ + for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) + continue; + if (*pe == EOS) { + /* + * We could not find a matching RBRACKET. + * Ignore and just look for RBRACE + */ + pe = pm; + } + } + else if (*pe == LBRACE) + i++; + else if (*pe == RBRACE) { + if (i == 0) + break; + i--; + } + + /* Non matching braces; just glob the pattern */ + if (i != 0 || *pe == EOS) { + *rv = glob0(patbuf, pglob); + return 0; + } + + for (i = 0, pl = pm = ptr; pm <= pe; pm++) + switch (*pm) { + case LBRACKET: + /* Ignore everything between [] */ + for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) + continue; + if (*pm == EOS) { + /* + * We could not find a matching RBRACKET. + * Ignore and just look for RBRACE + */ + pm = pl; + } + break; + + case LBRACE: + i++; + break; + + case RBRACE: + if (i) { + i--; + break; + } + /* FALLTHROUGH */ + case COMMA: + if (i && *pm == COMMA) + break; + else { + /* Append the current string */ + for (lm = ls; (pl < pm); *lm++ = *pl++) + continue; + /* + * Append the rest of the pattern after the + * closing brace + */ + for (pl = pe + 1; (*lm++ = *pl++) != EOS;) + continue; + + /* Expand the current pattern */ +#ifdef DEBUG + qprintf("globexp2:", patbuf); +#endif + *rv = globexp1(patbuf, pglob); + + /* move after the comma, to the next string */ + pl = pm + 1; + } + break; + + default: + break; + } + *rv = 0; + return 0; +} + + + +/* + * expand tilde from the passwd file. + */ +static const Char * +globtilde(pattern, patbuf, patbuf_len, pglob) + const Char *pattern; + Char *patbuf; + size_t patbuf_len; + glob_t *pglob; +{ + struct passwd *pwd; + char *h; + const Char *p; + Char *b, *eb; + + if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) + return pattern; + + /* + * Copy up to the end of the string or / + */ + eb = &patbuf[patbuf_len - 1]; + for (p = pattern + 1, h = (char *) patbuf; + h < (char *)eb && *p && *p != SLASH; *h++ = *p++) + continue; + + *h = EOS; + + if (((char *) patbuf)[0] == EOS) { + /* + * handle a plain ~ or ~/ by expanding $HOME first (iff + * we're not running setuid or setgid) and then trying + * the password file + */ + if ( +#ifndef __NETBSD_SYSCALLS + issetugid() != 0 || +#endif + (h = getenv("HOME")) == NULL) { + if (((h = getlogin()) != NULL && + (pwd = getpwnam(h)) != NULL) || + (pwd = getpwuid(getuid())) != NULL) + h = pwd->pw_dir; + else + return pattern; + } + } + else { + /* + * Expand a ~user + */ + if ((pwd = getpwnam((char*) patbuf)) == NULL) + return pattern; + else + h = pwd->pw_dir; + } + + /* Copy the home directory */ + for (b = patbuf; b < eb && *h; *b++ = *h++) + continue; + + /* Append the rest of the pattern */ + while (b < eb && (*b++ = *p++) != EOS) + continue; + *b = EOS; + + return patbuf; +} + + +/* + * The main glob() routine: compiles the pattern (optionally processing + * quotes), calls glob1() to do the real pattern matching, and finally + * sorts the list (unless unsorted operation is requested). Returns 0 + * if things went well, nonzero if errors occurred. It is not an error + * to find no matches. + */ +static int +glob0(pattern, pglob) + const Char *pattern; + glob_t *pglob; +{ + const Char *qpatnext; + int c, err, oldpathc; + Char *bufnext, patbuf[MAXPATHLEN+1]; + + qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char), + pglob); + oldpathc = pglob->gl_pathc; + bufnext = patbuf; + + /* We don't need to check for buffer overflow any more. */ + while ((c = *qpatnext++) != EOS) { + switch (c) { + case LBRACKET: + c = *qpatnext; + if (c == NOT) + ++qpatnext; + if (*qpatnext == EOS || + g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { + *bufnext++ = LBRACKET; + if (c == NOT) + --qpatnext; + break; + } + *bufnext++ = M_SET; + if (c == NOT) + *bufnext++ = M_NOT; + c = *qpatnext++; + do { + *bufnext++ = CHAR(c); + if (*qpatnext == RANGE && + (c = qpatnext[1]) != RBRACKET) { + *bufnext++ = M_RNG; + *bufnext++ = CHAR(c); + qpatnext += 2; + } + } while ((c = *qpatnext++) != RBRACKET); + pglob->gl_flags |= GLOB_MAGCHAR; + *bufnext++ = M_END; + break; + case QUESTION: + pglob->gl_flags |= GLOB_MAGCHAR; + *bufnext++ = M_ONE; + break; + case STAR: + pglob->gl_flags |= GLOB_MAGCHAR; + /* collapse adjacent stars to one, + * to avoid exponential behavior + */ + if (bufnext == patbuf || bufnext[-1] != M_ALL) + *bufnext++ = M_ALL; + break; + default: + *bufnext++ = CHAR(c); + break; + } + } + *bufnext = EOS; +#ifdef DEBUG + qprintf("glob0:", patbuf); +#endif + + if ((err = glob1(patbuf, pglob)) != 0) + return(err); + + /* + * If there was no match we are going to append the pattern + * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified + * and the pattern did not contain any magic characters + * GLOB_NOMAGIC is there just for compatibility with csh. + */ + if (pglob->gl_pathc == oldpathc && + ((pglob->gl_flags & GLOB_NOCHECK) || + ((pglob->gl_flags & GLOB_NOMAGIC) && + !(pglob->gl_flags & GLOB_MAGCHAR)))) + return(globextend(pattern, pglob)); + else if (!(pglob->gl_flags & GLOB_NOSORT)) + qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, + pglob->gl_pathc - oldpathc, sizeof(char *), compare); + return(0); +} + +static int +compare(p, q) + const void *p, *q; +{ + return(strcmp(*(char **)p, *(char **)q)); +} + +static int +glob1(pattern, pglob) + Char *pattern; + glob_t *pglob; +{ + Char pathbuf[MAXPATHLEN+1]; + + /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ + if (*pattern == EOS) + return(0); + return(glob2(pathbuf, pathbuf, pattern, pglob)); +} + +/* + * The functions glob2 and glob3 are mutually recursive; there is one level + * of recursion for each segment in the pattern that contains one or more + * meta characters. + */ +static int +glob2(pathbuf, pathend, pattern, pglob) + Char *pathbuf, *pathend, *pattern; + glob_t *pglob; +{ + struct stat sb; + Char *p, *q; + int anymeta; + + /* + * Loop over pattern segments until end of pattern or until + * segment with meta character found. + */ + for (anymeta = 0;;) { + if (*pattern == EOS) { /* End of pattern? */ + *pathend = EOS; + if (g_lstat(pathbuf, &sb, pglob)) + return(0); + + if (((pglob->gl_flags & GLOB_MARK) && + pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) + || (S_ISLNK(sb.st_mode) && + (g_stat(pathbuf, &sb, pglob) == 0) && + S_ISDIR(sb.st_mode)))) { + *pathend++ = SEP; + *pathend = EOS; + } + ++pglob->gl_matchc; + return(globextend(pathbuf, pglob)); + } + + /* Find end of next segment, copy tentatively to pathend. */ + q = pathend; + p = pattern; + while (*p != EOS && *p != SEP) { + if (ismeta(*p)) + anymeta = 1; + *q++ = *p++; + } + + if (!anymeta) { /* No expansion, do next segment. */ + pathend = q; + pattern = p; + while (*pattern == SEP) + *pathend++ = *pattern++; + } else /* Need expansion, recurse. */ + return(glob3(pathbuf, pathend, pattern, p, pglob)); + } + /* NOTREACHED */ +} + +static int +glob3(pathbuf, pathend, pattern, restpattern, pglob) + Char *pathbuf, *pathend, *pattern, *restpattern; + glob_t *pglob; +{ + register struct dirent *dp; + DIR *dirp; + int err; + char buf[MAXPATHLEN]; + + /* + * The readdirfunc declaration can't be prototyped, because it is + * assigned, below, to two functions which are prototyped in glob.h + * and dirent.h as taking pointers to differently typed opaque + * structures. + */ + struct dirent *(*readdirfunc)(); + + *pathend = EOS; + errno = 0; + + if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { + /* TODO: don't call for ENOENT or ENOTDIR? */ + if (pglob->gl_errfunc) { + g_Ctoc(pathbuf, buf); + if (pglob->gl_errfunc(buf, errno) || + pglob->gl_flags & GLOB_ERR) + return (GLOB_ABEND); + } + return(0); + } + + err = 0; + + /* Search directory for matching names. */ + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + readdirfunc = pglob->gl_readdir; + else + readdirfunc = readdir; + while ((dp = (*readdirfunc)(dirp))) { + register u_char *sc; + register Char *dc; + + /* Initial DOT must be matched literally. */ + if (dp->d_name[0] == DOT && *pattern != DOT) + continue; + for (sc = (u_char *) dp->d_name, dc = pathend; + (*dc++ = *sc++) != EOS;) + continue; + if (!match(pathend, pattern, restpattern)) { + *pathend = EOS; + continue; + } + err = glob2(pathbuf, --dc, restpattern, pglob); + if (err) + break; + } + + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + (*pglob->gl_closedir)(dirp); + else + closedir(dirp); + return(err); +} + + +/* + * Extend the gl_pathv member of a glob_t structure to accomodate a new item, + * add the new item, and update gl_pathc. + * + * This assumes the BSD realloc, which only copies the block when its size + * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic + * behavior. + * + * Return 0 if new item added, error code if memory couldn't be allocated. + * + * Invariant of the glob_t structure: + * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and + * gl_pathv points to (gl_offs + gl_pathc + 1) items. + */ +static int +globextend(path, pglob) + const Char *path; + glob_t *pglob; +{ + register char **pathv; + register int i; + u_int newsize; + char *copy; + const Char *p; + + newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); + pathv = pglob->gl_pathv ? + realloc((char *)pglob->gl_pathv, newsize) : + malloc(newsize); + if (pathv == NULL) + return(GLOB_NOSPACE); + + if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { + /* first time around -- clear initial gl_offs items */ + pathv += pglob->gl_offs; + for (i = pglob->gl_offs; --i >= 0; ) + *--pathv = NULL; + } + pglob->gl_pathv = pathv; + + for (p = path; *p++;) + continue; + if ((copy = malloc(p - path)) != NULL) { + g_Ctoc(path, copy); + pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; + } + pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; + return(copy == NULL ? GLOB_NOSPACE : 0); +} + +/* + * pattern matching function for filenames. Each occurrence of the * + * pattern causes a recursion level. + */ +static int +match(name, pat, patend) + register Char *name, *pat, *patend; +{ + int ok, negate_range; + Char c, k; + + while (pat < patend) { + c = *pat++; + switch (c & M_MASK) { + case M_ALL: + if (pat == patend) + return(1); + do + if (match(name, pat, patend)) + return(1); + while (*name++ != EOS); + return(0); + case M_ONE: + if (*name++ == EOS) + return(0); + break; + case M_SET: + ok = 0; + if ((k = *name++) == EOS) + return(0); + if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) + ++pat; + while (((c = *pat++) & M_MASK) != M_END) + if ((*pat & M_MASK) == M_RNG) { + if (__collate_load_error ? + CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) : + __collate_range_cmp(CHAR(c), CHAR(k)) <= 0 + && __collate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0 + ) + ok = 1; + pat += 2; + } else if (c == k) + ok = 1; + if (ok == negate_range) + return(0); + break; + default: + if (*name++ != c) + return(0); + break; + } + } + return(*name == EOS); +} + +/* Free allocated data belonging to a glob_t structure. */ +void +globfree(pglob) + glob_t *pglob; +{ + register int i; + register char **pp; + + if (pglob->gl_pathv != NULL) { + pp = pglob->gl_pathv + pglob->gl_offs; + for (i = pglob->gl_pathc; i--; ++pp) + if (*pp) + free(*pp); + free(pglob->gl_pathv); + } +} + +static DIR * +g_opendir(str, pglob) + register Char *str; + glob_t *pglob; +{ + char buf[MAXPATHLEN]; + + if (!*str) + strcpy(buf, "."); + else + g_Ctoc(str, buf); + + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + return((*pglob->gl_opendir)(buf)); + + return(opendir(buf)); +} + +static int +g_lstat(fn, sb, pglob) + register Char *fn; + struct stat *sb; + glob_t *pglob; +{ + char buf[MAXPATHLEN]; + + g_Ctoc(fn, buf); + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + return((*pglob->gl_lstat)(buf, sb)); + return(lstat(buf, sb)); +} + +static int +g_stat(fn, sb, pglob) + register Char *fn; + struct stat *sb; + glob_t *pglob; +{ + char buf[MAXPATHLEN]; + + g_Ctoc(fn, buf); + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + return((*pglob->gl_stat)(buf, sb)); + return(stat(buf, sb)); +} + +static Char * +g_strchr(str, ch) + Char *str; + int ch; +{ + do { + if (*str == ch) + return (str); + } while (*str++); + return (NULL); +} + +#ifdef notdef +static Char * +g_strcat(dst, src) + Char *dst; + const Char* src; +{ + Char *sdst = dst; + + while (*dst++) + continue; + --dst; + while((*dst++ = *src++) != EOS) + continue; + + return (sdst); +} +#endif + +static void +g_Ctoc(str, buf) + register const Char *str; + char *buf; +{ + register char *dc; + + for (dc = buf; (*dc++ = *str++) != EOS;) + continue; +} + +#ifdef DEBUG +static void +qprintf(str, s) + const char *str; + register Char *s; +{ + register Char *p; + + (void)printf("%s:\n", str); + for (p = s; *p; p++) + (void)printf("%c", CHAR(*p)); + (void)printf("\n"); + for (p = s; *p; p++) + (void)printf("%c", *p & M_PROTECT ? '"' : ' '); + (void)printf("\n"); + for (p = s; *p; p++) + (void)printf("%c", ismeta(*p) ? '_' : ' '); + (void)printf("\n"); +} +#endif diff --git a/lib/libc/gen/initgroups.3 b/lib/libc/gen/initgroups.3 new file mode 100644 index 0000000..5ed1e06 --- /dev/null +++ b/lib/libc/gen/initgroups.3 @@ -0,0 +1,84 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)initgroups.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt INITGROUPS 3 +.Os BSD 4.2 +.Sh NAME +.Nm initgroups +.Nd initialize group access list +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft int +.Fn initgroups "const char *name" "int basegid" +.Sh DESCRIPTION +The +.Fn initgroups +function +uses the +.Xr getgrouplist 3 +function to calculate the group access list for the user +specified in +.Fa name . +This group list is then setup for the current process using +.Xr setgroups 2 . +The +.Fa basegid +is automatically included in the groups list. +Typically this value is given as +the group number from the password file. +.Sh RETURN VALUES +The +.Fn initgroups +function +returns \-1 if it was not invoked by the super-user. +.Sh SEE ALSO +.Xr setgroups 2 , +.Xr getgrouplist 3 +.Sh HISTORY +The +.Fn initgroups +function appeared in +.Bx 4.2 . +.Sh BUGS +The +.Fn getgrouplist +function called by +.Fn initgroups +uses the routines based on +.Xr getgrent 3 . +If the invoking program uses any of these routines, +the group structure will +be overwritten in the call to +.Fn initgroups . diff --git a/lib/libc/gen/initgroups.c b/lib/libc/gen/initgroups.c new file mode 100644 index 0000000..b1ddd86 --- /dev/null +++ b/lib/libc/gen/initgroups.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)initgroups.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> + +#include <stdio.h> +#include <err.h> +#include <unistd.h> + +int +initgroups(uname, agroup) + const char *uname; + int agroup; +{ + int groups[NGROUPS], ngroups; + + ngroups = NGROUPS; + if (getgrouplist(uname, agroup, groups, &ngroups) < 0) + warnx("%s is in too many groups, using first %d", + uname, ngroups); + if (setgroups(ngroups, groups) < 0) { + warn("setgroups"); + return (-1); + } + return (0); +} diff --git a/lib/libc/gen/isatty.c b/lib/libc/gen/isatty.c new file mode 100644 index 0000000..f14f470 --- /dev/null +++ b/lib/libc/gen/isatty.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)isatty.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <termios.h> +#include <unistd.h> +#ifdef _THREAD_SAFE +#include <pthread.h> +#include "pthread_private.h" +#endif + +int +isatty(fd) + int fd; +{ + int retval; + struct termios t; + +#ifdef _THREAD_SAFE + if (_FD_LOCK(fd, FD_READ, NULL) == 0) { +#endif + retval = (tcgetattr(fd, &t) != -1); +#ifdef _THREAD_SAFE + _FD_UNLOCK(fd, FD_READ); + } else { + retval = 0; + } +#endif + return(retval); +} diff --git a/lib/libc/gen/isinf.3 b/lib/libc/gen/isinf.3 new file mode 100644 index 0000000..592bd04 --- /dev/null +++ b/lib/libc/gen/isinf.3 @@ -0,0 +1,73 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)isinf.3 8.2 (Berkeley) 1/29/94 +.\" $FreeBSD$ +.\" +.Dd January 29, 1994 +.Dt ISINF 3 +.Os +.Sh NAME +.Nm isinf , +.Nm isnan +.Nd test for infinity or not-a-number +.Sh SYNOPSIS +.Ft int +.Fn isinf double +.Ft int +.Fn isnan double +.Sh DESCRIPTION +The +.Fn isinf +function +returns 1 if the number is +.Dq \\*(If , +otherwise 0. +.Pp +The +.Fn isnan +function +returns 1 if the number is +.Dq not-a-number , +otherwise 0. +.Sh SEE ALSO +.Xr math 3 +.Rs +.%T "IEEE Standard for Binary Floating-Point Arithmetic" +.%Q ANSI +.%R Std 754-1985 +.Re +.Sh BUGS +Neither the +.Tn VAX +nor the Tahoe floating point have distinguished values +for either infinity or not-a-number. +These routines always return 0 on those architectures. diff --git a/lib/libc/gen/jrand48.c b/lib/libc/gen/jrand48.c new file mode 100644 index 0000000..051d5a6 --- /dev/null +++ b/lib/libc/gen/jrand48.c @@ -0,0 +1,21 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +long +jrand48(unsigned short xseed[3]) +{ + _dorand48(xseed); + return ((long) xseed[2] << 16) + (long) xseed[1]; +} diff --git a/lib/libc/gen/lcong48.c b/lib/libc/gen/lcong48.c new file mode 100644 index 0000000..f13826b --- /dev/null +++ b/lib/libc/gen/lcong48.c @@ -0,0 +1,30 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +extern unsigned short _rand48_seed[3]; +extern unsigned short _rand48_mult[3]; +extern unsigned short _rand48_add; + +void +lcong48(unsigned short p[7]) +{ + _rand48_seed[0] = p[0]; + _rand48_seed[1] = p[1]; + _rand48_seed[2] = p[2]; + _rand48_mult[0] = p[3]; + _rand48_mult[1] = p[4]; + _rand48_mult[2] = p[5]; + _rand48_add = p[6]; +} diff --git a/lib/libc/gen/ldexp.3 b/lib/libc/gen/ldexp.3 new file mode 100644 index 0000000..8a0ffef --- /dev/null +++ b/lib/libc/gen/ldexp.3 @@ -0,0 +1,78 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)ldexp.3 8.2 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd April 19, 1994 +.Dt LDEXP 3 +.Os +.Sh NAME +.Nm ldexp +.Nd multiply floating-point number by integral power of 2 +.Sh SYNOPSIS +.Fd #include <math.h> +.Ft double +.Fn ldexp "double x" "int exp" +.Sh DESCRIPTION +The +.Fn ldexp +function multiplies a floating-point number by an integral +power of 2. +.Sh RETURN VALUES +The +.Fn ldexp +function returns the value of +.Fa x +times 2 raised to the power +.Fa exp . +.Pp +If the resultant value would cause an overflow, +the global variable +.Va errno +is set to +.Er ERANGE +and the value +.Dv HUGE +is returned. +.Sh SEE ALSO +.Xr frexp 3 , +.Xr math 3 , +.Xr modf 3 +.Sh STANDARDS +The +.Fn ldexp +function conforms to +.St -ansiC . diff --git a/lib/libc/gen/lockf.3 b/lib/libc/gen/lockf.3 index 6094ff1..1cfc40a 100644 --- a/lib/libc/gen/lockf.3 +++ b/lib/libc/gen/lockf.3 @@ -34,14 +34,14 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" +.\" $FreeBSD$ +.\" .Dd December 19, 1997 .Dt LOCKF 3 .Os NetBSD 1.4 .Sh NAME .Nm lockf .Nd record locking on files -.Sh LIBRARY -.Lb libc .Sh SYNOPSIS .Fd #include <unistd.h> .Ft int diff --git a/lib/libc/gen/lockf.c b/lib/libc/gen/lockf.c index 0419390..3363d58 100644 --- a/lib/libc/gen/lockf.c +++ b/lib/libc/gen/lockf.c @@ -36,21 +36,15 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/cdefs.h> #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: lockf.c,v 1.1 1997/12/20 20:23:18 kleink Exp $"); +static const char rcsid[]= + "$FreeBSD$"; #endif -#include "namespace.h" #include <errno.h> #include <fcntl.h> #include <unistd.h> -#ifdef __weak_alias -__weak_alias(lockf,_lockf); -#endif - - int lockf(filedes, function, size) int filedes; diff --git a/lib/libc/gen/lrand48.c b/lib/libc/gen/lrand48.c new file mode 100644 index 0000000..a3d0111 --- /dev/null +++ b/lib/libc/gen/lrand48.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +extern unsigned short _rand48_seed[3]; + +long +lrand48(void) +{ + _dorand48(_rand48_seed); + return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >> 1); +} diff --git a/lib/libc/gen/modf.3 b/lib/libc/gen/modf.3 new file mode 100644 index 0000000..c2941a8 --- /dev/null +++ b/lib/libc/gen/modf.3 @@ -0,0 +1,73 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)modf.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt MODF 3 +.Os +.Sh NAME +.Nm modf +.Nd extract signed integral and fractional values from floating-point number +.Sh SYNOPSIS +.Fd #include <math.h> +.Ft double +.Fn modf "double value" "double *iptr" +.Sh DESCRIPTION +The +.Fn modf +function breaks the argument +.Fa value +into integral and fractional parts, each of which has the +same sign as the argument. +It stores the integral part as a +.Em double +in the object pointed to by +.Fa iptr . +.Sh RETURN VALUES +The +.Fn modf +function returns the signed fractional part of +.Fa value . +.Sh SEE ALSO +.Xr frexp 3 , +.Xr ldexp 3 , +.Xr math 3 +.Sh STANDARDS +The +.Fn modf +function conforms to +.St -ansiC . diff --git a/lib/libc/gen/mrand48.c b/lib/libc/gen/mrand48.c new file mode 100644 index 0000000..b23db51 --- /dev/null +++ b/lib/libc/gen/mrand48.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +extern unsigned short _rand48_seed[3]; + +long +mrand48(void) +{ + _dorand48(_rand48_seed); + return ((long) _rand48_seed[2] << 16) + (long) _rand48_seed[1]; +} diff --git a/lib/libc/gen/msgctl.3 b/lib/libc/gen/msgctl.3 new file mode 100644 index 0000000..326e8a5 --- /dev/null +++ b/lib/libc/gen/msgctl.3 @@ -0,0 +1,210 @@ +.\" $NetBSD: msgctl.2,v 1.1 1995/10/16 23:49:15 jtc Exp $ +.\" +.\" Copyright (c) 1995 Frank van der Linden +.\" All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed for the NetBSD Project +.\" by Frank van der Linden +.\" 4. 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 ``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 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. +.\" +.\" $FreeBSD$ +.\"/ +.Dd November 24, 1997 +.Dt MSGCTL 3 +.Os FreeBSD +.Sh NAME +.Nm msgctl +.Nd message control operations +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <sys/ipc.h> +.Fd #include <sys/msg.h> +.Ft int +.Fn msgctl "int msqid" "int cmd" "struct msqid_ds *buf" +.Sh DESCRIPTION +The +.Fn msgctl +system call performs some control operations on the message queue specified +by +.Fa msqid . + +Each message queue has a data structure associated with it, parts of which +may be altered by +.Fn msgctl +and parts of which determine the actions of +.Fn msgctl . +The data structure is defined in +.Aq Pa sys/msg.h +and contains (amongst others) the following members: +.Bd -literal +struct msqid_ds { + struct ipc_perm msg_perm; /* msg queue permission bits */ + struct msg *msg_first; /* first message in the queue */ + struct msg *msg_last; /* last message in the queue */ + u_long msg_cbytes; /* number of bytes in use on the queue */ + u_long msg_qnum; /* number of msgs in the queue */ + u_long msg_qbytes; /* max # of bytes on the queue */ + pid_t msg_lspid; /* pid of last msgsnd() */ + pid_t msg_lrpid; /* pid of last msgrcv() */ + time_t msg_stime; /* time of last msgsnd() */ + long msg_pad1; + time_t msg_rtime; /* time of last msgrcv() */ + long msg_pad2; + time_t msg_ctime; /* time of last msgctl() */ + long msg_pad3; + long msg_pad4[4]; +}; +.Ed +.Pp +The +.Bf -literal +ipc_perm +.Ef +structure used inside the +.Bf -literal +shmid_ds +.Ef +structure is defined in +.Aq Pa sys/ipc.h +and looks like this: +.Bd -literal +struct ipc_perm { + ushort cuid; /* creator user id */ + ushort cgid; /* creator group id */ + ushort uid; /* user id */ + ushort gid; /* group id */ + ushort mode; /* r/w permission */ + ushort seq; /* sequence # (to generate unique msg/sem/shm id) */ + key_t key; /* user specified msg/sem/shm key */ +}; +.Ed +.Pp +The operation to be performed by +.Fn msgctl +is specified in +.Fa cmd +and is one of: +.Bl -tag -width IPC_RMIDX +.It Dv IPC_STAT +Gather information about the message queue and place it in the +structure pointed to by +.Fa buf . +.It Dv IPC_SET +Set the value of the +.Va msg_perm.uid , +.Va msg_perm.gid , +.Va msg_perm.mode +and +.Va msg_qbytes +fields in the structure associated with +.Fa msqid . +The values are taken from the corresponding fields in the structure +pointed to by +.Fa buf . +This operation can only be executed by the super-user, or a process that +has an effective user id equal to either +.Va msg_perm.cuid +or +.Va msg_perm.uid +in the data structure associated with the message queue. +The value of +.Va msg_qbytes +can only be increased by the super-user. Values for +.Va msg_qbytes +that exceed the system limit (MSGMNB from +.Aq Pa sys/msg.h ) +are silently truncated to that limit. + +.It Dv IPC_RMID +Remove the message queue specified by +.Fa msqid +and destroy the data associated with it. Only the super-user or a process +with an effective uid equal to the +.Va msg_perm.cuid +or +.Va msg_perm.uid +values in the data structure associated with the queue can do this. +.El + +The permission to read from or write to a message queue (see +.Xr msgsnd 3 +and +.Xr msgrcv 3 ) +is determined by the +.Va msg_perm.mode +field in the same way as is +done with files (see +.Xr chmod 2 ), +but the effective uid can match either the +.Va msg_perm.cuid +field or the +.Va msg_perm.uid +field, and the +effective gid can match either +.Va msg_perm.cgid +or +.Va msg_perm.gid . +.Sh RETURN VALUES +Upon successful completion, a value of 0 is returned. Otherwise, -1 is +returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +.Fn msgctl +will fail if: +.Bl -tag -width Er +.It Bq Er EPERM +.Fa cmd +is equal to IPC_SET or IPC_RMID and the caller is not the super-user, nor does +the effective uid match either the +.Va msg_perm.uid +or +.Va msg_perm.cuid +fields of the data structure associated with the message queue. + +An attempt is made to increase the value of +.Va msg_qbytes +through IPC_SET +but the caller is not the super-user. +.It Bq Er EACCES +The command is IPC_STAT +and the caller has no read permission for this message queue. +.It Bq Er EINVAL +.Fa msqid +is not a valid message queue identifier. + +.Va cmd +is not a valid command. +.It Bq Er EFAULT +.Fa buf +specifies an invalid address. +.El +.Sh SEE ALSO +.Xr msgget 3 , +.Xr msgrcv 3 , +.Xr msgsnd 3 +.Sh HISTORY +Message queues appeared in the first release of +.At V . diff --git a/lib/libc/gen/msgctl.c b/lib/libc/gen/msgctl.c new file mode 100644 index 0000000..7b74c51e --- /dev/null +++ b/lib/libc/gen/msgctl.c @@ -0,0 +1,15 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/msg.h> + +#if __STDC__ +int msgctl(int msqid, int cmd, struct msqid_ds *buf) +#else +int msgctl(msqid,cmd,buf) + int msqid; + int cmd; + caddr_t buf; +#endif +{ + return (msgsys(0, msqid, cmd, buf)); +} diff --git a/lib/libc/gen/msgget.3 b/lib/libc/gen/msgget.3 new file mode 100644 index 0000000..f68cd8a --- /dev/null +++ b/lib/libc/gen/msgget.3 @@ -0,0 +1,137 @@ +.\" $NetBSD: msgget.2,v 1.1 1995/10/16 23:49:19 jtc Exp $ +.\" +.\" Copyright (c) 1995 Frank van der Linden +.\" All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed for the NetBSD Project +.\" by Frank van der Linden +.\" 4. 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 ``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 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. +.\" $FreeBSD$ +.\" +.\"/ +.Dd August 17, 1995 +.Dt MSGGET 3 +.Os FreeBSD +.Sh NAME +.Nm msgget +.Nd get message queue +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <sys/ipc.h> +.Fd #include <sys/msg.h> +.Ft int +.Fn msgget "key_t key" "int msgflg" +.Sh DESCRIPTION +.Fn msgget +returns the message queue identifier associated with +.Fa key . +A message queue identifier is a unique integer greater than zero. +.Pp +A message queue is created if either +.Fa key +is equal to +.Dv IPC_PRIVATE , +or +.Fa key +does not have a message queue identifier associated with it, and the +.Dv IPC_CREAT +bit is set in +.Fa msgflg. +.Pp +If a new message queue is created, the data structure associated with it (the +.Va msqid_ds +structure, see +.Xr msgctl 3 ) +is initialized as follows: +.Bl -bullet +.It +.Va msg_perm.cuid +and +.Va msg_perm.uid +are set to the effective uid of the calling process. +.It +.Va msg_perm.gid +and +.Va msg_perm.cgid +are set to the effective gid of the calling process. +.It +.Va msg_perm.mode +is set to the lower 9 bits of +.Fa msgflg . +.It +.Va msg_cbytes , +.Va msg_qnum , +.Va msg_lspid , +.Va msg_lrpid , +.Va msg_rtime , +and +.Va msg_stime +are set to 0. +.It +.Va msg_qbytes +is set to the system wide maximum value for the number of bytes in a queue +.Pf ( Dv MSGMNB ) . +.It +.Va msg_ctime +is set to the current time. +.El +.Sh RETURN VALUES +Upon successful completion a positive message queue identifier is returned. +Otherwise, -1 is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +.Bl -tag -width Er +.It Bq Er EACCES +A message queue is already associated with +.Fa key +and the caller has no permission to access it. +.It Bq Er EEXIST +Both +.Dv IPC_CREAT +and +.Dv IPC_EXCL +are set in +.Fa msgflg , +and a message queue is already associated with +.Fa key . +.It Bq Er ENOSPC +A new message queue could not be created because the system limit for +the number of message queues has been reached. +.It Bq Er ENOENT +.Dv IPC_CREAT +was not set in +.Fa msgflg +and no message queue associated with +.Fa key +was found. +.El +.Sh SEE ALSO +.Xr msgctl 3 , +.Xr msgrcv 3 , +.Xr msgsnd 3 +.Sh HISTORY +Message queues appeared in the first release of +.At V . diff --git a/lib/libc/gen/msgget.c b/lib/libc/gen/msgget.c new file mode 100644 index 0000000..3e146b0 --- /dev/null +++ b/lib/libc/gen/msgget.c @@ -0,0 +1,14 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/msg.h> + +#if __STDC__ +int msgget(key_t key, int msgflg) +#else +int msgget(key,msgflg) + key_t key; + int msgflg; +#endif +{ + return (msgsys(1, key, msgflg)); +} diff --git a/lib/libc/gen/msgrcv.3 b/lib/libc/gen/msgrcv.3 new file mode 100644 index 0000000..e4aa45d --- /dev/null +++ b/lib/libc/gen/msgrcv.3 @@ -0,0 +1,209 @@ +.\" $NetBSD: msgrcv.2,v 1.1 1995/10/16 23:49:20 jtc Exp $ +.\" +.\" Copyright (c) 1995 Frank van der Linden +.\" All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed for the NetBSD Project +.\" by Frank van der Linden +.\" 4. 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 ``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 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. +.\" $FreeBSD$ +.\" +.\"/ +.Dd November 24, 1997 +.Dt MSGRCV 3 +.Os FreeBSD +.Sh NAME +.Nm msgrcv +.Nd receive a message from a message queue +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <sys/ipc.h> +.Fd #include <sys/msg.h> +.Ft int +.Fn msgrcv "int msqid" "void *msgp" "size_t msgsz" "long msgtyp" "int msgflg" +.Sh DESCRIPTION +The +.Fn msgrcv +function receives a message from the message queue specified in +.Fa msqid , +and places it into the structure pointed to by +.Fa msgp . +This structure should consist of the following members: +.Bd -literal + long mtype; /* message type */ + char mtext[1]; /* body of message */ +.Ed +.Pp +.Va mtype +is an integer greater than 0 that can be used for selecting messages, +.Va mtext +is an array of bytes, with a size up to that of the system limit +.Pf ( Dv MSGMAX ) . +.Pp +The value of +.Fa msgtyp +has one of the following meanings: +.Bl -bullet +.It +.Fa msgtyp +is greater than 0. The first message of type +.Fa msgtyp +will be received. +.It +.Fa msgtyp +is equal to 0. The first message on the queue will be received. +.It +.Fa msgtyp +is less than 0. The first message of the lowest message type that is +less than or equal to the absolute value of +.Fa msgtyp +will be received. +.El +.Pp +.Fa msgsz +specifies the maximum length of the requested message. If the received +message has a length greater than +.Fa msgsz +it will be silently truncated if the +.Dv MSG_NOERROR +flag is set in +.Fa msgflg , +otherwise an error will be returned. +.Pp +If no matching message is present on the message queue specified by +.Fa msqid , +the behavior of +.Fn msgrcv +depends on whether the +.Dv IPC_NOWAIT +flag is set in +.Fa msgflg +or not. If +.Dv IPC_NOWAIT +is set, +.Fn msgrcv +will immediately return a value of -1, and set +.Va errno +to +.Er EAGAIN . +If +.Dv IPC_NOWAIT +is not set, the calling process will be blocked +until: +.Bl -bullet +.It +A message of the requested type becomes available on the message queue. +.It +The message queue is removed, in which case -1 will be returned, and +.Va errno +set to +.Er EINVAL . +.It +A signal is received and caught. -1 is returned, and +.Va errno +set to +.Er EINTR . +.El +.Pp +If a message is successfully received, the data structure associated with +.Fa msqid +is updated as follows: +.Bl -bullet +.It +.Va msg_cbytes +is decremented by the size of the message. +.It +.Va msg_lrpid +is set to the pid of the caller. +.It +.Va msg_lrtime +is set to the current time. +.It +.Va msg_qnum +is decremented by 1. +.Sh RETURN VALUES +Upon successful completion, +.Fn msgrcv +returns the number of bytes received into the +.Va mtext +field of the structure pointed to by +.Fa msgp . +Otherwise, -1 is returned, and +.Va errno +set to indicate the error. +.Sh ERRORS +.Fn msgrcv +will fail if: +.Bl -tag -width Er +.It Bq Er EINVAL +.Fa msqid +is not a valid message queue identifier. +.Pp +The message queue was removed while +.Fn msgrcv +was waiting for a message of the requested type to become available on it. +.Pp +.Fa msgsz +is less than 0. +.It Bq Er E2BIG +A matching message was received, but its size was greater than +.Fa msgsz +and the +.Dv MSG_NOERROR +flag was not set in +.Fa msgflg . +.It Bq Er EACCES +The calling process does not have read access to the message queue. +.It Bq Er EFAULT +.Fa msgp +points to an invalid address. +.It Bq Er EINTR +The system call was interrupted by the delivery of a signal. +.It Bq Er EAGAIN +There is no message of the requested type available on the message queue, +and +.Dv IPC_NOWAIT +is set in +.Fa msgflg . +.Sh SEE ALSO +.Xr msgctl 3 , +.Xr msgget 3 , +.Xr msgsnd 3 +.Sh BUGS +.Tn NetBSD +and +.Tn FreeBSD +do not define the +.Er EIDRM +error value, which should be used in +the case of a removed message queue, nor the +.Er ENOMSG +value, which +should be used when no suitable message is available and +.Dv IPC_NOWAIT +is set. +.Sh HISTORY +Message queues appeared in the first release of +.At V . diff --git a/lib/libc/gen/msgrcv.c b/lib/libc/gen/msgrcv.c new file mode 100644 index 0000000..5c1e387 --- /dev/null +++ b/lib/libc/gen/msgrcv.c @@ -0,0 +1,17 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/msg.h> + +#if __STDC__ +int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg) +#else +int msgrcv(msqid, msgp, msgsz, msgtyp, msgflg) + int msqid; + void *msgp; + size_t msgsz; + long msgtyp; + int msgflg; +#endif +{ + return (msgsys(3, msqid, msgp, msgsz, msgtyp, msgflg)); +} diff --git a/lib/libc/gen/msgsnd.3 b/lib/libc/gen/msgsnd.3 new file mode 100644 index 0000000..8946c1c --- /dev/null +++ b/lib/libc/gen/msgsnd.3 @@ -0,0 +1,164 @@ +.\" $NetBSD: msgsnd.2,v 1.1 1995/10/16 23:49:24 jtc Exp $ +.\" +.\" Copyright (c) 1995 Frank van der Linden +.\" All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed for the NetBSD Project +.\" by Frank van der Linden +.\" 4. 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 ``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 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. +.\" $FreeBSD$ +.\" +.\"/ +.Dd November 24, 1997 +.Dt MSGSND 3 +.Os FreeBSD +.Sh NAME +.Nm msgsnd +.Nd send a message to a message queue +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <sys/ipc.h> +.Fd #include <sys/msg.h> +.Ft int +.Fn msgsnd "int msqid" "void *msgp" "size_t msgsz" "int msgflg" +.Sh DESCRIPTION +The +.Fn msgsnd +function sends a message from the message queue specified in +.Fa msqid . +.Fa msgp +points to a structure containing the message. This structure should +consist of the following members: +.Bd -literal + long mtype; /* message type */ + char mtext[1]; /* body of message */ +.Ed +.Pp +.Va mtype +is an integer greater than 0 that can be used for selecting messages (see +.Xr msgrcv 3 ) , +.Va mtext +is an array of bytes, with a size up to that of the system limit +.Pf ( Dv MSGMAX ) . +.Pp +If the number of bytes already on the message queue plus +.Fa msgsz +is bigger than the maximum number of bytes on the message queue +.Pf ( Va msg_qbytes , +see +.Xr msgctl 3 ) , +or the number of messages on all queues system-wide is already equal to +the system limit, +.Fa msgflg +determines the action of +.Fn msgsnd . +If +.Fa msgflg +has +.Dv IPC_NOWAIT +mask set in it, the call will return immediately. If +.Fa msgflg +does not have +.Dv IPC_NOWAIT +set in it, the call will block until: +.Bl -bullet +.It +The condition which caused the call to block does no longer exist. +The message will be sent. +.It +The message queue is removed, in which case -1 will be returned, and +.Va errno +is set to +.Er EINVAL . +.It +The caller catches a signal. The call returns with +.Va errno +set to +.Er EINTR . +.El +.Pp +After a successful call, the data structure associated with the message +queue is updated in the following way: +.Bl -bullet +.It +.Va msg_cbytes +is incremented by the size of the message. +.It +.Va msg_qnum +is incremented by 1. +.It +.Va msg_lspid +is set to the pid of the calling process. +.It +.Va msg_stime +is set to the current time. +.El +.Sh RETURN VALUES +Upon successful completion, 0 is returned. Otherwise, -1 is returned and +.Va errno +is set to indicate the error. +.Sh ERRORS +.Fn msgsnd +will fail if: +.Bl -tag -width Er +.It Bq Er EINVAL +.Fa msqid +is not a valid message queue identifier +.Pp +The message queue was removed while +.Fn msgsnd +was waiting for a resource to become available in order to deliver the +message. +.Pp +.Fa msgsz +is less than 0, or greater than +.Va msg_qbytes . +.Pp +.Fa mtype +is not greater than 0. +.It Bq Er EACCES +The calling process does not have write access to the message queue. +.It Bq Er EAGAIN +There was no space for this message either on the queue, or in the whole +system, and +.Dv IPC_NOWAIT +was set in +.Fa msgflg . +.It Bq Er EFAULT +.Fa msgp +points to an invalid address. +.It Bq Er EINTR +The system call was interrupted by the delivery of a signal. +.El +.Sh BUGS +.Tn NetBSD +and +.Tn FreeBSD +do not define the +.Er EIDRM +error value, which should be used +in the case of a removed message queue. +.Sh HISTORY +Message queues appeared in the first release of AT&T Unix System V. diff --git a/lib/libc/gen/msgsnd.c b/lib/libc/gen/msgsnd.c new file mode 100644 index 0000000..a620915 --- /dev/null +++ b/lib/libc/gen/msgsnd.c @@ -0,0 +1,16 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/msg.h> + +#if __STDC__ +int msgsnd(int msqid, void *msgp, size_t msgsz, int msgflg) +#else +int msgsnd(msqid, msgp, msgsz, msgflg) + int msqid; + void *msgp; + size_t msgsz; + int msgflg; +#endif +{ + return (msgsys(2, msqid, msgp, msgsz, msgflg)); +} diff --git a/lib/libc/gen/nice.3 b/lib/libc/gen/nice.3 new file mode 100644 index 0000000..d61ec2e --- /dev/null +++ b/lib/libc/gen/nice.3 @@ -0,0 +1,70 @@ +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)nice.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt NICE 3 +.Os BSD 4 +.Sh NAME +.Nm nice +.Nd set program scheduling priority +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft int +.Fn nice "int incr" +.Sh DESCRIPTION +.Bf -symbolic +This interface is obsoleted by setpriority(2). +.Ef +.Pp +The +.Fn nice +function obtains the scheduling priority of the process +from the system and sets it to the priority value specified in +.Fa incr . +The priority is a value in the range -20 to 20. +The default priority is 0; lower priorities cause more favorable scheduling. +Only the super-user may lower priorities. +.Pp +Children inherit the priority of their parent processes via +.Xr fork 2 . +.Sh SEE ALSO +.Xr nice 1 , +.Xr fork 2 , +.Xr setpriority 2 , +.Xr renice 8 +.Sh HISTORY +A +.Fn nice +syscall appeared in +.At v6 . diff --git a/lib/libc/gen/nice.c b/lib/libc/gen/nice.c new file mode 100644 index 0000000..104c1a2 --- /dev/null +++ b/lib/libc/gen/nice.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)nice.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <errno.h> +#include <unistd.h> + +/* + * Backwards compatible nice. + */ +int +nice(incr) + int incr; +{ + int prio; + + errno = 0; + prio = getpriority(PRIO_PROCESS, 0); + if (prio == -1 && errno) + return (-1); + return (setpriority(PRIO_PROCESS, 0, prio + incr)); +} diff --git a/lib/libc/gen/nlist.3 b/lib/libc/gen/nlist.3 new file mode 100644 index 0000000..69add4a --- /dev/null +++ b/lib/libc/gen/nlist.3 @@ -0,0 +1,79 @@ +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)nlist.3 8.3 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd April 19, 1994 +.Dt NLIST 3 +.Os BSD 4 +.Sh NAME +.Nm nlist +.Nd retrieve symbol table name list from an executable file +.Sh SYNOPSIS +.Fd #include <nlist.h> +.Ft int +.Fn nlist "const char *filename" "struct nlist *nl" +.Sh DESCRIPTION +The +.Fn nlist +function +retrieves name list entries from the symbol table of an +executable file (see +.Xr a.out 5 ) . +The argument +.Fa \&nl +is set to reference the +beginning of the list. +The list is preened of binary and invalid data; +if an entry in the +name list is valid, the +.Fa n_type +and +.Fa n_value +for the entry are copied into the list +referenced by +.Fa \&nl . +No other data is copied. +The last entry in the list is always +.Dv NULL . +.Sh RETURN VALUES +The number of invalid entries is returned if successful; otherwise, +if the file +.Fa filename +does not exist or is not executable, the returned value is \-1. +.Sh SEE ALSO +.Xr a.out 5 +.Sh HISTORY +A +.Fn nlist +function appeared in +.At v6 . diff --git a/lib/libc/gen/nlist.c b/lib/libc/gen/nlist.c new file mode 100644 index 0000000..55f4f39 --- /dev/null +++ b/lib/libc/gen/nlist.c @@ -0,0 +1,413 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/file.h> + +#include <errno.h> +#include <a.out.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#define _NLIST_DO_AOUT +#define _NLIST_DO_ELF + +#ifdef _NLIST_DO_ELF +#include <elf.h> +#endif + +int __fdnlist __P((int, struct nlist *)); +int __aout_fdnlist __P((int, struct nlist *)); +int __elf_fdnlist __P((int, struct nlist *)); + +int +nlist(name, list) + const char *name; + struct nlist *list; +{ + int fd, n; + + fd = open(name, O_RDONLY, 0); + if (fd < 0) + return (-1); + n = __fdnlist(fd, list); + (void)close(fd); + return (n); +} + +static struct nlist_handlers { + int (*fn) __P((int fd, struct nlist *list)); +} nlist_fn[] = { +#ifdef _NLIST_DO_AOUT + { __aout_fdnlist }, +#endif +#ifdef _NLIST_DO_ELF + { __elf_fdnlist }, +#endif +}; + +int +__fdnlist(fd, list) + register int fd; + register struct nlist *list; +{ + int n = -1, i; + + for (i = 0; i < sizeof(nlist_fn) / sizeof(nlist_fn[0]); i++) { + n = (nlist_fn[i].fn)(fd, list); + if (n != -1) + break; + } + return (n); +} + +#define ISLAST(p) (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0) + +#ifdef _NLIST_DO_AOUT +int +__aout_fdnlist(fd, list) + register int fd; + register struct nlist *list; +{ + register struct nlist *p, *symtab; + register caddr_t strtab, a_out_mmap; + register off_t stroff, symoff; + register u_long symsize; + register int nent; + struct exec * exec; + struct stat st; + + /* check that file is at least as large as struct exec! */ + if ((fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec))) + return (-1); + + /* Check for files too large to mmap. */ + if (st.st_size > SIZE_T_MAX) { + errno = EFBIG; + return (-1); + } + + /* + * Map the whole a.out file into our address space. + * We then find the string table withing this area. + * We do not just mmap the string table, as it probably + * does not start at a page boundary - we save ourselves a + * lot of nastiness by mmapping the whole file. + * + * This gives us an easy way to randomly access all the strings, + * without making the memory allocation permanent as with + * malloc/free (i.e., munmap will return it to the system). + */ + a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0); + if (a_out_mmap == MAP_FAILED) + return (-1); + + exec = (struct exec *)a_out_mmap; + if (N_BADMAG(*exec)) { + munmap(a_out_mmap, (size_t)st.st_size); + return (-1); + } + + symoff = N_SYMOFF(*exec); + symsize = exec->a_syms; + stroff = symoff + symsize; + + /* find the string table in our mmapped area */ + strtab = a_out_mmap + stroff; + symtab = (struct nlist *)(a_out_mmap + symoff); + + /* + * clean out any left-over information for all valid entries. + * Type and value defined to be 0 if not found; historical + * versions cleared other and desc as well. Also figure out + * the largest string length so don't read any more of the + * string table than we have to. + * + * XXX clearing anything other than n_type and n_value violates + * the semantics given in the man page. + */ + nent = 0; + for (p = list; !ISLAST(p); ++p) { + p->n_type = 0; + p->n_other = 0; + p->n_desc = 0; + p->n_value = 0; + ++nent; + } + + while (symsize > 0) { + register int soff; + + symsize-= sizeof(struct nlist); + soff = symtab->n_un.n_strx; + + + if (soff != 0 && (symtab->n_type & N_STAB) == 0) + for (p = list; !ISLAST(p); p++) + if (!strcmp(&strtab[soff], p->n_un.n_name)) { + p->n_value = symtab->n_value; + p->n_type = symtab->n_type; + p->n_desc = symtab->n_desc; + p->n_other = symtab->n_other; + if (--nent <= 0) + break; + } + symtab++; + } + munmap(a_out_mmap, (size_t)st.st_size); + return (nent); +} +#endif + +#ifdef _NLIST_DO_ELF +static void elf_sym_to_nlist __P((struct nlist *, Elf_Sym *, Elf_Shdr *, int)); + +/* + * __elf_is_okay__ - Determine if ehdr really + * is ELF and valid for the target platform. + * + * WARNING: This is NOT a ELF ABI function and + * as such it's use should be restricted. + */ +int +__elf_is_okay__(ehdr) + register Elf_Ehdr *ehdr; +{ + register int retval = 0; + /* + * We need to check magic, class size, endianess, + * and version before we look at the rest of the + * Elf_Ehdr structure. These few elements are + * represented in a machine independant fashion. + */ + if (IS_ELF(*ehdr) && + ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS && + ehdr->e_ident[EI_DATA] == ELF_TARG_DATA && + ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) { + + /* Now check the machine dependant header */ + if (ehdr->e_machine == ELF_TARG_MACH && + ehdr->e_version == ELF_TARG_VER) + retval = 1; + } + return retval; +} + +int +__elf_fdnlist(fd, list) + register int fd; + register struct nlist *list; +{ + register struct nlist *p; + register Elf_Off symoff = 0, symstroff = 0; + register Elf_Word symsize = 0, symstrsize = 0; + register Elf_Sword cc, i; + int nent = -1; + int errsave; + Elf_Sym sbuf[1024]; + Elf_Sym *s; + Elf_Ehdr ehdr; + char *strtab = NULL; + Elf_Shdr *shdr = NULL; + Elf_Shdr *sh; + Elf_Word shdr_size; + void *base; + struct stat st; + + /* Make sure obj is OK */ + if (lseek(fd, (off_t)0, SEEK_SET) == -1 || + read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) || + !__elf_is_okay__(&ehdr) || + fstat(fd, &st) < 0) + return (-1); + + /* calculate section header table size */ + shdr_size = ehdr.e_shentsize * ehdr.e_shnum; + + /* Make sure it's not too big to mmap */ + if (shdr_size > SIZE_T_MAX) { + errno = EFBIG; + return (-1); + } + + /* mmap section header table */ + base = mmap(NULL, (size_t)shdr_size, PROT_READ, 0, fd, + (off_t)ehdr.e_shoff); + if (base == MAP_FAILED) + return (-1); + shdr = (Elf_Shdr *)base; + + /* + * Find the symbol table entry and it's corresponding + * string table entry. Version 1.1 of the ABI states + * that there is only one symbol table but that this + * could change in the future. + */ + for (i = 0; i < ehdr.e_shnum; i++) { + if (shdr[i].sh_type == SHT_SYMTAB) { + symoff = shdr[i].sh_offset; + symsize = shdr[i].sh_size; + symstroff = shdr[shdr[i].sh_link].sh_offset; + symstrsize = shdr[shdr[i].sh_link].sh_size; + break; + } + } + + /* Check for files too large to mmap. */ + if (symstrsize > SIZE_T_MAX) { + errno = EFBIG; + goto done; + } + /* + * Map string table into our address space. This gives us + * an easy way to randomly access all the strings, without + * making the memory allocation permanent as with malloc/free + * (i.e., munmap will return it to the system). + */ + base = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd, + (off_t)symstroff); + if (base == MAP_FAILED) + goto done; + strtab = (char *)base; + + /* + * clean out any left-over information for all valid entries. + * Type and value defined to be 0 if not found; historical + * versions cleared other and desc as well. Also figure out + * the largest string length so don't read any more of the + * string table than we have to. + * + * XXX clearing anything other than n_type and n_value violates + * the semantics given in the man page. + */ + nent = 0; + for (p = list; !ISLAST(p); ++p) { + p->n_type = 0; + p->n_other = 0; + p->n_desc = 0; + p->n_value = 0; + ++nent; + } + + /* Don't process any further if object is stripped. */ + if (symoff == 0) + goto done; + + if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) { + nent = -1; + goto done; + } + + while (symsize > 0 && nent > 0) { + cc = MIN(symsize, sizeof(sbuf)); + if (read(fd, sbuf, cc) != cc) + break; + symsize -= cc; + for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) { + char *name; + struct nlist *p; + + name = strtab + s->st_name; + if (name[0] == '\0') + continue; + for (p = list; !ISLAST(p); p++) { + if ((p->n_un.n_name[0] == '_' && + strcmp(name, p->n_un.n_name+1) == 0) + || strcmp(name, p->n_un.n_name) == 0) { + elf_sym_to_nlist(p, s, shdr, + ehdr.e_shnum); + if (--nent <= 0) + break; + } + } + } + } + done: + errsave = errno; + if (strtab != NULL) + munmap(strtab, symstrsize); + if (shdr != NULL) + munmap(shdr, shdr_size); + errno = errsave; + return (nent); +} + +/* + * Convert an Elf_Sym into an nlist structure. This fills in only the + * n_value and n_type members. + */ +static void +elf_sym_to_nlist(nl, s, shdr, shnum) + struct nlist *nl; + Elf_Sym *s; + Elf_Shdr *shdr; + int shnum; +{ + nl->n_value = s->st_value; + + switch (s->st_shndx) { + case SHN_UNDEF: + case SHN_COMMON: + nl->n_type = N_UNDF; + break; + case SHN_ABS: + nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ? + N_FN : N_ABS; + break; + default: + if (s->st_shndx >= shnum) + nl->n_type = N_UNDF; + else { + Elf_Shdr *sh = shdr + s->st_shndx; + + nl->n_type = sh->sh_type == SHT_PROGBITS ? + (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) : + (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF); + } + break; + } + + if (ELF_ST_BIND(s->st_info) == STB_GLOBAL || + ELF_ST_BIND(s->st_info) == STB_WEAK) + nl->n_type |= N_EXT; +} +#endif /* _NLIST_DO_ELF */ diff --git a/lib/libc/gen/nrand48.c b/lib/libc/gen/nrand48.c new file mode 100644 index 0000000..6c54065 --- /dev/null +++ b/lib/libc/gen/nrand48.c @@ -0,0 +1,21 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +long +nrand48(unsigned short xseed[3]) +{ + _dorand48(xseed); + return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1); +} diff --git a/lib/libc/gen/ntp_gettime.c b/lib/libc/gen/ntp_gettime.c new file mode 100644 index 0000000..3bb71c1 --- /dev/null +++ b/lib/libc/gen/ntp_gettime.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> +#include <sys/time.h> +#include <sys/timex.h> + +int +ntp_gettime(struct ntptimeval *ntv) +{ + struct ntptimeval tv; + size_t size = sizeof tv; + + if (sysctlbyname("kern.ntp_pll.gettime", &tv, &size, NULL, 0) == -1) + return TIME_ERROR; + if(ntv) *ntv = tv; + return tv.time_state; +} + diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c new file mode 100644 index 0000000..ff01646 --- /dev/null +++ b/lib/libc/gen/opendir.c @@ -0,0 +1,274 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)opendir.c 8.8 (Berkeley) 5/1/95"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/mount.h> +#include <sys/stat.h> + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <unistd.h> + +/* + * Open a directory. + */ +DIR * +opendir(name) + const char *name; +{ + + return (__opendir2(name, DTF_HIDEW|DTF_NODUP)); +} + +DIR * +__opendir2(name, flags) + const char *name; + int flags; +{ + DIR *dirp; + int fd; + int incr; + int saved_errno; + int unionstack; + struct stat statb; + + /* + * stat() before open() because opening of special files may be + * harmful. fstat() after open because the file may have changed. + */ + if (stat(name, &statb) != 0) + return (NULL); + if (!S_ISDIR(statb.st_mode)) { + errno = ENOTDIR; + return (NULL); + } + if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1) + return (NULL); + dirp = NULL; + if (fstat(fd, &statb) != 0) + goto fail; + if (!S_ISDIR(statb.st_mode)) { + errno = ENOTDIR; + goto fail; + } + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || + (dirp = malloc(sizeof(DIR))) == NULL) + goto fail; + + /* + * Use the system page size if that is a multiple of DIRBLKSIZ. + * Hopefully this can be a big win someday by allowing page + * trades to user space to be done by getdirentries(). + */ + incr = getpagesize(); + if ((incr % DIRBLKSIZ) != 0) + incr = DIRBLKSIZ; + + /* + * Determine whether this directory is the top of a union stack. + */ + if (flags & DTF_NODUP) { + struct statfs sfb; + + if (fstatfs(fd, &sfb) < 0) + goto fail; + unionstack = !strcmp(sfb.f_fstypename, "union"); + } else { + unionstack = 0; + } + + if (unionstack) { + int len = 0; + int space = 0; + char *buf = 0; + char *ddptr = 0; + char *ddeptr; + int n; + struct dirent **dpv; + + /* + * The strategy here is to read all the directory + * entries into a buffer, sort the buffer, and + * remove duplicate entries by setting the inode + * number to zero. + */ + + do { + /* + * Always make at least DIRBLKSIZ bytes + * available to getdirentries + */ + if (space < DIRBLKSIZ) { + space += incr; + len += incr; + buf = reallocf(buf, len); + if (buf == NULL) + goto fail; + ddptr = buf + (len - space); + } + + n = getdirentries(fd, ddptr, space, &dirp->dd_seek); + if (n > 0) { + ddptr += n; + space -= n; + } + } while (n > 0); + + ddeptr = ddptr; + flags |= __DTF_READALL; + + /* + * Re-open the directory. + * This has the effect of rewinding back to the + * top of the union stack and is needed by + * programs which plan to fchdir to a descriptor + * which has also been read -- see fts.c. + */ + if (flags & DTF_REWIND) { + (void) close(fd); + if ((fd = open(name, O_RDONLY)) == -1) { + saved_errno = errno; + free(buf); + free(dirp); + errno = saved_errno; + return (NULL); + } + } + + /* + * There is now a buffer full of (possibly) duplicate + * names. + */ + dirp->dd_buf = buf; + + /* + * Go round this loop twice... + * + * Scan through the buffer, counting entries. + * On the second pass, save pointers to each one. + * Then sort the pointers and remove duplicate names. + */ + for (dpv = 0;;) { + n = 0; + ddptr = buf; + while (ddptr < ddeptr) { + struct dirent *dp; + + dp = (struct dirent *) ddptr; + if ((long)dp & 03L) + break; + if ((dp->d_reclen <= 0) || + (dp->d_reclen > (ddeptr + 1 - ddptr))) + break; + ddptr += dp->d_reclen; + if (dp->d_fileno) { + if (dpv) + dpv[n] = dp; + n++; + } + } + + if (dpv) { + struct dirent *xp; + + /* + * This sort must be stable. + */ + mergesort(dpv, n, sizeof(*dpv), alphasort); + + dpv[n] = NULL; + xp = NULL; + + /* + * Scan through the buffer in sort order, + * zapping the inode number of any + * duplicate names. + */ + for (n = 0; dpv[n]; n++) { + struct dirent *dp = dpv[n]; + + if ((xp == NULL) || + strcmp(dp->d_name, xp->d_name)) { + xp = dp; + } else { + dp->d_fileno = 0; + } + if (dp->d_type == DT_WHT && + (flags & DTF_HIDEW)) + dp->d_fileno = 0; + } + + free(dpv); + break; + } else { + dpv = malloc((n+1) * sizeof(struct dirent *)); + if (dpv == NULL) + break; + } + } + + dirp->dd_len = len; + dirp->dd_size = ddptr - dirp->dd_buf; + } else { + dirp->dd_len = incr; + dirp->dd_buf = malloc(dirp->dd_len); + if (dirp->dd_buf == NULL) + goto fail; + dirp->dd_seek = 0; + flags &= ~DTF_REWIND; + } + + dirp->dd_loc = 0; + dirp->dd_fd = fd; + dirp->dd_flags = flags; + + /* + * Set up seek point for rewinddir. + */ + dirp->dd_rewind = telldir(dirp); + + return (dirp); + +fail: + saved_errno = errno; + free(dirp); + (void) close(fd); + errno = saved_errno; + return (NULL); +} diff --git a/lib/libc/gen/pause.3 b/lib/libc/gen/pause.3 new file mode 100644 index 0000000..e48bc8e --- /dev/null +++ b/lib/libc/gen/pause.3 @@ -0,0 +1,84 @@ +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)pause.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt PAUSE 3 +.Os BSD 4 +.Sh NAME +.Nm pause +.Nd stop until signal +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft int +.Fn pause void +.Sh DESCRIPTION +.Sy Pause is made obsolete by +.Xr sigsuspend 2 . +.Pp +The +.Fn pause +function +forces a process to pause until +a signal is received from either the +.Xr kill 2 +function +or an interval timer. +(See +.Xr setitimer 2 . ) +Upon termination of a signal handler started during a +.Fn pause , +the +.Fn pause +call will return. +.Sh RETURN VALUES +Always returns \-1. +.Sh ERRORS +The +.Fn pause +function +always returns: +.Bl -tag -width [EINTR] +.It Bq Er EINTR +The call was interrupted. +.El +.Sh SEE ALSO +.Xr kill 2 , +.Xr select 2 , +.Xr sigsuspend 2 +.Sh HISTORY +A +.Fn pause +syscall +appeared in +.At v6 . diff --git a/lib/libc/gen/pause.c b/lib/libc/gen/pause.c new file mode 100644 index 0000000..9cbe467 --- /dev/null +++ b/lib/libc/gen/pause.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)pause.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <signal.h> +#include <unistd.h> + +/* + * Backwards compatible pause. + */ +int +pause() +{ + + return sigpause(sigblock(0L)); +} diff --git a/lib/libc/gen/popen.3 b/lib/libc/gen/popen.3 new file mode 100644 index 0000000..53004c0 --- /dev/null +++ b/lib/libc/gen/popen.3 @@ -0,0 +1,200 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)popen.3 8.2 (Berkeley) 5/3/95 +.\" $FreeBSD$ +.\" +.Dd May 3, 1995 +.Dt POPEN 3 +.Os +.Sh NAME +.Nm popen , +.Nm pclose +.Nd process +.Tn I/O +.Sh SYNOPSIS +.Fd #include <stdio.h> +.Ft FILE * +.Fn popen "const char *command" "const char *type" +.Ft int +.Fn pclose "FILE *stream" +.Sh DESCRIPTION +The +.Fn popen +function +.Dq opens +a process by creating a bidirectional pipe +forking, +and invoking the shell. +Any streams opened by previous +.Fn popen +calls in the parent process are closed in the new child process. +Historically, +.Fn popen +was implemented with a unidirectional pipe; +hence many implementations of +.Fn popen +only allow the +.Fa type +argument to specify reading or writing, not both. +Since +.Fn popen +is now implemented using a bidirectional pipe, the +.Fa type +argument may request a bidirectional data flow. +The +.Fa type +argument is a pointer to a null-terminated string +which must be +.Ql r +for reading, +.Ql w +for writing, or +.Ql r+ +for reading and writing. +.Pp +The +.Fa command +argument is a pointer to a null-terminated string +containing a shell command line. +This command is passed to +.Pa /bin/sh +using the +.Fl c +flag; interpretation, if any, is performed by the shell. +.Pp +The return value from +.Fn popen +is a normal standard +.Tn I/O +stream in all respects +save that it must be closed with +.Fn pclose +rather than +.Fn fclose . +Writing to such a stream +writes to the standard input of the command; +the command's standard output is the same as that of the process that called +.Fn popen , +unless this is altered by the command itself. +Conversely, reading from a +.Dq popened +stream reads the command's standard output, and +the command's standard input is the same as that of the process that called +.Fn popen . +.Pp +Note that output +.Fn popen +streams are fully buffered by default. +.Pp +The +.Fn pclose +function waits for the associated process to terminate +and returns the exit status of the command +as returned by +.Fn wait4 . +.Sh RETURN VALUE +The +.Fn popen +function returns +.Dv NULL +if the +.Xr fork 2 +or +.Xr pipe 2 +calls fail, +or if it cannot allocate memory. +.Pp +The +.Fn pclose +function +returns \-1 if +.Fa stream +is not associated with a +.Dq popened +command, if +.Fa stream +already +.Dq pclosed , +or if +.Xr wait4 +returns an error. +.Sh ERRORS +The +.Fn popen +function does not reliably set +.Va errno . +.Sh SEE ALSO +.Xr sh 1 , +.Xr fork 2 , +.Xr pipe 2 , +.Xr wait4 2 , +.Xr fclose 3 , +.Xr fflush 3 , +.Xr fopen 3 , +.Xr stdio 3 , +.Xr system 3 +.Sh BUGS +Since the standard input of a command opened for reading +shares its seek offset with the process that called +.Fn popen , +if the original process has done a buffered read, +the command's input position may not be as expected. +Similarly, the output from a command opened for writing +may become intermingled with that of the original process. +The latter can be avoided by calling +.Xr fflush 3 +before +.Fn popen . +.Pp +Failure to execute the shell +is indistinguishable from the shell's failure to execute command, +or an immediate exit of the command. +The only hint is an exit status of 127. +.Pp +The +.Fn popen +argument +always calls +.Xr sh 1 , +never calls +.Xr csh 1 . +.Sh HISTORY +A +.Fn popen +and a +.Fn pclose +function appeared in +.At v7 . +.br +Bidirectional functionality was added in +.Tn FreeBSD +2.2.6. diff --git a/lib/libc/gen/popen.c b/lib/libc/gen/popen.c new file mode 100644 index 0000000..bcbd164 --- /dev/null +++ b/lib/libc/gen/popen.c @@ -0,0 +1,189 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software written by Ken Arnold and + * published in UNIX Review, Vol. 6, No. 8. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/wait.h> + +#include <signal.h> +#include <errno.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <paths.h> + +extern char **environ; + +static struct pid { + struct pid *next; + FILE *fp; + pid_t pid; +} *pidlist; + +FILE * +popen(command, type) + const char *command, *type; +{ + struct pid *cur; + FILE *iop; + int pdes[2], pid, twoway; + char *argv[4]; + struct pid *p; + + /* + * Lite2 introduced two-way popen() pipes using socketpair(). + * FreeBSD's pipe() is bidirectional, so we use that. + */ + if (strchr(type, '+')) { + twoway = 1; + type = "r+"; + } else { + twoway = 0; + if ((*type != 'r' && *type != 'w') || type[1]) + return (NULL); + } + if (pipe(pdes) < 0) + return (NULL); + + if ((cur = malloc(sizeof(struct pid))) == NULL) { + (void)close(pdes[0]); + (void)close(pdes[1]); + return (NULL); + } + + argv[0] = "sh"; + argv[1] = "-c"; + argv[2] = (char *)command; + argv[3] = NULL; + + switch (pid = vfork()) { + case -1: /* Error. */ + (void)close(pdes[0]); + (void)close(pdes[1]); + free(cur); + return (NULL); + /* NOTREACHED */ + case 0: /* Child. */ + if (*type == 'r') { + /* + * The dup2() to STDIN_FILENO is repeated to avoid + * writing to pdes[1], which might corrupt the + * parent's copy. This isn't good enough in + * general, since the _exit() is no return, so + * the compiler is free to corrupt all the local + * variables. + */ + (void) close(pdes[0]); + if (pdes[1] != STDOUT_FILENO) { + (void)dup2(pdes[1], STDOUT_FILENO); + (void)close(pdes[1]); + if (twoway) + (void)dup2(STDOUT_FILENO, STDIN_FILENO); + } else if (twoway && (pdes[1] != STDIN_FILENO)) + (void)dup2(pdes[1], STDIN_FILENO); + } else { + if (pdes[0] != STDIN_FILENO) { + (void)dup2(pdes[0], STDIN_FILENO); + (void)close(pdes[0]); + } + (void)close(pdes[1]); + } + for (p = pidlist; p; p = p->next) { + (void)close(fileno(p->fp)); + } + execve(_PATH_BSHELL, argv, environ); + _exit(127); + /* NOTREACHED */ + } + + /* Parent; assume fdopen can't fail. */ + if (*type == 'r') { + iop = fdopen(pdes[0], type); + (void)close(pdes[1]); + } else { + iop = fdopen(pdes[1], type); + (void)close(pdes[0]); + } + + /* Link into list of file descriptors. */ + cur->fp = iop; + cur->pid = pid; + cur->next = pidlist; + pidlist = cur; + + return (iop); +} + +/* + * pclose -- + * Pclose returns -1 if stream is not associated with a `popened' command, + * if already `pclosed', or waitpid returns an error. + */ +int +pclose(iop) + FILE *iop; +{ + register struct pid *cur, *last; + int omask; + int pstat; + pid_t pid; + + /* Find the appropriate file pointer. */ + for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) + if (cur->fp == iop) + break; + if (cur == NULL) + return (-1); + + (void)fclose(iop); + + do { + pid = waitpid(cur->pid, &pstat, 0); + } while (pid == -1 && errno == EINTR); + + /* Remove the entry from the linked list. */ + if (last == NULL) + pidlist = cur->next; + else + last->next = cur->next; + free(cur); + + return (pid == -1 ? -1 : pstat); +} diff --git a/lib/libc/gen/psignal.3 b/lib/libc/gen/psignal.3 new file mode 100644 index 0000000..325f8ec --- /dev/null +++ b/lib/libc/gen/psignal.3 @@ -0,0 +1,110 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)psignal.3 8.2 (Berkeley) 2/27/95 +.\" $FreeBSD$ +.\" +.Dd February 27, 1995 +.Dt PSIGNAL 3 +.Os BSD 4.2 +.Sh NAME +.Nm psignal , +.Nm strsignal , +.Nm sys_siglist , +.Nm sys_signame +.Nd system signal messages +.Sh SYNOPSIS +.Fd #include <signal.h> +.Ft void +.Fn psignal "unsigned sig" "const char *s" +.Ft "char *" +.Fn strsignal "unsigned sig" +.Vt extern const char * const sys_siglist[]; +.Vt extern const char * const sys_signame[]; +.Sh DESCRIPTION +The +.Fn psignal +and +.Fn strsignal +functions locate the descriptive message +string for a signal number. +.Pp +The +.Fn strsignal +function accepts a signal number argument +.Fa sig +and returns a pointer to the corresponding message string. +.Pp +The +.Fn psignal +function accepts an signal number argument +.Fa sig +and writes it to the standard error. +If the argument +.Fa s +is +.Pf non- Dv NULL +and does not point to the null character, +.Fa s +is written to the standard error file descriptor +prior to the message string, +immediately followed by a colon and a space. +If the signal number is not recognized +.Pq Xr sigaction 2 , +the string +.Dq "Unknown signal +is produced. +.Pp +The message strings can be accessed directly +through the external array +.Va sys_siglist , +indexed by recognized signal numbers. +The external array +.Va sys_signame +is used similarly and +contains short, lower-case abbreviations for signals +which are useful for recognizing signal names +in user input. +The defined variable +.Dv NSIG +contains a count of the strings in +.Va sys_siglist +and +.Va sys_signame . +.Sh SEE ALSO +.Xr sigaction 2 , +.Xr perror 3 , +.Xr strerror 3 +.Sh HISTORY +The +.Fn psignal +function appeared in +.Bx 4.2 . diff --git a/lib/libc/gen/psignal.c b/lib/libc/gen/psignal.c new file mode 100644 index 0000000..96eab9d --- /dev/null +++ b/lib/libc/gen/psignal.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)psignal.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +/* + * Print the name of the signal indicated + * along with the supplied message. + */ +#include <signal.h> +#include <string.h> +#include <unistd.h> + +void +psignal(sig, s) + unsigned int sig; + const char *s; +{ + register const char *c; + + if (sig < NSIG) + c = sys_siglist[sig]; + else + c = "Unknown signal"; + if (s != NULL && *s != '\0') { + (void)write(STDERR_FILENO, s, strlen(s)); + (void)write(STDERR_FILENO, ": ", 2); + } + (void)write(STDERR_FILENO, c, strlen(c)); + (void)write(STDERR_FILENO, "\n", 1); +} diff --git a/lib/libc/gen/pw_scan.c b/lib/libc/gen/pw_scan.c new file mode 100644 index 0000000..849effa --- /dev/null +++ b/lib/libc/gen/pw_scan.c @@ -0,0 +1,165 @@ +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)pw_scan.c 8.3 (Berkeley) 4/2/94"; +#endif +static const char rcsid[] = + "$FreeBSD$"; +#endif /* not lint */ + +/* + * This module is used to "verify" password entries by chpass(1) and + * pwd_mkdb(8). + */ + +#include <sys/param.h> + +#include <err.h> +#include <fcntl.h> +#include <pwd.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> + +#include "pw_scan.h" + +/* + * Some software assumes that IDs are short. We should emit warnings + * for id's which can not be stored in a short, but we are more liberal + * by default, warning for IDs greater than USHRT_MAX. + * + * If pw_big_ids_warning is anything other than -1 on entry to pw_scan() + * it will be set based on the existance of PW_SCAN_BIG_IDS in the + * environment. + */ +int pw_big_ids_warning = -1; + +int +pw_scan(bp, pw) + char *bp; + struct passwd *pw; +{ + long id; + int root; + char *p, *sh; + + if (pw_big_ids_warning == -1) + pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0; + + pw->pw_fields = 0; + if (!(pw->pw_name = strsep(&bp, ":"))) /* login */ + goto fmt; + root = !strcmp(pw->pw_name, "root"); + if(pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0')) + pw->pw_fields |= _PWF_NAME; + + if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */ + goto fmt; + if(pw->pw_passwd[0]) pw->pw_fields |= _PWF_PASSWD; + + if (!(p = strsep(&bp, ":"))) /* uid */ + goto fmt; + if (p[0]) + pw->pw_fields |= _PWF_UID; + else { + if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') { + warnx("no uid for user %s", pw->pw_name); + return (0); + } + } + id = atol(p); + if (root && id) { + warnx("root uid should be 0"); + return (0); + } + if (pw_big_ids_warning && id > USHRT_MAX) { + warnx("%s > max uid value (%u)", p, USHRT_MAX); + /*return (0);*/ /* THIS SHOULD NOT BE FATAL! */ + } + pw->pw_uid = id; + + if (!(p = strsep(&bp, ":"))) /* gid */ + goto fmt; + if(p[0]) pw->pw_fields |= _PWF_GID; + id = atol(p); + if (pw_big_ids_warning && id > USHRT_MAX) { + warnx("%s > max gid value (%u)", p, USHRT_MAX); + /* return (0); This should not be fatal! */ + } + pw->pw_gid = id; + + pw->pw_class = strsep(&bp, ":"); /* class */ + if(pw->pw_class[0]) pw->pw_fields |= _PWF_CLASS; + + if (!(p = strsep(&bp, ":"))) /* change */ + goto fmt; + if(p[0]) pw->pw_fields |= _PWF_CHANGE; + pw->pw_change = atol(p); + + if (!(p = strsep(&bp, ":"))) /* expire */ + goto fmt; + if(p[0]) pw->pw_fields |= _PWF_EXPIRE; + pw->pw_expire = atol(p); + + if (!(pw->pw_gecos = strsep(&bp, ":"))) /* gecos */ + goto fmt; + if(pw->pw_gecos[0]) pw->pw_fields |= _PWF_GECOS; + + if (!(pw->pw_dir = strsep(&bp, ":"))) /* directory */ + goto fmt; + if(pw->pw_dir[0]) pw->pw_fields |= _PWF_DIR; + + if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */ + goto fmt; + + p = pw->pw_shell; + if (root && *p) /* empty == /bin/sh */ + for (setusershell();;) { + if (!(sh = getusershell())) { + warnx("warning, unknown root shell"); + break; + } + if (!strcmp(p, sh)) + break; + } + if(p[0]) pw->pw_fields |= _PWF_SHELL; + + if ((p = strsep(&bp, ":"))) { /* too many */ +fmt: warnx("corrupted entry"); + return (0); + } + return (1); +} diff --git a/lib/libc/gen/pw_scan.h b/lib/libc/gen/pw_scan.h new file mode 100644 index 0000000..2519bd4 --- /dev/null +++ b/lib/libc/gen/pw_scan.h @@ -0,0 +1,40 @@ +/*- + * Copyright (c) 1994 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#)pw_scan.h 8.1 (Berkeley) 4/1/94 + * + * $FreeBSD$ + */ + +extern int pw_big_ids_warning; + +extern int pw_scan __P((char *, struct passwd *)); diff --git a/lib/libc/gen/pwcache.3 b/lib/libc/gen/pwcache.3 new file mode 100644 index 0000000..d5f08a2 --- /dev/null +++ b/lib/libc/gen/pwcache.3 @@ -0,0 +1,95 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)pwcache.3 8.1 (Berkeley) 6/9/93 +.\" $FreeBSD$ +.\" +.Dd June 9, 1993 +.Dt PWCACHE 3 +.Os +.Sh NAME +.Nm pwcache +.Nd cache password and group entries +.Sh SYNOPSIS +.Fd #include <stdlib.h> +.Ft char * +.Fn user_from_uid "unsigned long uid" "int nouser" +.Ft char * +.Fn group_from_gid "unsigned long gid" "int nogroup" +.Sh DESCRIPTION +.Pp +The +.Fn user_from_uid +function returns the user name associated with the argument +.Fa uid . +The user name is cached so that multiple calls with the same +.Fa uid +do not require additional calls to +.Xr getpwuid 3 . +If there is no user associated with the +.Fa uid , +a pointer is returned +to a string representation of the +.Fa uid , +unless the argument +.Fa nouser +is non-zero, in which case a +.Dv NULL +pointer is returned. +.Pp +The +.Fn group_from_gid +function returns the group name associated with the argument +.Fa gid . +The group name is cached so that multiple calls with the same +.Fa gid +do not require additional calls to +.Xr getgrgid 3 . +If there is no group associated with the +.Fa gid , +a pointer is returned +to a string representation of the +.Fa gid , +unless the argument +.Fa nogroup +is non-zero, in which case a +.Dv NULL +pointer is returned. +.Sh SEE ALSO +.Xr getgrgid 3 , +.Xr getpwuid 3 +.Sh HISTORY +The +.Fn user_from_uid +and +.Fn group_from_gid +functions first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/pwcache.c b/lib/libc/gen/pwcache.c new file mode 100644 index 0000000..aea7f55 --- /dev/null +++ b/lib/libc/gen/pwcache.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)pwcache.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> + +#include <grp.h> +#include <string.h> +#include <pwd.h> +#include <stdio.h> +#include <utmp.h> + +#define NCACHE 64 /* power of 2 */ +#define MASK (NCACHE - 1) /* bits to store with */ + +char * +user_from_uid(uid, nouser) + uid_t uid; + int nouser; +{ + static struct ncache { + uid_t uid; + int found; + char name[UT_NAMESIZE + 1]; + } c_uid[NCACHE]; + static int pwopen; + register struct passwd *pw; + register struct ncache *cp; + + cp = c_uid + (uid & MASK); + if (cp->uid != uid || !*cp->name) { + if (pwopen == 0) { + setpassent(1); + pwopen = 1; + } + pw = getpwuid(uid); + cp->uid = uid; + if (pw != NULL) { + cp->found = 1; + (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE); + cp->name[UT_NAMESIZE] = '\0'; + } else { + cp->found = 0; + (void)snprintf(cp->name, UT_NAMESIZE, "%u", uid); + if (nouser) + return (NULL); + } + } + return ((nouser && !cp->found) ? NULL : cp->name); +} + +char * +group_from_gid(gid, nogroup) + gid_t gid; + int nogroup; +{ + static struct ncache { + gid_t gid; + int found; + char name[UT_NAMESIZE + 1]; + } c_gid[NCACHE]; + static int gropen; + struct group *gr; + struct ncache *cp; + + cp = c_gid + (gid & MASK); + if (cp->gid != gid || !*cp->name) { + if (gropen == 0) { + setgroupent(1); + gropen = 1; + } + gr = getgrgid(gid); + cp->gid = gid; + if (gr != NULL) { + cp->found = 1; + (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE); + cp->name[UT_NAMESIZE] = '\0'; + } else { + cp->found = 0; + (void)snprintf(cp->name, UT_NAMESIZE, "%u", gid); + if (nogroup) + return (NULL); + } + } + return ((nogroup && !cp->found) ? NULL : cp->name); +} diff --git a/lib/libc/gen/raise.3 b/lib/libc/gen/raise.3 new file mode 100644 index 0000000..556ad2a --- /dev/null +++ b/lib/libc/gen/raise.3 @@ -0,0 +1,78 @@ +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)raise.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt RAISE 3 +.Os +.Sh NAME +.Nm raise +.Nd send a signal to the current process +.Sh SYNOPSIS +.Fd #include <signal.h> +.Ft int +.Fn raise "int sig" +.Sh DESCRIPTION +The +.Fn raise +function sends the signal +.Fa sig +to the current process. +.Sh RETURN VALUES +Upon successful completion, a value of 0 is returned. +Otherwise, a value of \-1 is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +The +.Fn raise +function +may fail and set +.Va errno +for any of the errors specified for the +library functions +.Xr getpid 2 +and +.Xr kill 2 . +.Sh SEE ALSO +.Xr kill 2 +.Sh STANDARDS +The +.Fn raise +function +conforms to +.St -ansiC . diff --git a/lib/libc/gen/raise.c b/lib/libc/gen/raise.c new file mode 100644 index 0000000..2562c81 --- /dev/null +++ b/lib/libc/gen/raise.c @@ -0,0 +1,46 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)raise.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <signal.h> +#include <unistd.h> + +int +raise(s) + int s; +{ + return(kill(getpid(), s)); +} diff --git a/lib/libc/gen/rand48.3 b/lib/libc/gen/rand48.3 new file mode 100644 index 0000000..86e7a2f --- /dev/null +++ b/lib/libc/gen/rand48.3 @@ -0,0 +1,161 @@ +\" Copyright (c) 1993 Martin Birgmeier +.\" All rights reserved. +.\" +.\" You may redistribute unmodified or modified versions of this source +.\" code provided that the above copyright notice and this and the +.\" following conditions are retained. +.\" +.\" This software is provided ``as is'', and comes with no warranties +.\" of any kind. I shall in no event be liable for anything that happens +.\" to anyone/anything when using this software. +.\" +.\" @(#)rand48.3 V1.0 MB 8 Oct 1993 +.\" $FreeBSD$ +.\" +.Dd October 8, 1993 +.Dt RAND48 3 +.Os FreeBSD +.Sh NAME +.Nm drand48 , +.Nm erand48 , +.Nm lrand48 , +.Nm nrand48 , +.Nm mrand48 , +.Nm jrand48 , +.Nm srand48 , +.Nm seed48 , +.Nm lcong48 +.Nd pseudo random number generators and initialization routines +.Sh SYNOPSIS +.Fd #include <stdlib.h> +.Ft double +.Fn drand48 void +.Ft double +.Fn erand48 "unsigned short xseed[3]" +.Ft long +.Fn lrand48 void +.Ft long +.Fn nrand48 "unsigned short xseed[3]" +.Ft long +.Fn mrand48 void +.Ft long +.Fn jrand48 "unsigned short xseed[3]" +.Ft void +.Fn srand48 "long seed" +.Ft "unsigned short *" +.Fn seed48 "unsigned short xseed[3]" +.Ft void +.Fn lcong48 "unsigned short p[7]" +.Sh DESCRIPTION +The +.Fn rand48 +family of functions generates pseudo-random numbers using a linear +congruential algorithm working on integers 48 bits in size. The +particular formula employed is +r(n+1) = (a * r(n) + c) mod m +where the default values are +for the multiplicand a = 0xfdeece66d = 25214903917 and +the addend c = 0xb = 11. The modulo is always fixed at m = 2 ** 48. +r(n) is called the seed of the random number generator. +.Pp +For all the six generator routines described next, the first +computational step is to perform a single iteration of the algorithm. +.Pp +.Fn drand48 +and +.Fn erand48 +return values of type double. The full 48 bits of r(n+1) are +loaded into the mantissa of the returned value, with the exponent set +such that the values produced lie in the interval [0.0, 1.0). +.Pp +.Fn lrand48 +and +.Fn nrand48 +return values of type long in the range +[0, 2**31-1]. The high-order (31) bits of +r(n+1) are loaded into the lower bits of the returned value, with +the topmost (sign) bit set to zero. +.Pp +.Fn mrand48 +and +.Fn jrand48 +return values of type long in the range +[-2**31, 2**31-1]. The high-order (32) bits of +r(n+1) are loaded into the returned value. +.Pp +.Fn drand48 , +.Fn lrand48 , +and +.Fn mrand48 +use an internal buffer to store r(n). For these functions +the initial value of r(0) = 0x1234abcd330e = 20017429951246. +.Pp +On the other hand, +.Fn erand48 , +.Fn nrand48 , +and +.Fn jrand48 +use a user-supplied buffer to store the seed r(n), +which consists of an array of 3 shorts, where the zeroth member +holds the least significant bits. +.Pp +All functions share the same multiplicand and addend. +.Pp +.Fn srand48 +is used to initialize the internal buffer r(n) of +.Fn drand48 , +.Fn lrand48 , +and +.Fn mrand48 +such that the 32 bits of the seed value are copied into the upper 32 bits +of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. +Additionally, the constant multiplicand and addend of the algorithm are +reset to the default values given above. +.Pp +.Fn seed48 +also initializes the internal buffer r(n) of +.Fn drand48 , +.Fn lrand48 , +and +.Fn mrand48 , +but here all 48 bits of the seed can be specified in an array of 3 shorts, +where the zeroth member specifies the lowest bits. Again, +the constant multiplicand and addend of the algorithm are +reset to the default values given above. +.Fn seed48 +returns a pointer to an array of 3 shorts which contains the old seed. +This array is statically allocated, thus its contents are lost after +each new call to +.Fn seed48 . +.Pp +Finally, +.Fn lcong48 +allows full control over the multiplicand and addend used in +.Fn drand48 , +.Fn erand48 , +.Fn lrand48 , +.Fn nrand48 , +.Fn mrand48 , +and +.Fn jrand48 , +and the seed used in +.Fn drand48 , +.Fn lrand48 , +and +.Fn mrand48 . +An array of 7 shorts is passed as parameter; the first three shorts are +used to initialize the seed; the second three are used to initialize the +multiplicand; and the last short is used to initialize the addend. +It is thus not possible to use values greater than 0xffff as the addend. +.Pp +Note that all three methods of seeding the random number generator +always also set the multiplicand and addend for any of the six +generator calls. +.Pp +For a more powerful random number generator, see +.Xr random 3 . +.Sh AUTHORS +.An Martin Birgmeier +.Sh SEE ALSO +.Xr rand 3 , +.Xr random 3 diff --git a/lib/libc/gen/rand48.h b/lib/libc/gen/rand48.h new file mode 100644 index 0000000..5b9f87d --- /dev/null +++ b/lib/libc/gen/rand48.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#ifndef _RAND48_H_ +#define _RAND48_H_ + +#include <math.h> +#include <stdlib.h> + +void _dorand48 __P((unsigned short[3])); + +#define RAND48_SEED_0 (0x330e) +#define RAND48_SEED_1 (0xabcd) +#define RAND48_SEED_2 (0x1234) +#define RAND48_MULT_0 (0xe66d) +#define RAND48_MULT_1 (0xdeec) +#define RAND48_MULT_2 (0x0005) +#define RAND48_ADD (0x000b) + +#endif /* _RAND48_H_ */ diff --git a/lib/libc/gen/readdir.c b/lib/libc/gen/readdir.c new file mode 100644 index 0000000..2e91a9a --- /dev/null +++ b/lib/libc/gen/readdir.c @@ -0,0 +1,125 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * $FreeBSD$ + * + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)readdir.c 8.3 (Berkeley) 9/29/94"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <dirent.h> +#include <errno.h> +#ifdef _THREAD_SAFE +#include <pthread.h> +#include "pthread_private.h" +#endif _THREAD_SAFE + +/* + * get next entry in a directory. + */ +struct dirent * +readdir(dirp) + register DIR *dirp; +{ + register struct dirent *dp; + + for (;;) { + if (dirp->dd_loc >= dirp->dd_size) { + if (dirp->dd_flags & __DTF_READALL) + return (NULL); + dirp->dd_loc = 0; + } + if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) { + dirp->dd_size = getdirentries(dirp->dd_fd, + dirp->dd_buf, dirp->dd_len, &dirp->dd_seek); + if (dirp->dd_size <= 0) + return (NULL); + } + dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc); + if ((long)dp & 03L) /* bogus pointer check */ + return (NULL); + if (dp->d_reclen <= 0 || + dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc) + return (NULL); + dirp->dd_loc += dp->d_reclen; + if (dp->d_ino == 0) + continue; + if (dp->d_type == DT_WHT && (dirp->dd_flags & DTF_HIDEW)) + continue; + return (dp); + } +} + +int +readdir_r(dirp, entry, result) + DIR *dirp; + struct dirent *entry; + struct dirent **result; +{ + struct dirent *dp; + int ret, saved_errno; + +#ifdef _THREAD_SAFE + if ((ret = _FD_LOCK(dirp->dd_fd, FD_READ, NULL)) != 0) + return (ret); +#endif + + saved_errno = errno; + errno = 0; + dp = readdir(dirp); + if (errno != 0) { + if (dp == NULL) { +#ifdef _THREAD_SAFE + _FD_UNLOCK(dirp->dd_fd, FD_READ); +#endif + return (errno); + } + } else + errno = saved_errno; + + if (dp != NULL) + memcpy(entry, dp, sizeof *entry); + +#ifdef _THREAD_SAFE + _FD_UNLOCK(dirp->dd_fd, FD_READ); +#endif + + if (dp != NULL) + *result = entry; + else + *result = NULL; + + return (0); +} diff --git a/lib/libc/gen/rewinddir.c b/lib/libc/gen/rewinddir.c new file mode 100644 index 0000000..2076ddd --- /dev/null +++ b/lib/libc/gen/rewinddir.c @@ -0,0 +1,50 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)rewinddir.c 8.1 (Berkeley) 6/8/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <dirent.h> + +extern void _seekdir __P(( DIR *, long )); + +void +rewinddir(dirp) + DIR *dirp; +{ + + _seekdir(dirp, dirp->dd_rewind); + dirp->dd_rewind = telldir(dirp); +} diff --git a/lib/libc/gen/scandir.3 b/lib/libc/gen/scandir.3 new file mode 100644 index 0000000..afcde28 --- /dev/null +++ b/lib/libc/gen/scandir.3 @@ -0,0 +1,107 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)scandir.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt SCANDIR 3 +.Os BSD 4.2 +.Sh NAME +.Nm scandir , +.Nm alphasort +.Nd scan a directory +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <dirent.h> +.Ft int +.Fn scandir "const char *dirname" "struct dirent ***namelist" "int \\*(lp*select\\*(rp\\*(lpstruct dirent *\\*(rp" "int \\*(lp*compar\\*(rp\\*(lpconst void *, const void *\\*(rp" +.Ft int +.Fn alphasort "const void *d1" "const void *d2" +.Sh DESCRIPTION +The +.Fn scandir +function +reads the directory +.Fa dirname +and builds an array of pointers to directory +entries using +.Xr malloc 3 . +It returns the number of entries in the array. +A pointer to the array of directory entries is stored in the location +referenced by +.Fa namelist . +.Pp +The +.Fa select +parameter is a pointer to a user supplied subroutine which is called by +.Fn scandir +to select which entries are to be included in the array. +The select routine is passed a +pointer to a directory entry and should return a non-zero +value if the directory entry is to be included in the array. +If +.Fa select +is null, then all the directory entries will be included. +.Pp +The +.Fa compar +parameter is a pointer to a user supplied subroutine which is passed to +.Xr qsort 3 +to sort the completed array. +If this pointer is null, the array is not sorted. +.Pp +The +.Fn alphasort +function +is a routine which can be used for the +.Fa compar +parameter to sort the array alphabetically. +.Pp +The memory allocated for the array can be deallocated with +.Xr free 3 , +by freeing each pointer in the array and then the array itself. +.Sh DIAGNOSTICS +Returns \-1 if the directory cannot be opened for reading or if +.Xr malloc 3 +cannot allocate enough memory to hold all the data structures. +.Sh SEE ALSO +.Xr directory 3 , +.Xr malloc 3 , +.Xr qsort 3 , +.Xr dir 5 +.Sh HISTORY +The +.Fn scandir +and +.Fn alphasort +functions appeared in +.Bx 4.2 . diff --git a/lib/libc/gen/scandir.c b/lib/libc/gen/scandir.c new file mode 100644 index 0000000..5c64b29 --- /dev/null +++ b/lib/libc/gen/scandir.c @@ -0,0 +1,146 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)scandir.c 8.3 (Berkeley) 1/2/94"; +#endif /* LIBC_SCCS and not lint */ + +/* + * Scan the directory dirname calling select to make a list of selected + * directory entries then sort using qsort and compare routine dcomp. + * Returns the number of entries and a pointer to a list of pointers to + * struct dirent (through namelist). Returns -1 if there were any errors. + */ + +#include <sys/types.h> +#include <sys/stat.h> +#include <dirent.h> +#include <stdlib.h> +#include <string.h> + +/* + * The DIRSIZ macro is the minimum record length which will hold the directory + * entry. This requires the amount of space in struct dirent without the + * d_name field, plus enough space for the name and a terminating nul byte + * (dp->d_namlen + 1), rounded up to a 4 byte boundary. + */ +#undef DIRSIZ +#define DIRSIZ(dp) \ + ((sizeof(struct dirent) - sizeof(dp)->d_name) + \ + (((dp)->d_namlen + 1 + 3) &~ 3)) + +int +scandir(dirname, namelist, select, dcomp) + const char *dirname; + struct dirent ***namelist; + int (*select) __P((struct dirent *)); + int (*dcomp) __P((const void *, const void *)); +{ + register struct dirent *d, *p, **names = NULL; + register size_t nitems = 0; + struct stat stb; + long arraysz; + DIR *dirp; + + if ((dirp = opendir(dirname)) == NULL) + return(-1); + if (fstat(dirp->dd_fd, &stb) < 0) + goto fail; + + /* + * estimate the array size by taking the size of the directory file + * and dividing it by a multiple of the minimum size entry. + */ + arraysz = (stb.st_size / 24); + names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *)); + if (names == NULL) + goto fail; + + while ((d = readdir(dirp)) != NULL) { + if (select != NULL && !(*select)(d)) + continue; /* just selected names */ + /* + * Make a minimum size copy of the data + */ + p = (struct dirent *)malloc(DIRSIZ(d)); + if (p == NULL) + goto fail; + p->d_fileno = d->d_fileno; + p->d_type = d->d_type; + p->d_reclen = d->d_reclen; + p->d_namlen = d->d_namlen; + bcopy(d->d_name, p->d_name, p->d_namlen + 1); + /* + * Check to make sure the array has space left and + * realloc the maximum size. + */ + if (nitems >= arraysz) { + const int inc = 10; /* increase by this much */ + struct dirent **names2; + + names2 = (struct dirent **)realloc((char *)names, + (arraysz + inc) * sizeof(struct dirent *)); + if (names2 == NULL) { + free(p); + goto fail; + } + names = names2; + arraysz += inc; + } + names[nitems++] = p; + } + closedir(dirp); + if (nitems && dcomp != NULL) + qsort(names, nitems, sizeof(struct dirent *), dcomp); + *namelist = names; + return(nitems); + +fail: + while (nitems > 0) + free(names[--nitems]); + free(names); + closedir(dirp); + return -1; +} + +/* + * Alphabetic order comparison routine for those who want it. + */ +int +alphasort(d1, d2) + const void *d1; + const void *d2; +{ + return(strcmp((*(struct dirent **)d1)->d_name, + (*(struct dirent **)d2)->d_name)); +} diff --git a/lib/libc/gen/seed48.c b/lib/libc/gen/seed48.c new file mode 100644 index 0000000..258c4ba --- /dev/null +++ b/lib/libc/gen/seed48.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +extern unsigned short _rand48_seed[3]; +extern unsigned short _rand48_mult[3]; +extern unsigned short _rand48_add; + +unsigned short * +seed48(unsigned short xseed[3]) +{ + static unsigned short sseed[3]; + + sseed[0] = _rand48_seed[0]; + sseed[1] = _rand48_seed[1]; + sseed[2] = _rand48_seed[2]; + _rand48_seed[0] = xseed[0]; + _rand48_seed[1] = xseed[1]; + _rand48_seed[2] = xseed[2]; + _rand48_mult[0] = RAND48_MULT_0; + _rand48_mult[1] = RAND48_MULT_1; + _rand48_mult[2] = RAND48_MULT_2; + _rand48_add = RAND48_ADD; + return sseed; +} diff --git a/lib/libc/gen/seekdir.c b/lib/libc/gen/seekdir.c new file mode 100644 index 0000000..1934bcc --- /dev/null +++ b/lib/libc/gen/seekdir.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)seekdir.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <dirent.h> + +extern void _seekdir __P(( DIR *, long )); + +/* + * Seek to an entry in a directory. + * _seekdir is in telldir.c so that it can share opaque data structures. + */ +void +seekdir(dirp, loc) + DIR *dirp; + long loc; +{ + + _seekdir(dirp, loc); +} diff --git a/lib/libc/gen/semconfig.c b/lib/libc/gen/semconfig.c new file mode 100644 index 0000000..cf5399b --- /dev/null +++ b/lib/libc/gen/semconfig.c @@ -0,0 +1,13 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/sem.h> + +#if __STDC__ +int semconfig(int cmd, int p1, int p2, int p3) +#else +int semconfig(cmd, p1, p2, p3) + int cmd, p1, p2, p3; +#endif +{ + return (semsys(3, cmd, p1, p2, p3)); +} diff --git a/lib/libc/gen/semctl.c b/lib/libc/gen/semctl.c new file mode 100644 index 0000000..03ecdbc --- /dev/null +++ b/lib/libc/gen/semctl.c @@ -0,0 +1,42 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/sem.h> +#if __STDC__ +#include <stdarg.h> +#else +#include <varargs.h> +#endif +#include <stdlib.h> + +#if __STDC__ +int semctl(int semid, int semnum, int cmd, ...) +#else +int semctl(semid, semnum, cmd, va_alist) + int semid, semnum; + int cmd; + va_dcl +#endif +{ + va_list ap; + union semun semun; + union semun *semun_ptr; +#if __STDC__ + va_start(ap, cmd); +#else + va_start(ap); +#endif + if (cmd == IPC_SET || cmd == IPC_STAT || cmd == GETALL + || cmd == SETVAL || cmd == SETALL) { + semun = va_arg(ap, union semun); + semun_ptr = &semun; + } else { + semun_ptr = NULL; + } + va_end(ap); + +#ifdef __NETBSD_SYSCALLS + return (__semctl(semid, semnum, cmd, semun_ptr)); +#else + return (semsys(0, semid, semnum, cmd, semun_ptr)); +#endif +} diff --git a/lib/libc/gen/semget.c b/lib/libc/gen/semget.c new file mode 100644 index 0000000..81c6a86 --- /dev/null +++ b/lib/libc/gen/semget.c @@ -0,0 +1,15 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/sem.h> + +#if __STDC__ +int semget(key_t key, int nsems, int semflg) +#else +int semget(key, nsems, semflg) + key_t key; + int nsems; + int semflg; +#endif +{ + return (semsys(1, key, nsems, semflg)); +} diff --git a/lib/libc/gen/semop.c b/lib/libc/gen/semop.c new file mode 100644 index 0000000..0b97c6a --- /dev/null +++ b/lib/libc/gen/semop.c @@ -0,0 +1,15 @@ +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/sem.h> + +#if __STDC__ +int semop(int semid, struct sembuf *sops, unsigned nsops) +#else +int semop(semid, sops, nsops) + int semid; + struct sembuf *sops; + unsigned nsops; +#endif +{ + return (semsys(2, semid, sops, nsops, 0)); +} diff --git a/lib/libc/gen/setdomainname.c b/lib/libc/gen/setdomainname.c new file mode 100644 index 0000000..8d46166 --- /dev/null +++ b/lib/libc/gen/setdomainname.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +/* +static char sccsid[] = "From: @(#)sethostname.c 8.1 (Berkeley) 6/4/93"; +*/ +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> + +#include <unistd.h> + +int +setdomainname(const char *name, int namelen) +{ + int mib[2]; + + mib[0] = CTL_KERN; + mib[1] = KERN_NISDOMAINNAME; + if (sysctl(mib, 2, NULL, NULL, (void *)name, namelen) == -1) + return (-1); + return (0); +} diff --git a/lib/libc/gen/setflags.c b/lib/libc/gen/setflags.c new file mode 100644 index 0000000..5235eb3 --- /dev/null +++ b/lib/libc/gen/setflags.c @@ -0,0 +1,174 @@ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)stat_flags.c 8.1 (Berkeley) 5/31/93"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif +#endif /* not lint */ + +#include <sys/types.h> +#include <sys/stat.h> + +#include <stddef.h> +#include <string.h> + +#define SAPPEND(s) { \ + if (prefix != NULL) \ + (void)strcat(string, prefix); \ + (void)strcat(string, s); \ + prefix = ","; \ +} + +/* + * flags_to_string -- + * Convert stat flags to a comma-separated string. If no flags + * are set, return the default string. + */ +char * +flags_to_string(flags, def) + u_long flags; + char *def; +{ + static char string[128]; + char *prefix; + + string[0] = '\0'; + prefix = NULL; + if (flags & UF_APPEND) + SAPPEND("uappnd"); + if (flags & UF_IMMUTABLE) + SAPPEND("uchg"); +#ifdef UF_NOUNLINK + if (flags & UF_NOUNLINK) + SAPPEND("uunlnk"); +#endif + if (flags & UF_NODUMP) + SAPPEND("nodump"); + if (flags & UF_OPAQUE) + SAPPEND("opaque"); + if (flags & SF_APPEND) + SAPPEND("sappnd"); + if (flags & SF_ARCHIVED) + SAPPEND("arch"); + if (flags & SF_IMMUTABLE) + SAPPEND("schg"); +#ifdef SF_NOUNLINK + if (flags & SF_NOUNLINK) + SAPPEND("sunlnk"); +#endif + return (prefix == NULL && def != NULL ? def : string); +} + +#define TEST(a, b, f) { \ + if (!memcmp(a, b, sizeof(b))) { \ + if (clear) { \ + if (clrp) \ + *clrp |= (f); \ + } else if (setp) \ + *setp |= (f); \ + break; \ + } \ +} + +/* + * string_to_flags -- + * Take string of arguments and return stat flags. Return 0 on + * success, 1 on failure. On failure, stringp is set to point + * to the offending token. + */ +int +string_to_flags(stringp, setp, clrp) + char **stringp; + u_long *setp, *clrp; +{ + int clear; + char *string, *p; + + if (setp) + *setp = 0; + if (clrp) + *clrp = 0; + string = *stringp; + while ((p = strsep(&string, "\t ,")) != NULL) { + clear = 0; + *stringp = p; + if (*p == '\0') + continue; + if (p[0] == 'n' && p[1] == 'o') { + clear = 1; + p += 2; + } + switch (p[0]) { + case 'a': + TEST(p, "arch", SF_ARCHIVED); + TEST(p, "archived", SF_ARCHIVED); + return (1); + case 'd': + clear = !clear; + TEST(p, "dump", UF_NODUMP); + return (1); + case 'o': + TEST(p, "opaque", UF_OPAQUE); + return (1); + case 's': + TEST(p, "sappnd", SF_APPEND); + TEST(p, "sappend", SF_APPEND); + TEST(p, "schg", SF_IMMUTABLE); + TEST(p, "schange", SF_IMMUTABLE); + TEST(p, "simmutable", SF_IMMUTABLE); +#ifdef SF_NOUNLINK + TEST(p, "sunlnk", SF_NOUNLINK); + TEST(p, "sunlink", SF_NOUNLINK); +#endif + return (1); + case 'u': + TEST(p, "uappnd", UF_APPEND); + TEST(p, "uappend", UF_APPEND); + TEST(p, "uchg", UF_IMMUTABLE); + TEST(p, "uchange", UF_IMMUTABLE); + TEST(p, "uimmutable", UF_IMMUTABLE); +#ifdef UF_NOUNLINK + TEST(p, "uunlnk", UF_NOUNLINK); + TEST(p, "uunlink", UF_NOUNLINK); +#endif + /* FALLTHROUGH */ + default: + return (1); + } + } + return (0); +} diff --git a/lib/libc/gen/setflagsbyname.c b/lib/libc/gen/setflagsbyname.c new file mode 100644 index 0000000..5235eb3 --- /dev/null +++ b/lib/libc/gen/setflagsbyname.c @@ -0,0 +1,174 @@ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)stat_flags.c 8.1 (Berkeley) 5/31/93"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif +#endif /* not lint */ + +#include <sys/types.h> +#include <sys/stat.h> + +#include <stddef.h> +#include <string.h> + +#define SAPPEND(s) { \ + if (prefix != NULL) \ + (void)strcat(string, prefix); \ + (void)strcat(string, s); \ + prefix = ","; \ +} + +/* + * flags_to_string -- + * Convert stat flags to a comma-separated string. If no flags + * are set, return the default string. + */ +char * +flags_to_string(flags, def) + u_long flags; + char *def; +{ + static char string[128]; + char *prefix; + + string[0] = '\0'; + prefix = NULL; + if (flags & UF_APPEND) + SAPPEND("uappnd"); + if (flags & UF_IMMUTABLE) + SAPPEND("uchg"); +#ifdef UF_NOUNLINK + if (flags & UF_NOUNLINK) + SAPPEND("uunlnk"); +#endif + if (flags & UF_NODUMP) + SAPPEND("nodump"); + if (flags & UF_OPAQUE) + SAPPEND("opaque"); + if (flags & SF_APPEND) + SAPPEND("sappnd"); + if (flags & SF_ARCHIVED) + SAPPEND("arch"); + if (flags & SF_IMMUTABLE) + SAPPEND("schg"); +#ifdef SF_NOUNLINK + if (flags & SF_NOUNLINK) + SAPPEND("sunlnk"); +#endif + return (prefix == NULL && def != NULL ? def : string); +} + +#define TEST(a, b, f) { \ + if (!memcmp(a, b, sizeof(b))) { \ + if (clear) { \ + if (clrp) \ + *clrp |= (f); \ + } else if (setp) \ + *setp |= (f); \ + break; \ + } \ +} + +/* + * string_to_flags -- + * Take string of arguments and return stat flags. Return 0 on + * success, 1 on failure. On failure, stringp is set to point + * to the offending token. + */ +int +string_to_flags(stringp, setp, clrp) + char **stringp; + u_long *setp, *clrp; +{ + int clear; + char *string, *p; + + if (setp) + *setp = 0; + if (clrp) + *clrp = 0; + string = *stringp; + while ((p = strsep(&string, "\t ,")) != NULL) { + clear = 0; + *stringp = p; + if (*p == '\0') + continue; + if (p[0] == 'n' && p[1] == 'o') { + clear = 1; + p += 2; + } + switch (p[0]) { + case 'a': + TEST(p, "arch", SF_ARCHIVED); + TEST(p, "archived", SF_ARCHIVED); + return (1); + case 'd': + clear = !clear; + TEST(p, "dump", UF_NODUMP); + return (1); + case 'o': + TEST(p, "opaque", UF_OPAQUE); + return (1); + case 's': + TEST(p, "sappnd", SF_APPEND); + TEST(p, "sappend", SF_APPEND); + TEST(p, "schg", SF_IMMUTABLE); + TEST(p, "schange", SF_IMMUTABLE); + TEST(p, "simmutable", SF_IMMUTABLE); +#ifdef SF_NOUNLINK + TEST(p, "sunlnk", SF_NOUNLINK); + TEST(p, "sunlink", SF_NOUNLINK); +#endif + return (1); + case 'u': + TEST(p, "uappnd", UF_APPEND); + TEST(p, "uappend", UF_APPEND); + TEST(p, "uchg", UF_IMMUTABLE); + TEST(p, "uchange", UF_IMMUTABLE); + TEST(p, "uimmutable", UF_IMMUTABLE); +#ifdef UF_NOUNLINK + TEST(p, "uunlnk", UF_NOUNLINK); + TEST(p, "uunlink", UF_NOUNLINK); +#endif + /* FALLTHROUGH */ + default: + return (1); + } + } + return (0); +} diff --git a/lib/libc/gen/sethostname.c b/lib/libc/gen/sethostname.c new file mode 100644 index 0000000..6c3adc3 --- /dev/null +++ b/lib/libc/gen/sethostname.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)sethostname.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> + +#if __STDC__ +int +sethostname(const char *name, int namelen) +#else +int +sethostname(name, namelen) + char *name; + int namelen; +#endif +{ + int mib[2]; + + mib[0] = CTL_KERN; + mib[1] = KERN_HOSTNAME; + if (sysctl(mib, 2, NULL, NULL, (void *)name, namelen) == -1) + return (-1); + return (0); +} diff --git a/lib/libc/gen/setjmp.3 b/lib/libc/gen/setjmp.3 new file mode 100644 index 0000000..779239c --- /dev/null +++ b/lib/libc/gen/setjmp.3 @@ -0,0 +1,174 @@ +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)setjmp.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt SETJMP 3 +.Os BSD 4 +.Sh NAME +.Nm sigsetjmp , +.Nm siglongjmp , +.Nm setjmp , +.Nm longjmp , +.Nm _setjmp , +.Nm _longjmp , +.Nm longjmperror +.Nd non-local jumps +.Sh SYNOPSIS +.Fd #include <setjmp.h> +.Ft int +.Fn sigsetjmp "sigjmp_buf env" "int savemask" +.Ft void +.Fn siglongjmp "sigjmp_buf env" "int val" +.Ft int +.Fn setjmp "jmp_buf env" +.Ft void +.Fn longjmp "jmp_buf env" "int val" +.Ft int +.Fn _setjmp "jmp_buf env" +.Ft void +.Fn _longjmp "jmp_buf env" "int val" +.Ft void +.Fn longjmperror void +.Sh DESCRIPTION +The +.Fn sigsetjmp , +.Fn setjmp , +and +.Fn _setjmp +functions save their calling environment in +.Fa env . +Each of these functions returns 0. +.Pp +The corresponding +.Fn longjmp +functions restore the environment saved by their most recent respective +invocations +of the +.Fn setjmp +function. +They then return so that program execution continues as if the corresponding +invocation of the +.Fn setjmp +call had just returned the value specified by +.Fa val , +instead of 0. +.Pp +Pairs of calls may be intermixed, i.e. both +.Fn sigsetjmp +and +.Fn siglongjmp +and +.Fn setjmp +and +.Fn longjmp +combinations may be used in the same program, however, individual +calls may not, e.g. the +.Fa env +argument to +.Fn setjmp +may not be passed to +.Fn siglongjmp . +.Pp +The +.Fn longjmp +routines may not be called after the routine which called the +.Fn setjmp +routines returns. +.Pp +All accessible objects have values as of the time +.Fn longjmp +routine was called, except that the values of objects of automatic storage +invocation duration that do not have the +.Em volatile +type and have been changed between the +.Fn setjmp +invocation and +.Fn longjmp +call are indeterminate. +.Pp +The +.Fn setjmp Ns / Ns Fn longjmp +pairs save and restore the signal mask while +.Fn _setjmp Ns / Ns Fn _longjmp +pairs save and restore only the register set and the stack. +(See +.Fn sigprocmask 2 . ) +.Pp +The +.Fn sigsetjmp Ns / Ns Fn siglongjmp +function +pairs save and restore the signal mask if the argument +.Fa savemask +is non-zero, otherwise only the register set and the stack are saved. +.Sh ERRORS +If the contents of the +.Fa env +are corrupted, or correspond to an environment that has already returned, +the +.Fn longjmp +routine calls the routine +.Fn longjmperror 3 . +If +.Fn longjmperror +returns the program is aborted (see +.Xr abort 3 ) . +The default version of +.Fn longjmperror +prints the message +.Dq Li longjmp botch +to standard error and returns. +User programs wishing to exit more gracefully should write their own +versions of +.Fn longjmperror . +.Sh SEE ALSO +.Xr sigaction 2 , +.Xr sigaltstack 2 , +.Xr signal 3 +.Sh STANDARDS +The +.Fn setjmp +and +.Fn longjmp +functions conform to +.St -ansiC . +The +.Fn sigsetjmp +and +.Fn siglongjmp +functions conform to +.St -p1003.1-88 . diff --git a/lib/libc/gen/setjmperr.c b/lib/libc/gen/setjmperr.c new file mode 100644 index 0000000..26f83b5 --- /dev/null +++ b/lib/libc/gen/setjmperr.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 1980, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)setjmperr.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +/* + * This routine is called from longjmp() when an error occurs. + * Programs that wish to exit gracefully from this error may + * write their own versions. + * If this routine returns, the program is aborted. + */ + +#include <setjmp.h> +#include <unistd.h> + +void +longjmperror() +{ +#define ERRMSG "longjmp botch.\n" + (void)write(STDERR_FILENO, ERRMSG, sizeof(ERRMSG) - 1); +} diff --git a/lib/libc/gen/setmode.3 b/lib/libc/gen/setmode.3 new file mode 100644 index 0000000..20a26fe --- /dev/null +++ b/lib/libc/gen/setmode.3 @@ -0,0 +1,116 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)setmode.3 8.2 (Berkeley) 4/28/95 +.\" $FreeBSD$ +.\" +.Dd April 28, 1995 +.Dt SETMODE 3 +.Os +.Sh NAME +.Nm getmode , +.Nm setmode +.Nd modify mode bits +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft mode_t +.Fn getmode "const void *set" "mode_t mode" +.Ft void * +.Fn setmode "const char *mode_str" +.Sh DESCRIPTION +The +.Fn getmode +function +returns a copy of the file permission bits +.Fa mode +as altered by the values pointed to by +.Fa set . +While only the mode bits are altered, other parts of the file mode +may be examined. +.Pp +The +.Fn setmode +function +takes an absolute (octal) or symbolic value, as described in +.Xr chmod 1 , +as an argument +and returns a pointer to mode values to be supplied to +.Fn getmode . +Because some of the symbolic values are relative to the file +creation mask, +.Fn setmode +may call +.Xr umask 2 . +If this occurs, the file creation mask will be restored before +.Fn setmode +returns. +If the calling program changes the value of its file creation mask +after calling +.Fn setmode , +.Fn setmode +must be called again if +.Fn getmode +is to modify future file modes correctly. +.Pp +If the mode passed to +.Fn setmode +is invalid or if memory cannot be allocated for the return value, +.Fn setmode +returns +.Dv NULL . +.Pp +The value returned from +.Fn setmode +is obtained from +.Fn malloc +and should be returned to the system with +.Fn free +when the program is done with it, generally after a call to +.Fn getmode . +.Sh ERRORS +The +.Fn setmode +function +may fail and set errno for any of the errors specified for the library +routine +.Xr malloc 3 . +.Sh SEE ALSO +.Xr chmod 1 , +.Xr stat 2 , +.Xr umask 2 , +.Xr malloc 3 +.Sh HISTORY +The +.Fn getmode +and +.Fn setmode +functions first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/setmode.c b/lib/libc/gen/setmode.c new file mode 100644 index 0000000..2c12b81 --- /dev/null +++ b/lib/libc/gen/setmode.c @@ -0,0 +1,457 @@ +/* + * Copyright (c) 1989, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Dave Borman at Cray Research, Inc. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94"; +#endif +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/stat.h> + +#include <ctype.h> +#include <errno.h> +#include <signal.h> +#include <stddef.h> +#include <stdlib.h> + +#ifdef SETMODE_DEBUG +#include <stdio.h> +#endif + +#define SET_LEN 6 /* initial # of bitcmd struct to malloc */ +#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */ + +typedef struct bitcmd { + char cmd; + char cmd2; + mode_t bits; +} BITCMD; + +#define CMD2_CLR 0x01 +#define CMD2_SET 0x02 +#define CMD2_GBITS 0x04 +#define CMD2_OBITS 0x08 +#define CMD2_UBITS 0x10 + +static BITCMD *addcmd __P((BITCMD *, int, int, int, u_int)); +static void compress_mode __P((BITCMD *)); +#ifdef SETMODE_DEBUG +static void dumpmode __P((BITCMD *)); +#endif + +/* + * Given the old mode and an array of bitcmd structures, apply the operations + * described in the bitcmd structures to the old mode, and return the new mode. + * Note that there is no '=' command; a strict assignment is just a '-' (clear + * bits) followed by a '+' (set bits). + */ +mode_t +getmode(bbox, omode) + void *bbox; + mode_t omode; +{ + register BITCMD *set; + register mode_t clrval, newmode, value; + + set = (BITCMD *)bbox; + newmode = omode; + for (value = 0;; set++) + switch(set->cmd) { + /* + * When copying the user, group or other bits around, we "know" + * where the bits are in the mode so that we can do shifts to + * copy them around. If we don't use shifts, it gets real + * grundgy with lots of single bit checks and bit sets. + */ + case 'u': + value = (newmode & S_IRWXU) >> 6; + goto common; + + case 'g': + value = (newmode & S_IRWXG) >> 3; + goto common; + + case 'o': + value = newmode & S_IRWXO; +common: if (set->cmd2 & CMD2_CLR) { + clrval = + (set->cmd2 & CMD2_SET) ? S_IRWXO : value; + if (set->cmd2 & CMD2_UBITS) + newmode &= ~((clrval<<6) & set->bits); + if (set->cmd2 & CMD2_GBITS) + newmode &= ~((clrval<<3) & set->bits); + if (set->cmd2 & CMD2_OBITS) + newmode &= ~(clrval & set->bits); + } + if (set->cmd2 & CMD2_SET) { + if (set->cmd2 & CMD2_UBITS) + newmode |= (value<<6) & set->bits; + if (set->cmd2 & CMD2_GBITS) + newmode |= (value<<3) & set->bits; + if (set->cmd2 & CMD2_OBITS) + newmode |= value & set->bits; + } + break; + + case '+': + newmode |= set->bits; + break; + + case '-': + newmode &= ~set->bits; + break; + + case 'X': + if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH)) + newmode |= set->bits; + break; + + case '\0': + default: +#ifdef SETMODE_DEBUG + (void)printf("getmode:%04o -> %04o\n", omode, newmode); +#endif + return (newmode); + } +} + +#define ADDCMD(a, b, c, d) \ + if (set >= endset) { \ + register BITCMD *newset; \ + setlen += SET_LEN_INCR; \ + newset = realloc(saveset, sizeof(BITCMD) * setlen); \ + if (!saveset) \ + return (NULL); \ + set = newset + (set - saveset); \ + saveset = newset; \ + endset = newset + (setlen - 2); \ + } \ + set = addcmd(set, (a), (b), (c), (d)) + +#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) + +void * +setmode(p) + register char *p; +{ + register int perm, who; + register char op; + BITCMD *set, *saveset, *endset; + sigset_t sigset, sigoset; + mode_t mask; + int equalopdone=0, permXbits, setlen; + + if (!*p) + return (NULL); + + /* + * Get a copy of the mask for the permissions that are mask relative. + * Flip the bits, we want what's not set. Since it's possible that + * the caller is opening files inside a signal handler, protect them + * as best we can. + */ + sigfillset(&sigset); + (void)sigprocmask(SIG_BLOCK, &sigset, &sigoset); + (void)umask(mask = umask(0)); + mask = ~mask; + (void)sigprocmask(SIG_SETMASK, &sigoset, NULL); + + setlen = SET_LEN + 2; + + if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL) + return (NULL); + saveset = set; + endset = set + (setlen - 2); + + /* + * If an absolute number, get it and return; disallow non-octal digits + * or illegal bits. + */ + if (isdigit((unsigned char)*p)) { + perm = (mode_t)strtol(p, NULL, 8); + if (perm & ~(STANDARD_BITS|S_ISTXT)) { + free(saveset); + return (NULL); + } + while (*++p) + if (*p < '0' || *p > '7') { + free(saveset); + return (NULL); + } + ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); + return (saveset); + } + + /* + * Build list of structures to set/clear/copy bits as described by + * each clause of the symbolic mode. + */ + for (;;) { + /* First, find out which bits might be modified. */ + for (who = 0;; ++p) { + switch (*p) { + case 'a': + who |= STANDARD_BITS; + break; + case 'u': + who |= S_ISUID|S_IRWXU; + break; + case 'g': + who |= S_ISGID|S_IRWXG; + break; + case 'o': + who |= S_IRWXO; + break; + default: + goto getop; + } + } + +getop: if ((op = *p++) != '+' && op != '-' && op != '=') { + free(saveset); + return (NULL); + } + if (op == '=') + equalopdone = 0; + + who &= ~S_ISTXT; + for (perm = 0, permXbits = 0;; ++p) { + switch (*p) { + case 'r': + perm |= S_IRUSR|S_IRGRP|S_IROTH; + break; + case 's': + /* If only "other" bits ignore set-id. */ + if (!who || who & ~S_IRWXO) + perm |= S_ISUID|S_ISGID; + break; + case 't': + /* If only "other" bits ignore sticky. */ + if (!who || who & ~S_IRWXO) { + who |= S_ISTXT; + perm |= S_ISTXT; + } + break; + case 'w': + perm |= S_IWUSR|S_IWGRP|S_IWOTH; + break; + case 'X': + permXbits = S_IXUSR|S_IXGRP|S_IXOTH; + break; + case 'x': + perm |= S_IXUSR|S_IXGRP|S_IXOTH; + break; + case 'u': + case 'g': + case 'o': + /* + * When ever we hit 'u', 'g', or 'o', we have + * to flush out any partial mode that we have, + * and then do the copying of the mode bits. + */ + if (perm) { + ADDCMD(op, who, perm, mask); + perm = 0; + } + if (op == '=') + equalopdone = 1; + if (op == '+' && permXbits) { + ADDCMD('X', who, permXbits, mask); + permXbits = 0; + } + ADDCMD(*p, who, op, mask); + break; + + default: + /* + * Add any permissions that we haven't already + * done. + */ + if (perm || (op == '=' && !equalopdone)) { + if (op == '=') + equalopdone = 1; + ADDCMD(op, who, perm, mask); + perm = 0; + } + if (permXbits) { + ADDCMD('X', who, permXbits, mask); + permXbits = 0; + } + goto apply; + } + } + +apply: if (!*p) + break; + if (*p != ',') + goto getop; + ++p; + } + set->cmd = 0; +#ifdef SETMODE_DEBUG + (void)printf("Before compress_mode()\n"); + dumpmode(saveset); +#endif + compress_mode(saveset); +#ifdef SETMODE_DEBUG + (void)printf("After compress_mode()\n"); + dumpmode(saveset); +#endif + return (saveset); +} + +static BITCMD * +addcmd(set, op, who, oparg, mask) + BITCMD *set; + register int oparg, who; + register int op; + u_int mask; +{ + switch (op) { + case '=': + set->cmd = '-'; + set->bits = who ? who : STANDARD_BITS; + set++; + + op = '+'; + /* FALLTHROUGH */ + case '+': + case '-': + case 'X': + set->cmd = op; + set->bits = (who ? who : mask) & oparg; + break; + + case 'u': + case 'g': + case 'o': + set->cmd = op; + if (who) { + set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) | + ((who & S_IRGRP) ? CMD2_GBITS : 0) | + ((who & S_IROTH) ? CMD2_OBITS : 0); + set->bits = ~0; + } else { + set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS; + set->bits = mask; + } + + if (oparg == '+') + set->cmd2 |= CMD2_SET; + else if (oparg == '-') + set->cmd2 |= CMD2_CLR; + else if (oparg == '=') + set->cmd2 |= CMD2_SET|CMD2_CLR; + break; + } + return (set + 1); +} + +#ifdef SETMODE_DEBUG +static void +dumpmode(set) + register BITCMD *set; +{ + for (; set->cmd; ++set) + (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n", + set->cmd, set->bits, set->cmd2 ? " cmd2:" : "", + set->cmd2 & CMD2_CLR ? " CLR" : "", + set->cmd2 & CMD2_SET ? " SET" : "", + set->cmd2 & CMD2_UBITS ? " UBITS" : "", + set->cmd2 & CMD2_GBITS ? " GBITS" : "", + set->cmd2 & CMD2_OBITS ? " OBITS" : ""); +} +#endif + +/* + * Given an array of bitcmd structures, compress by compacting consecutive + * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u', + * 'g' and 'o' commands continue to be separate. They could probably be + * compacted, but it's not worth the effort. + */ +static void +compress_mode(set) + register BITCMD *set; +{ + register BITCMD *nset; + register int setbits, clrbits, Xbits, op; + + for (nset = set;;) { + /* Copy over any 'u', 'g' and 'o' commands. */ + while ((op = nset->cmd) != '+' && op != '-' && op != 'X') { + *set++ = *nset++; + if (!op) + return; + } + + for (setbits = clrbits = Xbits = 0;; nset++) { + if ((op = nset->cmd) == '-') { + clrbits |= nset->bits; + setbits &= ~nset->bits; + Xbits &= ~nset->bits; + } else if (op == '+') { + setbits |= nset->bits; + clrbits &= ~nset->bits; + Xbits &= ~nset->bits; + } else if (op == 'X') + Xbits |= nset->bits & ~setbits; + else + break; + } + if (clrbits) { + set->cmd = '-'; + set->cmd2 = 0; + set->bits = clrbits; + set++; + } + if (setbits) { + set->cmd = '+'; + set->cmd2 = 0; + set->bits = setbits; + set++; + } + if (Xbits) { + set->cmd = 'X'; + set->cmd2 = 0; + set->bits = Xbits; + set++; + } + } +} diff --git a/lib/libc/gen/setproctitle.3 b/lib/libc/gen/setproctitle.3 new file mode 100644 index 0000000..10eae65 --- /dev/null +++ b/lib/libc/gen/setproctitle.3 @@ -0,0 +1,101 @@ +.\" Copyright (c) 1995 Peter Wemm <peter@freebsd.org> +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, is permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice immediately at the beginning of the file, without modification, +.\" 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. This work was done expressly for inclusion into FreeBSD. Other use +.\" is permitted provided this notation is included. +.\" 4. Absolutely no warranty of function or purpose is made by the author +.\" Peter Wemm. +.\" 5. Modifications may be freely made to this file providing the above +.\" conditions are met. +.\" +.\" $FreeBSD$ +.\" +.\" The following requests are required for all man pages. +.Dd December 16, 1995 +.Os FreeBSD +.Dt SETPROCTITLE 3 +.Sh NAME +.Nm setproctitle +.Nd set the process title for +.Xr ps 1 +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <libutil.h> +.Ft void +.Fn setproctitle "const char *fmt" "..." +.Pp +Link with +.Va -lutil +on the +.Xr cc 1 +command line. +.Sh DESCRIPTION +The +.Fn setproctitle +library routine sets the process title that appears on the +.Xr ps 1 +command. +.Pp +The title is set from the executable's name, followed by the +result of a +.Xr printf 3 +style expansion of the arguments as specified by the +.Va fmt +argument. +.Pp +If +.Va fmt +is NULL, the process title is restored. +.Sh EXAMPLES +To set the title on a daemon to indicate its activity: +.Bd -literal -offset indent +setproctitle("talking to %s", inet_ntoa(addr)); +.Ed +.Sh SEE ALSO +.Xr ps 1 , +.Xr w 1 , +.Xr kvm 3 , +.Xr kvm_getargv 3 , +.Xr printf 3 +.Sh STANDARDS +.Fn setproctitle +is implicitly non-standard. Other methods of causing the +.Xr ps 1 +command line to change, including copying over the argv[0] string are +also implicitly non-portable. It is preferable to use an operating system +supplied +.Fn setproctitle +if present. +.Pp +Unfortunately, it is possible that there are other calling conventions +to other versions of +.Fn setproctitle , +although none have been found by the author as yet. This is believed to be +the predominant convention. +.Pp +It is thought that the implementation is compatible with other systems, +including +.Nx +and +.Tn BSD/OS . +.Sh HISTORY +.Fn setproctitle +first appeared in +.Fx 2.2 . +Other operating systems have +similar functions. +.Sh AUTHORS +.An Peter Wemm Aq peter@FreeBSD.org +stole the idea from the +.Sy "Sendmail 8.7.3" +source code by +.An Eric Allman Aq eric@sendmail.org . diff --git a/lib/libc/gen/setproctitle.c b/lib/libc/gen/setproctitle.c new file mode 100644 index 0000000..37b9fab --- /dev/null +++ b/lib/libc/gen/setproctitle.c @@ -0,0 +1,162 @@ +/* + * Copyright (c) 1995 Peter Wemm <peter@freebsd.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, is permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice immediately at the beginning of the file, without modification, + * 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. Absolutely no warranty of function or purpose is made by the author + * Peter Wemm. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <sys/param.h> +#include <sys/exec.h> +#include <sys/sysctl.h> + +#include <vm/vm.h> +#include <vm/vm_param.h> +#include <vm/pmap.h> + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> + +/* + * Older FreeBSD 2.0, 2.1 and 2.2 had different ps_strings structures and + * in different locations. + * 1: old_ps_strings at the very top of the stack. + * 2: old_ps_strings at SPARE_USRSPACE below the top of the stack. + * 3: ps_strings at the very top of the stack. + * This attempts to support a kernel built in the #2 and #3 era. + */ + +struct old_ps_strings { + char *old_ps_argvstr; + int old_ps_nargvstr; + char *old_ps_envstr; + int old_ps_nenvstr; +}; +#define OLD_PS_STRINGS ((struct old_ps_strings *) \ + (USRSTACK - SPARE_USRSPACE - sizeof(struct old_ps_strings))) + +#if defined(__STDC__) /* from other parts of sendmail */ +#include <stdarg.h> +#else +#include <varargs.h> +#endif + + +#define SPT_BUFSIZE 2048 /* from other parts of sendmail */ +extern char * __progname; /* is this defined in a .h anywhere? */ + +void +#if defined(__STDC__) +setproctitle(const char *fmt, ...) +#else +setproctitle(fmt, va_alist) + const char *fmt; + va_dcl +#endif +{ + static struct ps_strings *ps_strings; + static char buf[SPT_BUFSIZE]; + static char obuf[SPT_BUFSIZE]; + static char **oargv, *kbuf; + static int oargc = -1; + static char *nargv[2] = { buf, NULL }; + char **nargvp; + int nargc; + va_list ap; + size_t len; + unsigned long ul_ps_strings; + int oid[4]; + +#if defined(__STDC__) + va_start(ap, fmt); +#else + va_start(ap); +#endif + + if (fmt) { + buf[sizeof(buf) - 1] = '\0'; + + /* print program name heading for grep */ + (void) snprintf(buf, sizeof(buf), "%s: ", __progname); + + /* + * can't use return from sprintf, as that is the count of how + * much it wanted to write, not how much it actually did. + */ + + len = strlen(buf); + + /* print the argument string */ + (void) vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); + + nargvp = nargv; + nargc = 1; + kbuf = buf; + } else if (*obuf != '\0') { + /* Idea from NetBSD - reset the title on fmt == NULL */ + nargvp = oargv; + nargc = oargc; + kbuf = obuf; + } else + /* Nothing to restore */ + return; + + va_end(ap); + + /* Set the title into the kernel cached command line */ + oid[0] = CTL_KERN; + oid[1] = KERN_PROC; + oid[2] = KERN_PROC_ARGS; + oid[3] = getpid(); + sysctl(oid, 4, 0, 0, kbuf, strlen(kbuf) + 1); + + if (ps_strings == NULL) { + len = sizeof(ul_ps_strings); + if (sysctlbyname("kern.ps_strings", &ul_ps_strings, &len, NULL, + 0) == -1) + ul_ps_strings = PS_STRINGS; + ps_strings = (struct ps_strings *)ul_ps_strings; + } + + /* PS_STRINGS points to zeroed memory on a style #2 kernel */ + if (ps_strings->ps_argvstr) { + /* style #3 */ + if (oargc == -1) { + /* Record our original args */ + oargc = ps_strings->ps_nargvstr; + oargv = ps_strings->ps_argvstr; + for (nargc = len = 0; nargc < oargc; nargc++) { + snprintf(obuf + len, sizeof(obuf) - len, "%s%s", + len ? " " : "", oargv[nargc]); + if (len) + len++; + len += strlen(oargv[nargc]); + if (len >= sizeof(obuf)) + break; + } + } + ps_strings->ps_nargvstr = nargc; + ps_strings->ps_argvstr = nargvp; + } else { + /* style #2 - we can only restore our first arg :-( */ + if (*obuf == '\0') + strncpy(obuf, OLD_PS_STRINGS->old_ps_argvstr, + sizeof(obuf) - 1); + OLD_PS_STRINGS->old_ps_nargvstr = 1; + OLD_PS_STRINGS->old_ps_argvstr = nargvp[0]; + } +} diff --git a/lib/libc/gen/shmat.c b/lib/libc/gen/shmat.c new file mode 100644 index 0000000..45b9143 --- /dev/null +++ b/lib/libc/gen/shmat.c @@ -0,0 +1,19 @@ +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/shm.h> + +#if __STDC__ +void *shmat(int shmid, void *shmaddr, int shmflg) +#else +void *shmat(shmid, shmaddr, shmflg) + int shmid; + void *shmaddr; + int shmflg; +#endif +{ + return ((void *)shmsys(0, shmid, shmaddr, shmflg)); +} diff --git a/lib/libc/gen/shmctl.c b/lib/libc/gen/shmctl.c new file mode 100644 index 0000000..eae9fbb --- /dev/null +++ b/lib/libc/gen/shmctl.c @@ -0,0 +1,19 @@ +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/shm.h> + +#if __STDC__ +int shmctl(int shmid, int cmd, struct shmid_ds *buf) +#else +int shmctl(shmid, cmd, buf) + int shmid; + int cmd; + struct shmid_ds *buf; +#endif +{ + return (shmsys(4, shmid, cmd, buf)); +} diff --git a/lib/libc/gen/shmdt.c b/lib/libc/gen/shmdt.c new file mode 100644 index 0000000..f39ad18 --- /dev/null +++ b/lib/libc/gen/shmdt.c @@ -0,0 +1,17 @@ +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/shm.h> + +#if __STDC__ +int shmdt(void *shmaddr) +#else +int shmdt(shmaddr) + void *shmaddr; +#endif +{ + return (shmsys(2, shmaddr)); +} diff --git a/lib/libc/gen/shmget.c b/lib/libc/gen/shmget.c new file mode 100644 index 0000000..8976d1c --- /dev/null +++ b/lib/libc/gen/shmget.c @@ -0,0 +1,19 @@ +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/ipc.h> +#include <sys/shm.h> + +#if __STDC__ +int shmget(key_t key, int size, int shmflg) +#else +int shmget(key, size, shmflg) + key_t key; + int size; + int shmflg; +#endif +{ + return (shmsys(3, key, size, shmflg)); +} diff --git a/lib/libc/gen/siginterrupt.3 b/lib/libc/gen/siginterrupt.3 new file mode 100644 index 0000000..ffce99c --- /dev/null +++ b/lib/libc/gen/siginterrupt.3 @@ -0,0 +1,112 @@ +.\" Copyright (c) 1985, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)siginterrupt.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt SIGINTERRUPT 3 +.Os BSD 4.3 +.Sh NAME +.Nm siginterrupt +.Nd allow signals to interrupt system calls +.Sh SYNOPSIS +.Fd #include <signal.h> +.Ft int +.Fn siginterrupt "int sig" "int flag" +.Sh DESCRIPTION +The +.Fn siginterrupt +function +is used to change the system call restart +behavior when a system call is interrupted by the specified signal. +If the flag is false (0), then system calls will be restarted if +they are interrupted by the specified signal +and no data has been transferred yet. +System call restart has been the default behavior since +.Bx 4.2 , +and is the default behaviour for +.Xr signal 3 +on +.Tn FreeBSD . +.Pp +If the flag is true (1), +then restarting of system calls is disabled. +If a system call is interrupted by the specified signal +and no data has been transferred, +the system call will return \-1 with the global variable +.Va errno +set to +.Dv EINTR . +Interrupted system calls that have started transferring +data will return the amount of data actually transferred. +System call interrupt is the signal behavior found on +.Bx 4.1 +and +.At V +systems. +.Pp +Note that the new +.Bx 4.2 +signal handling semantics are not +altered in any other way. +Most notably, signal handlers always remain installed until +explicitly changed by a subsequent +.Xr sigaction 2 +call, and the signal mask operates as documented in +.Xr sigaction 2 . +Programs may switch between restartable and interruptible +system call operation as often as desired in the execution of a program. +.Pp +Issuing a +.Fn siginterrupt 3 +call during the execution of a signal handler will cause +the new action to take place on the next signal to be caught. +.Sh NOTES +This library routine uses an extension of the +.Xr sigaction 2 +system call that is not available in +.Bx 4.2 , +hence it should not be used if backward compatibility is needed. +.Sh RETURN VALUES +A 0 value indicates that the call succeeded. +A \-1 value indicates that an invalid signal number has been supplied. +.Sh SEE ALSO +.Xr sigaction 2 , +.Xr sigblock 2 , +.Xr sigpause 2 , +.Xr sigsetmask 2 , +.Xr signal 3 +.Sh HISTORY +The +.Fn siginterrupt +function appeared in +.Bx 4.3 . diff --git a/lib/libc/gen/siginterrupt.c b/lib/libc/gen/siginterrupt.c new file mode 100644 index 0000000..06960ab --- /dev/null +++ b/lib/libc/gen/siginterrupt.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)siginterrupt.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <signal.h> + +/* + * Set signal state to prevent restart of system calls + * after an instance of the indicated signal. + */ +int +siginterrupt(sig, flag) + int sig, flag; +{ + extern sigset_t _sigintr; + struct sigaction sa; + int ret; + + if ((ret = sigaction(sig, (struct sigaction *)0, &sa)) < 0) + return (ret); + if (flag) { + sigaddset(&_sigintr, sig); + sa.sa_flags &= ~SA_RESTART; + } else { + sigdelset(&_sigintr, sig); + sa.sa_flags |= SA_RESTART; + } + return (sigaction(sig, &sa, (struct sigaction *)0)); +} diff --git a/lib/libc/gen/siglist.c b/lib/libc/gen/siglist.c new file mode 100644 index 0000000..e6987d5 --- /dev/null +++ b/lib/libc/gen/siglist.c @@ -0,0 +1,110 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)siglist.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/cdefs.h> +#include <signal.h> + +const char *const sys_signame[NSIG] = { + "Signal 0", + "hup", /* SIGHUP */ + "int", /* SIGINT */ + "quit", /* SIGQUIT */ + "ill", /* SIGILL */ + "trap", /* SIGTRAP */ + "abrt", /* SIGABRT */ + "emt", /* SIGEMT */ + "fpe", /* SIGFPE */ + "kill", /* SIGKILL */ + "bus", /* SIGBUS */ + "segv", /* SIGSEGV */ + "sys", /* SIGSYS */ + "pipe", /* SIGPIPE */ + "alrm", /* SIGALRM */ + "term", /* SIGTERM */ + "urg", /* SIGURG */ + "stop", /* SIGSTOP */ + "tstp", /* SIGTSTP */ + "cont", /* SIGCONT */ + "chld", /* SIGCHLD */ + "ttin", /* SIGTTIN */ + "ttou", /* SIGTTOU */ + "io", /* SIGIO */ + "xcpu", /* SIGXCPU */ + "xfsz", /* SIGXFSZ */ + "vtalrm", /* SIGVTALRM */ + "prof", /* SIGPROF */ + "winch", /* SIGWINCH */ + "info", /* SIGINFO */ + "usr1", /* SIGUSR1 */ + "usr2", /* SIGUSR2 */ +}; + +const char *const sys_siglist[NSIG] = { + "Signal 0", + "Hangup", /* SIGHUP */ + "Interrupt", /* SIGINT */ + "Quit", /* SIGQUIT */ + "Illegal instruction", /* SIGILL */ + "Trace/BPT trap", /* SIGTRAP */ + "Abort trap", /* SIGABRT */ + "EMT trap", /* SIGEMT */ + "Floating point exception", /* SIGFPE */ + "Killed", /* SIGKILL */ + "Bus error", /* SIGBUS */ + "Segmentation fault", /* SIGSEGV */ + "Bad system call", /* SIGSYS */ + "Broken pipe", /* SIGPIPE */ + "Alarm clock", /* SIGALRM */ + "Terminated", /* SIGTERM */ + "Urgent I/O condition", /* SIGURG */ + "Suspended (signal)", /* SIGSTOP */ + "Suspended", /* SIGTSTP */ + "Continued", /* SIGCONT */ + "Child exited", /* SIGCHLD */ + "Stopped (tty input)", /* SIGTTIN */ + "Stopped (tty output)", /* SIGTTOU */ + "I/O possible", /* SIGIO */ + "Cputime limit exceeded", /* SIGXCPU */ + "Filesize limit exceeded", /* SIGXFSZ */ + "Virtual timer expired", /* SIGVTALRM */ + "Profiling timer expired", /* SIGPROF */ + "Window size changes", /* SIGWINCH */ + "Information request", /* SIGINFO */ + "User defined signal 1", /* SIGUSR1 */ + "User defined signal 2" /* SIGUSR2 */ +}; +const int sys_nsig = sizeof(sys_siglist) / sizeof(sys_siglist[0]); diff --git a/lib/libc/gen/signal.3 b/lib/libc/gen/signal.3 new file mode 100644 index 0000000..3da89d8 --- /dev/null +++ b/lib/libc/gen/signal.3 @@ -0,0 +1,232 @@ +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)signal.3 8.3 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd April 19, 1994 +.Dt SIGNAL 3 +.Os BSD 4 +.Sh NAME +.Nm signal +.Nd simplified software signal facilities +.Sh SYNOPSIS +.Fd #include <signal.h> +.\" The following is Quite Ugly, but syntactically correct. Don't try to +.\" fix it. +.Ft void \*(lp* +.Fn signal "int sig" "void \*(lp*func\*(rp\*(lpint\*(rp\*(rp\*(rp\*(lpint" + +or in FreeBSD's equivalent but easier to read typedef'd version: +.Ft typedef "void \*(lp*sig_t\*(rp \*(lpint\*(rp" +.Ft sig_t +.Fn signal "int sig" "sig_t func" +.Sh DESCRIPTION +This +.Fn signal +facility +is a simplified interface to the more general +.Xr sigaction 2 +facility. +.Pp +Signals allow the manipulation of a process from outside its +domain as well as allowing the process to manipulate itself or +copies of itself (children). There are two general types of signals: +those that cause termination of a process and those that do not. +Signals which cause termination of a program might result from +an irrecoverable error or might be the result of a user at a terminal +typing the `interrupt' character. +Signals are used when a process is stopped because it wishes to access +its control terminal while in the background (see +.Xr tty 4 ) . +Signals are optionally generated +when a process resumes after being stopped, +when the status of child processes changes, +or when input is ready at the control terminal. +Most signals result in the termination of the process receiving them +if no action +is taken; some signals instead cause the process receiving them +to be stopped, or are simply discarded if the process has not +requested otherwise. +Except for the +.Dv SIGKILL +and +.Dv SIGSTOP +signals, the +.Fn signal +function allows for a signal to be caught, to be ignored, or to generate +an interrupt. +These signals are defined in the file +.Aq Pa signal.h : +.Bl -column SIGVTALARMXX "create core imagexxx" +.It Sy Name Default Action Description +.It Dv SIGHUP Ta "terminate process" Ta "terminal line hangup" +.It Dv SIGINT Ta "terminate process" Ta "interrupt program" +.It Dv SIGQUIT Ta "create core image" Ta "quit program" +.It Dv SIGILL Ta "create core image" Ta "illegal instruction" +.It Dv SIGTRAP Ta "create core image" Ta "trace trap" +.It Dv SIGABRT Ta "create core image" Ta Xr abort 2 +call (formerly +.Dv SIGIOT ) +.It Dv SIGEMT Ta "create core image" Ta "emulate instruction executed" +.It Dv SIGFPE Ta "create core image" Ta "floating-point exception" +.It Dv SIGKILL Ta "terminate process" Ta "kill program" +.It Dv SIGBUS Ta "create core image" Ta "bus error" +.It Dv SIGSEGV Ta "create core image" Ta "segmentation violation" +.It Dv SIGSYS Ta "create core image" Ta "non-existent system call invoked" +.It Dv SIGPIPE Ta "terminate process" Ta "write on a pipe with no reader" +.It Dv SIGALRM Ta "terminate process" Ta "real-time timer expired" +.It Dv SIGTERM Ta "terminate process" Ta "software termination signal" +.It Dv SIGURG Ta "discard signal" Ta "urgent condition present on socket" +.It Dv SIGSTOP Ta "stop process" Ta "stop (cannot be caught or ignored)" +.It Dv SIGTSTP Ta "stop process" Ta "stop signal generated from keyboard" +.It Dv SIGCONT Ta "discard signal" Ta "continue after stop" +.It Dv SIGCHLD Ta "discard signal" Ta "child status has changed" +.It Dv SIGTTIN Ta "stop process" Ta "background read attempted from control terminal" +.It Dv SIGTTOU Ta "stop process" Ta "background write attempted to control terminal" +.It Dv SIGIO Ta "discard signal" Ta Tn "I/O" +is possible on a descriptor (see +.Xr fcntl 2 ) +.It Dv SIGXCPU Ta "terminate process" Ta "cpu time limit exceeded (see" +.Xr setrlimit 2 ) +.It Dv SIGXFSZ Ta "terminate process" Ta "file size limit exceeded (see" +.Xr setrlimit 2 ) +.It Dv SIGVTALRM Ta "terminate process" Ta "virtual time alarm (see" +.Xr setitimer 2 ) +.It Dv SIGPROF Ta "terminate process" Ta "profiling timer alarm (see" +.Xr setitimer 2 ) +.It Dv SIGWINCH Ta "discard signal" Ta "Window size change" +.It Dv SIGINFO Ta "discard signal" Ta "status request from keyboard" +.It Dv SIGUSR1 Ta "terminate process" Ta "User defined signal 1" +.It Dv SIGUSR2 Ta "terminate process" Ta "User defined signal 2" +.El +.Pp +The +.Fa sig +parameter specifies which signal was received. +The +.Fa func +procedure allows a user to choose the action upon receipt of a signal. +To set the default action of the signal to occur as listed above, +.Fa func +should be +.Dv SIG_DFL . +A +.Dv SIG_DFL +resets the default action. +To ignore the signal +.Fa func +should be +.Dv SIG_IGN . +This will cause subsequent instances of the signal to be ignored +and pending instances to be discarded. If +.Dv SIG_IGN +is not used, +further occurrences of the signal are +automatically blocked and +.Fa func +is called. +.Pp +The handled signal is unblocked when the +function returns and +the process continues from where it left off when the signal occurred. +.Bf -symbolic +Unlike previous signal facilities, the handler +func() remains installed after a signal has been delivered. +.Ef +.Pp +For some system calls, if a signal is caught while the call is +executing and the call is prematurely terminated, +the call is automatically restarted. +(The handler is installed using the +.Dv SA_RESTART +flag with +.Xr sigaction 2 . ) +The affected system calls include +.Xr read 2 , +.Xr write 2 , +.Xr sendto 2 , +.Xr recvfrom 2 , +.Xr sendmsg 2 +and +.Xr recvmsg 2 +on a communications channel or a low speed device +and during a +.Xr ioctl 2 +or +.Xr wait 2 . +However, calls that have already committed are not restarted, +but instead return a partial success (for example, a short read count). +This semantics could be changed with +.Xr siginterrupt 3 . +.Pp +When a process which has installed signal handlers forks, +the child process inherits the signals. +All caught signals may be reset to their default action by a call +to the +.Xr execve 2 +function; +ignored signals remain ignored. +.Sh RETURN VALUES +The previous action is returned on a successful call. +Otherwise, SIG_ERR is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +.Fn Signal +will fail and no action will take place if one of the +following occur: +.Bl -tag -width [EINVAL] +.It Bq Er EINVAL +.Em Sig +is not a valid signal number. +.It Bq Er EINVAL +An attempt is made to ignore or supply a handler for +.Dv SIGKILL +or +.Ev SIGSTOP . +.Sh SEE ALSO +.Xr kill 1 , +.Xr kill 2 , +.Xr ptrace 2 , +.Xr sigaction 2 , +.Xr sigaltstack 2 , +.Xr sigprocmask 2 , +.Xr sigsuspend 2 , +.Xr fpsetmask 3 , +.Xr setjmp 3 , +.Xr siginterrupt 3 , +.Xr tty 4 +.Sh HISTORY +This +.Nm signal +facility appeared in +.Bx 4.0 . diff --git a/lib/libc/gen/signal.c b/lib/libc/gen/signal.c new file mode 100644 index 0000000..c4057f5 --- /dev/null +++ b/lib/libc/gen/signal.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 1985, 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)signal.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +/* + * Almost backwards compatible signal. + */ +#include <signal.h> + +sigset_t _sigintr; /* shared with siginterrupt */ + +sig_t +signal(s, a) + int s; + sig_t a; +{ + struct sigaction sa, osa; + + sa.sa_handler = a; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + if (!sigismember(&_sigintr, s)) + sa.sa_flags |= SA_RESTART; + if (sigaction(s, &sa, &osa) < 0) + return (SIG_ERR); + return (osa.sa_handler); +} diff --git a/lib/libc/gen/sigsetops.3 b/lib/libc/gen/sigsetops.3 new file mode 100644 index 0000000..a32daf6 --- /dev/null +++ b/lib/libc/gen/sigsetops.3 @@ -0,0 +1,114 @@ +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)sigsetops.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt SIGSETOPS 3 +.Os +.Sh NAME +.Nm sigemptyset , +.Nm sigfillset , +.Nm sigaddset , +.Nm sigdelset , +.Nm sigismember +.Nd manipulate signal sets +.Sh SYNOPSIS +.Fd #include <signal.h> +.Ft int +.Fn sigemptyset "sigset_t *set" +.Ft int +.Fn sigfillset "sigset_t *set" +.Ft int +.Fn sigaddset "sigset_t *set" "int signo" +.Ft int +.Fn sigdelset "sigset_t *set" "int signo" +.Ft int +.Fn sigismember "const sigset_t *set" "int signo" +.Sh DESCRIPTION +These functions manipulate signal sets stored in a +.Fa sigset_t . +Either +.Fn sigemptyset +or +.Fn sigfillset +must be called for every object of type +.Fa sigset_t +before any other use of the object. +.Pp +The +.Fn sigemptyset +function initializes a signal set to be empty. +.Pp +The +.Fn sigfillset +function initializes a signal set to contain all signals. +.Pp +The +.Fn sigaddset +function adds the specified signal +.Fa signo +to the signal set. +.Pp +The +.Fn sigdelset +function deletes the specified signal +.Fa signo +from the signal set. +.Pp +The +.Fn sigismember +function returns whether a specified signal +.Fa signo +is contained in the signal set. +.Pp +These functions +are provided as macros in the include file <signal.h>. +Actual functions are available +if their names are undefined (with #undef +.Em name ) . +.Sh RETURN VALUES +The +.Fn sigismember +function returns 1 +if the signal is a member of the set, +0 otherwise. +The other functions return 0. +.Sh ERRORS +Currently no errors are detected. +.Sh SEE ALSO +.Xr kill 2 , +.Xr sigaction 2 , +.Xr sigsuspend 2 +.Sh STANDARDS +These functions are defined by +.St -p1003.1-88 . diff --git a/lib/libc/gen/sigsetops.c b/lib/libc/gen/sigsetops.c new file mode 100644 index 0000000..ffba20c --- /dev/null +++ b/lib/libc/gen/sigsetops.c @@ -0,0 +1,108 @@ +/*- + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * + * @(#)sigsetops.c 8.1 (Berkeley) 6/4/93 + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sigsetops.c 8.1 (Berkeley) 6/4/93"; +#endif +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <errno.h> +#include <signal.h> + +int +sigaddset(set, signo) + sigset_t *set; + int signo; +{ + + if (signo <= 0 || signo > _SIG_MAXSIG) { + errno = EINVAL; + return (-1); + } + set->__bits[_SIG_WORD(signo)] |= _SIG_BIT(signo); + return (0); +} + +int +sigdelset(set, signo) + sigset_t *set; + int signo; +{ + + if (signo <= 0 || signo > _SIG_MAXSIG) { + errno = EINVAL; + return (-1); + } + set->__bits[_SIG_WORD(signo)] &= ~_SIG_BIT(signo); + return (0); +} + +int +sigemptyset(set) + sigset_t *set; +{ + int i; + + for (i = 0; i < _SIG_WORDS; i++) + set->__bits[i] = 0; + return (0); +} + +int +sigfillset(set) + sigset_t *set; +{ + int i; + + for (i = 0; i < _SIG_WORDS; i++) + set->__bits[i] = ~0U; + return (0); +} + +int +sigismember(set, signo) + const sigset_t *set; + int signo; +{ + + if (signo <= 0 || signo > _SIG_MAXSIG) { + errno = EINVAL; + return (-1); + } + return ((set->__bits[_SIG_WORD(signo)] & _SIG_BIT(signo)) ? 1 : 0); +} diff --git a/lib/libc/gen/sleep.3 b/lib/libc/gen/sleep.3 new file mode 100644 index 0000000..e59aad5 --- /dev/null +++ b/lib/libc/gen/sleep.3 @@ -0,0 +1,84 @@ +.\" Copyright (c) 1986, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)sleep.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd Feb 13, 1998 +.Dt SLEEP 3 +.Os +.Sh NAME +.Nm sleep +.Nd suspend process execution for an interval measured in seconds +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft unsigned int +.Fn sleep "unsigned int seconds" +.Sh DESCRIPTION +The +.Fn sleep +function suspends execution of the calling process until either +.Fa seconds +seconds have elapsed or a signal is delivered to the process and its +action is to invoke a signal-catching function or to terminate the +process. +System activity may lengthen the sleep by an indeterminate amount. +.Pp +This function is implemented using +.Xr nanosleep 2 +by pausing for +.Fa seconds +seconds or until a signal occurs. +Consequently, in this implementation, +sleeping has no effect on the state of process timers, +and there is no special handling for SIGALRM. +.Sh RETURN VALUES +If the +.Fn sleep +function returns because the requested time has elapsed, the value +returned will be zero. If the +.Fn sleep +function returns due to the delivery of a signal, the value returned +will be the unslept amount (the requested time minus the time actually +slept) in seconds. +.Sh SEE ALSO +.Xr nanosleep 2 , +.Xr usleep 3 +.Sh STANDARDS +The +.Fn sleep +function conforms to +.St -p1003.1-90 . +.Sh HISTORY +A +.Fn sleep +function appeared in +.At v7 . diff --git a/lib/libc/gen/sleep.c b/lib/libc/gen/sleep.c new file mode 100644 index 0000000..f87ffba --- /dev/null +++ b/lib/libc/gen/sleep.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sleep.c 8.1 (Berkeley) 6/4/93"; +#endif +static char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <errno.h> +#include <limits.h> +#include <time.h> +#include <unistd.h> + +unsigned int +sleep(seconds) + unsigned int seconds; +{ + struct timespec time_to_sleep; + struct timespec time_remaining; + + /* + * Avoid overflow when `seconds' is huge. This assumes that + * the maximum value for a time_t is >= INT_MAX. + */ + if (seconds > INT_MAX) + return (seconds - INT_MAX + sleep(INT_MAX)); + + time_to_sleep.tv_sec = seconds; + time_to_sleep.tv_nsec = 0; + if (nanosleep(&time_to_sleep, &time_remaining) != -1) + return (0); + if (errno != EINTR) + return (seconds); /* best guess */ + return (time_remaining.tv_sec + + (time_remaining.tv_nsec != 0)); /* round up */ +} diff --git a/lib/libc/gen/srand48.c b/lib/libc/gen/srand48.c new file mode 100644 index 0000000..fd369a0 --- /dev/null +++ b/lib/libc/gen/srand48.c @@ -0,0 +1,30 @@ +/* + * Copyright (c) 1993 Martin Birgmeier + * All rights reserved. + * + * You may redistribute unmodified or modified versions of this source + * code provided that the above copyright notice and this and the + * following conditions are retained. + * + * This software is provided ``as is'', and comes with no warranties + * of any kind. I shall in no event be liable for anything that happens + * to anyone/anything when using this software. + */ + +#include "rand48.h" + +extern unsigned short _rand48_seed[3]; +extern unsigned short _rand48_mult[3]; +extern unsigned short _rand48_add; + +void +srand48(long seed) +{ + _rand48_seed[0] = RAND48_SEED_0; + _rand48_seed[1] = (unsigned short) seed; + _rand48_seed[2] = (unsigned short) (seed >> 16); + _rand48_mult[0] = RAND48_MULT_0; + _rand48_mult[1] = RAND48_MULT_1; + _rand48_mult[2] = RAND48_MULT_2; + _rand48_add = RAND48_ADD; +} diff --git a/lib/libc/gen/stringlist.3 b/lib/libc/gen/stringlist.3 new file mode 100644 index 0000000..21fb2db --- /dev/null +++ b/lib/libc/gen/stringlist.3 @@ -0,0 +1,122 @@ +.\" $NetBSD: stringlist.3,v 1.2 1997/04/09 08:59:25 kleink Exp $ +.\" +.\" Copyright (c) 1997 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This file was contributed to The NetBSD Foundation by Luke Mewburn. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the NetBSD +.\" Foundation, Inc. and its contributors. +.\" 4. Neither the name of The NetBSD Foundation nor the names of its +.\" contributors may be used to endorse or promote products derived +.\" from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 REGENTS 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. +.\" +.\" $FreeBSD$ +.\" +.Dd February 24, 1997 +.Os NetBSD 1.3 +.Dt STRINGLIST 3 +.Sh NAME +.Nm stringlist , +.Nm sl_init , +.Nm sl_add , +.Nm sl_free , +.Nm sl_find +.Nd stringlist manipulation functions +.Sh SYNOPSIS +.Fd #include <stringlist.h> +.Ft StringList * +.Fn sl_init +.Ft void +.Fn sl_add "StringList *sl" "char *item" +.Ft void +.Fn sl_free "StringList *sl" "int freeall" +.Ft char * +.Fn sl_find "StringList *sl" "char *item" +.Sh DESCRIPTION +The +.Nm +functions manipulate stringlists, which are lists of +strings that extend automatically if necessary. +.Pp +The +.Ar StringList +structure has the following definition: +.Bd -literal -offset indent +typedef struct _stringlist { + char **sl_str; + size_t sl_max; + size_t sl_cur; +} StringList; +.Ed +.Pp +.Bl -tag -width "sl_str" -offset indent +.It Ar sl_str +a pointer to the base of the array containing the list. +.It Ar sl_max +the size of +.Ar sl_str . +.It Ar sl_cur +the offset in +.Ar sl_str +of the current element. +.El +.Pp +The following stringlist manipulation functions are available: +.Bl -tag -width "sl_init()" +.It Fn sl_init +Create a stringlist. +Returns a pointer to a +.Ar StringList . +.It Fn sl_free +Releases memory occupied by +.Ar sl +and the +.Ar sl->sl_str +array. +If +.Ar freeall +is non-zero, then each of the items within +.Ar sl->sl_str +is released as well. +.It Fn sl_add +Add +.Ar item +to +.Ar sl->sl_str +at +.Ar sl->sl_cur , +extending the size of +.Ar sl->sl_str +.It Fn sl_find +Find +.Ar item +in +.Ar sl , +returning NULL if it's not found. +.El +.Sh SEE ALSO +.Xr free 3 , +.Xr malloc 3 diff --git a/lib/libc/gen/stringlist.c b/lib/libc/gen/stringlist.c new file mode 100644 index 0000000..bb74919 --- /dev/null +++ b/lib/libc/gen/stringlist.c @@ -0,0 +1,120 @@ +/* $NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $ */ + +/* + * Copyright (c) 1994 Christos Zoulas + * All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Christos Zoulas. + * 4. 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 ``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 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include <stdio.h> +#include <string.h> +#include <err.h> +#include <stdlib.h> +#include <stringlist.h> + +#define _SL_CHUNKSIZE 20 + +/* + * sl_init(): Initialize a string list + */ +StringList * +sl_init() +{ + StringList *sl = malloc(sizeof(StringList)); + if (sl == NULL) + err(1, "stringlist: %m"); + + sl->sl_cur = 0; + sl->sl_max = _SL_CHUNKSIZE; + sl->sl_str = malloc(sl->sl_max * sizeof(char *)); + if (sl->sl_str == NULL) + err(1, "stringlist: %m"); + return sl; +} + + +/* + * sl_add(): Add an item to the string list + */ +void +sl_add(sl, name) + StringList *sl; + char *name; +{ + if (sl->sl_cur == sl->sl_max - 1) { + sl->sl_max += _SL_CHUNKSIZE; + sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); + if (sl->sl_str == NULL) + err(1, "stringlist: %m"); + } + sl->sl_str[sl->sl_cur++] = name; +} + + +/* + * sl_free(): Free a stringlist + */ +void +sl_free(sl, all) + StringList *sl; + int all; +{ + size_t i; + + if (sl == NULL) + return; + if (sl->sl_str) { + if (all) + for (i = 0; i < sl->sl_cur; i++) + free(sl->sl_str[i]); + free(sl->sl_str); + } + free(sl); +} + + +/* + * sl_find(): Find a name in the string list + */ +char * +sl_find(sl, name) + StringList *sl; + char *name; +{ + size_t i; + + for (i = 0; i < sl->sl_cur; i++) + if (strcmp(sl->sl_str[i], name) == 0) + return sl->sl_str[i]; + + return NULL; +} diff --git a/lib/libc/gen/strtofflags.c b/lib/libc/gen/strtofflags.c new file mode 100644 index 0000000..5235eb3 --- /dev/null +++ b/lib/libc/gen/strtofflags.c @@ -0,0 +1,174 @@ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#ifndef lint +#if 0 +static char sccsid[] = "@(#)stat_flags.c 8.1 (Berkeley) 5/31/93"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif +#endif /* not lint */ + +#include <sys/types.h> +#include <sys/stat.h> + +#include <stddef.h> +#include <string.h> + +#define SAPPEND(s) { \ + if (prefix != NULL) \ + (void)strcat(string, prefix); \ + (void)strcat(string, s); \ + prefix = ","; \ +} + +/* + * flags_to_string -- + * Convert stat flags to a comma-separated string. If no flags + * are set, return the default string. + */ +char * +flags_to_string(flags, def) + u_long flags; + char *def; +{ + static char string[128]; + char *prefix; + + string[0] = '\0'; + prefix = NULL; + if (flags & UF_APPEND) + SAPPEND("uappnd"); + if (flags & UF_IMMUTABLE) + SAPPEND("uchg"); +#ifdef UF_NOUNLINK + if (flags & UF_NOUNLINK) + SAPPEND("uunlnk"); +#endif + if (flags & UF_NODUMP) + SAPPEND("nodump"); + if (flags & UF_OPAQUE) + SAPPEND("opaque"); + if (flags & SF_APPEND) + SAPPEND("sappnd"); + if (flags & SF_ARCHIVED) + SAPPEND("arch"); + if (flags & SF_IMMUTABLE) + SAPPEND("schg"); +#ifdef SF_NOUNLINK + if (flags & SF_NOUNLINK) + SAPPEND("sunlnk"); +#endif + return (prefix == NULL && def != NULL ? def : string); +} + +#define TEST(a, b, f) { \ + if (!memcmp(a, b, sizeof(b))) { \ + if (clear) { \ + if (clrp) \ + *clrp |= (f); \ + } else if (setp) \ + *setp |= (f); \ + break; \ + } \ +} + +/* + * string_to_flags -- + * Take string of arguments and return stat flags. Return 0 on + * success, 1 on failure. On failure, stringp is set to point + * to the offending token. + */ +int +string_to_flags(stringp, setp, clrp) + char **stringp; + u_long *setp, *clrp; +{ + int clear; + char *string, *p; + + if (setp) + *setp = 0; + if (clrp) + *clrp = 0; + string = *stringp; + while ((p = strsep(&string, "\t ,")) != NULL) { + clear = 0; + *stringp = p; + if (*p == '\0') + continue; + if (p[0] == 'n' && p[1] == 'o') { + clear = 1; + p += 2; + } + switch (p[0]) { + case 'a': + TEST(p, "arch", SF_ARCHIVED); + TEST(p, "archived", SF_ARCHIVED); + return (1); + case 'd': + clear = !clear; + TEST(p, "dump", UF_NODUMP); + return (1); + case 'o': + TEST(p, "opaque", UF_OPAQUE); + return (1); + case 's': + TEST(p, "sappnd", SF_APPEND); + TEST(p, "sappend", SF_APPEND); + TEST(p, "schg", SF_IMMUTABLE); + TEST(p, "schange", SF_IMMUTABLE); + TEST(p, "simmutable", SF_IMMUTABLE); +#ifdef SF_NOUNLINK + TEST(p, "sunlnk", SF_NOUNLINK); + TEST(p, "sunlink", SF_NOUNLINK); +#endif + return (1); + case 'u': + TEST(p, "uappnd", UF_APPEND); + TEST(p, "uappend", UF_APPEND); + TEST(p, "uchg", UF_IMMUTABLE); + TEST(p, "uchange", UF_IMMUTABLE); + TEST(p, "uimmutable", UF_IMMUTABLE); +#ifdef UF_NOUNLINK + TEST(p, "uunlnk", UF_NOUNLINK); + TEST(p, "uunlink", UF_NOUNLINK); +#endif + /* FALLTHROUGH */ + default: + return (1); + } + } + return (0); +} diff --git a/lib/libc/gen/sysconf.3 b/lib/libc/gen/sysconf.3 new file mode 100644 index 0000000..e18c045 --- /dev/null +++ b/lib/libc/gen/sysconf.3 @@ -0,0 +1,187 @@ +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)sysconf.3 8.3 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd April 19, 1994 +.Dt SYSCONF 3 +.Os BSD 4 +.Sh NAME +.Nm sysconf +.Nd get configurable system variables +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft long +.Fn sysconf "int name" +.Sh DESCRIPTION +.Pp +This interface is defined by +.St -p1003.1-88 . +A far more complete interface is available using +.Xr sysctl 3 . +.Pp +The +.Fn sysconf +function provides a method for applications to determine the current +value of a configurable system limit or option variable. +The +.Fa name +argument specifies the system variable to be queried. +Symbolic constants for each name value are found in the include file +.Li <unistd.h> . +.Pp +The available values are as follows: +.Pp +.Bl -tag -width "123456" +.Pp +.It Li _SC_ARG_MAX +The maximum bytes of argument to +.Xr execve 2 . +.It Li _SC_CHILD_MAX +The maximum number of simultaneous processes per user id. +.It Li _SC_CLK_TCK +The frequency of the statistics clock in ticks per second. +.It Li _SC_NGROUPS_MAX +The maximum number of supplemental groups. +.It Li _SC_OPEN_MAX +The maximum number of open files per user id. +.It Li _SC_STREAM_MAX +The minimum maximum number of streams that a process may have open +at any one time. +.It Li _SC_TZNAME_MAX +The minimum maximum number of types supported for the name of a +timezone. +.It Li _SC_JOB_CONTROL +Return 1 if job control is available on this system, otherwise \-1. +.It Li _SC_SAVED_IDS +Returns 1 if saved set-group and saved set-user ID is available, +otherwise \-1. +.It Li _SC_VERSION +The version of ISO/IEC 9945 (POSIX 1003.1) with which the system +attempts to comply. +.It Li _SC_BC_BASE_MAX +The maximum ibase/obase values in the +.Xr bc 1 +utility. +.It Li _SC_BC_DIM_MAX +The maximum array size in the +.Xr bc 1 +utility. +.It Li _SC_BC_SCALE_MAX +The maximum scale value in the +.Xr bc 1 +utility. +.It Li _SC_BC_STRING_MAX +The maximum string length in the +.Xr bc 1 +utility. +.It Li _SC_COLL_WEIGHTS_MAX +The maximum number of weights that can be assigned to any entry of +the LC_COLLATE order keyword in the locale definition file. +.It Li _SC_EXPR_NEST_MAX +The maximum number of expressions that can be nested within +parenthesis by the +.Xr expr 1 +utility. +.It Li _SC_LINE_MAX +The maximum length in bytes of a text-processing utility's input +line. +.It Li _SC_RE_DUP_MAX +The maximum number of repeated occurrences of a regular expression +permitted when using interval notation. +.It Li _SC_2_VERSION +The version of POSIX 1003.2 with which the system attempts to comply. +.It Li _SC_2_C_BIND +Return 1 if the system's C-language development facilities support the +C-Language Bindings Option, otherwise \-1. +.It Li _SC_2_C_DEV +Return 1 if the system supports the C-Language Development Utilities Option, +otherwise \-1. +.It Li _SC_2_CHAR_TERM +Return 1 if the system supports at least one terminal type capable of +all operations described in POSIX 1003.2, otherwise \-1. +.It Li _SC_2_FORT_DEV +Return 1 if the system supports the FORTRAN Development Utilities Option, +otherwise \-1. +.It Li _SC_2_FORT_RUN +Return 1 if the system supports the FORTRAN Runtime Utilities Option, +otherwise \-1. +.It Li _SC_2_LOCALEDEF +Return 1 if the system supports the creation of locales, otherwise \-1. +.It Li _SC_2_SW_DEV +Return 1 if the system supports the Software Development Utilities Option, +otherwise \-1. +.It Li _SC_2_UPE +Return 1 if the system supports the User Portability Utilities Option, +otherwise \-1. +.El +.Sh RETURN VALUES +If the call to +.Fn sysconf +is not successful, \-1 is returned and +.Va errno +is set appropriately. +Otherwise, if the variable is associated with functionality that is not +supported, \-1 is returned and +.Va errno +is not modified. +Otherwise, the current variable value is returned. +.Sh ERRORS +The +.Fn sysconf +function may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr sysctl 3 . +In addition, the following error may be reported: +.Bl -tag -width Er +.It Bq Er EINVAL +The value of the +.Fa name +argument is invalid. +.Sh SEE ALSO +.Xr sysctl 3 +.Sh BUGS +The value for _SC_STREAM_MAX is a minimum maximum, and required to be +the same as ANSI C's FOPEN_MAX, so the returned value is a ridiculously +small and misleading number. +.Sh STANDARDS +Except for the fact that values returned by +.Fn sysconf +may change over the lifetime of the calling process, +this function conforms to +.St -p1003.1-88 . +.Sh HISTORY +The +.Fn sysconf +function first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/sysconf.c b/lib/libc/gen/sysconf.c new file mode 100644 index 0000000..2298bb1 --- /dev/null +++ b/lib/libc/gen/sysconf.c @@ -0,0 +1,299 @@ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Sean Eric Fagan of Cygnus Support. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)sysconf.c 8.2 (Berkeley) 3/20/94"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/_posix.h> +#include <sys/param.h> +#include <sys/time.h> +#include <sys/sysctl.h> +#include <sys/resource.h> + +#include <errno.h> +#include <time.h> +#include <unistd.h> + +/* + * sysconf -- + * get configurable system variables. + * + * XXX + * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values + * not change during the lifetime of the calling process. This would seem + * to require that any change to system limits kill all running processes. + * A workaround might be to cache the values when they are first retrieved + * and then simply return the cached value on subsequent calls. This is + * less useful than returning up-to-date values, however. + */ +long +sysconf(name) + int name; +{ + struct rlimit rl; + size_t len; + int mib[2], value; + long defaultresult; + + len = sizeof(value); + defaultresult = -1; + + switch (name) { +/* 1003.1 */ + case _SC_ARG_MAX: + mib[0] = CTL_KERN; + mib[1] = KERN_ARGMAX; + break; + case _SC_CHILD_MAX: + return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur); + case _SC_CLK_TCK: + return (CLK_TCK); + case _SC_JOB_CONTROL: + mib[0] = CTL_KERN; + mib[1] = KERN_JOB_CONTROL; + goto yesno; + case _SC_NGROUPS_MAX: + mib[0] = CTL_KERN; + mib[1] = KERN_NGROUPS; + break; + case _SC_OPEN_MAX: + return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur); + case _SC_STREAM_MAX: + mib[0] = CTL_USER; + mib[1] = USER_STREAM_MAX; + break; + case _SC_TZNAME_MAX: + mib[0] = CTL_USER; + mib[1] = USER_TZNAME_MAX; + break; + case _SC_SAVED_IDS: + mib[0] = CTL_KERN; + mib[1] = KERN_SAVED_IDS; + goto yesno; + case _SC_VERSION: + mib[0] = CTL_KERN; + mib[1] = KERN_POSIX1; + break; + +/* 1003.2 */ + case _SC_BC_BASE_MAX: + mib[0] = CTL_USER; + mib[1] = USER_BC_BASE_MAX; + break; + case _SC_BC_DIM_MAX: + mib[0] = CTL_USER; + mib[1] = USER_BC_DIM_MAX; + break; + case _SC_BC_SCALE_MAX: + mib[0] = CTL_USER; + mib[1] = USER_BC_SCALE_MAX; + break; + case _SC_BC_STRING_MAX: + mib[0] = CTL_USER; + mib[1] = USER_BC_STRING_MAX; + break; + case _SC_COLL_WEIGHTS_MAX: + mib[0] = CTL_USER; + mib[1] = USER_COLL_WEIGHTS_MAX; + break; + case _SC_EXPR_NEST_MAX: + mib[0] = CTL_USER; + mib[1] = USER_EXPR_NEST_MAX; + break; + case _SC_LINE_MAX: + mib[0] = CTL_USER; + mib[1] = USER_LINE_MAX; + break; + case _SC_RE_DUP_MAX: + mib[0] = CTL_USER; + mib[1] = USER_RE_DUP_MAX; + break; + case _SC_2_VERSION: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_VERSION; + break; + case _SC_2_C_BIND: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_C_BIND; + goto yesno; + case _SC_2_C_DEV: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_C_DEV; + goto yesno; + case _SC_2_CHAR_TERM: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_CHAR_TERM; + goto yesno; + case _SC_2_FORT_DEV: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_FORT_DEV; + goto yesno; + case _SC_2_FORT_RUN: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_FORT_RUN; + goto yesno; + case _SC_2_LOCALEDEF: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_LOCALEDEF; + goto yesno; + case _SC_2_SW_DEV: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_SW_DEV; + goto yesno; + case _SC_2_UPE: + mib[0] = CTL_USER; + mib[1] = USER_POSIX2_UPE; + goto yesno; + +#ifdef _P1003_1B_VISIBLE + /* POSIX.1B */ + + case _SC_ASYNCHRONOUS_IO: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_ASYNCHRONOUS_IO; + goto yesno; + case _SC_MAPPED_FILES: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_MAPPED_FILES; + goto yesno; + case _SC_MEMLOCK: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_MEMLOCK; + goto yesno; + case _SC_MEMLOCK_RANGE: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_MEMLOCK_RANGE; + goto yesno; + case _SC_MEMORY_PROTECTION: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_MEMORY_PROTECTION; + goto yesno; + case _SC_MESSAGE_PASSING: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_MESSAGE_PASSING; + goto yesno; + case _SC_PRIORITIZED_IO: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_PRIORITIZED_IO; + goto yesno; + case _SC_PRIORITY_SCHEDULING: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_PRIORITY_SCHEDULING; + goto yesno; + case _SC_REALTIME_SIGNALS: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_REALTIME_SIGNALS; + goto yesno; + case _SC_SEMAPHORES: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_SEMAPHORES; + goto yesno; + case _SC_FSYNC: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_FSYNC; + goto yesno; + case _SC_SHARED_MEMORY_OBJECTS: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_SHARED_MEMORY_OBJECTS; + goto yesno; + case _SC_SYNCHRONIZED_IO: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_SYNCHRONIZED_IO; + goto yesno; + case _SC_TIMERS: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_TIMERS; + goto yesno; + case _SC_AIO_LISTIO_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_AIO_LISTIO_MAX; + goto yesno; + case _SC_AIO_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_AIO_MAX; + goto yesno; + case _SC_AIO_PRIO_DELTA_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX; + goto yesno; + case _SC_DELAYTIMER_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_DELAYTIMER_MAX; + goto yesno; + case _SC_MQ_OPEN_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_MQ_OPEN_MAX; + goto yesno; + case _SC_PAGESIZE: + defaultresult = getpagesize(); + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_PAGESIZE; + goto yesno; + case _SC_RTSIG_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_RTSIG_MAX; + goto yesno; + case _SC_SEM_NSEMS_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_SEM_NSEMS_MAX; + goto yesno; + case _SC_SEM_VALUE_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_SEM_VALUE_MAX; + goto yesno; + case _SC_SIGQUEUE_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_SIGQUEUE_MAX; + goto yesno; + case _SC_TIMER_MAX: + mib[0] = CTL_P1003_1B; + mib[1] = CTL_P1003_1B_TIMER_MAX; + goto yesno; +#endif /* _P1003_1B_VISIBLE */ + +yesno: if (sysctl(mib, 2, &value, &len, NULL, 0) == -1) + return (-1); + if (value == 0) + return (defaultresult); + return (value); + break; + default: + errno = EINVAL; + return (-1); + } + return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value); +} diff --git a/lib/libc/gen/sysctl.3 b/lib/libc/gen/sysctl.3 new file mode 100644 index 0000000..098d4f2 --- /dev/null +++ b/lib/libc/gen/sysctl.3 @@ -0,0 +1,746 @@ +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)sysctl.3 8.4 (Berkeley) 5/9/95 +.\" $FreeBSD$ +.\" +.Dd May 9, 1995 +.Dt SYSCTL 3 +.Os +.Sh NAME +.Nm sysctl , +.Nm sysctlbyname +.Nd get or set system information +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <sys/sysctl.h> +.Ft int +.Fn sysctl "int *name" "u_int namelen" "void *oldp" "size_t *oldlenp" "void *newp" "size_t newlen" +.Ft int +.Fn sysctlbyname "const char *name" "void *oldp" "size_t *oldlenp" "void *newp" "size_t newlen" +.Sh DESCRIPTION +The +.Fn sysctl +function retrieves system information and allows processes with +appropriate privileges to set system information. +The information available from +.Fn sysctl +consists of integers, strings, and tables. +Information may be retrieved and set from the command interface +using the +.Xr sysctl 8 +utility. +.Pp +Unless explicitly noted below, +.Fn sysctl +returns a consistent snapshot of the data requested. +Consistency is obtained by locking the destination +buffer into memory so that the data may be copied out without blocking. +Calls to +.Fn sysctl +are serialized to avoid deadlock. +.Pp +The state is described using a ``Management Information Base'' (MIB) +style name, listed in +.Fa name , +which is a +.Fa namelen +length array of integers. +.Pp +The +.Fn sysctlbyname +function accepts an ASCII representation of the name and internally +looks up the integer name vector. Apart from that, it behaves the same +as the standard +.Fn sysctl +function. +.Pp +The information is copied into the buffer specified by +.Fa oldp . +The size of the buffer is given by the location specified by +.Fa oldlenp +before the call, +and that location gives the amount of data copied after a successful call +and after a call that returns with the error code ENOMEM. +If the amount of data available is greater +than the size of the buffer supplied, +the call supplies as much data as fits in the buffer provided +and returns with the error code ENOMEM. +If the old value is not desired, +.Fa oldp +and +.Fa oldlenp +should be set to NULL. +.Pp +The size of the available data can be determined by calling +.Fn sysctl +with a NULL parameter for +.Fa oldp . +The size of the available data will be returned in the location pointed to by +.Fa oldlenp . +For some operations, the amount of space may change often. +For these operations, +the system attempts to round up so that the returned size is +large enough for a call to return the data shortly thereafter. +.Pp +To set a new value, +.Fa newp +is set to point to a buffer of length +.Fa newlen +from which the requested value is to be taken. +If a new value is not to be set, +.Fa newp +should be set to NULL and +.Fa newlen +set to 0. +.Pp +The top level names are defined with a CTL_ prefix in +.Pa <sys/sysctl.h> , +and are as follows. +The next and subsequent levels down are found in the include files +listed here, and described in separate sections below. +.Pp +.Bl -column CTLXMACHDEPXXX "Next level namesXXXXXX" -offset indent +.It Sy Pa Name Next level names Description +.It CTL\_DEBUG sys/sysctl.h Debugging +.It CTL\_VFS sys/mount.h Filesystem +.It CTL\_HW sys/sysctl.h Generic CPU, I/O +.It CTL\_KERN sys/sysctl.h High kernel limits +.It CTL\_MACHDEP sys/sysctl.h Machine dependent +.It CTL\_NET sys/socket.h Networking +.It CTL\_USER sys/sysctl.h User-level +.It CTL\_VM vm/vm_param.h Virtual memory +.El +.Pp +For example, the following retrieves the maximum number of processes allowed +in the system: +.Pp +.Bd -literal -offset indent -compact +int mib[2], maxproc; +size_t len; + +mib[0] = CTL_KERN; +mib[1] = KERN_MAXPROC; +len = sizeof(maxproc); +sysctl(mib, 2, &maxproc, &len, NULL, 0); +.Ed +.Pp +To retrieve the standard search path for the system utilities: +.Pp +.Bd -literal -offset indent -compact +int mib[2]; +size_t len; +char *p; + +mib[0] = CTL_USER; +mib[1] = USER_CS_PATH; +sysctl(mib, 2, NULL, &len, NULL, 0); +p = malloc(len); +sysctl(mib, 2, p, &len, NULL, 0); +.Ed +.Sh CTL_DEBUG +The debugging variables vary from system to system. +A debugging variable may be added or deleted without need to recompile +.Fn sysctl +to know about it. +Each time it runs, +.Fn sysctl +gets the list of debugging variables from the kernel and +displays their current values. +The system defines twenty +.Ns ( Va struct ctldebug ) +variables named +.Nm debug0 +through +.Nm debug19 . +They are declared as separate variables so that they can be +individually initialized at the location of their associated variable. +The loader prevents multiple use of the same variable by issuing errors +if a variable is initialized in more than one place. +For example, to export the variable +.Nm dospecialcheck +as a debugging variable, the following declaration would be used: +.Bd -literal -offset indent -compact +int dospecialcheck = 1; +struct ctldebug debug5 = { "dospecialcheck", &dospecialcheck }; +.Ed +.Sh CTL_VFS +A distinguished second level name, VFS_GENERIC, +is used to get general information about all filesystems. +One of its third level identifiers is VFS_MAXTYPENUM +that gives the highest valid filesystem type number. +Its other third level identifier is VFS_CONF that +returns configuration information about the filesystem +type given as a fourth level identifier (see +.Xr getvfsbyname 3 +as an example of its use). +The remaining second level identifiers are the +filesystem type number returned by a +.Xr statfs 2 +call or from VFS_CONF. +The third level identifiers available for each filesystem +are given in the header file that defines the mount +argument structure for that filesystem. +.Sh CTL_HW +The string and integer information available for the CTL_HW level +is detailed below. +The changeable column shows whether a process with appropriate +privilege may change the value. +.Bl -column "Second level nameXXXXXX" integerXXX -offset indent +.It Sy Pa Second level name Type Changeable +.It HW\_MACHINE string no +.It HW\_MODEL string no +.It HW\_NCPU integer no +.It HW\_BYTEORDER integer no +.It HW\_PHYSMEM integer no +.It HW\_USERMEM integer no +.It HW\_PAGESIZE integer no +.It HW\_FLOATINGPOINT integer no +.It HW\_MACHINE\_ARCH string no +.\".It HW\_DISKNAMES integer no +.\".It HW\_DISKSTATS integer no +.El +.Pp +.Bl -tag -width "123456" +.It Li HW_MACHINE +The machine class. +.It Li HW_MODEL +The machine model +.It Li HW_NCPU +The number of cpus. +.ne 1i +.It Li HW_BYTEORDER +The byteorder (4,321, or 1,234). +.It Li HW_PHYSMEM +The bytes of physical memory. +.It Li HW_USERMEM +The bytes of non-kernel memory. +.It Li HW_PAGESIZE +The software page size. +.It Li HW_FLOATINGPOINT +Nonzero if the floating point support is in hardware. +.It Li HW_MACHINE_ARCH +The machine dependent architecture type. +.\".It Fa HW_DISKNAMES +.\".It Fa HW_DISKSTATS +.El +.Sh CTL_KERN +The string and integer information available for the CTL_KERN level +is detailed below. +The changeable column shows whether a process with appropriate +privilege may change the value. +The types of data currently available are process information, +system vnodes, the open file entries, routing table entries, +virtual memory statistics, load average history, and clock rate +information. +.Bl -column "KERNXMAXFILESPERPROCXXX" "struct clockrateXXX" -offset indent +.It Sy Pa Second level name Type Changeable +.It KERN\_ARGMAX integer no +.It KERN\_BOOTFILE string yes +.It KERN\_BOOTTIME struct timeval no +.It KERN\_CLOCKRATE struct clockinfo no +.It KERN\_FILE struct file no +.It KERN\_HOSTID integer yes +.It KERN\_HOSTNAME string yes +.It KERN\_JOB\_CONTROL integer no +.It KERN\_MAXFILES integer yes +.It KERN\_MAXFILESPERPROC integer yes +.It KERN\_MAXPROC integer no +.It KERN\_MAXPROCPERUID integer yes +.It KERN\_MAXVNODES integer yes +.It KERN\_NGROUPS integer no +.It KERN\_NISDOMAINNAME string yes +.It KERN\_OSRELDATE integer no +.It KERN\_OSRELEASE string no +.It KERN\_OSREV integer no +.It KERN\_OSTYPE string no +.It KERN\_POSIX1 integer no +.It KERN\_PROC struct proc no +.It KERN\_PROF node not applicable +.It KERN\_SAVED\_IDS integer no +.It KERN\_SECURELVL integer raise only +.It KERN\_UPDATEINTERVAL integer no +.It KERN\_VERSION string no +.It KERN\_VNODE struct vnode no +.El +.ne 1i +.Pp +.Bl -tag -width "123456" +.It Li KERN_ARGMAX +The maximum bytes of argument to +.Xr execve 2 . +.It Li KERN_BOOTFILE +The full pathname of the file from which the kernel was loaded. +.It Li KERN_BOOTTIME +A +.Va struct timeval +structure is returned. +This structure contains the time that the system was booted. +.It Li KERN_CLOCKRATE +A +.Va struct clockinfo +structure is returned. +This structure contains the clock, statistics clock and profiling clock +frequencies, the number of micro-seconds per hz tick and the skew rate. +.It Li KERN_FILE +Return the entire file table. +The returned data consists of a single +.Va struct filehead +followed by an array of +.Va struct file , +whose size depends on the current number of such objects in the system. +.It Li KERN_HOSTID +Get or set the host id. +.It Li KERN_HOSTNAME +Get or set the hostname. +.It Li KERN_JOB_CONTROL +Return 1 if job control is available on this system, otherwise 0. +.It Li KERN_MAXFILES +The maximum number of files that may be open in the system. +.It Li KERN_MAXFILESPERPROC +The maximum number of files that may be open for a single process. +This limit only applies to processes with an effective uid of nonzero +at the time of the open request. +Files that have already been opened are not affected if the limit +or the effective uid is changed. +.It Li KERN_MAXPROC +The maximum number of concurrent processes the system will allow. +.It Li KERN_MAXPROCPERUID +The maximum number of concurrent processes the system will allow +for a single effective uid. +This limit only applies to processes with an effective uid of nonzero +at the time of a fork request. +Processes that have already been started are not affected if the limit +is changed. +.It Li KERN_MAXVNODES +The maximum number of vnodes available on the system. +.It Li KERN_NGROUPS +The maximum number of supplemental groups. +.It Li KERN_NISDOMAINNAME +The name of the current YP/NIS domain. +.It Li KERN_OSRELDATE +The system release date in YYYYMM format +(January 1996 is encoded as 199601). +.It Li KERN_OSRELEASE +The system release string. +.It Li KERN_OSREV +The system revision string. +.It Li KERN_OSTYPE +The system type string. +.It Li KERN_POSIX1 +The version of ISO/IEC 9945 (POSIX 1003.1) with which the system +attempts to comply. +.It Li KERN_PROC +Return the entire process table, or a subset of it. +An array of +.Va struct kinfo_proc +structures is returned, +whose size depends on the current number of such objects in the system. +The third and fourth level names are as follows: +.Bl -column "Third level nameXXXXXX" "Fourth level is:XXXXXX" -offset indent +.It Pa Third level name Fourth level is: +.It KERN\_PROC\_ALL None +.It KERN\_PROC\_PID A process ID +.It KERN\_PROC\_PGRP A process group +.It KERN\_PROC\_TTY A tty device +.It KERN\_PROC\_UID A user ID +.It KERN\_PROC\_RUID A real user ID +.El +.It Li KERN_PROF +Return profiling information about the kernel. +If the kernel is not compiled for profiling, +attempts to retrieve any of the KERN_PROF values will +fail with EOPNOTSUPP. +The third level names for the string and integer profiling information +is detailed below. +The changeable column shows whether a process with appropriate +privilege may change the value. +.Bl -column "GPROFXGMONPARAMXXX" "struct gmonparamXXX" -offset indent +.It Sy Pa Third level name Type Changeable +.It GPROF\_STATE integer yes +.It GPROF\_COUNT u_short[\|] yes +.It GPROF\_FROMS u_short[\|] yes +.It GPROF\_TOS struct tostruct yes +.It GPROF\_GMONPARAM struct gmonparam no +.El +.Pp +The variables are as follows: +.Bl -tag -width "123456" +.It Li GPROF_STATE +Returns GMON_PROF_ON or GMON_PROF_OFF to show that profiling +is running or stopped. +.It Li GPROF_COUNT +Array of statistical program counter counts. +.It Li GPROF_FROMS +Array indexed by program counter of call-from points. +.It Li GPROF_TOS +Array of +.Va struct tostruct +describing destination of calls and their counts. +.It Li GPROF_GMONPARAM +Structure giving the sizes of the above arrays. +.El +.ne 1i +.It Li KERN_SAVED_IDS +Returns 1 if saved set-group and saved set-user ID is available. +.It Li KERN_SECURELVL +The system security level. +This level may be raised by processes with appropriate privilege. +It may not be lowered. +.It Li KERN_VERSION +The system version string. +.It Li KERN_VNODE +Return the entire vnode table. +Note, the vnode table is not necessarily a consistent snapshot of +the system. +The returned data consists of an array whose size depends on the +current number of such objects in the system. +Each element of the array contains the kernel address of a vnode +.Va struct vnode * +followed by the vnode itself +.Va struct vnode . +.It Li KERN_UPDATEINTERVAL +The interval between +.Xr sync 2 +calls in the +.Xr update 4 +process. +.El +.Sh CTL_MACHDEP +The set of variables defined is architecture dependent. +The following variables are defined for the i386 architecture. +.Bl -column "CONSOLE_DEVICEXXX" "struct bootinfoXXX" -offset indent +.It Sy Pa Second level name Type Changeable +.It Li CPU_CONSDEV dev_t no +.It Li CPU_ADJKERNTZ int yes +.It Li CPU_DISRTCSET int yes +.It Li CPU_BOOTINFO struct bootinfo no +.It Li CPU_WALLCLOCK int yes +.El +.Sh CTL_NET +The string and integer information available for the CTL_NET level +is detailed below. +The changeable column shows whether a process with appropriate +privilege may change the value. +.Bl -column "Second level nameXXXXXX" "routing messagesXXX" -offset indent +.It Sy Pa Second level name Type Changeable +.It PF\_ROUTE routing messages no +.It PF\_INET internet values yes +.El +.Pp +.Bl -tag -width "123456" +.It Li PF_ROUTE +Return the entire routing table or a subset of it. +The data is returned as a sequence of routing messages (see +.Xr route 4 +for the header file, format and meaning). +The length of each message is contained in the message header. +.Pp +The third level name is a protocol number, which is currently always 0. +The fourth level name is an address family, which may be set to 0 to +select all address families. +The fifth and sixth level names are as follows: +.Bl -column "Fifth level nameXXXXXX" "Sixth level is:XXX" -offset indent +.It Pa Fifth level name Sixth level is: +.It NET\_RT\_FLAGS rtflags +.It NET\_RT\_DUMP None +.It NET\_RT\_IFLIST None +.El +.It Li PF_INET +Get or set various global information about the internet protocols. +The third level name is the protocol. +The fourth level name is the variable name. +The currently defined protocols and names are: +.ne 1i +.Bl -column ProtocolXX VariableXX TypeXX ChangeableXX +.It Pa Protocol Variable Type Changeable +.It icmp bmcastecho integer yes +.It icmp maskrepl integer yes +.It ip forwarding integer yes +.It ip redirect integer yes +.It ip ttl integer yes +.It udp checksum integer yes +.El +.Pp +The variables are as follows: +.Bl -tag -width "123456" +.It Li icmp.bmcastecho +Returns 1 if an ICMP echo request to a broadcast or multicast address is +to be answered. +.It Li icmp.maskrepl +Returns 1 if ICMP network mask requests are to be answered. +.It Li ip.forwarding +Returns 1 when IP forwarding is enabled for the host, +meaning that the host is acting as a router. +.It Li ip.redirect +Returns 1 when ICMP redirects may be sent by the host. +This option is ignored unless the host is routing IP packets, +and should normally be enabled on all systems. +.It Li ip.ttl +The maximum time-to-live (hop count) value for an IP packet sourced by +the system. +This value applies to normal transport protocols, not to ICMP. +.It Li udp.checksum +Returns 1 when UDP checksums are being computed and checked. +Disabling UDP checksums is strongly discouraged. +.El +.Sh CTL_USER +The string and integer information available for the CTL_USER level +is detailed below. +The changeable column shows whether a process with appropriate +privilege may change the value. +.Bl -column "USER_COLL_WEIGHTS_MAXXXX" "integerXXX" -offset indent +.It Sy Pa Second level name Type Changeable +.It USER\_BC\_BASE\_MAX integer no +.It USER\_BC\_DIM\_MAX integer no +.It USER\_BC\_SCALE\_MAX integer no +.It USER\_BC\_STRING\_MAX integer no +.It USER\_COLL\_WEIGHTS\_MAX integer no +.It USER\_CS\_PATH string no +.It USER\_EXPR\_NEST\_MAX integer no +.It USER\_LINE\_MAX integer no +.It USER\_POSIX2\_CHAR\_TERM integer no +.It USER\_POSIX2\_C\_BIND integer no +.It USER\_POSIX2\_C\_DEV integer no +.It USER\_POSIX2\_FORT\_DEV integer no +.It USER\_POSIX2\_FORT\_RUN integer no +.It USER\_POSIX2\_LOCALEDEF integer no +.It USER\_POSIX2\_SW\_DEV integer no +.It USER\_POSIX2\_UPE integer no +.It USER\_POSIX2\_VERSION integer no +.It USER\_RE\_DUP\_MAX integer no +.It USER\_STREAM\_MAX integer no +.It USER\_TZNAME\_MAX integer no +.El +.Bl -tag -width "123456" +.Pp +.It Li USER_BC_BASE_MAX +The maximum ibase/obase values in the +.Xr bc 1 +utility. +.It Li USER_BC_DIM_MAX +The maximum array size in the +.Xr bc 1 +utility. +.It Li USER_BC_SCALE_MAX +The maximum scale value in the +.Xr bc 1 +utility. +.It Li USER_BC_STRING_MAX +The maximum string length in the +.Xr bc 1 +utility. +.It Li USER_COLL_WEIGHTS_MAX +The maximum number of weights that can be assigned to any entry of +the LC_COLLATE order keyword in the locale definition file. +.It Li USER_CS_PATH +Return a value for the +.Ev PATH +environment variable that finds all the standard utilities. +.It Li USER_EXPR_NEST_MAX +The maximum number of expressions that can be nested within +parenthesis by the +.Xr expr 1 +utility. +.It Li USER_LINE_MAX +The maximum length in bytes of a text-processing utility's input +line. +.It Li USER_POSIX2_CHAR_TERM +Return 1 if the system supports at least one terminal type capable of +all operations described in POSIX 1003.2, otherwise 0. +.It Li USER_POSIX2_C_BIND +Return 1 if the system's C-language development facilities support the +C-Language Bindings Option, otherwise 0. +.It Li USER_POSIX2_C_DEV +Return 1 if the system supports the C-Language Development Utilities Option, +otherwise 0. +.It Li USER_POSIX2_FORT_DEV +Return 1 if the system supports the FORTRAN Development Utilities Option, +otherwise 0. +.It Li USER_POSIX2_FORT_RUN +Return 1 if the system supports the FORTRAN Runtime Utilities Option, +otherwise 0. +.It Li USER_POSIX2_LOCALEDEF +Return 1 if the system supports the creation of locales, otherwise 0. +.It Li USER_POSIX2_SW_DEV +Return 1 if the system supports the Software Development Utilities Option, +otherwise 0. +.It Li USER_POSIX2_UPE +Return 1 if the system supports the User Portability Utilities Option, +otherwise 0. +.It Li USER_POSIX2_VERSION +The version of POSIX 1003.2 with which the system attempts to comply. +.It Li USER_RE_DUP_MAX +The maximum number of repeated occurrences of a regular expression +permitted when using interval notation. +.ne 1i +.It Li USER_STREAM_MAX +The minimum maximum number of streams that a process may have open +at any one time. +.It Li USER_TZNAME_MAX +The minimum maximum number of types supported for the name of a +timezone. +.El +.Sh CTL_VM +The string and integer information available for the CTL_VM level +is detailed below. +The changeable column shows whether a process with appropriate +privilege may change the value. +.Bl -column "Second level nameXXXXXX" "struct loadavgXXX" -offset indent +.It Sy Pa Second level name Type Changeable +.It VM\_LOADAVG struct loadavg no +.It VM\_METER struct vmtotal no +.It VM\_PAGEOUT\_ALGORITHM integer yes +.It VM\_SWAPPING\_ENABLED integer maybe +.It VM\_V\_CACHE\_MAX integer yes +.It VM\_V\_CACHE\_MIN integer yes +.It VM\_V\_FREE\_MIN integer yes +.It VM\_V\_FREE\_RESERVED integer yes +.It VM\_V\_FREE\_TARGET integer yes +.It VM\_V\_INACTIVE\_TARGET integer yes +.It VM\_V\_PAGEOUT\_FREE\_MIN integer yes +.El +.Pp +.Bl -tag -width "123456" +.It Li VM_LOADAVG +Return the load average history. +The returned data consists of a +.Va struct loadavg . +.It Li VM_METER +Return the system wide virtual memory statistics. +The returned data consists of a +.Va struct vmtotal . +.It Li VM_PAGEOUT_ALGORITHM +0 if the statistics-based page management algorithm is in use +or 1 if the near-LRU algorithm is in use. +.It Li VM_SWAPPING_ENABLED +1 if process swapping is enabled or 0 if disabled. This variable is +permanently set to 0 if the kernel was built with swapping disabled. +.It Li VM_V_CACHE_MAX +Maximum desired size of the cache queue. +.It Li VM_V_CACHE_MIN +Minimum desired size of the cache queue. If the cache queue size +falls very far below this value, the pageout daemon is awakened. +.It Li VM_V_FREE_MIN +Minimum amount of memory (cache memory plus free memory) +required to be available before a process waiting on memory will be +awakened. +.It Li VM_V_FREE_RESERVED +Processes will awaken the pageout daemon and wait for memory if the +number of free and cached pages drops below this value. +.It Li VM_V_FREE_TARGET +The total amount of free memory (including cache memory) that the +pageout daemon tries to maintain. +.It Li VM_V_INACTIVE_TARGET +The desired number of inactive pages that the pageout daemon should +achieve when it runs. Inactive pages can be quickly inserted into +process address space when needed. +.It Li VM_V_PAGEOUT_FREE_MIN +If the amount of free and cache memory falls below this value, the +pageout daemon will enter "memory conserving mode" to avoid deadlock. +.El +.Sh RETURN VALUES +.Fn sysctl +and +.Fn sysctlbyname +return 0 when successful. +Otherwise \-1 is returned and +.Va errno +is set appropriately. +.Sh ERRORS +The following errors may be reported: +.Bl -tag -width Er +.It Bq Er EFAULT +The buffer +.Fa name , +.Fa oldp , +.Fa newp , +or length pointer +.Fa oldlenp +contains an invalid address. +.It Bq Er EINVAL +The +.Fa name +array is less than two or greater than CTL_MAXNAME. +.It Bq Er EINVAL +A non-null +.Fa newp +is given and its specified length in +.Fa newlen +is too large or too small. +.It Bq Er ENOMEM +The length pointed to by +.Fa oldlenp +is too short to hold the requested value. +.It Bq Er ENOTDIR +The +.Fa name +array specifies an intermediate rather than terminal name. +.It Bq Er EISDIR +The +.Fa name +array specifies a terminal name, but the actual name is not terminal. +.It Bq Er EOPNOTSUPP +The +.Fa name +array specifies a value that is unknown. +.It Bq Er EPERM +An attempt is made to set a read-only value. +.It Bq Er EPERM +A process without appropriate privilege attempts to set a value. +.El +.Sh FILES +.Bl -tag -width <netinet/icmpXvar.h> -compact +.It Pa <sys/sysctl.h> +definitions for top level identifiers, second level kernel and hardware +identifiers, and user level identifiers +.It Pa <sys/socket.h> +definitions for second level network identifiers +.It Pa <sys/gmon.h> +definitions for third level profiling identifiers +.It Pa <vm/vm_param.h> +definitions for second level virtual memory identifiers +.It Pa <netinet/in.h> +definitions for third level Internet identifiers and +fourth level IP identifiers +.It Pa <netinet/icmp_var.h> +definitions for fourth level ICMP identifiers +.It Pa <netinet/udp_var.h> +definitions for fourth level UDP identifiers +.El +.Sh SEE ALSO +.Xr sysconf 3 , +.Xr sysctl 8 +.Sh HISTORY +The +.Fn sysctl +function first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/sysctl.c b/lib/libc/gen/sysctl.c new file mode 100644 index 0000000..19709d5 --- /dev/null +++ b/lib/libc/gen/sysctl.c @@ -0,0 +1,182 @@ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)sysctl.c 8.2 (Berkeley) 1/4/94"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> + +#include <errno.h> +#include <limits.h> +#include <paths.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> + +int +sysctl(name, namelen, oldp, oldlenp, newp, newlen) + int *name; + u_int namelen; + void *oldp, *newp; + size_t *oldlenp, newlen; +{ + if (name[0] != CTL_USER) + return (__sysctl(name, namelen, oldp, oldlenp, newp, newlen)); + + if (newp != NULL) { + errno = EPERM; + return (-1); + } + if (namelen != 2) { + errno = EINVAL; + return (-1); + } + + switch (name[1]) { + case USER_CS_PATH: + if (oldp && *oldlenp < sizeof(_PATH_STDPATH)) { + errno = ENOMEM; + return -1; + } + *oldlenp = sizeof(_PATH_STDPATH); + if (oldp != NULL) + memmove(oldp, _PATH_STDPATH, sizeof(_PATH_STDPATH)); + return (0); + } + + if (oldp && *oldlenp < sizeof(int)) { + errno = ENOMEM; + return (-1); + } + *oldlenp = sizeof(int); + if (oldp == NULL) + return (0); + + switch (name[1]) { + case USER_BC_BASE_MAX: + *(int *)oldp = BC_BASE_MAX; + return (0); + case USER_BC_DIM_MAX: + *(int *)oldp = BC_DIM_MAX; + return (0); + case USER_BC_SCALE_MAX: + *(int *)oldp = BC_SCALE_MAX; + return (0); + case USER_BC_STRING_MAX: + *(int *)oldp = BC_STRING_MAX; + return (0); + case USER_COLL_WEIGHTS_MAX: + *(int *)oldp = COLL_WEIGHTS_MAX; + return (0); + case USER_EXPR_NEST_MAX: + *(int *)oldp = EXPR_NEST_MAX; + return (0); + case USER_LINE_MAX: + *(int *)oldp = LINE_MAX; + return (0); + case USER_RE_DUP_MAX: + *(int *)oldp = RE_DUP_MAX; + return (0); + case USER_POSIX2_VERSION: + *(int *)oldp = _POSIX2_VERSION; + return (0); + case USER_POSIX2_C_BIND: +#ifdef POSIX2_C_BIND + *(int *)oldp = 1; +#else + *(int *)oldp = 0; +#endif + return (0); + case USER_POSIX2_C_DEV: +#ifdef POSIX2_C_DEV + *(int *)oldp = 1; +#else + *(int *)oldp = 0; +#endif + return (0); + case USER_POSIX2_CHAR_TERM: +#ifdef POSIX2_CHAR_TERM + *(int *)oldp = 1; +#else + *(int *)oldp = 0; +#endif + return (0); + case USER_POSIX2_FORT_DEV: +#ifdef POSIX2_FORT_DEV + *(int *)oldp = 1; +#else + *(int *)oldp = 0; +#endif + return (0); + case USER_POSIX2_FORT_RUN: +#ifdef POSIX2_FORT_RUN + *(int *)oldp = 1; +#else + *(int *)oldp = 0; +#endif + return (0); + case USER_POSIX2_LOCALEDEF: +#ifdef POSIX2_LOCALEDEF + *(int *)oldp = 1; +#else + *(int *)oldp = 0; +#endif + return (0); + case USER_POSIX2_SW_DEV: +#ifdef POSIX2_SW_DEV + *(int *)oldp = 1; +#else + *(int *)oldp = 0; +#endif + return (0); + case USER_POSIX2_UPE: +#ifdef POSIX2_UPE + *(int *)oldp = 1; +#else + *(int *)oldp = 0; +#endif + return (0); + case USER_STREAM_MAX: + *(int *)oldp = FOPEN_MAX; + return (0); + case USER_TZNAME_MAX: + *(int *)oldp = NAME_MAX; + return (0); + default: + errno = EINVAL; + return (-1); + } + /* NOTREACHED */ +} diff --git a/lib/libc/gen/sysctlbyname.c b/lib/libc/gen/sysctlbyname.c new file mode 100644 index 0000000..3d19ca2 --- /dev/null +++ b/lib/libc/gen/sysctlbyname.c @@ -0,0 +1,37 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp + * ---------------------------------------------------------------------------- + * + * $FreeBSD$ + * + */ +#include <sys/types.h> +#include <sys/sysctl.h> +#include <string.h> + +int +sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, + size_t newlen) +{ + int name2oid_oid[2]; + int real_oid[CTL_MAXNAME+2]; + int error; + size_t oidlen; + + name2oid_oid[0] = 0; /* This is magic & undocumented! */ + name2oid_oid[1] = 3; + + oidlen = sizeof(real_oid); + error = sysctl(name2oid_oid, 2, real_oid, &oidlen, (void *)name, + strlen(name)); + if (error < 0) + return error; + oidlen /= sizeof (int); + error = sysctl(real_oid, oidlen, oldp, oldlenp, newp, newlen); + return (error); +} + diff --git a/lib/libc/gen/syslog.3 b/lib/libc/gen/syslog.3 new file mode 100644 index 0000000..437b622 --- /dev/null +++ b/lib/libc/gen/syslog.3 @@ -0,0 +1,274 @@ +.\" Copyright (c) 1985, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)syslog.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt SYSLOG 3 +.Os BSD 4.2 +.Sh NAME +.Nm syslog , +.Nm vsyslog , +.Nm openlog , +.Nm closelog , +.Nm setlogmask +.Nd control system log +.Sh SYNOPSIS +.Fd #include <syslog.h> +.Fd #include <varargs.h> +.Ft void +.Fn syslog "int priority" "const char *message" "..." +.Ft void +.Fn vsyslog "int priority" "const char *message" "va_list args" +.Ft void +.Fn openlog "const char *ident" "int logopt" "int facility" +.Ft void +.Fn closelog void +.Ft int +.Fn setlogmask "int maskpri" +.Sh DESCRIPTION +The +.Fn syslog +function +writes +.Fa message +to the system message logger. +The message is then written to the system console, log files, +logged-in users, or forwarded to other machines as appropriate. (See +.Xr syslogd 8 . ) +.Pp +The message is identical to a +.Xr printf 3 +format string, except that +.Ql %m +is replaced by the current error +message. (As denoted by the global variable +.Va errno ; +see +.Xr strerror 3 . ) +A trailing newline is added if none is present. +.Pp +The +.Fn vsyslog +function +is an alternate form in which the arguments have already been captured +using the variable-length argument facilities of +.Xr varargs 3 . +.Pp +The message is tagged with +.Fa priority . +Priorities are encoded as a +.Fa facility +and a +.Em level . +The facility describes the part of the system +generating the message. +The level is selected from the following +.Em ordered +(high to low) list: +.Bl -tag -width LOG_AUTHPRIV +.It Dv LOG_EMERG +A panic condition. +This is normally broadcast to all users. +.It Dv LOG_ALERT +A condition that should be corrected immediately, such as a corrupted +system database. +.It Dv LOG_CRIT +Critical conditions, e.g., hard device errors. +.It Dv LOG_ERR +Errors. +.It Dv LOG_WARNING +Warning messages. +.It Dv LOG_NOTICE +Conditions that are not error conditions, +but should possibly be handled specially. +.It Dv LOG_INFO +Informational messages. +.It Dv LOG_DEBUG +Messages that contain information +normally of use only when debugging a program. +.El +.Pp +The +.Fn openlog +function +provides for more specialized processing of the messages sent +by +.Fn syslog +and +.Fn vsyslog . +The parameter +.Fa ident +is a string that will be prepended to every message. +The +.Fa logopt +argument +is a bit field specifying logging options, which is formed by +.Tn OR Ns 'ing +one or more of the following values: +.Bl -tag -width LOG_AUTHPRIV +.It Dv LOG_CONS +If +.Fn syslog +cannot pass the message to +.Xr syslogd 8 +it will attempt to write the message to the console +.Pq Dq Pa /dev/console. +.It Dv LOG_NDELAY +Open the connection to +.Xr syslogd 8 +immediately. +Normally the open is delayed until the first message is logged. +Useful for programs that need to manage the order in which file +descriptors are allocated. +.It Dv LOG_PERROR +Write the message to standard error output as well to the system log. +.It Dv LOG_PID +Log the process id with each message: useful for identifying +instantiations of daemons. +.El +.Pp +The +.Fa facility +parameter encodes a default facility to be assigned to all messages +that do not have an explicit facility encoded: +.Bl -tag -width LOG_AUTHPRIV +.It Dv LOG_AUTH +The authorization system: +.Xr login 1 , +.Xr su 1 , +.Xr getty 8 , +etc. +.It Dv LOG_AUTHPRIV +The same as +.Dv LOG_AUTH , +but logged to a file readable only by +selected individuals. +.It Dv LOG_CRON +The cron daemon: +.Xr cron 8 . +.It Dv LOG_DAEMON +System daemons, such as +.Xr routed 8 , +that are not provided for explicitly by other facilities. +.It Dv LOG_FTP +The file transfer protocol daemons: +.Xr ftpd 8 , +.Xr tftpd 8 . +.It Dv LOG_KERN +Messages generated by the kernel. +These cannot be generated by any user processes. +.It Dv LOG_LPR +The line printer spooling system: +.Xr lpr 1 , +.Xr lpc 8 , +.Xr lpd 8 , +etc. +.It Dv LOG_MAIL +The mail system. +.It Dv LOG_NEWS +The network news system. +.It Dv LOG_SECURITY +Security subsystems, such as +.It Dv LOG_SYSLOG +Messages generated internally by +.Xr syslogd 8 . +.It Dv LOG_USER +Messages generated by random user processes. +This is the default facility identifier if none is specified. +.It Dv LOG_UUCP +The uucp system. +.Xr ipfw 4 . +.It Dv LOG_LOCAL0 +Reserved for local use. +Similarly for +.Dv LOG_LOCAL1 +through +.Dv LOG_LOCAL7 . +.El +.Pp +The +.Fn closelog +function +can be used to close the log file. +.Pp +The +.Fn setlogmask +function +sets the log priority mask to +.Fa maskpri +and returns the previous mask. +Calls to +.Fn syslog +with a priority not set in +.Fa maskpri +are rejected. +The mask for an individual priority +.Fa pri +is calculated by the macro +.Fn LOG_MASK pri ; +the mask for all priorities up to and including +.Fa toppri +is given by the macro +.Fn LOG_UPTO toppri ; . +The default allows all priorities to be logged. +.Sh RETURN VALUES +The routines +.Fn closelog , +.Fn openlog , +.Fn syslog +and +.Fn vsyslog +return no value. +.Pp +The routine +.Fn setlogmask +always returns the previous log mask level. +.Sh EXAMPLES +.Bd -literal -offset indent -compact +syslog(LOG_ALERT, "who: internal error 23"); + +openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); + +setlogmask(LOG_UPTO(LOG_ERR)); + +syslog(LOG_INFO, "Connection from host %d", CallingHost); + +syslog(LOG_INFO|LOG_LOCAL2, "foobar error: %m"); +.Ed +.Sh SEE ALSO +.Xr logger 1 , +.Xr syslogd 8 +.Sh HISTORY +These +functions appeared in +.Bx 4.2 . diff --git a/lib/libc/gen/syslog.c b/lib/libc/gen/syslog.c new file mode 100644 index 0000000..3a9eca4 --- /dev/null +++ b/lib/libc/gen/syslog.c @@ -0,0 +1,365 @@ +/* + * Copyright (c) 1983, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +/* +static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95"; +*/ +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/syslog.h> +#include <sys/uio.h> +#include <sys/un.h> +#include <netdb.h> + +#include <errno.h> +#include <fcntl.h> +#include <paths.h> +#include <stdio.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#if __STDC__ +#include <stdarg.h> +#else +#include <varargs.h> +#endif + +static int LogFile = -1; /* fd for log */ +static int connected; /* have done connect */ +static int opened; /* have done openlog() */ +static int LogStat = 0; /* status bits, set by openlog() */ +static const char *LogTag = NULL; /* string to tag the entry with */ +static int LogFacility = LOG_USER; /* default facility code */ +static int LogMask = 0xff; /* mask of priorities to be logged */ +extern char *__progname; /* Program name, from crt0. */ + +static void disconnectlog __P((void)); /* disconnect from syslogd */ +static void connectlog __P((void)); /* (re)connect to syslogd */ + +/* + * Format of the magic cookie passed through the stdio hook + */ +struct bufcookie { + char *base; /* start of buffer */ + int left; +}; + +/* + * stdio write hook for writing to a static string buffer + * XXX: Maybe one day, dynamically allocate it so that the line length + * is `unlimited'. + */ +static +int writehook(cookie, buf, len) + void *cookie; /* really [struct bufcookie *] */ + char *buf; /* characters to copy */ + int len; /* length to copy */ +{ + struct bufcookie *h; /* private `handle' */ + + h = (struct bufcookie *)cookie; + if (len > h->left) { + /* clip in case of wraparound */ + len = h->left; + } + if (len > 0) { + (void)memcpy(h->base, buf, len); /* `write' it. */ + h->base += len; + h->left -= len; + } + return 0; +} + +/* + * syslog, vsyslog -- + * print message on log file; output is intended for syslogd(8). + */ +void +#if __STDC__ +syslog(int pri, const char *fmt, ...) +#else +syslog(pri, fmt, va_alist) + int pri; + char *fmt; + va_dcl +#endif +{ + va_list ap; + +#if __STDC__ + va_start(ap, fmt); +#else + va_start(ap); +#endif + vsyslog(pri, fmt, ap); + va_end(ap); +} + +void +vsyslog(pri, fmt, ap) + int pri; + register const char *fmt; + va_list ap; +{ + register int cnt; + register char ch, *p; + time_t now; + int fd, saved_errno; + char *stdp, tbuf[2048], fmt_cpy[1024]; + FILE *fp, *fmt_fp; + struct bufcookie tbuf_cookie; + struct bufcookie fmt_cookie; + +#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID + /* Check for invalid bits. */ + if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { + syslog(INTERNALLOG, + "syslog: unknown facility/priority: %x", pri); + pri &= LOG_PRIMASK|LOG_FACMASK; + } + + /* Check priority against setlogmask values. */ + if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) + return; + + saved_errno = errno; + + /* Set default facility if none specified. */ + if ((pri & LOG_FACMASK) == 0) + pri |= LogFacility; + + /* Create the primary stdio hook */ + tbuf_cookie.base = tbuf; + tbuf_cookie.left = sizeof(tbuf); + fp = fwopen(&tbuf_cookie, writehook); + if (fp == NULL) + return; + + /* Build the message. */ + (void)time(&now); + (void)fprintf(fp, "<%d>", pri); + (void)fprintf(fp, "%.15s ", ctime(&now) + 4); + if (LogStat & LOG_PERROR) { + /* Transfer to string buffer */ + (void)fflush(fp); + stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left); + } + if (LogTag == NULL) + LogTag = __progname; + if (LogTag != NULL) + (void)fprintf(fp, "%s", LogTag); + if (LogStat & LOG_PID) + (void)fprintf(fp, "[%d]", getpid()); + if (LogTag != NULL) { + (void)fprintf(fp, ": "); + } + + /* Check to see if we can skip expanding the %m */ + if (strstr(fmt, "%m")) { + + /* Create the second stdio hook */ + fmt_cookie.base = fmt_cpy; + fmt_cookie.left = sizeof(fmt_cpy) - 1; + fmt_fp = fwopen(&fmt_cookie, writehook); + if (fmt_fp == NULL) { + fclose(fp); + return; + } + + /* Substitute error message for %m. */ + for ( ; (ch = *fmt); ++fmt) + if (ch == '%' && fmt[1] == 'm') { + ++fmt; + fputs(strerror(saved_errno), fmt_fp); + } else + fputc(ch, fmt_fp); + + /* Null terminate if room */ + fputc(0, fmt_fp); + fclose(fmt_fp); + + /* Guarantee null termination */ + fmt_cpy[sizeof(fmt_cpy) - 1] = '\0'; + + fmt = fmt_cpy; + } + + (void)vfprintf(fp, fmt, ap); + (void)fclose(fp); + + cnt = sizeof(tbuf) - tbuf_cookie.left; + + /* Output to stderr if requested. */ + if (LogStat & LOG_PERROR) { + struct iovec iov[2]; + register struct iovec *v = iov; + + v->iov_base = stdp; + v->iov_len = cnt - (stdp - tbuf); + ++v; + v->iov_base = "\n"; + v->iov_len = 1; + (void)writev(STDERR_FILENO, iov, 2); + } + + /* Get connected, output the message to the local logger. */ + if (!opened) + openlog(LogTag, LogStat | LOG_NDELAY, 0); + connectlog(); + if (send(LogFile, tbuf, cnt, 0) >= 0) + return; + + /* + * If the send() failed, the odds are syslogd was restarted. + * Make one (only) attempt to reconnect to /dev/log. + */ + disconnectlog(); + connectlog(); + if (send(LogFile, tbuf, cnt, 0) >= 0) + return; + + /* + * Output the message to the console; don't worry about blocking, + * if console blocks everything will. Make sure the error reported + * is the one from the syslogd failure. + */ + if (LogStat & LOG_CONS && + (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) { + struct iovec iov[2]; + register struct iovec *v = iov; + + p = strchr(tbuf, '>') + 1; + v->iov_base = p; + v->iov_len = cnt - (p - tbuf); + ++v; + v->iov_base = "\r\n"; + v->iov_len = 2; + (void)writev(fd, iov, 2); + (void)close(fd); + } +} +static void +disconnectlog() +{ + /* + * If the user closed the FD and opened another in the same slot, + * that's their problem. They should close it before calling on + * system services. + */ + if (LogFile != -1) { + close(LogFile); + LogFile = -1; + } + connected = 0; /* retry connect */ +} + +static void +connectlog() +{ + struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ + + if (LogFile == -1) { + if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) + return; + (void)fcntl(LogFile, F_SETFD, 1); + } + if (LogFile != -1 && !connected) { + SyslogAddr.sun_len = sizeof(SyslogAddr); + SyslogAddr.sun_family = AF_UNIX; + (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, + sizeof SyslogAddr.sun_path); + connected = connect(LogFile, (struct sockaddr *)&SyslogAddr, + sizeof(SyslogAddr)) != -1; + + if (!connected) { + /* + * Try the old "/dev/log" path, for backward + * compatibility. + */ + (void)strncpy(SyslogAddr.sun_path, _PATH_OLDLOG, + sizeof SyslogAddr.sun_path); + connected = connect(LogFile, + (struct sockaddr *)&SyslogAddr, + sizeof(SyslogAddr)) != -1; + } + + if (!connected) { + (void)close(LogFile); + LogFile = -1; + } + } +} + +void +openlog(ident, logstat, logfac) + const char *ident; + int logstat, logfac; +{ + if (ident != NULL) + LogTag = ident; + LogStat = logstat; + if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) + LogFacility = logfac; + + if (LogStat & LOG_NDELAY) /* open immediately */ + connectlog(); + + opened = 1; /* ident and facility has been set */ +} + +void +closelog() +{ + (void)close(LogFile); + LogFile = -1; + connected = 0; +} + +/* setlogmask -- set the log mask level */ +int +setlogmask(pmask) + int pmask; +{ + int omask; + + omask = LogMask; + if (pmask != 0) + LogMask = pmask; + return (omask); +} diff --git a/lib/libc/gen/tcgetpgrp.3 b/lib/libc/gen/tcgetpgrp.3 new file mode 100644 index 0000000..7a026b7 --- /dev/null +++ b/lib/libc/gen/tcgetpgrp.3 @@ -0,0 +1,80 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)tcgetpgrp.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt TCGETPGRP 3 +.Os +.Sh NAME +.Nm tcgetpgrp +.Nd get foreground process group ID +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <unistd.h> +.Ft pid_t +.Fn tcgetpgrp "int fd" +.Sh DESCRIPTION +The +.Fn tcgetpgrp +function returns the value of the process group ID of the foreground +process group associated with the terminal device. +If there is no foreground process group, +.Fn tcgetpgrp +returns an invalid process ID. +.Sh ERRORS +If an error occurs, +.Fn tcgetpgrp +returns -1 and the global variable +.Va errno +is set to indicate the error, as follows: +.Bl -tag -width Er +.It Bq Er EBADF +The +.Fa fd +argument is not a valid file descriptor. +.It Bq Er ENOTTY +The calling process does not have a controlling terminal or the +underlying terminal device represented by +.Fa fd +is not the controlling terminal. +.El +.Sh SEE ALSO +.Xr setpgid 2 , +.Xr setsid 2 , +.Xr tcsetpgrp 3 +.Sh STANDARDS +The +.Fn tcgetpgrp +function is expected to be compliant with the +.St -p1003.1-88 +specification. diff --git a/lib/libc/gen/tcsendbreak.3 b/lib/libc/gen/tcsendbreak.3 new file mode 100644 index 0000000..6b5f754 --- /dev/null +++ b/lib/libc/gen/tcsendbreak.3 @@ -0,0 +1,155 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)tcsendbreak.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt TCSENDBREAK 3 +.Os +.Sh NAME +.Nm tcsendbreak , +.Nm tcdrain , +.Nm tcflush , +.Nm tcflow +.Nd line control functions +.Sh SYNOPSIS +.Fd #include <termios.h> +.Ft int +.Fn tcdrain "int fd" +.Ft int +.Fn tcflow "int fd" "int action" +.Ft int +.Fn tcflush "int fd" "int action" +.Ft int +.Fn tcsendbreak "int fd" "int len" +.Sh DESCRIPTION +The +.Fn tcdrain +function waits until all output written to the terminal referenced by +.Fa fd +has been transmitted to the terminal. +.Pp +The +.Fn tcflow +function suspends transmission of data to or the reception of data from +the terminal referenced by +.Fa fd +depending on the value of +.Fa action . +The value of +.Fa action +must be one of the following: +.Bl -tag -width "TCIOFF" +.It Fa TCOOFF +Suspend output. +.It Fa TCOON +Restart suspended output. +.It Fa TCIOFF +Transmit a STOP character, which is intended to cause the terminal to stop +transmitting data to the system. +(See the description of IXOFF in the +.Ql Input Modes +section of +.Xr termios 4 ). +.It Fa TCION +Transmit a START character, which is intended to cause the terminal to start +transmitting data to the system. +(See the description of IXOFF in the +.Ql Input Modes +section of +.Xr termios 4 ). +.El +.Pp +The +.Fn tcflush +function discards any data written to the terminal referenced by +.Fa fd +which has not been transmitted to the terminal, or any data received +from the terminal but not yet read, depending on the value of +.Fa action . +The value of +.Fa action +must be one of the following: +.Bl -tag -width "TCIOFLUSH" +.It Fa TCIFLUSH +Flush data received but not read. +.It Fa TCOFLUSH +Flush data written but not transmitted. +.It Fa TCIOFLUSH +Flush both data received but not read and data written but not transmitted. +.El +.Pp +The +.Fn tcsendbreak +function transmits a continuous stream of zero-valued bits for four-tenths +of a second to the terminal referenced by +.Fa fd . +The +.Fa len +parameter is ignored in this implementation. +.Sh RETURN VALUES +Upon successful completion, all of these functions return a value of zero. +.Sh ERRORS +If any error occurs, a value of -1 is returned and the global variable +.Va errno +is set to indicate the error, as follows: +.Bl -tag -width Er +.It Bq Er EBADF +The +.Fa fd +argument is not a valid file descriptor. +.It Bq Er EINVAL +The +.Fa action +argument is not a proper value. +.It Bq Er ENOTTY +The file associated with +.Fa fd +is not a terminal. +.It Bq Er EINTR +A signal interrupted the +.Fn tcdrain +function. +.El +.Sh SEE ALSO +.Xr tcsetattr 3 , +.Xr termios 4 +.Sh STANDARDS +The +.Fn tcsendbreak , +.Fn tcdrain , +.Fn tcflush +and +.Fn tcflow +functions are expected to be compliant with the +.St -p1003.1-88 +specification. diff --git a/lib/libc/gen/tcsetattr.3 b/lib/libc/gen/tcsetattr.3 new file mode 100644 index 0000000..b5c6fd9 --- /dev/null +++ b/lib/libc/gen/tcsetattr.3 @@ -0,0 +1,331 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)tcsetattr.3 8.3 (Berkeley) 1/2/94 +.\" $FreeBSD$ +.\" +.Dd January 2, 1994 +.Dt TCSETATTR 3 +.Os +.Sh NAME +.Nm cfgetispeed , +.Nm cfsetispeed , +.Nm cfgetospeed , +.Nm cfsetospeed , +.Nm cfsetspeed , +.Nm cfmakeraw , +.Nm tcgetattr , +.Nm tcsetattr +.Nd manipulating the termios structure +.Sh SYNOPSIS +.Fd #include <termios.h> +.Ft speed_t +.Fn cfgetispeed "const struct termios *t" +.Ft int +.Fn cfsetispeed "struct termios *t" "speed_t speed" +.Ft speed_t +.Fn cfgetospeed "const struct termios *t" +.Ft int +.Fn cfsetospeed "struct termios *t" "speed_t speed" +.Ft int +.Fn cfsetspeed "struct termios *t" "speed_t speed" +.Ft void +.Fn cfmakeraw "struct termios *t" +.Ft int +.Fn tcgetattr "int fd" "struct termios *t" +.Ft int +.Fn tcsetattr "int fd" "int action" "const struct termios *t" +.Sh DESCRIPTION +The +.Fn cfmakeraw , +.Fn tcgetattr +and +.Fn tcsetattr +functions are provided for getting and setting the termios structure. +.Pp +The +.Fn cfgetispeed , +.Fn cfsetispeed , +.Fn cfgetospeed , +.Fn cfsetospeed +and +.Fn cfsetspeed +functions are provided for getting and setting the baud rate values in +the termios structure. +The effects of the functions on the terminal as described below +do not become effective, nor are all errors detected, until the +.Fn tcsetattr +function is called. +Certain values for baud rates set in the termios structure and passed to +.Fn tcsetattr +have special meanings. +These are discussed in the portion of the manual page that describes the +.Fn tcsetattr +function. +.Sh GETTING AND SETTING THE BAUD RATE +The input and output baud rates are found in the termios structure. +The unsigned integer +.Li speed_t +is typdef'd in the include file +.Aq Pa termios.h . +The value of the integer corresponds directly to the baud rate being +represented, however, the following symbolic values are defined. +.Bd -literal +#define B0 0 +#define B50 50 +#define B75 75 +#define B110 110 +#define B134 134 +#define B150 150 +#define B200 200 +#define B300 300 +#define B600 600 +#define B1200 1200 +#define B1800 1800 +#define B2400 2400 +#define B4800 4800 +#define B9600 9600 +#define B19200 19200 +#define B38400 38400 +#ifndef _POSIX_SOURCE +#define EXTA 19200 +#define EXTB 38400 +#endif /*_POSIX_SOURCE */ +.Ed +.Pp +The +.Fn cfgetispeed +function returns the input baud rate in the termios structure referenced by +.Fa tp . +.Pp +The +.Fn cfsetispeed +function sets the input baud rate in the termios structure referenced by +.Fa tp +to +.Fa speed . +.Pp +The +.Fn cfgetospeed +function returns the output baud rate in the termios structure referenced by +.Fa tp . +.Pp +The +.Fn cfsetospeed +function sets the output baud rate in the termios structure referenced by +.Fa tp +to +.Fa speed . +.Pp +The +.Fn cfsetspeed +function sets both the input and output baud rate in the termios structure +referenced by +.Fa tp +to +.Fa speed . +.Pp +Upon successful completion, the functions +.Fn cfsetispeed , +.Fn cfsetospeed , +and +.Fn cfsetspeed +return a value of 0. +Otherwise, a value of -1 is returned and the global variable +.Va errno +is set to indicate the error. +.Sh GETTING AND SETTING THE TERMIOS STATE +This section describes the functions that are used to control the general +terminal interface. +Unless otherwise noted for a specific command, these functions are restricted +from use by background processes. +Attempts to perform these operations shall cause the process group to be sent +a SIGTTOU signal. +If the calling process is blocking or ignoring SIGTTOU signals, the process +is allowed to perform the operation and the SIGTTOU signal is not sent. +.Pp +In all the functions, although +.Fa fd +is an open file descriptor, the functions affect the underlying terminal +file, not just the open file description associated with the particular +file descriptor. +.Pp +The +.Fn cfmakeraw +function sets the flags stored in the termios structure to a state disabling +all input and output processing, giving a +.Dq raw I/O path. +It should be noted that there is no function to reverse this effect. +This is because there are a variety of processing options that could be +re-enabled and the correct method is for an application to snapshot the +current terminal state using the function +.Fn tcgetattr , +setting raw mode with +.Fn cfmakeraw +and the subsequent +.Fn tcsetattr , +and then using another +.Fn tcsetattr +with the saved state to revert to the previous terminal state. +.Pp +The +.Fn tcgetattr +function copies the parameters associated with the terminal referenced +by +.Fa fd +in the termios structure referenced by +.Fa tp . +This function is allowed from a background process, however, the terminal +attributes may be subsequently changed by a foreground process. +.Pp +The +.Fn tcsetattr +function sets the parameters associated with the terminal from the +termios structure referenced by +.Fa tp . +The +.Fa action +field is created by +.Em or Ns 'ing +the following values, as specified in the include file +.Aq Pa termios.h . +.Bl -tag -width "TCSADRAIN" +.It Fa TCSANOW +The change occurs immediately. +.It Fa TCSADRAIN +The change occurs after all output written to +.Fa fd +has been transmitted to the terminal. +This value of +.Fa action +should be used when changing parameters that affect output. +.It Fa TCSAFLUSH +The change occurs after all output written to +.Fa fd +has been transmitted to the terminal. +Additionally, any input that has been received but not read is discarded. +.It Fa TCSASOFT +If this value is +.Em or Ns 'ed +into the +.Fa action +value, the values of the +.Em c_cflag , +.Em c_ispeed , +and +.Em c_ospeed +fields are ignored. +.El +.Pp +The 0 baud rate is used to terminate the connection. +If 0 is specified as the output speed to the function +.Fn tcsetattr , +modem control will no longer be asserted on the terminal, disconnecting +the terminal. +.Pp +If zero is specified as the input speed to the function +.Fn tcsetattr , +the input baud rate will be set to the same value as that specified by +the output baud rate. +.Pp +If +.Fn tcsetattr +is unable to make any of the requested changes, it returns -1 and +sets errno. +Otherwise, it makes all of the requested changes it can. +If the specified input and output baud rates differ and are a combination +that is not supported, neither baud rate is changed. +.Pp +Upon successful completion, the functions +.Fn tcgetattr +and +.Fn tcsetattr +return a value of 0. +Otherwise, they +return -1 and the global variable +.Va errno +is set to indicate the error, as follows: +.Bl -tag -width Er +.It Bq Er EBADF +The +.Fa fd +argument to +.Fn tcgetattr +or +.Fn tcsetattr +was not a valid file descriptor. +.It Bq Er EINTR +The +.Fn tcsetattr +function was interrupted by a signal. +.It Bq Er EINVAL +The +.Fa action +argument to the +.Fn tcsetattr +function was not valid, or an attempt was made to change an attribute +represented in the termios structure to an unsupported value. +.It Bq Er ENOTTY +The file associated with the +.Fa fd +argument to +.Fn tcgetattr +or +.Fn tcsetattr +is not a terminal. +.El +.Sh SEE ALSO +.Xr tcsendbreak 3 , +.Xr termios 4 +.Sh STANDARDS +The +.Fn cfgetispeed , +.Fn cfsetispeed , +.Fn cfgetospeed , +.Fn cfsetospeed , +.Fn tcgetattr +and +.Fn tcsetattr +functions are expected to be compliant with the +.St -p1003.1-88 +specification. +The +.Fn cfmakeraw +and +.Fn cfsetspeed +functions, +as well as the +.Li TCSASOFT +option to the +.Fn tcsetattr +function are extensions to the +.St -p1003.1-88 +specification. diff --git a/lib/libc/gen/tcsetpgrp.3 b/lib/libc/gen/tcsetpgrp.3 new file mode 100644 index 0000000..3da2cd1 --- /dev/null +++ b/lib/libc/gen/tcsetpgrp.3 @@ -0,0 +1,101 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)tcsetpgrp.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt TCSETPGRP 3 +.Os +.Sh NAME +.Nm tcsetpgrp +.Nd set foreground process group ID +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <unistd.h> +.Ft int +.Fn tcsetpgrp "int fd" "pid_t pgrp_id" +.Sh DESCRIPTION +If the process has a controlling terminal, the +.Nm tcsetpgrp +function sets the foreground process group ID associated with the +terminal device to +.Fa pgrp_id . +The terminal device associated with +.Fa fd +must be the controlling terminal of the calling process and the +controlling terminal must be currently associated with the session +of the calling process. +The value of +.Fa pgrp_id +must be the same as the process group ID of a process in the same +session as the calling process. +.Pp +Upon successful completion, +.Nm tcsetpgrp +returns a value of zero. +.Sh ERRORS +If an error occurs, +.Nm tcsetpgrp +returns -1 and the global variable +.Va errno +is set to indicate the error, as follows: +.Bl -tag -width Er +.It Bq Er EBADF +The +.Fa fd +argument is not a valid file descriptor. +.It Bq Er EINVAL +An invalid value of +.Fa pgrp_id +was specified. +.It Bq Er ENOTTY +The calling process does not have a controlling terminal, or the file +represented by +.Fa fd +is not the controlling terminal, or the controlling terminal is no +longer associated with the session of the calling process. +.It Bq Er EPERM +The +.Fa pgrp_id +argument does not match the process group ID of a process in the same +session as the calling process. +.El +.Sh SEE ALSO +.Xr setpgid 2 , +.Xr setsid 2 , +.Xr tcgetpgrp 3 +.Sh STANDARDS +The +.Nm tcsetpgprp +function is expected to be compliant with the +.St -p1003.1-88 +specification. diff --git a/lib/libc/gen/telldir.c b/lib/libc/gen/telldir.c new file mode 100644 index 0000000..ab23d9d --- /dev/null +++ b/lib/libc/gen/telldir.c @@ -0,0 +1,157 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)telldir.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <dirent.h> +#include <stdlib.h> +#include <unistd.h> + +/* + * The option SINGLEUSE may be defined to say that a telldir + * cookie may be used only once before it is freed. This option + * is used to avoid having memory usage grow without bound. + */ +#define SINGLEUSE + +/* + * One of these structures is malloced to describe the current directory + * position each time telldir is called. It records the current magic + * cookie returned by getdirentries and the offset within the buffer + * associated with that return value. + */ +struct ddloc { + struct ddloc *loc_next;/* next structure in list */ + long loc_index; /* key associated with structure */ + long loc_seek; /* magic cookie returned by getdirentries */ + long loc_loc; /* offset of entry in buffer */ + const DIR* loc_dirp; /* directory which used this entry */ +}; + +#define NDIRHASH 32 /* Num of hash lists, must be a power of 2 */ +#define LOCHASH(i) ((i)&(NDIRHASH-1)) + +static long dd_loccnt; /* Index of entry for sequential readdir's */ +static struct ddloc *dd_hash[NDIRHASH]; /* Hash list heads for ddlocs */ + +/* + * return a pointer into a directory + */ +long +telldir(dirp) + const DIR *dirp; +{ + register int index; + register struct ddloc *lp; + + if ((lp = (struct ddloc *)malloc(sizeof(struct ddloc))) == NULL) + return (-1); + index = dd_loccnt++; + lp->loc_index = index; + lp->loc_seek = dirp->dd_seek; + lp->loc_loc = dirp->dd_loc; + lp->loc_dirp = dirp; + lp->loc_next = dd_hash[LOCHASH(index)]; + dd_hash[LOCHASH(index)] = lp; + return (index); +} + +/* + * seek to an entry in a directory. + * Only values returned by "telldir" should be passed to seekdir. + */ +void +_seekdir(dirp, loc) + register DIR *dirp; + long loc; +{ + register struct ddloc *lp; + register struct ddloc **prevlp; + struct dirent *dp; + + prevlp = &dd_hash[LOCHASH(loc)]; + lp = *prevlp; + while (lp != NULL) { + if (lp->loc_index == loc) + break; + prevlp = &lp->loc_next; + lp = lp->loc_next; + } + if (lp == NULL) + return; + if (lp->loc_loc == dirp->dd_loc && lp->loc_seek == dirp->dd_seek) + goto found; + (void) lseek(dirp->dd_fd, (off_t)lp->loc_seek, SEEK_SET); + dirp->dd_seek = lp->loc_seek; + dirp->dd_loc = 0; + while (dirp->dd_loc < lp->loc_loc) { + dp = readdir(dirp); + if (dp == NULL) + break; + } +found: +#ifdef SINGLEUSE + *prevlp = lp->loc_next; + free((caddr_t)lp); +#endif +} + +/* + * Reclaim memory for telldir cookies which weren't used. + */ +void +_reclaim_telldir(dirp) + register const DIR *dirp; +{ + register struct ddloc *lp; + register struct ddloc **prevlp; + int i; + + for (i = 0; i < NDIRHASH; i++) { + prevlp = &dd_hash[i]; + lp = *prevlp; + while (lp != NULL) { + if (lp->loc_dirp == dirp) { + *prevlp = lp->loc_next; + free((caddr_t)lp); + lp = *prevlp; + continue; + } + prevlp = &lp->loc_next; + lp = lp->loc_next; + } + } +} diff --git a/lib/libc/gen/termios.c b/lib/libc/gen/termios.c new file mode 100644 index 0000000..5264a2d --- /dev/null +++ b/lib/libc/gen/termios.c @@ -0,0 +1,245 @@ +/*- + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)termios.c 8.2 (Berkeley) 2/21/94"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/fcntl.h> +#include <sys/ioctl.h> +#include <sys/time.h> + +#include <errno.h> +#include <termios.h> +#include <unistd.h> + +int +tcgetattr(fd, t) + int fd; + struct termios *t; +{ + + return (ioctl(fd, TIOCGETA, t)); +} + +int +tcsetattr(fd, opt, t) + int fd, opt; + const struct termios *t; +{ + struct termios localterm; + + if (opt & TCSASOFT) { + localterm = *t; + localterm.c_cflag |= CIGNORE; + t = &localterm; + } + switch (opt & ~TCSASOFT) { + case TCSANOW: + return (ioctl(fd, TIOCSETA, t)); + case TCSADRAIN: + return (ioctl(fd, TIOCSETAW, t)); + case TCSAFLUSH: + return (ioctl(fd, TIOCSETAF, t)); + default: + errno = EINVAL; + return (-1); + } +} + +int +#if __STDC__ +tcsetpgrp(int fd, pid_t pgrp) +#else +tcsetpgrp(fd, pgrp) + int fd; + pid_t pgrp; +#endif +{ + int s; + + s = pgrp; + return (ioctl(fd, TIOCSPGRP, &s)); +} + +pid_t +tcgetpgrp(fd) + int fd; +{ + int s; + + if (ioctl(fd, TIOCGPGRP, &s) < 0) + return ((pid_t)-1); + + return ((pid_t)s); +} + +speed_t +cfgetospeed(t) + const struct termios *t; +{ + + return (t->c_ospeed); +} + +speed_t +cfgetispeed(t) + const struct termios *t; +{ + + return (t->c_ispeed); +} + +int +cfsetospeed(t, speed) + struct termios *t; + speed_t speed; +{ + + t->c_ospeed = speed; + return (0); +} + +int +cfsetispeed(t, speed) + struct termios *t; + speed_t speed; +{ + + t->c_ispeed = speed; + return (0); +} + +int +cfsetspeed(t, speed) + struct termios *t; + speed_t speed; +{ + + t->c_ispeed = t->c_ospeed = speed; + return (0); +} + +/* + * Make a pre-existing termios structure into "raw" mode: character-at-a-time + * mode with no characters interpreted, 8-bit data path. + */ +void +cfmakeraw(t) + struct termios *t; +{ + + t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR); + t->c_iflag |= IGNBRK; + t->c_oflag &= ~OPOST; + t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN); + t->c_cflag &= ~(CSIZE|PARENB); + t->c_cflag |= CS8|CREAD; + t->c_cc[VMIN] = 1; + t->c_cc[VTIME] = 0; +} + +int +tcsendbreak(fd, len) + int fd, len; +{ + struct timeval sleepytime; + + sleepytime.tv_sec = 0; + sleepytime.tv_usec = 400000; + if (ioctl(fd, TIOCSBRK, 0) == -1) + return (-1); + (void)select(0, 0, 0, 0, &sleepytime); + if (ioctl(fd, TIOCCBRK, 0) == -1) + return (-1); + return (0); +} + +int +tcdrain(fd) + int fd; +{ + + return (ioctl(fd, TIOCDRAIN, 0)); +} + +int +tcflush(fd, which) + int fd, which; +{ + int com; + + switch (which) { + case TCIFLUSH: + com = FREAD; + break; + case TCOFLUSH: + com = FWRITE; + break; + case TCIOFLUSH: + com = FREAD | FWRITE; + break; + default: + errno = EINVAL; + return (-1); + } + return (ioctl(fd, TIOCFLUSH, &com)); +} + +int +tcflow(fd, action) + int fd, action; +{ + struct termios term; + u_char c; + + switch (action) { + case TCOOFF: + return (ioctl(fd, TIOCSTOP, 0)); + case TCOON: + return (ioctl(fd, TIOCSTART, 0)); + case TCION: + case TCIOFF: + if (tcgetattr(fd, &term) == -1) + return (-1); + c = term.c_cc[action == TCIOFF ? VSTOP : VSTART]; + if (c != _POSIX_VDISABLE && write(fd, &c, sizeof(c)) == -1) + return (-1); + return (0); + default: + errno = EINVAL; + return (-1); + } + /* NOTREACHED */ +} diff --git a/lib/libc/gen/time.3 b/lib/libc/gen/time.3 new file mode 100644 index 0000000..71c1982 --- /dev/null +++ b/lib/libc/gen/time.3 @@ -0,0 +1,86 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)time.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt TIME 3 +.Os BSD 4 +.Sh NAME +.Nm time +.Nd get time of day +.Sh SYNOPSIS +.Fd #include <time.h> +.Ft time_t +.Fn time "time_t *tloc" +.Sh DESCRIPTION +The +.Fn time +function +returns the value of time in seconds since 0 hours, 0 minutes, +0 seconds, January 1, 1970, Coordinated Universal Time. +.Pp +A copy of the time value may be saved to the area indicated by the +pointer +.Fa tloc . +If +.Fa tloc +is a NULL pointer, no value is stored. +.Pp +Upon successful completion, +.Fn time +returns the value of time. +Otherwise a value of +.Po +.Po Fa time_t Pc \-1 +.Pc +is returned and the global variable +.Va errno +is set to indicate the error. +.Sh ERRORS +The following error codes may be set in +.Va errno : +.Bl -tag -width [EFAULT] +.It Bq Er EFAULT +An argument address referenced invalid memory. +.Sh SEE ALSO +.Xr gettimeofday 2 , +.Xr ctime 3 +.Sh HISTORY +A +.Fn time +function appeared in +.At v6 . diff --git a/lib/libc/gen/time.c b/lib/libc/gen/time.c new file mode 100644 index 0000000..044d3a2 --- /dev/null +++ b/lib/libc/gen/time.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/time.h> + +time_t +time(t) + time_t *t; +{ + struct timeval tt; + + if (gettimeofday(&tt, (struct timezone *)0) < 0) + return(-1); + if (t) + *t = tt.tv_sec; + return(tt.tv_sec); +} diff --git a/lib/libc/gen/times.3 b/lib/libc/gen/times.3 new file mode 100644 index 0000000..b971e2d --- /dev/null +++ b/lib/libc/gen/times.3 @@ -0,0 +1,138 @@ +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)times.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt TIMES 3 +.Os BSD 4 +.Sh NAME +.Nm times +.Nd process times +.Sh SYNOPSIS +.Fd #include <sys/times.h> +.Ft clock_t +.Fn times "struct tms *tp" +.Sh DESCRIPTION +.Bf -symbolic +This interface is obsoleted by getrusage(2) +and gettimeofday(3). +.Ef +.Pp +The +.Fn times +function returns the value of time in +.Dv CLK_TCK Ns 's +of a second since +0 hours, 0 minutes, 0 seconds, January 1, 1970, Coordinated Universal +Time. +.Pp +It also fills in the structure pointed to by +.Fa tp +with time-accounting information. +.Pp +The +.Fa tms +structure is defined as follows: +.Bd -literal -offset indent +typedef struct { + clock_t tms_utime; + clock_t tms_stime; + clock_t tms_cutime; + clock_t tms_cstime; +} +.Ed +.Pp +The elements of this structure are defined as follows: +.Bl -tag -width tms_cutime +.It Fa tms_utime +The +.Tn CPU +time charged for the execution of user instructions. +.It Fa tms_stime +The +.Tn CPU +time charged for execution by the system on behalf of +the process. +.It Fa tms_cutime +The sum of the +.Fa tms_utime s +and +.Fa tms_cutime s +of the child processes. +.It Fa tms_cstime +The sum of the +.Fa tms_stime Ns s +and +.Fa tms_cstime Ns s +of the child processes. +.El +.Pp +All times are in +.Dv CLK_TCK Ns 's +of a second. +.Pp +The times of a terminated child process are included in the +.Fa tms_cutime +and +.Fa tms_cstime +elements of the parent when one of the +.Xr wait 2 +functions returns the process ID of the terminated child to the parent. +If an error occurs, +.Fn times +returns the value +.Pq (clock_t)\-1 , +and sets errno to indicate the error. +.Sh ERRORS +The +.Fn times +function +may fail and set the global variable +.Va errno +for any of the errors specified for the library +routines +.Xr getrusage 2 +and +.Xr gettimeofday 2 . +.Sh SEE ALSO +.Xr time 1 , +.Xr getrusage 2 , +.Xr gettimeofday 2 , +.Xr wait 2 , +.Xr clocks 7 +.Sh STANDARDS +The +.Fn times +function +conforms to +.St -p1003.1-88 . diff --git a/lib/libc/gen/times.c b/lib/libc/gen/times.c new file mode 100644 index 0000000..c145d0a --- /dev/null +++ b/lib/libc/gen/times.c @@ -0,0 +1,67 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)times.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/time.h> +#include <sys/times.h> +#include <sys/resource.h> + +/* + * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000, + * but this would overflow if we switch to nanosec. + */ +#define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) + +clock_t +times(tp) + register struct tms *tp; +{ + struct rusage ru; + struct timeval t; + + if (getrusage(RUSAGE_SELF, &ru) < 0) + return ((clock_t)-1); + tp->tms_utime = CONVTCK(ru.ru_utime); + tp->tms_stime = CONVTCK(ru.ru_stime); + if (getrusage(RUSAGE_CHILDREN, &ru) < 0) + return ((clock_t)-1); + tp->tms_cutime = CONVTCK(ru.ru_utime); + tp->tms_cstime = CONVTCK(ru.ru_stime); + if (gettimeofday(&t, (struct timezone *)0)) + return ((clock_t)-1); + return ((clock_t)(CONVTCK(t))); +} diff --git a/lib/libc/gen/timezone.3 b/lib/libc/gen/timezone.3 new file mode 100644 index 0000000..7468aa4 --- /dev/null +++ b/lib/libc/gen/timezone.3 @@ -0,0 +1,68 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)timezone.3 8.2 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd April 19, 1994 +.Dt TIMEZONE 3 +.Os +.Sh NAME +.Nm timezone +.Nd return the timezone abbreviation +.Sh SYNOPSIS +.Ft char * +.Fn timezone "int zone" "int dst" +.Sh DESCRIPTION +.ft B +This interface is for compatibility only; it is impossible to reliably +map timezone's arguments to a time zone abbreviation. +See ctime(3). +.ft P +.Pp +The +.Fn timezone +function returns a pointer to a time zone abbreviation for the specified +.Ar zone +and +.Ar dst +values. +.Ar Zone +is the number of minutes west of GMT and +.Ar dst +is non-zero if daylight savings time is in effect. +.Sh SEE ALSO +.Xr ctime 3 +.Sh HISTORY +A +.Fn timezone +function appeared in +.At v7 . diff --git a/lib/libc/gen/timezone.c b/lib/libc/gen/timezone.c new file mode 100644 index 0000000..74086ef --- /dev/null +++ b/lib/libc/gen/timezone.c @@ -0,0 +1,135 @@ +/* + * Copyright (c) 1987, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)timezone.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/time.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#define TZ_MAX_CHARS 255 + +char *_tztab(); + +/* + * timezone -- + * The arguments are the number of minutes of time you are westward + * from Greenwich and whether DST is in effect. It returns a string + * giving the name of the local timezone. Should be replaced, in the + * application code, by a call to localtime. + */ + +static char czone[TZ_MAX_CHARS]; /* space for zone name */ + +char * +timezone(zone, dst) + int zone, + dst; +{ + register char *beg, + *end; + + if ( (beg = getenv("TZNAME")) ) { /* set in environment */ + if ( (end = index(beg, ',')) ) {/* "PST,PDT" */ + if (dst) + return(++end); + *end = '\0'; + (void)strncpy(czone,beg,sizeof(czone) - 1); + czone[sizeof(czone) - 1] = '\0'; + *end = ','; + return(czone); + } + return(beg); + } + return(_tztab(zone,dst)); /* default: table or created zone */ +} + +static struct zone { + int offset; + char *stdzone; + char *dlzone; +} zonetab[] = { + {-1*60, "MET", "MET DST"}, /* Middle European */ + {-2*60, "EET", "EET DST"}, /* Eastern European */ + {4*60, "AST", "ADT"}, /* Atlantic */ + {5*60, "EST", "EDT"}, /* Eastern */ + {6*60, "CST", "CDT"}, /* Central */ + {7*60, "MST", "MDT"}, /* Mountain */ + {8*60, "PST", "PDT"}, /* Pacific */ +#ifdef notdef + /* there's no way to distinguish this from WET */ + {0, "GMT", 0}, /* Greenwich */ +#endif + {0*60, "WET", "WET DST"}, /* Western European */ + {-10*60,"EST", "EST"}, /* Aust: Eastern */ + {-10*60+30,"CST", "CST"}, /* Aust: Central */ + {-8*60, "WST", 0}, /* Aust: Western */ + {-1} +}; + +/* + * _tztab -- + * check static tables or create a new zone name; broken out so that + * we can make a guess as to what the zone is if the standard tables + * aren't in place in /etc. DO NOT USE THIS ROUTINE OUTSIDE OF THE + * STANDARD LIBRARY. + */ +char * +_tztab(zone,dst) + register int zone; + int dst; +{ + register struct zone *zp; + register char sign; + + for (zp = zonetab; zp->offset != -1;++zp) /* static tables */ + if (zp->offset == zone) { + if (dst && zp->dlzone) + return(zp->dlzone); + if (!dst && zp->stdzone) + return(zp->stdzone); + } + + if (zone < 0) { /* create one */ + zone = -zone; + sign = '+'; + } + else + sign = '-'; + (void)snprintf(czone, sizeof(czone), + "GMT%c%d:%02d",sign,zone / 60,zone % 60); + return(czone); +} diff --git a/lib/libc/gen/ttyname.3 b/lib/libc/gen/ttyname.3 new file mode 100644 index 0000000..719b9fb --- /dev/null +++ b/lib/libc/gen/ttyname.3 @@ -0,0 +1,127 @@ +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)ttyname.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt TTYNAME 3 +.Os +.Sh NAME +.Nm ttyname , +.Nm isatty , +.Nm ttyslot +.Nd get name of associated terminal (tty) from file descriptor +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft char * +.Fn ttyname "int fd" +.Ft int +.Fn isatty "int fd" +.Ft int +.Fn ttyslot void +.Sh DESCRIPTION +These functions operate on the system file descriptors for terminal +type devices. These descriptors are not related to the standard +.Tn I/O +.Dv FILE +typedef, but refer to the special device files found in +.Pa /dev +and named +.Pa /dev/tty Ns Em xx +and for which an entry exists +in the initialization file +.Pa /etc/ttys. +(See +.Xr ttys 5 . ) +.Pp +The +.Fn isatty +function +determines if the file descriptor +.Fa fd +refers to a valid +terminal type device. +.Pp +The +.Fn ttyname +function +gets the related device name of +a file descriptor for which +.Fn isatty +is true +.Pp +The +.Fn ttyslot +function +fetches the current process' control terminal number from the +.Xr ttys 5 +file entry. +.Sh RETURN VALUES +The +.Fn ttyname +function +returns the null terminated name if the device is found and +.Fn isatty +is true; otherwise +a +.Dv NULL +pointer is returned. +.Pp +The +.Fn ttyslot +function +returns the unit number of the device file if found; otherwise +the value zero is returned. +.Sh FILES +.Bl -tag -width /etc/ttys -compact +.It Pa /dev/\(** +.It Pa /etc/ttys +.El +.Sh SEE ALSO +.Xr ioctl 2 , +.Xr ttys 5 +.Sh HISTORY +A +.Fn isatty , +.Fn ttyname , +and +.Fn ttyslot +function +appeared in +.At v7 . +.Sh BUGS +The +.Fn ttyname +function leaves its result in an internal static object and returns +a pointer to that object. Subsequent calls to +.Fn ttyname +will modify the same object. diff --git a/lib/libc/gen/ttyname.c b/lib/libc/gen/ttyname.c new file mode 100644 index 0000000..cb01870 --- /dev/null +++ b/lib/libc/gen/ttyname.c @@ -0,0 +1,222 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)ttyname.c 8.2 (Berkeley) 1/27/94"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <dirent.h> +#include <stdlib.h> +#include <termios.h> +#include <unistd.h> +#include <db.h> +#include <string.h> +#include <paths.h> + +#ifdef _THREAD_SAFE +#include <pthread.h> +#include "pthread_private.h" +static struct pthread_mutex _ttyname_lockd = PTHREAD_MUTEX_STATIC_INITIALIZER; +static pthread_mutex_t ttyname_lock = &_ttyname_lockd; +static pthread_key_t ttyname_key; +static int ttyname_init = 0; + +char * +ttyname(int fd) +{ + char *ret; + + if (_FD_LOCK(fd, FD_READ, NULL) == 0) { + ret = __ttyname_basic(fd); + _FD_UNLOCK(fd, FD_READ); + } else { + ret = NULL; + } + + return (ret); +} + +char * +__ttyname_r_basic(int fd, char *buf, size_t len) +{ + register struct dirent *dirp; + register DIR *dp; + struct stat dsb; + struct stat sb; + char *rval; + int minlen; + + rval = NULL; + + /* Must be a terminal. */ + if (!isatty(fd)) + return (rval); + /* Must be a character device. */ + if (_thread_sys_fstat(fd, &sb) || !S_ISCHR(sb.st_mode)) + return (rval); + /* Must have enough room */ + if (len <= sizeof(_PATH_DEV)) + return (rval); + + if ((dp = opendir(_PATH_DEV)) != NULL) { + memcpy(buf, _PATH_DEV, sizeof(_PATH_DEV)); + for (rval = NULL; (dirp = readdir(dp)) != NULL;) { + if (dirp->d_fileno != sb.st_ino) + continue; + minlen = (len - (sizeof(_PATH_DEV) - 1)) < (dirp->d_namlen + 1) ? + (len - (sizeof(_PATH_DEV) - 1)) : (dirp->d_namlen + 1); + memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name, minlen); + if (stat(buf, &dsb) || sb.st_dev != dsb.st_dev || + sb.st_ino != dsb.st_ino) + continue; + rval = buf; + break; + } + (void) closedir(dp); + } + return (rval); +} + +char * +__ttyname_basic(int fd) +{ + char *buf; + + pthread_mutex_lock(&ttyname_lock); + if (ttyname_init == 0) { + if (pthread_key_create(&ttyname_key, free)) { + pthread_mutex_unlock(&ttyname_lock); + return (NULL); + } + ttyname_init = 1; + } + pthread_mutex_unlock(&ttyname_lock); + + /* Must have thread specific data field to put data */ + if ((buf = pthread_getspecific(ttyname_key)) == NULL) { + if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) { + if (pthread_setspecific(ttyname_key, buf) != 0) { + free(buf); + return (NULL); + } + } else { + return (NULL); + } + } + return (__ttyname_r_basic(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN)); +} + +char * +ttyname_r(int fd, char *buf, size_t len) +{ + char *ret; + + if (_FD_LOCK(fd, FD_READ, NULL) == 0) { + ret = __ttyname_r_basic(fd, buf, len); + _FD_UNLOCK(fd, FD_READ); + } else { + ret = NULL; + } + return (ret); +} +#else +static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV; +static char *oldttyname __P((int, struct stat *)); + +char * +ttyname(fd) + int fd; +{ + struct stat sb; + struct termios ttyb; + DB *db; + DBT data, key; + struct { + mode_t type; + dev_t dev; + } bkey; + + /* Must be a terminal. */ + if (tcgetattr(fd, &ttyb) < 0) + return (NULL); + /* Must be a character device. */ + if (fstat(fd, &sb) || !S_ISCHR(sb.st_mode)) + return (NULL); + + if ( (db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL)) ) { + memset(&bkey, 0, sizeof(bkey)); + bkey.type = S_IFCHR; + bkey.dev = sb.st_rdev; + key.data = &bkey; + key.size = sizeof(bkey); + if (!(db->get)(db, &key, &data, 0)) { + bcopy(data.data, + buf + sizeof(_PATH_DEV) - 1, data.size); + (void)(db->close)(db); + return (buf); + } + (void)(db->close)(db); + } + return (oldttyname(fd, &sb)); +} + +static char * +oldttyname(fd, sb) + int fd; + struct stat *sb; +{ + register struct dirent *dirp; + register DIR *dp; + struct stat dsb; + + if ((dp = opendir(_PATH_DEV)) == NULL) + return (NULL); + + while ( (dirp = readdir(dp)) ) { + if (dirp->d_fileno != sb->st_ino) + continue; + bcopy(dirp->d_name, buf + sizeof(_PATH_DEV) - 1, + dirp->d_namlen + 1); + if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev || + sb->st_ino != dsb.st_ino) + continue; + (void)closedir(dp); + return (buf); + } + (void)closedir(dp); + return (NULL); +} +#endif diff --git a/lib/libc/gen/ttyslot.c b/lib/libc/gen/ttyslot.c new file mode 100644 index 0000000..2f72ebb --- /dev/null +++ b/lib/libc/gen/ttyslot.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)ttyslot.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <ttyent.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int +ttyslot() +{ + register struct ttyent *ttyp; + register int slot; + register char *p; + int cnt; + char *name; + + setttyent(); + for (cnt = 0; cnt < 3; ++cnt) + if ( (name = ttyname(cnt)) ) { + if ( (p = rindex(name, '/')) ) + ++p; + else + p = name; + for (slot = 1; (ttyp = getttyent()); ++slot) + if (!strcmp(ttyp->ty_name, p)) { + endttyent(); + return(slot); + } + break; + } + endttyent(); + return(0); +} diff --git a/lib/libc/gen/tzset.3 b/lib/libc/gen/tzset.3 new file mode 100644 index 0000000..853a1da --- /dev/null +++ b/lib/libc/gen/tzset.3 @@ -0,0 +1,326 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Arthur Olson. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)tzset.3 8.2 (Berkeley) 11/17/93 +.\" $FreeBSD$ +.\" +.Dd November 17, 1993 +.Dt TZSET 3 +.Os +.Sh NAME +.Nm tzset , +.Nm tzsetwall +.Nd initialize time conversion information +.Sh SYNOPSIS +.Fd #include <time.h> +.Ft void +.Fn tzset void +.Ft void +.Fn tzsetwall void +.Sh DESCRIPTION +The +.Fn tzset +function +initializes time conversion information used by the library routine +.Xr localtime 3 . +The environment variable +.Ev TZ +specifies how this is done. +.Pp +If +.Ev TZ +does not appear in the environment, the best available approximation to +local wall clock time, as specified by the +.Xr tzfile 5 Ns -format +file +.Pa /etc/localtime +is used. +.Pp +If +.Ev TZ +appears in the environment but its value is a null string, Coordinated +Universal Time +.Pq Tn UTC +is used (without leap second correction). +.Pp +If +.Ev TZ +appears in the environment and its value begins with a colon +.Pq Ql \: , +the rest of its value is used as a pathname of a +.Xr tzfile 5 Ns -format +file from which to read the time conversion information. +If the first character of the pathname is a slash +.Pq Ql / +it is used as +an absolute pathname; otherwise, it is used as a pathname relative to +the system time conversion information directory. +.Pp +If its value does not begin with a colon, it is first used as the pathname +of a file (as described above) from which to read the time conversion +information. +If that file cannot be read, the value is then interpreted as a direct +specification (the format is described below) of the time conversion +information. +.Pp +If the +.Ev TZ +environment variable does not specify a +.Xr tzfile 5 Ns -format +file and cannot be interpreted as a direct specification, +.Tn UTC +is used. +.Pp +The +.Fn tzsetwall +function +sets things up so that +.Xr localtime +returns the best available approximation of local wall clock time. +.Sh SPECIFICATION FORMAT +When +.Ev TZ +is used directly as a specification of the time conversion information, +it must have the following syntax (spaces inserted for clarity): +.Bd -filled -offset indent +.Em std offset Bo +.Em dst Bo +.Em offset +.Bc +.Bo +.No , Em rule +.Bc +.Bc +.Ed +.Pp +Where: +.Bl -tag -width std_and_dst -offset indent +.It Em std No and Em dst +Three or more bytes that are the designation for the standard +.Pq Em std +or summer +.Pq Em dst +time zone. Only +.Em std +is required; if +.Em dst +is missing, then summer time does not apply in this locale. +Upper and lowercase letters are explicitly allowed. Any characters +except a leading colon +.Pq Ql \: , +digits, comma +.Pq Ql \&, , +minus +.Pq Ql \- , +plus +.Pq Ql + , +and +.Tn ASCII +.Dv NUL +are allowed. +.It Em offset +Indicates the value one must add to the local time to arrive at +Coordinated Universal Time. The +.Em offset +has the form: +.Bd -unfilled -offset indent +.Em hh Bo +.Pf \&: Em mm +.Bo +.Pf \&: Em ss +.Bc +.Bc +.Ed +.Pp +The minutes +.Pq Em mm +and seconds +.Pq Em ss +are optional. The hour +.Pq Em hh +is required and may be a single digit. The +.Em offset +following +.Em std +is required. If no +.Em offset +follows +.Em dst , +summer time is assumed to be one hour ahead of standard time. One or +more digits may be used; the value is always interpreted as a decimal +number. The hour must be between zero and 24, and the minutes (and +seconds) \(em if present \(em between zero and 59. If preceded by a +.Pq Ql \- +the time zone shall be east of the Prime Meridian; otherwise it shall be +west (which may be indicated by an optional preceding +.Pq Ql + ) . +.It Em rule +Indicates when to change to and back from summer time. The +.Em rule +has the form: +.Bd -filled -offset indent +.Em date/time,date/time +.Ed +.Pp +where the first +.Em date +describes when the change from standard to summer time occurs and the +second +.Em date +describes when the change back happens. Each +.Em time +field describes when, in current local time, the change to the other +time is made. +.Pp +The format of +.Em date +is one of the following: +.Bl -tag -width "M.m.n.d" +.It Sy J Em n +The Julian day +.Em n +(1 \*(Le +.Em n +\*(Le 365). +Leap days are not counted; that is, in all years \(em including leap +years \(em February 28 is day 59 and March 1 is day 60. It is +impossible to explicitly refer to the occasional February 29. +.It Em n +The zero-based Julian day +(0 \*(Le +.Em n +\*(Le 365 ) . +Leap days are counted, and it is possible to refer to February 29. +.It Sy M Em m.n.d +The +.Em d Ns 'th +day (0 \*(Le +.Em d +\*(Le 6 ) +of week +.Em n +of month +.Em m +of the year +(1 \*(Le +.Em n +\*(Le 5), +(1 \*(Le +.Em m +\*(Le 12), +where week 5 means +.Do +the last +.Em d +day in month +.Em m +.Dc +which may occur in either the fourth or the fifth week). Week 1 is the +first week in which the +.Em d Ns 'th +day occurs. Day zero is Sunday. +.Pp +The +.Em time +has the same format as +.Em offset +except that no leading sign +.Pq Ql \- +or +.Pq Ql + +is allowed. The default, if +.Em time +is not given, is +.Sy 02:00:00 . +.El +.Pp +If no +.Em rule +is present in the +.Ev TZ +specification, the rules specified +by the +.Xr tzfile 5 Ns -format +file +.Em posixrules +in the system time conversion information directory are used, with the +standard and summer time offsets from +.Tn UTC +replaced by those specified by +the +.Em offset +values in +.Ev TZ . +.El +.Pp +For compatibility with System V Release 3.1, a semicolon +.Pq Ql \; +may be used to separate the +.Em rule +from the rest of the specification. +.Sh FILES +.Bl -tag -width /usr/share/zoneinfo/posixrules -compact +.It Pa /etc/localtime +local time zone file +.It Pa /usr/share/zoneinfo +time zone directory +.It Pa /usr/share/zoneinfo/posixrules +rules for +.Tn POSIX Ns -style +.Tn TZ Ns 's +.It Pa /usr/share/zoneinfo/GMT for +.Tn UTC +leap seconds +.El +.Pp +If the file +.Pa /usr/share/zoneinfo/GMT +does not exist, +.Tn UTC +leap seconds are loaded from +.Pa /usr/share/zoneinfo/posixrules . +.Sh SEE ALSO +.Xr date 1 , +.Xr gettimeofday 2 , +.Xr ctime 3 , +.Xr getenv 3 , +.Xr time 3 , +.Xr tzfile 5 +.Sh HISTORY +The +.Fn tzset +and +.Fn tzsetwall +functions first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/ualarm.3 b/lib/libc/gen/ualarm.3 new file mode 100644 index 0000000..bbf9457 --- /dev/null +++ b/lib/libc/gen/ualarm.3 @@ -0,0 +1,99 @@ +.\" Copyright (c) 1986, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" From: @(#)ualarm.3 8.2 (Berkeley) 4/19/94 +.\" $FreeBSD$ +.\" +.Dd April 19, 1994 +.Dt UALARM 3 +.Os BSD 4.3 +.Sh NAME +.Nm ualarm +.Nd schedule signal after specified time +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft u_int +.Fn ualarm "u_int microseconds" "u_int interval" +.Sh DESCRIPTION +.Bf -symbolic +This is a simplified interface to setitimer(2). +.Ef +.Pp +The +.Fn ualarm +function +waits a count of +.Ar microseconds +before asserting the terminating signal +.Dv SIGALRM . +System activity or time used in processing the call may cause a slight +delay. +.Pp +If the +.Fa interval +argument is non-zero, the +.Dv SIGALRM +signal will be sent +to the process every +.Fa interval +microseconds after the timer expires (e.g. after +.Fa value +microseconds have passed). +.Pp +Due to +.Xr setitimer 2 +restriction the maximum number of +.Ar microseconds +and +.Ar interval +is limited to 100000000000000 +(in case this value fit in the unsigned integer). +.Sh RETURN VALUES +When the signal has successfully been caught, +.Fn ualarm +returns the amount of time left on the clock. +.Sh NOTES +.Pp +A microsecond is 0.000001 seconds. +.Sh SEE ALSO +.Xr getitimer 2 , +.Xr setitimer 2 , +.Xr sigpause 2 , +.Xr sigvec 2 , +.Xr alarm 3 , +.Xr signal 3 , +.Xr sleep 3 , +.Xr usleep 3 +.Sh HISTORY +The +.Fn ualarm +function appeared in +.Bx 4.3 . diff --git a/lib/libc/gen/ualarm.c b/lib/libc/gen/ualarm.c new file mode 100644 index 0000000..ec49a2d --- /dev/null +++ b/lib/libc/gen/ualarm.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 1985, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)ualarm.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/time.h> +#include <unistd.h> + +#define USPS 1000000 /* # of microseconds in a second */ + +/* + * Generate a SIGALRM signal in ``usecs'' microseconds. + * If ``reload'' is non-zero, keep generating SIGALRM + * every ``reload'' microseconds after the first signal. + */ +unsigned +ualarm(usecs, reload) + register unsigned usecs; + register unsigned reload; +{ + struct itimerval new, old; + + new.it_interval.tv_usec = reload % USPS; + new.it_interval.tv_sec = reload / USPS; + + new.it_value.tv_usec = usecs % USPS; + new.it_value.tv_sec = usecs / USPS; + + if (setitimer(ITIMER_REAL, &new, &old) == 0) + return (old.it_value.tv_sec * USPS + old.it_value.tv_usec); + /* else */ + return (-1); +} diff --git a/lib/libc/gen/uname.3 b/lib/libc/gen/uname.3 new file mode 100644 index 0000000..6a3aef6 --- /dev/null +++ b/lib/libc/gen/uname.3 @@ -0,0 +1,94 @@ +.\" Copyright (c) 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)uname.3 8.1 (Berkeley) 1/4/94 +.\" $FreeBSD$ +.\" +.Dd January 4, 1994 +.Dt UNAME 3 +.Os +.Sh NAME +.Nm uname +.Nd get system identification +.Sh SYNOPSIS +.Fd #include <sys/utsname.h> +.Ft int +.Fn uname "struct utsname *name" +.Sh DESCRIPTION +The +.Fn uname +function stores nul-terminated strings of information identifying +the current system into the structure referenced by +.Fa name . +.Pp +The +.Li utsname +structure is defined in the +.Li <sys/utsname.h> +header file, and contains the following members: +.Bl -tag -width nodenameXXXX -offset indent +.It sysname +Name of the operating system implementation. +.It nodename +Network name of this machine. +.It release +Release level of the operating system. +.It version +Version level of the operating system. +.It machine +Machine hardware platform. +.El +.Sh RETURN VALUES +If +.Fn uname +is successful, 0 is returned, otherwise, -1 is returned and +.Va errno +is set appropriately. +.Sh ERRORS +The +.Fn uname +function may fail and set +.Va errno +for any of the errors specified for the library functions +.Xr sysctl 3 . +.Sh SEE ALSO +.Xr uname 1 , +.Xr sysctl 3 +.Sh STANDARDS +The +.Fn uname +function conforms to +.St -p1003.1-88 . +.Sh HISTORY +The +.Fn uname +function first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/uname.c b/lib/libc/gen/uname.c new file mode 100644 index 0000000..944c2dc --- /dev/null +++ b/lib/libc/gen/uname.c @@ -0,0 +1,125 @@ +/*- + * Copyright (c) 1994 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +/*static char sccsid[] = "From: @(#)uname.c 8.1 (Berkeley) 1/4/94";*/ +static const char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/param.h> +#include <sys/sysctl.h> +#include <sys/utsname.h> +#include <errno.h> + +int +uname(name) + struct utsname *name; +{ + int mib[2], rval; + size_t len; + char *p; + int oerrno; + + rval = 0; + + mib[0] = CTL_KERN; + mib[1] = KERN_OSTYPE; + len = sizeof(name->sysname); + oerrno = errno; + if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1) { + if(errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->sysname[sizeof(name->sysname) - 1] = '\0'; + + mib[0] = CTL_KERN; + mib[1] = KERN_HOSTNAME; + len = sizeof(name->nodename); + oerrno = errno; + if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1) { + if(errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->nodename[sizeof(name->nodename) - 1] = '\0'; + + mib[0] = CTL_KERN; + mib[1] = KERN_OSRELEASE; + len = sizeof(name->release); + oerrno = errno; + if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1) { + if(errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->release[sizeof(name->release) - 1] = '\0'; + + /* The version may have newlines in it, turn them into spaces. */ + mib[0] = CTL_KERN; + mib[1] = KERN_VERSION; + len = sizeof(name->version); + oerrno = errno; + if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1) { + if (errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->version[sizeof(name->version) - 1] = '\0'; + for (p = name->version; len--; ++p) { + if (*p == '\n' || *p == '\t') { + if (len > 1) + *p = ' '; + else + *p = '\0'; + } + } + + mib[0] = CTL_HW; + mib[1] = HW_MACHINE; + len = sizeof(name->machine); + oerrno = errno; + if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1) { + if (errno == ENOMEM) + errno = oerrno; + else + rval = -1; + } + name->machine[sizeof(name->machine) - 1] = '\0'; + return (rval); +} diff --git a/lib/libc/gen/unvis.3 b/lib/libc/gen/unvis.3 new file mode 100644 index 0000000..16734d1 --- /dev/null +++ b/lib/libc/gen/unvis.3 @@ -0,0 +1,164 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)unvis.3 8.2 (Berkeley) 12/11/93 +.\" $FreeBSD$ +.\" +.Dd December 11, 1993 +.Dt UNVIS 3 +.Os +.Sh NAME +.Nm unvis , +.Nm strunvis +.Nd decode a visual representation of characters +.Sh SYNOPSIS +.Fd #include <vis.h> +.Ft int +.Fn unvis "char *cp" "int c" "int *astate" "int flag" +.Ft int +.Fn strunvis "char *dst" "const char *src" +.Sh DESCRIPTION +The +.Fn unvis +and +.Fn strunvis +functions +are used to decode a visual representation of characters, as produced +by the +.Xr vis 3 +function, back into +the original form. Unvis is called with successive characters in +.Ar c +until a valid +sequence is recognized, at which time the decoded character is +available at the character pointed to by +.Ar cp . +Strunvis decodes the +characters pointed to by +.Ar src +into the buffer pointed to by +.Ar dst . +.Pp +The +.Fn strunvis +function +simply copies +.Ar src +to +.Ar dst , +decoding any escape sequences along the way, +and returns the number of characters placed into +.Ar dst , +or \-1 if an +invalid escape sequence was detected. The size of +.Ar dst +should be +equal to the size of +.Ar src +(that is, no expansion takes place during +decoding). +.Pp +The +.Fn unvis +function +implements a state machine that can be used to decode an arbitrary +stream of bytes. All state associated with the bytes being decoded +is stored outside the +.Fn unvis +function (that is, a pointer to the state is passed in), so +calls decoding different streams can be freely intermixed. To +start decoding a stream of bytes, first initialize an integer +to zero. Call +.Fn unvis +with each successive byte, along with a pointer +to this integer, and a pointer to a destination character. +The +.Fn unvis +function +has several return codes that must be handled properly. They are: +.Bl -tag -width UNVIS_VALIDPUSH +.It Li \&0 (zero) +Another character is necessary; nothing has been recognized yet. +.It Dv UNVIS_VALID +A valid character has been recognized and is available at the location +pointed to by cp. +.It Dv UNVIS_VALIDPUSH +A valid character has been recognized and is available at the location +pointed to by cp; however, the character currently passed in should +be passed in again. +.It Dv UNVIS_NOCHAR +A valid sequence was detected, but no character was produced. This +return code is necessary to indicate a logical break between characters. +.It Dv UNVIS_SYNBAD +An invalid escape sequence was detected, or the decoder is in an +unknown state. The decoder is placed into the starting state. +.El +.Pp +When all bytes in the stream have been processed, call +.Fn unvis +one more time with flag set to +.Dv UNVIS_END +to extract any remaining character (the character passed in is ignored). +.Pp +The following code fragment illustrates a proper use of +.Fn unvis . +.Bd -literal -offset indent +int state = 0; +char out; + +while ((ch = getchar()) != EOF) { +again: + switch(unvis(&out, ch, &state, 0)) { + case 0: + case UNVIS_NOCHAR: + break; + case UNVIS_VALID: + (void) putchar(out); + break; + case UNVIS_VALIDPUSH: + (void) putchar(out); + goto again; + case UNVIS_SYNBAD: + (void)fprintf(stderr, "bad sequence!\n"); + exit(1); + } +} +if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID) + (void) putchar(out); +.Ed +.Sh SEE ALSO +.Xr vis 1 +.Sh HISTORY +The +.Nm unvis +function +first appeared in +.Bx 4.4 . diff --git a/lib/libc/gen/unvis.c b/lib/libc/gen/unvis.c new file mode 100644 index 0000000..32405df --- /dev/null +++ b/lib/libc/gen/unvis.c @@ -0,0 +1,247 @@ +/*- + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <vis.h> + +/* + * decode driven by state machine + */ +#define S_GROUND 0 /* haven't seen escape char */ +#define S_START 1 /* start decoding special sequence */ +#define S_META 2 /* metachar started (M) */ +#define S_META1 3 /* metachar more, regular char (-) */ +#define S_CTRL 4 /* control char started (^) */ +#define S_OCTAL2 5 /* octal digit 2 */ +#define S_OCTAL3 6 /* octal digit 3 */ + +#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') + +/* + * unvis - decode characters previously encoded by vis + */ +int +unvis(cp, c, astate, flag) + char *cp; + int c, *astate, flag; +{ + + if (flag & UNVIS_END) { + if (*astate == S_OCTAL2 || *astate == S_OCTAL3) { + *astate = S_GROUND; + return (UNVIS_VALID); + } + return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD); + } + + switch (*astate) { + + case S_GROUND: + *cp = 0; + if (c == '\\') { + *astate = S_START; + return (0); + } + *cp = c; + return (UNVIS_VALID); + + case S_START: + switch(c) { + case '\\': + *cp = c; + *astate = S_GROUND; + return (UNVIS_VALID); + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + *cp = (c - '0'); + *astate = S_OCTAL2; + return (0); + case 'M': + *cp = 0200; + *astate = S_META; + return (0); + case '^': + *astate = S_CTRL; + return (0); + case 'n': + *cp = '\n'; + *astate = S_GROUND; + return (UNVIS_VALID); + case 'r': + *cp = '\r'; + *astate = S_GROUND; + return (UNVIS_VALID); + case 'b': + *cp = '\b'; + *astate = S_GROUND; + return (UNVIS_VALID); + case 'a': + *cp = '\007'; + *astate = S_GROUND; + return (UNVIS_VALID); + case 'v': + *cp = '\v'; + *astate = S_GROUND; + return (UNVIS_VALID); + case 't': + *cp = '\t'; + *astate = S_GROUND; + return (UNVIS_VALID); + case 'f': + *cp = '\f'; + *astate = S_GROUND; + return (UNVIS_VALID); + case 's': + *cp = ' '; + *astate = S_GROUND; + return (UNVIS_VALID); + case 'E': + *cp = '\033'; + *astate = S_GROUND; + return (UNVIS_VALID); + case '\n': + /* + * hidden newline + */ + *astate = S_GROUND; + return (UNVIS_NOCHAR); + case '$': + /* + * hidden marker + */ + *astate = S_GROUND; + return (UNVIS_NOCHAR); + } + *astate = S_GROUND; + return (UNVIS_SYNBAD); + + case S_META: + if (c == '-') + *astate = S_META1; + else if (c == '^') + *astate = S_CTRL; + else { + *astate = S_GROUND; + return (UNVIS_SYNBAD); + } + return (0); + + case S_META1: + *astate = S_GROUND; + *cp |= c; + return (UNVIS_VALID); + + case S_CTRL: + if (c == '?') + *cp |= 0177; + else + *cp |= c & 037; + *astate = S_GROUND; + return (UNVIS_VALID); + + case S_OCTAL2: /* second possible octal digit */ + if (isoctal(c)) { + /* + * yes - and maybe a third + */ + *cp = (*cp << 3) + (c - '0'); + *astate = S_OCTAL3; + return (0); + } + /* + * no - done with current sequence, push back passed char + */ + *astate = S_GROUND; + return (UNVIS_VALIDPUSH); + + case S_OCTAL3: /* third possible octal digit */ + *astate = S_GROUND; + if (isoctal(c)) { + *cp = (*cp << 3) + (c - '0'); + return (UNVIS_VALID); + } + /* + * we were done, push back passed char + */ + return (UNVIS_VALIDPUSH); + + default: + /* + * decoder in unknown state - (probably uninitialized) + */ + *astate = S_GROUND; + return (UNVIS_SYNBAD); + } +} + +/* + * strunvis - decode src into dst + * + * Number of chars decoded into dst is returned, -1 on error. + * Dst is null terminated. + */ + +int +strunvis(dst, src) + register char *dst; + register const char *src; +{ + register char c; + char *start = dst; + int state = 0; + + while ( (c = *src++) ) { + again: + switch (unvis(dst, c, &state, 0)) { + case UNVIS_VALID: + dst++; + break; + case UNVIS_VALIDPUSH: + dst++; + goto again; + case 0: + case UNVIS_NOCHAR: + break; + default: + return (-1); + } + } + if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID) + dst++; + *dst = '\0'; + return (dst - start); +} diff --git a/lib/libc/gen/usleep.3 b/lib/libc/gen/usleep.3 new file mode 100644 index 0000000..77d6259 --- /dev/null +++ b/lib/libc/gen/usleep.3 @@ -0,0 +1,81 @@ +.\" Copyright (c) 1986, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)usleep.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd Feb 13, 1998 +.Dt USLEEP 3 +.Os BSD 4.3 +.Sh NAME +.Nm usleep +.Nd suspend process execution for an interval measured in microseconds +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft int +.Fn usleep "unsigned int microseconds" +.Sh DESCRIPTION +The +.Fn usleep +function suspends execution of the calling process until either +.Fa microseconds +microseconds have elapsed or a signal is delivered to the process and its +action is to invoke a signal-catching function or to terminate the +process. +System activity may lengthen the sleep by an indeterminate amount. +.Pp +This function is implemented using +.Xr nanosleep 2 +by pausing for +.Fa microseconds +microseconds or until a signal occurs. +Consequently, in this implementation, +sleeping has no effect on the state of process timers, +and there is no special handling for SIGALRM. +.Sh RETURN VALUES +.Rv -std usleep +.Sh ERRORS +The +.Fn usleep +function +will fail if: +.Bl -tag -width [EINTR] +.It Bq Er EINTR +A signal was delivered to the process and its +action was to invoke a signal-catching function. +.Sh SEE ALSO +.Xr nanosleep 2 , +.Xr sleep 3 +.Sh HISTORY +The +.Fn usleep +function appeared in +.Bx 4.3 . diff --git a/lib/libc/gen/usleep.c b/lib/libc/gen/usleep.c new file mode 100644 index 0000000..4b6c67d --- /dev/null +++ b/lib/libc/gen/usleep.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)usleep.c 8.1 (Berkeley) 6/4/93"; +#endif +static char rcsid[] = + "$FreeBSD$"; +#endif /* LIBC_SCCS and not lint */ + +#include <time.h> +#include <unistd.h> + +int +usleep(useconds) + unsigned int useconds; +{ + struct timespec time_to_sleep; + + time_to_sleep.tv_nsec = (useconds % 1000000) * 1000; + time_to_sleep.tv_sec = useconds / 1000000; + return (nanosleep(&time_to_sleep, NULL)); +} diff --git a/lib/libc/gen/utime.3 b/lib/libc/gen/utime.3 new file mode 100644 index 0000000..b8d66fd --- /dev/null +++ b/lib/libc/gen/utime.3 @@ -0,0 +1,88 @@ +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)utime.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt UTIME 3 +.Os BSD 4 +.Sh NAME +.Nm utime +.Nd set file times +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <utime.h> +.Ft int +.Fn utime "const char *file" "const struct utimbuf *timep" +.Sh DESCRIPTION +.Bf -symbolic +This interface is obsoleted by utimes(2) . +.Ef +.Pp +The +.Fn utime +function sets the access and modification times of the named file from +the structures in the argument array +.Fa timep . +.Pp +If the times are specified (the +.Fa timep +argument is +.Pf non- Dv NULL ) +the caller must be the owner of the file or be the super-user. +.Pp +If the times are not specified (the +.Fa timep +argument is +.Dv NULL ) +the caller must be the owner of the file, have permission to write +the file, or be the super-user. +.Sh ERRORS +The +.Fn utime +function may fail and set +.Va errno +for any of the errors specified for the library function +.Xr utimes 2 . +.Sh SEE ALSO +.Xr stat 2 , +.Xr utimes 2 +.Sh HISTORY +A +.Fn utime +function appeared in +.At v7 . +.Sh STANDARDS +The +.Fn utime +function conforms to +.St -p1003.1-88 . diff --git a/lib/libc/gen/utime.c b/lib/libc/gen/utime.c new file mode 100644 index 0000000..a561fd6 --- /dev/null +++ b/lib/libc/gen/utime.c @@ -0,0 +1,57 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)utime.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/time.h> + +#include <utime.h> + +int +utime(path, times) + const char *path; + const struct utimbuf *times; +{ + struct timeval tv[2], *tvp; + + if (times) { + tv[0].tv_sec = times->actime; + tv[1].tv_sec = times->modtime; + tv[0].tv_usec = tv[1].tv_usec = 0; + tvp = tv; + } else + tvp = NULL; + return (utimes(path, tvp)); +} diff --git a/lib/libc/gen/valloc.3 b/lib/libc/gen/valloc.3 new file mode 100644 index 0000000..e97d1f3 --- /dev/null +++ b/lib/libc/gen/valloc.3 @@ -0,0 +1,76 @@ +.\" Copyright (c) 1980, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" @(#)valloc.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd June 4, 1993 +.Dt VALLOC 3 +.Os BSD 3 +.Sh NAME +.Nm valloc +.Nd aligned memory allocation function +.Sh SYNOPSIS +.Fd #include <unistd.h> +.Ft void * +.Fn valloc "size_t size" +.Sh DESCRIPTION +.Bf -symbolic +Valloc is obsoleted by the current version of malloc(3), +which aligns page-sized and larger allocations. +.Ef +.Pp +The +.Fn valloc +function +allocates +.Fa size +bytes aligned on a page boundary. +It is implemented by calling +.Xr malloc 3 +with a slightly larger request, saving the true beginning of the block +allocated, and returning a properly aligned pointer. +.Sh RETURN VALUES +The +.Fn valloc +function returns +a pointer to the allocated space if successful; otherwise +a null pointer is returned +.Sh HISTORY +The +.Fn valloc +function appeared in +.Bx 3.0 . +.Sh BUGS +A +.Em vfree +function +has not been implemented. diff --git a/lib/libc/gen/valloc.c b/lib/libc/gen/valloc.c new file mode 100644 index 0000000..6035329 --- /dev/null +++ b/lib/libc/gen/valloc.c @@ -0,0 +1,50 @@ +/* + * Copyright (c) 1980, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)valloc.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <stdlib.h> +#include <unistd.h> + +void * +valloc(i) + size_t i; +{ + long valsiz = getpagesize(), j; + void *cp = malloc(i + (valsiz-1)); + + j = ((long)cp + (valsiz-1)) &~ (valsiz-1); + return ((void *)j); +} diff --git a/lib/libc/gen/vis.3 b/lib/libc/gen/vis.3 new file mode 100644 index 0000000..e8860b0 --- /dev/null +++ b/lib/libc/gen/vis.3 @@ -0,0 +1,265 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" 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. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.\" From: @(#)vis.3 8.1 (Berkeley) 6/9/93 +.\" $FreeBSD$ +.\" +.Dd July 25, 1996 +.Dt VIS 3 +.Os +.Sh NAME +.Nm vis +.Nd visually encode characters +.Sh SYNOPSIS +.Fd #include <vis.h> +.Ft char * +.Fn vis "char *dst" "int c" "int flag" "int nextc" +.Ft int +.Fn strvis "char *dst" "const char *src" "int flag" +.Ft int +.Fn strvisx "char *dst" "const char *src" "size_t len" "int flag" +.Sh DESCRIPTION +The +.Fn vis +function +copies into +.Fa dst +a string which represents the character +.Fa c . +If +.Fa c +needs no encoding, it is copied in unaltered. The string is +null terminated, and a pointer to the end of the string is +returned. The maximum length of any encoding is four +characters (not including the trailing +.Dv NUL ) ; +thus, when +encoding a set of characters into a buffer, the size of the buffer should +be four times the number of characters encoded, plus one for the trailing +.Dv NUL . +The flag parameter is used for altering the default range of +characters considered for encoding and for altering the visual +representation. +The additional character, +.Fa nextc , +is only used when selecting the +.Dv VIS_CSTYLE +encoding format (explained below). +.Pp +The +.Fn strvis +and +.Fn strvisx +functions copy into +.Fa dst +a visual representation of +the string +.Fa src . +The +.Fn strvis +function encodes characters from +.Fa src +up to the +first +.Dv NUL . +The +.Fn strvisx +function encodes exactly +.Fa len +characters from +.Fa src +(this +is useful for encoding a block of data that may contain +.Dv NUL Ns 's). +Both forms +.Dv NUL +terminate +.Fa dst . +The size of +.Fa dst +must be four times the number +of characters encoded from +.Fa src +(plus one for the +.Dv NUL ) . +Both +forms return the number of characters in dst (not including +the trailing +.Dv NUL ) . +.Pp +The encoding is a unique, invertible representation composed entirely of +graphic characters; it can be decoded back into the original form using +the +.Xr unvis 3 +or +.Xr strunvis 3 +functions. +.Pp +There are two parameters that can be controlled: the range of +characters that are encoded, and the type +of representation used. +By default, all non-graphic characters. +except space, tab, and newline are encoded. +(See +.Xr isgraph 3 . ) +The following flags +alter this: +.Bl -tag -width VIS_WHITEX +.It Dv VIS_SP +Also encode space. +.It Dv VIS_TAB +Also encode tab. +.It Dv VIS_NL +Also encode newline. +.It Dv VIS_WHITE +Synonym for +.Dv VIS_SP +\&| +.Dv VIS_TAB +\&| +.Dv VIS_NL . +.It Dv VIS_SAFE +Only encode "unsafe" characters. Unsafe means control +characters which may cause common terminals to perform +unexpected functions. Currently this form allows space, +tab, newline, backspace, bell, and return - in addition +to all graphic characters - unencoded. +.El +.Pp +There are three forms of encoding. +All forms use the backslash character +.Ql \e +to introduce a special +sequence; two backslashes are used to represent a real backslash. +These are the visual formats: +.Bl -tag -width VIS_CSTYLE +.It (default) +Use an +.Ql M +to represent meta characters (characters with the 8th +bit set), and use carat +.Ql ^ +to represent control characters see +.Pf ( Xr iscntrl 3 ) . +The following formats are used: +.Bl -tag -width xxxxx +.It Dv \e^C +Represents the control character +.Ql C . +Spans characters +.Ql \e000 +through +.Ql \e037 , +and +.Ql \e177 +(as +.Ql \e^? ) . +.It Dv \eM-C +Represents character +.Ql C +with the 8th bit set. +Spans characters +.Ql \e241 +through +.Ql \e376 . +.It Dv \eM^C +Represents control character +.Ql C +with the 8th bit set. +Spans characters +.Ql \e200 +through +.Ql \e237 , +and +.Ql \e377 +(as +.Ql \eM^? ) . +.It Dv \e040 +Represents +.Tn ASCII +space. +.It Dv \e240 +Represents Meta-space. +.El +.Pp +.It Dv VIS_CSTYLE +Use C-style backslash sequences to represent standard non-printable +characters. +The following sequences are used to represent the indicated characters: +.Bd -unfilled -offset indent +.Li \ea Tn - BEL No (007) +.Li \eb Tn - BS No (010) +.Li \ef Tn - NP No (014) +.Li \en Tn - NL No (012) +.Li \er Tn - CR No (015) +.Li \et Tn - HT No (011) +.Li \ev Tn - VT No (013) +.Li \e0 Tn - NUL No (000) +.Ed +.Pp +When using this format, the nextc parameter is looked at to determine +if a +.Dv NUL +character can be encoded as +.Ql \e0 +instead of +.Ql \e000 . +If +.Fa nextc +is an octal digit, the latter representation is used to +avoid ambiguity. +.It Dv VIS_OCTAL +Use a three digit octal sequence. The form is +.Ql \eddd +where +.Em d +represents an octal digit. +.El +.Pp +There is one additional flag, +.Dv VIS_NOSLASH , +which inhibits the +doubling of backslashes and the backslash before the default +format (that is, control characters are represented by +.Ql ^C +and +meta characters as +.Ql M-C ) . +With this flag set, the encoding is +ambiguous and non-invertible. +.Sh SEE ALSO +.Xr unvis 1 , +.Xr strunvis 3 , +.Xr unvis 3 +.Sh HISTORY +These functions first appeared in +.Bx 4.4 . + diff --git a/lib/libc/gen/vis.c b/lib/libc/gen/vis.c new file mode 100644 index 0000000..61aa836 --- /dev/null +++ b/lib/libc/gen/vis.c @@ -0,0 +1,187 @@ +/*- + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 7/19/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <limits.h> +#include <ctype.h> +#include <vis.h> + +#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') + +/* + * vis - visually encode characters + */ +char * +vis(dst, c, flag, nextc) + register char *dst; + int c, nextc; + register int flag; +{ + c = (unsigned char)c; + if (isgraph(c) || + ((flag & VIS_SP) == 0 && c == ' ') || + ((flag & VIS_TAB) == 0 && c == '\t') || + ((flag & VIS_NL) == 0 && c == '\n') || + ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) { + *dst++ = c; + if (c == '\\' && (flag & VIS_NOSLASH) == 0) + *dst++ = '\\'; + *dst = '\0'; + return (dst); + } + + if (flag & VIS_CSTYLE) { + switch(c) { + case '\n': + *dst++ = '\\'; + *dst++ = 'n'; + goto done; + case '\r': + *dst++ = '\\'; + *dst++ = 'r'; + goto done; + case '\b': + *dst++ = '\\'; + *dst++ = 'b'; + goto done; +#if __STDC__ + case '\a': +#else + case '\007': +#endif + *dst++ = '\\'; + *dst++ = 'a'; + goto done; + case '\v': + *dst++ = '\\'; + *dst++ = 'v'; + goto done; + case '\t': + *dst++ = '\\'; + *dst++ = 't'; + goto done; + case '\f': + *dst++ = '\\'; + *dst++ = 'f'; + goto done; + case ' ': + *dst++ = '\\'; + *dst++ = 's'; + goto done; + case '\0': + *dst++ = '\\'; + *dst++ = '0'; + if (isoctal(nextc)) { + *dst++ = '0'; + *dst++ = '0'; + } + goto done; + } + } + if (((c & 0177) == ' ') || (flag & VIS_OCTAL)) { + *dst++ = '\\'; + *dst++ = ((u_char)c >> 6 & 07) + '0'; + *dst++ = ((u_char)c >> 3 & 07) + '0'; + *dst++ = ((u_char)c & 07) + '0'; + goto done; + } + if ((flag & VIS_NOSLASH) == 0) + *dst++ = '\\'; + if (c & 0200) { + c &= 0177; + *dst++ = 'M'; + } + if (iscntrl(c)) { + *dst++ = '^'; + if (c == 0177) + *dst++ = '?'; + else + *dst++ = c + '@'; + } else { + *dst++ = '-'; + *dst++ = c; + } +done: + *dst = '\0'; + return (dst); +} + +/* + * strvis, strvisx - visually encode characters from src into dst + * + * Dst must be 4 times the size of src to account for possible + * expansion. The length of dst, not including the trailing NULL, + * is returned. + * + * Strvisx encodes exactly len bytes from src into dst. + * This is useful for encoding a block of data. + */ +int +strvis(dst, src, flag) + register char *dst; + register const char *src; + int flag; +{ + register char c; + char *start; + + for (start = dst; (c = *src); ) + dst = vis(dst, c, flag, *++src); + *dst = '\0'; + return (dst - start); +} + +int +strvisx(dst, src, len, flag) + register char *dst; + register const char *src; + register size_t len; + int flag; +{ + int c; + char *start; + + for (start = dst; len > 1; len--) { + c = *src; + dst = vis(dst, c, flag, *++src); + } + if (len) + dst = vis(dst, *src, flag, '\0'); + *dst = '\0'; + + return (dst - start); +} diff --git a/lib/libc/gen/wait.c b/lib/libc/gen/wait.c new file mode 100644 index 0000000..0bf5e8b --- /dev/null +++ b/lib/libc/gen/wait.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)wait.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/time.h> +#include <sys/wait.h> +#include <sys/resource.h> + +pid_t +wait(istat) + int *istat; +{ + return (wait4(WAIT_ANY, istat, 0, (struct rusage *)0)); +} diff --git a/lib/libc/gen/wait3.c b/lib/libc/gen/wait3.c new file mode 100644 index 0000000..c8d8f9e --- /dev/null +++ b/lib/libc/gen/wait3.c @@ -0,0 +1,50 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)wait3.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/time.h> +#include <sys/wait.h> +#include <sys/resource.h> + +pid_t +wait3(istat, options, rup) + int *istat; + int options; + struct rusage *rup; +{ + return (wait4(WAIT_ANY, istat, options, rup)); +} diff --git a/lib/libc/gen/waitpid.c b/lib/libc/gen/waitpid.c new file mode 100644 index 0000000..7af6a63 --- /dev/null +++ b/lib/libc/gen/waitpid.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)waitpid.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <sys/time.h> +#include <sys/wait.h> +#include <sys/resource.h> + +pid_t +#if __STDC__ +waitpid(pid_t pid, int *istat, int options) +#else +waitpid(pid, istat, options) + pid_t pid; + int *istat; + int options; +#endif +{ + return (wait4(pid, istat, options, (struct rusage *)0)); +} |