Mercurial > emacs
annotate lib-src/update-game-score.c @ 49594:a29fefe5c6a9
Whitespace changes.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 04 Feb 2003 12:00:09 +0000 |
parents | 2346caa2a66a |
children | ca4fb31aae14 |
rev | line source |
---|---|
44184 | 1 /* update-game-score.c --- Update a score file |
2 Copyright (C) 2002 Free Software Foundation, Inc. | |
3 | |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs; see the file COPYING. If not, write to | |
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 Boston, MA 02111-1307, USA. */ | |
20 | |
21 /* This program is allows a game to securely and atomically update a | |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
22 score file. It should be installed setuid, owned by an appropriate |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
23 user like `games'. |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
24 |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
25 Alternatively, it can be compiled without HAVE_SHARED_GAME_DIR |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
26 defined, and in that case it will store scores in the user's home |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
27 directory (it should NOT be setuid). |
44184 | 28 |
29 Created 2002/03/22, by Colin Walters <walters@debian.org> | |
30 */ | |
31 | |
44639
44995332ed1b
Move config.h before the other headers, to prevent compiler warnings
Eli Zaretskii <eliz@gnu.org>
parents:
44576
diff
changeset
|
32 #include <config.h> |
44995332ed1b
Move config.h before the other headers, to prevent compiler warnings
Eli Zaretskii <eliz@gnu.org>
parents:
44576
diff
changeset
|
33 |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
34 #ifdef HAVE_UNISTD_H |
44184 | 35 #include <unistd.h> |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
36 #endif |
44184 | 37 #include <errno.h> |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
38 #ifdef HAVE_STRING_H |
44184 | 39 #include <string.h> |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
40 #endif |
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
41 #ifdef HAVE_STDLIB_H |
44184 | 42 #include <stdlib.h> |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
43 #endif |
44184 | 44 #include <stdio.h> |
45 #include <time.h> | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
46 #include <pwd.h> |
44184 | 47 #include <ctype.h> |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
48 #ifdef HAVE_FCNTL_H |
44184 | 49 #include <fcntl.h> |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
50 #endif |
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
51 #ifdef STDC_HEADERS |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
52 #include <stdarg.h> |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
53 #endif |
44184 | 54 #include <sys/stat.h> |
55 | |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
56 /* Needed for SunOS4, for instance. */ |
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
57 extern char *optarg; |
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
58 extern int optind, opterr; |
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
59 |
44184 | 60 #define MAX_ATTEMPTS 5 |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
61 #define MAX_SCORES 200 |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
62 #define MAX_DATA_LEN 1024 |
44419
16b3622178f9
update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents:
44404
diff
changeset
|
63 |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
64 /* Declare the prototype for a general external function. */ |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
65 #if defined (PROTOTYPES) || defined (WINDOWSNT) |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
66 #define P_(proto) proto |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
67 #else |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
68 #define P_(proto) () |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
69 #endif |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
70 |
44184 | 71 int |
49594 | 72 usage (err) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
73 int err; |
44184 | 74 { |
49594 | 75 fprintf (stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n"); |
76 fprintf (stdout, " update-game-score -h\n"); | |
77 fprintf (stdout, " -h\t\tDisplay this help.\n"); | |
78 fprintf (stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n"); | |
79 fprintf (stdout, " -r\t\tSort the scores in increasing order.\n"); | |
80 fprintf (stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n"); | |
81 exit (err); | |
44184 | 82 } |
83 | |
49594 | 84 int lock_file P_ ((const char *filename, void **state)); |
85 int unlock_file P_ ((const char *filename, void *state)); | |
44184 | 86 |
87 struct score_entry | |
88 { | |
89 long score; | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
90 char *username; |
44184 | 91 char *data; |
92 }; | |
93 | |
49594 | 94 int read_scores P_ ((const char *filename, struct score_entry **scores, |
95 int *count)); | |
96 int push_score P_ ((struct score_entry **scores, int *count, | |
97 int newscore, char *username, char *newdata)); | |
98 void sort_scores P_ ((struct score_entry *scores, int count, int reverse)); | |
99 int write_scores P_ ((const char *filename, const struct score_entry *scores, | |
100 int count)); | |
101 | |
102 void lose P_ ((const char *msg)) NO_RETURN; | |
103 | |
44184 | 104 void |
49594 | 105 lose (msg) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
106 const char *msg; |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
107 { |
49594 | 108 fprintf (stderr, "%s\n", msg); |
109 exit (1); | |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
110 } |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
111 |
49594 | 112 void lose_syserr P_ ((const char *msg)) NO_RETURN; |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
113 |
49594 | 114 void |
115 lose_syserr (msg) | |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
116 const char *msg; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
117 { |
49594 | 118 fprintf (stderr, "%s: %s\n", msg, strerror (errno)); |
119 exit (1); | |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
120 } |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
121 |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
122 char * |
48422
2346caa2a66a
Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents:
46774
diff
changeset
|
123 get_user_id P_ ((void)) |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
124 { |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
125 char *name; |
49594 | 126 struct passwd *buf = getpwuid (getuid ()); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
127 if (!buf) |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
128 { |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
129 int count = 1; |
49594 | 130 int uid = (int) getuid (); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
131 int tuid = uid; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
132 while (tuid /= 10) |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
133 count++; |
49594 | 134 name = malloc (count+1); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
135 if (!name) |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
136 return NULL; |
49594 | 137 sprintf (name, "%d", uid); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
138 return name; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
139 } |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
140 return buf->pw_name; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
141 } |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
142 |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
143 char * |
49594 | 144 get_prefix (running_suid, user_prefix) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
145 int running_suid; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
146 char *user_prefix; |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
147 { |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
148 if (!running_suid && user_prefix == NULL) |
49594 | 149 lose ("Not using a shared game directory, and no prefix given."); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
150 if (running_suid) |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
151 { |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
152 #ifdef HAVE_SHARED_GAME_DIR |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
153 return HAVE_SHARED_GAME_DIR; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
154 #else |
49594 | 155 lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid."); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
156 #endif |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
157 } |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
158 return user_prefix; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
159 } |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
160 |
44184 | 161 int |
49594 | 162 main (argc, argv) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
163 int argc; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
164 char **argv; |
44184 | 165 { |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
166 int c, running_suid; |
44184 | 167 void *lockstate; |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
168 char *user_id, *scorefile, *prefix, *user_prefix = NULL; |
44184 | 169 struct stat buf; |
170 struct score_entry *scores; | |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
171 int newscore, scorecount, reverse = 0, max = MAX_SCORES; |
44184 | 172 char *newdata; |
173 | |
49594 | 174 srand (time (0)); |
44184 | 175 |
49594 | 176 while ((c = getopt (argc, argv, "hrm:d:")) != -1) |
44184 | 177 switch (c) |
178 { | |
179 case 'h': | |
49594 | 180 usage (0); |
44184 | 181 break; |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
182 case 'd': |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
183 user_prefix = optarg; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
184 break; |
44184 | 185 case 'r': |
186 reverse = 1; | |
187 break; | |
188 case 'm': | |
49594 | 189 max = atoi (optarg); |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
190 if (max > MAX_SCORES) |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
191 max = MAX_SCORES; |
44184 | 192 break; |
193 default: | |
49594 | 194 usage (1); |
44184 | 195 } |
196 | |
197 if (optind+3 != argc) | |
49594 | 198 usage (1); |
44419
16b3622178f9
update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents:
44404
diff
changeset
|
199 |
49594 | 200 running_suid = (getuid () != geteuid ()); |
44419
16b3622178f9
update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents:
44404
diff
changeset
|
201 |
49594 | 202 prefix = get_prefix (running_suid, user_prefix); |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
203 |
49594 | 204 scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2); |
44184 | 205 if (!scorefile) |
49594 | 206 lose_syserr ("Couldn't allocate score file"); |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
207 |
49594 | 208 strcpy (scorefile, prefix); |
209 strcat (scorefile, "/"); | |
210 strcat (scorefile, argv[optind]); | |
211 newscore = atoi (argv[optind+1]); | |
44184 | 212 newdata = argv[optind+2]; |
49594 | 213 if (strlen (newdata) > MAX_DATA_LEN) |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
214 newdata[MAX_DATA_LEN] = '\0'; |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
215 |
49594 | 216 if ((user_id = get_user_id ()) == NULL) |
217 lose_syserr ("Couldn't determine user id"); | |
44184 | 218 |
49594 | 219 if (stat (scorefile, &buf) < 0) |
220 lose_syserr ("Failed to access scores file"); | |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
221 |
49594 | 222 if (lock_file (scorefile, &lockstate) < 0) |
223 lose_syserr ("Failed to lock scores file"); | |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
224 |
49594 | 225 if (read_scores (scorefile, &scores, &scorecount) < 0) |
44184 | 226 { |
49594 | 227 unlock_file (scorefile, lockstate); |
228 lose_syserr ("Failed to read scores file"); | |
44184 | 229 } |
49594 | 230 push_score (&scores, &scorecount, newscore, user_id, newdata); |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
231 /* Limit the number of scores. If we're using reverse sorting, then |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
232 we should increment the beginning of the array, to skip over the |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
233 *smallest* scores. Otherwise, we just decrement the number of |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
234 scores, since the smallest will be at the end. */ |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
235 if (scorecount > MAX_SCORES) |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
236 scorecount -= (scorecount - MAX_SCORES); |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
237 if (reverse) |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
238 scores += (scorecount - MAX_SCORES); |
49594 | 239 sort_scores (scores, scorecount, reverse); |
240 if (write_scores (scorefile, scores, scorecount) < 0) | |
44184 | 241 { |
49594 | 242 unlock_file (scorefile, lockstate); |
243 lose_syserr ("Failed to write scores file"); | |
44184 | 244 } |
49594 | 245 unlock_file (scorefile, lockstate); |
246 exit (0); | |
44184 | 247 } |
248 | |
249 int | |
49594 | 250 read_score (f, score) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
251 FILE *f; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
252 struct score_entry *score; |
44184 | 253 { |
254 int c; | |
49594 | 255 if (feof (f)) |
44184 | 256 return 1; |
49594 | 257 while ((c = getc (f)) != EOF |
258 && isdigit (c)) | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
259 { |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
260 score->score *= 10; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
261 score->score += (c-48); |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
262 } |
49594 | 263 while ((c = getc (f)) != EOF |
264 && isspace (c)) | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
265 ; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
266 if (c == EOF) |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
267 return -1; |
49594 | 268 ungetc (c, f); |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
269 #ifdef HAVE_GETDELIM |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
270 { |
44566
7a0ad319b38f
(read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents:
44481
diff
changeset
|
271 size_t count = 0; |
49594 | 272 if (getdelim (&score->username, &count, ' ', f) < 1 |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
273 || score->username == NULL) |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
274 return -1; |
44789
d095e59dc01d
(read_score) [HAVE_GETDELIM]: Trim trailing space.
Colin Walters <walters@gnu.org>
parents:
44639
diff
changeset
|
275 /* Trim the space */ |
49594 | 276 score->username[strlen (score->username)-1] = '\0'; |
44184 | 277 } |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
278 #else |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
279 { |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
280 int unameread = 0; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
281 int unamelen = 30; |
49594 | 282 char *username = malloc (unamelen); |
44404
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
283 if (!username) |
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
284 return -1; |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
285 |
49594 | 286 while ((c = getc (f)) != EOF |
287 && !isspace (c)) | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
288 { |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
289 if (unameread >= unamelen-1) |
49594 | 290 if (!(username = realloc (username, unamelen *= 2))) |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
291 return -1; |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
292 username[unameread] = c; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
293 unameread++; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
294 } |
44404
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
295 if (c == EOF) |
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
296 return -1; |
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
297 username[unameread] = '\0'; |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
298 score->username = username; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
299 } |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
300 #endif |
44184 | 301 #ifdef HAVE_GETLINE |
302 score->data = NULL; | |
44566
7a0ad319b38f
(read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents:
44481
diff
changeset
|
303 errno = 0; |
44184 | 304 { |
44566
7a0ad319b38f
(read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents:
44481
diff
changeset
|
305 size_t len; |
49594 | 306 if (getline (&score->data, &len, f) < 0) |
44184 | 307 return -1; |
49594 | 308 score->data[strlen (score->data)-1] = '\0'; |
44184 | 309 } |
310 #else | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
311 { |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
312 int cur = 0; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
313 int len = 16; |
49594 | 314 char *buf = malloc (len); |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
315 if (!buf) |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
316 return -1; |
49594 | 317 while ((c = getc (f)) != EOF |
44404
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
318 && c != '\n') |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
319 { |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
320 if (cur >= len-1) |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
321 { |
49594 | 322 if (!(buf = realloc (buf, len *= 2))) |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
323 return -1; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
324 } |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
325 buf[cur] = c; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
326 cur++; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
327 } |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
328 score->data = buf; |
44419
16b3622178f9
update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents:
44404
diff
changeset
|
329 score->data[cur] = '\0'; |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
330 } |
44184 | 331 #endif |
332 return 0; | |
333 } | |
334 | |
335 int | |
49594 | 336 read_scores (filename, scores, count) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
337 const char *filename; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
338 struct score_entry **scores; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
339 int *count; |
44184 | 340 { |
341 int readval, scorecount, cursize; | |
342 struct score_entry *ret; | |
49594 | 343 FILE *f = fopen (filename, "r"); |
44184 | 344 if (!f) |
345 return -1; | |
346 scorecount = 0; | |
347 cursize = 16; | |
49594 | 348 ret = malloc (sizeof (struct score_entry) * cursize); |
44184 | 349 if (!ret) |
350 return -1; | |
49594 | 351 while ((readval = read_score (f, &ret[scorecount])) == 0) |
44184 | 352 { |
353 /* We encoutered an error */ | |
354 if (readval < 0) | |
355 return -1; | |
356 scorecount++; | |
357 if (scorecount >= cursize) | |
358 { | |
49594 | 359 ret = realloc (ret, cursize *= 2); |
44184 | 360 if (!ret) |
361 return -1; | |
362 } | |
363 } | |
364 *count = scorecount; | |
365 *scores = ret; | |
366 return 0; | |
367 } | |
368 | |
369 int | |
49594 | 370 score_compare (a, b) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
371 const void *a; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
372 const void *b; |
44184 | 373 { |
374 const struct score_entry *sa = (const struct score_entry *) a; | |
375 const struct score_entry *sb = (const struct score_entry *) b; | |
376 return (sb->score > sa->score) - (sb->score < sa->score); | |
377 } | |
378 | |
379 int | |
49594 | 380 score_compare_reverse (a, b) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
381 const void *a; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
382 const void *b; |
44184 | 383 { |
384 const struct score_entry *sa = (const struct score_entry *) a; | |
385 const struct score_entry *sb = (const struct score_entry *) b; | |
386 return (sa->score > sb->score) - (sa->score < sb->score); | |
387 } | |
388 | |
389 int | |
49594 | 390 push_score (scores, count, newscore, username, newdata) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
391 struct score_entry **scores; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
392 int *count; int newscore; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
393 char *username; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
394 char *newdata; |
44184 | 395 { |
49594 | 396 struct score_entry *newscores |
397 = realloc (*scores, | |
398 sizeof (struct score_entry) * ((*count) + 1)); | |
44184 | 399 if (!newscores) |
400 return -1; | |
401 newscores[*count].score = newscore; | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
402 newscores[*count].username = username; |
44184 | 403 newscores[*count].data = newdata; |
404 (*count) += 1; | |
405 *scores = newscores; | |
406 return 0; | |
407 } | |
408 | |
409 void | |
49594 | 410 sort_scores (scores, count, reverse) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
411 struct score_entry *scores; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
412 int count; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
413 int reverse; |
44184 | 414 { |
49594 | 415 qsort (scores, count, sizeof (struct score_entry), |
44184 | 416 reverse ? score_compare_reverse : score_compare); |
417 } | |
418 | |
419 int | |
49594 | 420 write_scores (filename, scores, count) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
421 const char *filename; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
422 const struct score_entry * scores; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
423 int count; |
44184 | 424 { |
425 FILE *f; | |
426 int i; | |
49594 | 427 char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1); |
44184 | 428 if (!tempfile) |
429 return -1; | |
49594 | 430 strcpy (tempfile, filename); |
431 strcat (tempfile, ".tempXXXXXX"); | |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
432 #ifdef HAVE_MKSTEMP |
49594 | 433 if (mkstemp (tempfile) < 0 |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
434 #else |
49594 | 435 if (mktemp (tempfile) != tempfile |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
436 #endif |
49594 | 437 || !(f = fopen (tempfile, "w"))) |
44184 | 438 return -1; |
439 for (i = 0; i < count; i++) | |
49594 | 440 if (fprintf (f, "%ld %s %s\n", scores[i].score, scores[i].username, |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
441 scores[i].data) < 0) |
44184 | 442 return -1; |
49594 | 443 fclose (f); |
444 if (rename (tempfile, filename) < 0) | |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
445 return -1; |
49594 | 446 if (chmod (filename, 0644) < 0) |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
447 return -1; |
44184 | 448 return 0; |
449 } | |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
450 |
44184 | 451 int |
49594 | 452 lock_file (filename, state) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
453 const char *filename; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
454 void **state; |
44184 | 455 { |
456 int fd; | |
44576
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
parents:
44566
diff
changeset
|
457 struct stat buf; |
44184 | 458 int attempts = 0; |
459 char *lockext = ".lockfile"; | |
49594 | 460 char *lockpath = malloc (strlen (filename) + strlen (lockext) + 60); |
44184 | 461 if (!lockpath) |
462 return -1; | |
49594 | 463 strcpy (lockpath, filename); |
464 strcat (lockpath, lockext); | |
44184 | 465 *state = lockpath; |
466 trylock: | |
467 attempts++; | |
44576
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
parents:
44566
diff
changeset
|
468 /* If the lock is over an hour old, delete it. */ |
49594 | 469 if (stat (lockpath, &buf) == 0 |
470 && (difftime (buf.st_ctime, time (NULL) > 60*60))) | |
471 unlink (lockpath); | |
472 if ((fd = open (lockpath, O_CREAT | O_EXCL, 0600)) < 0) | |
44184 | 473 { |
474 if (errno == EEXIST) | |
475 { | |
476 /* Break the lock; we won't corrupt the file, but we might | |
477 lose some scores. */ | |
478 if (attempts > MAX_ATTEMPTS) | |
44576
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
parents:
44566
diff
changeset
|
479 { |
49594 | 480 unlink (lockpath); |
44576
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
parents:
44566
diff
changeset
|
481 attempts = 0; |
90866353c7bd
(lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
parents:
44566
diff
changeset
|
482 } |
49594 | 483 sleep ((rand () % 2)+1); |
44184 | 484 goto trylock; |
485 } | |
486 else | |
487 return -1; | |
488 } | |
49594 | 489 close (fd); |
44184 | 490 return 0; |
491 } | |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
492 |
44184 | 493 int |
49594 | 494 unlock_file (filename, state) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
495 const char *filename; |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
496 void *state; |
44184 | 497 { |
498 char *lockpath = (char *) state; | |
49594 | 499 int ret = unlink (lockpath); |
44184 | 500 int saved_errno = errno; |
49594 | 501 free (lockpath); |
44184 | 502 errno = saved_errno; |
503 return ret; | |
504 } |