File: exec.c

package info (click to toggle)
tama 1.2.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 136 kB
  • ctags: 89
  • sloc: ansic: 1,078; sh: 113; makefile: 110; exp: 75
file content (330 lines) | stat: -rw-r--r-- 6,533 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
**	The Net Tamagotchi command interpreter.
**	ff, 1997.
*/

/* includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>

#include "tama.h"

extern int s;

/* returns -1 if command is to quit, 0 otherwise */
int exec(char *buf, char *arg, char *pass, char *name)
{
	char *ptr;
	int diff, knockoff;

	/* Check if the tamagotchi exists or not */
	if(strcmp(buf, "status")==0 || strcmp(buf, "see")==0 ||
	   strcmp(buf, "passwd")==0 || strcmp(buf, "chpass")==0 ||
	   strcmp(buf, "chname")==0 || strcmp(buf, "feed")==0 ||
	   strcmp(buf, "pet")==0 || strcmp(buf, "play")==0 ||
	   strcmp(buf, "kill")==0 || strcmp(buf, "rm")==0) {
		if(arg[0]==0 || strcmp(arg, name)==0)
			ptr = name;
		else
			ptr = arg;

		if(exist(ptr) < 0) {
			put("Sorry, no Tamagotchi by that name exists.\n");
			return 0;
		}
	}

	if(strcmp(buf, "quit")==0 || strcmp(buf, "exit")==0) return -1;
	else if(strcmp(buf, "help")==0 || strcmp(buf, "?")==0) {
		put(HELP);
		put(HELP_ARG);
	}
	else if(strcmp(buf, "about")==0 || strcmp(buf, "ver")==0)
		put(VER);
	else if(strcmp(buf, "status")==0 || strcmp(buf, "see")==0) {
		if(arg[0]==0 || strcmp(arg, name)==0)
			status(name, 0);
		else {
			if(exist(arg) < 0) {
				put("Sorry, no Tamagotchi by that name exists.\n");
				return 0;
			}

			if(pass == NULL) {
				put("Enter password for ");
				put(arg);
				put(": ");
				get(buf);
				pass = buf;
			}

			if(checkpass(arg, pass)<0) {
				printf("%s Incorrect password for %s\n", logtime(), arg);
				put("Password incorrect.\n");
				return 0;
			}

		/* Process if checking another Tamagotchi */
			status(arg, 1);
			
		}
	}
	else if(strcmp(buf, "passwd")==0 || strcmp(buf, "chpass")==0) {
		if(arg[0]==0 || strcmp(arg, name)==0) ptr = name;
		else {
			if(exist(arg) < 0) {
				put("Sorry, no Tamagotchi by that name exists.\n");
				return 0;
			}

			if(pass == NULL) {
				put("Enter password for ");
				put(arg);
				put(": ");
				get(buf);
				pass = buf;
			}

			if(checkpass(arg, pass)<0) {
				printf("%s Incorrect password for %s\n", logtime(), arg);
				put("Password incorrect.\n");
				return 0;
			}

			ptr = arg;
		}

		put("Enter new password for ");
		put(ptr);
		put(": ");

		get(buf);
		if(check(buf)<0) {
			put("That password is invalid.\n");
			put(STRINGRULE);
			return 0;
		}
		if(setpass(ptr, buf) < 0)
			put(NOACCESS);
		else put("Password changed.\n");	
	}
	else if(strcmp(buf, "chname")==0) {
		if(arg[0]==0 || strcmp(arg, name)==0) ptr = name;
		else {
			if(exist(arg) < 0) {
				put("Sorry, no Tamagotchi by that name exists.\n");
				return 0;
			}

			if(pass == NULL) {
				put("Enter password for ");
				put(arg);
				put(": ");
				get(buf);
				pass = buf;
			}

			if(checkpass(arg, pass)<0) {
				printf("%s Incorrect password for %s\n", logtime(), arg);
				put("Password incorrect.\n");
				return 0;
			}

			ptr = arg;
		}

		put("Enter new name for ");
		put(ptr);
		put(": ");

		get(buf);
		if(check(buf)<0) {
			put("That name is invalid.\n");
			put(STRINGRULE);
			return 0;
		}
		if(exist(buf)==0) {
			put("Sorry, that name is taken.\n");
			return 0;
		}
		if(setname(ptr, buf) < 0)
			put(NOACCESS);
		else {
			put("Changed ");
			put(ptr);
			put("'s name to ");
			put(buf);
			put(".\n");

			if(arg[0]==0) strncpy(name, buf, MAXNAME);

			printf("%s Changed %s's name to '%s'\n", logtime(), ptr, name);
		}
	}
	else if(strcmp(buf, "feed")==0) {
		if(arg[0]==0 || strcmp(arg, name)==0) ptr = name;
		else {
			if(exist(arg) < 0) {
				put("Sorry, no Tamagotchi by that name exists.\n");
				return 0;
			}

			if(pass == NULL) {
				put("Enter password for ");
				put(arg);
				put(": ");
				get(buf);
				pass = buf;
			}

			if(checkpass(arg, pass)<0) {
				printf("%s Incorrect password for %s\n", logtime(), arg);
				put("Password incorrect.\n");
				return 0;
			}

			diff=(time(NULL)-gettime(arg))/3600;
			if((time(NULL)-getpet(arg))/3600 < LONELYTIME)
				diff--;


			if((diff-HUNGERTIME)>0) {
				knockoff = (diff-HUNGERTIME) / HUNGERPOUND;
				if(setweight(arg, getweight(arg)-knockoff)<0) {
					put(NOACCESS);
					return 0;	/* paranoid */
				}
			}

			if(getweight(arg)<1) {
				put(DEAD);
				del(arg);
				return 0;
			}

			ptr = arg;
		}

		switch(feed(ptr)) {
		case -1: {
			put(NOACCESS);
			break;
			}
		case 1: {
			put("No, thank you. I'm not hungry.\n");
			break;
			}
		default:
			put("Thank you!! Your Tamagotchi loves you! :)\n");
		}
	}
	else if(strcmp(buf, "pet")==0 || strcmp(buf, "play")==0) {
		if(arg[0]==0 || strcmp(arg, name)==0) ptr = name;
		else {
			if(exist(arg) < 0) {
				put("Sorry, no Tamagotchi by that name exists.\n");
				return 0;
			}

			if(pass == NULL) {
				put("Enter password for ");
				put(arg);
				put(": ");
				get(buf);
				pass = buf;
			}

			if(checkpass(arg, pass)<0) {
				printf("%s Incorrect password for %s\n", logtime(), arg);
				put("Password incorrect.\n");
				return 0;
			}

			diff=(time(NULL)-gettime(arg))/3600;
			if((time(NULL)-getpet(arg))/3600 < LONELYTIME)
				diff--;


			if((diff-HUNGERTIME)>0) {
				knockoff = (diff-HUNGERTIME) / HUNGERPOUND;
				if(setweight(arg, getweight(arg)-knockoff)<0) {
					put(NOACCESS);
					return 0;	/* paranoid */
				}
			}

			if(getweight(arg)<1) {
				put(DEAD);
				del(arg);
				return 0;
			}

			ptr = arg;
		}
		if(pet(ptr) < 0)
			put(NOACCESS);
		else put("Your Tamagotchi giggles... it just loves attention! :)\n");
	}
	else if(strcmp(buf, "kill")==0 || strcmp(buf, "rm")==0) {
		if(arg[0]==0 || strcmp(arg, name)==0) ptr = name;
		else {
			if(exist(arg) < 0) {
				put("Sorry, no Tamagotchi by that name exists.\n");
				return 0;
			}

			if(pass == NULL) {
				put("Enter password for ");
				put(arg);
				put(": ");
				get(buf);
				pass = buf;
			}

			if(checkpass(arg, pass)<0) {
				printf("%s Incorrect password for %s\n", logtime(), arg);
				put("Password incorrect.\n");
				return 0;
			}

			ptr = arg;
		}
	
		put("Are you sure you want to kill ");
		put(ptr);
		put("? ");
		get(buf);

		if(buf[0]!='y' && buf[0]!='Y')
			put("Good choice.\n");

		else {
			put(DEAD);
			del(ptr);
		}

		if(strcmp(arg, name)==0) {
			close(s);
			exit(0);
		}
	}
	else if(strcmp(buf, "list")==0 || strcmp(buf, "ls")==0 || strcmp(buf, "who")==0)
		list();
	else if(strcmp(buf, "motd")==0) {
		if((diff=open(MOTD, O_RDONLY)) < 0) {
			put("Sorry, there is no MOTD today...\n");
			return 0;
		}
		
		putmotd(diff);
		return 0;
	}

	else put(NOCOMMAND);	

	return 0;
}