Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A simple program that sets up a pty interface to login to. */
- #include <stdio.h>
- #include <string.h>
- #include <netdb.h>
- int ForkPTYI(int *FD, char *Name) /* This is a quick function I wrote to encaspulate forkpty(). */
- { /* The return value is a pid of either the master or slave. */
- int PID;
- PID = forkpty(FD, Name, 0, 0);
- return PID;
- }
- int main(void)
- {
- int pty, pid, ret;
- char Name[400];
- if((pid = ForkPTYI(&pty, Name)) < 0)
- { /* Cannot open PTY. */
- perror("FORK");
- return -1;
- }
- if(!pid)
- { /* Slave. */
- execv("/bin/bash", NULL);
- }
- else
- { /* Master. */
- char Output[500];
- char Input[500];
- fd_set FDs, FDCopy;
- /* Setup select(). */
- FD_ZERO(&FDs);
- FD_SET(0, &FDs); /* Add STDIN to the list. */
- FD_SET(pty, &FDs); /* Add child's STDOUT to the list. */
- for(;;)
- { /* This loop continually looks for input on the descriptors. */
- FDCopy = FDs;
- select(pty+1, &FDCopy, NULL, NULL, NULL);
- /* Check for output from the child. */
- if(FD_ISSET(pty, &FDCopy))
- {
- memset(Output, 0, sizeof(Output));
- read(pty, Output, sizeof(Output));
- printf("%s", Output);
- }
- /* Check if the user has entered commands. */
- if(FD_ISSET(0, &FDCopy))
- {
- memset(Input, 0, sizeof(Input));
- fgets(Input, sizeof(Input), stdin); /* Read from keyboard. */
- write(pty, Input, strlen(Input));
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement