int main(void) { char user[10], pass[10]; int logres, seats, bookres, bookch, loginch, usercheck; int maxseats = 20; //hardcoded max seats available system("cls"); printf("\nWelcome to Railway booking system!\n"); //Provide option to login or sign up printf("1. Press 1 to Login if you have already signed up.\n"); printf("2. Press 2 to Sign Up.\n\n"); printf("Your Choice?: "); scanf("%d", &loginch); usercheck = logincheck(loginch); //Check if user opted for login or sign-up if(usercheck == 1) //user opted for login in loginch, logincheck() returns 1 { A: //label to return to login after sign-up printf("\nUser Login:\nPlease enter your username and password:\n"); printf("Username: "******"%s", user); printf("Password: "******"%s", pass); logres = login(user, pass); //call to login, checks if user name and pass are correct if(logres == 1) //login success if login returns 1 above, ie. user name and pass are correct { printf("Login successful!\n\nPlease proceed for booking!\n20 Seats available!"); printf("\nPlease enter number of seats: "); scanf("%d", &seats); bookres = booking(seats, maxseats); //allow booking only if login success, //call booking() with no of seats user enters and max seats if(bookres == 1)//booking() above checks if enough seats are available, returns 1 if available { printf("\nEnough seats available! Proceed for booking.\n\n"); //Allow class selection if seats available printf("Please select your class:\n"); printf("1.Enter 1 for AC 1 Tier.\n"); printf("2.Enter 2 for AC 2 Tier.\n"); printf("3.Enter 3 for AC 3 Tier.\n"); printf("4.Enter 4 for Sleeper.\n"); printf("\nYour choice?: "); scanf("%d", &bookch); maxseats = bookmenu(bookch, seats, maxseats); //Actual Booking, call to bookmenu() } else //booking returns 0 if user entered seats exceeds available seats { printf("Not enough seats!"); } } else //login returns 0 if username OR pass is incorrect, comparison fails { printf("Invalid username or password!"); } } else if(usercheck == 2)//user opted for sign-up, logincheck() returns 2 { createuser();//Call user creation module system("cls"); goto A; //continue with login, after user creation } printf("\n\nThank You for using the Railway booking system. Press any key to exit!"); getche(); return 0; }
static int mklock(char *file) { int fd, try; Dir *dir; fd = openlock(file); if (fd >= 0) { /* make it a lock file if it wasn't */ dir = dirfstat(fd); if (dir == nil) error("%s vanished: %r", file); dir->mode |= DMEXCL; dir->qid.type |= QTEXCL; dirfwstat(fd, dir); free(dir); /* reopen in case it wasn't a lock file at last open */ close(fd); } for (try = 0; try < 65 && (fd = openlock(file)) < 0; try++) sleep(10*1000); return fd; } void main(int argc, char *argv[]) { Job *j; Tm tm; Time t; ulong now, last; /* in seconds */ int i, lock; debug = 0; ARGBEGIN{ case 'c': createuser(); exits(0); case 'd': debug = 1; break; default: usage(); }ARGEND if(debug){ readalljobs(); printjobs(); exits(0); } initcap(); /* do this early, before cpurc removes it */ switch(fork()){ case -1: fatal("can't fork: %r"); case 0: break; default: exits(0); } /* * it can take a few minutes before the file server notices that * we've rebooted and gives up the lock. */ lock = mklock("/cron/lock"); if (lock < 0) fatal("cron already running: %r"); argv0 = "cron"; srand(getpid()*time(0)); last = time(0); for(;;){ readalljobs(); /* * the system's notion of time may have jumped forward or * backward an arbitrary amount since the last call to time(). */ now = time(0); /* * if time has jumped backward, just note it and adapt. * if time has jumped forward more than a day, * just execute one day's jobs. */ if (now < last) { clog("time went backward"); last = now; } else if (now - last > Day) { clog("time advanced more than a day"); last = now - Day; } now = minute(now); for(last = minute(last); last <= now; last += Minute){ tm = *localtime(last); t.min = 1ULL << tm.min; t.hour = 1 << tm.hour; t.wday = 1 << tm.wday; t.mday = 1 << tm.mday; t.mon = 1 << (tm.mon + 1); for(i = 0; i < nuser; i++) for(j = users[i].jobs; j; j = j->next) if(j->time.min & t.min && j->time.hour & t.hour && j->time.wday & t.wday && j->time.mday & t.mday && j->time.mon & t.mon) rexec(&users[i], j); } seek(lock, 0, 0); write(lock, "x", 1); /* keep the lock alive */ /* * if we're not at next minute yet, sleep until a second past * (to allow for sleep intervals being approximate), * which synchronises with minute roll-over as a side-effect. */ sleepuntil(now + Minute + 1); } /* not reached */ }