(mouse-wheel-scroll-amount): Revert previous change -
[emacs.git] / lib-src / update-game-score.c
blob7393135daa915dbe0b1e50fbc407a1ec0db45d73
1 /* update-game-score.c --- Update a score file
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This program is allows a game to securely and atomically update a
22 score file. It should be installed setuid, owned by an appropriate
23 user like `games'.
25 Alternatively, it can be compiled without HAVE_SHARED_GAME_DIR
26 defined, and in that case it will store scores in the user's home
27 directory (it should NOT be setuid).
29 Created 2002/03/22, by Colin Walters <walters@debian.org>
32 #include <config.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #include <errno.h>
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #include <stdio.h>
45 #include <time.h>
46 #include <pwd.h>
47 #include <ctype.h>
48 #ifdef HAVE_FCNTL_H
49 #include <fcntl.h>
50 #endif
51 #ifdef STDC_HEADERS
52 #include <stdarg.h>
53 #endif
54 #include <sys/stat.h>
56 /* Needed for SunOS4, for instance. */
57 extern char *optarg;
58 extern int optind, opterr;
60 #define MAX_ATTEMPTS 5
61 #define MAX_SCORES 200
62 #define MAX_DATA_LEN 1024
64 /* Declare the prototype for a general external function. */
65 #if defined (PROTOTYPES) || defined (WINDOWSNT)
66 #define P_(proto) proto
67 #else
68 #define P_(proto) ()
69 #endif
71 int
72 usage(err)
73 int err;
75 fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
76 fprintf(stdout, " update-game-score -h\n");
77 fprintf(stdout, " -h\t\tDisplay this help.\n");
78 fprintf(stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
79 fprintf(stdout, " -r\t\tSort the scores in increasing order.\n");
80 fprintf(stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
81 exit(err);
84 int
85 lock_file P_((const char *filename, void **state));
86 int
87 unlock_file P_((const char *filename, void *state));
89 struct score_entry
91 long score;
92 char *username;
93 char *data;
96 int
97 read_scores P_((const char *filename, struct score_entry **scores,
98 int *count));
99 int
100 push_score P_((struct score_entry **scores, int *count,
101 int newscore, char *username, char *newdata));
102 void
103 sort_scores P_((struct score_entry *scores, int count, int reverse));
105 write_scores P_((const char *filename, const struct score_entry *scores,
106 int count));
108 void lose P_((const char *msg)) NO_RETURN;
110 void lose(msg)
111 const char *msg;
113 fprintf(stderr, "%s\n", msg);
114 exit(1);
117 void lose_syserr P_((const char *msg)) NO_RETURN;
119 void lose_syserr(msg)
120 const char *msg;
122 fprintf(stderr, "%s: %s\n", msg, strerror(errno));
123 exit(1);
126 char *
127 get_user_id P_ ((void))
129 char *name;
130 struct passwd *buf = getpwuid(getuid());
131 if (!buf)
133 int count = 1;
134 int uid = (int) getuid();
135 int tuid = uid;
136 while (tuid /= 10)
137 count++;
138 name = malloc(count+1);
139 if (!name)
140 return NULL;
141 sprintf(name, "%d", uid);
142 return name;
144 return buf->pw_name;
147 char *
148 get_prefix(running_suid, user_prefix)
149 int running_suid;
150 char *user_prefix;
152 if (!running_suid && user_prefix == NULL)
153 lose("Not using a shared game directory, and no prefix given.");
154 if (running_suid)
156 #ifdef HAVE_SHARED_GAME_DIR
157 return HAVE_SHARED_GAME_DIR;
158 #else
159 lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
160 #endif
162 return user_prefix;
166 main(argc, argv)
167 int argc;
168 char **argv;
170 int c, running_suid;
171 void *lockstate;
172 char *user_id, *scorefile, *prefix, *user_prefix = NULL;
173 struct stat buf;
174 struct score_entry *scores;
175 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
176 char *newdata;
178 srand(time(0));
180 while ((c = getopt(argc, argv, "hrm:d:")) != -1)
181 switch (c)
183 case 'h':
184 usage(0);
185 break;
186 case 'd':
187 user_prefix = optarg;
188 break;
189 case 'r':
190 reverse = 1;
191 break;
192 case 'm':
193 max = atoi(optarg);
194 if (max > MAX_SCORES)
195 max = MAX_SCORES;
196 break;
197 default:
198 usage(1);
201 if (optind+3 != argc)
202 usage(1);
204 running_suid = (getuid() != geteuid());
206 prefix = get_prefix(running_suid, user_prefix);
208 scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2);
209 if (!scorefile)
210 lose_syserr("Couldn't allocate score file");
212 strcpy(scorefile, prefix);
213 strcat(scorefile, "/");
214 strcat(scorefile, argv[optind]);
215 newscore = atoi(argv[optind+1]);
216 newdata = argv[optind+2];
217 if (strlen(newdata) > MAX_DATA_LEN)
218 newdata[MAX_DATA_LEN] = '\0';
220 if ((user_id = get_user_id()) == NULL)
221 lose_syserr("Couldn't determine user id");
223 if (stat(scorefile, &buf) < 0)
224 lose_syserr("Failed to access scores file");
226 if (lock_file(scorefile, &lockstate) < 0)
227 lose_syserr("Failed to lock scores file");
229 if (read_scores(scorefile, &scores, &scorecount) < 0)
231 unlock_file(scorefile, lockstate);
232 lose_syserr("Failed to read scores file");
234 push_score(&scores, &scorecount, newscore, user_id, newdata);
235 /* Limit the number of scores. If we're using reverse sorting, then
236 we should increment the beginning of the array, to skip over the
237 *smallest* scores. Otherwise, we just decrement the number of
238 scores, since the smallest will be at the end. */
239 if (scorecount > MAX_SCORES)
240 scorecount -= (scorecount - MAX_SCORES);
241 if (reverse)
242 scores += (scorecount - MAX_SCORES);
243 sort_scores(scores, scorecount, reverse);
244 if (write_scores(scorefile, scores, scorecount) < 0)
246 unlock_file(scorefile, lockstate);
247 lose_syserr("Failed to write scores file");
249 unlock_file(scorefile, lockstate);
250 exit(0);
254 read_score(f, score)
255 FILE *f;
256 struct score_entry *score;
258 int c;
259 if (feof(f))
260 return 1;
261 while ((c = getc(f)) != EOF
262 && isdigit(c))
264 score->score *= 10;
265 score->score += (c-48);
267 while ((c = getc(f)) != EOF
268 && isspace(c))
270 if (c == EOF)
271 return -1;
272 ungetc(c, f);
273 #ifdef HAVE_GETDELIM
275 size_t count = 0;
276 if (getdelim(&score->username, &count, ' ', f) < 1
277 || score->username == NULL)
278 return -1;
279 /* Trim the space */
280 score->username[strlen(score->username)-1] = '\0';
282 #else
284 int unameread = 0;
285 int unamelen = 30;
286 char *username = malloc(unamelen);
287 if (!username)
288 return -1;
290 while ((c = getc(f)) != EOF
291 && !isspace(c))
293 if (unameread >= unamelen-1)
294 if (!(username = realloc(username, unamelen *= 2)))
295 return -1;
296 username[unameread] = c;
297 unameread++;
299 if (c == EOF)
300 return -1;
301 username[unameread] = '\0';
302 score->username = username;
304 #endif
305 #ifdef HAVE_GETLINE
306 score->data = NULL;
307 errno = 0;
309 size_t len;
310 if (getline(&score->data, &len, f) < 0)
311 return -1;
312 score->data[strlen(score->data)-1] = '\0';
314 #else
316 int cur = 0;
317 int len = 16;
318 char *buf = malloc(len);
319 if (!buf)
320 return -1;
321 while ((c = getc(f)) != EOF
322 && c != '\n')
324 if (cur >= len-1)
326 if (!(buf = realloc(buf, len *= 2)))
327 return -1;
329 buf[cur] = c;
330 cur++;
332 score->data = buf;
333 score->data[cur] = '\0';
335 #endif
336 return 0;
340 read_scores(filename, scores, count)
341 const char *filename;
342 struct score_entry **scores;
343 int *count;
345 int readval, scorecount, cursize;
346 struct score_entry *ret;
347 FILE *f = fopen(filename, "r");
348 if (!f)
349 return -1;
350 scorecount = 0;
351 cursize = 16;
352 ret = malloc(sizeof(struct score_entry) * cursize);
353 if (!ret)
354 return -1;
355 while ((readval = read_score(f, &ret[scorecount])) == 0)
357 /* We encoutered an error */
358 if (readval < 0)
359 return -1;
360 scorecount++;
361 if (scorecount >= cursize)
363 ret = realloc(ret, cursize *= 2);
364 if (!ret)
365 return -1;
368 *count = scorecount;
369 *scores = ret;
370 return 0;
374 score_compare(a, b)
375 const void *a;
376 const void *b;
378 const struct score_entry *sa = (const struct score_entry *) a;
379 const struct score_entry *sb = (const struct score_entry *) b;
380 return (sb->score > sa->score) - (sb->score < sa->score);
384 score_compare_reverse(a, b)
385 const void *a;
386 const void *b;
388 const struct score_entry *sa = (const struct score_entry *) a;
389 const struct score_entry *sb = (const struct score_entry *) b;
390 return (sa->score > sb->score) - (sa->score < sb->score);
394 push_score(scores, count, newscore, username, newdata)
395 struct score_entry **scores;
396 int *count; int newscore;
397 char *username;
398 char *newdata;
400 struct score_entry *newscores = realloc(*scores,
401 sizeof(struct score_entry) * ((*count) + 1));
402 if (!newscores)
403 return -1;
404 newscores[*count].score = newscore;
405 newscores[*count].username = username;
406 newscores[*count].data = newdata;
407 (*count) += 1;
408 *scores = newscores;
409 return 0;
412 void
413 sort_scores(scores, count, reverse)
414 struct score_entry *scores;
415 int count;
416 int reverse;
418 qsort(scores, count, sizeof(struct score_entry),
419 reverse ? score_compare_reverse : score_compare);
423 write_scores(filename, scores, count)
424 const char *filename;
425 const struct score_entry * scores;
426 int count;
428 FILE *f;
429 int i;
430 char *tempfile = malloc(strlen(filename) + strlen(".tempXXXXXX") + 1);
431 if (!tempfile)
432 return -1;
433 strcpy(tempfile, filename);
434 strcat(tempfile, ".tempXXXXXX");
435 #ifdef HAVE_MKSTEMP
436 if (mkstemp(tempfile) < 0
437 #else
438 if (mktemp(tempfile) != tempfile
439 #endif
440 || !(f = fopen(tempfile, "w")))
441 return -1;
442 for (i = 0; i < count; i++)
443 if (fprintf(f, "%ld %s %s\n", scores[i].score, scores[i].username,
444 scores[i].data) < 0)
445 return -1;
446 fclose(f);
447 if (rename(tempfile, filename) < 0)
448 return -1;
449 if (chmod(filename, 0644) < 0)
450 return -1;
451 return 0;
455 lock_file(filename, state)
456 const char *filename;
457 void **state;
459 int fd;
460 struct stat buf;
461 int attempts = 0;
462 char *lockext = ".lockfile";
463 char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60);
464 if (!lockpath)
465 return -1;
466 strcpy(lockpath, filename);
467 strcat(lockpath, lockext);
468 *state = lockpath;
469 trylock:
470 attempts++;
471 /* If the lock is over an hour old, delete it. */
472 if (stat(lockpath, &buf) == 0
473 && (difftime(buf.st_ctime, time(NULL) > 60*60)))
474 unlink(lockpath);
475 if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0)
477 if (errno == EEXIST)
479 /* Break the lock; we won't corrupt the file, but we might
480 lose some scores. */
481 if (attempts > MAX_ATTEMPTS)
483 unlink(lockpath);
484 attempts = 0;
486 sleep((rand() % 2)+1);
487 goto trylock;
489 else
490 return -1;
492 close(fd);
493 return 0;
497 unlock_file(filename, state)
498 const char *filename;
499 void *state;
501 char *lockpath = (char *) state;
502 int ret = unlink(lockpath);
503 int saved_errno = errno;
504 free(lockpath);
505 errno = saved_errno;
506 return ret;