annotate lib-src/update-game-score.c @ 112318:deae5bb3f0f6

Give a name FLOAT_TO_STRING_BUFSIZE to the constant 350. * lisp.h (FLOAT_TO_STRING_BUFSIZE): New macro. * data.c (Fnumber_to_string): Use it. * print.c (float_to_string, print_object): Likewise.
author Paul Eggert <eggert@cs.ucla.edu>
date Sun, 09 Jan 2011 00:21:21 -0800
parents 7df2e30d72ec
children 17e0028efc29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
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
1d1d5d9bd884 Add 2010 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 105171
diff changeset
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
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
7
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
8 This file is part of GNU Emacs.
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
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
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
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
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
14
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
15 GNU Emacs is distributed in the hope that it will be useful,
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
18 GNU General Public License for more details.
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
19
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
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
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
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
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
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
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
33 */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
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
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
37 #include <unistd.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
38 #include <errno.h>
48422
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
39 #ifdef HAVE_STRING_H
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
40 #include <string.h>
48422
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
41 #endif
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
42 #ifdef HAVE_STDLIB_H
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
43 #include <stdlib.h>
48422
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
44 #endif
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
45 #include <stdio.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
46 #include <time.h>
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
47 #include <pwd.h>
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
48 #include <ctype.h>
48422
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
49 #ifdef HAVE_FCNTL_H
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
50 #include <fcntl.h>
48422
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
51 #endif
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
52 #ifdef STDC_HEADERS
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
53 #include <stdarg.h>
48422
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
54 #endif
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
55 #include <sys/stat.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
56
48422
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
57 /* Needed for SunOS4, for instance. */
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
58 extern char *optarg;
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
59 extern int optind, opterr;
2346caa2a66a Include unistd.h, string.h, stdlib.h,
Dave Love <fx@gnu.org>
parents: 46774
diff changeset
60
109511
09a43f890565 Add NO_RETURN specifiers to functions in lib-src.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109172
diff changeset
61 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
62
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
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
51146
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
67 #ifndef HAVE_DIFFTIME
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
68 /* 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
69 #define difftime(t1, t0) (double)((t1) - (t0))
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
70 #endif
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
71
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
72 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
73 usage (int err)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
74 {
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
75 fprintf (stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
76 fprintf (stdout, " update-game-score -h\n");
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
77 fprintf (stdout, " -h\t\tDisplay this help.\n");
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
78 fprintf (stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
79 fprintf (stdout, " -r\t\tSort the scores in increasing order.\n");
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
80 fprintf (stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
81 exit (err);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
82 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
83
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
84 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
85 int unlock_file (const char *filename, void *state);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
86
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
87 struct score_entry
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
88 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
89 long score;
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
90 char *username;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
91 char *data;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
92 };
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
93
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
94 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
95 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
96 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
97 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
98 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
99 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
100 int count);
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
101
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
102 void lose (const char *msg) NO_RETURN;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
103
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
104 void
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
105 lose (const char *msg)
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
106 {
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
107 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
108 exit (EXIT_FAILURE);
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
109 }
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
110
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
111 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
112
51146
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
113 /* Taken from sysdep.c. */
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
114 #ifndef HAVE_STRERROR
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
115 #ifndef WINDOWSNT
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
116 char *
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
117 strerror (errnum)
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
118 int errnum;
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
119 {
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
120 extern char *sys_errlist[];
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
121 extern int sys_nerr;
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
122
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
123 if (errnum >= 0 && errnum < sys_nerr)
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
124 return sys_errlist[errnum];
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
125 return (char *) "Unknown error";
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
126 }
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
127 #endif /* not WINDOWSNT */
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
128 #endif /* ! HAVE_STRERROR */
f20c52ac1b8d (difftime) [!HAVE_DIFFTIME]: Define.
Dave Love <fx@gnu.org>
parents: 50718
diff changeset
129
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
130 void
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
131 lose_syserr (const char *msg)
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
132 {
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
133 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
134 exit (EXIT_FAILURE);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
135 }
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
136
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
137 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
138 get_user_id (void)
44987
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 char *name;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
141 struct passwd *buf = getpwuid (getuid ());
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
142 if (!buf)
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
143 {
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
144 int count = 1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
145 int uid = (int) getuid ();
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
146 int tuid = uid;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
147 while (tuid /= 10)
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
148 count++;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
149 name = malloc (count+1);
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
150 if (!name)
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
151 return NULL;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
152 sprintf (name, "%d", uid);
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
153 return name;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
154 }
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
155 return buf->pw_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
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
158 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
159 get_prefix (int running_suid, const char *user_prefix)
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
160 {
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
161 if (!running_suid && user_prefix == NULL)
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
162 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
163 if (running_suid)
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 #ifdef HAVE_SHARED_GAME_DIR
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
166 return HAVE_SHARED_GAME_DIR;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
167 #else
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
168 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
169 #endif
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
170 }
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
171 return user_prefix;
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
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
174 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
175 main (int argc, char **argv)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
176 {
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
177 int c, running_suid;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
178 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
179 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
180 const char *prefix, *user_prefix = NULL;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
181 struct stat buf;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
182 struct score_entry *scores;
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
183 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
184 char *newdata;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
185
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
186 srand (time (0));
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
187
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
188 while ((c = getopt (argc, argv, "hrm:d:")) != -1)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
189 switch (c)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
190 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
191 case 'h':
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 52401
diff changeset
192 usage (EXIT_SUCCESS);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
193 break;
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
194 case 'd':
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
195 user_prefix = optarg;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
196 break;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
197 case 'r':
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
198 reverse = 1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
199 break;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
200 case 'm':
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
201 max = atoi (optarg);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
202 if (max > MAX_SCORES)
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
203 max = MAX_SCORES;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
204 break;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
205 default:
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 52401
diff changeset
206 usage (EXIT_FAILURE);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
207 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
208
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
209 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
210 usage (EXIT_FAILURE);
44419
16b3622178f9 update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents: 44404
diff changeset
211
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
212 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
213
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
214 prefix = get_prefix (running_suid, user_prefix);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
215
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
216 scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
217 if (!scorefile)
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
218 lose_syserr ("Couldn't allocate score file");
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
219
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
220 strcpy (scorefile, prefix);
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
221 strcat (scorefile, "/");
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
222 strcat (scorefile, argv[optind]);
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
223 newscore = atoi (argv[optind+1]);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
224 newdata = argv[optind+2];
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
225 if (strlen (newdata) > MAX_DATA_LEN)
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
226 newdata[MAX_DATA_LEN] = '\0';
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
227
49595
ca4fb31aae14 (push_score, read_scores): Cast values of malloc and realloc.
Richard M. Stallman <rms@gnu.org>
parents: 49594
diff changeset
228 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
229 if (user_id == NULL)
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
230 lose_syserr ("Couldn't determine user id");
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
231
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
232 if (stat (scorefile, &buf) < 0)
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
233 lose_syserr ("Failed to access scores file");
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
234
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
235 if (lock_file (scorefile, &lockstate) < 0)
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
236 lose_syserr ("Failed to lock scores file");
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
237
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
238 if (read_scores (scorefile, &scores, &scorecount) < 0)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
239 {
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
240 unlock_file (scorefile, lockstate);
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
241 lose_syserr ("Failed to read scores file");
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
242 }
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
243 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
244 sort_scores (scores, scorecount, reverse);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
245 /* 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
246 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
247 *smallest* scores. Otherwise, we just decrement the number of
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
248 scores, since the smallest will be at the end. */
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
249 if (scorecount > MAX_SCORES)
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
250 scorecount -= (scorecount - MAX_SCORES);
104955
be5f5f7e93cb (main): Sort scores before trimming them,
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 100958
diff changeset
251 if (reverse)
be5f5f7e93cb (main): Sort scores before trimming them,
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 100958
diff changeset
252 scores += (scorecount - MAX_SCORES);
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
253 if (write_scores (scorefile, scores, scorecount) < 0)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
254 {
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
255 unlock_file (scorefile, lockstate);
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
256 lose_syserr ("Failed to write scores file");
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
257 }
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
258 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
259 exit (EXIT_SUCCESS);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
260 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
261
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
262 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
263 read_score (FILE *f, struct score_entry *score)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
264 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
265 int c;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
266 if (feof (f))
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
267 return 1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
268 while ((c = getc (f)) != EOF
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
269 && isdigit (c))
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
270 {
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
271 score->score *= 10;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
272 score->score += (c-48);
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
273 }
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
274 while ((c = getc (f)) != EOF
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
275 && isspace (c))
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
276 ;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
277 if (c == EOF)
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
278 return -1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
279 ungetc (c, f);
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
280 #ifdef HAVE_GETDELIM
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
281 {
44566
7a0ad319b38f (read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents: 44481
diff changeset
282 size_t count = 0;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
283 if (getdelim (&score->username, &count, ' ', f) < 1
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
284 || score->username == NULL)
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
285 return -1;
44789
d095e59dc01d (read_score) [HAVE_GETDELIM]: Trim trailing space.
Colin Walters <walters@gnu.org>
parents: 44639
diff changeset
286 /* Trim the space */
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
287 score->username[strlen (score->username)-1] = '\0';
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
288 }
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
289 #else
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
290 {
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
291 int unameread = 0;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
292 int unamelen = 30;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
293 char *username = malloc (unamelen);
44404
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
294 if (!username)
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
295 return -1;
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
296
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
297 while ((c = getc (f)) != EOF
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
298 && !isspace (c))
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
299 {
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
300 if (unameread >= unamelen-1)
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
301 if (!(username = realloc (username, unamelen *= 2)))
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
302 return -1;
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
303 username[unameread] = c;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
304 unameread++;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
305 }
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
306 if (c == EOF)
44404
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
307 return -1;
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
308 username[unameread] = '\0';
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
309 score->username = username;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
310 }
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
311 #endif
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
312 #ifdef HAVE_GETLINE
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
313 score->data = NULL;
44566
7a0ad319b38f (read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents: 44481
diff changeset
314 errno = 0;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
315 {
44566
7a0ad319b38f (read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents: 44481
diff changeset
316 size_t len;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
317 if (getline (&score->data, &len, f) < 0)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
318 return -1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
319 score->data[strlen (score->data)-1] = '\0';
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
320 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
321 #else
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
322 {
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
323 int cur = 0;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
324 int len = 16;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
325 char *buf = malloc (len);
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
326 if (!buf)
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
327 return -1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
328 while ((c = getc (f)) != EOF
44404
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
329 && c != '\n')
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
330 {
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
331 if (cur >= len-1)
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
332 {
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
333 if (!(buf = realloc (buf, len *= 2)))
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
334 return -1;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
335 }
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
336 buf[cur] = c;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
337 cur++;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
338 }
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
339 score->data = buf;
44419
16b3622178f9 update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents: 44404
diff changeset
340 score->data[cur] = '\0';
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
341 }
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
342 #endif
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
343 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
344 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
345
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
346 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
347 read_scores (const char *filename, struct score_entry **scores, int *count)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
348 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
349 int readval, scorecount, cursize;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
350 struct score_entry *ret;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
351 FILE *f = fopen (filename, "r");
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
352 if (!f)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
353 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
354 scorecount = 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
355 cursize = 16;
49595
ca4fb31aae14 (push_score, read_scores): Cast values of malloc and realloc.
Richard M. Stallman <rms@gnu.org>
parents: 49594
diff changeset
356 ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
357 if (!ret)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
358 return -1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
359 while ((readval = read_score (f, &ret[scorecount])) == 0)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
360 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
361 /* We encoutered an error */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
362 if (readval < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
363 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
364 scorecount++;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
365 if (scorecount >= cursize)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
366 {
50718
240f333f936c (read_scores): Fix corruption of score files.
Juanma Barranquero <lekktu@gmail.com>
parents: 49600
diff changeset
367 cursize *= 2;
240f333f936c (read_scores): Fix corruption of score files.
Juanma Barranquero <lekktu@gmail.com>
parents: 49600
diff changeset
368 ret = (struct score_entry *)
240f333f936c (read_scores): Fix corruption of score files.
Juanma Barranquero <lekktu@gmail.com>
parents: 49600
diff changeset
369 realloc (ret, (sizeof (struct score_entry) * cursize));
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
370 if (!ret)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
371 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
372 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
373 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
374 *count = scorecount;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
375 *scores = ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
376 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
377 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
378
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
379 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
380 score_compare (const void *a, const void *b)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
381 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
382 const struct score_entry *sa = (const struct score_entry *) a;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
383 const struct score_entry *sb = (const struct score_entry *) b;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
384 return (sb->score > sa->score) - (sb->score < sa->score);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
385 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
386
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
387 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
388 score_compare_reverse (const void *a, const void *b)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
389 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
390 const struct score_entry *sa = (const struct score_entry *) a;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
391 const struct score_entry *sb = (const struct score_entry *) b;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
392 return (sa->score > sb->score) - (sa->score < sb->score);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
393 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
394
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
395 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
396 push_score (struct score_entry **scores, int *count, int newscore, char *username, char *newdata)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
397 {
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
398 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
399 = (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
400 sizeof (struct score_entry) * ((*count) + 1));
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
401 if (!newscores)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
402 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
403 newscores[*count].score = newscore;
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
404 newscores[*count].username = username;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
405 newscores[*count].data = newdata;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
406 (*count) += 1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
407 *scores = newscores;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
408 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
409 }
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
410
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
411 void
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
412 sort_scores (struct score_entry *scores, int count, int reverse)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
413 {
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
414 qsort (scores, count, sizeof (struct score_entry),
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
415 reverse ? score_compare_reverse : score_compare);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
416 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
417
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
418 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
419 write_scores (const char *filename, const struct score_entry *scores, int count)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
420 {
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
421 FILE *f;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
422 int i;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
423 char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
424 if (!tempfile)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
425 return -1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
426 strcpy (tempfile, filename);
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
427 strcat (tempfile, ".tempXXXXXX");
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
428 #ifdef HAVE_MKSTEMP
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
429 if (mkstemp (tempfile) < 0
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
430 #else
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
431 if (mktemp (tempfile) != tempfile
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
432 #endif
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
433 || !(f = fopen (tempfile, "w")))
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
434 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
435 for (i = 0; i < count; i++)
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
436 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
437 scores[i].data) < 0)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
438 return -1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
439 fclose (f);
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
440 if (rename (tempfile, filename) < 0)
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
441 return -1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
442 if (chmod (filename, 0644) < 0)
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
443 return -1;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
444 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
445 }
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
446
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
447 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
448 lock_file (const char *filename, void **state)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
449 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
450 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
451 struct stat buf;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
452 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
453 const char *lockext = ".lockfile";
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
454 char *lockpath = malloc (strlen (filename) + strlen (lockext) + 60);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
455 if (!lockpath)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
456 return -1;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
457 strcpy (lockpath, filename);
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
458 strcat (lockpath, lockext);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
459 *state = lockpath;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
460 trylock:
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
461 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
462 /* If the lock is over an hour old, delete it. */
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
463 if (stat (lockpath, &buf) == 0
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
464 && (difftime (buf.st_ctime, time (NULL) > 60*60)))
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
465 unlink (lockpath);
49595
ca4fb31aae14 (push_score, read_scores): Cast values of malloc and realloc.
Richard M. Stallman <rms@gnu.org>
parents: 49594
diff changeset
466 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
467 if (fd < 0)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
468 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
469 if (errno == EEXIST)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
470 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
471 /* Break the lock; we won't corrupt the file, but we might
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
472 lose some scores. */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
473 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
474 {
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
475 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
476 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
477 }
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
478 sleep ((rand () % 2)+1);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
479 goto trylock;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
480 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
481 else
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
482 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
483 }
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
484 close (fd);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
485 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
486 }
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 49595
diff changeset
487
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
488 int
109111
52b76722152a Convert function definitions to standard C.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 109100
diff changeset
489 unlock_file (const char *filename, void *state)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
490 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
491 char *lockpath = (char *) state;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
492 int ret = unlink (lockpath);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
493 int saved_errno = errno;
49594
a29fefe5c6a9 Whitespace changes.
Richard M. Stallman <rms@gnu.org>
parents: 48422
diff changeset
494 free (lockpath);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
495 errno = saved_errno;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
496 return ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
497 }
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 51146
diff changeset
498
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 51146
diff changeset
499 /* arch-tag: 2bf5c52e-4beb-463a-954e-c58b9c64736b
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 51146
diff changeset
500 (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
501
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 52401
diff changeset
502 /* update-game-score.c ends here */