annotate lib-src/update-game-score.c @ 46654:70e2943a7427

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