aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2003-02-04 12:00:09 +0000
committerRichard M. Stallman2003-02-04 12:00:09 +0000
commita5d107f3c71c8d38d150cc2626440c8f9c52e9be (patch)
treed931f729b7865e4ba453fea8b705bfd280b24644
parent22b94eeb2cda1be98f8cc6ac9b86e567ad221ab4 (diff)
downloademacs-a5d107f3c71c8d38d150cc2626440c8f9c52e9be.tar.gz
emacs-a5d107f3c71c8d38d150cc2626440c8f9c52e9be.zip
Whitespace changes.
-rw-r--r--lib-src/update-game-score.c247
1 files changed, 122 insertions, 125 deletions
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c
index 7393135daa9..2e526b00bd0 100644
--- a/lib-src/update-game-score.c
+++ b/lib-src/update-game-score.c
@@ -69,22 +69,20 @@ extern int optind, opterr;
69#endif 69#endif
70 70
71int 71int
72usage(err) 72usage (err)
73 int err; 73 int err;
74{ 74{
75 fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n"); 75 fprintf (stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
76 fprintf(stdout, " update-game-score -h\n"); 76 fprintf (stdout, " update-game-score -h\n");
77 fprintf(stdout, " -h\t\tDisplay this help.\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"); 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"); 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"); 80 fprintf (stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
81 exit(err); 81 exit (err);
82} 82}
83 83
84int 84int lock_file P_ ((const char *filename, void **state));
85lock_file P_((const char *filename, void **state)); 85int unlock_file P_ ((const char *filename, void *state));
86int
87unlock_file P_((const char *filename, void *state));
88 86
89struct score_entry 87struct score_entry
90{ 88{
@@ -93,77 +91,75 @@ struct score_entry
93 char *data; 91 char *data;
94}; 92};
95 93
96int 94int read_scores P_ ((const char *filename, struct score_entry **scores,
97read_scores P_((const char *filename, struct score_entry **scores, 95 int *count));
98 int *count)); 96int push_score P_ ((struct score_entry **scores, int *count,
99int 97 int newscore, char *username, char *newdata));
100push_score P_((struct score_entry **scores, int *count, 98void sort_scores P_ ((struct score_entry *scores, int count, int reverse));
101 int newscore, char *username, char *newdata)); 99int write_scores P_ ((const char *filename, const struct score_entry *scores,
102void 100 int count));
103sort_scores P_((struct score_entry *scores, int count, int reverse));
104int
105write_scores P_((const char *filename, const struct score_entry *scores,
106 int count));
107 101
108void lose P_((const char *msg)) NO_RETURN; 102void lose P_ ((const char *msg)) NO_RETURN;
109 103
110void lose(msg) 104void
105lose (msg)
111 const char *msg; 106 const char *msg;
112{ 107{
113 fprintf(stderr, "%s\n", msg); 108 fprintf (stderr, "%s\n", msg);
114 exit(1); 109 exit (1);
115} 110}
116 111
117void lose_syserr P_((const char *msg)) NO_RETURN; 112void lose_syserr P_ ((const char *msg)) NO_RETURN;
118 113
119void lose_syserr(msg) 114void
115lose_syserr (msg)
120 const char *msg; 116 const char *msg;
121{ 117{
122 fprintf(stderr, "%s: %s\n", msg, strerror(errno)); 118 fprintf (stderr, "%s: %s\n", msg, strerror (errno));
123 exit(1); 119 exit (1);
124} 120}
125 121
126char * 122char *
127get_user_id P_ ((void)) 123get_user_id P_ ((void))
128{ 124{
129 char *name; 125 char *name;
130 struct passwd *buf = getpwuid(getuid()); 126 struct passwd *buf = getpwuid (getuid ());
131 if (!buf) 127 if (!buf)
132 { 128 {
133 int count = 1; 129 int count = 1;
134 int uid = (int) getuid(); 130 int uid = (int) getuid ();
135 int tuid = uid; 131 int tuid = uid;
136 while (tuid /= 10) 132 while (tuid /= 10)
137 count++; 133 count++;
138 name = malloc(count+1); 134 name = malloc (count+1);
139 if (!name) 135 if (!name)
140 return NULL; 136 return NULL;
141 sprintf(name, "%d", uid); 137 sprintf (name, "%d", uid);
142 return name; 138 return name;
143 } 139 }
144 return buf->pw_name; 140 return buf->pw_name;
145} 141}
146 142
147char * 143char *
148get_prefix(running_suid, user_prefix) 144get_prefix (running_suid, user_prefix)
149 int running_suid; 145 int running_suid;
150 char *user_prefix; 146 char *user_prefix;
151{ 147{
152 if (!running_suid && user_prefix == NULL) 148 if (!running_suid && user_prefix == NULL)
153 lose("Not using a shared game directory, and no prefix given."); 149 lose ("Not using a shared game directory, and no prefix given.");
154 if (running_suid) 150 if (running_suid)
155 { 151 {
156#ifdef HAVE_SHARED_GAME_DIR 152#ifdef HAVE_SHARED_GAME_DIR
157 return HAVE_SHARED_GAME_DIR; 153 return HAVE_SHARED_GAME_DIR;
158#else 154#else
159 lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid."); 155 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
160#endif 156#endif
161 } 157 }
162 return user_prefix; 158 return user_prefix;
163} 159}
164 160
165int 161int
166main(argc, argv) 162main (argc, argv)
167 int argc; 163 int argc;
168 char **argv; 164 char **argv;
169{ 165{
@@ -175,13 +171,13 @@ main(argc, argv)
175 int newscore, scorecount, reverse = 0, max = MAX_SCORES; 171 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
176 char *newdata; 172 char *newdata;
177 173
178 srand(time(0)); 174 srand (time (0));
179 175
180 while ((c = getopt(argc, argv, "hrm:d:")) != -1) 176 while ((c = getopt (argc, argv, "hrm:d:")) != -1)
181 switch (c) 177 switch (c)
182 { 178 {
183 case 'h': 179 case 'h':
184 usage(0); 180 usage (0);
185 break; 181 break;
186 case 'd': 182 case 'd':
187 user_prefix = optarg; 183 user_prefix = optarg;
@@ -190,48 +186,48 @@ main(argc, argv)
190 reverse = 1; 186 reverse = 1;
191 break; 187 break;
192 case 'm': 188 case 'm':
193 max = atoi(optarg); 189 max = atoi (optarg);
194 if (max > MAX_SCORES) 190 if (max > MAX_SCORES)
195 max = MAX_SCORES; 191 max = MAX_SCORES;
196 break; 192 break;
197 default: 193 default:
198 usage(1); 194 usage (1);
199 } 195 }
200 196
201 if (optind+3 != argc) 197 if (optind+3 != argc)
202 usage(1); 198 usage (1);
203 199
204 running_suid = (getuid() != geteuid()); 200 running_suid = (getuid () != geteuid ());
205 201
206 prefix = get_prefix(running_suid, user_prefix); 202 prefix = get_prefix (running_suid, user_prefix);
207 203
208 scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2); 204 scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2);
209 if (!scorefile) 205 if (!scorefile)
210 lose_syserr("Couldn't allocate score file"); 206 lose_syserr ("Couldn't allocate score file");
211 207
212 strcpy(scorefile, prefix); 208 strcpy (scorefile, prefix);
213 strcat(scorefile, "/"); 209 strcat (scorefile, "/");
214 strcat(scorefile, argv[optind]); 210 strcat (scorefile, argv[optind]);
215 newscore = atoi(argv[optind+1]); 211 newscore = atoi (argv[optind+1]);
216 newdata = argv[optind+2]; 212 newdata = argv[optind+2];
217 if (strlen(newdata) > MAX_DATA_LEN) 213 if (strlen (newdata) > MAX_DATA_LEN)
218 newdata[MAX_DATA_LEN] = '\0'; 214 newdata[MAX_DATA_LEN] = '\0';
219 215
220 if ((user_id = get_user_id()) == NULL) 216 if ((user_id = get_user_id ()) == NULL)
221 lose_syserr("Couldn't determine user id"); 217 lose_syserr ("Couldn't determine user id");
222 218
223 if (stat(scorefile, &buf) < 0) 219 if (stat (scorefile, &buf) < 0)
224 lose_syserr("Failed to access scores file"); 220 lose_syserr ("Failed to access scores file");
225 221
226 if (lock_file(scorefile, &lockstate) < 0) 222 if (lock_file (scorefile, &lockstate) < 0)
227 lose_syserr("Failed to lock scores file"); 223 lose_syserr ("Failed to lock scores file");
228 224
229 if (read_scores(scorefile, &scores, &scorecount) < 0) 225 if (read_scores (scorefile, &scores, &scorecount) < 0)
230 { 226 {
231 unlock_file(scorefile, lockstate); 227 unlock_file (scorefile, lockstate);
232 lose_syserr("Failed to read scores file"); 228 lose_syserr ("Failed to read scores file");
233 } 229 }
234 push_score(&scores, &scorecount, newscore, user_id, newdata); 230 push_score (&scores, &scorecount, newscore, user_id, newdata);
235 /* Limit the number of scores. If we're using reverse sorting, then 231 /* 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 232 we should increment the beginning of the array, to skip over the
237 *smallest* scores. Otherwise, we just decrement the number of 233 *smallest* scores. Otherwise, we just decrement the number of
@@ -240,58 +236,58 @@ main(argc, argv)
240 scorecount -= (scorecount - MAX_SCORES); 236 scorecount -= (scorecount - MAX_SCORES);
241 if (reverse) 237 if (reverse)
242 scores += (scorecount - MAX_SCORES); 238 scores += (scorecount - MAX_SCORES);
243 sort_scores(scores, scorecount, reverse); 239 sort_scores (scores, scorecount, reverse);
244 if (write_scores(scorefile, scores, scorecount) < 0) 240 if (write_scores (scorefile, scores, scorecount) < 0)
245 { 241 {
246 unlock_file(scorefile, lockstate); 242 unlock_file (scorefile, lockstate);
247 lose_syserr("Failed to write scores file"); 243 lose_syserr ("Failed to write scores file");
248 } 244 }
249 unlock_file(scorefile, lockstate); 245 unlock_file (scorefile, lockstate);
250 exit(0); 246 exit (0);
251} 247}
252 248
253int 249int
254read_score(f, score) 250read_score (f, score)
255 FILE *f; 251 FILE *f;
256 struct score_entry *score; 252 struct score_entry *score;
257{ 253{
258 int c; 254 int c;
259 if (feof(f)) 255 if (feof (f))
260 return 1; 256 return 1;
261 while ((c = getc(f)) != EOF 257 while ((c = getc (f)) != EOF
262 && isdigit(c)) 258 && isdigit (c))
263 { 259 {
264 score->score *= 10; 260 score->score *= 10;
265 score->score += (c-48); 261 score->score += (c-48);
266 } 262 }
267 while ((c = getc(f)) != EOF 263 while ((c = getc (f)) != EOF
268 && isspace(c)) 264 && isspace (c))
269 ; 265 ;
270 if (c == EOF) 266 if (c == EOF)
271 return -1; 267 return -1;
272 ungetc(c, f); 268 ungetc (c, f);
273#ifdef HAVE_GETDELIM 269#ifdef HAVE_GETDELIM
274 { 270 {
275 size_t count = 0; 271 size_t count = 0;
276 if (getdelim(&score->username, &count, ' ', f) < 1 272 if (getdelim (&score->username, &count, ' ', f) < 1
277 || score->username == NULL) 273 || score->username == NULL)
278 return -1; 274 return -1;
279 /* Trim the space */ 275 /* Trim the space */
280 score->username[strlen(score->username)-1] = '\0'; 276 score->username[strlen (score->username)-1] = '\0';
281 } 277 }
282#else 278#else
283 { 279 {
284 int unameread = 0; 280 int unameread = 0;
285 int unamelen = 30; 281 int unamelen = 30;
286 char *username = malloc(unamelen); 282 char *username = malloc (unamelen);
287 if (!username) 283 if (!username)
288 return -1; 284 return -1;
289 285
290 while ((c = getc(f)) != EOF 286 while ((c = getc (f)) != EOF
291 && !isspace(c)) 287 && !isspace (c))
292 { 288 {
293 if (unameread >= unamelen-1) 289 if (unameread >= unamelen-1)
294 if (!(username = realloc(username, unamelen *= 2))) 290 if (!(username = realloc (username, unamelen *= 2)))
295 return -1; 291 return -1;
296 username[unameread] = c; 292 username[unameread] = c;
297 unameread++; 293 unameread++;
@@ -307,23 +303,23 @@ read_score(f, score)
307 errno = 0; 303 errno = 0;
308 { 304 {
309 size_t len; 305 size_t len;
310 if (getline(&score->data, &len, f) < 0) 306 if (getline (&score->data, &len, f) < 0)
311 return -1; 307 return -1;
312 score->data[strlen(score->data)-1] = '\0'; 308 score->data[strlen (score->data)-1] = '\0';
313 } 309 }
314#else 310#else
315 { 311 {
316 int cur = 0; 312 int cur = 0;
317 int len = 16; 313 int len = 16;
318 char *buf = malloc(len); 314 char *buf = malloc (len);
319 if (!buf) 315 if (!buf)
320 return -1; 316 return -1;
321 while ((c = getc(f)) != EOF 317 while ((c = getc (f)) != EOF
322 && c != '\n') 318 && c != '\n')
323 { 319 {
324 if (cur >= len-1) 320 if (cur >= len-1)
325 { 321 {
326 if (!(buf = realloc(buf, len *= 2))) 322 if (!(buf = realloc (buf, len *= 2)))
327 return -1; 323 return -1;
328 } 324 }
329 buf[cur] = c; 325 buf[cur] = c;
@@ -337,22 +333,22 @@ read_score(f, score)
337} 333}
338 334
339int 335int
340read_scores(filename, scores, count) 336read_scores (filename, scores, count)
341 const char *filename; 337 const char *filename;
342 struct score_entry **scores; 338 struct score_entry **scores;
343 int *count; 339 int *count;
344{ 340{
345 int readval, scorecount, cursize; 341 int readval, scorecount, cursize;
346 struct score_entry *ret; 342 struct score_entry *ret;
347 FILE *f = fopen(filename, "r"); 343 FILE *f = fopen (filename, "r");
348 if (!f) 344 if (!f)
349 return -1; 345 return -1;
350 scorecount = 0; 346 scorecount = 0;
351 cursize = 16; 347 cursize = 16;
352 ret = malloc(sizeof(struct score_entry) * cursize); 348 ret = malloc (sizeof (struct score_entry) * cursize);
353 if (!ret) 349 if (!ret)
354 return -1; 350 return -1;
355 while ((readval = read_score(f, &ret[scorecount])) == 0) 351 while ((readval = read_score (f, &ret[scorecount])) == 0)
356 { 352 {
357 /* We encoutered an error */ 353 /* We encoutered an error */
358 if (readval < 0) 354 if (readval < 0)
@@ -360,7 +356,7 @@ read_scores(filename, scores, count)
360 scorecount++; 356 scorecount++;
361 if (scorecount >= cursize) 357 if (scorecount >= cursize)
362 { 358 {
363 ret = realloc(ret, cursize *= 2); 359 ret = realloc (ret, cursize *= 2);
364 if (!ret) 360 if (!ret)
365 return -1; 361 return -1;
366 } 362 }
@@ -371,7 +367,7 @@ read_scores(filename, scores, count)
371} 367}
372 368
373int 369int
374score_compare(a, b) 370score_compare (a, b)
375 const void *a; 371 const void *a;
376 const void *b; 372 const void *b;
377{ 373{
@@ -381,7 +377,7 @@ score_compare(a, b)
381} 377}
382 378
383int 379int
384score_compare_reverse(a, b) 380score_compare_reverse (a, b)
385 const void *a; 381 const void *a;
386 const void *b; 382 const void *b;
387{ 383{
@@ -391,14 +387,15 @@ score_compare_reverse(a, b)
391} 387}
392 388
393int 389int
394push_score(scores, count, newscore, username, newdata) 390push_score (scores, count, newscore, username, newdata)
395 struct score_entry **scores; 391 struct score_entry **scores;
396 int *count; int newscore; 392 int *count; int newscore;
397 char *username; 393 char *username;
398 char *newdata; 394 char *newdata;
399{ 395{
400 struct score_entry *newscores = realloc(*scores, 396 struct score_entry *newscores
401 sizeof(struct score_entry) * ((*count) + 1)); 397 = realloc (*scores,
398 sizeof (struct score_entry) * ((*count) + 1));
402 if (!newscores) 399 if (!newscores)
403 return -1; 400 return -1;
404 newscores[*count].score = newscore; 401 newscores[*count].score = newscore;
@@ -410,49 +407,49 @@ push_score(scores, count, newscore, username, newdata)
410} 407}
411 408
412void 409void
413sort_scores(scores, count, reverse) 410sort_scores (scores, count, reverse)
414 struct score_entry *scores; 411 struct score_entry *scores;
415 int count; 412 int count;
416 int reverse; 413 int reverse;
417{ 414{
418 qsort(scores, count, sizeof(struct score_entry), 415 qsort (scores, count, sizeof (struct score_entry),
419 reverse ? score_compare_reverse : score_compare); 416 reverse ? score_compare_reverse : score_compare);
420} 417}
421 418
422int 419int
423write_scores(filename, scores, count) 420write_scores (filename, scores, count)
424 const char *filename; 421 const char *filename;
425 const struct score_entry * scores; 422 const struct score_entry * scores;
426 int count; 423 int count;
427{ 424{
428 FILE *f; 425 FILE *f;
429 int i; 426 int i;
430 char *tempfile = malloc(strlen(filename) + strlen(".tempXXXXXX") + 1); 427 char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1);
431 if (!tempfile) 428 if (!tempfile)
432 return -1; 429 return -1;
433 strcpy(tempfile, filename); 430 strcpy (tempfile, filename);
434 strcat(tempfile, ".tempXXXXXX"); 431 strcat (tempfile, ".tempXXXXXX");
435#ifdef HAVE_MKSTEMP 432#ifdef HAVE_MKSTEMP
436 if (mkstemp(tempfile) < 0 433 if (mkstemp (tempfile) < 0
437#else 434#else
438 if (mktemp(tempfile) != tempfile 435 if (mktemp (tempfile) != tempfile
439#endif 436#endif
440 || !(f = fopen(tempfile, "w"))) 437 || !(f = fopen (tempfile, "w")))
441 return -1; 438 return -1;
442 for (i = 0; i < count; i++) 439 for (i = 0; i < count; i++)
443 if (fprintf(f, "%ld %s %s\n", scores[i].score, scores[i].username, 440 if (fprintf (f, "%ld %s %s\n", scores[i].score, scores[i].username,
444 scores[i].data) < 0) 441 scores[i].data) < 0)
445 return -1; 442 return -1;
446 fclose(f); 443 fclose (f);
447 if (rename(tempfile, filename) < 0) 444 if (rename (tempfile, filename) < 0)
448 return -1; 445 return -1;
449 if (chmod(filename, 0644) < 0) 446 if (chmod (filename, 0644) < 0)
450 return -1; 447 return -1;
451 return 0; 448 return 0;
452} 449}
453 450
454int 451int
455lock_file(filename, state) 452lock_file (filename, state)
456 const char *filename; 453 const char *filename;
457 void **state; 454 void **state;
458{ 455{
@@ -460,19 +457,19 @@ lock_file(filename, state)
460 struct stat buf; 457 struct stat buf;
461 int attempts = 0; 458 int attempts = 0;
462 char *lockext = ".lockfile"; 459 char *lockext = ".lockfile";
463 char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60); 460 char *lockpath = malloc (strlen (filename) + strlen (lockext) + 60);
464 if (!lockpath) 461 if (!lockpath)
465 return -1; 462 return -1;
466 strcpy(lockpath, filename); 463 strcpy (lockpath, filename);
467 strcat(lockpath, lockext); 464 strcat (lockpath, lockext);
468 *state = lockpath; 465 *state = lockpath;
469 trylock: 466 trylock:
470 attempts++; 467 attempts++;
471 /* If the lock is over an hour old, delete it. */ 468 /* If the lock is over an hour old, delete it. */
472 if (stat(lockpath, &buf) == 0 469 if (stat (lockpath, &buf) == 0
473 && (difftime(buf.st_ctime, time(NULL) > 60*60))) 470 && (difftime (buf.st_ctime, time (NULL) > 60*60)))
474 unlink(lockpath); 471 unlink (lockpath);
475 if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0) 472 if ((fd = open (lockpath, O_CREAT | O_EXCL, 0600)) < 0)
476 { 473 {
477 if (errno == EEXIST) 474 if (errno == EEXIST)
478 { 475 {
@@ -480,28 +477,28 @@ lock_file(filename, state)
480 lose some scores. */ 477 lose some scores. */
481 if (attempts > MAX_ATTEMPTS) 478 if (attempts > MAX_ATTEMPTS)
482 { 479 {
483 unlink(lockpath); 480 unlink (lockpath);
484 attempts = 0; 481 attempts = 0;
485 } 482 }
486 sleep((rand() % 2)+1); 483 sleep ((rand () % 2)+1);
487 goto trylock; 484 goto trylock;
488 } 485 }
489 else 486 else
490 return -1; 487 return -1;
491 } 488 }
492 close(fd); 489 close (fd);
493 return 0; 490 return 0;
494} 491}
495 492
496int 493int
497unlock_file(filename, state) 494unlock_file (filename, state)
498 const char *filename; 495 const char *filename;
499 void *state; 496 void *state;
500{ 497{
501 char *lockpath = (char *) state; 498 char *lockpath = (char *) state;
502 int ret = unlink(lockpath); 499 int ret = unlink (lockpath);
503 int saved_errno = errno; 500 int saved_errno = errno;
504 free(lockpath); 501 free (lockpath);
505 errno = saved_errno; 502 errno = saved_errno;
506 return ret; 503 return ret;
507} 504}