diff options
| author | Colin Walters | 2002-04-07 05:46:15 +0000 |
|---|---|---|
| committer | Colin Walters | 2002-04-07 05:46:15 +0000 |
| commit | 5795b42089cafe8d25f248cecc31a6032bf99121 (patch) | |
| tree | 3f3ffb18cd97654c1281f8abb3c4082a3f3d7f75 /lib-src | |
| parent | 596d02bc1b2078ed61c83cf1c1db0ddabd179755 (diff) | |
| download | emacs-5795b42089cafe8d25f248cecc31a6032bf99121.tar.gz emacs-5795b42089cafe8d25f248cecc31a6032bf99121.zip | |
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.
Diffstat (limited to 'lib-src')
| -rw-r--r-- | lib-src/ChangeLog | 9 | ||||
| -rw-r--r-- | lib-src/update-game-score.c | 46 |
2 files changed, 46 insertions, 9 deletions
diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog index 605fa235f5f..7134f4cdf80 100644 --- a/lib-src/ChangeLog +++ b/lib-src/ChangeLog | |||
| @@ -1,3 +1,12 @@ | |||
| 1 | 2002-04-07 Colin Walters <walters@verbum.org> | ||
| 2 | |||
| 3 | * update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode. | ||
| 4 | (get_user_id): Take struct passwd as an argument. | ||
| 5 | (get_home_dir): New function. | ||
| 6 | (main): Read in user information here. Discover home directory if | ||
| 7 | necessary. | ||
| 8 | (read_score): Trim newline only in `getline' case. | ||
| 9 | |||
| 1 | 2002-04-05 Colin Walters <walters@debian.org> | 10 | 2002-04-05 Colin Walters <walters@debian.org> |
| 2 | 11 | ||
| 3 | * update-game-score.c (toplevel): Include pwd.h. | 12 | * update-game-score.c (toplevel): Include pwd.h. |
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c index 084b54fdb32..0e257e057f5 100644 --- a/lib-src/update-game-score.c +++ b/lib-src/update-game-score.c | |||
| @@ -40,7 +40,12 @@ Boston, MA 02111-1307, USA. */ | |||
| 40 | #include <config.h> | 40 | #include <config.h> |
| 41 | 41 | ||
| 42 | #define MAX_ATTEMPTS 5 | 42 | #define MAX_ATTEMPTS 5 |
| 43 | #define SCORE_FILE_PREFIX "/var/games/emacs/" | 43 | |
| 44 | #ifdef HAVE_SHARED_GAME_DIR | ||
| 45 | #define SCORE_FILE_PREFIX HAVE_SHARED_GAME_DIR | ||
| 46 | #else | ||
| 47 | #define SCORE_FILE_PREFIX "$HOME" | ||
| 48 | #endif | ||
| 44 | 49 | ||
| 45 | int | 50 | int |
| 46 | usage(int err) | 51 | usage(int err) |
| @@ -78,10 +83,9 @@ write_scores(const char *filename, const struct score_entry *scores, | |||
| 78 | int count); | 83 | int count); |
| 79 | 84 | ||
| 80 | char * | 85 | char * |
| 81 | get_user_id() | 86 | get_user_id(struct passwd *buf) |
| 82 | { | 87 | { |
| 83 | char *name; | 88 | char *name; |
| 84 | struct passwd *buf = getpwuid(getuid()); | ||
| 85 | if (!buf) | 89 | if (!buf) |
| 86 | { | 90 | { |
| 87 | int count = 1; | 91 | int count = 1; |
| @@ -95,16 +99,25 @@ get_user_id() | |||
| 95 | return buf->pw_name; | 99 | return buf->pw_name; |
| 96 | } | 100 | } |
| 97 | 101 | ||
| 102 | char * | ||
| 103 | get_home_dir(struct passwd *buf) | ||
| 104 | { | ||
| 105 | if (!buf) | ||
| 106 | return NULL; | ||
| 107 | return buf->pw_dir; | ||
| 108 | } | ||
| 109 | |||
| 98 | int | 110 | int |
| 99 | main(int argc, char **argv) | 111 | main(int argc, char **argv) |
| 100 | { | 112 | { |
| 101 | int c; | 113 | int c; |
| 102 | void *lockstate; | 114 | void *lockstate; |
| 103 | char *scorefile; | 115 | char *scorefile, *prefix; |
| 104 | struct stat buf; | 116 | struct stat buf; |
| 105 | struct score_entry *scores; | 117 | struct score_entry *scores; |
| 106 | int newscore, scorecount, reverse = 0, max = -1; | 118 | int newscore, scorecount, reverse = 0, max = -1; |
| 107 | char *newdata; | 119 | char *newdata; |
| 120 | struct passwd *passwdbuf; | ||
| 108 | 121 | ||
| 109 | srand(time(0)); | 122 | srand(time(0)); |
| 110 | 123 | ||
| @@ -126,14 +139,29 @@ main(int argc, char **argv) | |||
| 126 | 139 | ||
| 127 | if (optind+3 != argc) | 140 | if (optind+3 != argc) |
| 128 | usage(1); | 141 | usage(1); |
| 129 | scorefile = malloc(strlen(SCORE_FILE_PREFIX) + strlen(argv[optind]) + 1); | 142 | |
| 143 | passwdbuf = getpwuid(getuid()); | ||
| 144 | |||
| 145 | if (!strcmp(SCORE_FILE_PREFIX, "$HOME")) | ||
| 146 | { | ||
| 147 | prefix = get_home_dir(passwdbuf); | ||
| 148 | if (!prefix) | ||
| 149 | { | ||
| 150 | fprintf(stderr, "Unable to determine home directory\n"); | ||
| 151 | exit(1); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | else | ||
| 155 | prefix = SCORE_FILE_PREFIX; | ||
| 156 | |||
| 157 | scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 1); | ||
| 130 | if (!scorefile) | 158 | if (!scorefile) |
| 131 | { | 159 | { |
| 132 | fprintf(stderr, "Couldn't create score file name: %s\n", | 160 | fprintf(stderr, "Couldn't create score file name: %s\n", |
| 133 | strerror(errno)); | 161 | strerror(errno)); |
| 134 | goto fail; | 162 | goto fail; |
| 135 | } | 163 | } |
| 136 | strcpy(scorefile, SCORE_FILE_PREFIX); | 164 | strcpy(scorefile, prefix); |
| 137 | strcat(scorefile, argv[optind]); | 165 | strcat(scorefile, argv[optind]); |
| 138 | newscore = atoi(argv[optind+1]); | 166 | newscore = atoi(argv[optind+1]); |
| 139 | newdata = argv[optind+2]; | 167 | newdata = argv[optind+2]; |
| @@ -156,7 +184,7 @@ main(int argc, char **argv) | |||
| 156 | scorefile, strerror(errno)); | 184 | scorefile, strerror(errno)); |
| 157 | goto fail_unlock; | 185 | goto fail_unlock; |
| 158 | } | 186 | } |
| 159 | push_score(&scores, &scorecount, newscore, get_user_id(), newdata); | 187 | push_score(&scores, &scorecount, newscore, get_user_id(passwdbuf), newdata); |
| 160 | sort_scores(scores, scorecount, reverse); | 188 | sort_scores(scores, scorecount, reverse); |
| 161 | if (write_scores(scorefile, scores, scorecount) < 0) | 189 | if (write_scores(scorefile, scores, scorecount) < 0) |
| 162 | { | 190 | { |
| @@ -229,6 +257,7 @@ read_score(FILE *f, struct score_entry *score) | |||
| 229 | int len; | 257 | int len; |
| 230 | if (getline(&score->data, &len, f) < 0) | 258 | if (getline(&score->data, &len, f) < 0) |
| 231 | return -1; | 259 | return -1; |
| 260 | score->data[strlen(score->data)-1] = '\0'; | ||
| 232 | } | 261 | } |
| 233 | #else | 262 | #else |
| 234 | { | 263 | { |
| @@ -249,10 +278,9 @@ read_score(FILE *f, struct score_entry *score) | |||
| 249 | cur++; | 278 | cur++; |
| 250 | } | 279 | } |
| 251 | score->data = buf; | 280 | score->data = buf; |
| 281 | score->data[cur] = '\0'; | ||
| 252 | } | 282 | } |
| 253 | #endif | 283 | #endif |
| 254 | /* Trim the newline */ | ||
| 255 | score->data[strlen(score->data)-1] = '\0'; | ||
| 256 | return 0; | 284 | return 0; |
| 257 | } | 285 | } |
| 258 | 286 | ||