From 65b3153ce0492f73b0b2b2d0efdf50bffd984e94 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 5 Nov 2003 23:29:37 +0000 Subject: [PATCH] Add the 'varsym' utility which may be used to manipulate system-wide and user-wide variables. Note that it is not quite complete, it can only set and retrieve specific variables and cannot yet list the variables. --- bin/Makefile | 5 ++- bin/varsym/Makefile | 8 ++++ bin/varsym/varsym.1 | 79 ++++++++++++++++++++++++++++++++ bin/varsym/varsym.c | 107 ++++++++++++++++++++++++++++++++++++++++++++ include/unistd.h | 4 +- 5 files changed, 200 insertions(+), 3 deletions(-) create mode 100644 bin/varsym/Makefile create mode 100644 bin/varsym/varsym.1 create mode 100644 bin/varsym/varsym.c diff --git a/bin/Makefile b/bin/Makefile index b512d0421e..a5ce42a0ec 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -1,6 +1,6 @@ # From: @(#)Makefile 8.1 (Berkeley) 5/31/93 # $FreeBSD: src/bin/Makefile,v 1.15.2.2 2000/10/31 09:43:57 dougb Exp $ -# $DragonFly: src/bin/Makefile,v 1.4 2003/07/28 06:37:19 hmp Exp $ +# $DragonFly: src/bin/Makefile,v 1.5 2003/11/05 23:29:33 dillon Exp $ SUBDIR= cat \ chio \ @@ -29,7 +29,8 @@ SUBDIR= cat \ sleep \ stty \ sync \ - test + test \ + varsym .if !defined(NO_TCSH) SUBDIR+=csh diff --git a/bin/varsym/Makefile b/bin/varsym/Makefile new file mode 100644 index 0000000000..3d7efac86e --- /dev/null +++ b/bin/varsym/Makefile @@ -0,0 +1,8 @@ +# $DragonFly: src/bin/varsym/Makefile,v 1.1 2003/11/05 23:29:37 dillon Exp $ + +PROG= varsym +MLINKS= varsym.1 + +WARNS?= 2 + +.include diff --git a/bin/varsym/varsym.1 b/bin/varsym/varsym.1 new file mode 100644 index 0000000000..12bc412dbc --- /dev/null +++ b/bin/varsym/varsym.1 @@ -0,0 +1,79 @@ +.\" Copyright (c) 2003 Matthew Dillon +.\" 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. +.\" +.\" $DragonFly: src/bin/varsym/varsym.1,v 1.1 2003/11/05 23:29:37 dillon Exp $ +.\" +.Dd Nov 5, 2003 +.Dt VARSYM 1 +.Os +.Sh NAME +.Nm varsym +.Nd get and set user and system-wide variables for variant symlinks +.Sh SYNOPSIS +.Nm +.Op Fl qdsup +.Ar var[=data] +.Sh DESCRIPTION +The +.Nm +program manages user and system-wide variables. These variables are typically +used by the system to resolve variant symlinks but may also be used generally. +.Pp +For each operand set, modify, retrieve, or delete the specified variable. +By default variables specified without data are retrieved and variables +specified with data are set. Variables may be set to empty. +.Bl -tag -width Ar +.It Fl q +Quiet mode. When retrieving a variable only its data is printed. +.It Fl d +Delete mode. The specified variables are deleted. Any specified data is +ignored. +.It Fl s +This option causes variables to be set system-wide and restricts retrievals +to system-specific variables. +.It Fl u +This option causes variables to be set on a per-user-id basis and restricts +retrievals to user-specific variables. This is the default. +.It Fl p +This option causes variables to be set on a per-process basis and restricts +retrievals to process-specific variables. It exists for completeness. Note +that since +.Nm +is run as its own process, using this option will not effect your shell's +namespace. +.El +.Sh RETURN VALUES +The +.Nm +utility exits with one of the following values: +.Bl -tag -width Ds +.It 0 +No errors occured. +.It 1 +A requested variable could not be found +.It 2 +A requested variable could not be set +.El +.Sh SEE ALSO +.Xr ln 1 , diff --git a/bin/varsym/varsym.c b/bin/varsym/varsym.c new file mode 100644 index 0000000000..4a580da978 --- /dev/null +++ b/bin/varsym/varsym.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2003 Matthew Dillon + * 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. + * + * $DragonFly: src/bin/varsym/varsym.c,v 1.1 2003/11/05 23:29:37 dillon Exp $ + */ + +#include +#include +#include +#include +#include +#include + +int +main(int ac, char **av) +{ + int i; + int mask = VARSYM_ALL_MASK; + int level = VARSYM_USER; + int nary = 0; + int mary = 8; + int deleteOpt = 0; + int verboseOpt = 1; + char **ary; + + ary = malloc(sizeof(char *) * mary); + for (i = 1; i < ac; ++i) { + char *ptr = av[i]; + if (*ptr != '-') { + ary[nary++] = ptr; + if (nary == mary) { + mary *= 2; + ary = realloc(ary, sizeof(char *) * mary); + } + continue; + } + ptr += 2; + switch(ptr[-1]) { + case 'q': + verboseOpt = 0; + break; + case 'd': + deleteOpt = 1; + break; + case 's': + mask = VARSYM_SYS_MASK; + level = VARSYM_SYS; + break; + case 'u': + mask = VARSYM_USER_MASK; + level = VARSYM_USER; + break; + case 'p': + mask = VARSYM_PROC_MASK; + level = VARSYM_PROC; + break; + } + } + for (i = 0; i < nary; ++i) { + char *name = ary[i]; + char *data = strchr(name, '='); + int error; + char buf[MAXVARSYM_DATA]; + + if (data) + *data++ = 0; + + if (deleteOpt) { + error = varsym_set(level, name, NULL); + } else if (data) { + error = varsym_set(level, name, data); + } else { + error = varsym_get(mask, name, buf, sizeof(buf)); + if (error >= 0 && error <= (int)sizeof(buf)) { + if (verboseOpt) + printf("%s=", name); + printf("%s\n", buf); + } + } + if (error < 0) + fprintf(stderr, "%s: %s\n", name, strerror(errno)); + } + return(0); +} + diff --git a/include/unistd.h b/include/unistd.h index 64f99dc11e..aedcbedc13 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -32,7 +32,7 @@ * * @(#)unistd.h 8.12 (Berkeley) 4/27/95 * $FreeBSD: src/include/unistd.h,v 1.35.2.10 2002/04/15 12:52:28 nectar Exp $ - * $DragonFly: src/include/unistd.h,v 1.2 2003/06/17 04:25:56 dillon Exp $ + * $DragonFly: src/include/unistd.h,v 1.3 2003/11/05 23:29:35 dillon Exp $ */ #ifndef _UNISTD_H_ @@ -222,6 +222,8 @@ int unwhiteout __P((const char *)); int usleep __P((unsigned int)); void *valloc __P((size_t)); /* obsoleted by malloc() */ pid_t vfork __P((void)); +int varsym_set(int level, const char *name, const char *data); +int varsym_get(int mask, const char *wild, char *buf, int maxbufsize); extern char *suboptarg; /* getsubopt(3) external variable */ int getsubopt __P((char **, char * const *, char **)); -- 2.28.0