/* move completed bones file to proper name */ void commit_bonesfile(char *bonesid) { const char *fq_bones, *tempname; char tempbuf[PL_NSIZ+32]; int ret; sprintf(bones, "bon%s", bonesid); fq_bones = fqname(bones, BONESPREFIX, 0); sprintf(tempbuf, "%d%s.bn", (int)getuid(), plname); tempname = fqname(tempbuf, BONESPREFIX, 1); ret = rename(tempname, fq_bones); if (wizard && ret != 0) pline("couldn't rename %s to %s.", tempname, fq_bones); }
int create_bonesfile(char *bonesid, char errbuf[]) { const char *file; char tempname[PL_NSIZ+32]; int fd; if (errbuf) *errbuf = '\0'; sprintf(bones, "bon%s", bonesid); sprintf(tempname, "%d%s.bn", (int)getuid(), plname); file = fqname(tempname, BONESPREFIX, 0); #if defined(WIN32) /* Use O_TRUNC to force the file to be shortened if it already * exists and is currently longer. */ fd = open(file, O_WRONLY |O_CREAT | O_TRUNC | O_BINARY, FCMASK); #else fd = creat(file, FCMASK); #endif if (fd < 0 && errbuf) /* failure explanation */ sprintf(errbuf, "Cannot create bones id %s (errno %d).", bonesid, errno); return fd; }
/* NOTE: a simpler version of this routine also exists in util/dlb_main.c */ FILE *fopen_datafile(const char *filename, const char *mode, int prefix) { FILE *fp; filename = fqname(filename, prefix, prefix == TROUBLEPREFIX ? 3 : 0); fp = fopen(filename, mode); return fp; }
int open_bonesfile(char *bonesid) { const char *fq_bones; int fd; sprintf(bones, "bon%s", bonesid); fq_bones = fqname(bones, BONESPREFIX, 0); fd = open(fq_bones, O_RDONLY | O_BINARY, 0); return fd; }
static int eraseoldlocks() { register int i; /* cannot use maxledgerno() here, because we need to find a lock name * before starting everything (including the dungeon initialization * that sets astral_level, needed for maxledgerno()) up */ for(i = 1; i <= MAXDUNGEON*MAXLEVEL + 1; i++) { /* try to remove all */ set_levelfile_name(lock, i); (void) unlink(fqname(lock, LEVELPREFIX, 0)); } set_levelfile_name(lock, 0); if(unlink(fqname(lock, LEVELPREFIX, 0))) return 0; /* cannot remove it */ return(1); /* success! */ }
jboolean nhjni_run() { windowprocs = _nhjni_proxy_procs; _nhjni_proxy_init(); int fd = create_levelfile(0, (char *)0); if (fd < 0) { raw_print("Cannot create lock file"); } else { hackpid = 1; write(fd, (genericptr_t) &hackpid, sizeof(hackpid)); close(fd); } iflags.news = TRUE; int argc=1; char *argv[]={"nethack",NULL}; initoptions(); init_nhwindows(&argc,argv); dlb_init(); vision_init(); display_gamewindows(); if ((fd = restore_saved_game()) >= 0) { const char *fq_save = fqname(SAVEF, SAVEPREFIX, 1); pline("Restoring save file..."); mark_synch(); /* flush output */ if(!dorecover(fd)) goto not_recovered; check_special_room(FALSE); //wd_message(); if (discover || wizard) { if(yn("Do you want to keep the save file?") == 'n') (void) delete_savefile(); else { (void) chmod(fq_save,FCMASK); /* back to readable */ compress(fq_save); } } flags.move = 0; } else { not_recovered: player_selection(); newgame(); flags.move = 0; set_wear(); (void) pickup(1); } moveloop(); return JNI_TRUE; }
/* open a file */ int open_datafile(const char *filename, int oflags, int prefix) { int fd; #ifdef WIN32 oflags |= O_BINARY; #endif filename = fqname(filename, prefix, prefix == TROUBLEPREFIX ? 3 : 0); fd = open(filename, oflags, S_IRUSR | S_IWUSR); return fd; }
void nds_save_key_config() { FILE *fp = fopen(fqname(KEY_CONFIG_FILE, CONFIGPREFIX, 0), "w"); int i; int magic = KEY_MAGIC; if (fp == (FILE *)0) { return; } fwrite(&magic, 1, sizeof(magic), fp); fwrite(&chord_keys, 1, sizeof(chord_keys), fp); fwrite(&numkeys, 1, sizeof(numkeys), fp); for (i = 0; i < numkeys; i++) { u16 key = keymap[i].key; fwrite(&key, 1, sizeof(key), fp); fwrite(keymap[i].command, 1, INPUT_BUFFER_SIZE, fp); } fclose(fp); }
int dosave() { #ifdef KEEP_SAVE /*WAC for reloading*/ register int fd; #endif clear_nhwindow(WIN_MESSAGE); if(yn("Really save?") == 'n') { clear_nhwindow(WIN_MESSAGE); if(multi > 0) nomul(0); } else { clear_nhwindow(WIN_MESSAGE); pline("Saving..."); #if defined(UNIX) || defined(VMS) || defined(__EMX__) program_state.done_hup = 0; #endif #ifdef KEEP_SAVE saverestore = FALSE; if (flags.keep_savefile) if(yn("Really quit?") == 'n') saverestore = TRUE; if(dosave0() && !saverestore) { #else if(dosave0()) { #endif program_state.something_worth_saving = 0; u.uhp = -1; /* universal game's over indicator */ /* make sure they see the Saving message */ display_nhwindow(WIN_MESSAGE, TRUE); exit_nhwindows("Be seeing you..."); terminate(EXIT_SUCCESS); } /*WAC redraw later else (void)doredraw();*/ } #ifdef KEEP_SAVE if (saverestore) { /*WAC pulled this from pcmain.c - restore game from the file just saved*/ fd = create_levelfile(0); if (fd < 0) { raw_print("Cannot create lock file"); } else { hackpid = 1; write(fd, (genericptr_t) &hackpid, sizeof(hackpid)); close(fd); } #ifdef MFLOPPY level_info[0].where = ACTIVE; #endif fd = restore_saved_game(); if (fd >= 0) dorecover(fd); check_special_room(FALSE); flags.move = 0; /*WAC correct these after restore*/ if(flags.moonphase == FULL_MOON) change_luck(1); if(flags.friday13) change_luck(-1); if(iflags.window_inited) clear_nhwindow(WIN_MESSAGE); } saverestore = FALSE; #endif (void)doredraw(); return 0; } #if defined(UNIX) || defined(VMS) || defined (__EMX__) || defined(WIN32) /*ARGSUSED*/ void hangup(sig_unused) /* called as signal() handler, so sent at least one arg */ int sig_unused; { # ifdef NOSAVEONHANGUP (void) signal(SIGINT, SIG_IGN); clearlocks(); # ifndef VMS terminate(EXIT_FAILURE); # endif # else /* SAVEONHANGUP */ if (!program_state.done_hup++) { if (program_state.something_worth_saving) (void) dosave0(); # ifdef VMS /* don't call exit when already within an exit handler; that would cancel any other pending user-mode handlers */ if (!program_state.exiting) # endif { clearlocks(); terminate(EXIT_FAILURE); } } # endif return; } #endif /* returns 1 if save successful */ int dosave0() { const char *fq_save; register int fd, ofd; xchar ltmp; d_level uz_save; char whynot[BUFSZ]; if (!SAVEF[0]) return 0; fq_save = fqname(SAVEF, SAVEPREFIX, 1); /* level files take 0 */ #if defined(UNIX) || defined(VMS) (void) signal(SIGHUP, SIG_IGN); #endif #ifndef NO_SIGNAL (void) signal(SIGINT, SIG_IGN); #endif #if defined(MICRO) && defined(MFLOPPY) if (!saveDiskPrompt(0)) return 0; #endif HUP if (iflags.window_inited) { uncompress_area(fq_save, SAVEF); fd = open_savefile(); if (fd > 0) { (void) close(fd); clear_nhwindow(WIN_MESSAGE); There("seems to be an old save file."); if (yn("Overwrite the old file?") == 'n') { compress_area(fq_save, SAVEF); #ifdef KEEP_SAVE /*WAC don't restore if you didn't save*/ saverestore = FALSE; #endif return 0; } } } HUP mark_synch(); /* flush any buffered screen output */ fd = create_savefile(); if(fd < 0) { HUP pline("Cannot open save file."); (void) delete_savefile(); /* ab@unido */ return(0); } vision_recalc(2); /* shut down vision to prevent problems in the event of an impossible() call */ /* undo date-dependent luck adjustments made at startup time */ if(flags.moonphase == FULL_MOON) /* ut-sally!fletcher */ change_luck(-1); /* and unido!ab */ if(flags.friday13) change_luck(1); if(iflags.window_inited) HUP clear_nhwindow(WIN_MESSAGE); #if defined(MICRO) && defined(TTY_GRAPHICS) if (!strncmpi("tty", windowprocs.name, 3)) { dotcnt = 0; dotrow = 2; curs(WIN_MAP, 1, 1); putstr(WIN_MAP, 0, "Saving:"); } #endif #ifdef MFLOPPY /* make sure there is enough disk space */ if (iflags.checkspace) { long fds, needed; savelev(fd, ledger_no(&u.uz), COUNT_SAVE); savegamestate(fd, COUNT_SAVE); needed = bytes_counted; for (ltmp = 1; ltmp <= maxledgerno(); ltmp++) if (ltmp != ledger_no(&u.uz) && level_info[ltmp].where) needed += level_info[ltmp].size + (sizeof ltmp); fds = freediskspace(fq_save); if (needed > fds) { HUP { There("is insufficient space on SAVE disk."); pline("Require %ld bytes but only have %ld.", needed, fds); } flushout(); (void) close(fd); (void) delete_savefile(); return 0; } co_false(); }
int delete_bonesfile(char *bonesid) { sprintf(bones, "bon%s", bonesid); return !(unlink(fqname(bones, BONESPREFIX, 0)) < 0); }
/* returns 1 if save successful */ int dosave0(void) { const char *fq_save; register int fd, ofd; xchar ltmp; d_level uz_save; char whynot[BUFSZ]; #ifdef WHEREIS_FILE delete_whereis(); #endif if (!SAVEF[0]) return 0; fq_save = fqname(SAVEF, SAVEPREFIX, 1); /* level files take 0 */ #if defined(UNIX) || defined(VMS) (void) signal(SIGHUP, SIG_IGN); #endif #ifndef NO_SIGNAL (void) signal(SIGINT, SIG_IGN); #endif #if defined(MICRO) && defined(MFLOPPY) if (!saveDiskPrompt(0)) return 0; #endif HUP if (iflags.window_inited) { uncompress_area(fq_save, SAVEF); fd = open_savefile(); if (fd > 0) { (void) close(fd); clear_nhwindow(WIN_MESSAGE); There("seems to be an old save file."); if (yn("Overwrite the old file?") == 'n') { compress_area(fq_save, SAVEF); return 0; } } } HUP mark_synch(); /* flush any buffered screen output */ fd = create_savefile(); if(fd < 0) { HUP pline("Cannot open save file."); (void) delete_savefile(); /* ab@unido */ return(0); } vision_recalc(2); /* shut down vision to prevent problems in the event of an impossible() call */ /* undo date-dependent luck adjustments made at startup time */ if(flags.moonphase == FULL_MOON) /* ut-sally!fletcher */ change_luck(-1); /* and unido!ab */ if(flags.friday13) change_luck(1); if(iflags.window_inited) HUP clear_nhwindow(WIN_MESSAGE); #ifdef MICRO dotcnt = 0; dotrow = 2; curs(WIN_MAP, 1, 1); if (strncmpi("X11", windowprocs.name, 3)) putstr(WIN_MAP, 0, "Saving:"); #endif #ifdef MFLOPPY /* make sure there is enough disk space */ if (iflags.checkspace) { long fds, needed; savelev(fd, ledger_no(&u.uz), COUNT_SAVE); savegamestate(fd, COUNT_SAVE); needed = bytes_counted; for (ltmp = 1; ltmp <= maxledgerno(); ltmp++) if (ltmp != ledger_no(&u.uz) && level_info[ltmp].where) needed += level_info[ltmp].size + (sizeof ltmp); fds = freediskspace(fq_save); if (needed > fds) { HUP { There("is insufficient space on SAVE disk."); pline("Require %ld bytes but only have %ld.", needed, fds); } flushout(); (void) close(fd); (void) delete_savefile(); return 0; }
int nds_load_key_config() { /* We need this for backward compatibility */ u16 oldkeys[OLD_NUMKEYS] = { KEY_A, KEY_B, KEY_X, KEY_Y, KEY_SELECT, KEY_START, KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN, KEY_A | KEY_R, KEY_B | KEY_R, KEY_X | KEY_R, KEY_Y | KEY_R, KEY_SELECT | KEY_R, KEY_START | KEY_R, KEY_RIGHT | KEY_R, KEY_LEFT | KEY_R, KEY_UP | KEY_R, KEY_DOWN | KEY_R, }; FILE *fp = fopen(fqname(KEY_CONFIG_FILE, CONFIGPREFIX, 0), "r"); int ret; int magic; char buffer[INPUT_BUFFER_SIZE]; int i; int mapcnt; if (fp == (FILE *)0) { return -1; } if ((ret = fread(&magic, 1, sizeof(magic), fp)) < sizeof(magic)) { DEBUG_PRINT("Unable to read magic number!\n"); return -1; } if (magic != KEY_MAGIC) { chord_keys = KEY_R; fclose(fp); fp = fopen(fqname(KEY_CONFIG_FILE, CONFIGPREFIX, 0), "r"); for (i = 0; i < OLD_NUMKEYS; i++) { if ((ret = fread(buffer, 1, sizeof(buffer), fp)) < sizeof(buffer)) { DEBUG_PRINT("Unable to read keymap (%d != %d).\n", ret, sizeof(buffer)); return -1; } if (*buffer) { nds_add_keymap_entry(oldkeys[i], buffer); } } } else { int i; u16 key; if ((ret = fread(&chord_keys, 1, sizeof(chord_keys), fp)) < sizeof(chord_keys)) { DEBUG_PRINT("Unable to read chord keys.\n"); return -1; } if ((ret = fread(&mapcnt, 1, sizeof(mapcnt), fp)) < sizeof(mapcnt)) { DEBUG_PRINT("Unable to read key count.\n"); return -1; } for (i = 0; i < mapcnt; i++) { if ((ret = fread(&key, 1, sizeof(key), fp)) < sizeof(key)) { DEBUG_PRINT("Unable to read keymap key (%d != %d, %d of %d).\n", ret, sizeof(key), i, numkeys); return -1; } if ((ret = fread(buffer, 1, sizeof(buffer), fp)) < sizeof(buffer)) { DEBUG_PRINT("Unable to read keymap command.\n"); return -1; } nds_add_keymap_entry(key, buffer); } } fclose(fp); return 0; }
void getlock() { const char *fq_lock; char tbuf[BUFSZ]; TCHAR wbuf[BUFSZ]; HANDLE f; int fd; int choice; /* regularize(lock); */ /* already done in pcmain */ Sprintf(tbuf, "%s", fqname(lock, LEVELPREFIX, 0)); set_levelfile_name(lock, 0); fq_lock = fqname(lock, LEVELPREFIX, 1); f = CreateFile( NH_A2W(fq_lock, wbuf, BUFSZ), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if( f==INVALID_HANDLE_VALUE ) { if(GetLastError()==ERROR_FILE_NOT_FOUND) goto gotlock; /* no such file */ error("Cannot open %s", fq_lock); } CloseHandle(f); /* prompt user that the game alredy exist */ choice = MessageBox( GetNHApp()->hMainWnd, TEXT("There are files from a game in progress under your name. Recover?"), TEXT("Nethack"), MB_YESNO | MB_DEFBUTTON1 ); switch(choice) { case IDYES: if(recover_savefile()) { goto gotlock; } else { error("Couldn't recover old game."); } break; case IDNO: unlock_file(HLOCK); error("%s", "Cannot start a new game."); break; }; gotlock: fd = creat(fq_lock, FCMASK); if(fd == -1) { error("cannot creat lock file (%s.)", fq_lock); } else { if(write(fd, (char *) &hackpid, sizeof(hackpid)) != sizeof(hackpid)){ error("cannot write lock (%s)", fq_lock); } if(close(fd) == -1) { error("cannot close lock (%s)", fq_lock); } } }