#define MAX_SCORES 200
#define MAX_DATA_LEN 1024
-#ifdef HAVE_SHARED_GAME_DIR
-#define SCORE_FILE_PREFIX HAVE_SHARED_GAME_DIR
-#else
-#define SCORE_FILE_PREFIX "~/.emacs.d/games"
-#endif
-
#if !defined (__GNUC__) || __GNUC__ < 2
#define __attribute__(x)
#endif
+/* Declare the prototype for a general external function. */
+#if defined (PROTOTYPES) || defined (WINDOWSNT)
+#define P_(proto) proto
+#else
+#define P_(proto) ()
+#endif
+
int
-usage(int err)
+usage(err)
+ int err;
{
fprintf(stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
fprintf(stdout, " update-game-score -h\n");
fprintf(stdout, " -h\t\tDisplay this help.\n");
fprintf(stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
fprintf(stdout, " -r\t\tSort the scores in increasing order.\n");
+ fprintf(stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
exit(err);
}
int
-lock_file(const char *filename, void **state);
+lock_file P_((const char *filename, void **state));
int
-unlock_file(const char *filename, void *state);
+unlock_file P_((const char *filename, void *state));
struct score_entry
{
};
int
-read_scores(const char *filename, struct score_entry **scores,
- int *count);
+read_scores P_((const char *filename, struct score_entry **scores,
+ int *count));
int
-push_score(struct score_entry **scores, int *count,
- int newscore, char *username, char *newdata);
+push_score P_((struct score_entry **scores, int *count,
+ int newscore, char *username, char *newdata));
void
-sort_scores(struct score_entry *scores, int count, int reverse);
+sort_scores P_((struct score_entry *scores, int count, int reverse));
int
-write_scores(const char *filename, const struct score_entry *scores,
- int count);
+write_scores P_((const char *filename, const struct score_entry *scores,
+ int count));
+
+void lose P_((const char *msg))
+ __attribute__ ((noreturn));
+
+void lose(msg)
+ const char *msg;
+{
+ fprintf(stderr, "%s\n", msg);
+ exit(1);
+}
+
+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);
+}
char *
-get_user_id(struct passwd *buf)
+get_user_id(void)
{
char *name;
+ struct passwd *buf = getpwuid(getuid());
if (!buf)
{
int count = 1;
while (tuid /= 10)
count++;
name = malloc(count+1);
+ if (!name)
+ return NULL;
sprintf(name, "%d", uid);
return name;
}
}
char *
-get_home_dir(struct passwd *buf)
-{
- if (!buf)
- return NULL;
- return buf->pw_dir;
-}
-
-void lose(const char *msg, ...)
- __attribute__ ((format (printf,1,0), noreturn));
-
-void lose(const char *msg, ...)
+get_prefix(running_suid, user_prefix)
+ int running_suid;
+ char *user_prefix;
{
- va_list ap;
- va_start(ap, msg);
- vfprintf(stderr, msg, ap);
- va_end(ap);
- exit(1);
+ if (!running_suid && user_prefix == NULL)
+ lose("Not using a shared game directory, and no prefix given.");
+ if (running_suid)
+ {
+#ifdef HAVE_SHARED_GAME_DIR
+ return HAVE_SHARED_GAME_DIR;
+#else
+ lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
+#endif
+ }
+ return user_prefix;
}
int
-main(int argc, char **argv)
+main(argc, argv)
+ int argc;
+ char **argv;
{
- int c;
+ int c, running_suid;
void *lockstate;
- char *scorefile, *prefix;
+ char *user_id, *scorefile, *prefix, *user_prefix = NULL;
struct stat buf;
struct score_entry *scores;
int newscore, scorecount, reverse = 0, max = MAX_SCORES;
char *newdata;
- struct passwd *passwdbuf;
srand(time(0));
- while ((c = getopt(argc, argv, "hrm:")) != -1)
+ while ((c = getopt(argc, argv, "hrm:d:")) != -1)
switch (c)
{
case 'h':
usage(0);
break;
+ case 'd':
+ user_prefix = optarg;
+ break;
case 'r':
reverse = 1;
break;
if (optind+3 != argc)
usage(1);
- passwdbuf = getpwuid(getuid());
-
- if (!strncmp(SCORE_FILE_PREFIX, "~", 1))
- {
- char *homedir = get_home_dir(passwdbuf);
- if (!homedir)
- lose("Unable to determine home directory\n");
- prefix = malloc(strlen(homedir) + strlen(SCORE_FILE_PREFIX) + 1);
- strcpy(prefix, homedir);
- /* Skip over the '~'. */
- strcat(prefix, SCORE_FILE_PREFIX+1);
- }
- else
- prefix = strdup(SCORE_FILE_PREFIX);
+ running_suid = (getuid() != geteuid());
- if (!prefix)
- lose("Couldn't create score file name: %s\n", strerror(errno));
+ prefix = get_prefix(running_suid, user_prefix);
scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2);
if (!scorefile)
- lose("Couldn't create score file name: %s\n", strerror(errno));
+ lose_syserr("Couldn't allocate score file");
strcpy(scorefile, prefix);
- free(prefix);
strcat(scorefile, "/");
strcat(scorefile, argv[optind]);
newscore = atoi(argv[optind+1]);
newdata = argv[optind+2];
if (strlen(newdata) > MAX_DATA_LEN)
newdata[MAX_DATA_LEN] = '\0';
+
+ if ((user_id = get_user_id()) == NULL)
+ lose_syserr("Couldn't determine user id");
if (stat(scorefile, &buf) < 0)
- lose("Failed to access scores file \"%s\": %s\n", scorefile,
- strerror(errno));
+ lose_syserr("Failed to access scores file");
+
if (lock_file(scorefile, &lockstate) < 0)
- lose("Failed to lock scores file \"%s\": %s\n",
- scorefile, strerror(errno));
+ lose_syserr("Failed to lock scores file");
+
if (read_scores(scorefile, &scores, &scorecount) < 0)
{
unlock_file(scorefile, lockstate);
- lose("Failed to read scores file \"%s\": %s\n", scorefile,
- strerror(errno));
+ lose_syserr("Failed to read scores file");
}
- push_score(&scores, &scorecount, newscore, get_user_id(passwdbuf), newdata);
+ push_score(&scores, &scorecount, newscore, user_id, newdata);
/* Limit the number of scores. If we're using reverse sorting, then
we should increment the beginning of the array, to skip over the
*smallest* scores. Otherwise, we just decrement the number of
if (write_scores(scorefile, scores, scorecount) < 0)
{
unlock_file(scorefile, lockstate);
- lose("Failed to write scores file \"%s\": %s\n", scorefile,
- strerror(errno));
+ lose_syserr("Failed to write scores file");
}
unlock_file(scorefile, lockstate);
exit(0);
}
int
-read_score(FILE *f, struct score_entry *score)
+read_score(f, score)
+ FILE *f;
+ struct score_entry *score;
{
int c;
if (feof(f))
}
int
-read_scores(const char *filename, struct score_entry **scores,
- int *count)
+read_scores(filename, scores, count)
+ const char *filename;
+ struct score_entry **scores;
+ int *count;
{
int readval, scorecount, cursize;
struct score_entry *ret;
}
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 *sb = (const struct score_entry *) b;
}
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 *sb = (const struct score_entry *) b;
}
int
-push_score(struct score_entry **scores, int *count,
- int newscore, char *username, char *newdata)
+push_score(scores, count, newscore, username, newdata)
+ struct score_entry **scores;
+ int *count; int newscore;
+ char *username;
+ char *newdata;
{
struct score_entry *newscores = realloc(*scores,
sizeof(struct score_entry) * ((*count) + 1));
}
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),
reverse ? score_compare_reverse : score_compare);
}
int
-write_scores(const char *filename, const struct score_entry *scores,
- int count)
+write_scores(filename, scores, count)
+ const char *filename;
+ const struct score_entry * scores;
+ int count;
{
FILE *f;
int i;
return -1;
return 0;
}
-
+
int
-lock_file(const char *filename, void **state)
+lock_file(filename, state)
+ const char *filename;
+ void **state;
{
int fd;
struct stat buf;
close(fd);
return 0;
}
-
+
int
-unlock_file(const char *filename, void *state)
+unlock_file(filename, state)
+ const char *filename;
+ void *state;
{
char *lockpath = (char *) state;
int ret = unlink(lockpath);