2002-03-27 20:57:06 +00:00
|
|
|
/* update-game-score.c --- Update a score file
|
2008-05-09 23:19:13 +00:00
|
|
|
|
2011-01-26 08:36:39 +00:00
|
|
|
Copyright (C) 2002-2011 Free Software Foundation, Inc.
|
2008-05-09 23:19:13 +00:00
|
|
|
|
|
|
|
Author: Colin Walters <walters@debian.org>
|
2002-03-27 20:57:06 +00:00
|
|
|
|
|
|
|
This file is part of GNU Emacs.
|
|
|
|
|
2008-05-09 23:19:13 +00:00
|
|
|
GNU Emacs is free software: you can redistribute it and/or modify
|
2002-03-27 20:57:06 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2008-05-09 23:19:13 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2002-03-27 20:57:06 +00:00
|
|
|
|
|
|
|
GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2008-05-09 23:19:13 +00:00
|
|
|
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2002-03-27 20:57:06 +00:00
|
|
|
|
2009-09-24 01:44:35 +00:00
|
|
|
/* This program allows a game to securely and atomically update a
|
2002-04-10 19:10:23 +00:00
|
|
|
score file. It should be installed setuid, owned by an appropriate
|
|
|
|
user like `games'.
|
|
|
|
|
|
|
|
Alternatively, it can be compiled without HAVE_SHARED_GAME_DIR
|
|
|
|
defined, and in that case it will store scores in the user's home
|
|
|
|
directory (it should NOT be setuid).
|
2002-03-27 20:57:06 +00:00
|
|
|
|
2008-05-09 23:19:13 +00:00
|
|
|
Created 2002/03/22.
|
2002-03-27 20:57:06 +00:00
|
|
|
*/
|
|
|
|
|
2002-04-16 20:05:08 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2002-03-27 20:57:06 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2002-11-18 15:35:07 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
2002-03-27 20:57:06 +00:00
|
|
|
#include <string.h>
|
2002-11-18 15:35:07 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDLIB_H
|
2002-03-27 20:57:06 +00:00
|
|
|
#include <stdlib.h>
|
2002-11-18 15:35:07 +00:00
|
|
|
#endif
|
2002-03-27 20:57:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2002-04-05 08:58:12 +00:00
|
|
|
#include <pwd.h>
|
2002-03-27 20:57:06 +00:00
|
|
|
#include <ctype.h>
|
2002-11-18 15:35:07 +00:00
|
|
|
#ifdef HAVE_FCNTL_H
|
2002-03-27 20:57:06 +00:00
|
|
|
#include <fcntl.h>
|
2002-11-18 15:35:07 +00:00
|
|
|
#endif
|
|
|
|
#ifdef STDC_HEADERS
|
2002-04-10 19:10:23 +00:00
|
|
|
#include <stdarg.h>
|
2002-11-18 15:35:07 +00:00
|
|
|
#endif
|
2002-03-27 20:57:06 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2002-11-18 15:35:07 +00:00
|
|
|
/* Needed for SunOS4, for instance. */
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind, opterr;
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int usage (int err) NO_RETURN;
|
2010-07-24 17:18:18 +00:00
|
|
|
|
2002-03-27 20:57:06 +00:00
|
|
|
#define MAX_ATTEMPTS 5
|
2002-04-10 19:10:23 +00:00
|
|
|
#define MAX_SCORES 200
|
|
|
|
#define MAX_DATA_LEN 1024
|
2002-04-07 05:46:15 +00:00
|
|
|
|
2003-05-22 20:01:19 +00:00
|
|
|
#ifndef HAVE_DIFFTIME
|
|
|
|
/* OK on POSIX (time_t is arithmetic type) modulo overflow in subtraction. */
|
|
|
|
#define difftime(t1, t0) (double)((t1) - (t0))
|
|
|
|
#endif
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int
|
2010-07-03 00:50:23 +00:00
|
|
|
usage (int err)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
2003-02-04 12:00:09 +00:00
|
|
|
fprintf (stdout, "Usage: update-game-score [-m MAX ] [ -r ] game/scorefile SCORE DATA\n");
|
|
|
|
fprintf (stdout, " update-game-score -h\n");
|
|
|
|
fprintf (stdout, " -h\t\tDisplay this help.\n");
|
|
|
|
fprintf (stdout, " -m MAX\t\tLimit the maximum number of scores to MAX.\n");
|
|
|
|
fprintf (stdout, " -r\t\tSort the scores in increasing order.\n");
|
|
|
|
fprintf (stdout, " -d DIR\t\tStore scores in DIR (only if not setuid).\n");
|
|
|
|
exit (err);
|
2002-03-27 20:57:06 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int lock_file (const char *filename, void **state);
|
|
|
|
static int unlock_file (const char *filename, void *state);
|
2002-03-27 20:57:06 +00:00
|
|
|
|
|
|
|
struct score_entry
|
|
|
|
{
|
|
|
|
long score;
|
2002-04-05 08:58:12 +00:00
|
|
|
char *username;
|
2002-03-27 20:57:06 +00:00
|
|
|
char *data;
|
|
|
|
};
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int read_scores (const char *filename, struct score_entry **scores,
|
|
|
|
int *count);
|
|
|
|
static int push_score (struct score_entry **scores, int *count,
|
|
|
|
int newscore, char *username, char *newdata);
|
|
|
|
static void sort_scores (struct score_entry *scores, int count, int reverse);
|
|
|
|
static int write_scores (const char *filename,
|
|
|
|
const struct score_entry *scores, int count);
|
2002-08-01 01:31:44 +00:00
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static void lose (const char *msg) NO_RETURN;
|
2002-08-01 01:31:44 +00:00
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static void
|
2010-07-03 00:50:23 +00:00
|
|
|
lose (const char *msg)
|
2002-08-01 01:31:44 +00:00
|
|
|
{
|
2003-02-04 12:00:09 +00:00
|
|
|
fprintf (stderr, "%s\n", msg);
|
2004-05-08 15:26:33 +00:00
|
|
|
exit (EXIT_FAILURE);
|
2002-08-01 01:31:44 +00:00
|
|
|
}
|
2002-03-27 20:57:06 +00:00
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static void lose_syserr (const char *msg) NO_RETURN;
|
2002-04-29 22:49:00 +00:00
|
|
|
|
2003-05-22 20:01:19 +00:00
|
|
|
/* Taken from sysdep.c. */
|
|
|
|
#ifndef HAVE_STRERROR
|
|
|
|
#ifndef WINDOWSNT
|
|
|
|
char *
|
|
|
|
strerror (errnum)
|
|
|
|
int errnum;
|
|
|
|
{
|
|
|
|
extern char *sys_errlist[];
|
|
|
|
extern int sys_nerr;
|
|
|
|
|
|
|
|
if (errnum >= 0 && errnum < sys_nerr)
|
|
|
|
return sys_errlist[errnum];
|
|
|
|
return (char *) "Unknown error";
|
|
|
|
}
|
|
|
|
#endif /* not WINDOWSNT */
|
|
|
|
#endif /* ! HAVE_STRERROR */
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static void
|
2010-07-03 00:50:23 +00:00
|
|
|
lose_syserr (const char *msg)
|
2002-04-29 22:49:00 +00:00
|
|
|
{
|
2003-02-04 12:00:09 +00:00
|
|
|
fprintf (stderr, "%s: %s\n", msg, strerror (errno));
|
2004-05-08 15:26:33 +00:00
|
|
|
exit (EXIT_FAILURE);
|
2002-04-29 22:49:00 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static char *
|
2010-07-02 12:19:53 +00:00
|
|
|
get_user_id (void)
|
2002-04-05 08:58:12 +00:00
|
|
|
{
|
|
|
|
char *name;
|
2003-02-04 12:00:09 +00:00
|
|
|
struct passwd *buf = getpwuid (getuid ());
|
2002-04-05 08:58:12 +00:00
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
int count = 1;
|
2003-02-04 12:00:09 +00:00
|
|
|
int uid = (int) getuid ();
|
2002-04-10 19:10:23 +00:00
|
|
|
int tuid = uid;
|
|
|
|
while (tuid /= 10)
|
2002-04-05 08:58:12 +00:00
|
|
|
count++;
|
2003-02-04 12:00:09 +00:00
|
|
|
name = malloc (count+1);
|
2002-04-29 22:49:00 +00:00
|
|
|
if (!name)
|
|
|
|
return NULL;
|
2003-02-04 12:00:09 +00:00
|
|
|
sprintf (name, "%d", uid);
|
2002-04-05 08:58:12 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
return buf->pw_name;
|
|
|
|
}
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static const char *
|
Fix warnings produced by compiling with -Wwrite_strings (i.e. use const char *).
* b2m.c (concat, fatal): Use const char*.
(main): Don't assign labels a string literal.
* ebrowse.c (struct member): filename, def_filename is const.
(struct sym): filename, sfilename is const.
(struct kw): name is const.
(add_sym, yyerror, token_string, insert_keyword, main): Use const char*.
* emacsclient.c (message, sock_err_message, send_to_emacs)
(quote_argument, set_local_socket)
(start_daemon_and_retry_set_socket): Use const char*.
* etags.c (compressor, language, Ada_suffix, Ada_help, Asm_suffixes)
(Asm_help, default_C_suffixes, default_C_help, Cplusplus_suffixes)
(Cplusplus_help, Cjava_suffixes, Cobol_suffixes, Cstar_suffixes)
(Erlang_suffixes, Erlang_help, Forth_suffixes, Forth_help)
(Fortran_suffixes, Fortran_help, HTML_suffixes, HTML_help)
(Lisp_suffixes, Lisp_help, Lua_suffixes, Lua_help)
(Makefile_filenames, Makefile_help, Objc_suffixes, Objc_help)
(Pascal_suffixes, Pascal_help, Perl_suffixes, Perl_interpreters)
(Perl_help, PHP_suffixes, PHP_help, plain_C_suffixses, PS_suffixes)
(PS_help, Prolog_suffixes, Prolog_help, Python_suffixes, Python_help)
(Scheme_suffixes, Scheme_help, TeX_suffixes, TeX_help, Texinfo_suffixes)
(Texinfo_help, Yacc_suffixes, Yacc_help, auto_help, none_help)
(no_lang_help, print_language_names)
(get_language_from_interpreter, get_language_from_filename)
(init, make_tag, struct C_stab_entry, write_classname, TEX_defenv)
(TEX_decode_env, nocase_tail, savestr, savenstr, fatal, pfatal)
(concat): Use const char*.
* make-docfile.c (error, fatal, scan_c_file, scan_lisp_file): Use
const char *.
* movemail.c (fatal, error, concat): Use const char *.
* pop.c (pop_multi_first, socket_connection, sendline): Use conat char*.
* pop.h (pop_multi_first): Use const char *.
(_ARGS): Remove.
* sorted-doc.c (error, fatal, states): Use const char *.
* update-game-score.c (get_prefix, write_scores, main): Use const char*.
2010-08-11 08:20:34 +00:00
|
|
|
get_prefix (int running_suid, const char *user_prefix)
|
2002-04-10 19:10:23 +00:00
|
|
|
{
|
2002-04-29 22:49:00 +00:00
|
|
|
if (!running_suid && user_prefix == NULL)
|
2003-02-04 12:00:09 +00:00
|
|
|
lose ("Not using a shared game directory, and no prefix given.");
|
2002-04-29 22:49:00 +00:00
|
|
|
if (running_suid)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SHARED_GAME_DIR
|
|
|
|
return HAVE_SHARED_GAME_DIR;
|
|
|
|
#else
|
2003-02-04 12:00:09 +00:00
|
|
|
lose ("This program was compiled without HAVE_SHARED_GAME_DIR,\n and should not be suid.");
|
2002-04-29 22:49:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return user_prefix;
|
2002-04-10 19:10:23 +00:00
|
|
|
}
|
|
|
|
|
2002-03-27 20:57:06 +00:00
|
|
|
int
|
2010-07-03 00:50:23 +00:00
|
|
|
main (int argc, char **argv)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
2002-04-29 22:49:00 +00:00
|
|
|
int c, running_suid;
|
2002-03-27 20:57:06 +00:00
|
|
|
void *lockstate;
|
Fix warnings produced by compiling with -Wwrite_strings (i.e. use const char *).
* b2m.c (concat, fatal): Use const char*.
(main): Don't assign labels a string literal.
* ebrowse.c (struct member): filename, def_filename is const.
(struct sym): filename, sfilename is const.
(struct kw): name is const.
(add_sym, yyerror, token_string, insert_keyword, main): Use const char*.
* emacsclient.c (message, sock_err_message, send_to_emacs)
(quote_argument, set_local_socket)
(start_daemon_and_retry_set_socket): Use const char*.
* etags.c (compressor, language, Ada_suffix, Ada_help, Asm_suffixes)
(Asm_help, default_C_suffixes, default_C_help, Cplusplus_suffixes)
(Cplusplus_help, Cjava_suffixes, Cobol_suffixes, Cstar_suffixes)
(Erlang_suffixes, Erlang_help, Forth_suffixes, Forth_help)
(Fortran_suffixes, Fortran_help, HTML_suffixes, HTML_help)
(Lisp_suffixes, Lisp_help, Lua_suffixes, Lua_help)
(Makefile_filenames, Makefile_help, Objc_suffixes, Objc_help)
(Pascal_suffixes, Pascal_help, Perl_suffixes, Perl_interpreters)
(Perl_help, PHP_suffixes, PHP_help, plain_C_suffixses, PS_suffixes)
(PS_help, Prolog_suffixes, Prolog_help, Python_suffixes, Python_help)
(Scheme_suffixes, Scheme_help, TeX_suffixes, TeX_help, Texinfo_suffixes)
(Texinfo_help, Yacc_suffixes, Yacc_help, auto_help, none_help)
(no_lang_help, print_language_names)
(get_language_from_interpreter, get_language_from_filename)
(init, make_tag, struct C_stab_entry, write_classname, TEX_defenv)
(TEX_decode_env, nocase_tail, savestr, savenstr, fatal, pfatal)
(concat): Use const char*.
* make-docfile.c (error, fatal, scan_c_file, scan_lisp_file): Use
const char *.
* movemail.c (fatal, error, concat): Use const char *.
* pop.c (pop_multi_first, socket_connection, sendline): Use conat char*.
* pop.h (pop_multi_first): Use const char *.
(_ARGS): Remove.
* sorted-doc.c (error, fatal, states): Use const char *.
* update-game-score.c (get_prefix, write_scores, main): Use const char*.
2010-08-11 08:20:34 +00:00
|
|
|
char *user_id, *scorefile;
|
|
|
|
const char *prefix, *user_prefix = NULL;
|
2002-03-27 20:57:06 +00:00
|
|
|
struct stat buf;
|
|
|
|
struct score_entry *scores;
|
2002-04-10 19:10:23 +00:00
|
|
|
int newscore, scorecount, reverse = 0, max = MAX_SCORES;
|
2002-03-27 20:57:06 +00:00
|
|
|
char *newdata;
|
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
srand (time (0));
|
2002-03-27 20:57:06 +00:00
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
while ((c = getopt (argc, argv, "hrm:d:")) != -1)
|
2002-03-27 20:57:06 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'h':
|
2004-05-08 15:26:33 +00:00
|
|
|
usage (EXIT_SUCCESS);
|
2002-03-27 20:57:06 +00:00
|
|
|
break;
|
2002-04-29 22:49:00 +00:00
|
|
|
case 'd':
|
|
|
|
user_prefix = optarg;
|
|
|
|
break;
|
2002-03-27 20:57:06 +00:00
|
|
|
case 'r':
|
|
|
|
reverse = 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
2003-02-04 12:00:09 +00:00
|
|
|
max = atoi (optarg);
|
2002-04-10 19:10:23 +00:00
|
|
|
if (max > MAX_SCORES)
|
|
|
|
max = MAX_SCORES;
|
2002-03-27 20:57:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
2004-05-08 15:26:33 +00:00
|
|
|
usage (EXIT_FAILURE);
|
2002-03-27 20:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (optind+3 != argc)
|
2004-05-08 15:26:33 +00:00
|
|
|
usage (EXIT_FAILURE);
|
2002-04-07 05:46:15 +00:00
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
running_suid = (getuid () != geteuid ());
|
2002-04-07 05:46:15 +00:00
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
prefix = get_prefix (running_suid, user_prefix);
|
2002-04-10 19:10:23 +00:00
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2);
|
2002-03-27 20:57:06 +00:00
|
|
|
if (!scorefile)
|
2003-02-04 12:00:09 +00:00
|
|
|
lose_syserr ("Couldn't allocate score file");
|
2002-04-10 19:10:23 +00:00
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
strcpy (scorefile, prefix);
|
|
|
|
strcat (scorefile, "/");
|
|
|
|
strcat (scorefile, argv[optind]);
|
|
|
|
newscore = atoi (argv[optind+1]);
|
2002-03-27 20:57:06 +00:00
|
|
|
newdata = argv[optind+2];
|
2003-02-04 12:00:09 +00:00
|
|
|
if (strlen (newdata) > MAX_DATA_LEN)
|
2002-04-10 19:10:23 +00:00
|
|
|
newdata[MAX_DATA_LEN] = '\0';
|
2002-04-29 22:49:00 +00:00
|
|
|
|
2003-02-04 12:03:44 +00:00
|
|
|
user_id = get_user_id ();
|
|
|
|
if (user_id == NULL)
|
2003-02-04 12:00:09 +00:00
|
|
|
lose_syserr ("Couldn't determine user id");
|
2003-02-04 14:56:31 +00:00
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
if (stat (scorefile, &buf) < 0)
|
|
|
|
lose_syserr ("Failed to access scores file");
|
2003-02-04 14:56:31 +00:00
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
if (lock_file (scorefile, &lockstate) < 0)
|
|
|
|
lose_syserr ("Failed to lock scores file");
|
2003-02-04 14:56:31 +00:00
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
if (read_scores (scorefile, &scores, &scorecount) < 0)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
2003-02-04 12:00:09 +00:00
|
|
|
unlock_file (scorefile, lockstate);
|
|
|
|
lose_syserr ("Failed to read scores file");
|
2002-03-27 20:57:06 +00:00
|
|
|
}
|
2003-02-04 12:00:09 +00:00
|
|
|
push_score (&scores, &scorecount, newscore, user_id, newdata);
|
2009-09-11 19:11:07 +00:00
|
|
|
sort_scores (scores, scorecount, reverse);
|
2002-04-10 19:10:23 +00:00
|
|
|
/* Limit the number of scores. If we're using reverse sorting, then
|
|
|
|
we should increment the beginning of the array, to skip over the
|
|
|
|
*smallest* scores. Otherwise, we just decrement the number of
|
|
|
|
scores, since the smallest will be at the end. */
|
|
|
|
if (scorecount > MAX_SCORES)
|
|
|
|
scorecount -= (scorecount - MAX_SCORES);
|
2009-09-11 19:11:07 +00:00
|
|
|
if (reverse)
|
|
|
|
scores += (scorecount - MAX_SCORES);
|
2003-02-04 12:00:09 +00:00
|
|
|
if (write_scores (scorefile, scores, scorecount) < 0)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
2003-02-04 12:00:09 +00:00
|
|
|
unlock_file (scorefile, lockstate);
|
|
|
|
lose_syserr ("Failed to write scores file");
|
2002-03-27 20:57:06 +00:00
|
|
|
}
|
2003-02-04 12:00:09 +00:00
|
|
|
unlock_file (scorefile, lockstate);
|
2004-05-08 15:26:33 +00:00
|
|
|
exit (EXIT_SUCCESS);
|
2002-03-27 20:57:06 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int
|
2010-07-03 00:50:23 +00:00
|
|
|
read_score (FILE *f, struct score_entry *score)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
|
|
|
int c;
|
2003-02-04 12:00:09 +00:00
|
|
|
if (feof (f))
|
2002-03-27 20:57:06 +00:00
|
|
|
return 1;
|
2003-02-04 12:00:09 +00:00
|
|
|
while ((c = getc (f)) != EOF
|
|
|
|
&& isdigit (c))
|
2002-04-05 08:58:12 +00:00
|
|
|
{
|
|
|
|
score->score *= 10;
|
|
|
|
score->score += (c-48);
|
|
|
|
}
|
2003-02-04 12:00:09 +00:00
|
|
|
while ((c = getc (f)) != EOF
|
|
|
|
&& isspace (c))
|
2002-04-05 08:58:12 +00:00
|
|
|
;
|
|
|
|
if (c == EOF)
|
|
|
|
return -1;
|
2003-02-04 12:00:09 +00:00
|
|
|
ungetc (c, f);
|
2002-04-05 08:58:12 +00:00
|
|
|
#ifdef HAVE_GETDELIM
|
|
|
|
{
|
2002-04-14 01:02:53 +00:00
|
|
|
size_t count = 0;
|
2003-02-04 12:00:09 +00:00
|
|
|
if (getdelim (&score->username, &count, ' ', f) < 1
|
2002-04-05 08:58:12 +00:00
|
|
|
|| score->username == NULL)
|
|
|
|
return -1;
|
2002-04-23 20:23:05 +00:00
|
|
|
/* Trim the space */
|
2003-02-04 12:00:09 +00:00
|
|
|
score->username[strlen (score->username)-1] = '\0';
|
2002-03-27 20:57:06 +00:00
|
|
|
}
|
2002-04-05 08:58:12 +00:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
int unameread = 0;
|
|
|
|
int unamelen = 30;
|
2003-02-04 12:00:09 +00:00
|
|
|
char *username = malloc (unamelen);
|
2002-04-05 09:18:57 +00:00
|
|
|
if (!username)
|
|
|
|
return -1;
|
2003-02-04 14:56:31 +00:00
|
|
|
|
2003-02-04 12:00:09 +00:00
|
|
|
while ((c = getc (f)) != EOF
|
|
|
|
&& !isspace (c))
|
2002-04-05 08:58:12 +00:00
|
|
|
{
|
2002-04-10 19:10:23 +00:00
|
|
|
if (unameread >= unamelen-1)
|
2003-02-04 12:00:09 +00:00
|
|
|
if (!(username = realloc (username, unamelen *= 2)))
|
2002-04-10 19:10:23 +00:00
|
|
|
return -1;
|
2002-04-05 08:58:12 +00:00
|
|
|
username[unameread] = c;
|
|
|
|
unameread++;
|
|
|
|
}
|
2003-02-04 14:56:31 +00:00
|
|
|
if (c == EOF)
|
2002-04-05 09:18:57 +00:00
|
|
|
return -1;
|
|
|
|
username[unameread] = '\0';
|
2002-04-05 08:58:12 +00:00
|
|
|
score->username = username;
|
|
|
|
}
|
|
|
|
#endif
|
2002-03-27 20:57:06 +00:00
|
|
|
#ifdef HAVE_GETLINE
|
|
|
|
score->data = NULL;
|
2002-04-14 01:02:53 +00:00
|
|
|
errno = 0;
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
2002-04-14 01:02:53 +00:00
|
|
|
size_t len;
|
2003-02-04 12:00:09 +00:00
|
|
|
if (getline (&score->data, &len, f) < 0)
|
2002-03-27 20:57:06 +00:00
|
|
|
return -1;
|
2003-02-04 12:00:09 +00:00
|
|
|
score->data[strlen (score->data)-1] = '\0';
|
2002-03-27 20:57:06 +00:00
|
|
|
}
|
|
|
|
#else
|
2002-04-05 08:58:12 +00:00
|
|
|
{
|
|
|
|
int cur = 0;
|
|
|
|
int len = 16;
|
2003-02-04 12:00:09 +00:00
|
|
|
char *buf = malloc (len);
|
2002-04-05 08:58:12 +00:00
|
|
|
if (!buf)
|
|
|
|
return -1;
|
2003-02-04 12:00:09 +00:00
|
|
|
while ((c = getc (f)) != EOF
|
2002-04-05 09:18:57 +00:00
|
|
|
&& c != '\n')
|
2002-04-05 08:58:12 +00:00
|
|
|
{
|
|
|
|
if (cur >= len-1)
|
|
|
|
{
|
2003-02-04 12:00:09 +00:00
|
|
|
if (!(buf = realloc (buf, len *= 2)))
|
2002-04-05 08:58:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf[cur] = c;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
score->data = buf;
|
2002-04-07 05:46:15 +00:00
|
|
|
score->data[cur] = '\0';
|
2002-04-05 08:58:12 +00:00
|
|
|
}
|
2002-03-27 20:57:06 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int
|
2010-07-03 00:50:23 +00:00
|
|
|
read_scores (const char *filename, struct score_entry **scores, int *count)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
|
|
|
int readval, scorecount, cursize;
|
|
|
|
struct score_entry *ret;
|
2003-02-04 12:00:09 +00:00
|
|
|
FILE *f = fopen (filename, "r");
|
2003-02-04 14:56:31 +00:00
|
|
|
if (!f)
|
2002-03-27 20:57:06 +00:00
|
|
|
return -1;
|
|
|
|
scorecount = 0;
|
|
|
|
cursize = 16;
|
2003-02-04 12:03:44 +00:00
|
|
|
ret = (struct score_entry *) malloc (sizeof (struct score_entry) * cursize);
|
2003-02-04 14:56:31 +00:00
|
|
|
if (!ret)
|
2002-03-27 20:57:06 +00:00
|
|
|
return -1;
|
2003-02-04 12:00:09 +00:00
|
|
|
while ((readval = read_score (f, &ret[scorecount])) == 0)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
|
|
|
/* We encoutered an error */
|
|
|
|
if (readval < 0)
|
|
|
|
return -1;
|
|
|
|
scorecount++;
|
|
|
|
if (scorecount >= cursize)
|
|
|
|
{
|
2003-04-27 11:22:16 +00:00
|
|
|
cursize *= 2;
|
|
|
|
ret = (struct score_entry *)
|
|
|
|
realloc (ret, (sizeof (struct score_entry) * cursize));
|
2002-03-27 20:57:06 +00:00
|
|
|
if (!ret)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*count = scorecount;
|
|
|
|
*scores = ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int
|
2010-07-03 00:50:23 +00:00
|
|
|
score_compare (const void *a, const void *b)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
|
|
|
const struct score_entry *sa = (const struct score_entry *) a;
|
|
|
|
const struct score_entry *sb = (const struct score_entry *) b;
|
|
|
|
return (sb->score > sa->score) - (sb->score < sa->score);
|
|
|
|
}
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int
|
2010-07-03 00:50:23 +00:00
|
|
|
score_compare_reverse (const void *a, const void *b)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
|
|
|
const struct score_entry *sa = (const struct score_entry *) a;
|
|
|
|
const struct score_entry *sb = (const struct score_entry *) b;
|
|
|
|
return (sa->score > sb->score) - (sa->score < sb->score);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-07-03 00:50:23 +00:00
|
|
|
push_score (struct score_entry **scores, int *count, int newscore, char *username, char *newdata)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
2003-02-04 12:00:09 +00:00
|
|
|
struct score_entry *newscores
|
2003-02-04 12:03:44 +00:00
|
|
|
= (struct score_entry *) realloc (*scores,
|
|
|
|
sizeof (struct score_entry) * ((*count) + 1));
|
2002-03-27 20:57:06 +00:00
|
|
|
if (!newscores)
|
|
|
|
return -1;
|
|
|
|
newscores[*count].score = newscore;
|
2002-04-05 08:58:12 +00:00
|
|
|
newscores[*count].username = username;
|
2002-03-27 20:57:06 +00:00
|
|
|
newscores[*count].data = newdata;
|
|
|
|
(*count) += 1;
|
|
|
|
*scores = newscores;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-02-04 14:56:31 +00:00
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static void
|
2010-07-03 00:50:23 +00:00
|
|
|
sort_scores (struct score_entry *scores, int count, int reverse)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
2003-02-04 12:00:09 +00:00
|
|
|
qsort (scores, count, sizeof (struct score_entry),
|
2002-03-27 20:57:06 +00:00
|
|
|
reverse ? score_compare_reverse : score_compare);
|
|
|
|
}
|
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int
|
2010-07-03 00:50:23 +00:00
|
|
|
write_scores (const char *filename, const struct score_entry *scores, int count)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
2003-02-04 14:56:31 +00:00
|
|
|
FILE *f;
|
2002-03-27 20:57:06 +00:00
|
|
|
int i;
|
2003-02-04 12:00:09 +00:00
|
|
|
char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1);
|
2002-03-27 20:57:06 +00:00
|
|
|
if (!tempfile)
|
|
|
|
return -1;
|
2003-02-04 12:00:09 +00:00
|
|
|
strcpy (tempfile, filename);
|
|
|
|
strcat (tempfile, ".tempXXXXXX");
|
2002-04-10 19:10:23 +00:00
|
|
|
#ifdef HAVE_MKSTEMP
|
2003-02-04 12:00:09 +00:00
|
|
|
if (mkstemp (tempfile) < 0
|
2002-04-10 19:10:23 +00:00
|
|
|
#else
|
2003-02-04 12:00:09 +00:00
|
|
|
if (mktemp (tempfile) != tempfile
|
2002-04-10 19:10:23 +00:00
|
|
|
#endif
|
2003-02-04 12:00:09 +00:00
|
|
|
|| !(f = fopen (tempfile, "w")))
|
2002-03-27 20:57:06 +00:00
|
|
|
return -1;
|
|
|
|
for (i = 0; i < count; i++)
|
2003-02-04 12:00:09 +00:00
|
|
|
if (fprintf (f, "%ld %s %s\n", scores[i].score, scores[i].username,
|
2002-04-05 08:58:12 +00:00
|
|
|
scores[i].data) < 0)
|
2002-03-27 20:57:06 +00:00
|
|
|
return -1;
|
2003-02-04 12:00:09 +00:00
|
|
|
fclose (f);
|
|
|
|
if (rename (tempfile, filename) < 0)
|
2002-03-27 20:57:06 +00:00
|
|
|
return -1;
|
2003-02-04 12:00:09 +00:00
|
|
|
if (chmod (filename, 0644) < 0)
|
2002-03-27 20:57:06 +00:00
|
|
|
return -1;
|
2002-04-10 19:10:23 +00:00
|
|
|
return 0;
|
2002-03-27 20:57:06 +00:00
|
|
|
}
|
2003-02-04 14:56:31 +00:00
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int
|
2010-07-03 00:50:23 +00:00
|
|
|
lock_file (const char *filename, void **state)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2002-04-14 06:40:19 +00:00
|
|
|
struct stat buf;
|
2002-03-27 20:57:06 +00:00
|
|
|
int attempts = 0;
|
Fix warnings produced by compiling with -Wwrite_strings (i.e. use const char *).
* b2m.c (concat, fatal): Use const char*.
(main): Don't assign labels a string literal.
* ebrowse.c (struct member): filename, def_filename is const.
(struct sym): filename, sfilename is const.
(struct kw): name is const.
(add_sym, yyerror, token_string, insert_keyword, main): Use const char*.
* emacsclient.c (message, sock_err_message, send_to_emacs)
(quote_argument, set_local_socket)
(start_daemon_and_retry_set_socket): Use const char*.
* etags.c (compressor, language, Ada_suffix, Ada_help, Asm_suffixes)
(Asm_help, default_C_suffixes, default_C_help, Cplusplus_suffixes)
(Cplusplus_help, Cjava_suffixes, Cobol_suffixes, Cstar_suffixes)
(Erlang_suffixes, Erlang_help, Forth_suffixes, Forth_help)
(Fortran_suffixes, Fortran_help, HTML_suffixes, HTML_help)
(Lisp_suffixes, Lisp_help, Lua_suffixes, Lua_help)
(Makefile_filenames, Makefile_help, Objc_suffixes, Objc_help)
(Pascal_suffixes, Pascal_help, Perl_suffixes, Perl_interpreters)
(Perl_help, PHP_suffixes, PHP_help, plain_C_suffixses, PS_suffixes)
(PS_help, Prolog_suffixes, Prolog_help, Python_suffixes, Python_help)
(Scheme_suffixes, Scheme_help, TeX_suffixes, TeX_help, Texinfo_suffixes)
(Texinfo_help, Yacc_suffixes, Yacc_help, auto_help, none_help)
(no_lang_help, print_language_names)
(get_language_from_interpreter, get_language_from_filename)
(init, make_tag, struct C_stab_entry, write_classname, TEX_defenv)
(TEX_decode_env, nocase_tail, savestr, savenstr, fatal, pfatal)
(concat): Use const char*.
* make-docfile.c (error, fatal, scan_c_file, scan_lisp_file): Use
const char *.
* movemail.c (fatal, error, concat): Use const char *.
* pop.c (pop_multi_first, socket_connection, sendline): Use conat char*.
* pop.h (pop_multi_first): Use const char *.
(_ARGS): Remove.
* sorted-doc.c (error, fatal, states): Use const char *.
* update-game-score.c (get_prefix, write_scores, main): Use const char*.
2010-08-11 08:20:34 +00:00
|
|
|
const char *lockext = ".lockfile";
|
2003-02-04 12:00:09 +00:00
|
|
|
char *lockpath = malloc (strlen (filename) + strlen (lockext) + 60);
|
2002-03-27 20:57:06 +00:00
|
|
|
if (!lockpath)
|
|
|
|
return -1;
|
2003-02-04 12:00:09 +00:00
|
|
|
strcpy (lockpath, filename);
|
|
|
|
strcat (lockpath, lockext);
|
2002-03-27 20:57:06 +00:00
|
|
|
*state = lockpath;
|
|
|
|
trylock:
|
|
|
|
attempts++;
|
2002-04-14 06:40:19 +00:00
|
|
|
/* If the lock is over an hour old, delete it. */
|
2003-02-04 12:00:09 +00:00
|
|
|
if (stat (lockpath, &buf) == 0
|
|
|
|
&& (difftime (buf.st_ctime, time (NULL) > 60*60)))
|
|
|
|
unlink (lockpath);
|
2003-02-04 12:03:44 +00:00
|
|
|
fd = open (lockpath, O_CREAT | O_EXCL, 0600);
|
|
|
|
if (fd < 0)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
|
|
|
if (errno == EEXIST)
|
|
|
|
{
|
|
|
|
/* Break the lock; we won't corrupt the file, but we might
|
|
|
|
lose some scores. */
|
|
|
|
if (attempts > MAX_ATTEMPTS)
|
2002-04-14 06:40:19 +00:00
|
|
|
{
|
2003-02-04 12:00:09 +00:00
|
|
|
unlink (lockpath);
|
2002-04-14 06:40:19 +00:00
|
|
|
attempts = 0;
|
|
|
|
}
|
2003-02-04 12:00:09 +00:00
|
|
|
sleep ((rand () % 2)+1);
|
2002-03-27 20:57:06 +00:00
|
|
|
goto trylock;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2003-02-04 12:00:09 +00:00
|
|
|
close (fd);
|
2002-03-27 20:57:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-02-04 14:56:31 +00:00
|
|
|
|
2011-02-21 18:06:25 +00:00
|
|
|
static int
|
2010-07-03 00:50:23 +00:00
|
|
|
unlock_file (const char *filename, void *state)
|
2002-03-27 20:57:06 +00:00
|
|
|
{
|
|
|
|
char *lockpath = (char *) state;
|
2003-02-04 12:00:09 +00:00
|
|
|
int ret = unlink (lockpath);
|
2002-03-27 20:57:06 +00:00
|
|
|
int saved_errno = errno;
|
2003-02-04 12:00:09 +00:00
|
|
|
free (lockpath);
|
2002-03-27 20:57:06 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
return ret;
|
|
|
|
}
|
2003-09-01 15:45:59 +00:00
|
|
|
|
2004-05-08 15:26:33 +00:00
|
|
|
|
|
|
|
/* update-game-score.c ends here */
|