[comp.windows.news] wy.c and pshexec.c

siegel@hc.DSPO.GOV (josh Siegel) (03/28/89)

The remote server will be built into the mini-NeWS... I will
post the pieces you need in the next day or two.

			--Josh Siegel

To compile:
	% cc -o wy wy.c
	% cc -o pshexec pshexec.c

to run:
	% cat /etc/termcap | wy wc > o
			(the bottom of 0 will have the word count)
	% pshexec popup.ps ls

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  popup.ps pshexec.c wy.c
# Wrapped by siegel@eggroll on Mon Mar 27 21:38:31 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'popup.ps' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'popup.ps'\"
else
echo shar: Extracting \"'popup.ps'\" \(171 characters\)
sed "s/^X//" >'popup.ps' <<'END_OF_FILE'
X% Example by Don Hopkins
X
X{ currentcursorlocation
X  [ ($@ $0 $1 $2 $3 $4 $5 $6 $7 $8 $9)
X    { currentfile 80 string readline not { exit } if } loop
X  ] popmsg pop }
Xexec
END_OF_FILE
if test 171 -ne `wc -c <'popup.ps'`; then
    echo shar: \"'popup.ps'\" unpacked with wrong size!
fi
# end of 'popup.ps'
fi
if test -f 'pshexec.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pshexec.c'\"
else
echo shar: Extracting \"'pshexec.c'\" \(2437 characters\)
sed "s/^X//" >'pshexec.c' <<'END_OF_FILE'
X
X/* 
X * Written by Josh Siegel
X * siegel@hc.dspo.gov
X *
X * Mon Mar 27, 1989
X *
X * Does ${0-9} substitution as well as $@
X *
X */
X
X
X#include <stdio.h>
X#include <ctype.h>
X#include <sys/param.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <sys/file.h>
X#include <sys/socket.h>
X#include <netinet/in.h>
X#include <netdb.h>
X
Xchar cwd[MAXPATHLEN];
X
Xmain(argc, argv)
X	int             argc;
X	char           *argv[];
X{
X	int             ns;
X
X
X	ns = open_news();
X	if (ns < 0) {
X		perror("phone");
X		exit(0);
X	}
X
X
X	dump_out(ns, argv[1],argv,argc);
X
X	dup2(ns, 1);
X	dup2(ns, 0);
X	execvp(argv[2], &argv[2]);
X}
Xdump_out(news, file,argv,argc)
X	int             news,argc;
X	char           *file;
X	char *argv[];
X{
X	struct stat     fbuf;
X	int             n, fd;
X	register char           *buf,*p,*p2;
X
X	getwd(cwd);
X
X	n = stat(file, &fbuf);	/* st_size */
X
X	if (n != 0) {
X		perror("stat");
X		exit(0);
X	}
X	buf = (char *) malloc(fbuf.st_size);
X
X	fd = open(file, O_RDONLY);
X	if (fd < 0) {
X		perror(file);
X		exit(0);
X	}
X	n=read(fd, buf, fbuf.st_size);
X	buf[n] = '\0';
X	close(fd);
X
X	p2 = p = buf;
X	while(1) {
X		while(*p2!='$' && *p2!='\0') 
X			p2++;
X		write(news, p, p2-p);
X		if(*p2=='\0')
X			break;
X		p2++;
X		if(*p2>='0' && *p2 <= '9') {
X			if(*p2-'0'+2 < argc)
X				write(news,argv[*p2-'0'+2],strlen(argv[*p2-'0'+2]));
X		}
X		else 
X			switch(*p2) {
X				case '@':
X					write(news,cwd,strlen(cwd));
X					break;
X				default:
X					write(news,p2-1,2);
X					break;
X			}
X		p2++;
X		p = p2;
X	}
X
X	free(buf);
X}
X/*
X	Phone:  Connects to a established port setup by accept().
X*/
Xopen_news()
X{
X	char           *p, *p2, *p3;
X	int             news;
X
X	p = (char *) getenv("NEWSSERVER");
X
X	if (p) {
X		p2 = (char *) index(p, '.') + 1;
X		p3 = (char *) index(p2, ';');
X		*p3++ = '\0';
X		news = phone(atoi(p2), p3);
X	} else
X		news = phone(2000, "localhost");
X
X	if (news < 0) {
X		perror("phone");
X		exit(0);
X	}
X	return (news);
X}
Xphone(port, host)
X	int             port;
X	char            host[];
X{
X
X	struct sockaddr_in sin;
X	struct servent *sp;
X	struct hostent *hp;
X	int             s, *p;
X	char            buff[255];
X
X	bzero((char *) &sin, sizeof(sin));
X
X	sin.sin_port = port;
X
X	hp = gethostbyname(host);
X
X	if (hp == NULL)
X		return (-1);
X
X	bcopy(hp->h_addr, (char *) &sin.sin_addr, hp->h_length);
X	sin.sin_family = hp->h_addrtype;
X	s = socket(AF_INET, SOCK_STREAM, 0);
X	if (s < 0)
X		return (-1);
X
X	if (connect(s, &sin, sizeof(sin)) < 0) {
X		close(s);
X		return (-1);
X	}
X	return (s);
X}
END_OF_FILE
if test 2437 -ne `wc -c <'pshexec.c'`; then
    echo shar: \"'pshexec.c'\" unpacked with wrong size!
fi
# end of 'pshexec.c'
fi
if test -f 'wy.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'wy.c'\"
else
echo shar: Extracting \"'wy.c'\" \(384 characters\)
sed "s/^X//" >'wy.c' <<'END_OF_FILE'
X/* 
X * Written by Josh Siegel
X * siegel@hc.dspo.gov
X *
X * Mon Mar 27, 1989
X */
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X	int fildes[2],n;
X	char buff[4096];
X
X	pipe(fildes);
X
X	if(fork()) {
X		close(fildes[0]);
X		while(n=read(0,buff,sizeof(buff))) {
X			write(1,buff,n);
X			write(fildes[1],buff,n);
X		}
X	} else {
X		close(fildes[1]);
X		dup2(fildes[0],0);
X		execvp(argv[1],&argv[1]);
X	}
X}
END_OF_FILE
if test 384 -ne `wc -c <'wy.c'`; then
    echo shar: \"'wy.c'\" unpacked with wrong size!
fi
# end of 'wy.c'
fi
echo shar: End of shell archive.
exit 0
-- 
Josh Siegel		(siegel@hc.dspo.gov)
I like using a C-47A "puff dragon" to go shooting beer cans with.