Annotation of dgamelaunch-openbsd/nethackstub.c, Revision 1.1.1.1
1.1 rubenllo 1: /* nethackstub.c
2: * Copyright (c) 2004 Jilles Tjoelker <jilles@stack.nl>
3: *
4: * This program is free software; you can redistribute it and/or modify
5: * it under the terms of the GNU General Public License as published by
6: * the Free Software Foundation; either version 2 of the License, or
7: * (at your option) any later version.
8: *
9: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17: */
18:
19: /*
20: * This program can be used instead of nethack to test dgamelaunch.
21: */
22:
23: static const char rcsid[] = "$Id: nethackstub.c,v 1.4 2004/01/06 13:33:36 jilles Exp $";
24:
25: #include <sys/types.h>
26:
27: #include <ctype.h>
28: #include <errno.h>
29: #include <fcntl.h>
30: #include <signal.h>
31: #include <stdlib.h>
32: #include <string.h>
33: #include <unistd.h>
34:
35: void
36: sighup(int sig)
37: {
38: (void)sig;
39: #define S "SIGHUP received.\n"
40: write(STDOUT_FILENO, S, -1 + sizeof S);
41: #undef S
42: }
43:
44: void
45: sigterm(int sig)
46: {
47: (void)sig;
48: #define S "SIGTERM received.\n"
49: write(STDOUT_FILENO, S, -1 + sizeof S);
50: #undef S
51: exit(1);
52: }
53:
54: void
55: checkmail()
56: {
57: char *mailfile;
58: char buf[256];
59: int in, n;
60: struct flock fl;
61:
62: mailfile = getenv("MAIL");
63: if (getenv("SIMPLEMAIL"))
64: {
65: in = open(mailfile, O_RDWR);
66: if (in != -1)
67: {
68: fl.l_start = 0;
69: fl.l_len = 0;
70: fl.l_pid = getpid();
71: fl.l_type = F_WRLCK;
72: fl.l_whence = SEEK_SET;
73: if (fcntl(in, F_SETLK, &fl) != -1)
74: {
75: while ((n = read(in, buf, sizeof buf)) > 0)
76: {
77: write(STDOUT_FILENO, buf, n);
78: }
79: #define S "End of mail - press return to unlock mailfile\n"
80: write(STDOUT_FILENO, S, -1 + sizeof S);
81: #undef S
82: read(STDIN_FILENO, buf, sizeof buf);
83: ftruncate(in, (off_t)0);
84: /* File will be unlocked automatically when closed */
85: }
86: else
87: {
88: #define S "Cannot lock mailfile\n"
89: write(STDOUT_FILENO, S, -1 + sizeof S);
90: #undef S
91: }
92: close(in);
93: }
94: else
95: {
96: #define S "Cannot open mailfile\n"
97: write(STDOUT_FILENO, S, -1 + sizeof S);
98: #undef S
99: }
100: }
101: else
102: {
103: #define S "No SIMPLEMAIL\n"
104: write(STDOUT_FILENO, S, -1 + sizeof S);
105: #undef S
106: }
107: }
108:
109: int
110: main(int argc, char *argv[])
111: {
112: char buf[256];
113: int showusage = 1, n, i;
114: struct sigaction SA;
115:
116: /* Clear the screen for the benefit of dgamelaunch's check. */
117: #define S " \033[H\033[Jnethackstub started with arguments:\n"
118: write(STDOUT_FILENO, S, -1 + sizeof S);
119: #undef S
120: for (i = 1; i < argc; i++)
121: {
122: write(STDOUT_FILENO, argv[i], strlen(argv[i]));
123: #define S "\n"
124: write(STDOUT_FILENO, S, -1 + sizeof S);
125: #undef S
126: }
127:
128: sigemptyset(&SA.sa_mask);
129: SA.sa_flags = 0;
130: SA.sa_handler = sighup;
131: sigaction(SIGHUP, &SA, NULL);
132: SA.sa_handler = sigterm;
133: sigaction(SIGTERM, &SA, NULL);
134:
135: for (;;)
136: {
137: if (showusage)
138: {
139: #define S "i: close stdin - o: close stdout/stderr - m: check mail - q: quit\n"
140: write(STDOUT_FILENO, S, -1 + sizeof S);
141: #undef S
142: }
143: n = read(STDIN_FILENO, buf, sizeof buf);
144: if (n == -1 && errno == EINTR)
145: continue;
146: if (n <= 0)
147: break;
148: for (i = 0; i < n; i++)
149: {
150: switch (tolower(buf[i]))
151: {
152: case 'i':
153: close(STDIN_FILENO);
154: break;
155: case 'o':
156: close(STDOUT_FILENO);
157: close(STDERR_FILENO);
158: break;
159: case 'm':
160: checkmail();
161: break;
162: case 'q':
163: return 0;
164: break;
165: case '\r':
166: case '\n':
167: case ' ':
168: break;
169: default:
170: showusage = 1;
171: }
172: }
173: }
174:
175: for (;;)
176: pause();
177:
178: return 0;
179: }
180:
181: /* vim:ts=8:cin:sw=4
182: */
CVSweb