26.07.2018 Views

hacking-the-art-of-exploitation

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

printf("6 - Reset your account at 100 credits\n");<br />

printf("7 - Quit\n");<br />

printf("[Name: %s]\n", player.name);<br />

printf("[You have %u credits] -> ", player.credits);<br />

scanf("%d", &choice);<br />

}<br />

if((choice < 1) || (choice > 7))<br />

printf("\n[!!] The number %d is an invalid selection.\n\n", choice);<br />

else if (choice < 4) {<br />

// O<strong>the</strong>rwise, choice was a game <strong>of</strong> some sort.<br />

if(choice != last_game) { // If <strong>the</strong> function ptr isn't set<br />

if(choice == 1) // <strong>the</strong>n point it at <strong>the</strong> selected game<br />

player.current_game = pick_a_number;<br />

else if(choice == 2)<br />

player.current_game = dealer_no_match;<br />

else<br />

player.current_game = find_<strong>the</strong>_ace;<br />

last_game = choice; // and set last_game.<br />

}<br />

play_<strong>the</strong>_game();<br />

// Play <strong>the</strong> game.<br />

}<br />

else if (choice == 4)<br />

show_highscore();<br />

else if (choice == 5) {<br />

printf("\nChange user name\n");<br />

printf("Enter your new name: ");<br />

input_name();<br />

printf("Your name has been changed.\n\n");<br />

}<br />

else if (choice == 6) {<br />

printf("\nYour account has been reset with 100 credits.\n\n");<br />

player.credits = 100;<br />

}<br />

}<br />

update_player_data();<br />

printf("\nThanks for playing! Bye.\n");<br />

// This function reads <strong>the</strong> player data for <strong>the</strong> current uid<br />

// from <strong>the</strong> file. It returns -1 if it is unable to find player<br />

// data for <strong>the</strong> current uid.<br />

int get_player_data() {<br />

int fd, uid, read_bytes;<br />

struct user entry;<br />

uid = getuid();<br />

fd = open(DATAFILE, O_RDONLY);<br />

if(fd == -1) // Can't open <strong>the</strong> file, maybe it doesn't exist<br />

return -1;<br />

read_bytes = read(fd, &entry, size<strong>of</strong>(struct user)); // Read <strong>the</strong> first chunk.<br />

while(entry.uid != uid && read_bytes > 0) { // Loop until proper uid is found.<br />

read_bytes = read(fd, &entry, size<strong>of</strong>(struct user)); // Keep reading.<br />

}<br />

close(fd); // Close <strong>the</strong> file.<br />

if(read_bytes < size<strong>of</strong>(struct user)) // This means that <strong>the</strong> end <strong>of</strong> file was reached.<br />

104 0x200

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!