aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorColin Walters2002-08-01 01:31:44 +0000
committerColin Walters2002-08-01 01:31:44 +0000
commitcf398788c7bc6018e00e5b1134457d449138c98c (patch)
treec047d439a808a4609014b2888bd1f6ce3df90917 /lib-src
parent5fba5c216bfc0c83b64859e0145ad03a8a11d06a (diff)
downloademacs-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.c130
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
56int 63int
57usage(int err) 64usage(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
68int 76int
69lock_file(const char *filename, void **state); 77lock_file P_((const char *filename, void **state));
70int 78int
71unlock_file(const char *filename, void *state); 79unlock_file P_((const char *filename, void *state));
72 80
73struct score_entry 81struct score_entry
74{ 82{
@@ -78,27 +86,35 @@ struct score_entry
78}; 86};
79 87
80int 88int
81read_scores(const char *filename, struct score_entry **scores, 89read_scores P_((const char *filename, struct score_entry **scores,
82 int *count); 90 int *count));
83int 91int
84push_score(struct score_entry **scores, int *count, 92push_score P_((struct score_entry **scores, int *count,
85 int newscore, char *username, char *newdata); 93 int newscore, char *username, char *newdata));
86void 94void
87sort_scores(struct score_entry *scores, int count, int reverse); 95sort_scores P_((struct score_entry *scores, int count, int reverse));
88int 96int
89write_scores(const char *filename, const struct score_entry *scores, 97write_scores P_((const char *filename, const struct score_entry *scores,
90 int count); 98 int count));
99
100void lose P_((const char *msg))
101 __attribute__ ((noreturn));
102
103void lose(msg)
104 const char *msg;
105{
106 fprintf(stderr, "%s\n", msg);
107 exit(1);
108}
91 109
92void lose(const char *msg, ...) 110void lose_syserr P_((const char *msg))
93 __attribute__ ((format (printf,1,0), noreturn)); 111 __attribute__ ((noreturn));
94 112
95void lose(const char *msg, ...) 113void 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
104char * 120char *
@@ -123,23 +139,27 @@ get_user_id(void)
123} 139}
124 140
125char * 141char *
126get_prefix(int running_suid, char *user_prefix) 142get_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
141int 159int
142main(int argc, char **argv) 160main(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
229int 247int
230read_score(FILE *f, struct score_entry *score) 248read_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
313int 333int
314read_scores(const char *filename, struct score_entry **scores, 334read_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
345int 367int
346score_compare(const void *a, const void *b) 368score_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
353int 377int
354score_compare_reverse(const void *a, const void *b) 378score_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
361int 387int
362push_score(struct score_entry **scores, int *count, 388push_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
377void 406void
378sort_scores(struct score_entry *scores, int count, int reverse) 407sort_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
384int 416int
385write_scores(const char *filename, const struct score_entry *scores, 417write_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
414int 448int
415lock_file(const char *filename, void **state) 449lock_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
454int 490int
455unlock_file(const char *filename, void *state) 491unlock_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);