26.07.2018 Views

hacking-the-art-of-exploitation

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

return -1;<br />

else<br />

player = entry; // Copy <strong>the</strong> read entry into <strong>the</strong> player struct.<br />

return 1;<br />

// Return a success.<br />

// This is <strong>the</strong> new user registration function.<br />

// It will create a new player account and append it to <strong>the</strong> file.<br />

void register_new_player() {<br />

int fd;<br />

printf("-=-={ New Player Registration }=-=-\n");<br />

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

input_name();<br />

player.uid = getuid();<br />

player.highscore = player.credits = 100;<br />

fd = open(DATAFILE, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);<br />

if(fd == -1)<br />

fatal("in register_new_player() while opening file");<br />

write(fd, &player, size<strong>of</strong>(struct user));<br />

close(fd);<br />

}<br />

printf("\nWelcome to <strong>the</strong> Game <strong>of</strong> Chance %s.\n", player.name);<br />

printf("You have been given %u credits.\n", player.credits);<br />

// This function writes <strong>the</strong> current player data to <strong>the</strong> file.<br />

// It is used primarily for updating <strong>the</strong> credits after games.<br />

void update_player_data() {<br />

int fd, i, read_uid;<br />

char burned_byte;<br />

}<br />

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

if(fd == -1) // If open fails here, something is really wrong.<br />

fatal("in update_player_data() while opening file");<br />

read(fd, &read_uid, 4);<br />

// Read <strong>the</strong> uid from <strong>the</strong> first struct.<br />

while(read_uid != player.uid) { // Loop until correct uid is found.<br />

for(i=0; i < size<strong>of</strong>(struct user) - 4; i++) // Read through <strong>the</strong><br />

read(fd, &burned_byte, 1);<br />

// rest <strong>of</strong> that struct.<br />

read(fd, &read_uid, 4); // Read <strong>the</strong> uid from <strong>the</strong> next struct.<br />

}<br />

write(fd, &(player.credits), 4); // Update credits.<br />

write(fd, &(player.highscore), 4); // Update highscore.<br />

write(fd, &(player.name), 100); // Update name.<br />

close(fd);<br />

// This function will display <strong>the</strong> current high score and<br />

// <strong>the</strong> name <strong>of</strong> <strong>the</strong> person who set that high score.<br />

void show_highscore() {<br />

unsigned int top_score = 0;<br />

char top_name[100];<br />

struct user entry;<br />

Programming 105

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

Saved successfully!

Ooh no, something went wrong!