diff options
| author | Colin Walters | 2002-04-05 08:58:12 +0000 |
|---|---|---|
| committer | Colin Walters | 2002-04-05 08:58:12 +0000 |
| commit | cd553ffbf3c5eae5d6edb492ff62ce3fb6e7e6b8 (patch) | |
| tree | df73b8dc737d34f58686545432a876bb9daa1b0c /lib-src | |
| parent | 368ab68fc90c5ba68d9f04939cb24d896d024e48 (diff) | |
| download | emacs-cd553ffbf3c5eae5d6edb492ff62ce3fb6e7e6b8.tar.gz emacs-cd553ffbf3c5eae5d6edb492ff62ce3fb6e7e6b8.zip | |
(toplevel): Include pwd.h.
(struct score_entry): Add username field.
(push_score): Use it.
(get_user_id): New function.
(main): Don't malloc excessively.
(main): Use username field.
(read_score): Read it.
(push_score): Handle it.
(write_scores) Write it.
(read_score): Handle arbitrary length data.
Diffstat (limited to 'lib-src')
| -rw-r--r-- | lib-src/ChangeLog | 17 | ||||
| -rw-r--r-- | lib-src/update-game-score.c | 98 |
2 files changed, 100 insertions, 15 deletions
diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog index 804ebb10933..605fa235f5f 100644 --- a/lib-src/ChangeLog +++ b/lib-src/ChangeLog | |||
| @@ -1,3 +1,16 @@ | |||
| 1 | 2002-04-05 Colin Walters <walters@debian.org> | ||
| 2 | |||
| 3 | * update-game-score.c (toplevel): Include pwd.h. | ||
| 4 | (struct score_entry): Add username field. | ||
| 5 | (push_score): Use it. | ||
| 6 | (get_user_id): New function. | ||
| 7 | (main): Don't malloc excessively. | ||
| 8 | (main): Use username field. | ||
| 9 | (read_score): Read it. | ||
| 10 | (push_score): Handle it. | ||
| 11 | (write_scores) Write it. | ||
| 12 | (read_score): Handle arbitrary length data. | ||
| 13 | |||
| 1 | 2002-03-30 Eli Zaretskii <eliz@is.elta.co.il> | 14 | 2002-03-30 Eli Zaretskii <eliz@is.elta.co.il> |
| 2 | 15 | ||
| 3 | * ebrowse.c (add_declarator): Fix the first call to add_member_defn. | 16 | * ebrowse.c (add_declarator): Fix the first call to add_member_defn. |
| @@ -11,6 +24,10 @@ | |||
| 11 | 24 | ||
| 12 | * makefile.w32-in (lisp): Move backquote.elc into emacs-lisp. | 25 | * makefile.w32-in (lisp): Move backquote.elc into emacs-lisp. |
| 13 | 26 | ||
| 27 | 2002-03-27 Colin Walters <walters@debian.org> | ||
| 28 | |||
| 29 | * update-game-score.c: New file. | ||
| 30 | |||
| 14 | 2002-03-22 Paul Eggert <eggert@twinsun.com> | 31 | 2002-03-22 Paul Eggert <eggert@twinsun.com> |
| 15 | 32 | ||
| 16 | * etags.c (main): Use `sort -o TAGFILE TAGFILE' instead of | 33 | * etags.c (main): Use `sort -o TAGFILE TAGFILE' instead of |
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c index 9cf4c4359dd..e713d732fef 100644 --- a/lib-src/update-game-score.c +++ b/lib-src/update-game-score.c | |||
| @@ -33,10 +33,11 @@ Boston, MA 02111-1307, USA. */ | |||
| 33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> | 34 | #include <stdio.h> |
| 35 | #include <time.h> | 35 | #include <time.h> |
| 36 | #include <pwd.h> | ||
| 36 | #include <ctype.h> | 37 | #include <ctype.h> |
| 37 | #include <fcntl.h> | 38 | #include <fcntl.h> |
| 38 | #include <sys/stat.h> | 39 | #include <sys/stat.h> |
| 39 | // #include "config.h" | 40 | #include <config.h> |
| 40 | 41 | ||
| 41 | #define MAX_ATTEMPTS 5 | 42 | #define MAX_ATTEMPTS 5 |
| 42 | #define SCORE_FILE_PREFIX "/var/games/emacs/" | 43 | #define SCORE_FILE_PREFIX "/var/games/emacs/" |
| @@ -60,6 +61,7 @@ unlock_file(const char *filename, void *state); | |||
| 60 | struct score_entry | 61 | struct score_entry |
| 61 | { | 62 | { |
| 62 | long score; | 63 | long score; |
| 64 | char *username; | ||
| 63 | char *data; | 65 | char *data; |
| 64 | }; | 66 | }; |
| 65 | 67 | ||
| @@ -68,13 +70,31 @@ read_scores(const char *filename, struct score_entry **scores, | |||
| 68 | int *count); | 70 | int *count); |
| 69 | int | 71 | int |
| 70 | push_score(struct score_entry **scores, int *count, | 72 | push_score(struct score_entry **scores, int *count, |
| 71 | int newscore, char *newdata); | 73 | int newscore, char *username, char *newdata); |
| 72 | void | 74 | void |
| 73 | sort_scores(struct score_entry *scores, int count, int reverse); | 75 | sort_scores(struct score_entry *scores, int count, int reverse); |
| 74 | int | 76 | int |
| 75 | write_scores(const char *filename, const struct score_entry *scores, | 77 | write_scores(const char *filename, const struct score_entry *scores, |
| 76 | int count); | 78 | int count); |
| 77 | 79 | ||
| 80 | char * | ||
| 81 | get_user_id() | ||
| 82 | { | ||
| 83 | char *name; | ||
| 84 | struct passwd *buf = getpwuid(getuid()); | ||
| 85 | if (!buf) | ||
| 86 | { | ||
| 87 | int count = 1; | ||
| 88 | int uid = (int) getuid(); | ||
| 89 | while (uid /= 10) | ||
| 90 | count++; | ||
| 91 | name = malloc(count+1); | ||
| 92 | sprintf(name, "%d", uid); | ||
| 93 | return name; | ||
| 94 | } | ||
| 95 | return buf->pw_name; | ||
| 96 | } | ||
| 97 | |||
| 78 | int | 98 | int |
| 79 | main(int argc, char **argv) | 99 | main(int argc, char **argv) |
| 80 | { | 100 | { |
| @@ -106,7 +126,7 @@ main(int argc, char **argv) | |||
| 106 | 126 | ||
| 107 | if (optind+3 != argc) | 127 | if (optind+3 != argc) |
| 108 | usage(1); | 128 | usage(1); |
| 109 | scorefile = malloc(strlen(SCORE_FILE_PREFIX) + strlen(argv[optind]) + 20); | 129 | scorefile = malloc(strlen(SCORE_FILE_PREFIX) + strlen(argv[optind]) + 1); |
| 110 | if (!scorefile) | 130 | if (!scorefile) |
| 111 | { | 131 | { |
| 112 | fprintf(stderr, "Couldn't create score file name: %s\n", | 132 | fprintf(stderr, "Couldn't create score file name: %s\n", |
| @@ -136,7 +156,7 @@ main(int argc, char **argv) | |||
| 136 | scorefile, strerror(errno)); | 156 | scorefile, strerror(errno)); |
| 137 | goto fail_unlock; | 157 | goto fail_unlock; |
| 138 | } | 158 | } |
| 139 | push_score(&scores, &scorecount, newscore, newdata); | 159 | push_score(&scores, &scorecount, newscore, get_user_id(), newdata); |
| 140 | sort_scores(scores, scorecount, reverse); | 160 | sort_scores(scores, scorecount, reverse); |
| 141 | if (write_scores(scorefile, scores, scorecount) < 0) | 161 | if (write_scores(scorefile, scores, scorecount) < 0) |
| 142 | { | 162 | { |
| @@ -159,10 +179,43 @@ read_score(FILE *f, struct score_entry *score) | |||
| 159 | if (feof(f)) | 179 | if (feof(f)) |
| 160 | return 1; | 180 | return 1; |
| 161 | while ((c = getc(f)) != EOF | 181 | while ((c = getc(f)) != EOF |
| 162 | && isdigit(c)) { | 182 | && isdigit(c)) |
| 163 | score->score *= 10; | 183 | { |
| 164 | score->score += (c-48); | 184 | score->score *= 10; |
| 185 | score->score += (c-48); | ||
| 186 | } | ||
| 187 | while ((c = getc(f)) != EOF | ||
| 188 | && isspace(c)) | ||
| 189 | ; | ||
| 190 | if (c == EOF) | ||
| 191 | return -1; | ||
| 192 | #ifdef HAVE_GETDELIM | ||
| 193 | { | ||
| 194 | int count = 0; | ||
| 195 | if (getdelim(&score->username, &count, ' ', f) < 1 | ||
| 196 | || score->username == NULL) | ||
| 197 | return -1; | ||
| 165 | } | 198 | } |
| 199 | #else | ||
| 200 | { | ||
| 201 | int unameread = 0; | ||
| 202 | int unamelen = 30; | ||
| 203 | char *username; | ||
| 204 | |||
| 205 | while ((c = getc(f)) != EOF | ||
| 206 | && !isspace(c)) | ||
| 207 | { | ||
| 208 | if (unameread == unamelen) | ||
| 209 | { | ||
| 210 | if (!(username = realloc(username, unamelen *= 2))) | ||
| 211 | return -1; | ||
| 212 | } | ||
| 213 | username[unameread] = c; | ||
| 214 | unameread++; | ||
| 215 | } | ||
| 216 | score->username = username; | ||
| 217 | } | ||
| 218 | #endif | ||
| 166 | #ifdef HAVE_GETLINE | 219 | #ifdef HAVE_GETLINE |
| 167 | score->data = NULL; | 220 | score->data = NULL; |
| 168 | errno = ESUCCES; | 221 | errno = ESUCCES; |
| @@ -172,12 +225,25 @@ read_score(FILE *f, struct score_entry *score) | |||
| 172 | return -1; | 225 | return -1; |
| 173 | } | 226 | } |
| 174 | #else | 227 | #else |
| 175 | /* We should probably just copy the getline code into here from | 228 | { |
| 176 | glibc, instead of this halfassed solution. */ | 229 | int cur = 0; |
| 177 | score->data = malloc(122); | 230 | int len = 16; |
| 178 | score->data[0] = '\0'; | 231 | char *buf = malloc(len); |
| 179 | if (!fgets(score->data, 120, f)) | 232 | if (!buf) |
| 180 | return -1; | 233 | return -1; |
| 234 | while ((c = getc(f)) != EOF) | ||
| 235 | { | ||
| 236 | if (cur >= len-1) | ||
| 237 | { | ||
| 238 | if (!(buf = realloc(buf, len *= 2))) | ||
| 239 | return -1; | ||
| 240 | } | ||
| 241 | buf[cur] = c; | ||
| 242 | cur++; | ||
| 243 | } | ||
| 244 | score->data = buf; | ||
| 245 | score->data[cur+1] = '\0'; | ||
| 246 | } | ||
| 181 | #endif | 247 | #endif |
| 182 | /* Trim the newline */ | 248 | /* Trim the newline */ |
| 183 | score->data[strlen(score->data)-1] = '\0'; | 249 | score->data[strlen(score->data)-1] = '\0'; |
| @@ -234,13 +300,14 @@ score_compare_reverse(const void *a, const void *b) | |||
| 234 | 300 | ||
| 235 | int | 301 | int |
| 236 | push_score(struct score_entry **scores, int *count, | 302 | push_score(struct score_entry **scores, int *count, |
| 237 | int newscore, char *newdata) | 303 | int newscore, char *username, char *newdata) |
| 238 | { | 304 | { |
| 239 | struct score_entry *newscores = realloc(*scores, | 305 | struct score_entry *newscores = realloc(*scores, |
| 240 | sizeof(struct score_entry) * ((*count) + 1)); | 306 | sizeof(struct score_entry) * ((*count) + 1)); |
| 241 | if (!newscores) | 307 | if (!newscores) |
| 242 | return -1; | 308 | return -1; |
| 243 | newscores[*count].score = newscore; | 309 | newscores[*count].score = newscore; |
| 310 | newscores[*count].username = username; | ||
| 244 | newscores[*count].data = newdata; | 311 | newscores[*count].data = newdata; |
| 245 | (*count) += 1; | 312 | (*count) += 1; |
| 246 | *scores = newscores; | 313 | *scores = newscores; |
| @@ -269,7 +336,8 @@ write_scores(const char *filename, const struct score_entry *scores, | |||
| 269 | || !(f = fopen(tempfile, "w"))) | 336 | || !(f = fopen(tempfile, "w"))) |
| 270 | return -1; | 337 | return -1; |
| 271 | for (i = 0; i < count; i++) | 338 | for (i = 0; i < count; i++) |
| 272 | if (fprintf(f, "%ld %s\n", scores[i].score, scores[i].data) < 0) | 339 | if (fprintf(f, "%ld %s %s\n", scores[i].score, scores[i].username, |
| 340 | scores[i].data) < 0) | ||
| 273 | return -1; | 341 | return -1; |
| 274 | fclose(f); | 342 | fclose(f); |
| 275 | rename(tempfile, filename); | 343 | rename(tempfile, filename); |