/* * terminal_read() * * Description: * Implements read syscall specific to the terminal. * * Inputs: * buf: buf to read the command buffer into * nbytes: number of bytes to read * * Outputs: * countread: the number of bytes read */ int32_t terminal_read(unsigned char * buf, int32_t nbytes) { int i; int countread = 0; set_command_location(get_tty_number()); /* Spin until allow_terminal_read = 1 (we allow it to be read). */ while(!allow_terminal_read[get_tty_number()]); /* We can only get here if we are the active terminal and the user * presses ENTER. */ new_line(); /* Iterate through nbytes reading (putting) the command buffer into buf. */ for (i = 0; i < nbytes; i++) { buf[i] = command_buffer[active_terminal][i]; command_buffer[active_terminal][i] = NULL; countread++; } /* Reset command_length, cursor, and the allow_terminal_read lock. */ command_length[active_terminal] = 0; cursor_x[active_terminal] = 0; allow_terminal_read[active_terminal] = 0; return countread; }
void timeout_handler() { printf("[-] read() timeout! \n"); if (!get_tty_number()) printf("Try with console ownership: switch to a TTY by using " "Ctrl-Alt-F[1-6] and try again.\n"); else printf("Maybe inotify isn't enabled.\n"); _exit(1); }
/* * terminal_write() * * Description: * Implements write syscall specific to the terminal. It prints a buffer with * "putc" and returns the number of bytes (or characters) printed. * * Inputs: * buf: input buffer * nbytes: number of bytes to print from buffer * * Outputs: * successputs: the number of bytes printed */ int32_t terminal_write(const unsigned char * buf, int32_t nbytes) { int i; /* Number of bytes printed begins at zero. */ int successputs = 0; /* Iteratively print the buf to the screen. */ for (i = 0; i < nbytes; i++) { /* Print a char from the buffer. */ putc(buf[i], get_tty_number()); /* Increment the number of bytes printed. */ successputs++; } /* Return the number of bytes printed. */ return successputs; }