(Fquo): If any argument is float, do the computation in floating point.
[bpt/emacs.git] / lib-src / update-game-score.c
CommitLineData
2f1de3dd 1/* update-game-score.c --- Update a score file
1d28afaf 2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2f1de3dd
CW
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21/* This program is allows a game to securely and atomically update a
8eec804f
CW
22 score file. It should be installed setuid, owned by an appropriate
23 user like `games'.
24
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).
2f1de3dd
CW
28
29 Created 2002/03/22, by Colin Walters <walters@debian.org>
30*/
31
c42d6dbd
EZ
32#include <config.h>
33
5b400482 34#ifdef HAVE_UNISTD_H
2f1de3dd 35#include <unistd.h>
5b400482 36#endif
2f1de3dd 37#include <errno.h>
5b400482 38#ifdef HAVE_STRING_H
2f1de3dd 39#include <string.h>
5b400482
DL
40#endif
41#ifdef HAVE_STDLIB_H
2f1de3dd 42#include <stdlib.h>
5b400482 43#endif
2f1de3dd
CW
44#include <stdio.h>
45#include <time.h>
cd553ffb 46#include <pwd.h>
2f1de3dd 47#include <ctype.h>
5b400482 48#ifdef HAVE_FCNTL_H
2f1de3dd 49#include <fcntl.h>
5b400482
DL
50#endif
51#ifdef STDC_HEADERS
8eec804f 52#include <stdarg.h>
5b400482 53#endif
2f1de3dd 54#include <sys/stat.h>
2f1de3dd 55
5b400482
DL
56/* Needed for SunOS4, for instance. */
57extern char *optarg;
58extern int optind, opterr;
59
2f1de3dd 60#define MAX_ATTEMPTS 5
8eec804f
CW
61#define MAX_SCORES 200
62#define MAX_DATA_LEN 1024
5795b420 63
cf398788
CW
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
70
1d28afaf
DL
71#ifndef HAVE_DIFFTIME
72/* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
73#define difftime(t1, t0) (double)((t1) - (t0))
74#endif
75
2f1de3dd 76int
a5d107f3 77usage (err)
cf398788 78 int err;
2f1de3dd 79{
a5d107f3
RS
80 fprintf (stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
81 fprintf (stdout, " update-game-score -h\n");
82 fprintf (stdout, " -h\t\tDisplay this help.\n");
83 fprintf (stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
84 fprintf (stdout, " -r\t\tSort the scores in increasing order.\n");
85 fprintf (stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
86 exit (err);
2f1de3dd
CW
87}
88
a5d107f3
RS
89int lock_file P_ ((const char *filename, void **state));
90int unlock_file P_ ((const char *filename, void *state));
2f1de3dd
CW
91
92struct score_entry
93{
94 long score;
cd553ffb 95 char *username;
2f1de3dd
CW
96 char *data;
97};
98
a5d107f3
RS
99int read_scores P_ ((const char *filename, struct score_entry **scores,
100 int *count));
101int push_score P_ ((struct score_entry **scores, int *count,
102 int newscore, char *username, char *newdata));
103void sort_scores P_ ((struct score_entry *scores, int count, int reverse));
104int write_scores P_ ((const char *filename, const struct score_entry *scores,
105 int count));
cf398788 106
a5d107f3 107void lose P_ ((const char *msg)) NO_RETURN;
cf398788 108
a5d107f3
RS
109void
110lose (msg)
cf398788
CW
111 const char *msg;
112{
a5d107f3
RS
113 fprintf (stderr, "%s\n", msg);
114 exit (1);
cf398788 115}
2f1de3dd 116
a5d107f3 117void lose_syserr P_ ((const char *msg)) NO_RETURN;
d99fabf0 118
1d28afaf
DL
119/* Taken from sysdep.c. */
120#ifndef HAVE_STRERROR
121#ifndef WINDOWSNT
122char *
123strerror (errnum)
124 int errnum;
125{
126 extern char *sys_errlist[];
127 extern int sys_nerr;
128
129 if (errnum >= 0 && errnum < sys_nerr)
130 return sys_errlist[errnum];
131 return (char *) "Unknown error";
132}
133#endif /* not WINDOWSNT */
134#endif /* ! HAVE_STRERROR */
135
a5d107f3
RS
136void
137lose_syserr (msg)
cf398788 138 const char *msg;
d99fabf0 139{
a5d107f3
RS
140 fprintf (stderr, "%s: %s\n", msg, strerror (errno));
141 exit (1);
d99fabf0
CW
142}
143
cd553ffb 144char *
5b400482 145get_user_id P_ ((void))
cd553ffb
CW
146{
147 char *name;
a5d107f3 148 struct passwd *buf = getpwuid (getuid ());
cd553ffb
CW
149 if (!buf)
150 {
151 int count = 1;
a5d107f3 152 int uid = (int) getuid ();
8eec804f
CW
153 int tuid = uid;
154 while (tuid /= 10)
cd553ffb 155 count++;
a5d107f3 156 name = malloc (count+1);
d99fabf0
CW
157 if (!name)
158 return NULL;
a5d107f3 159 sprintf (name, "%d", uid);
cd553ffb
CW
160 return name;
161 }
162 return buf->pw_name;
163}
164
5795b420 165char *
a5d107f3 166get_prefix (running_suid, user_prefix)
cf398788
CW
167 int running_suid;
168 char *user_prefix;
8eec804f 169{
d99fabf0 170 if (!running_suid && user_prefix == NULL)
a5d107f3 171 lose ("Not using a shared game directory, and no prefix given.");
d99fabf0
CW
172 if (running_suid)
173 {
174#ifdef HAVE_SHARED_GAME_DIR
175 return HAVE_SHARED_GAME_DIR;
176#else
a5d107f3 177 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
d99fabf0
CW
178#endif
179 }
180 return user_prefix;
8eec804f
CW
181}
182
2f1de3dd 183int
a5d107f3 184main (argc, argv)
cf398788
CW
185 int argc;
186 char **argv;
2f1de3dd 187{
d99fabf0 188 int c, running_suid;
2f1de3dd 189 void *lockstate;
d99fabf0 190 char *user_id, *scorefile, *prefix, *user_prefix = NULL;
2f1de3dd
CW
191 struct stat buf;
192 struct score_entry *scores;
8eec804f 193 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
2f1de3dd
CW
194 char *newdata;
195
a5d107f3 196 srand (time (0));
2f1de3dd 197
a5d107f3 198 while ((c = getopt (argc, argv, "hrm:d:")) != -1)
2f1de3dd
CW
199 switch (c)
200 {
201 case 'h':
a5d107f3 202 usage (0);
2f1de3dd 203 break;
d99fabf0
CW
204 case 'd':
205 user_prefix = optarg;
206 break;
2f1de3dd
CW
207 case 'r':
208 reverse = 1;
209 break;
210 case 'm':
a5d107f3 211 max = atoi (optarg);
8eec804f
CW
212 if (max > MAX_SCORES)
213 max = MAX_SCORES;
2f1de3dd
CW
214 break;
215 default:
a5d107f3 216 usage (1);
2f1de3dd
CW
217 }
218
219 if (optind+3 != argc)
a5d107f3 220 usage (1);
5795b420 221
a5d107f3 222 running_suid = (getuid () != geteuid ());
5795b420 223
a5d107f3 224 prefix = get_prefix (running_suid, user_prefix);
8eec804f 225
a5d107f3 226 scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2);
2f1de3dd 227 if (!scorefile)
a5d107f3 228 lose_syserr ("Couldn't allocate score file");
8eec804f 229
a5d107f3
RS
230 strcpy (scorefile, prefix);
231 strcat (scorefile, "/");
232 strcat (scorefile, argv[optind]);
233 newscore = atoi (argv[optind+1]);
2f1de3dd 234 newdata = argv[optind+2];
a5d107f3 235 if (strlen (newdata) > MAX_DATA_LEN)
8eec804f 236 newdata[MAX_DATA_LEN] = '\0';
d99fabf0 237
00a7e408
RS
238 user_id = get_user_id ();
239 if (user_id == NULL)
a5d107f3 240 lose_syserr ("Couldn't determine user id");
177c0ea7 241
a5d107f3
RS
242 if (stat (scorefile, &buf) < 0)
243 lose_syserr ("Failed to access scores file");
177c0ea7 244
a5d107f3
RS
245 if (lock_file (scorefile, &lockstate) < 0)
246 lose_syserr ("Failed to lock scores file");
177c0ea7 247
a5d107f3 248 if (read_scores (scorefile, &scores, &scorecount) < 0)
2f1de3dd 249 {
a5d107f3
RS
250 unlock_file (scorefile, lockstate);
251 lose_syserr ("Failed to read scores file");
2f1de3dd 252 }
a5d107f3 253 push_score (&scores, &scorecount, newscore, user_id, newdata);
8eec804f
CW
254 /* Limit the number of scores. If we're using reverse sorting, then
255 we should increment the beginning of the array, to skip over the
256 *smallest* scores. Otherwise, we just decrement the number of
257 scores, since the smallest will be at the end. */
258 if (scorecount > MAX_SCORES)
259 scorecount -= (scorecount - MAX_SCORES);
260 if (reverse)
261 scores += (scorecount - MAX_SCORES);
a5d107f3
RS
262 sort_scores (scores, scorecount, reverse);
263 if (write_scores (scorefile, scores, scorecount) < 0)
2f1de3dd 264 {
a5d107f3
RS
265 unlock_file (scorefile, lockstate);
266 lose_syserr ("Failed to write scores file");
2f1de3dd 267 }
a5d107f3
RS
268 unlock_file (scorefile, lockstate);
269 exit (0);
2f1de3dd
CW
270}
271
272int
a5d107f3 273read_score (f, score)
cf398788
CW
274 FILE *f;
275 struct score_entry *score;
2f1de3dd
CW
276{
277 int c;
a5d107f3 278 if (feof (f))
2f1de3dd 279 return 1;
a5d107f3
RS
280 while ((c = getc (f)) != EOF
281 && isdigit (c))
cd553ffb
CW
282 {
283 score->score *= 10;
284 score->score += (c-48);
285 }
a5d107f3
RS
286 while ((c = getc (f)) != EOF
287 && isspace (c))
cd553ffb
CW
288 ;
289 if (c == EOF)
290 return -1;
a5d107f3 291 ungetc (c, f);
cd553ffb
CW
292#ifdef HAVE_GETDELIM
293 {
7605f1bd 294 size_t count = 0;
a5d107f3 295 if (getdelim (&score->username, &count, ' ', f) < 1
cd553ffb
CW
296 || score->username == NULL)
297 return -1;
1d4328ff 298 /* Trim the space */
a5d107f3 299 score->username[strlen (score->username)-1] = '\0';
2f1de3dd 300 }
cd553ffb
CW
301#else
302 {
303 int unameread = 0;
304 int unamelen = 30;
a5d107f3 305 char *username = malloc (unamelen);
f5bceaf8
CW
306 if (!username)
307 return -1;
177c0ea7 308
a5d107f3
RS
309 while ((c = getc (f)) != EOF
310 && !isspace (c))
cd553ffb 311 {
8eec804f 312 if (unameread >= unamelen-1)
a5d107f3 313 if (!(username = realloc (username, unamelen *= 2)))
8eec804f 314 return -1;
cd553ffb
CW
315 username[unameread] = c;
316 unameread++;
317 }
177c0ea7 318 if (c == EOF)
f5bceaf8
CW
319 return -1;
320 username[unameread] = '\0';
cd553ffb
CW
321 score->username = username;
322 }
323#endif
2f1de3dd
CW
324#ifdef HAVE_GETLINE
325 score->data = NULL;
7605f1bd 326 errno = 0;
2f1de3dd 327 {
7605f1bd 328 size_t len;
a5d107f3 329 if (getline (&score->data, &len, f) < 0)
2f1de3dd 330 return -1;
a5d107f3 331 score->data[strlen (score->data)-1] = '\0';
2f1de3dd
CW
332 }
333#else
cd553ffb
CW
334 {
335 int cur = 0;
336 int len = 16;
a5d107f3 337 char *buf = malloc (len);
cd553ffb
CW
338 if (!buf)
339 return -1;
a5d107f3 340 while ((c = getc (f)) != EOF
f5bceaf8 341 && c != '\n')
cd553ffb
CW
342 {
343 if (cur >= len-1)
344 {
a5d107f3 345 if (!(buf = realloc (buf, len *= 2)))
cd553ffb
CW
346 return -1;
347 }
348 buf[cur] = c;
349 cur++;
350 }
351 score->data = buf;
5795b420 352 score->data[cur] = '\0';
cd553ffb 353 }
2f1de3dd 354#endif
2f1de3dd
CW
355 return 0;
356}
357
358int
a5d107f3 359read_scores (filename, scores, count)
cf398788
CW
360 const char *filename;
361 struct score_entry **scores;
362 int *count;
2f1de3dd
CW
363{
364 int readval, scorecount, cursize;
365 struct score_entry *ret;
a5d107f3 366 FILE *f = fopen (filename, "r");
177c0ea7 367 if (!f)
2f1de3dd
CW
368 return -1;
369 scorecount = 0;
370 cursize = 16;
00a7e408 371 ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize);
177c0ea7 372 if (!ret)
2f1de3dd 373 return -1;
a5d107f3 374 while ((readval = read_score (f, &ret[scorecount])) == 0)
2f1de3dd
CW
375 {
376 /* We encoutered an error */
377 if (readval < 0)
378 return -1;
379 scorecount++;
380 if (scorecount >= cursize)
381 {
ee435bae
JB
382 cursize *= 2;
383 ret = (struct score_entry *)
384 realloc (ret, (sizeof (struct score_entry) * cursize));
2f1de3dd
CW
385 if (!ret)
386 return -1;
387 }
388 }
389 *count = scorecount;
390 *scores = ret;
391 return 0;
392}
393
394int
a5d107f3 395score_compare (a, b)
cf398788
CW
396 const void *a;
397 const void *b;
2f1de3dd
CW
398{
399 const struct score_entry *sa = (const struct score_entry *) a;
400 const struct score_entry *sb = (const struct score_entry *) b;
401 return (sb->score > sa->score) - (sb->score < sa->score);
402}
403
404int
a5d107f3 405score_compare_reverse (a, b)
cf398788
CW
406 const void *a;
407 const void *b;
2f1de3dd
CW
408{
409 const struct score_entry *sa = (const struct score_entry *) a;
410 const struct score_entry *sb = (const struct score_entry *) b;
411 return (sa->score > sb->score) - (sa->score < sb->score);
412}
413
414int
177c0ea7 415push_score (scores, count, newscore, username, newdata)
cf398788
CW
416 struct score_entry **scores;
417 int *count; int newscore;
418 char *username;
419 char *newdata;
2f1de3dd 420{
a5d107f3 421 struct score_entry *newscores
00a7e408
RS
422 = (struct score_entry *) realloc (*scores,
423 sizeof (struct score_entry) * ((*count) + 1));
2f1de3dd
CW
424 if (!newscores)
425 return -1;
426 newscores[*count].score = newscore;
cd553ffb 427 newscores[*count].username = username;
2f1de3dd
CW
428 newscores[*count].data = newdata;
429 (*count) += 1;
430 *scores = newscores;
431 return 0;
432}
177c0ea7 433
2f1de3dd 434void
a5d107f3 435sort_scores (scores, count, reverse)
cf398788
CW
436 struct score_entry *scores;
437 int count;
177c0ea7 438 int reverse;
2f1de3dd 439{
a5d107f3 440 qsort (scores, count, sizeof (struct score_entry),
2f1de3dd
CW
441 reverse ? score_compare_reverse : score_compare);
442}
443
444int
a5d107f3 445write_scores (filename, scores, count)
cf398788
CW
446 const char *filename;
447 const struct score_entry * scores;
177c0ea7 448 int count;
2f1de3dd 449{
177c0ea7 450 FILE *f;
2f1de3dd 451 int i;
a5d107f3 452 char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1);
2f1de3dd
CW
453 if (!tempfile)
454 return -1;
a5d107f3
RS
455 strcpy (tempfile, filename);
456 strcat (tempfile, ".tempXXXXXX");
8eec804f 457#ifdef HAVE_MKSTEMP
a5d107f3 458 if (mkstemp (tempfile) < 0
8eec804f 459#else
a5d107f3 460 if (mktemp (tempfile) != tempfile
8eec804f 461#endif
a5d107f3 462 || !(f = fopen (tempfile, "w")))
2f1de3dd
CW
463 return -1;
464 for (i = 0; i < count; i++)
a5d107f3 465 if (fprintf (f, "%ld %s %s\n", scores[i].score, scores[i].username,
cd553ffb 466 scores[i].data) < 0)
2f1de3dd 467 return -1;
a5d107f3
RS
468 fclose (f);
469 if (rename (tempfile, filename) < 0)
2f1de3dd 470 return -1;
a5d107f3 471 if (chmod (filename, 0644) < 0)
2f1de3dd 472 return -1;
8eec804f 473 return 0;
2f1de3dd 474}
177c0ea7 475
2f1de3dd 476int
a5d107f3 477lock_file (filename, state)
cf398788
CW
478 const char *filename;
479 void **state;
2f1de3dd
CW
480{
481 int fd;
7c4f6873 482 struct stat buf;
2f1de3dd
CW
483 int attempts = 0;
484 char *lockext = ".lockfile";
a5d107f3 485 char *lockpath = malloc (strlen (filename) + strlen (lockext) + 60);
2f1de3dd
CW
486 if (!lockpath)
487 return -1;
a5d107f3
RS
488 strcpy (lockpath, filename);
489 strcat (lockpath, lockext);
2f1de3dd
CW
490 *state = lockpath;
491 trylock:
492 attempts++;
7c4f6873 493 /* If the lock is over an hour old, delete it. */
a5d107f3
RS
494 if (stat (lockpath, &buf) == 0
495 && (difftime (buf.st_ctime, time (NULL) > 60*60)))
496 unlink (lockpath);
00a7e408
RS
497 fd = open (lockpath, O_CREAT | O_EXCL, 0600);
498 if (fd < 0)
2f1de3dd
CW
499 {
500 if (errno == EEXIST)
501 {
502 /* Break the lock; we won't corrupt the file, but we might
503 lose some scores. */
504 if (attempts > MAX_ATTEMPTS)
7c4f6873 505 {
a5d107f3 506 unlink (lockpath);
7c4f6873
CW
507 attempts = 0;
508 }
a5d107f3 509 sleep ((rand () % 2)+1);
2f1de3dd
CW
510 goto trylock;
511 }
512 else
513 return -1;
514 }
a5d107f3 515 close (fd);
2f1de3dd
CW
516 return 0;
517}
177c0ea7 518
2f1de3dd 519int
a5d107f3 520unlock_file (filename, state)
cf398788
CW
521 const char *filename;
522 void *state;
2f1de3dd
CW
523{
524 char *lockpath = (char *) state;
a5d107f3 525 int ret = unlink (lockpath);
2f1de3dd 526 int saved_errno = errno;
a5d107f3 527 free (lockpath);
2f1de3dd
CW
528 errno = saved_errno;
529 return ret;
530}
ab5796a9
MB
531
532/* arch-tag: 2bf5c52e-4beb-463a-954e-c58b9c64736b
533 (do not change this comment) */