From dba3562d7800d1fed3cb2cd859754872fbb2e84f Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Tue, 11 Sep 2012 18:02:49 +0200 Subject: [PATCH] Updating libutil from netbsd --- include/util.h | 18 +++- lib/libutil/Makefile | 29 +++--- lib/libutil/getdiskrawname.3 | 71 +++++++++++++ lib/libutil/getdiskrawname.c | 89 ++++++++++++++++ lib/libutil/getfsspecname.3 | 84 +++++++++++++++ lib/libutil/getfsspecname.c | 147 ++++++++++++++++++++++++++ lib/libutil/getfstypename.3 | 55 ++++++++++ lib/libutil/getlabelsector.3 | 16 ++- lib/libutil/getlabelsector.c | 18 +++- lib/libutil/getrawpartition.c | 6 +- lib/libutil/login_cap.c | 26 ++--- lib/libutil/login_tty.c | 6 +- lib/libutil/openpty.3 | 17 ++- lib/libutil/parsedate.3 | 6 +- lib/libutil/parsedate.y | 123 +++++----------------- lib/libutil/passwd.c | 10 +- lib/libutil/pidfile.3 | 48 ++++++--- lib/libutil/pidfile.c | 160 +++++++++++++++++++---------- lib/libutil/pidlock.c | 20 ++-- lib/libutil/proc_compare.3 | 107 +++++++++++++++++++ lib/libutil/pty.c | 10 +- lib/libutil/raise_default_signal.3 | 4 +- lib/libutil/shlib_version | 4 + lib/libutil/sockaddr_snprintf.c | 20 ++++ lib/libutil/stat_flags.3 | 5 +- lib/libutil/strpct.3 | 99 ++++++++++++++++++ lib/libutil/strpct.c | 127 +++++++++++++++++++++++ lib/libutil/util.3 | 8 +- 28 files changed, 1087 insertions(+), 246 deletions(-) create mode 100644 lib/libutil/getdiskrawname.3 create mode 100644 lib/libutil/getdiskrawname.c create mode 100644 lib/libutil/getfsspecname.3 create mode 100644 lib/libutil/getfsspecname.c create mode 100644 lib/libutil/getfstypename.3 create mode 100644 lib/libutil/proc_compare.3 create mode 100644 lib/libutil/strpct.3 create mode 100644 lib/libutil/strpct.c diff --git a/include/util.h b/include/util.h index 34252cca4..b9ea78b08 100644 --- a/include/util.h +++ b/include/util.h @@ -1,4 +1,4 @@ -/* $NetBSD: util.h,v 1.55 2010/02/25 18:37:12 joerg Exp $ */ +/* $NetBSD: util.h,v 1.63 2012/04/07 16:44:59 christos Exp $ */ /*- * Copyright (c) 1995 @@ -35,11 +35,13 @@ #include #include #include -#include #include +#include +#include #include #include #include +#include #include #ifdef _BSD_TIME_T_ @@ -68,8 +70,13 @@ pid_t forkpty(int *, char *, struct termios *, struct winsize *); const char *getbootfile(void); off_t getlabeloffset(void); int getlabelsector(void); +int getlabelusesmbr(void); int getmaxpartitions(void); int getrawpartition(void); +const char *getdiskrawname(char *, size_t, const char *); +const char *getdiskcookedname(char *, size_t, const char *); +const char *getfstypename(int); +const char *getfsspecname(char *, size_t, const char *); #ifndef __LIBC12_SOURCE__ void login(const struct utmp *) __RENAME(__login50); void loginx(const struct utmpx *) __RENAME(__loginx50); @@ -98,7 +105,7 @@ int pw_copyx(int, int, struct passwd *, struct passwd *, char *, size_t) __RENAME(__pw_copyx50); #endif void pw_edit(int, const char *); -void pw_error(const char *, int, int); +__dead void pw_error(const char *, int, int); void pw_getconf(char *, size_t, const char *, const char *); #ifndef __LIBC12_SOURCE__ void pw_getpwconf(char *, size_t, const struct passwd *, @@ -116,6 +123,8 @@ int snprintb_m(char *, size_t, const char *, uint64_t, size_t); int snprintb(char *, size_t, const char *, uint64_t); int sockaddr_snprintf(char *, size_t, const char *, const struct sockaddr *); +char *strpct(char *, size_t, uintmax_t, uintmax_t, size_t); +char *strspct(char *, size_t, intmax_t, intmax_t, size_t); int string_to_flags(char **, unsigned long *, unsigned long *); int ttyaction(const char *, const char *, const char *); int ttylock(const char *, int, pid_t *); @@ -138,8 +147,7 @@ void *erealloc(void *, size_t); struct __sFILE *efopen(const char *, const char *); int easprintf(char ** __restrict, const char * __restrict, ...) __printflike(2, 3); -int evasprintf(char ** __restrict, const char * __restrict, - _BSD_VA_LIST_) +int evasprintf(char ** __restrict, const char * __restrict, va_list) __printflike(2, 0); __END_DECLS diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile index 908558bbb..af32c645f 100644 --- a/lib/libutil/Makefile +++ b/lib/libutil/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.63 2010/01/27 19:10:31 drochner Exp $ +# $NetBSD: Makefile,v 1.73 2012/04/07 16:44:39 christos Exp $ # @(#)Makefile 8.1 (Berkeley) 6/4/93 USE_SHLIBDIR= yes @@ -7,35 +7,36 @@ USE_SHLIBDIR= yes .include "${NETBSDSRCDIR}/common/lib/libutil/Makefile.inc" -WARNS= 4 +WARNS?= 5 LIB= util CPPFLAGS+=-DLIBC_SCCS -I${.CURDIR} +LINTFLAGS+=-w .if !defined(__MINIX) SRCS+= efun.c getbootfile.c getlabelsector.c getmaxpartitions.c \ - getmntopts.c getrawpartition.c \ + getfsspecname.c getmntopts.c getrawpartition.c getdiskrawname.c \ disklabel_dkcksum.c disklabel_scan.c \ if_media.c \ login.c loginx.c login_cap.c login_tty.c logout.c logoutx.c \ logwtmp.c logwtmpx.c opendisk.c parsedate.y \ passwd.c pw_scan.c pidfile.c pidlock.c pty.c \ raise_default_signal.c \ - secure_path.c stat_flags.c \ - ttyaction.c ttymsg.c \ + secure_path.c sockaddr_snprintf.c stat_flags.c \ + strpct.c ttyaction.c ttymsg.c -MAN= efun.3 getbootfile.3 getlabelsector.3 getmaxpartitions.3 \ - getmntopts.3 \ - getrawpartition.3 \ +MAN= efun.3 getbootfile.3 getfstypename.3 getlabelsector.3 \ + getmaxpartitions.3 getmntopts.3 getrawpartition.3 \ + getdiskrawname.3 getfsspecname.3 \ login.3 login_cap.3 loginx.3 \ disklabel_dkcksum.3 disklabel_scan.3 \ opendisk.3 openpty.3 parsedate.3 pidfile.3 pidlock.3 \ - pw_getconf.3 pw_init.3 pw_lock.3 secure_path.3 \ + proc_compare.3 pw_getconf.3 pw_init.3 pw_lock.3 secure_path.3 \ raise_default_signal.3 \ - snprintb.3 sockaddr_snprintf.3 stat_flags.3 ttyaction.3 \ + snprintb.3 sockaddr_snprintf.3 stat_flags.3 strpct.3 ttyaction.3 \ ttymsg.3 util.3 .else SRCS+= efun.c getbootfile.c \ - getmntopts.c sockaddr_snprintf.c\ + getfsspecname.c getmntopts.c sockaddr_snprintf.c\ login.c loginx.c login_cap.c login_tty.c logout.c logoutx.c \ logwtmp.c logwtmpx.c opendisk.c \ passwd.c pw_scan.c pidfile.c pidlock.c pty.c \ @@ -44,7 +45,7 @@ SRCS+= efun.c getbootfile.c \ ttyaction.c \ MAN= efun.3 getbootfile.3 \ - getmntopts.3 \ + getmntopts.3 getfsspecname.3 \ login.3 login_cap.3 loginx.3 \ opendisk.3 openpty.3 pidfile.3 pidlock.3 \ pw_getconf.3 pw_init.3 pw_lock.3 secure_path.3 \ @@ -65,6 +66,8 @@ YPREFIX=__pd .if !defined(__MINIX) MLINKS+=getlabelsector.3 getlabeloffset.3 .endif +MLINKS+=getlabelsector.3 getlabelusesmbr.3 +MLINKS+=getdiskrawname.3 getdiskcookedname.3 MLINKS+=login.3 logout.3 MLINKS+=login.3 logwtmp.3 MLINKS+=login_cap.3 login_getclass.3 @@ -106,5 +109,7 @@ MLINKS+=efun.3 evasprintf.3 MLINKS+=stat_flags.3 string_to_flags.3 MLINKS+=stat_flags.3 flags_to_string.3 MLINKS+=snprintb.3 snprintb_m.3 +MLINKS+=util.3 libutil.3 +MLINKS+=strpct.3 strspct.3 .include diff --git a/lib/libutil/getdiskrawname.3 b/lib/libutil/getdiskrawname.3 new file mode 100644 index 000000000..bb71354fa --- /dev/null +++ b/lib/libutil/getdiskrawname.3 @@ -0,0 +1,71 @@ +.\" $NetBSD: getdiskrawname.3,v 1.2 2012/04/08 16:06:23 wiz Exp $ +.\" +.\" Copyright (c) 2012 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Christos Zoulas. +.\" +.\" 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 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 FOUNDATION 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. +.\" +.\" +.Dd March 7, 2012 +.Dt GETDISKRAWNAME 3 +.Os +.Sh NAME +.Nm getdiskrawname +.Nd get the the block/character device name for a disk +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In util.h +.Ft const char * +.Fn getdiskrawname "char *buf" "size_t buflen" "const char *name" +.Ft const char * +.Fn getdiskcookedname "char *buf" "size_t buflen" "const char *name" +.Sh DESCRIPTION +The +.Fn getdiskrawname +function converts the +.Fa name +argument thar contains a path to a disk block device node to the +path that contains the corresponding character device node. +The +.Fn getdiskcookedname +function converts the +.Fa name +argument thar contains a path to a disk character device node to the +path that contains the corresponding block device node. +.Sh RETURN VALUES +On success the absolute pathname of the underlying device node is returned. +On failure +.Dv NULL +is returned and +.Va errno +contains the reason for the error. +.Sh HISTORY +The +.Fn getdiskrawname +and +.Fn getdiskcookedname +functions appeared in +.Nx 7.0 . diff --git a/lib/libutil/getdiskrawname.c b/lib/libutil/getdiskrawname.c new file mode 100644 index 000000000..81f9ac7f3 --- /dev/null +++ b/lib/libutil/getdiskrawname.c @@ -0,0 +1,89 @@ +/* $NetBSD: getdiskrawname.c,v 1.1 2012/04/07 16:44:39 christos Exp $ */ + +/*- + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 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 FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include +__RCSID("$NetBSD: getdiskrawname.c,v 1.1 2012/04/07 16:44:39 christos Exp $"); + +#include + +#include +#include +#include +#include + +const char * +getdiskrawname(char *buf, size_t bufsiz, const char *name) +{ + const char *dp = strrchr(name, '/'); + struct stat st; + + if (dp == NULL) { + errno = EINVAL; + return NULL; + } + + if (stat(name, &st) == -1) + return NULL; + + if (!S_ISBLK(st.st_mode)) { + errno = EFTYPE; + return NULL; + } + + (void)snprintf(buf, bufsiz, "%.*s/r%s", (int)(dp - name), name, dp + 1); + + return buf; +} + +const char * +getdiskcookedname(char *buf, size_t bufsiz, const char *name) +{ + const char *dp; + struct stat st; + + if ((dp = strrchr(name, '/')) == NULL) { + errno = EINVAL; + return NULL; + } + if (stat(name, &st) == -1) + return NULL; + + if (!S_ISCHR(st.st_mode)) { + errno = EFTYPE; + return NULL; + } + if (dp[1] != 'r') { + errno = EINVAL; + return NULL; + } + (void)snprintf(buf, bufsiz, "%.*s/%s", (int)(dp - name), name, dp + 2); + + return buf; +} diff --git a/lib/libutil/getfsspecname.3 b/lib/libutil/getfsspecname.3 new file mode 100644 index 000000000..3af38f4e6 --- /dev/null +++ b/lib/libutil/getfsspecname.3 @@ -0,0 +1,84 @@ +.\" $NetBSD: getfsspecname.3,v 1.3 2012/04/07 16:22:42 christos Exp $ +.\" +.\" Copyright (c) 2012 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Christos Zoulas. +.\" +.\" 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 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 FOUNDATION 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. +.\" +.\" +.Dd March 6, 2012 +.Dt GETFSSPECNAME 3 +.Os +.Sh NAME +.Nm getfsspecname +.Nd get the underlying wedge name from a label +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In util.h +.Ft const char * +.Fn getfsspecname "char *buf" "size_t buflen" "const char *spec" +.Sh DESCRIPTION +The +.Fn getfsspecname +function translates an +.Ft fs_spec +field in the +.Fa spec +argument of the form +.Dq NAME=wedgename +to the underlying +.Xr dk 4 +device node, and places the resulting pathname in +.Fa buf +up to len +.Fa bufsiz . +.Pp +If the +.Fa spec +argument is not of the form +.Dq NAME=wedgename , +.Fa spec +is copied +to +.Fa buf +and returned. +.Sh RETURN VALUES +On success the absolute pathname of the underlying wedge device is returned, +or the original +.Fa spec +argument. +On failure +.Dv NULL +is return and +.Fa buf +contains the reason for the error. +.Sh SEE ALSO +.Xr fstab 5 +.Sh HISTORY +The +.Fn getfsspecname +function appeared in +.Nx 7.0 . diff --git a/lib/libutil/getfsspecname.c b/lib/libutil/getfsspecname.c new file mode 100644 index 000000000..f5b530be6 --- /dev/null +++ b/lib/libutil/getfsspecname.c @@ -0,0 +1,147 @@ +/* $NetBSD: getfsspecname.c,v 1.3 2012/04/08 20:56:12 christos Exp $ */ + +/*- + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * 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 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 FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include +__RCSID("$NetBSD: getfsspecname.c,v 1.3 2012/04/08 20:56:12 christos Exp $"); + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define COMPAT_DKWEDGE /* To be removed */ + +const char * +getfsspecname(char *buf, size_t bufsiz, const char *name) +{ + static const int mib[] = { CTL_HW, HW_DISKNAMES }; + static const unsigned int miblen = __arraycount(mib); + char *drives, *dk; + size_t len; + int fd, savee; + char *vname; + + drives = NULL; + vname = NULL; + if (strncasecmp(name, "NAME=", 5) != 0) { +#ifdef COMPAT_DKWEDGE + /* + * We try to open the disk name, and if we fail with EBUSY + * we use the name as the label to find the wedge. + */ + char rbuf[MAXPATHLEN]; + if (name[0] == '/') { + if (getdiskrawname(rbuf, sizeof(rbuf), name) != NULL) { + if ((fd = open(rbuf, O_RDONLY)) == -1) { + if (errno == EBUSY) { + name = strrchr(name, '/') + 1; + goto search; + } + } else + close(fd); + } + } +#endif + strlcpy(buf, name, bufsiz); + return buf; + } else + name += 5; + +#ifdef COMPAT_DKWEDGE +search: +#endif + vname = malloc(strlen(name) * 4 + 1); + if (vname == NULL) { + savee = errno; + strlcpy(buf, "malloc failed", bufsiz); + goto out; + } + + strunvis(vname, name); + + if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) { + savee = errno; + strlcpy(buf, "sysctl hw.disknames failed", bufsiz); + goto out; + } + + drives = malloc(len); + if (drives == NULL) { + savee = errno; + strlcpy(buf, "malloc failed", bufsiz); + goto out; + } + if (sysctl(mib, miblen, drives, &len, NULL, 0) == -1) { + savee = errno; + strlcpy(buf, "sysctl hw.disknames failed", bufsiz); + goto out; + } + + for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) { + struct dkwedge_info dkw; + if (strncmp(dk, "dk", 2) != 0) + continue; + fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0); + if (fd == -1) + continue; + if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1) { + savee = errno; + snprintf(buf, bufsiz, "%s: getwedgeinfo", dk); + (void)close(fd); + goto out; + } + (void)close(fd); + if (strcmp(vname, (char *)dkw.dkw_wname) == 0) { + char *p = strstr(buf, "/rdk"); + if (p++ == NULL) + return buf; + strcpy(p, p + 1); + free(drives); + free(vname); + return buf; + } + } + savee = ESRCH; + snprintf(buf, bufsiz, "no match for `%s'", vname); +out: + free(drives); + free(vname); + errno = savee; + return NULL; +} diff --git a/lib/libutil/getfstypename.3 b/lib/libutil/getfstypename.3 new file mode 100644 index 000000000..068d254f3 --- /dev/null +++ b/lib/libutil/getfstypename.3 @@ -0,0 +1,55 @@ +.\" $NetBSD: getfstypename.3,v 1.3 2011/11/18 04:15:41 christos Exp $ +.\" +.\" Copyright (c) 2011 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Christos Zoulas. +.\" +.\" 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 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 FOUNDATION 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. +.\" +.Dd November 13, 2011 +.Dt GETFSTYPENAME 3 +.Os +.Sh NAME +.Nm getfstypename +.Nd convert a partition file system type integer to a wedge partition type name +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In util.h +.Ft const char * +.Fn getfstypename "int fstype" +.Sh DESCRIPTION +.Fn getfstypename +returns the disk wedge partitition type name corresponding to the +.Ar fstype +argument as specified in +.In sys/disk.h +or +.Dv DKW_PTYPE_UNKNOWN +if none is found. +.Sh HISTORY +The +.Fn getfstypename +function call appeared in +.Nx 6.0 . diff --git a/lib/libutil/getlabelsector.3 b/lib/libutil/getlabelsector.3 index 5db5aff6e..ed2a722ce 100644 --- a/lib/libutil/getlabelsector.3 +++ b/lib/libutil/getlabelsector.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: getlabelsector.3,v 1.5 2009/06/24 22:31:58 zafer Exp $ +.\" $NetBSD: getlabelsector.3,v 1.8 2011/09/14 11:43:29 njoly Exp $ .\" .\" .\" Copyright 2002 Wasabi Systems, Inc. @@ -34,13 +34,14 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd December 11, 2002 +.Dd August 25, 2011 .Dt GETLABELSECTOR 3 .Os .Sh NAME .Nm getlabelsector , -.Nm getlabeloffset -.Nd get the sector number and offset of the disklabel +.Nm getlabeloffset , +.Nm getlabelusesmbr +.Nd get disklabel location informations .Sh LIBRARY .Lb libutil .Sh SYNOPSIS @@ -49,6 +50,8 @@ .Fn getlabelsector void .Ft off_t .Fn getlabeloffset void +.Ft int +.Fn getlabelusesmbr void .Sh DESCRIPTION The .Fn getlabelsector @@ -63,6 +66,11 @@ and .Dv LABELOFFSET definitions previously used to derive the location of the .Xr disklabel 5 . +.Pp +The +.Fn getlabelusesmbr +returns 1 if the disklabel is located inside a MBR partition, 0 if it's stored +relative to the start of the disk, or \-1 on error. .Sh SEE ALSO .Xr sysctl 3 , .Xr disklabel 5 diff --git a/lib/libutil/getlabelsector.c b/lib/libutil/getlabelsector.c index 7badd029a..114f9bd82 100644 --- a/lib/libutil/getlabelsector.c +++ b/lib/libutil/getlabelsector.c @@ -1,4 +1,4 @@ -/* $NetBSD: getlabelsector.c,v 1.3 2005/09/17 01:51:21 elad Exp $ */ +/* $NetBSD: getlabelsector.c,v 1.5 2011/09/04 12:34:49 jmcneill Exp $ */ /* * Copyright 2002 Wasabi Systems, Inc. @@ -37,7 +37,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: getlabelsector.c,v 1.3 2005/09/17 01:51:21 elad Exp $"); +__RCSID("$NetBSD: getlabelsector.c,v 1.5 2011/09/04 12:34:49 jmcneill Exp $"); #endif #include @@ -73,3 +73,17 @@ getlabeloffset(void) return ((off_t)offset); } + +int +getlabelusesmbr(void) +{ + int use; + size_t uselen; + + uselen = sizeof(use); + if (sysctlbyname("kern.labelusesmbr", &use, &uselen, + NULL, (size_t)0) < 0) + return (-1); + + return use; +} diff --git a/lib/libutil/getrawpartition.c b/lib/libutil/getrawpartition.c index 159cc8824..85238430d 100644 --- a/lib/libutil/getrawpartition.c +++ b/lib/libutil/getrawpartition.c @@ -1,4 +1,4 @@ -/* $NetBSD: getrawpartition.c,v 1.6 2008/04/28 20:23:03 martin Exp $ */ +/* $NetBSD: getrawpartition.c,v 1.7 2012/06/25 22:32:47 abs Exp $ */ /*- * Copyright (c) 1996 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: getrawpartition.c,v 1.6 2008/04/28 20:23:03 martin Exp $"); +__RCSID("$NetBSD: getrawpartition.c,v 1.7 2012/06/25 22:32:47 abs Exp $"); #endif #include @@ -39,7 +39,7 @@ __RCSID("$NetBSD: getrawpartition.c,v 1.6 2008/04/28 20:23:03 martin Exp $"); #include int -getrawpartition() +getrawpartition(void) { int rawpart, mib[2]; size_t varlen; diff --git a/lib/libutil/login_cap.c b/lib/libutil/login_cap.c index 8e8745a49..0daab2c3e 100644 --- a/lib/libutil/login_cap.c +++ b/lib/libutil/login_cap.c @@ -1,4 +1,4 @@ -/* $NetBSD: login_cap.c,v 1.29 2007/12/04 22:09:02 mjf Exp $ */ +/* $NetBSD: login_cap.c,v 1.30 2012/04/07 16:16:34 christos Exp $ */ /*- * Copyright (c) 1995,1997 Berkeley Software Design, Inc. All rights reserved. @@ -36,7 +36,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: login_cap.c,v 1.29 2007/12/04 22:09:02 mjf Exp $"); +__RCSID("$NetBSD: login_cap.c,v 1.30 2012/04/07 16:16:34 christos Exp $"); #endif /* LIBC_SCCS and not lint */ #include @@ -448,25 +448,25 @@ gsetrl(login_cap_t *lc, int what, const char *name, int type) return (-1); } -#define RCUR r.rlim_cur -#define RMAX r.rlim_max +#define RCUR ((quad_t)r.rlim_cur) +#define RMAX ((quad_t)r.rlim_max) switch (type) { case R_CTIME: - RCUR = login_getcaptime(lc, name, RCUR, RCUR); - RMAX = login_getcaptime(lc, name, RMAX, RMAX); + r.rlim_cur = login_getcaptime(lc, name, RCUR, RCUR); + r.rlim_max = login_getcaptime(lc, name, RMAX, RMAX); rl.rlim_cur = login_getcaptime(lc, name_cur, RCUR, RCUR); rl.rlim_max = login_getcaptime(lc, name_max, RMAX, RMAX); break; case R_CSIZE: - RCUR = login_getcapsize(lc, name, RCUR, RCUR); - RMAX = login_getcapsize(lc, name, RMAX, RMAX); + r.rlim_cur = login_getcapsize(lc, name, RCUR, RCUR); + r.rlim_max = login_getcapsize(lc, name, RMAX, RMAX); rl.rlim_cur = login_getcapsize(lc, name_cur, RCUR, RCUR); rl.rlim_max = login_getcapsize(lc, name_max, RMAX, RMAX); break; case R_CNUMB: - RCUR = login_getcapnum(lc, name, RCUR, RCUR); - RMAX = login_getcapnum(lc, name, RMAX, RMAX); + r.rlim_cur = login_getcapnum(lc, name, RCUR, RCUR); + r.rlim_max = login_getcapnum(lc, name, RMAX, RMAX); rl.rlim_cur = login_getcapnum(lc, name_cur, RCUR, RCUR); rl.rlim_max = login_getcapnum(lc, name_max, RMAX, RMAX); break; @@ -519,7 +519,7 @@ setuserenv(login_cap_t *lc, envfunc_t senv, void *envp) /* allocate ptr array and string */ count = i; - res = malloc(count * sizeof(char *) + strlen(str) + 1); + res = malloc(count * sizeof(*res) + strlen(str) + 1); if (!res) return -1; @@ -575,8 +575,10 @@ setusercontext(login_cap_t *lc, struct passwd *pwd, uid_t uid, u_int flags) if (!lc) flc = lc = login_getclass(pwd ? pwd->pw_class : NULL); - +#ifdef __minix #define LOGIN_SETLOGIN 0 +#endif + /* * Without the pwd entry being passed we cannot set either * the group or the login. We could complain about it. diff --git a/lib/libutil/login_tty.c b/lib/libutil/login_tty.c index 66a4148cc..56386978c 100644 --- a/lib/libutil/login_tty.c +++ b/lib/libutil/login_tty.c @@ -1,4 +1,4 @@ -/* $NetBSD: login_tty.c,v 1.12 2008/02/09 05:07:26 dholland Exp $ */ +/* $NetBSD: login_tty.c,v 1.13 2011/09/16 16:13:16 plunky Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -34,7 +34,7 @@ #if 0 static char sccsid[] = "@(#)login_tty.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: login_tty.c,v 1.12 2008/02/09 05:07:26 dholland Exp $"); +__RCSID("$NetBSD: login_tty.c,v 1.13 2011/09/16 16:13:16 plunky Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -54,7 +54,7 @@ login_tty(int fd) (void) setsid(); #ifdef TIOCSCTTY - if (ioctl(fd, TIOCSCTTY, (char *)NULL) == -1) + if (ioctl(fd, TIOCSCTTY, NULL) == -1) return (-1); #endif (void) dup2(fd, STDIN_FILENO); diff --git a/lib/libutil/openpty.3 b/lib/libutil/openpty.3 index f6f453dd7..f0c540510 100644 --- a/lib/libutil/openpty.3 +++ b/lib/libutil/openpty.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: openpty.3,v 1.14 2008/11/28 07:17:17 dholland Exp $ +.\" $NetBSD: openpty.3,v 1.17 2012/07/27 21:33:46 christos Exp $ .\" .\" Copyright (c) 1995 .\" The Regents of the University of California. All rights reserved. @@ -31,7 +31,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd November 28, 2008 +.Dd July 27, 2012 .Dt OPENPTY 3 .Os .Sh NAME @@ -68,6 +68,19 @@ If .Fa name is non-null, the filename of the slave is returned in .Fa name . +The length of +.Fa name +is limited to +.Dv PATH_MAX +as any other regular path name, so a buffer of this size should be used. +.\" .Dv 16 +.\" characters in the current +.\" .Xr ptm 4 +.\" device driver (including the terminating +.\" .Dv NUL ) +.\" which limits the maximum to +.\" .Dv 100,000 +.\" ptys. If .Fa termp is non-null, the terminal parameters of the slave will be set to the diff --git a/lib/libutil/parsedate.3 b/lib/libutil/parsedate.3 index 024b0936d..f698ff982 100644 --- a/lib/libutil/parsedate.3 +++ b/lib/libutil/parsedate.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: parsedate.3,v 1.10 2010/12/22 09:12:28 wiz Exp $ +.\" $NetBSD: parsedate.3,v 1.11 2012/04/06 11:36:04 wiz Exp $ .\" .\" Copyright (c) 2006 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -44,7 +44,7 @@ The .Fn parsedate function parses a datetime from .Ar datestr -described in english relative to an optional +described in English relative to an optional .Ar time point and an optional timezone offset in seconds specified in .Ar tzoff . @@ -94,7 +94,7 @@ is unused so that it is not confused with 10, .Dv eleventh or eleven = 11, -.Dv twelfth or twoelve = +.Dv twelfth or twelve = 12. .Pp The following words are recognized in English only: diff --git a/lib/libutil/parsedate.y b/lib/libutil/parsedate.y index bc592ce17..8b17aa234 100644 --- a/lib/libutil/parsedate.y +++ b/lib/libutil/parsedate.y @@ -573,69 +573,24 @@ yyerror(struct dateinfo *param, const char **inp, const char *s __unused) } -static time_t -ToSeconds( - time_t Hours, - time_t Minutes, - time_t Seconds, - MERIDIAN Meridian -) -{ - if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59) - return -1; - switch (Meridian) { - case MER24: - if (Hours < 0 || Hours > 23) - return -1; - return (Hours * 60L + Minutes) * 60L + Seconds; - case MERam: - if (Hours < 1 || Hours > 12) - return -1; - if (Hours == 12) - Hours = 0; - return (Hours * 60L + Minutes) * 60L + Seconds; - case MERpm: - if (Hours < 1 || Hours > 12) - return -1; - if (Hours == 12) - Hours = 0; - return ((Hours + 12) * 60L + Minutes) * 60L + Seconds; - default: - abort (); - } - /* NOTREACHED */ -} - -static int -isLeap(int year) -{ - return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); -} - - /* Year is either * A negative number, which means to use its absolute value (why?) * A number from 0 to 99, which means a year from 1900 to 1999, or * The actual year (>=100). */ static time_t Convert( - time_t Month, - time_t Day, - time_t Year, - time_t Hours, - time_t Minutes, - time_t Seconds, - time_t Timezone, - MERIDIAN Meridian, - DSTMODE DSTmode + time_t Month, /* month of year [1-12] */ + time_t Day, /* day of month [1-31] */ + time_t Year, /* year; see above comment */ + time_t Hours, /* Hour of day [0-24] */ + time_t Minutes, /* Minute of hour [0-59] */ + time_t Seconds, /* Second of minute [0-60] */ + time_t Timezone, /* Timezone as seconds west of UTC */ + MERIDIAN Meridian, /* Hours are am/pm/24 hour clock */ + DSTMODE DSTmode /* DST on/off/maybe */ ) { - static int DaysInMonth[12] = { - 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 - }; - time_t tod; - time_t Julian, oJulian; - int i; + struct tm tm; /* XXX Y2K */ if (Year < 0) @@ -644,53 +599,21 @@ Convert( Year += 2000; else if (Year < 100) Year += 1900; - DaysInMonth[1] = isLeap(Year) ? 29 : 28; - if (Year < EPOCH || Month < 1 || Month > 12 - /* Lint fluff: "conversion from long may lose accuracy" */ - || Day < 1 || Day > DaysInMonth[(int)--Month]) - /* FIXME: - * It would be nice to set a global error string here. - * "February 30 is not a valid date" is much more informative than - * "Can't parse date/time: 100 months" when the user input was - * "100 months" and addition resolved that to February 30, for - * example. See rcs2-7 in src/sanity.sh for more. */ - return -1; - for (Julian = Day - 1, i = 0; i < Month; i++) - Julian += DaysInMonth[i]; - - oJulian = Julian; - for (i = EPOCH; i < Year; i++) { - Julian += 365 + isLeap(i); - if (oJulian > Julian) - return -1; - oJulian = Julian; + tm.tm_sec = Seconds; + tm.tm_min = Minutes; + tm.tm_hour = Hours + (Meridian == MERpm ? 12 : 0); + tm.tm_mday = Day; + tm.tm_mon = Month - 1; + tm.tm_year = Year - 1900; + switch (DSTmode) { + case DSTon: tm.tm_isdst = 1; break; + case DSToff: tm.tm_isdst = 0; break; + default: tm.tm_isdst = -1; break; } + tm.tm_gmtoff = -Timezone; - Julian *= SECSPERDAY; - if (oJulian > Julian) - return -1; - oJulian = Julian; - Julian += Timezone * 60L; - if (Timezone > 0 && oJulian > Julian) - return -1; - oJulian = Julian; - - if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0) - return -1; - - Julian += tod; - if (oJulian > Julian) - return -1; - - if (DSTmode == DSTon || (DSTmode == DSTmaybe)) { - struct tm *tm; - if ((tm = localtime(&Julian)) == NULL) - return -1; - if (tm->tm_isdst) - Julian -= 60 * 60; - } - return Julian; + return mktime(&tm); } @@ -1008,7 +931,7 @@ parsedate(const char *p, const time_t *now, const int *zone) Start = Convert(param.yyMonth, param.yyDay, param.yyYear, param.yyHour, param.yyMinutes, param.yySeconds, param.yyTimezone, param.yyMeridian, param.yyDSTmode); - if (Start < 0) + if (Start == -1) return -1; } else { diff --git a/lib/libutil/passwd.c b/lib/libutil/passwd.c index ad25d72ce..02c589533 100644 --- a/lib/libutil/passwd.c +++ b/lib/libutil/passwd.c @@ -1,4 +1,4 @@ -/* $NetBSD: passwd.c,v 1.50 2010/08/18 08:32:02 christos Exp $ */ +/* $NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs Exp $ */ /* * Copyright (c) 1987, 1993, 1994, 1995 @@ -31,7 +31,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: passwd.c,v 1.50 2010/08/18 08:32:02 christos Exp $"); +__RCSID("$NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs Exp $"); #endif /* LIBC_SCCS and not lint */ #include @@ -134,9 +134,7 @@ pw_lock(int retries) } int -pw_mkdb(username, secureonly) - const char *username; - int secureonly; +pw_mkdb(const char *username, int secureonly) { const char *args[9]; int pstat, i; @@ -172,7 +170,7 @@ pw_mkdb(username, secureonly) } if (WIFEXITED(pstat)) { if (WEXITSTATUS(pstat) != 0) { - warnx("pwd_mkdb exited with static %d", + warnx("pwd_mkdb exited with status %d", WEXITSTATUS(pstat)); return -1; } diff --git a/lib/libutil/pidfile.3 b/lib/libutil/pidfile.3 index 8ad13328b..b1ca92c44 100644 --- a/lib/libutil/pidfile.3 +++ b/lib/libutil/pidfile.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: pidfile.3,v 1.12 2010/05/05 22:05:31 wiz Exp $ +.\" $NetBSD: pidfile.3,v 1.13 2011/03/29 13:55:37 jmmv Exp $ .\" .\" Copyright (c) 1999 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -27,7 +27,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd May 4, 2010 +.Dd March 23, 2011 .Dt PIDFILE 3 .Os .Sh NAME @@ -38,34 +38,48 @@ .Sh SYNOPSIS .In util.h .Ft int -.Fn pidfile "const char *basename" +.Fn pidfile "const char *path" .Sh DESCRIPTION .Fn pidfile -writes a file containing the process ID of the program to the +creates a file containing the process ID of the caller program. +The pid file can be used as a quick reference if +the process needs to be sent a signal. +When the program exits, the pid file is removed automatically, unless +the program receives a fatal signal. +.Pp +If +.Ar path +is +.Dv NULL +or a plain basename (a name containing no directory components), the pid file +is created in the .Pa /var/run directory. The file name has the form .Pa /var/run/basename.pid . -If the -.Ar basename -argument is +The basename part is either the value of +.Ar path +if it was not .Dv NULL , -.Fn pidfile -will determine the program name and use that instead. +or the program name as returned by +.Xr getprogname 3 +otherwise. .Pp -The pid file can be used as a quick reference if -the process needs to be sent a signal. -When the program exits, the pid file will be removed automatically, unless -the program receives a fatal signal. +If +.Ar path +is an absolute or relative path (i.e. it contains the +.Sq / +character), +the pid file is created in the provided location. .Pp Note that only the first invocation of .Fn pidfile causes a pid file to be written; subsequent invocations have no effect unless a new -.Ar basename +.Ar path is supplied. If called with a new -.Ar basename , +.Ar path , .Fn pidfile will remove the old pid file and write the new one. .Sh RETURN VALUES @@ -78,11 +92,13 @@ The .Fn pidfile function call appeared in .Nx 1.5 . +Support for creating pid files in any arbitrary path was added in +.Nx 6.0 . .Sh BUGS .Fn pidfile uses .Xr atexit 3 -to ensure the pidfile is unlinked at program exit. +to ensure the pid file is unlinked at program exit. However, programs that use the .Xr _exit 2 function (for example, in signal handlers) diff --git a/lib/libutil/pidfile.c b/lib/libutil/pidfile.c index 90a1bdc69..f66fd683d 100644 --- a/lib/libutil/pidfile.c +++ b/lib/libutil/pidfile.c @@ -1,11 +1,11 @@ -/* $NetBSD: pidfile.c,v 1.8 2008/04/28 20:23:03 martin Exp $ */ +/* $NetBSD: pidfile.c,v 1.9 2011/03/29 13:55:37 jmmv Exp $ */ /*- * Copyright (c) 1999 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation - * by Jason R. Thorpe and Matthias Scheler. + * by Jason R. Thorpe, Matthias Scheler and Julio Merino. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,90 +31,144 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: pidfile.c,v 1.8 2008/04/28 20:23:03 martin Exp $"); +__RCSID("$NetBSD: pidfile.c,v 1.9 2011/03/29 13:55:37 jmmv Exp $"); #endif #include + #include +#include #include #include #include #include #include -static int pidfile_atexit_done; static pid_t pidfile_pid; -static char *pidfile_basename; static char *pidfile_path; -static void pidfile_cleanup(void); - -int -pidfile(const char *basename) +/* Deletes an existent pidfile iff it was created by this process. */ +static void +pidfile_cleanup(void) { - FILE *f; - /* - * Register handler which will remove the pidfile later. - */ - if (!pidfile_atexit_done) { + if ((pidfile_path != NULL) && (pidfile_pid == getpid())) + (void) unlink(pidfile_path); +} + +/* Registers an atexit(3) handler to delete the pidfile we have generated. + * We only register the handler when we create a pidfile, so we can assume + * that the pidfile exists. + * + * Returns 0 on success or -1 if the handler could not be registered. */ +static int +register_atexit_handler(void) +{ + static bool done = false; + + if (!done) { if (atexit(pidfile_cleanup) < 0) return -1; - pidfile_atexit_done = 1; + done = true; } + return 0; +} + +/* Given a new pidfile name in 'path', deletes any previously-created pidfile + * if the previous file differs to the new one. + * + * If a previous file is deleted, returns 1, which means that a new pidfile + * must be created. Otherwise, this returns 0, which means that the existing + * file does not need to be touched. */ +static int +cleanup_old_pidfile(const char* path) +{ + if (pidfile_path != NULL) { + if (strcmp(pidfile_path, path) != 0) { + pidfile_cleanup(); + + free(pidfile_path); + pidfile_path = NULL; + + return 1; + } else + return 0; + } else + return 1; +} + +/* Constructs a name for a pidfile in the default location (/var/run). If + * 'basename' is NULL, uses the name of the current program for the name of + * the pidfile. + * + * Returns a pointer to a dynamically-allocatd string containing the absolute + * path to the pidfile; NULL on failure. */ +static char * +generate_varrun_path(const char *basename) +{ + char *path; + if (basename == NULL) basename = getprogname(); - /* - * If pidfile has already been created for the supplied basename - * we don't need to create a pidfile again. - */ - if (pidfile_path != NULL) { - if (strcmp(pidfile_basename, basename) == 0) - return 0; - /* - * Remove existing pidfile if it was created by this process. - */ - pidfile_cleanup(); + /* _PATH_VARRUN includes trailing / */ + (void) asprintf(&path, "%s%s.pid", _PATH_VARRUN, basename); + return path; +} +/* Creates a pidfile with the provided name. The new pidfile is "registered" + * in the global variables pidfile_path and pidfile_pid so that any further + * call to pidfile(3) can check if we are recreating the same file or a new + * one. + * + * Returns 0 on success or -1 if there is any error. */ +static int +create_pidfile(const char* path) +{ + FILE *f; + + if (register_atexit_handler() == -1) + return -1; + + if (cleanup_old_pidfile(path) == 0) + return 0; + + pidfile_path = strdup(path); + if (pidfile_path == NULL) + return -1; + + if ((f = fopen(path, "w")) == NULL) { free(pidfile_path); pidfile_path = NULL; - free(pidfile_basename); - pidfile_basename = NULL; + return -1; } pidfile_pid = getpid(); - pidfile_basename = strdup(basename); - if (pidfile_basename == NULL) - return -1; - - /* _PATH_VARRUN includes trailing / */ - (void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename); - if (pidfile_path == NULL) { - free(pidfile_basename); - pidfile_basename = NULL; - return -1; - } - - if ((f = fopen(pidfile_path, "w")) == NULL) { - free(pidfile_path); - pidfile_path = NULL; - free(pidfile_basename); - pidfile_basename = NULL; - return -1; - } - (void) fprintf(f, "%d\n", pidfile_pid); (void) fclose(f); + return 0; } -static void -pidfile_cleanup(void) +int +pidfile(const char *path) { - /* Only remove the pidfile if it was created by this process. */ - if ((pidfile_path != NULL) && (pidfile_pid == getpid())) - (void) unlink(pidfile_path); + + if (path == NULL || strchr(path, '/') == NULL) { + char *default_path; + + if ((default_path = generate_varrun_path(path)) == NULL) + return -1; + + if (create_pidfile(default_path) == -1) { + free(default_path); + return -1; + } + + free(default_path); + return 0; + } else + return create_pidfile(path); } diff --git a/lib/libutil/pidlock.c b/lib/libutil/pidlock.c index 62e67ed74..adef5990e 100644 --- a/lib/libutil/pidlock.c +++ b/lib/libutil/pidlock.c @@ -1,4 +1,4 @@ -/* $NetBSD: pidlock.c,v 1.15 2009/01/18 12:13:04 lukem Exp $ */ +/* $NetBSD: pidlock.c,v 1.16 2012/04/07 16:17:17 christos Exp $ */ /* * Copyright 1996, 1997 by Curt Sampson . @@ -24,7 +24,7 @@ #include #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: pidlock.c,v 1.15 2009/01/18 12:13:04 lukem Exp $"); +__RCSID("$NetBSD: pidlock.c,v 1.16 2012/04/07 16:17:17 christos Exp $"); #endif /* LIBC_SCCS and not lint */ #include @@ -52,8 +52,8 @@ pidlock(const char *lockfile, int flags, pid_t *locker, const char *info) char hostname[MAXHOSTNAMELEN + 1]; pid_t pid2 = -1; struct stat st; - int err; - int f = -1; + ssize_t n; + int f = -1, savee; char s[256]; char *p; size_t len; @@ -114,16 +114,16 @@ lockfailed: goto out; /* Find out who has this lockfile. */ if ((f = open(lockfile, O_RDONLY, 0)) != -1) { - if ((err = read(f, s, (size_t)11)) == -1) + if ((n = read(f, s, (size_t)11)) == -1) goto out; - if (err == 0) { + if (n == 0) { errno = EINVAL; goto out; } pid2 = atoi(s); - if ((err = read(f, s, sizeof(s) - 2)) == -1) + if ((n = read(f, s, sizeof(s) - 2)) == -1) goto out; - if (err == 0) + if (n == 0) *s = '\0'; s[sizeof(s) - 1] = '\0'; if ((p = strchr(s, '\n')) != NULL) @@ -164,11 +164,11 @@ lockfailed: errno = 0; return 0; out: - err = errno; + savee = errno; if (f != -1) (void)close(f); (void)unlink(tempfile); - errno = err; + errno = savee; return -1; } diff --git a/lib/libutil/proc_compare.3 b/lib/libutil/proc_compare.3 new file mode 100644 index 000000000..c845502d7 --- /dev/null +++ b/lib/libutil/proc_compare.3 @@ -0,0 +1,107 @@ +.\" $NetBSD: proc_compare.3,v 1.2 2011/10/21 12:58:53 wiz Exp $ +.\" +.\" Copyright (c) 2011 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Christos Zoulas. +.\" +.\" 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 FOUNDATION 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. +.\" +.\" +.Dd October 20, 2011 +.Dt PROC_COMPARE 3 +.Os +.Sh NAME +.Nm proc_compare +.Nd compare two processes' interactivity +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In sys/sysctl.h +.Ft int +.Fn "proc_compare" "const struct kinfo_proc2 *p1" "const struct kinfo_lwp *l1" \ +"const struct kinfo_proc2 *p2" "const struct kinfo_lwp *l2" +.Sh DESCRIPTION +The +.Fn proc_compare +function compares two processes that are on the same terminal for their +interactivity. +This means that the process returned is the one that has a better chance +being the active foreground process on that tty. +This algorithm is used in the kernel for +.Dv SIGINFO +reporting and in userland by +.Xr w 1 . +.Pp +The algorithm used is as follows: +.Bl -bullet -compact -offset indent +.It +If one of them is runnable, it is preferred. +.It +If both are runnable, the one with the largest CPU percent is preferred. +.It +In a CPU percent tie, the one started more recently wins. +.It +If none are runnable, and one of them is a zombie, the non-zombie is preferred +.It +If both are zombies, the one started more recently wins. +.It +If neither is a zombie, the one with the smaller sleep time wins. +.It +In a tie, and one is sleeping in non-interruptible sleep, prefer that one. +.It +If both are in the same state, the one started more recently is preferred. +.El +In all cases where the most recently started wins, if there was no winner, +the one with the largest PID wins. +.Sh RETURN VALUES +The +.Fn proc_compare +function returns +.Dv 0 +if +.Fa p1 +is to be preferred +and +.Dv 1 +if +.Fa p2 +is to be preferred. +.Sh SEE ALSO +.Xr w 1 +.Sh HISTORY +The +.Fn proc_compare +was extracted from +.Pa src/sys/kern/tty.c +and +.Pa src/usr.bin/w/proc_compare.c +and merged in +.Nx 6.0 . diff --git a/lib/libutil/pty.c b/lib/libutil/pty.c index dc1bee630..fbc170fab 100644 --- a/lib/libutil/pty.c +++ b/lib/libutil/pty.c @@ -77,7 +77,6 @@ openpty(int *amaster, int *aslave, char *name, struct termios *term, /* term may be NULL */ /* winp may be NULL */ -#ifndef __minix if ((master = open("/dev/ptm", O_RDWR)) != -1) { struct ptmget pt; if (ioctl(master, TIOCPTMGET, &pt) != -1) { @@ -89,7 +88,6 @@ openpty(int *amaster, int *aslave, char *name, struct termios *term, } (void)close(master); } -#endif (void)getgrnam_r("tty", &grs, grbuf, sizeof(grbuf), &grp); if (grp != NULL) { @@ -105,7 +103,7 @@ openpty(int *amaster, int *aslave, char *name, struct termios *term, for (cp = cp2 = TTY_OLD_SUFFIX TTY_NEW_SUFFIX; *cp2; cp2++) { line[5] = 'p'; line[9] = *cp2; - if ((master = open(line, O_RDWR | O_NOCTTY, 0)) == -1) { + if ((master = open(line, O_RDWR, 0)) == -1) { if (errno != ENOENT) continue; /* busy */ if ((size_t)(cp2 - cp + 1) < sizeof(TTY_OLD_SUFFIX)) @@ -117,13 +115,9 @@ openpty(int *amaster, int *aslave, char *name, struct termios *term, linep = line; if (chown(line, getuid(), ttygid) == 0 && chmod(line, mode) == 0 && -#ifndef __minix revoke(line) == 0 && -#endif - (slave = open(line, O_RDWR | O_NOCTTY, 0)) != -1) { -#ifndef __minix + (slave = open(line, O_RDWR, 0)) != -1) { gotit: -#endif *amaster = master; *aslave = slave; if (name) diff --git a/lib/libutil/raise_default_signal.3 b/lib/libutil/raise_default_signal.3 index 059a01e80..9b2c0a885 100644 --- a/lib/libutil/raise_default_signal.3 +++ b/lib/libutil/raise_default_signal.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: raise_default_signal.3,v 1.2 2008/04/30 13:10:52 martin Exp $ +.\" $NetBSD: raise_default_signal.3,v 1.3 2011/03/27 12:53:16 njoly Exp $ .\" .\" Copyright (c) 2007 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -60,7 +60,7 @@ The operations performed are: .Bl -enum -offset indent .It Block all signals, using -.Xr sigprocmask 3 . +.Xr sigprocmask 2 . .It Set the signal handler for signal .Fa sig diff --git a/lib/libutil/shlib_version b/lib/libutil/shlib_version index 97c9f92d6..3c8340119 100644 --- a/lib/libutil/shlib_version +++ b/lib/libutil/shlib_version @@ -1,2 +1,6 @@ +# $NetBSD: shlib_version,v 1.51 2012/04/07 16:44:39 christos Exp $ +# Remember to update distrib/sets/lists/base/shl.* when changing +# +# MINIX Keep lower than NETBSD until we are ABI compatible major=0 minor=0 diff --git a/lib/libutil/sockaddr_snprintf.c b/lib/libutil/sockaddr_snprintf.c index 4b0dabe5e..29a7c061e 100644 --- a/lib/libutil/sockaddr_snprintf.c +++ b/lib/libutil/sockaddr_snprintf.c @@ -38,6 +38,10 @@ __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.9 2008/04/28 20:23:03 martin Exp $"); #include #include +#ifndef __minix +#include +#include +#endif #include #include @@ -115,7 +119,11 @@ sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt, if (addr == abuf) name = addr; +#ifndef __minix + if (a && getnameinfo(sa, (socklen_t)sa->sa_len, addr = abuf, +#else if (a && getnameinfo(sa, (socklen_t)len, addr = abuf, +#endif (unsigned int)sizeof(abuf), NULL, 0, NI_NUMERICHOST|NI_NUMERICSERV) != 0) return -1; @@ -145,7 +153,11 @@ sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt, ADDS(nbuf); break; case 'l': +#ifndef __minix + (void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len); +#else (void)snprintf(nbuf, sizeof(nbuf), "%d", len); +#endif ADDS(nbuf); break; case 'A': @@ -154,7 +166,11 @@ sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt, else if (!a) ADDNA(); else { +#ifndef __minix + getnameinfo(sa, (socklen_t)sa->sa_len, +#else getnameinfo(sa, (socklen_t)len, +#endif name = Abuf, (unsigned int)sizeof(nbuf), NULL, 0, 0); ADDS(name); @@ -166,7 +182,11 @@ sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt, else if (p == -1) ADDNA(); else { +#ifndef __minix + getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0, +#else getnameinfo(sa, (socklen_t)len, NULL, 0, +#endif port = pbuf, (unsigned int)sizeof(pbuf), 0); ADDS(port); diff --git a/lib/libutil/stat_flags.3 b/lib/libutil/stat_flags.3 index 87b6407f6..c765e7e22 100644 --- a/lib/libutil/stat_flags.3 +++ b/lib/libutil/stat_flags.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: stat_flags.3,v 1.6 2010/05/04 06:53:35 jruoho Exp $ +.\" $NetBSD: stat_flags.3,v 1.7 2011/08/06 11:28:24 jruoho Exp $ .\" .\" Copyright (c) 1996 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -27,7 +27,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd May 4, 2010 +.Dd August 6, 2011 .Dt STAT_FLAGS 3 .Os .Sh NAME @@ -122,4 +122,5 @@ if it fails to parse the string, setting .Fa stringp to point to the first string that it failed to parse. .Sh SEE ALSO +.Xr chflags 2 , .Xr stat 2 diff --git a/lib/libutil/strpct.3 b/lib/libutil/strpct.3 new file mode 100644 index 000000000..eaf4ab4e0 --- /dev/null +++ b/lib/libutil/strpct.3 @@ -0,0 +1,99 @@ +.\" $NetBSD: strpct.3,v 1.4 2012/01/07 18:40:55 christos Exp $ +.\" +.\" Copyright (c) 2011 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This file was contributed to The NetBSD Foundation by Christos Zoulas. +.\" +.\" 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 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 FOUNDATION 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. +.\" +.Dd January 7, 2012 +.Dt STRPCT 3 +.Os +.Sh NAME +.Nm strpct , +.Nm strspct +.Nd decimal percent formatters +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In util.h +.Ft char * +.Fn strpct "char *buf" "size_t bufsiz" "uintmax_t numerator" "uintmax_t denominator" "size_t precision" +.Ft char * +.Fn strspct "char *buf" "size_t bufsiz" "intmax_t numerator" "intmax_t denominator" "size_t precision" +.Sh DESCRIPTION +The +.Fn strpct +function formats the fraction represented by +.Fa numerator +and +.Fa denominator +into a percentage representation with given number of digits of +.Fa precision +without using floating point arithmetic. +.Sh RETURN VALUES +.Fn strpct +and +.Fn strspct +always return a pointer to a NUL-terminated (unless +.Fa buflen +is +.Dv 0 ) +formatted string which +is placed in +.Fa buf +and is up to +.Fa buflen +characters. +If there was an overflow, the formatted string will reflect that precision +loss. +.Sh EXAMPLES +.Bd -literal -offset indent +strpct(buf, buflen, 1, 16, 3); +\(rA "6.250" +strpct(buf, buflen, 1, 2, 0); +\(rA "50" +.Ed +.Sh HISTORY +.Fn strpct +was originally implemented in +.Xr csh 1 +for +.Nx 1.3 . +It printed into a static buffer, was not locale aware, handled +.Ft unsigned long +numbers, and printed a +.Dq % +at the end of the number. +Other programs such as +.Xr df 1 +and +.Xr time 1 +started using it. +.Fn strpct +and +.Fn strspct +appeared separately in libutil for +.Nx 6.0 . +.Sh AUTHORS +.An Erik E. Fair Aq fair@netbsd.org diff --git a/lib/libutil/strpct.c b/lib/libutil/strpct.c new file mode 100644 index 000000000..b13806e18 --- /dev/null +++ b/lib/libutil/strpct.c @@ -0,0 +1,127 @@ +/* $NetBSD: strpct.c,v 1.3 2012/01/07 18:40:56 christos Exp $ */ + +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Erik E. Fair + * + * 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 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 FOUNDATION 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. + */ + +/* + * Calculate a percentage without resorting to floating point + * and return a pointer to a string + * + * "digits" is the number of digits past the decimal place you want + * (zero being the straight percentage with no decimals) + * + * Erik E. Fair , May 8, 1997 + */ + +#include +__RCSID("$NetBSD: strpct.c,v 1.3 2012/01/07 18:40:56 christos Exp $"); + +#include +#include +#include +#include +#include +#include + +char * +strspct(char *buf, size_t bufsiz, intmax_t numerator, intmax_t denominator, + size_t digits) +{ + int sign; + + switch (bufsiz) { + case 1: + *buf = '\0'; + /*FALLTHROUGH*/ + case 0: + return buf; + default: + break; + } + + if (denominator < 0) { + denominator = -denominator; + sign = 1; + } else + sign = 0; + + if (numerator < 0) { + numerator = -numerator; + sign++; + } + + sign &= 1; + (void)strpct(buf + sign, bufsiz - sign, (uintmax_t)numerator, + (uintmax_t)denominator, digits); + if (sign) + *buf = '-'; + return buf; +} + +char * +strpct(char *buf, size_t bufsiz, uintmax_t numerator, uintmax_t denominator, + size_t digits) +{ + uintmax_t factor, result; + size_t u; + + factor = 100; + for (u = 0; u < digits; u++) { + /* watch out for overflow! */ + if (factor < (UINTMAX_MAX / 10)) + factor *= 10; + else + break; + } + + /* watch out for overflow! */ + if (numerator < (UINTMAX_MAX / factor)) + numerator *= factor; + else { + /* toss some of the bits of lesser significance */ + denominator /= factor; + } + + if (denominator == 0) + denominator = 1; + + result = numerator / denominator; + + if (digits == 0) + (void)snprintf(buf, bufsiz, "%ju", result); + else { + factor /= 100; /* undo initialization */ + + (void)snprintf(buf, bufsiz, "%ju%s%0*ju", + result / factor, localeconv()->decimal_point, (int)u, + result % factor); + } + + return buf; +} diff --git a/lib/libutil/util.3 b/lib/libutil/util.3 index d55b0cfa1..6e1d76fb6 100644 --- a/lib/libutil/util.3 +++ b/lib/libutil/util.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: util.3,v 1.21 2010/05/05 06:11:14 jruoho Exp $ +.\" $NetBSD: util.3,v 1.24 2011/08/29 12:39:50 jruoho Exp $ .\" .\" Copyright (c) 2001 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -27,11 +27,12 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd May 5, 2010 +.Dd August 29, 2011 .Dt UTIL 3 .Os .Sh NAME -.Nm util +.Nm util , +.Nm libutil .Nd system utilities library .Sh LIBRARY .Lb libutil @@ -101,6 +102,7 @@ directory. .It Xr setusercontext 3 Ta query login.conf database about a user class .It Xr snprintb 3 Ta bitmask output conversion .It Xr sockaddr_snprintf 3 Ta socket address formatting function +.It Xr strpct 3 Ta decimal percent formatter .It Xr ttyaction 3 Ta ttyaction utility function .It Xr ttylock 3 Ta locks based on files containing PIDs .It Xr ttymsg 3 Ta ttymsg utility function