-2

I have a few questions. One, how would one determine the TTY a program uses, and then passing it as a argument to another program?

Also, I'm using the writevt program and i'm sending a string to a TTY device. It works fine, but doesn't use the carriage return i'm sending it with. So, my question is, how would I do this?

Thank you for your time.

user2411434
  • 109
  • 1
  • 4

1 Answers1

2

To get the name of the current tty, use ttyname(). To send the carriage return character, use $'\r'.

#include <unistd.h> and char *tty_name = ttyname(STDIN_FILENO);

will give you the name of the current tty. See man 3 ttyname for more.

v7d8dpo4
  • 1,399
  • 8
  • 9
  • Pardon my ignorance, but how would I use ttyname() ? – user2411434 Mar 30 '16 at 04:08
  • Also, thank you for the answer on the carriage return. It's working perfectly. – user2411434 Mar 30 '16 at 04:09
  • One more question. How would I add ttyname() to this code: – user2411434 Mar 30 '16 at 04:48
  • I'm having trouble formatting the code. The "back ticks" don't work. int apply(void) { char *tty_name = ttyname(STDIN_FILENO); system("/usr/sbin/writevt -t ttyname -T new$'\r'"); } – user2411434 Mar 30 '16 at 04:54
  • So, yeah. I'm not sure how to add it to that command line. And this "Add Comment" dialog, doesn't allow me to use the enter key. – user2411434 Mar 30 '16 at 05:04
  • `ttyname(STDIN_FILENO)` will give you the tty that the program runs in. You should use the tty you want to write to. – v7d8dpo4 Mar 30 '16 at 05:08
  • `system("/usr/sbin/writevt -t ttyname(STDIN_FILENO) -T new$'\r'");` – user2411434 Mar 30 '16 at 05:16
  • Sorry to pester. But could you repost that code with "back ticks" ?? – user2411434 Mar 30 '16 at 05:29
  • Bleh. Same problem. It's unreadable. Would it be too much to ask if you could put it on pastebin? – user2411434 Mar 30 '16 at 05:57
  • If you want to write to the input buffer of another tty, use these headers: fcntl.h, terimos.h, sys/ioctl.h, unistd.h, string.h. Use this code: https://pastebin.mozilla.org/8865545 – v7d8dpo4 Mar 30 '16 at 06:01
  • Thank you, but it isn't compiling. – user2411434 Mar 30 '16 at 06:14
  • You should have a string `message`, which is whatever you want to write to the input buffer of the tty. And `tty_name` is the name of the tty you want to write to. If it is passed by the command line, like `program /dev/pts/1` it is `argv[1]`. – v7d8dpo4 Mar 30 '16 at 06:18
  • I'm looking for a way to pass the tty to `writevt`. – user2411434 Mar 30 '16 at 06:24
  • `char command[255] = "/usr/sbin/writevt -t "; strcpy(stpcpy(command + 21, tty_name), " -T $\'new\r\'"); system(command);` – v7d8dpo4 Mar 30 '16 at 06:31
  • `matrix.c: In function ‘apply’:` `matrix.c:136:75: error: ‘tty_name’ undeclared (first use in this function)` ` command[255] = "/usr/sbin/writevt -t "; strcpy(stpcpy(command + 21, ``tty_name), ` ^ `matrix.c:136:75: note: each undeclared identifier is reported only once ``for each function it appears in` `: recipe for target 'matrix.o' failed` `make: *** [matrix.o] Error 1` – user2411434 Mar 30 '16 at 06:39
  • Getting closer. Thank you. – user2411434 Mar 30 '16 at 06:39
  • Ok. I figured it out. Thank you so much for your help. – user2411434 Mar 30 '16 at 07:05
  • Ok. I'm having problems now. The `writevt` command seems to work locally, but not remotely. Is there anyway to invoke it remotely? – user2411434 Apr 07 '16 at 02:27
  • And by "locally", I mean literally that. At my own prompt. – user2411434 Apr 07 '16 at 02:28
  • Also, i'm using a `ttysnoop` like program to snoop on the user. For some reason the source isn't running the code when I use it. I created a `printf` to announce my arrival at the array for the code, and it only displays that `printf` when i'm not using that `ttysnoop` like program. I'm not sure what the problem is. – user2411434 Apr 07 '16 at 02:35
  • Again.. the users of my BBS aren't able to see the code in action. It's not executing the code for them. I've must of edited and compiled the code at least a hundred times trying to figure this out. – user2411434 Apr 07 '16 at 02:38
  • The problem is with `writevt` somehow. And yes, I have permissions to execute it. – user2411434 Apr 07 '16 at 02:46
  • Just to be clear, this is the code i'm talking about: `char command[255] = "/usr/sbin/writevt -t "; strcpy(stpcpy(command + 21, tty_name), " -T $\'new\r\'"); system(command);` – user2411434 Apr 07 '16 at 02:57
  • If you want to write to the input buffer of another tty, tty_name should be the tty you want to write to, not the name of the tty your program is running in. – v7d8dpo4 Apr 07 '16 at 03:21
  • Can you give me some example code? Thanks for your answer, BTW. – user2411434 Apr 07 '16 at 03:26
  • If you will pass name of the tty you want to write to as an argument, use `char command[255] = "/usr/sbin/writevt -t "; strcpy(stpcpy(command + 21, argv[1]), " -T \'new\r\'"); system(command);`. – v7d8dpo4 Apr 07 '16 at 03:30
  • What if I don't know the _name_ of the tty? – user2411434 Apr 07 '16 at 03:31
  • If you don't know the name of the tty, how are you going to know which one to write to? – v7d8dpo4 Apr 07 '16 at 03:37
  • That's exactly it, I don't know. – user2411434 Apr 07 '16 at 03:38
  • By the way, since you are using string in c, `'\r'` means carriage return. Sorry that I forgot it the first time. – v7d8dpo4 Apr 07 '16 at 03:39
  • Not a problem at all. – user2411434 Apr 07 '16 at 03:40
  • Ok. Really stupid question. But, how do I pass the output of "printf $(tty)" to `writevt`? – user2411434 Apr 07 '16 at 04:15
  • `char command[255] = "/usr/sbin/writevt -t "; strcpy(stpcpy(command + 21, argv[1]), " -T \'new\r\'"); system(command);` When you run the program, type `program /dev/tty1` to pass the name of the tty. – v7d8dpo4 Apr 07 '16 at 04:21
  • `#include ` `#include ` `#include ` `#include ` `#include ` `#include ` `#include ` `int main(int argc, char *argv[])` `{` `char command[255] = "/usr/sbin/writevt -t "; strcpy(stpcpy(command + 21, argv[1]), " -T \'new\r\'"); system(command);` `}` : – user2411434 Apr 07 '16 at 04:27
  • Sorry for the format of that code. But, is that correct? Because I get a segfault when running it with no parameters. – user2411434 Apr 07 '16 at 04:28
  • If you run it with no parameters other than the the name of the program, `argc` will be 1, so be sure you check `argc>1` before you use `argv[1]`. – v7d8dpo4 Apr 07 '16 at 04:34
  • Understood. Thank you. – user2411434 Apr 07 '16 at 04:40
  • Ok. I know the problem. The code is sending commands to the TTY, not the Pseudo Terminal. Hence, the code is sending commands to my telnet connection, rather than the actual PTS. – user2411434 Apr 07 '16 at 06:38
  • So, my question is this: will `writevt` work with pty devices? – user2411434 Apr 07 '16 at 07:24
  • Ok. It does. Now the question is what do I replace `tty_name` with. – user2411434 Apr 07 '16 at 08:10
  • Ok. I managed to get it working. I'm using the TTYSnoop telnetd server, and it appears to be working. I guess because TTYSnoop puts the screen in "raw" mode. – user2411434 Apr 07 '16 at 15:26