annotate lib-src/update-game-score.c @ 47456:6289c5b94789

*** empty log message ***
author Kim F. Storm <storm@cua.dk>
date Fri, 13 Sep 2002 07:18:05 +0000
parents 7411dc9e5008
children 2346caa2a66a
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
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
56 /* Declare the prototype for a general external function. */
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
57 #if defined (PROTOTYPES) || defined (WINDOWSNT)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
58 #define P_(proto) proto
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
59 #else
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
60 #define P_(proto) ()
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
61 #endif
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
62
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
63 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
64 usage(err)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
65 int err;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
66 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
67 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
68 fprintf(stdout, " update-game-score -h\n");
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
69 fprintf(stdout, " -h\t\tDisplay this help.\n");
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
70 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
71 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
72 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
73 exit(err);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
74 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
75
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
76 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
77 lock_file P_((const char *filename, void **state));
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
78 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
79 unlock_file P_((const char *filename, void *state));
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
80
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
81 struct score_entry
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
82 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
83 long score;
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
84 char *username;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
85 char *data;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
86 };
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
87
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
88 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
89 read_scores P_((const char *filename, struct score_entry **scores,
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
90 int *count));
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
91 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
92 push_score P_((struct score_entry **scores, int *count,
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
93 int newscore, char *username, char *newdata));
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
94 void
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
95 sort_scores P_((struct score_entry *scores, int count, int reverse));
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
96 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
97 write_scores P_((const char *filename, const struct score_entry *scores,
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
98 int count));
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
99
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
100 void lose P_((const char *msg))
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
101 __attribute__ ((noreturn));
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
102
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
103 void lose(msg)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
104 const char *msg;
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
105 {
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
106 fprintf(stderr, "%s\n", msg);
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
107 exit(1);
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
108 }
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
109
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
110 void lose_syserr P_((const char *msg))
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
111 __attribute__ ((noreturn));
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
112
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
113 void lose_syserr(msg)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
114 const char *msg;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
115 {
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
116 fprintf(stderr, "%s: %s\n", msg, strerror(errno));
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
117 exit(1);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
118 }
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
119
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
120 char *
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
121 get_user_id(void)
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
122 {
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
123 char *name;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
124 struct passwd *buf = getpwuid(getuid());
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
125 if (!buf)
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
126 {
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
127 int count = 1;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
128 int uid = (int) getuid();
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
129 int tuid = uid;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
130 while (tuid /= 10)
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
131 count++;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
132 name = malloc(count+1);
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
133 if (!name)
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
134 return NULL;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
135 sprintf(name, "%d", uid);
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
136 return name;
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 buf->pw_name;
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
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
141 char *
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
142 get_prefix(running_suid, user_prefix)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
143 int running_suid;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
144 char *user_prefix;
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
145 {
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
146 if (!running_suid && user_prefix == NULL)
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
147 lose("Not using a shared game directory, and no prefix given.");
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
148 if (running_suid)
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
149 {
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
150 #ifdef HAVE_SHARED_GAME_DIR
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
151 return HAVE_SHARED_GAME_DIR;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
152 #else
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
153 lose("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
154 #endif
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
155 }
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
156 return user_prefix;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
157 }
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
158
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
159 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
160 main(argc, argv)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
161 int argc;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
162 char **argv;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
163 {
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
164 int c, running_suid;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
165 void *lockstate;
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
166 char *user_id, *scorefile, *prefix, *user_prefix = NULL;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
167 struct stat buf;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
168 struct score_entry *scores;
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
169 int newscore, scorecount, reverse = 0, max = MAX_SCORES;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
170 char *newdata;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
171
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
172 srand(time(0));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
173
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
174 while ((c = getopt(argc, argv, "hrm:d:")) != -1)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
175 switch (c)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
176 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
177 case 'h':
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
178 usage(0);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
179 break;
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
180 case 'd':
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
181 user_prefix = optarg;
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
182 break;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
183 case 'r':
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
184 reverse = 1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
185 break;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
186 case 'm':
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
187 max = atoi(optarg);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
188 if (max > MAX_SCORES)
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
189 max = MAX_SCORES;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
190 break;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
191 default:
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
192 usage(1);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
193 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
194
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
195 if (optind+3 != argc)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
196 usage(1);
44419
16b3622178f9 update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents: 44404
diff changeset
197
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
198 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
199
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
200 prefix = get_prefix(running_suid, user_prefix);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
201
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
202 scorefile = malloc(strlen(prefix) + strlen(argv[optind]) + 2);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
203 if (!scorefile)
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
204 lose_syserr("Couldn't allocate score file");
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
205
44419
16b3622178f9 update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents: 44404
diff changeset
206 strcpy(scorefile, prefix);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
207 strcat(scorefile, "/");
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
208 strcat(scorefile, argv[optind]);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
209 newscore = atoi(argv[optind+1]);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
210 newdata = argv[optind+2];
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
211 if (strlen(newdata) > MAX_DATA_LEN)
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
212 newdata[MAX_DATA_LEN] = '\0';
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
213
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
214 if ((user_id = get_user_id()) == NULL)
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
215 lose_syserr("Couldn't determine user id");
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
216
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
217 if (stat(scorefile, &buf) < 0)
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
218 lose_syserr("Failed to access scores file");
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
219
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
220 if (lock_file(scorefile, &lockstate) < 0)
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
221 lose_syserr("Failed to lock scores file");
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
222
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
223 if (read_scores(scorefile, &scores, &scorecount) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
224 {
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
225 unlock_file(scorefile, lockstate);
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
226 lose_syserr("Failed to read scores file");
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
227 }
44987
cc0ab9acdc46 (SCORE_FILE_PREFIX): Delete.
Colin Walters <walters@gnu.org>
parents: 44789
diff changeset
228 push_score(&scores, &scorecount, newscore, user_id, newdata);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
229 /* 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
230 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
231 *smallest* scores. Otherwise, we just decrement the number of
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
232 scores, since the smallest will be at the end. */
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
233 if (scorecount > MAX_SCORES)
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
234 scorecount -= (scorecount - MAX_SCORES);
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
235 if (reverse)
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
236 scores += (scorecount - MAX_SCORES);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
237 sort_scores(scores, scorecount, reverse);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
238 if (write_scores(scorefile, scores, scorecount) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
239 {
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
240 unlock_file(scorefile, lockstate);
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
241 lose_syserr("Failed to write scores file");
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
242 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
243 unlock_file(scorefile, lockstate);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
244 exit(0);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
245 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
246
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
247 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
248 read_score(f, score)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
249 FILE *f;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
250 struct score_entry *score;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
251 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
252 int c;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
253 if (feof(f))
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
254 return 1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
255 while ((c = getc(f)) != EOF
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
256 && isdigit(c))
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 score->score *= 10;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
259 score->score += (c-48);
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
260 }
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
261 while ((c = getc(f)) != EOF
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
262 && isspace(c))
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 if (c == EOF)
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
265 return -1;
44404
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
266 ungetc(c, f);
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
267 #ifdef HAVE_GETDELIM
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
268 {
44566
7a0ad319b38f (read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents: 44481
diff changeset
269 size_t count = 0;
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
270 if (getdelim(&score->username, &count, ' ', f) < 1
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
271 || score->username == NULL)
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
272 return -1;
44789
d095e59dc01d (read_score) [HAVE_GETDELIM]: Trim trailing space.
Colin Walters <walters@gnu.org>
parents: 44639
diff changeset
273 /* Trim the space */
d095e59dc01d (read_score) [HAVE_GETDELIM]: Trim trailing space.
Colin Walters <walters@gnu.org>
parents: 44639
diff changeset
274 score->username[strlen(score->username)-1] = '\0';
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
275 }
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
276 #else
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 int unameread = 0;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
279 int unamelen = 30;
44404
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
280 char *username = malloc(unamelen);
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
281 if (!username)
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
282 return -1;
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
283
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
284 while ((c = getc(f)) != EOF
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
285 && !isspace(c))
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
286 {
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
287 if (unameread >= unamelen-1)
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
288 if (!(username = realloc(username, unamelen *= 2)))
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
289 return -1;
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
290 username[unameread] = c;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
291 unameread++;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
292 }
44404
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
293 if (c == EOF)
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
294 return -1;
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
295 username[unameread] = '\0';
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
296 score->username = username;
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 #endif
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
299 #ifdef HAVE_GETLINE
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
300 score->data = NULL;
44566
7a0ad319b38f (read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents: 44481
diff changeset
301 errno = 0;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
302 {
44566
7a0ad319b38f (read_score): Fix type of second parameter
Andreas Schwab <schwab@suse.de>
parents: 44481
diff changeset
303 size_t len;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
304 if (getline(&score->data, &len, f) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
305 return -1;
44419
16b3622178f9 update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents: 44404
diff changeset
306 score->data[strlen(score->data)-1] = '\0';
44184
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
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
309 {
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
310 int cur = 0;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
311 int len = 16;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
312 char *buf = malloc(len);
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
313 if (!buf)
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
314 return -1;
44404
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
315 while ((c = getc(f)) != EOF
21e4d76a9e8a Actually make previous changes work (oops).
Colin Walters <walters@gnu.org>
parents: 44402
diff changeset
316 && c != '\n')
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
317 {
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
318 if (cur >= len-1)
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
319 {
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
320 if (!(buf = realloc(buf, len *= 2)))
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
321 return -1;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
322 }
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
323 buf[cur] = c;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
324 cur++;
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
325 }
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
326 score->data = buf;
44419
16b3622178f9 update-game-score.c (SCORE_FILE_PREFIX): Don't hardcode.
Colin Walters <walters@gnu.org>
parents: 44404
diff changeset
327 score->data[cur] = '\0';
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
328 }
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
329 #endif
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
330 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
331 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
332
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
333 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
334 read_scores(filename, scores, count)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
335 const char *filename;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
336 struct score_entry **scores;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
337 int *count;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
338 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
339 int readval, scorecount, cursize;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
340 struct score_entry *ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
341 FILE *f = fopen(filename, "r");
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
342 if (!f)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
343 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
344 scorecount = 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
345 cursize = 16;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
346 ret = malloc(sizeof(struct score_entry) * cursize);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
347 if (!ret)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
348 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
349 while ((readval = read_score(f, &ret[scorecount])) == 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
350 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
351 /* We encoutered an error */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
352 if (readval < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
353 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
354 scorecount++;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
355 if (scorecount >= cursize)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
356 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
357 ret = realloc(ret, cursize *= 2);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
358 if (!ret)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
359 return -1;
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 *count = scorecount;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
363 *scores = ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
364 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
365 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
366
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
367 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
368 score_compare(a, b)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
369 const void *a;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
370 const void *b;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
371 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
372 const struct score_entry *sa = (const struct score_entry *) a;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
373 const struct score_entry *sb = (const struct score_entry *) b;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
374 return (sb->score > sa->score) - (sb->score < sa->score);
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 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
378 score_compare_reverse(a, b)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
379 const void *a;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
380 const void *b;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
381 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
382 const struct score_entry *sa = (const struct score_entry *) a;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
383 const struct score_entry *sb = (const struct score_entry *) b;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
384 return (sa->score > sb->score) - (sa->score < sb->score);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
385 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
386
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
387 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
388 push_score(scores, count, newscore, username, newdata)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
389 struct score_entry **scores;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
390 int *count; int newscore;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
391 char *username;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
392 char *newdata;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
393 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
394 struct score_entry *newscores = realloc(*scores,
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
395 sizeof(struct score_entry) * ((*count) + 1));
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
396 if (!newscores)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
397 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
398 newscores[*count].score = newscore;
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
399 newscores[*count].username = username;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
400 newscores[*count].data = newdata;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
401 (*count) += 1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
402 *scores = newscores;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
403 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
404 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
405
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
406 void
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
407 sort_scores(scores, count, reverse)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
408 struct score_entry *scores;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
409 int count;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
410 int reverse;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
411 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
412 qsort(scores, count, sizeof(struct score_entry),
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
413 reverse ? score_compare_reverse : score_compare);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
414 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
415
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
416 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
417 write_scores(filename, scores, count)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
418 const char *filename;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
419 const struct score_entry * scores;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
420 int count;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
421 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
422 FILE *f;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
423 int i;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
424 char *tempfile = malloc(strlen(filename) + strlen(".tempXXXXXX") + 1);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
425 if (!tempfile)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
426 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
427 strcpy(tempfile, filename);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
428 strcat(tempfile, ".tempXXXXXX");
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
429 #ifdef HAVE_MKSTEMP
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
430 if (mkstemp(tempfile) < 0
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
431 #else
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
432 if (mktemp(tempfile) != tempfile
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
433 #endif
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
434 || !(f = fopen(tempfile, "w")))
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
435 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
436 for (i = 0; i < count; i++)
44402
968fd5ccea65 (toplevel): Include pwd.h.
Colin Walters <walters@gnu.org>
parents: 44184
diff changeset
437 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
438 scores[i].data) < 0)
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
439 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
440 fclose(f);
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
441 if (rename(tempfile, filename) < 0)
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
442 return -1;
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
443 if (chmod(filename, 0644) < 0)
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
444 return -1;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
445 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
446 }
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
447
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
448 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
449 lock_file(filename, state)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
450 const char *filename;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
451 void **state;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
452 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
453 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
454 struct stat buf;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
455 int attempts = 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
456 char *lockext = ".lockfile";
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
457 char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
458 if (!lockpath)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
459 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
460 strcpy(lockpath, filename);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
461 strcat(lockpath, lockext);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
462 *state = lockpath;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
463 trylock:
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
464 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
465 /* 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
466 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
467 && (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
468 unlink(lockpath);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
469 if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
470 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
471 if (errno == EEXIST)
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
472 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
473 /* Break the lock; we won't corrupt the file, but we might
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
474 lose some scores. */
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
475 if (attempts > MAX_ATTEMPTS)
44576
90866353c7bd (lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
parents: 44566
diff changeset
476 {
90866353c7bd (lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
parents: 44566
diff changeset
477 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
478 attempts = 0;
90866353c7bd (lock_file): If the lock file is older than an hour, delete it. Reset
Colin Walters <walters@gnu.org>
parents: 44566
diff changeset
479 }
44481
a0dc261f564a (toplevel): Include stdarg.h.
Colin Walters <walters@gnu.org>
parents: 44419
diff changeset
480 sleep((rand() % 2)+1);
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
481 goto trylock;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
482 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
483 else
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
484 return -1;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
485 }
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
486 close(fd);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
487 return 0;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
488 }
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
489
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
490 int
46774
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
491 unlock_file(filename, state)
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
492 const char *filename;
7411dc9e5008 (P_): New macro. Use it for all prototypes.
Colin Walters <walters@gnu.org>
parents: 44987
diff changeset
493 void *state;
44184
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
494 {
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
495 char *lockpath = (char *) state;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
496 int ret = unlink(lockpath);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
497 int saved_errno = errno;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
498 free(lockpath);
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
499 errno = saved_errno;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
500 return ret;
dca60454e83f Initial version.
Colin Walters <walters@gnu.org>
parents:
diff changeset
501 }