running screen command from a c program - c

Here is the part of code I have written to stuff 0 to a screen session open in one of my Ubuntu terminal tabs.
char command[60];
strcpy( command, "screen -S 8305.pts-1.MYUb -X stuff $'0'" );
system(command);
It gets compiled fine with only a warning like
ignoring return value of ‘system’,
But when it comes to running it does nothing regarding the screen command. Before this, I was getting "No screen session found." error which was fortunately solved by asking about it here.
I have tried system() with other shell commands and it works perfectly fine. The command for screen also works fine when you run it in a terminal session not in c code.

Related

GDB monitor inside TMUX garbled

I was debugging in terminal using gdb, and I typed inlayout src,then I entered the UI mode.
Most of the time, it worked fine, but sometimes, the whole terminal text code got mixed.(I post the picture below.).
what's more, sometimes I typed in some shell command in gdb like !pmap <pid>, its output's format was also strange.
I tried to set TERM to screen-256color but it doesn't work.
I solved it in some sense.
I redirect the output of my program to another tty.

Can not connect to an already active screen through system() in c

Here is the part of code I have written to stuff 0 to a screen session open in one of my Ubuntu terminal tabs.
char command[60];
strcpy( command, "screen -S 8305.pts-1.MYUb -X stuff $'0'" );
system(command);
It gets compiled fine with only a warning like
ignoring return value of ‘system’,
But when it comes to running I get the message shown below:
No screen session found.
I have tried system() with other shell commands and it works perfectly fine. The command for screen also works fine when you run it in a terminal session not in c code.
Most probably you are running the command as a different user than the user that owns the screen. For example running the binary as sudo.
You can run ps aux to find the user under which your binary is running as.
To make the system command work you should run it as the user who owns the screen.
I think the problem is you are using -S which creates a new named screen, and -X which submits a command to an already running screen session.
You either want:
system( "screen -S 8305.pts-1.MYUb cmd" );
OR
system( "screen -r 8305.pts-1.MYUb -X cmd" );
FYI -- I'm not sure what stuff $0 is suppose to be, and in the context of the code you supplied is not going to work -- but I believe that a different problem then the one you reported.
From man(1) page
-S sessionname
When creating a new session, this option can be used to specify a meaningful name for the session.
And
-X Send the specified command to a running screen session.

How do i redirect code from terminal to a drawing app?

I am trying to create a simple program using C, that reads data from a text file and then creates a graph based on that data. I am running UNIX.
Half of my code is meant to use the terminal for printf and scanf for user input and displaying messages, and once that half is complete, the other half needs to start.
The 2nd half is meant to create a graph using drawapp.jar, a seperate drawing program.
When i compile using gcc -o filename filename.c and run using ./filename , it compiles fun but only runs the first half
When i compile using gcc -o filename filename.c graphics.c and run using ./filename | java -jar drawapp.jar , it compiles fine but when it runs it opens drawapp.jar which freezes on a blank grey screen and the terminal window does not show the first half of my code where it is meant to ask for user input and just freezes, the terminal allows me to type but it does nothing
Image on this link shows my problem: http://picpaste.com/Untitled-IlRWjqLN.png
How do i get the c code correct so that it does the first half in terminal then switches to drawapp.jar?
Thanks
fork() and execlp() with "java -jar drawapp.jar"
So as far as I understand, your program's stdout is piped to the java-process. Now you first want to communicate with the user using the terminal-window, and then send commands via stdout to the java.process.
So you could just open /dev/tty for output
FILE *f=fopen ("/dev/tty", "w");
fprintf (f, "Hi there\n");
and talk this way to the terminal. If you have output with no \n at the end, you might have to use fflush:
fprintf (f, "enter name: ");
fflush (f);

How to get this click and run effect in linux?

I have a c program
#include <stdio.h>
int main ()
{
printf("Hello");
}
On Windows:
I compile that program on windows and get a.exe
Now when I double-click on a.exe
Command windows opens
a.exe is run
and automatically close that windows.
To overcome this i have two solution
1> Create a batch file with the following content:
a.exe
pause
2> or add getch() function in my code
On LINUX
Now i want the same thing to happen in linux
I have compiled that program on linux get a.out and whenever i click on that nothing happens?
I have made one shell script:
#!/bin/bash
./a.out &
pause
and run that script by clicking on it but still nothing happened?
The thing you're not seeing is that it won't open in anything at all; the program will just execute quietly in the background.
To make the output visible, you'll need to make a shell script to run the program in a terminal, which can be as simple as this:
#!/bin/bash
gnome-terminal -x "`dirname \"$0\"`"/a.out
Mark the script as executable, and now you should be able to double-click it and see your program open in a terminal window. Note that tying the program to a particular terminal emulator (like gnome-terminal) is probably a bad idea, and you should rethink why you want this behaviour.
Linux doesn't distinct between terminal and GUI applications.
What you need to do is to actually configure the program to run in a terminal, or just run a terminal and execute the program in it.

Opening a new terminal window & executing a command

I've been trying to open a new terminal window from my application and execute a command on this second window as specified by the user. I've built some debugging software and I would like to execute the user's program on a separate window so my debugging output doesn't get intermixed with the programs output.
I am using fork() and exec(). The command I am executing is gnome-terminal -e 'the program to be executed'.
I have 2 questions:
Calling gnome-terminal means the user has to be running a gnome graphical environment. Is there a more cross-platform command to use (I am only interested in Linux machines though)?
After the command finishes executing the second terminal also finishes executing and closes. Is there any way to pause it, or just let it continue normal operation by waiting for input?
You probably want something like xterm -hold.
1) gnome-terminal should work reasonably also without the whole gnome environonment, anyway the old plain "xterm" is enough.
2) you can execute a short bash script that launch your program and at the end reads a line:
bash -c 'my program ... ; read a'
(or also 'xterm -e ...')

Resources