Mercurial > emacs
annotate lib-src/update-game-score.c @ 112119:48f5ac42611a
Reconcile with changes in line movement behavior for long text lines
that cross more than a single physical window line, ie when truncate-lines
is nil.
(allout-next-visible-heading): Provide for change in line-move behavior on
long lines when truncate-lines is nil. In that case, line-move can wind up
on the same textual line when it moves to the next window line, and moving
to the bullet position after the move yields zero advancement. Add logic
to detect and compensate for the lack of progress.
(allout-current-topic-collapsed-p): move-end-of-line respect for field
boundaries is different when operating with body lines shorter than window
width versus ones greater than window width, which can yield false
negatives in this function. Avoid difference by applying move-end-of-line
while field-text-motion is inhibited.
author | Ken Manheimer <ken.manheimer@gmail.com> |
---|---|
date | Tue, 04 Jan 2011 14:44:10 -0500 |
parents | fdbd24f8d999 |
children | 417b1e4d63cd 7df2e30d72ec |
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 |
106815 | 3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
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 |
109511
09a43f890565
Add NO_RETURN specifiers to functions in lib-src.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109172
diff
changeset
|
63 int usage (int err) NO_RETURN; |
09a43f890565
Add NO_RETURN specifiers to functions in lib-src.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109172
diff
changeset
|
64 |
44184 | 65 #define MAX_ATTEMPTS 5 |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
66 #define MAX_SCORES 200 |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
67 #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
|
68 |
51146
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
69 #ifndef HAVE_DIFFTIME |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
70 /* 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
|
71 #define difftime(t1, t0) (double)((t1) - (t0)) |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
72 #endif |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
73 |
44184 | 74 int |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
75 usage (int err) |
44184 | 76 { |
49594 | 77 fprintf (stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n"); |
78 fprintf (stdout, " update-game-score -h\n"); | |
79 fprintf (stdout, " -h\t\tDisplay this help.\n"); | |
80 fprintf (stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n"); | |
81 fprintf (stdout, " -r\t\tSort the scores in increasing order.\n"); | |
82 fprintf (stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n"); | |
83 exit (err); | |
44184 | 84 } |
85 | |
109100
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
86 int lock_file (const char *filename, void **state); |
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
87 int unlock_file (const char *filename, void *state); |
44184 | 88 |
89 struct score_entry | |
90 { | |
91 long score; | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
92 char *username; |
44184 | 93 char *data; |
94 }; | |
95 | |
109100
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
96 int read_scores (const char *filename, struct score_entry **scores, |
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
97 int *count); |
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
98 int push_score (struct score_entry **scores, int *count, |
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
99 int newscore, char *username, char *newdata); |
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
100 void sort_scores (struct score_entry *scores, int count, int reverse); |
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
101 int write_scores (const char *filename, const struct score_entry *scores, |
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
102 int count); |
49594 | 103 |
109100
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
104 void lose (const char *msg) NO_RETURN; |
49594 | 105 |
44184 | 106 void |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
107 lose (const char *msg) |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
108 { |
49594 | 109 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
|
110 exit (EXIT_FAILURE); |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
111 } |
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
112 |
109100
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
113 void lose_syserr (const char *msg) NO_RETURN; |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
114 |
51146
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
115 /* Taken from sysdep.c. */ |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
116 #ifndef HAVE_STRERROR |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
117 #ifndef WINDOWSNT |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
118 char * |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
119 strerror (errnum) |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
120 int errnum; |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
121 { |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
122 extern char *sys_errlist[]; |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
123 extern int sys_nerr; |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
124 |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
125 if (errnum >= 0 && errnum < sys_nerr) |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
126 return sys_errlist[errnum]; |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
127 return (char *) "Unknown error"; |
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 #endif /* not WINDOWSNT */ |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
130 #endif /* ! HAVE_STRERROR */ |
f20c52ac1b8d
(difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents:
50718
diff
changeset
|
131 |
49594 | 132 void |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
133 lose_syserr (const char *msg) |
46774
7411dc9e5008
(P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents:
44987
diff
changeset
|
134 { |
49594 | 135 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
|
136 exit (EXIT_FAILURE); |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
137 } |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
138 |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
139 char * |
109100
2bc9a0c04c87
Remove __P and P_ from .c and .m files and definition of P_
Jan D <jan.h.d@swipnet.se>
parents:
106815
diff
changeset
|
140 get_user_id (void) |
44987
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 char *name; |
49594 | 143 struct passwd *buf = getpwuid (getuid ()); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
144 if (!buf) |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
145 { |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
146 int count = 1; |
49594 | 147 int uid = (int) getuid (); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
148 int tuid = uid; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
149 while (tuid /= 10) |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
150 count++; |
49594 | 151 name = malloc (count+1); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
152 if (!name) |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
153 return NULL; |
49594 | 154 sprintf (name, "%d", uid); |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
155 return name; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
156 } |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
157 return buf->pw_name; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
158 } |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
159 |
109744
fdbd24f8d999
Fix warnings produced by compiling with -Wwrite_strings (i.e. use const char *).
Jan D <jan.h.d@swipnet.se>
parents:
109511
diff
changeset
|
160 const char * |
fdbd24f8d999
Fix warnings produced by compiling with -Wwrite_strings (i.e. use const char *).
Jan D <jan.h.d@swipnet.se>
parents:
109511
diff
changeset
|
161 get_prefix (int running_suid, const char *user_prefix) |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
162 { |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
163 if (!running_suid && user_prefix == NULL) |
49594 | 164 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
|
165 if (running_suid) |
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 #ifdef HAVE_SHARED_GAME_DIR |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
168 return HAVE_SHARED_GAME_DIR; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
169 #else |
49594 | 170 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
|
171 #endif |
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 return user_prefix; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
174 } |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
175 |
44184 | 176 int |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
177 main (int argc, char **argv) |
44184 | 178 { |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
179 int c, running_suid; |
44184 | 180 void *lockstate; |
109744
fdbd24f8d999
Fix warnings produced by compiling with -Wwrite_strings (i.e. use const char *).
Jan D <jan.h.d@swipnet.se>
parents:
109511
diff
changeset
|
181 char *user_id, *scorefile; |
fdbd24f8d999
Fix warnings produced by compiling with -Wwrite_strings (i.e. use const char *).
Jan D <jan.h.d@swipnet.se>
parents:
109511
diff
changeset
|
182 const char *prefix, *user_prefix = NULL; |
44184 | 183 struct stat buf; |
184 struct score_entry *scores; | |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
185 int newscore, scorecount, reverse = 0, max = MAX_SCORES; |
44184 | 186 char *newdata; |
187 | |
49594 | 188 srand (time (0)); |
44184 | 189 |
49594 | 190 while ((c = getopt (argc, argv, "hrm:d:")) != -1) |
44184 | 191 switch (c) |
192 { | |
193 case 'h': | |
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
194 usage (EXIT_SUCCESS); |
44184 | 195 break; |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
196 case 'd': |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
197 user_prefix = optarg; |
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
198 break; |
44184 | 199 case 'r': |
200 reverse = 1; | |
201 break; | |
202 case 'm': | |
49594 | 203 max = atoi (optarg); |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
204 if (max > MAX_SCORES) |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
205 max = MAX_SCORES; |
44184 | 206 break; |
207 default: | |
55442
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
208 usage (EXIT_FAILURE); |
44184 | 209 } |
210 | |
211 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
|
212 usage (EXIT_FAILURE); |
44419
16b3622178f9
update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents:
44404
diff
changeset
|
213 |
49594 | 214 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
|
215 |
49594 | 216 prefix = get_prefix (running_suid, user_prefix); |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
217 |
49594 | 218 scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2); |
44184 | 219 if (!scorefile) |
49594 | 220 lose_syserr ("Couldn't allocate score file"); |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
221 |
49594 | 222 strcpy (scorefile, prefix); |
223 strcat (scorefile, "/"); | |
224 strcat (scorefile, argv[optind]); | |
225 newscore = atoi (argv[optind+1]); | |
44184 | 226 newdata = argv[optind+2]; |
49594 | 227 if (strlen (newdata) > MAX_DATA_LEN) |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
228 newdata[MAX_DATA_LEN] = '\0'; |
44987
cc0ab9acdc46
(SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents:
44789
diff
changeset
|
229 |
49595
ca4fb31aae14
(push_score, read_scores): Cast values of malloc and realloc.
Richard M. Stallman <rms@gnu.org>
parents:
49594
diff
changeset
|
230 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
|
231 if (user_id == NULL) |
49594 | 232 lose_syserr ("Couldn't determine user id"); |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
233 |
49594 | 234 if (stat (scorefile, &buf) < 0) |
235 lose_syserr ("Failed to access scores file"); | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
236 |
49594 | 237 if (lock_file (scorefile, &lockstate) < 0) |
238 lose_syserr ("Failed to lock scores file"); | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
239 |
49594 | 240 if (read_scores (scorefile, &scores, &scorecount) < 0) |
44184 | 241 { |
49594 | 242 unlock_file (scorefile, lockstate); |
243 lose_syserr ("Failed to read scores file"); | |
44184 | 244 } |
49594 | 245 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
|
246 sort_scores (scores, scorecount, reverse); |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
247 /* 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
|
248 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
|
249 *smallest* scores. Otherwise, we just decrement the number of |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
250 scores, since the smallest will be at the end. */ |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
251 if (scorecount > MAX_SCORES) |
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
252 scorecount -= (scorecount - MAX_SCORES); |
104955
be5f5f7e93cb
(main): Sort scores before trimming them,
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
100958
diff
changeset
|
253 if (reverse) |
be5f5f7e93cb
(main): Sort scores before trimming them,
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
100958
diff
changeset
|
254 scores += (scorecount - MAX_SCORES); |
49594 | 255 if (write_scores (scorefile, scores, scorecount) < 0) |
44184 | 256 { |
49594 | 257 unlock_file (scorefile, lockstate); |
258 lose_syserr ("Failed to write scores file"); | |
44184 | 259 } |
49594 | 260 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
|
261 exit (EXIT_SUCCESS); |
44184 | 262 } |
263 | |
264 int | |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
265 read_score (FILE *f, struct score_entry *score) |
44184 | 266 { |
267 int c; | |
49594 | 268 if (feof (f)) |
44184 | 269 return 1; |
49594 | 270 while ((c = getc (f)) != EOF |
271 && isdigit (c)) | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
272 { |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
273 score->score *= 10; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
274 score->score += (c-48); |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
275 } |
49594 | 276 while ((c = getc (f)) != EOF |
277 && isspace (c)) | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
278 ; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
279 if (c == EOF) |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
280 return -1; |
49594 | 281 ungetc (c, f); |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
282 #ifdef HAVE_GETDELIM |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
283 { |
44566
7a0ad319b38f
(read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents:
44481
diff
changeset
|
284 size_t count = 0; |
49594 | 285 if (getdelim (&score->username, &count, ' ', f) < 1 |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
286 || score->username == NULL) |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
287 return -1; |
44789
d095e59dc01d
(read_score) [HAVE_GETDELIM]: Trim trailing space.
Colin Walters <walters@gnu.org>
parents:
44639
diff
changeset
|
288 /* Trim the space */ |
49594 | 289 score->username[strlen (score->username)-1] = '\0'; |
44184 | 290 } |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
291 #else |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
292 { |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
293 int unameread = 0; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
294 int unamelen = 30; |
49594 | 295 char *username = malloc (unamelen); |
44404
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
296 if (!username) |
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
297 return -1; |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
298 |
49594 | 299 while ((c = getc (f)) != EOF |
300 && !isspace (c)) | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
301 { |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
302 if (unameread >= unamelen-1) |
49594 | 303 if (!(username = realloc (username, unamelen *= 2))) |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
304 return -1; |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
305 username[unameread] = c; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
306 unameread++; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
307 } |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
308 if (c == EOF) |
44404
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
309 return -1; |
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
310 username[unameread] = '\0'; |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
311 score->username = username; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
312 } |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
313 #endif |
44184 | 314 #ifdef HAVE_GETLINE |
315 score->data = NULL; | |
44566
7a0ad319b38f
(read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents:
44481
diff
changeset
|
316 errno = 0; |
44184 | 317 { |
44566
7a0ad319b38f
(read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents:
44481
diff
changeset
|
318 size_t len; |
49594 | 319 if (getline (&score->data, &len, f) < 0) |
44184 | 320 return -1; |
49594 | 321 score->data[strlen (score->data)-1] = '\0'; |
44184 | 322 } |
323 #else | |
44402
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 int cur = 0; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
326 int len = 16; |
49594 | 327 char *buf = malloc (len); |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
328 if (!buf) |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
329 return -1; |
49594 | 330 while ((c = getc (f)) != EOF |
44404
21e4d76a9e8a
Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents:
44402
diff
changeset
|
331 && c != '\n') |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
332 { |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
333 if (cur >= len-1) |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
334 { |
49594 | 335 if (!(buf = realloc (buf, len *= 2))) |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
336 return -1; |
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 buf[cur] = c; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
339 cur++; |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
340 } |
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
341 score->data = buf; |
44419
16b3622178f9
update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents:
44404
diff
changeset
|
342 score->data[cur] = '\0'; |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
343 } |
44184 | 344 #endif |
345 return 0; | |
346 } | |
347 | |
348 int | |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
349 read_scores (const char *filename, struct score_entry **scores, int *count) |
44184 | 350 { |
351 int readval, scorecount, cursize; | |
352 struct score_entry *ret; | |
49594 | 353 FILE *f = fopen (filename, "r"); |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
354 if (!f) |
44184 | 355 return -1; |
356 scorecount = 0; | |
357 cursize = 16; | |
49595
ca4fb31aae14
(push_score, read_scores): Cast values of malloc and realloc.
Richard M. Stallman <rms@gnu.org>
parents:
49594
diff
changeset
|
358 ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize); |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
359 if (!ret) |
44184 | 360 return -1; |
49594 | 361 while ((readval = read_score (f, &ret[scorecount])) == 0) |
44184 | 362 { |
363 /* We encoutered an error */ | |
364 if (readval < 0) | |
365 return -1; | |
366 scorecount++; | |
367 if (scorecount >= cursize) | |
368 { | |
50718
240f333f936c
(read_scores): Fix corruption of score files.
Juanma Barranquero <lekktu@gmail.com>
parents:
49600
diff
changeset
|
369 cursize *= 2; |
240f333f936c
(read_scores): Fix corruption of score files.
Juanma Barranquero <lekktu@gmail.com>
parents:
49600
diff
changeset
|
370 ret = (struct score_entry *) |
240f333f936c
(read_scores): Fix corruption of score files.
Juanma Barranquero <lekktu@gmail.com>
parents:
49600
diff
changeset
|
371 realloc (ret, (sizeof (struct score_entry) * cursize)); |
44184 | 372 if (!ret) |
373 return -1; | |
374 } | |
375 } | |
376 *count = scorecount; | |
377 *scores = ret; | |
378 return 0; | |
379 } | |
380 | |
381 int | |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
382 score_compare (const void *a, 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 (sb->score > sa->score) - (sb->score < sa->score); | |
387 } | |
388 | |
389 int | |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
390 score_compare_reverse (const void *a, const void *b) |
44184 | 391 { |
392 const struct score_entry *sa = (const struct score_entry *) a; | |
393 const struct score_entry *sb = (const struct score_entry *) b; | |
394 return (sa->score > sb->score) - (sa->score < sb->score); | |
395 } | |
396 | |
397 int | |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
398 push_score (struct score_entry **scores, int *count, int newscore, char *username, char *newdata) |
44184 | 399 { |
49594 | 400 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
|
401 = (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
|
402 sizeof (struct score_entry) * ((*count) + 1)); |
44184 | 403 if (!newscores) |
404 return -1; | |
405 newscores[*count].score = newscore; | |
44402
968fd5ccea65
(toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents:
44184
diff
changeset
|
406 newscores[*count].username = username; |
44184 | 407 newscores[*count].data = newdata; |
408 (*count) += 1; | |
409 *scores = newscores; | |
410 return 0; | |
411 } | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
412 |
44184 | 413 void |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
414 sort_scores (struct score_entry *scores, int count, int reverse) |
44184 | 415 { |
49594 | 416 qsort (scores, count, sizeof (struct score_entry), |
44184 | 417 reverse ? score_compare_reverse : score_compare); |
418 } | |
419 | |
420 int | |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
421 write_scores (const char *filename, const struct score_entry *scores, int count) |
44184 | 422 { |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
423 FILE *f; |
44184 | 424 int i; |
49594 | 425 char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1); |
44184 | 426 if (!tempfile) |
427 return -1; | |
49594 | 428 strcpy (tempfile, filename); |
429 strcat (tempfile, ".tempXXXXXX"); | |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
430 #ifdef HAVE_MKSTEMP |
49594 | 431 if (mkstemp (tempfile) < 0 |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
432 #else |
49594 | 433 if (mktemp (tempfile) != tempfile |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
434 #endif |
49594 | 435 || !(f = fopen (tempfile, "w"))) |
44184 | 436 return -1; |
437 for (i = 0; i < count; i++) | |
49594 | 438 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
|
439 scores[i].data) < 0) |
44184 | 440 return -1; |
49594 | 441 fclose (f); |
442 if (rename (tempfile, filename) < 0) | |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
443 return -1; |
49594 | 444 if (chmod (filename, 0644) < 0) |
44481
a0dc261f564a
(toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents:
44419
diff
changeset
|
445 return -1; |
44184 | 446 return 0; |
447 } | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
448 |
44184 | 449 int |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
450 lock_file (const char *filename, void **state) |
44184 | 451 { |
452 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
|
453 struct stat buf; |
44184 | 454 int attempts = 0; |
109744
fdbd24f8d999
Fix warnings produced by compiling with -Wwrite_strings (i.e. use const char *).
Jan D <jan.h.d@swipnet.se>
parents:
109511
diff
changeset
|
455 const char *lockext = ".lockfile"; |
49594 | 456 char *lockpath = malloc (strlen (filename) + strlen (lockext) + 60); |
44184 | 457 if (!lockpath) |
458 return -1; | |
49594 | 459 strcpy (lockpath, filename); |
460 strcat (lockpath, lockext); | |
44184 | 461 *state = lockpath; |
462 trylock: | |
463 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
|
464 /* If the lock is over an hour old, delete it. */ |
49594 | 465 if (stat (lockpath, &buf) == 0 |
466 && (difftime (buf.st_ctime, time (NULL) > 60*60))) | |
467 unlink (lockpath); | |
49595
ca4fb31aae14
(push_score, read_scores): Cast values of malloc and realloc.
Richard M. Stallman <rms@gnu.org>
parents:
49594
diff
changeset
|
468 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
|
469 if (fd < 0) |
44184 | 470 { |
471 if (errno == EEXIST) | |
472 { | |
473 /* Break the lock; we won't corrupt the file, but we might | |
474 lose some scores. */ | |
475 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
|
476 { |
49594 | 477 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
|
478 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
|
479 } |
49594 | 480 sleep ((rand () % 2)+1); |
44184 | 481 goto trylock; |
482 } | |
483 else | |
484 return -1; | |
485 } | |
49594 | 486 close (fd); |
44184 | 487 return 0; |
488 } | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49595
diff
changeset
|
489 |
44184 | 490 int |
109111
52b76722152a
Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
109100
diff
changeset
|
491 unlock_file (const char *filename, void *state) |
44184 | 492 { |
493 char *lockpath = (char *) state; | |
49594 | 494 int ret = unlink (lockpath); |
44184 | 495 int saved_errno = errno; |
49594 | 496 free (lockpath); |
44184 | 497 errno = saved_errno; |
498 return ret; | |
499 } | |
52401 | 500 |
501 /* arch-tag: 2bf5c52e-4beb-463a-954e-c58b9c64736b | |
502 (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
|
503 |
a47704955f8d
Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
504 /* update-game-score.c ends here */ |