/* Y2K fix for 3.0j nethack on UNIXPC; reading of topten records will go * wrong otherwise. logfile has the same format, but is write-only. * structure comment and locking stolen from nethack's src/topten.c. */ #include #include extern int errno; #include #define NHDIR "/usr/games/lib/nethackdir" /* struct toptenentry { struct toptenentry *tt_next; long int points; 10 int level,maxlvl,hp,maxhp; 4*5 int uid; 5 char plchar; 1 char sex; 1 char name[NAMSZ+1]; 10 char death[DTHSZ+1]; 60 char date[7]; 6 10 separators ---- 123 } 920128 102 1 1 10 25 240 PF wizard,quit 1992 1191110 107 1 1 3 13 114 HM test,quit 2019 120 124 102 6 6 -1 40 1838 HM demo,killed by a soldier ant 2020 */ int main() { char *recfile = "record"; char *reclock = "record_lock"; char *recfile2 = "record2"; FILE *rfile, *rfile2; char line[128]; int sleepct = 100; chdir(NHDIR); /* lock record file like nethack, just in case we end up with serial * terminals or something */ while (link(recfile, reclock) == -1) { perror(reclock); if (!sleepct--) { puts("I give up. Sorry."); puts("Perhaps there is an old record_lock around?"); return(1); } printf("Waiting for access to record file. (%d)\n", sleepct); fflush(stdout); sleep(1); } if (!(rfile = fopen(recfile,"r"))) { puts("Cannot open record file!"); unlink(reclock); return(1); } if (!(rfile2 = fopen(recfile2,"w"))) { puts("Cannot open record2 file!"); unlink(reclock); return(1); } if (chmod(recfile2,0644) != 0) { puts("Cannot chmod record2!"); unlink(reclock); return(1); } while (fgets(line,128,rfile)) { if (line[6] == ' ') { fputs(line, rfile2); } else if (line[7] == ' ') { /* nethack filled month and day when not shifted */ if (line[3] == ' ') line[3] = '0'; if (line[5] == ' ') line[5] = '0'; fputs(line+1, rfile2); } else { printf("Unexpected line format '%s'\n", line); unlink(reclock); return(1); } } if (unlink(recfile) != 0) { puts("Cannot unlink record!"); unlink(reclock); return(1); } if (link(recfile2,recfile) != 0) { puts("Cannot link record2!"); unlink(reclock); return(1); } if (unlink(recfile2) != 0) { puts("Cannot unlink record!"); unlink(reclock); return(1); } fclose(rfile); fclose(rfile2); unlink(reclock); return(0); }