annotate lib-src/update-game-score.c @ 44184:dca60454e83f

Initial version.
author Colin Walters <walters@gnu.org>
date Wed, 27 Mar 2002 20:57:06 +0000
parents
children 968fd5ccea65
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
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
22 score file. It should be installed setgid, owned by an appropriate
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
23 group like `games'.
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
24
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
25 Created 2002/03/22, by Colin Walters <walters@debian.org>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
26 */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
27
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
28 #define _GNU_SOURCE
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
29
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
30 #include <unistd.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
31 #include <errno.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
32 #include <string.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
33 #include <stdlib.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
34 #include <stdio.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
35 #include <time.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
36 #include <ctype.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
37 #include <fcntl.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
38 #include <sys/stat.h>
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
39 // #include "config.h"
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
40
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
41 #define MAX_ATTEMPTS 5
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
42 #define SCORE_FILE_PREFIX "/var/games/emacs/"
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
43
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
44 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
45 usage(int err)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
46 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
47 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
48 fprintf(stdout, " update-game-score -h\n");
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
49 fprintf(stdout, " -h\t\tDisplay this help.\n");
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
50 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
51 fprintf(stdout, " -r\t\tSort the scores in increasing order.\n");
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
52 exit(err);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
53 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
54
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
55 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
56 lock_file(const char *filename, void **state);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
57 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
58 unlock_file(const char *filename, void *state);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
59
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
60 struct score_entry
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
61 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
62 long score;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
63 char *data;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
64 };
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
65
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
66 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
67 read_scores(const char *filename, struct score_entry **scores,
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
68 int *count);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
69 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
70 push_score(struct score_entry **scores, int *count,
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
71 int newscore, char *newdata);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
72 void
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
73 sort_scores(struct score_entry *scores, int count, int reverse);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
74 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
75 write_scores(const char *filename, const struct score_entry *scores,
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
76 int count);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
77
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
78 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
79 main(int argc, char **argv)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
80 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
81 int c;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
82 void *lockstate;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
83 char *scorefile;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
84 struct stat buf;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
85 struct score_entry *scores;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
86 int newscore, scorecount, reverse = 0, max = -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
87 char *newdata;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
88
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
89 srand(time(0));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
90
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
91 while ((c = getopt(argc, argv, "hrm:")) != -1)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
92 switch (c)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
93 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
94 case 'h':
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
95 usage(0);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
96 break;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
97 case 'r':
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
98 reverse = 1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
99 break;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
100 case 'm':
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
101 max = atoi(optarg);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
102 break;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
103 default:
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
104 usage(1);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
105 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
106
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
107 if (optind+3 != argc)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
108 usage(1);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
109 scorefile = malloc(strlen(SCORE_FILE_PREFIX) + strlen(argv[optind]) + 20);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
110 if (!scorefile)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
111 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
112 fprintf(stderr, "Couldn't create score file name: %s\n",
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
113 strerror(errno));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
114 goto fail;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
115 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
116 strcpy(scorefile, SCORE_FILE_PREFIX);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
117 strcat(scorefile, argv[optind]);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
118 newscore = atoi(argv[optind+1]);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
119 newdata = argv[optind+2];
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
120
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
121 if (stat(scorefile, &buf) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
122 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
123 fprintf(stderr, "Failed to access scores file \"%s\": %s\n",
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
124 scorefile, strerror(errno));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
125 goto fail;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
126 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
127 if (lock_file(scorefile, &lockstate) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
128 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
129 fprintf(stderr, "Failed to lock scores file \"%s\": %s\n",
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
130 scorefile, strerror(errno));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
131 goto fail;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
132 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
133 if (read_scores(scorefile, &scores, &scorecount) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
134 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
135 fprintf(stderr, "Failed to read scores file \"%s\": %s\n",
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
136 scorefile, strerror(errno));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
137 goto fail_unlock;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
138 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
139 push_score(&scores, &scorecount, newscore, newdata);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
140 sort_scores(scores, scorecount, reverse);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
141 if (write_scores(scorefile, scores, scorecount) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
142 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
143 fprintf(stderr, "Failed to write scores file \"%s\": %s\n",
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
144 scorefile, strerror(errno));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
145 goto fail_unlock;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
146 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
147 unlock_file(scorefile, lockstate);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
148 exit(0);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
149 fail_unlock:
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
150 unlock_file(scorefile, lockstate);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
151 fail:
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
152 exit(1);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
153 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
154
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
155 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
156 read_score(FILE *f, struct score_entry *score)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
157 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
158 int c;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
159 if (feof(f))
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
160 return 1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
161 while ((c = getc(f)) != EOF
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
162 && isdigit(c)) {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
163 score->score *= 10;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
164 score->score += (c-48);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
165 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
166 #ifdef HAVE_GETLINE
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
167 score->data = NULL;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
168 errno = ESUCCES;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
169 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
170 int len;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
171 if (getline(&score->data, &len, f) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
172 return -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 #else
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
175 /* We should probably just copy the getline code into here from
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
176 glibc, instead of this halfassed solution. */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
177 score->data = malloc(122);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
178 score->data[0] = '\0';
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
179 if (!fgets(score->data, 120, f))
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
180 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
181 #endif
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
182 /* Trim the newline */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
183 score->data[strlen(score->data)-1] = '\0';
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
184 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
185 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
186
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
187 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
188 read_scores(const char *filename, struct score_entry **scores,
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
189 int *count)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
190 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
191 int readval, scorecount, cursize;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
192 struct score_entry *ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
193 FILE *f = fopen(filename, "r");
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
194 if (!f)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
195 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
196 scorecount = 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
197 cursize = 16;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
198 ret = malloc(sizeof(struct score_entry) * cursize);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
199 if (!ret)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
200 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
201 while ((readval = read_score(f, &ret[scorecount])) == 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
202 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
203 /* We encoutered an error */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
204 if (readval < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
205 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
206 scorecount++;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
207 if (scorecount >= cursize)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
208 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
209 ret = realloc(ret, cursize *= 2);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
210 if (!ret)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
211 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
212 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
213 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
214 *count = scorecount;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
215 *scores = ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
216 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
217 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
218
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
219 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
220 score_compare(const void *a, const void *b)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
221 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
222 const struct score_entry *sa = (const struct score_entry *) a;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
223 const struct score_entry *sb = (const struct score_entry *) b;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
224 return (sb->score > sa->score) - (sb->score < sa->score);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
225 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
226
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
227 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
228 score_compare_reverse(const void *a, const void *b)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
229 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
230 const struct score_entry *sa = (const struct score_entry *) a;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
231 const struct score_entry *sb = (const struct score_entry *) b;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
232 return (sa->score > sb->score) - (sa->score < sb->score);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
233 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
234
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
235 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
236 push_score(struct score_entry **scores, int *count,
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
237 int newscore, char *newdata)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
238 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
239 struct score_entry *newscores = realloc(*scores,
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
240 sizeof(struct score_entry) * ((*count) + 1));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
241 if (!newscores)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
242 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
243 newscores[*count].score = newscore;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
244 newscores[*count].data = newdata;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
245 (*count) += 1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
246 *scores = newscores;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
247 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
248 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
249
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
250 void
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
251 sort_scores(struct score_entry *scores, int count, int reverse)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
252 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
253 qsort(scores, count, sizeof(struct score_entry),
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
254 reverse ? score_compare_reverse : score_compare);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
255 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
256
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
257 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
258 write_scores(const char *filename, const struct score_entry *scores,
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
259 int count)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
260 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
261 FILE *f;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
262 int i;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
263 char *tempfile = malloc(strlen(filename) + strlen(".tempXXXXXX") + 1);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
264 if (!tempfile)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
265 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
266 strcpy(tempfile, filename);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
267 strcat(tempfile, ".tempXXXXXX");
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
268 if (mkstemp(tempfile) < 0
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
269 || !(f = fopen(tempfile, "w")))
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
270 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
271 for (i = 0; i < count; i++)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
272 if (fprintf(f, "%ld %s\n", scores[i].score, scores[i].data) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
273 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
274 fclose(f);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
275 rename(tempfile, filename);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
276 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
277 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
278
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
279 #if 0
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
280 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
281 lock_file(const char *filename, void **state)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
282 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
283 struct flock lock;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
284 int *istate;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
285 int fd, attempts = 0, ret = 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
286 lock.l_type = F_WRLCK;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
287 lock.l_whence = SEEK_SET;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
288 lock.l_start = 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
289 lock.l_len = 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
290 istate = malloc(sizeof(int));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
291 if (!istate)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
292 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
293 if ((fd = open(filename, O_RDWR, 0600)) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
294 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
295 *istate = fd;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
296 trylock:
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
297 attempts++;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
298 if ((ret = fcntl(fd, F_GETLK, &lock)) == -1)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
299 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
300 if (ret == EACCES || ret == EAGAIN)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
301 if (attempts > MAX_ATTEMPTS)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
302 exit(1);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
303 else
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
304 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
305 sleep((rand() % 3)+1);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
306 goto trylock;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
307 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
308 else
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
309 ret = 0 ;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
310 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
311 else
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
312 ret = 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
313 *state = istate;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
314 return ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
315 }
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
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
318 unlock_file(const char *filename, void *state)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
319 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
320 int fd, ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
321 fd = *((int *) state);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
322 free(state);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
323 ret = close(fd);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
324 return ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
325 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
326
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
327 #else
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
328
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
329 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
330 lock_file(const char *filename, void **state)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
331 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
332 int fd;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
333 int attempts = 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
334 char *lockext = ".lockfile";
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
335 char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
336 if (!lockpath)
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 strcpy(lockpath, filename);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
339 strcat(lockpath, lockext);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
340 *state = lockpath;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
341 trylock:
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
342 attempts++;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
343 if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 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 if (errno == EEXIST)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
346 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
347 /* Break the lock; we won't corrupt the file, but we might
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
348 lose some scores. */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
349 if (attempts > MAX_ATTEMPTS)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
350 unlink(lockpath);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
351 else
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
352 sleep((rand() % 2)+1);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
353 goto trylock;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
354 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
355 else
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
356 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
357 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
358 close(fd);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
359 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
360 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
361
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
362 int
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
363 unlock_file(const char *filename, void *state)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
364 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
365 char *lockpath = (char *) state;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
366 int ret = unlink(lockpath);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
367 int saved_errno = errno;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
368 free(lockpath);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
369 errno = saved_errno;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
370 return ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
371 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
372
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
373 #endif