#include <sys/param.h>
#include <sys/filio.h>
#include <errno.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <pwd.h>
#define COMMANDER←PORT 9999
main(int argc, char **argv)
{
struct hostent *e;
struct sockaddr←in addr;
int s, i;
char buf[1000];
FILE *f;
struct in←addr ia;
enum { Waiting, Printing } state = Waiting;
char wd[MAXPATHLEN];
uid←t uid;
struct passwd *passwdPtr;
addr.sin←family = AF←INET;
addr.sin←port = htons(COMMANDER←PORT);
ia.s←addr = inet←addr("127.0.0.1");
memcpy(&addr.sin←addr, &ia, sizeof(ia));
s = socket(AF←INET, SOCK←STREAM, 0);
if (s < 0) {
perror("Making socket");
exit(3);
}
if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(s);
if (errno == ECONNREFUSED)
exit(2);
else
exit(3);
}
f = fdopen(s, "w");
uid = getuid();
passwdPtr = getpwuid(uid);
fprintf(f, "AssertUser %s;", passwdPtr->pw←name);
fprintf(f, "cd %s; echo -s -n \007; ", getwd(wd));
for (i = 1; i < argc; i++) {
if (i != 1)
fputc(' ', f);
fputs(argv[i], f);
}
fprintf(f, "; echo -s -n \"\\001 Ignore this line...\";; echo -s -n \007\n");
fflush(f);
shutdown(s, 1);
while (read(s, buf, 1)) {
long count;
ioctl(s, FIONREAD, &count);
read(s, buf + 1, count > sizeof(buf) - 1 ? sizeof(buf) - 1 : count);
for (i = 0; i <= count; i++) {
switch (state) {
case Waiting:
if (buf[i] == '\007')
state = Printing;
break;
case Printing:
if (buf[i] == '\007')
exit(1);
else if (buf[i] == '\001') /* success marker */
exit(0);
else {
putc(buf[i], stdout);
break;
}
}
}
fflush(stdout);
}
exit(3); /* Shouldn't get EOF */
}