/* see whether we should throw away this xlock file */ static int veryold(int fd) { int i; time_t date; if(fstat(fd, &buf)) return(0); /* cannot get status */ if(buf.st_size != sizeof(int)) return(0); /* not an xlock file */ (void) time(&date); if(date - buf.st_mtime < 3L*24L*60L*60L) { /* recent */ int lockedpid; /* should be the same size as hackpid */ if(read(fd, (char *)&lockedpid, sizeof(lockedpid)) != sizeof(lockedpid)) /* strange ... */ return(0); /* From: Rick Adams <seismo!rick> This will work on 4.1cbsd, 4.2bsd and system 3? & 5. It will do nothing on V7 or 4.1bsd. */ if(!(kill(lockedpid, 0) == -1 && errno == ESRCH)) return(0); } (void) close(fd); for(i = 1; i <= MAXLEVEL; i++) { /* try to remove all */ glo(i); (void) unlink(lock); } glo(0); if(unlink(lock)) return(0); /* cannot remove it */ return(1); /* success! */ }
/* returns 1 if save successful */ static int dosave0(int hu) { int fd, ofd; int tmp; /* not ! */ (void) signal(SIGHUP, SIG_IGN); (void) signal(SIGINT, SIG_IGN); if ((fd = creat(SAVEF, FMASK)) < 0) { if (!hu) pline("Cannot open save file. (Continue or Quit)"); (void) unlink(SAVEF); /* ab@unido */ return (0); } if (flags.moonphase == FULL_MOON) /* ut-sally!fletcher */ u.uluck--; /* and unido!ab */ savelev(fd, dlevel); saveobjchn(fd, invent); saveobjchn(fd, fcobj); savemonchn(fd, fallen_down); tmp = getuid(); bwrite(fd, &tmp, sizeof tmp); bwrite(fd, &flags, sizeof(struct flag)); bwrite(fd, &dlevel, sizeof dlevel); bwrite(fd, &maxdlevel, sizeof maxdlevel); bwrite(fd, &moves, sizeof moves); bwrite(fd, &u, sizeof(struct you)); if (u.ustuck) bwrite(fd, &(u.ustuck->m_id), sizeof u.ustuck->m_id); bwrite(fd, pl_character, sizeof pl_character); bwrite(fd, genocided, sizeof genocided); bwrite(fd, fut_geno, sizeof fut_geno); savenames(fd); for (tmp = 1; tmp <= maxdlevel; tmp++) { if (tmp == dlevel || !level_exists[tmp]) continue; glo(tmp); if ((ofd = open(lock, O_RDONLY)) < 0) { if (!hu) pline("Error while saving: cannot read %s.", lock); (void) close(fd); (void) unlink(SAVEF); if (!hu) done("tricked"); return (0); } getlev(ofd, hackpid, tmp); (void) close(ofd); bwrite(fd, &tmp, sizeof tmp); /* level number */ savelev(fd, tmp); /* actual level */ (void) unlink(lock); } (void) close(fd); glo(dlevel); (void) unlink(lock); /* get rid of current level --jgm */ glo(0); (void) unlink(lock); return (1); }
// see whether we should throw away this xlock file int veryold(int fd) { int i; time_t date; if (fstat(fd, &buf)) return 0; // cannot get status if (buf.st_size != sizeof(int)) return 0; // not an xlock file (void) time(&date); if (date - buf.st_mtime < 3L * 24L * 60L * 60L) { // recent int lockedpid; // should be the same size as // hackpid if (read(fd, (char *) &lockedpid, sizeof(lockedpid)) != sizeof(lockedpid)) // strange ... return 0; // From: Rick Adams <seismo!rick> This will work on // 4.1cbsd, 4.2bsd and system 3? & 5. It will do nothing // on V7 or 4.1bsd. if (!(kill(lockedpid, 0) == -1 && errno == ESRCH)) return 0; } (void) close(fd); for (i = 1; i <= MAXLEVEL; i++) { // try to remove all glo(i); (void) unlink(lock); } glo(0); if (unlink(lock)) return 0; // cannot remove it return 1; // success! }
void clearlocks(void) { int x; signal(SIGHUP,SIG_IGN); for(x = maxdlevel; x >= 0; x--) { glo(x); unlink(lock); /* not all levels need be present */ } }
void getlock(void) { int i = 0, fd; (void) fflush(stdout); /* we ignore QUIT and INT at this point */ if (link(HLOCK, LLOCK) == -1) { int errnosv = errno; perror(HLOCK); printf("Cannot link %s to %s\n", LLOCK, HLOCK); switch (errnosv) { case ENOENT: printf("Perhaps there is no (empty) file %s ?\n", HLOCK); break; case EACCES: printf("It seems you don't have write permission here.\n"); break; case EEXIST: printf("(Try again or rm %s.)\n", LLOCK); break; default: printf("I don't know what is wrong."); } getret(); error("%s", ""); /* NOTREACHED */ } regularize(lock); glo(0); if (locknum > 25) locknum = 25; do { if (locknum) lock[0] = 'a' + i++; if ((fd = open(lock, O_RDONLY)) == -1) { if (errno == ENOENT) goto gotlock; /* no such file */ perror(lock); (void) unlink(LLOCK); error("Cannot open %s", lock); } if (veryold(fd))/* if true, this closes fd and unlinks lock */ goto gotlock; (void) close(fd); } while (i < locknum); (void) unlink(LLOCK); error(locknum ? "Too many hacks running now." : "There is a game in progress under your name."); gotlock: fd = creat(lock, FMASK); if (unlink(LLOCK) == -1) error("Cannot unlink %s.", LLOCK); if (fd == -1) { error("cannot creat lock file."); } else { if (write(fd, &hackpid, sizeof(hackpid)) != sizeof(hackpid)) { error("cannot write lock"); } if (close(fd) == -1) { error("cannot close lock"); } } }
void goto_level(int newlevel, boolean at_stairs) { int fd; boolean up = (newlevel < dlevel); if(newlevel <= 0) done("escaped"); /* in fact < 0 is impossible */ if(newlevel > MAXLEVEL) newlevel = MAXLEVEL; /* strange ... */ if(newlevel == dlevel) return; /* this can happen */ glo(dlevel); fd = creat(lock, FMASK); if(fd < 0) { /* * This is not quite impossible: e.g., we may have * exceeded our quota. If that is the case then we * cannot leave this level, and cannot save either. * Another possibility is that the directory was not * writable. */ pline("A mysterious force prevents you from going %s.", up ? "up" : "down"); return; } if(Punished) unplacebc(); u.utrap = 0; /* needed in level_tele */ u.ustuck = 0; /* idem */ keepdogs(); seeoff(1); if(u.uswallow) /* idem */ u.uswldtim = u.uswallow = 0; flags.nscrinh = 1; u.ux = FAR; /* hack */ (void) inshop(); /* probably was a trapdoor */ savelev(fd,dlevel); (void) close(fd); dlevel = newlevel; if(maxdlevel < dlevel) maxdlevel = dlevel; glo(dlevel); if(!level_exists[(int)dlevel]) mklev(); else { extern int hackpid; if((fd = open(lock, O_RDONLY)) < 0) { pline("Cannot open %s .", lock); pline("Probably someone removed it."); done("tricked"); } getlev(fd, hackpid, dlevel); (void) close(fd); } if(at_stairs) { if(up) { u.ux = xdnstair; u.uy = ydnstair; if(!u.ux) { /* entering a maze from below? */ u.ux = xupstair; /* this will confuse the player! */ u.uy = yupstair; } if(Punished && !Levitation){ pline("With great effort you climb the stairs."); placebc(1); } } else { u.ux = xupstair; u.uy = yupstair; if(inv_weight() + 5 > 0 || Punished){ pline("You fall down the stairs."); /* %% */ losehp(rnd(3), "fall"); if(Punished) { if(uwep != uball && rn2(3)){ pline("... and are hit by the iron ball."); losehp(rnd(20), "iron ball"); } placebc(1); } selftouch("Falling, you"); } } { struct monst *mtmp = m_at(u.ux, u.uy); if(mtmp) mnexto(mtmp); } } else { /* trapdoor or level_tele */ do { u.ux = rnd(COLNO-1); u.uy = rn2(ROWNO); } while(levl[(int)u.ux][(int)u.uy].typ != ROOM || m_at(u.ux,u.uy)); if(Punished){ if(uwep != uball && !up /* %% */ && rn2(5)){ pline("The iron ball falls on your head."); losehp(rnd(25), "iron ball"); } placebc(1); } selftouch("Falling, you"); } (void) inshop(); initrack(); losedogs(); { struct monst *mtmp; if ((mtmp = m_at(u.ux, u.uy))) mnexto(mtmp); /* riv05!a3 */ } flags.nscrinh = 0; setsee(); seeobjs(); /* make old cadavers disappear - riv05!a3 */ docrt(); pickup(1); read_engr_at(u.ux,u.uy); }
int dorecover(int fd) { int nfd; int tmp; /* not a ! */ unsigned mid; /* idem */ struct obj *otmp; restoring = TRUE; getlev(fd, 0, 0); invent = restobjchn(fd); for (otmp = invent; otmp; otmp = otmp->nobj) if (otmp->owornmask) setworn(otmp, otmp->owornmask); fcobj = restobjchn(fd); fallen_down = restmonchn(fd); mread(fd, &tmp, sizeof tmp); if (tmp != (int) getuid()) { /* strange ... */ (void) close(fd); (void) unlink(SAVEF); puts("Saved game was not yours."); restoring = FALSE; return (0); } mread(fd, &flags, sizeof(struct flag)); mread(fd, &dlevel, sizeof dlevel); mread(fd, &maxdlevel, sizeof maxdlevel); mread(fd, &moves, sizeof moves); mread(fd, &u, sizeof(struct you)); if (u.ustuck) mread(fd, &mid, sizeof mid); mread(fd, pl_character, sizeof pl_character); mread(fd, genocided, sizeof genocided); mread(fd, fut_geno, sizeof fut_geno); restnames(fd); while (1) { if (read(fd, &tmp, sizeof tmp) != sizeof tmp) break; getlev(fd, 0, tmp); glo(tmp); if ((nfd = creat(lock, FMASK)) < 0) panic("Cannot open temp file %s!\n", lock); savelev(nfd, tmp); (void) close(nfd); } (void) lseek(fd, (off_t) 0, SEEK_SET); getlev(fd, 0, 0); (void) close(fd); (void) unlink(SAVEF); if (Punished) { for (otmp = fobj; otmp; otmp = otmp->nobj) if (otmp->olet == CHAIN_SYM) goto chainfnd; panic("Cannot find the iron chain?"); chainfnd: uchain = otmp; if (!uball) { for (otmp = fobj; otmp; otmp = otmp->nobj) if (otmp->olet == BALL_SYM && otmp->spe) goto ballfnd; panic("Cannot find the iron ball?"); ballfnd: uball = otmp; } } if (u.ustuck) { struct monst *mtmp; for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) if (mtmp->m_id == mid) goto monfnd; panic("Cannot find the monster ustuck."); monfnd: u.ustuck = mtmp; } #ifndef QUEST setsee(); /* only to recompute seelx etc. - these * weren't saved */ #endif /* QUEST */ docrt(); restoring = FALSE; return (1); }
//main menu int main(void) { int choice=0; srand(time(NULL)); //creat random seed based on time printf("Which mode do you like to execute?\n" "a. Automatic\n" "b. Manual\n" "c. Red Light Only\n" "d. Green Light Only\n" "q. Exit\n"); printf("Mode [_]\b\b"); while(scanf("%c", &choice)){ if(choice == 'Q' || choice == 'q') //exit detection break; else{ switch(choice){ // Main menu. case 'a': case 'A': atmtc(); break; case 'b': case 'B': mnl(); break; case 'c': case 'C': rlo(); break; case 'd': case 'D': glo(); break; case 't': //t: testbench for bingo() case 'T':// which is a randomly access function bingo(); break; default: printf("Your choice is not in the menu.\n\n"); } _flushall(); printf("Which mode do you like to execute?\n" "a. Automatic\n" "b. Manual\n" "c. Red Light Only\n" "d. Green Light Only\n" "q. Exit\n"); printf("Mode [_]\b\b"); } } printf("\nHope you've enjoyed this walking trip!\n" "Press any botton to exit..."); _flushall(); getchar(); return 0; };