0

I'm a beginner in C programming and I'm trying to write a code to send the input from one terminal to another terminal in wich I'm running a program (I don't know if it makes any difference but it's a telnet program). The reason for this is that often I'm writing a command to the program and it sends some text, making what I wrote so far impossible to change. My intention was running a program in a small terminal under the main one, and send the commands through this one, I've looked at many questions at stack overflow to write this program but it doesn't seem to work, so I decided to ask here what am I doing wrong? Sorry if that's not an apropriate question for this site, but it is the best I know. Here is my code:

(I'd also appreciate any sugestions you might have)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char command[1001];
    char px[1001 + 8 + 13 + 3] = "";
    char p1[8] = "echo | ";
    char p2[13] = " > /dev/pts/";
    char p3[3] = "";

    int term, compLimit, i;
    compLimit = 14;

    printf("Put terminal number here: ");
    scanf("%d", &term);
    sprintf(p3, "%s%d", p3, term);

    while(strncmp(px, "exit mudclient", compLimit))
    {
        for(i = 0; i < 1021; i++) px[i] = 0;
        scanf("%s", command);
        strcat(px, p1);
        strcat(px, command);
        strcat(px, p2);
        strcat(px, p3);
        system(px);
    }
    return 0;
}
Ivan Lerner
  • 101
  • 2
  • Instead getting a tty number, try getting the pid, then write to /proc/pid/fd/0 – ott-- May 23 '13 at 21:06
  • 1
    possible duplicate of [How can I create a loop between two TTYs?](http://stackoverflow.com/questions/513908/how-can-i-create-a-loop-between-two-ttys) [Also here](http://stackoverflow.com/a/7370822/775806). – n. m. could be an AI May 23 '13 at 21:27
  • I don't have the pid directory inside proc. @n.m. I'll take a look at that, thanks. – Ivan Lerner May 23 '13 at 22:40
  • @n.m. I looked at some pages of documentation and other ones trying to explain what the ioctl() function does, and how to use it, but I couldn't understand literaly anything, as I said, I'm a begginer. Could you please try to explain to me how to use it? Please don't judge me because I only spent half an hour looking for it, usually if I get just a tiny feeling that I can understand it I would spend hours looking for it, but this one seems to be really out of my skills. – Ivan Lerner May 23 '13 at 23:04
  • 1
    `ioctl` controls a device or a file in a way that standard file APIs do not cover. It has many uses as there are many things to control, and these things vary from device to device. For this reason it can accept many kinds of data as its third argument. The kind it accepts is determined by the second argument. Don't worry, just use it like shown [here](http://stackoverflow.com/a/7370822/775806). In this case, `ioctl` accepts a single character as the third argument, and inserts it in the TTY input as if it was send by the hardware. – n. m. could be an AI May 24 '13 at 12:40

1 Answers1

0

It looks to me from your description of your problem that what you actually need is master ncurses library. With it you will be able to separate output from input within a single terminal, and you will also gain full-featured input editing capabilities (with a scrollable command history).

Alexander Amelkin
  • 759
  • 1
  • 9
  • 15