LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell commands execution in c program without system function (https://www.linuxquestions.org/questions/programming-9/shell-commands-execution-in-c-program-without-system-function-4175418528/)

yahoosam 07-25-2012 01:41 AM

shell commands execution in c program without system function
 
Dear All:
i require to use execute shell commands in c program without the use of "system()" function.
As it was strictly warned us to take that advantage.

can any one assist me any Header/Library with some group of functions which does exactly as it's done by System function.
Thanx in Advance

NevemTeve 07-25-2012 01:44 AM

Homework?

fork + execl(and co) are your friends...

jschiwal 07-25-2012 01:51 AM

This does sound like homework. That gives me license for a little RTFM. Read the notes in the man 3 system manpage. It explains when system() can be dangerous and what to use instead.

yahoosam 07-25-2012 04:06 AM

Dear jschiwal,
i read MAN page now for specific commands which i entered into my program..
Actually what i want to know is that,
As i write date on Terminal,it gives current system date
while using the same in C-Program we use system function.But Sir told not to use it.Don't know why he told that.
he Exactly told us to find some functions/libraries which will do as system does for you.

In case of DATE,find a function which will return current date of your system..
so,now can you assist please.

NevemTeve 07-25-2012 04:24 AM

For a quickstart: time(2) + ctime(3)
Advanced usage: time(2) + localtime_r(3) + strftime(3)

Note: the number after the name refers to the manual sections, eg:
man 2 time
man 3 ctime
etc

jschiwal 07-25-2012 04:51 AM

Quote:

i require to use execute shell commands in c program without the use of "system()" function.
The man page for system() has an explanation why using the system function can be dangerous. It then says which functions you should use in it's place. This is the information you are asking about.

yahoosam 07-25-2012 07:09 AM

Dear jschiwal,
i went through MAN page of following commands->cat,ps,top,df,more
i could not find any function suggestion in here.
will you please view MAN for above few commands and post in here,so that i could get some rough idea for further commands.
Actually i am very new in reading those system manuals and all.the whole manual is beyond my understanding.

NevemTeve 07-25-2012 07:53 AM

As mentioned above, it is time, ctime, localtime_r and strftime.

Note: Tha manpage of system explains how can you use fork+exec instead; but just to get the current date/time you don't have to call external programs at all.

bigearsbilly 07-26-2012 06:01 AM

I think you may find popen a good choice.

NevemTeve 07-26-2012 06:21 AM

Function popen, to get the current date? How?

Quote:

In case of DATE,find a function which will return current date of your system..
so,now can you assist please.

bigearsbilly 07-26-2012 09:01 AM

popen in answer to the original question.

yahoosam 08-28-2012 12:38 AM

Thanks all of you..
I tried with fork but execution speed and Robustness were primary concern.
let me tell you easy way,might be helpfull for others.
I tried with "EditLine Library",& have full unbreakable shell,Actually it Interfaces between our c program and System command functions.


All times are GMT -5. The time now is 03:38 PM.