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