LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-25-2016, 01:54 PM   #1
Jasson
LQ Newbie
 
Registered: Jan 2016
Posts: 15

Rep: Reputation: Disabled
Write to cmd line from C/C++ program


Hello perople,

is it possible to write to the cmd Line from a C/C++ program (im 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 im asking
is, that i want to write to the "export" file in sys/class/gpio from an App. But i cant because the App is not on root level and i dont 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...
 
Old 01-25-2016, 02:01 PM   #2
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 3,017

Rep: Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507
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...?

Last edited by BW-userx; 01-25-2016 at 02:05 PM.
 
Old 01-25-2016, 02:09 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,353

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
You can use popen/pclose to interact with a shell.

Last edited by suicidaleggroll; 01-25-2016 at 02:10 PM.
 
Old 01-25-2016, 02:39 PM   #4
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 3,017

Rep: Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507
Quote:
Originally Posted by suicidaleggroll View Post
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

Last edited by BW-userx; 01-25-2016 at 03:06 PM.
 
Old 01-25-2016, 02:45 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
printf() ? or am i missing something ?
 
Old 01-25-2016, 02:47 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,353

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
Quote:
Originally Posted by BW-userx View Post
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.
 
1 members found this post helpful.
Old 01-25-2016, 02:57 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,009
Blog Entries: 1

Rep: Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110Reputation: 2110
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..

Last edited by astrogeek; 01-25-2016 at 02:58 PM.
 
Old 01-25-2016, 03:09 PM   #8
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 3,017

Rep: Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507Reputation: 507
Quote:
Originally Posted by suicidaleggroll View Post
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

Last edited by BW-userx; 01-25-2016 at 03:10 PM.
 
Old 01-25-2016, 03:19 PM   #9
Jasson
LQ Newbie
 
Registered: Jan 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
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...
 
Old 01-25-2016, 03:22 PM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
Quote:
Originally Posted by suicidaleggroll View Post
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.
 
Old 01-25-2016, 03:31 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,353

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
Quote:
Originally Posted by Jasson View Post
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?
 
Old 01-30-2016, 05:40 AM   #12
Jasson
LQ Newbie
 
Registered: Jan 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
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

Last edited by Jasson; 01-30-2016 at 05:46 AM.
 
Old 01-30-2016, 07:05 AM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i am trying to write a php program n ends up giving me parse error on line 21 shygvy Programming 6 05-09-2012 11:11 AM
Issue sg_modes cmd at cmd line, want to see the cmd in binary form NuUser Linux - Newbie 1 03-28-2012 09:08 AM
[SOLVED] NSLOOKUP - One Line cmd Line NetRock Programming 6 09-23-2010 08:53 AM
[SOLVED] Cmd Line help SteveThePirate Linux - Newbie 7 08-14-2009 03:40 AM
how to write command line program? vito_huang Linux - Software 3 04-14-2006 01:20 AM


All times are GMT -5. The time now is 06:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration