diff options
| author | Colin Walters | 2002-08-01 01:31:44 +0000 |
|---|---|---|
| committer | Colin Walters | 2002-08-01 01:31:44 +0000 |
| commit | cf398788c7bc6018e00e5b1134457d449138c98c (patch) | |
| tree | c047d439a808a4609014b2888bd1f6ce3df90917 /lib-src | |
| parent | 5fba5c216bfc0c83b64859e0145ad03a8a11d06a (diff) | |
| download | emacs-cf398788c7bc6018e00e5b1134457d449138c98c.tar.gz emacs-cf398788c7bc6018e00e5b1134457d449138c98c.zip | |
(P_): New macro. Use it for all prototypes.
(lose): Don't use varargs.
(lose_syserr): New function.
Change all functions to K&R style.
Diffstat (limited to 'lib-src')
| -rw-r--r-- | lib-src/update-game-score.c | 130 |
1 files changed, 84 insertions, 46 deletions
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c index 194997b63ab..21309a3634a 100644 --- a/lib-src/update-game-score.c +++ b/lib-src/update-game-score.c | |||
| @@ -53,8 +53,16 @@ Boston, MA 02111-1307, USA. */ | |||
| 53 | #define __attribute__(x) | 53 | #define __attribute__(x) |
| 54 | #endif | 54 | #endif |
| 55 | 55 | ||
| 56 | /* Declare the prototype for a general external function. */ | ||
| 57 | #if defined (PROTOTYPES) || defined (WINDOWSNT) | ||
| 58 | #define P_(proto) proto | ||
| 59 | #else | ||
| 60 | #define P_(proto) () | ||
| 61 | #endif | ||
| 62 | |||
| 56 | int | 63 | int |
| 57 | usage(int err) | 64 | usage(err) |
| 65 | int err; | ||
| 58 | { | 66 | { |
| 59 | fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n"); | 67 | fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n"); |
| 60 | fprintf(stdout, " update-game-score -h\n"); | 68 | fprintf(stdout, " update-game-score -h\n"); |
| @@ -66,9 +74,9 @@ usage(int err) | |||
| 66 | } | 74 | } |
| 67 | 75 | ||
| 68 | int | 76 | int |
| 69 | lock_file(const char *filename, void **state); | 77 | lock_file P_((const char *filename, void **state)); |
| 70 | int | 78 | int |
| 71 | unlock_file(const char *filename, void *state); | 79 | unlock_file P_((const char *filename, void *state)); |
| 72 | 80 | ||
| 73 | struct score_entry | 81 | struct score_entry |
| 74 | { | 82 | { |
| @@ -78,27 +86,35 @@ struct score_entry | |||
| 78 | }; | 86 | }; |
| 79 | 87 | ||
| 80 | int | 88 | int |
| 81 | read_scores(const char *filename, struct score_entry **scores, | 89 | read_scores P_((const char *filename, struct score_entry **scores, |
| 82 | int *count); | 90 | int *count)); |
| 83 | int | 91 | int |
| 84 | push_score(struct score_entry **scores, int *count, | 92 | push_score P_((struct score_entry **scores, int *count, |
| 85 | int newscore, char *username, char *newdata); | 93 | int newscore, char *username, char *newdata)); |
| 86 | void | 94 | void |
| 87 | sort_scores(struct score_entry *scores, int count, int reverse); | 95 | sort_scores P_((struct score_entry *scores, int count, int reverse)); |
| 88 | int | 96 | int |
| 89 | write_scores(const char *filename, const struct score_entry *scores, | 97 | write_scores P_((const char *filename, const struct score_entry *scores, |
| 90 | int count); | 98 | int count)); |
| 99 | |||
| 100 | void lose P_((const char *msg)) | ||
| 101 | __attribute__ ((noreturn)); | ||
| 102 | |||
| 103 | void lose(msg) | ||
| 104 | const char *msg; | ||
| 105 | { | ||
| 106 | fprintf(stderr, "%s\n", msg); | ||
| 107 | exit(1); | ||
| 108 | } | ||
| 91 | 109 | ||
| 92 | void lose(const char *msg, ...) | 110 | void lose_syserr P_((const char *msg)) |
| 93 | __attribute__ ((format (printf,1,0), noreturn)); | 111 | __attribute__ ((noreturn)); |
| 94 | 112 | ||
| 95 | void lose(const char *msg, ...) | 113 | void lose_syserr(msg) |
| 114 | const char *msg; | ||
| 96 | { | 115 | { |
| 97 | va_list ap; | 116 | fprintf(stderr, "%s: %s\n", msg, strerror(errno)); |
| 98 | va_start(ap, msg); | 117 | exit(1); |
| 99 | vfprintf(stderr, msg, ap); | ||
| 100 | va_end(ap); | ||
| 101 | exit(1); | ||
| 102 | } | 118 | } |
| 103 | 119 | ||
| 104 | char * | 120 | char * |
| @@ -123,23 +139,27 @@ get_user_id(void) | |||
| 123 | } | 139 | } |
| 124 | 140 | ||
| 125 | char * | 141 | char * |
| 126 | get_prefix(int running_suid, char *user_prefix) | 142 | get_prefix(running_suid, user_prefix) |
| 143 | int running_suid; | ||
| 144 | char *user_prefix; | ||
| 127 | { | 145 | { |
| 128 | if (!running_suid && user_prefix == NULL) | 146 | if (!running_suid && user_prefix == NULL) |
| 129 | lose("Not using a shared game directory, and no prefix given.\n"); | 147 | lose("Not using a shared game directory, and no prefix given."); |
| 130 | if (running_suid) | 148 | if (running_suid) |
| 131 | { | 149 | { |
| 132 | #ifdef HAVE_SHARED_GAME_DIR | 150 | #ifdef HAVE_SHARED_GAME_DIR |
| 133 | return HAVE_SHARED_GAME_DIR; | 151 | return HAVE_SHARED_GAME_DIR; |
| 134 | #else | 152 | #else |
| 135 | lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.\n"); | 153 | lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid."); |
| 136 | #endif | 154 | #endif |
| 137 | } | 155 | } |
| 138 | return user_prefix; | 156 | return user_prefix; |
| 139 | } | 157 | } |
| 140 | 158 | ||
| 141 | int | 159 | int |
| 142 | main(int argc, char **argv) | 160 | main(argc, argv) |
| 161 | int argc; | ||
| 162 | char **argv; | ||
| 143 | { | 163 | { |
| 144 | int c, running_suid; | 164 | int c, running_suid; |
| 145 | void *lockstate; | 165 | void *lockstate; |
| @@ -181,7 +201,7 @@ main(int argc, char **argv) | |||
| 181 | 201 | ||
| 182 | scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2); | 202 | scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2); |
| 183 | if (!scorefile) | 203 | if (!scorefile) |
| 184 | lose("Couldn't create score file name: %s\n", strerror(errno)); | 204 | lose_syserr("Couldn't allocate score file"); |
| 185 | 205 | ||
| 186 | strcpy(scorefile, prefix); | 206 | strcpy(scorefile, prefix); |
| 187 | strcat(scorefile, "/"); | 207 | strcat(scorefile, "/"); |
| @@ -192,19 +212,18 @@ main(int argc, char **argv) | |||
| 192 | newdata[MAX_DATA_LEN] = '\0'; | 212 | newdata[MAX_DATA_LEN] = '\0'; |
| 193 | 213 | ||
| 194 | if ((user_id = get_user_id()) == NULL) | 214 | if ((user_id = get_user_id()) == NULL) |
| 195 | lose("Couldn't determine user id: %s\n", strerror(errno)); | 215 | lose_syserr("Couldn't determine user id"); |
| 196 | 216 | ||
| 197 | if (stat(scorefile, &buf) < 0) | 217 | if (stat(scorefile, &buf) < 0) |
| 198 | lose("Failed to access scores file \"%s\": %s\n", scorefile, | 218 | lose_syserr("Failed to access scores file"); |
| 199 | strerror(errno)); | 219 | |
| 200 | if (lock_file(scorefile, &lockstate) < 0) | 220 | if (lock_file(scorefile, &lockstate) < 0) |
| 201 | lose("Failed to lock scores file \"%s\": %s\n", | 221 | lose_syserr("Failed to lock scores file"); |
| 202 | scorefile, strerror(errno)); | 222 | |
| 203 | if (read_scores(scorefile, &scores, &scorecount) < 0) | 223 | if (read_scores(scorefile, &scores, &scorecount) < 0) |
| 204 | { | 224 | { |
| 205 | unlock_file(scorefile, lockstate); | 225 | unlock_file(scorefile, lockstate); |
| 206 | lose("Failed to read scores file \"%s\": %s\n", scorefile, | 226 | lose_syserr("Failed to read scores file"); |
| 207 | strerror(errno)); | ||
| 208 | } | 227 | } |
| 209 | push_score(&scores, &scorecount, newscore, user_id, newdata); | 228 | push_score(&scores, &scorecount, newscore, user_id, newdata); |
| 210 | /* Limit the number of scores. If we're using reverse sorting, then | 229 | /* Limit the number of scores. If we're using reverse sorting, then |
| @@ -219,15 +238,16 @@ main(int argc, char **argv) | |||
| 219 | if (write_scores(scorefile, scores, scorecount) < 0) | 238 | if (write_scores(scorefile, scores, scorecount) < 0) |
| 220 | { | 239 | { |
| 221 | unlock_file(scorefile, lockstate); | 240 | unlock_file(scorefile, lockstate); |
| 222 | lose("Failed to write scores file \"%s\": %s\n", scorefile, | 241 | lose_syserr("Failed to write scores file"); |
| 223 | strerror(errno)); | ||
| 224 | } | 242 | } |
| 225 | unlock_file(scorefile, lockstate); | 243 | unlock_file(scorefile, lockstate); |
| 226 | exit(0); | 244 | exit(0); |
| 227 | } | 245 | } |
| 228 | 246 | ||
| 229 | int | 247 | int |
| 230 | read_score(FILE *f, struct score_entry *score) | 248 | read_score(f, score) |
| 249 | FILE *f; | ||
| 250 | struct score_entry *score; | ||
| 231 | { | 251 | { |
| 232 | int c; | 252 | int c; |
| 233 | if (feof(f)) | 253 | if (feof(f)) |
| @@ -311,8 +331,10 @@ read_score(FILE *f, struct score_entry *score) | |||
| 311 | } | 331 | } |
| 312 | 332 | ||
| 313 | int | 333 | int |
| 314 | read_scores(const char *filename, struct score_entry **scores, | 334 | read_scores(filename, scores, count) |
| 315 | int *count) | 335 | const char *filename; |
| 336 | struct score_entry **scores; | ||
| 337 | int *count; | ||
| 316 | { | 338 | { |
| 317 | int readval, scorecount, cursize; | 339 | int readval, scorecount, cursize; |
| 318 | struct score_entry *ret; | 340 | struct score_entry *ret; |
| @@ -343,7 +365,9 @@ read_scores(const char *filename, struct score_entry **scores, | |||
| 343 | } | 365 | } |
| 344 | 366 | ||
| 345 | int | 367 | int |
| 346 | score_compare(const void *a, const void *b) | 368 | score_compare(a, b) |
| 369 | const void *a; | ||
| 370 | const void *b; | ||
| 347 | { | 371 | { |
| 348 | const struct score_entry *sa = (const struct score_entry *) a; | 372 | const struct score_entry *sa = (const struct score_entry *) a; |
| 349 | const struct score_entry *sb = (const struct score_entry *) b; | 373 | const struct score_entry *sb = (const struct score_entry *) b; |
| @@ -351,7 +375,9 @@ score_compare(const void *a, const void *b) | |||
| 351 | } | 375 | } |
| 352 | 376 | ||
| 353 | int | 377 | int |
| 354 | score_compare_reverse(const void *a, const void *b) | 378 | score_compare_reverse(a, b) |
| 379 | const void *a; | ||
| 380 | const void *b; | ||
| 355 | { | 381 | { |
| 356 | const struct score_entry *sa = (const struct score_entry *) a; | 382 | const struct score_entry *sa = (const struct score_entry *) a; |
| 357 | const struct score_entry *sb = (const struct score_entry *) b; | 383 | const struct score_entry *sb = (const struct score_entry *) b; |
| @@ -359,8 +385,11 @@ score_compare_reverse(const void *a, const void *b) | |||
| 359 | } | 385 | } |
| 360 | 386 | ||
| 361 | int | 387 | int |
| 362 | push_score(struct score_entry **scores, int *count, | 388 | push_score(scores, count, newscore, username, newdata) |
| 363 | int newscore, char *username, char *newdata) | 389 | struct score_entry **scores; |
| 390 | int *count; int newscore; | ||
| 391 | char *username; | ||
| 392 | char *newdata; | ||
| 364 | { | 393 | { |
| 365 | struct score_entry *newscores = realloc(*scores, | 394 | struct score_entry *newscores = realloc(*scores, |
| 366 | sizeof(struct score_entry) * ((*count) + 1)); | 395 | sizeof(struct score_entry) * ((*count) + 1)); |
| @@ -375,15 +404,20 @@ push_score(struct score_entry **scores, int *count, | |||
| 375 | } | 404 | } |
| 376 | 405 | ||
| 377 | void | 406 | void |
| 378 | sort_scores(struct score_entry *scores, int count, int reverse) | 407 | sort_scores(scores, count, reverse) |
| 408 | struct score_entry *scores; | ||
| 409 | int count; | ||
| 410 | int reverse; | ||
| 379 | { | 411 | { |
| 380 | qsort(scores, count, sizeof(struct score_entry), | 412 | qsort(scores, count, sizeof(struct score_entry), |
| 381 | reverse ? score_compare_reverse : score_compare); | 413 | reverse ? score_compare_reverse : score_compare); |
| 382 | } | 414 | } |
| 383 | 415 | ||
| 384 | int | 416 | int |
| 385 | write_scores(const char *filename, const struct score_entry *scores, | 417 | write_scores(filename, scores, count) |
| 386 | int count) | 418 | const char *filename; |
| 419 | const struct score_entry * scores; | ||
| 420 | int count; | ||
| 387 | { | 421 | { |
| 388 | FILE *f; | 422 | FILE *f; |
| 389 | int i; | 423 | int i; |
| @@ -410,9 +444,11 @@ write_scores(const char *filename, const struct score_entry *scores, | |||
| 410 | return -1; | 444 | return -1; |
| 411 | return 0; | 445 | return 0; |
| 412 | } | 446 | } |
| 413 | 447 | ||
| 414 | int | 448 | int |
| 415 | lock_file(const char *filename, void **state) | 449 | lock_file(filename, state) |
| 450 | const char *filename; | ||
| 451 | void **state; | ||
| 416 | { | 452 | { |
| 417 | int fd; | 453 | int fd; |
| 418 | struct stat buf; | 454 | struct stat buf; |
| @@ -450,9 +486,11 @@ lock_file(const char *filename, void **state) | |||
| 450 | close(fd); | 486 | close(fd); |
| 451 | return 0; | 487 | return 0; |
| 452 | } | 488 | } |
| 453 | 489 | ||
| 454 | int | 490 | int |
| 455 | unlock_file(const char *filename, void *state) | 491 | unlock_file(filename, state) |
| 492 | const char *filename; | ||
| 493 | void *state; | ||
| 456 | { | 494 | { |
| 457 | char *lockpath = (char *) state; | 495 | char *lockpath = (char *) state; |
| 458 | int ret = unlink(lockpath); | 496 | int ret = unlink(lockpath); |