2004-01-05 15:01:03 +01:00
|
|
|
/* nethackstub.c
|
|
|
|
* Copyright (c) 2004 Jilles Tjoelker <jilles@stack.nl>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program can be used instead of nethack to test dgamelaunch.
|
|
|
|
*/
|
|
|
|
|
2004-01-06 14:33:36 +01:00
|
|
|
static const char rcsid[] = "$Id: nethackstub.c,v 1.4 2004/01/06 13:33:36 jilles Exp $";
|
2004-01-05 15:01:03 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2004-01-06 14:33:36 +01:00
|
|
|
|
2004-01-05 15:01:03 +01:00
|
|
|
#include <ctype.h>
|
2004-01-06 14:33:36 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2004-01-05 15:01:03 +01:00
|
|
|
#include <signal.h>
|
2004-01-06 14:33:36 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-01-05 15:01:03 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
sighup(int sig)
|
|
|
|
{
|
|
|
|
(void)sig;
|
|
|
|
#define S "SIGHUP received.\n"
|
|
|
|
write(STDOUT_FILENO, S, -1 + sizeof S);
|
|
|
|
#undef S
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sigterm(int sig)
|
|
|
|
{
|
|
|
|
(void)sig;
|
|
|
|
#define S "SIGTERM received.\n"
|
|
|
|
write(STDOUT_FILENO, S, -1 + sizeof S);
|
|
|
|
#undef S
|
2004-03-04 15:31:21 +01:00
|
|
|
exit(1);
|
2004-01-05 15:01:03 +01:00
|
|
|
}
|
|
|
|
|
2004-01-06 14:33:36 +01:00
|
|
|
void
|
|
|
|
checkmail()
|
|
|
|
{
|
|
|
|
char *mailfile;
|
|
|
|
char buf[256];
|
|
|
|
int in, n;
|
|
|
|
struct flock fl;
|
|
|
|
|
|
|
|
mailfile = getenv("MAIL");
|
|
|
|
if (getenv("SIMPLEMAIL"))
|
|
|
|
{
|
|
|
|
in = open(mailfile, O_RDWR);
|
|
|
|
if (in != -1)
|
|
|
|
{
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = 0;
|
|
|
|
fl.l_pid = getpid();
|
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
if (fcntl(in, F_SETLK, &fl) != -1)
|
|
|
|
{
|
|
|
|
while ((n = read(in, buf, sizeof buf)) > 0)
|
|
|
|
{
|
|
|
|
write(STDOUT_FILENO, buf, n);
|
|
|
|
}
|
|
|
|
#define S "End of mail - press return to unlock mailfile\n"
|
|
|
|
write(STDOUT_FILENO, S, -1 + sizeof S);
|
|
|
|
#undef S
|
|
|
|
read(STDIN_FILENO, buf, sizeof buf);
|
|
|
|
ftruncate(in, (off_t)0);
|
|
|
|
/* File will be unlocked automatically when closed */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#define S "Cannot lock mailfile\n"
|
|
|
|
write(STDOUT_FILENO, S, -1 + sizeof S);
|
|
|
|
#undef S
|
|
|
|
}
|
|
|
|
close(in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#define S "Cannot open mailfile\n"
|
|
|
|
write(STDOUT_FILENO, S, -1 + sizeof S);
|
|
|
|
#undef S
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#define S "No SIMPLEMAIL\n"
|
|
|
|
write(STDOUT_FILENO, S, -1 + sizeof S);
|
|
|
|
#undef S
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-05 15:01:03 +01:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
int showusage = 1, n, i;
|
2004-01-05 17:10:30 +01:00
|
|
|
struct sigaction SA;
|
2004-01-05 15:01:03 +01:00
|
|
|
|
2004-01-06 14:33:36 +01:00
|
|
|
/* Clear the screen for the benefit of dgamelaunch's check. */
|
|
|
|
#define S " \033[H\033[Jnethackstub started with arguments:\n"
|
2004-01-05 15:01:03 +01:00
|
|
|
write(STDOUT_FILENO, S, -1 + sizeof S);
|
|
|
|
#undef S
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
write(STDOUT_FILENO, argv[i], strlen(argv[i]));
|
|
|
|
#define S "\n"
|
|
|
|
write(STDOUT_FILENO, S, -1 + sizeof S);
|
|
|
|
#undef S
|
|
|
|
}
|
|
|
|
|
2004-01-05 17:10:30 +01:00
|
|
|
sigemptyset(&SA.sa_mask);
|
|
|
|
SA.sa_flags = 0;
|
|
|
|
SA.sa_handler = sighup;
|
|
|
|
sigaction(SIGHUP, &SA, NULL);
|
|
|
|
SA.sa_handler = sigterm;
|
|
|
|
sigaction(SIGTERM, &SA, NULL);
|
2004-01-05 15:01:03 +01:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (showusage)
|
|
|
|
{
|
2004-01-06 14:33:36 +01:00
|
|
|
#define S "i: close stdin - o: close stdout/stderr - m: check mail - q: quit\n"
|
2004-01-05 15:01:03 +01:00
|
|
|
write(STDOUT_FILENO, S, -1 + sizeof S);
|
|
|
|
#undef S
|
|
|
|
}
|
|
|
|
n = read(STDIN_FILENO, buf, sizeof buf);
|
2004-01-06 14:33:36 +01:00
|
|
|
if (n == -1 && errno == EINTR)
|
|
|
|
continue;
|
2004-01-05 15:01:03 +01:00
|
|
|
if (n <= 0)
|
|
|
|
break;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
switch (tolower(buf[i]))
|
|
|
|
{
|
|
|
|
case 'i':
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
|
|
|
break;
|
2004-01-06 14:33:36 +01:00
|
|
|
case 'm':
|
|
|
|
checkmail();
|
|
|
|
break;
|
2004-01-05 15:01:03 +01:00
|
|
|
case 'q':
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
case ' ':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
showusage = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
pause();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:ts=8:cin:sw=4
|
|
|
|
*/
|