1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-10 00:00:39 -08:00

(P_): New macro. Use it for all prototypes.

(lose): Don't use varargs.
(lose_syserr): New function.

Change all functions to K&R style.
This commit is contained in:
Colin Walters 2002-08-01 01:31:44 +00:00
parent 5fba5c216b
commit cf398788c7

View file

@ -53,8 +53,16 @@ Boston, MA 02111-1307, USA. */
#define __attribute__(x) #define __attribute__(x)
#endif #endif
/* Declare the prototype for a general external function. */
#if defined (PROTOTYPES) || defined (WINDOWSNT)
#define P_(proto) proto
#else
#define P_(proto) ()
#endif
int int
usage(int err) usage(err)
int err;
{ {
fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n"); fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
fprintf(stdout, " update-game-score -h\n"); fprintf(stdout, " update-game-score -h\n");
@ -66,9 +74,9 @@ usage(int err)
} }
int int
lock_file(const char *filename, void **state); lock_file P_((const char *filename, void **state));
int int
unlock_file(const char *filename, void *state); unlock_file P_((const char *filename, void *state));
struct score_entry struct score_entry
{ {
@ -78,26 +86,34 @@ struct score_entry
}; };
int int
read_scores(const char *filename, struct score_entry **scores, read_scores P_((const char *filename, struct score_entry **scores,
int *count); int *count));
int int
push_score(struct score_entry **scores, int *count, push_score P_((struct score_entry **scores, int *count,
int newscore, char *username, char *newdata); int newscore, char *username, char *newdata));
void void
sort_scores(struct score_entry *scores, int count, int reverse); sort_scores P_((struct score_entry *scores, int count, int reverse));
int int
write_scores(const char *filename, const struct score_entry *scores, write_scores P_((const char *filename, const struct score_entry *scores,
int count); int count));
void lose(const char *msg, ...) void lose P_((const char *msg))
__attribute__ ((format (printf,1,0), noreturn)); __attribute__ ((noreturn));
void lose(const char *msg, ...) void lose(msg)
const char *msg;
{ {
va_list ap; fprintf(stderr, "%s\n", msg);
va_start(ap, msg); exit(1);
vfprintf(stderr, msg, ap); }
va_end(ap);
void lose_syserr P_((const char *msg))
__attribute__ ((noreturn));
void lose_syserr(msg)
const char *msg;
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(1); exit(1);
} }
@ -123,23 +139,27 @@ get_user_id(void)
} }
char * char *
get_prefix(int running_suid, char *user_prefix) get_prefix(running_suid, user_prefix)
int running_suid;
char *user_prefix;
{ {
if (!running_suid && user_prefix == NULL) if (!running_suid && user_prefix == NULL)
lose("Not using a shared game directory, and no prefix given.\n"); lose("Not using a shared game directory, and no prefix given.");
if (running_suid) if (running_suid)
{ {
#ifdef HAVE_SHARED_GAME_DIR #ifdef HAVE_SHARED_GAME_DIR
return HAVE_SHARED_GAME_DIR; return HAVE_SHARED_GAME_DIR;
#else #else
lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.\n"); lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
#endif #endif
} }
return user_prefix; return user_prefix;
} }
int int
main(int argc, char **argv) main(argc, argv)
int argc;
char **argv;
{ {
int c, running_suid; int c, running_suid;
void *lockstate; void *lockstate;
@ -181,7 +201,7 @@ main(int argc, char **argv)
scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2); scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2);
if (!scorefile) if (!scorefile)
lose("Couldn't create score file name: %s\n", strerror(errno)); lose_syserr("Couldn't allocate score file");
strcpy(scorefile, prefix); strcpy(scorefile, prefix);
strcat(scorefile, "/"); strcat(scorefile, "/");
@ -192,19 +212,18 @@ main(int argc, char **argv)
newdata[MAX_DATA_LEN] = '\0'; newdata[MAX_DATA_LEN] = '\0';
if ((user_id = get_user_id()) == NULL) if ((user_id = get_user_id()) == NULL)
lose("Couldn't determine user id: %s\n", strerror(errno)); lose_syserr("Couldn't determine user id");
if (stat(scorefile, &buf) < 0) if (stat(scorefile, &buf) < 0)
lose("Failed to access scores file \"%s\": %s\n", scorefile, lose_syserr("Failed to access scores file");
strerror(errno));
if (lock_file(scorefile, &lockstate) < 0) if (lock_file(scorefile, &lockstate) < 0)
lose("Failed to lock scores file \"%s\": %s\n", lose_syserr("Failed to lock scores file");
scorefile, strerror(errno));
if (read_scores(scorefile, &scores, &scorecount) < 0) if (read_scores(scorefile, &scores, &scorecount) < 0)
{ {
unlock_file(scorefile, lockstate); unlock_file(scorefile, lockstate);
lose("Failed to read scores file \"%s\": %s\n", scorefile, lose_syserr("Failed to read scores file");
strerror(errno));
} }
push_score(&scores, &scorecount, newscore, user_id, newdata); push_score(&scores, &scorecount, newscore, user_id, newdata);
/* Limit the number of scores. If we're using reverse sorting, then /* Limit the number of scores. If we're using reverse sorting, then
@ -219,15 +238,16 @@ main(int argc, char **argv)
if (write_scores(scorefile, scores, scorecount) < 0) if (write_scores(scorefile, scores, scorecount) < 0)
{ {
unlock_file(scorefile, lockstate); unlock_file(scorefile, lockstate);
lose("Failed to write scores file \"%s\": %s\n", scorefile, lose_syserr("Failed to write scores file");
strerror(errno));
} }
unlock_file(scorefile, lockstate); unlock_file(scorefile, lockstate);
exit(0); exit(0);
} }
int int
read_score(FILE *f, struct score_entry *score) read_score(f, score)
FILE *f;
struct score_entry *score;
{ {
int c; int c;
if (feof(f)) if (feof(f))
@ -311,8 +331,10 @@ read_score(FILE *f, struct score_entry *score)
} }
int int
read_scores(const char *filename, struct score_entry **scores, read_scores(filename, scores, count)
int *count) const char *filename;
struct score_entry **scores;
int *count;
{ {
int readval, scorecount, cursize; int readval, scorecount, cursize;
struct score_entry *ret; struct score_entry *ret;
@ -343,7 +365,9 @@ read_scores(const char *filename, struct score_entry **scores,
} }
int int
score_compare(const void *a, const void *b) score_compare(a, b)
const void *a;
const void *b;
{ {
const struct score_entry *sa = (const struct score_entry *) a; const struct score_entry *sa = (const struct score_entry *) a;
const struct score_entry *sb = (const struct score_entry *) b; const struct score_entry *sb = (const struct score_entry *) b;
@ -351,7 +375,9 @@ score_compare(const void *a, const void *b)
} }
int int
score_compare_reverse(const void *a, const void *b) score_compare_reverse(a, b)
const void *a;
const void *b;
{ {
const struct score_entry *sa = (const struct score_entry *) a; const struct score_entry *sa = (const struct score_entry *) a;
const struct score_entry *sb = (const struct score_entry *) b; const struct score_entry *sb = (const struct score_entry *) b;
@ -359,8 +385,11 @@ score_compare_reverse(const void *a, const void *b)
} }
int int
push_score(struct score_entry **scores, int *count, push_score(scores, count, newscore, username, newdata)
int newscore, char *username, char *newdata) struct score_entry **scores;
int *count; int newscore;
char *username;
char *newdata;
{ {
struct score_entry *newscores = realloc(*scores, struct score_entry *newscores = realloc(*scores,
sizeof(struct score_entry) * ((*count) + 1)); sizeof(struct score_entry) * ((*count) + 1));
@ -375,15 +404,20 @@ push_score(struct score_entry **scores, int *count,
} }
void void
sort_scores(struct score_entry *scores, int count, int reverse) sort_scores(scores, count, reverse)
struct score_entry *scores;
int count;
int reverse;
{ {
qsort(scores, count, sizeof(struct score_entry), qsort(scores, count, sizeof(struct score_entry),
reverse ? score_compare_reverse : score_compare); reverse ? score_compare_reverse : score_compare);
} }
int int
write_scores(const char *filename, const struct score_entry *scores, write_scores(filename, scores, count)
int count) const char *filename;
const struct score_entry * scores;
int count;
{ {
FILE *f; FILE *f;
int i; int i;
@ -412,7 +446,9 @@ write_scores(const char *filename, const struct score_entry *scores,
} }
int int
lock_file(const char *filename, void **state) lock_file(filename, state)
const char *filename;
void **state;
{ {
int fd; int fd;
struct stat buf; struct stat buf;
@ -452,7 +488,9 @@ lock_file(const char *filename, void **state)
} }
int int
unlock_file(const char *filename, void *state) unlock_file(filename, state)
const char *filename;
void *state;
{ {
char *lockpath = (char *) state; char *lockpath = (char *) state;
int ret = unlink(lockpath); int ret = unlink(lockpath);