* Copyright (c) 1990 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
* Copy permission is hereby granted provided that this notice is
* retained on all partial or complete copies.
* For more info on this and all of my stuff, mail edjames@berkeley.edu.
static char sccsid
[] = "@(#)log.c 5.6 (Berkeley) 4/30/90";
if (b
->planes
== a
->planes
)
return (b
->time
- a
->time
);
return (b
->planes
- a
->planes
);
#define SECAHOUR (SECAMIN * MINAHOUR)
#define SECADAY (SECAHOUR * HOURADAY)
#define DAY(t) ((t) / SECADAY)
#define HOUR(t) (((t) % SECADAY) / SECAHOUR)
#define MIN(t) (((t) % SECAHOUR) / SECAMIN)
#define SEC(t) ((t) % SECAMIN)
(void)sprintf(s
, "%dd+%02dhrs", DAY(t
), HOUR(t
));
(void)sprintf(s
, "%d:%02d:%02d", HOUR(t
), MIN(t
), SEC(t
));
(void)sprintf(s
, "%d:%02d", MIN(t
), SEC(t
));
(void)sprintf(s
, ":%02d", SEC(t
));
register int i
, fd
, num_scores
= 0, good
, changed
= 0, found
= 0;
char *cp
, *index(), *rindex();
SCORE score
[100], thisscore
;
fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
, 0644);
* This is done to take advantage of stdio, while still
* allowing a O_CREAT during the open(2) of the log file.
if (flock(fileno(fp
), LOCK_EX
) < 0)
while (lockf(fileno(fp
), F_LOCK
, 1) < 0)
good
= fscanf(fp
, "%s %s %s %d %d %d",
&score
[num_scores
].planes
,
&score
[num_scores
].real_time
);
if (good
!= 6 || ++num_scores
>= NUM_SCORES
)
if (!test_mode
&& !list_em
) {
if ((pw
= (struct passwd
*) getpwuid(getuid())) == NULL
) {
"getpwuid failed for uid %d. Who are you?\n",
strcpy(thisscore
.name
, pw
->pw_name
);
if (gethostname(thisscore
.host
, sizeof (thisscore
.host
)) < 0) {
strcpy(thisscore
.host
, name
.sysname
);
fprintf(stderr
, "log: where's the '/' in %s?\n", file
);
strcpy(thisscore
.game
, cp
);
thisscore
.planes
= safe_planes
;
thisscore
.real_time
= time(0) - start_time
;
for (i
= 0; i
< num_scores
; i
++) {
if (strcmp(thisscore
.name
, score
[i
].name
) == 0 &&
strcmp(thisscore
.host
, score
[i
].host
) == 0 &&
strcmp(thisscore
.game
, score
[i
].game
) == 0) {
if (thisscore
.time
> score
[i
].time
) {
score
[i
].time
= thisscore
.time
;
score
[i
].planes
= thisscore
.planes
;
for (i
= 0; i
< num_scores
; i
++) {
if (thisscore
.time
> score
[i
].time
) {
if (num_scores
< NUM_SCORES
)
bcopy(&thisscore
, &score
[i
],
if (!found
&& !changed
&& num_scores
< NUM_SCORES
) {
bcopy(&thisscore
, &score
[num_scores
],
sizeof (score
[num_scores
]));
puts("You beat your previous score!");
puts("You made the top players list!");
qsort(score
, num_scores
, sizeof (*score
), compar
);
for (i
= 0; i
< num_scores
; i
++)
fprintf(fp
, "%s %s %s %d %d %d\n",
score
[i
].name
, score
[i
].host
,
score
[i
].game
, score
[i
].planes
,
score
[i
].time
, score
[i
].real_time
);
puts("You didn't beat your previous score.");
puts("You didn't make the top players list.");
flock(fileno(fp
), LOCK_UN
);
/* lock will evaporate upon close */
printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name", "host",
"game", "time", "real time", "planes safe");
puts("-------------------------------------------------------------------------------");
for (i
= 0; i
< num_scores
; i
++) {
cp
= index(score
[i
].host
, '.');
printf("%2d: %-8s %-8s %-18s %4d %9s %4d\n", i
+ 1,
score
[i
].name
, score
[i
].host
, score
[i
].game
,
score
[i
].time
, timestr(score
[i
].real_time
),