summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.bin/sup/src/Makefile.sunos5.gcc131
-rw-r--r--usr.bin/sup/src/scm.c72
-rw-r--r--usr.bin/sup/src/supfilesrv.c33
-rw-r--r--usr.bin/sup/src/unvis.c255
-rw-r--r--usr.bin/sup/src/vis.c187
-rw-r--r--usr.bin/sup/src/vis.h87
-rw-r--r--usr.bin/sup/src/vprintf.c15
7 files changed, 763 insertions, 17 deletions
diff --git a/usr.bin/sup/src/Makefile.sunos5.gcc b/usr.bin/sup/src/Makefile.sunos5.gcc
new file mode 100644
index 00000000000..431641185b3
--- /dev/null
+++ b/usr.bin/sup/src/Makefile.sunos5.gcc
@@ -0,0 +1,131 @@
+# $OpenBSD: Makefile.sunos5.gcc,v 1.1 1997/10/11 23:34:20 beck Exp $
+
+# Copyright (c) 1992,1991 Carnegie Mellon University
+# All Rights Reserved.
+#
+# Permission to use, copy, modify and distribute this software and its
+# documentation is hereby granted, provided that both the copyright
+# notice and this permission notice appear in all copies of the
+# software, derivative works or modified versions, and any portions
+# thereof, and that both notices appear in supporting documentation.
+#
+# CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
+# CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
+# ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
+#
+# Carnegie Mellon requests users of this software to return to
+#
+# Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
+# School of Computer Science
+# Carnegie Mellon University
+# Pittsburgh PA 15213-3890
+#
+# any improvements or extensions that they make and grant Carnegie the rights
+# to redistribute these changes.
+######################################################################
+# Makefile to build sup (the client side), supfilesrv (the repository
+# side, and supscan (used by the repository. If you only want to sup
+# files from CMU, just build sup.
+# The header files: c.h, libc.h and sysent.h are only
+# necessary if you are compiling on a non-Mach system. Likewise the
+# files in libextra.a are normally found in libcs.a on a Mach system.
+# DOPRINT_VA is used by vprintf.c and should be defined if your version
+# of libc/doprnt.c defines the routine _doprnt_va. If it defines _doprnt
+# instead, leave DORPINT_VA undefined.
+######################################################################
+#
+# If you need to build a sup for export outside of North America use
+# "make EXPORTABLE_SYSTEM=true"
+# this will remove (not use) any vestiges of crypt code that is present
+# on the system.
+#
+# If you have crypt/crypt.c and /usr/lib/libcrypt.a, you will be building
+# a system that uses the SUP crypt mechanism by default.
+#
+SITE = NETBSD
+#SITE = CMUCS
+NETBSD_DEFINES = -UMACH -DVAR_TMP -DHAS_DAEMON -DHAS_POSIX_DIR
+SOLARIS_DEFINES = -UMACH -DNO_SETLOGIN -DVAR_TMP -DHAS_POSIX_DIR -DNEED_VSNPRINTF -DHAS_VFPRINTF -DNEED_SNPRINTF -DNEED_VIS -DUSE_LOCKF
+AFS_DEFINES = -DAFS -I/usr/afsws/include
+OSF_DEFINES = -UMACH -DOSF -D_BSD -noshrlib -g -DNEED_VSNPRINTF -DVAR_TMP
+CMUCS_DEFINES = -DMACH -DDOPRINT_VA -DNEED_VPRINTF
+NON_MACH_DEFINES = -UMACH
+#DEFS = -UCMUCS -UCMU ${${SITE}_DEFINES}
+DEFS = -UCMUCS -UCMU ${SOLARIS_DEFINES}
+#DEFS = -UCMUCS -UCMU ${NETBSD_DEFINES}
+
+#INSTALLATION PARAMETERS
+NETBSD_BINDIR = /usr/OpenBSD/sbin
+NETBSD_MAN1 = /usr/OpenBSD/man/man1
+NETBSD_MAN8 = /usr/OpenBSD/man/man8
+
+CFLAGS = ${DEFS} -O -g -I.
+
+SUPCL = supcmain.o supcvers.o supcparse.o supcname.o \
+ supcmisc.o supcmeat.o
+SUPS = scm.o scmio.o stree.o log.o supmsg.o netcrypt.o
+EXTRA = atoo.o errmsg.o expand.o ffilecopy.o filecopy.o nxtarg.o \
+ path.o quit.o run.o salloc.o skipto.o vprintf.o snprintf.o vis.o unvis.o
+
+
+PROGRAMS = sup supscan supfilesrv
+MAN1 = sup.1
+MAN8 = supservers.8
+
+AFS_LIBPATH = /usr/afs/lib
+AFS_LIBS = -L${AFS_LIBPATH}/afs -lkauth -lprot -L${AFS_LIBPATH} -lubik -lauth -lrxkad -lsys -ldes -lrx -llwp -lcmd -lcom_err -lc ${AFS_LIBPATH}/afs/util.a
+
+
+NETBSD_LIBS = -lcrypt -lutil
+SOLARIS_LIBS = -lcrypt -lsocket -lnsl
+CMUCS_LIBS = -lsys
+OSF_LIBS = -lbsd
+LIBS = libextra.a
+sup_OFILES = ${SUPCL} ${SUPS}
+supfilesrv_OFILES = supfilesrv.o scan.o ${SUPS}
+supfilesrv_LIBS = libextra.a
+supscan_OFILES = supscan.o stree.o scan.o
+
+#OS_LIBS = ${NETBSD_LIBS}
+OS_LIBS = ${SOLARIS_LIBS}
+
+
+all: ${PROGRAMS}
+
+sup: ${sup_OFILES} ${LIBS}
+ ${CC} ${CFLAGS} -o sup ${sup_OFILES} ${LIBS} ${OS_LIBS}
+
+supfilesrv: ${supfilesrv_OFILES} ${supfilesrv_LIBS}
+ ${CC} ${CFLAGS} -o supfilesrv ${supfilesrv_OFILES} ${supfilesrv_LIBS} ${OS_LIBS}
+
+supscan: ${supscan_OFILES} ${LIBS}
+ ${CC} ${CFLAGS} -o supscan ${supscan_OFILES} ${LIBS} ${OS_LIBS}
+
+libextra.a: ${EXTRA}
+ ar r libextra.a $?
+ ranlib libextra.a
+
+clean cleandir:
+ rm -f ${PROGRAMS} libextra.a netcrypt.c *.o core a.out
+
+install: ${PROGRAMS}
+ install -cs -m 555 -o bin -g bin ${PROGRAMS} ${NETBSD_BINDIR}
+ install -c -m 444 -o bin -g bin ${MAN1} ${NETBSD_MAN1}
+ install -c -m 444 -o bin -g bin ${MAN8} ${NETBSD_MAN8}
+
+netcrypt.c: crypt.diffs
+
+scan.o: sup.h
+scm.o: sup.h
+scmio.o: sup.h supmsg.h
+stree.o: sup.h
+supcmain.o: sup.h supmsg.h supcdefs.h
+supcmeat.o: sup.h supmsg.h supcdefs.h
+supcmisc.o: sup.h supmsg.h supcdefs.h
+supcname.o: sup.h supmsg.h supcdefs.h
+supcparse.o: sup.h supmsg.h supcdefs.h
+supfilesrv.o: sup.h supmsg.h
+supmsg.o: sup.h supmsg.h
+supscan.o: sup.h
+netcryptvoid.o: sup.h supmsg.h
+netcrypt.o: sup.h supmsg.h
diff --git a/usr.bin/sup/src/scm.c b/usr.bin/sup/src/scm.c
index f5349b48643..7f1cfb9f9f1 100644
--- a/usr.bin/sup/src/scm.c
+++ b/usr.bin/sup/src/scm.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: scm.c,v 1.6 1997/09/16 11:01:17 deraadt Exp $ */
+/* $OpenBSD: scm.c,v 1.7 1997/10/11 23:34:20 beck Exp $ */
/*
* Copyright (c) 1992 Carnegie Mellon University
@@ -159,6 +159,7 @@
#include <arpa/inet.h>
#include <net/if.h>
#include <netdb.h>
+#include <syslog.h>
#ifdef __STDC__
#include <stdarg.h>
#else
@@ -208,6 +209,75 @@ static char *myhost __P((void));
*** F O R S E R V E R ***
***************************************************/
+int lock_host_file(char *lockdir) {
+
+ /* mark that we are servicing a connection from host at ia. we do
+ * this by getting an exclusive lock on a file that is "ia" in
+ * dotted decimal, in directory "lockdir". For convienence's sake we
+ * write our process ID in there. Should be called from servicing
+ * child, so lock goes away on exit. must be called after service()
+ * sets global remoteaddr (above).
+ *
+ * returns fd for file on success,-1 on failure.
+ */
+
+ char *dd, *lpath;
+ int i, fd;
+ FILE *f;
+
+ dd=strdup(inet_ntoa(remoteaddr));
+ if (dd == NULL) {
+ syslog(LOG_ERR, "Malloc failed in lock_host_file()");
+ return(-1);
+ }
+ i=strlen(lockdir) + strlen(dd) + 2; /* null and maybe a / */
+ lpath=(char *)malloc(i * sizeof(char));
+ lpath[i-1]='\0';
+ if (lpath == NULL) {
+ syslog(LOG_ERR, "Malloc failed in lock_host_file()");
+ free(dd);
+ return(-1);
+ }
+ (void) strncpy(lpath, lockdir, i-1);
+ if (lpath[strlen(lpath) - 1] != '/') {
+ lpath[strlen(lpath) + 1] = '\0';
+ lpath[strlen(lpath)] = '/';
+ }
+ (void) strncat(lpath, dd, i - strlen(lpath));
+ if (lpath[i-1] != '\0') {
+ syslog(LOG_CRIT, "Buffer overrun in lock_host_file(). SHOULD NOT HAPPEN!");
+ abort();
+ }
+ free(dd);
+ if ((fd = open(lpath, O_CREAT | O_WRONLY, 0600)) < 0) {
+ syslog(LOG_ERR, "Couldn't open/create lock file %s!", lpath);
+ free(lpath);
+ return(-1);
+ }
+#ifdef USE_LOCKF
+ if (lockf(fd, F_TLOCK, 0) != 0)
+#else
+ if (flock(fd, LOCK_EX | LOCK_NB ) != 0)
+#endif
+ {
+ syslog(LOG_DEBUG, "Can't get lock on %s.", lpath);
+ free(lpath);
+ close(fd);
+ return(-1);
+ }
+ f=fdopen(fd,"w");
+ if (f == NULL) {
+ syslog(LOG_ERR, "Couldn't fopen fd %d for lock file %s!", fd, lpath);
+ free(lpath);
+ close(fd);
+ return(-1);
+ }
+ (void)fprintf(f,"%d\n", getpid());
+ fflush(f);
+ free(lpath);
+ return(fd);
+}
+
int
servicesetup (server) /* listen for clients */
char *server;
diff --git a/usr.bin/sup/src/supfilesrv.c b/usr.bin/sup/src/supfilesrv.c
index 785669c9823..7dafe68a2d9 100644
--- a/usr.bin/sup/src/supfilesrv.c
+++ b/usr.bin/sup/src/supfilesrv.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: supfilesrv.c,v 1.11 1997/09/16 11:01:23 deraadt Exp $ */
+/* $OpenBSD: supfilesrv.c,v 1.12 1997/10/11 23:34:21 beck Exp $ */
/*
* Copyright (c) 1992 Carnegie Mellon University
@@ -28,12 +28,13 @@
/*
* supfilesrv -- SUP File Server
*
- * Usage: supfilesrv [-l] [-P] [-N] [-R] [-S]
+ * Usage: supfilesrv [-l] [-P] [-N] [-R] [-S] [-O]
* -l "live" -- don't fork daemon
* -P "debug ports" -- use debugging network ports
* -N "debug network" -- print debugging messages for network i/o
* -R "RCS mode" -- if file is an rcs file, use co to get contents
* -S "Operate silently" -- Only print error messages
+ * -O "One Connection" -- Reject servicing multiple connections
*
**********************************************************************
* HISTORY
@@ -235,6 +236,7 @@ int progpid = -1; /* and process id */
jmp_buf sjbuf; /* jump location for network errors */
TREELIST *listTL; /* list of trees to upgrade */
+char *oneconnect = NULL; /* -O flag */
int silent; /* -S flag */
int live; /* -l flag */
int dbgportsq; /* -P flag */
@@ -319,9 +321,22 @@ char **argv;
init (argc,argv); /* process arguments */
-#ifdef HAS_DAEMON
if (!live) /* if not debugging, turn into daemon */
+#ifdef HAS_DAEMON
daemon(0, 0);
+#else
+ {
+ int r;
+ r=fork();
+ if (r>0) {
+ exit(0);
+ }
+ else if (r < 0) {
+ perror("fork:");
+ exit(-1);
+ }
+ setsid();
+ }
#endif
logopen ("supfile");
@@ -439,6 +454,12 @@ char **argv;
argv++;
maxchildren = atoi(argv[0]);
break;
+ case 'O':
+ if (--argc < 1)
+ quit (1,"Missing arg to -O\n");
+ argv++;
+ oneconnect = argv[0];
+ break;
case 'H':
if (--argc < 3)
quit (1,"Missing args to -H\n");
@@ -858,6 +879,10 @@ srvsetup ()
lockfd = x;
}
#endif
+ if (oneconnect != NULL
+ && (lock_host_file(oneconnect) < 0)) {
+ goaway("I'm still working on a previous request from your host.");
+ }
setupack = FSETUPOK;
x = msgsetupack ();
if (x != SCMOK) goaway ("Error sending setup reply to client");
@@ -1621,8 +1646,10 @@ int fileuid,filegid;
logerr ("setegid: %%m");
if (setgid (pwd->pw_gid) < 0)
logerr ("setgid: %%m");
+#ifndef NO_SETLOGIN
if (setlogin (pwd->pw_name) < 0)
logerr ("setlogin: %%m");
+#endif
if (seteuid (pwd->pw_uid) < 0)
logerr ("seteuid: %%m");
if (setuid (pwd->pw_uid) < 0)
diff --git a/usr.bin/sup/src/unvis.c b/usr.bin/sup/src/unvis.c
new file mode 100644
index 00000000000..e228983afc6
--- /dev/null
+++ b/usr.bin/sup/src/unvis.c
@@ -0,0 +1,255 @@
+/*-
+ * 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 rcsid[] = "$OpenBSD: unvis.c,v 1.1 1997/10/11 23:34:21 beck Exp $";
+#endif /* LIBC_SCCS and not lint */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <vis.h>
+
+#ifdef NEED_VIS
+/*
+ * 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
+#ifdef __STDC__
+unvis(char *cp, char c, int *astate, int flag)
+#else
+unvis(cp, c, astate, flag)
+ char *cp;
+ char c;
+ int *astate, flag;
+#endif
+{
+
+ 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 = (char) 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);
+}
+#endif /* NEED_VIS */
diff --git a/usr.bin/sup/src/vis.c b/usr.bin/sup/src/vis.c
new file mode 100644
index 00000000000..98cff6a5cb8
--- /dev/null
+++ b/usr.bin/sup/src/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 rcsid[] = "$OpenBSD: vis.c,v 1.1 1997/10/11 23:34:22 beck Exp $";
+#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')
+#ifdef NEED_VIS
+/*
+ * vis - visually encode characters
+ */
+char *
+vis(dst, c, flag, nextc)
+ register char *dst;
+ int c, nextc;
+ register int flag;
+{
+ if (((u_int)c <= UCHAR_MAX && isascii(c) && 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;
+#ifdef __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;
+{
+ register char 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);
+}
+#endif /* NEED_VIS */
diff --git a/usr.bin/sup/src/vis.h b/usr.bin/sup/src/vis.h
new file mode 100644
index 00000000000..aeff6b7c89c
--- /dev/null
+++ b/usr.bin/sup/src/vis.h
@@ -0,0 +1,87 @@
+/* $OpenBSD: vis.h,v 1.1 1997/10/11 23:34:22 beck Exp $ */
+/* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */
+
+/*-
+ * Copyright (c) 1990 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.
+ *
+ * @(#)vis.h 5.9 (Berkeley) 4/3/91
+ */
+
+#ifndef _VIS_H_
+#define _VIS_H_
+
+/*
+ * to select alternate encoding format
+ */
+#define VIS_OCTAL 0x01 /* use octal \ddd format */
+#define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropiate */
+
+/*
+ * to alter set of characters encoded (default is to encode all
+ * non-graphic except space, tab, and newline).
+ */
+#define VIS_SP 0x04 /* also encode space */
+#define VIS_TAB 0x08 /* also encode tab */
+#define VIS_NL 0x10 /* also encode newline */
+#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL)
+#define VIS_SAFE 0x20 /* only encode "unsafe" characters */
+
+/*
+ * other
+ */
+#define VIS_NOSLASH 0x40 /* inhibit printing '\' */
+
+/*
+ * unvis return codes
+ */
+#define UNVIS_VALID 1 /* character valid */
+#define UNVIS_VALIDPUSH 2 /* character valid, push back passed char */
+#define UNVIS_NOCHAR 3 /* valid sequence, no character produced */
+#define UNVIS_SYNBAD -1 /* unrecognized escape sequence */
+#define UNVIS_ERROR -2 /* decoder in unknown state (unrecoverable) */
+
+/*
+ * unvis flags
+ */
+#define UNVIS_END 1 /* no more characters */
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+char *vis __P((char *, int, int, int));
+int strvis __P((char *, const char *, int));
+int strvisx __P((char *, const char *, size_t, int));
+int strunvis __P((char *, const char *));
+int unvis __P((char *, char, int *, int));
+__END_DECLS
+
+#endif /* !_VIS_H_ */
diff --git a/usr.bin/sup/src/vprintf.c b/usr.bin/sup/src/vprintf.c
index dc04f46795c..0e500681c48 100644
--- a/usr.bin/sup/src/vprintf.c
+++ b/usr.bin/sup/src/vprintf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vprintf.c,v 1.4 1997/08/31 06:57:26 deraadt Exp $ */
+/* $OpenBSD: vprintf.c,v 1.5 1997/10/11 23:34:22 beck Exp $ */
/*
* Copyright (c) 1991 Carnegie Mellon University
@@ -95,27 +95,16 @@ vsnprintf(s, n, fmt, args)
va_list args;
{
FILE fakebuf;
-#ifdef HAS_VFPRINTF
- int ret;
-#endif
fakebuf._flag = STRFLAG;
- fakebuf._ptr = s;
+ fakebuf._base = fakebuf._ptr = s;
fakebuf._cnt = n-1;
fakebuf._file = -1;
-#ifdef HAS_VFPRINTF
- ret = vfprintf(&fakebuf, fmt, args);
-#else
_doprnt(fmt, args, &fakebuf);
fakebuf._cnt++;
-#endif
putc('\0', &fakebuf);
if (fakebuf._cnt<0)
fakebuf._cnt = 0;
-#ifdef HAS_VFPRINTF
- return(ret);
-#else
return (n-fakebuf._cnt-1);
-#endif
}
#endif /* NEED_VPRINTF || NEED_VSNPRINTF */