+2002-04-07 Colin Walters <walters@verbum.org>
+
+ * update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
+ (get_user_id): Take struct passwd as an argument.
+ (get_home_dir): New function.
+ (main): Read in user information here. Discover home directory if
+ necessary.
+ (read_score): Trim newline only in `getline' case.
+
2002-04-05 Colin Walters <walters@debian.org>
* update-game-score.c (toplevel): Include pwd.h.
#include <config.h>
#define MAX_ATTEMPTS 5
-#define SCORE_FILE_PREFIX "/var/games/emacs/"
+
+#ifdef HAVE_SHARED_GAME_DIR
+#define SCORE_FILE_PREFIX HAVE_SHARED_GAME_DIR
+#else
+#define SCORE_FILE_PREFIX "$HOME"
+#endif
int
usage(int err)
int count);
char *
-get_user_id()
+get_user_id(struct passwd *buf)
{
char *name;
- struct passwd *buf = getpwuid(getuid());
if (!buf)
{
int count = 1;
return buf->pw_name;
}
+char *
+get_home_dir(struct passwd *buf)
+{
+ if (!buf)
+ return NULL;
+ return buf->pw_dir;
+}
+
int
main(int argc, char **argv)
{
int c;
void *lockstate;
- char *scorefile;
+ char *scorefile, *prefix;
struct stat buf;
struct score_entry *scores;
int newscore, scorecount, reverse = 0, max = -1;
char *newdata;
+ struct passwd *passwdbuf;
srand(time(0));
if (optind+3 != argc)
usage(1);
- scorefile = malloc(strlen(SCORE_FILE_PREFIX) + strlen(argv[optind]) + 1);
+
+ passwdbuf = getpwuid(getuid());
+
+ if (!strcmp(SCORE_FILE_PREFIX, "$HOME"))
+ {
+ prefix = get_home_dir(passwdbuf);
+ if (!prefix)
+ {
+ fprintf(stderr, "Unable to determine home directory\n");
+ exit(1);
+ }
+ }
+ else
+ prefix = SCORE_FILE_PREFIX;
+
+ scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 1);
if (!scorefile)
{
fprintf(stderr, "Couldn't create score file name: %s\n",
strerror(errno));
goto fail;
}
- strcpy(scorefile, SCORE_FILE_PREFIX);
+ strcpy(scorefile, prefix);
strcat(scorefile, argv[optind]);
newscore = atoi(argv[optind+1]);
newdata = argv[optind+2];
scorefile, strerror(errno));
goto fail_unlock;
}
- push_score(&scores, &scorecount, newscore, get_user_id(), newdata);
+ push_score(&scores, &scorecount, newscore, get_user_id(passwdbuf), newdata);
sort_scores(scores, scorecount, reverse);
if (write_scores(scorefile, scores, scorecount) < 0)
{
int len;
if (getline(&score->data, &len, f) < 0)
return -1;
+ score->data[strlen(score->data)-1] = '\0';
}
#else
{
cur++;
}
score->data = buf;
+ score->data[cur] = '\0';
}
#endif
- /* Trim the newline */
- score->data[strlen(score->data)-1] = '\0';
return 0;
}