LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Write to cmd line from C/C++ program (https://www.linuxquestions.org/questions/linux-newbie-8/write-to-cmd-line-from-c-c-program-4175567862/)

Jasson 01-25-2016 12:54 PM

Write to cmd line from C/C++ program
 
Hello perople,

is it possible to write to the cmd Line from a C/C++ program (iīm afraid no)?
In case yes, how?
Or may be it is possible to create a bash file by a C/C++ program and run it it from there?

The reason iīm asking
is, that i want to write to the "export" file in sys/class/gpio from an App. But i canīt because the App is not on root level and i donīt want to be forced to install "stuff" - fu__ing bad for distribution...

So if i could write to the cmd line, i could tell the system to do a chmod operation on the files i need.

best regards...

BW-userx 01-25-2016 01:01 PM

Code:

main(int argc, char *argv[])
int argc is the count off cmd line that goes to *argv[] the argument off cmd line

Bash or C you can't really combine the two together in code per sa.

sounds like by what little information given BASH Sctipt maybe a good starting point for you.

but what is your main project idea...?

suicidaleggroll 01-25-2016 01:09 PM

You can use popen/pclose to interact with a shell.

BW-userx 01-25-2016 01:39 PM

Quote:

Originally Posted by suicidaleggroll (Post 5487225)
You can use popen/pclose to interact with a shell.

you'd be using BASH script code to open and close files not C or C++ so no.

Quote:

The popen() function shall execute the command specified by the string command. It shall create a pipe between the calling program and the executed command, and shall return a pointer to a stream that can be used to either read from or write to the pipe.

...

The pclose() function shall close a stream that was opened by popen(), wait for the command to terminate, and return the termination status of the process that was running the command language interpreter. However, if a call caused the termination status to be unavailable to pclose(), then pclose() shall return -1 with errno set to [ECHILD] to report this situation. This can happen if the application calls one of the following functions:
them two calls are replaced with this
Code:

|
its called the "Pipe" that is used to send information between two programs.

Code:

userx@voided ~>>$ps aux | grep firefox
userx    1767 27.3 23.0 2655216 924128 ?      Sl  05:26 139:32 firefox
userx    32425  0.0  0.0  10728  2272 pts/2    S+  13:55  0:00 grep firefox

sends all of the information to grep that picks out what I tell it to look for returning just that information and not everything that is running by the use of the Pipe. It does the same function as the two system calls you gave.


When to use Bash

schneidz 01-25-2016 01:45 PM

printf() ? or am i missing something ?

suicidaleggroll 01-25-2016 01:47 PM

Quote:

Originally Posted by BW-userx (Post 5487240)
you'd be using BASH script code to open and close files not C or C++ so no.

What?

He asked for a way to run shell commands from inside C/C++, that's exactly what popen does.

astrogeek 01-25-2016 01:57 PM

Yes, popen/pclose it is.

If you want to use c++ stream syntax instead you should look into the pstreams project on sourceforge - does the same thing but with streams instead of popen..

BW-userx 01-25-2016 02:09 PM

Quote:

Originally Posted by suicidaleggroll (Post 5487242)
What?

He asked for a way to run shell commands from inside C/C++, that's exactly what popen does.

OIC, my mindset was BASHing, missread, read this

Jasson 01-25-2016 02:19 PM

Quote:

Description

The popen() function opens a process
This sounds usefull. As i wrote, i want to write to the "export" file in sys/class/gpio from an App. But the export file needs root priviliges.
So if i open a process with popen and do a "su" with it the process should become root. (?)

The complete background:
I have a Banana-Pi with Android 4.2. I can write Apps for it with Qt Creator which can build .apk files out of the box which makes distributing quite simple. But the moment i need to preinstall something to perfrom GPIO access or type something to a terminal on the device all the convinient feeling from distributing .apk file is useless.
So i need a way to make my Qt App to write to this "export" file.

To be honest, i dont see a reason to provide GPIO drivers and block them for non root accesses. That makes it kind of useless if i cant use the drivers from a program...

schneidz 01-25-2016 02:22 PM

Quote:

Originally Posted by suicidaleggroll (Post 5487242)
What?

He asked for a way to run shell commands from inside C/C++, that's exactly what popen does.

thanks for the translation, i used to do fork(); execve(); wait();. but then i started using system();. i havent had this need in years though.

suicidaleggroll 01-25-2016 02:31 PM

Quote:

Originally Posted by Jasson (Post 5487256)
This sounds usefull. As i wrote, i want to write to the "export" file in sys/class/gpio from an App. But the export file needs root priviliges.
So if i open a process with popen and do a "su" with it the process should become root. (?)

The complete background:
I have a Banana-Pi with Android 4.2. I can write Apps for it with Qt Creator which can build .apk files out of the box which makes distributing quite simple. But the moment i need to preinstall something to perfrom GPIO access or type something to a terminal on the device all the convinient feeling from distributing .apk file is useless.
So i need a way to make my Qt App to write to this "export" file.

To be honest, i dont see a reason to provide GPIO drivers and block them for non root accesses. That makes it kind of useless if i cant use the drivers from a program...

Two things:

1) Usually systems like that have a dedicated group for access to gpios and the like, and the default user is already a member of the group, so you shouldn't have to worry about permissions.

2) Usually systems like that have passwordless sudo access for the default user, so you could just stick "sudo" in front of the command to run it. Does the Banana-Pi not do this?

Jasson 01-30-2016 04:40 AM

I have tried to put sudo or su in front of the commands, but that doesnt work in my distribution.

What i want do is performing
su [hit ENTER]
echo 101 > /sys/class/gpio/export [hit ENTER]

from C code in order to get the gpio101 folder exported.
I tried that with the "yes" command, but that is not installed on the distribution, i cant accept to be forced to do installations.

It does NOT work, if i open the pipe with a command and try to use "fprintf" on the filepointer with further commands like
fp = popen(su, "r");
if(fp == NULL) close();
fprintf(fp, "s", "echo 101 > /sys/class/gpio/export");

There IS a percentage-sign in front of the s, its just not showing here...

I have also been searching on how to [hit ENTER] in scribt files - didnt help
I have tried
fp = popen("su\n echo 101 > /sys/class/gpio/export\n", "r");
which didnt work

I know, that a SINGLE command is performed when doing for example
fp = popen("echo 101 > /sys/class/gpio/export", "r");
I verified that, by performing
su
cd /sys/class/gpio
chmod 777 export

in a shell before starting the app.
But i must get around using the shell

jpollard 01-30-2016 06:05 AM

Note: using popen/system is NOT a secure function. The interaction between environment variables/functions, search lists, file globbing opens all kinds of security issues.


All times are GMT -5. The time now is 05:03 AM.