LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2012, 01:41 AM   #1
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Rep: Reputation: Disabled
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
 
Old 07-25-2012, 01:44 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,452

Rep: Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057
Homework?

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

Last edited by NevemTeve; 07-25-2012 at 01:48 AM.
 
1 members found this post helpful.
Old 07-25-2012, 01:51 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
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.
 
1 members found this post helpful.
Old 07-25-2012, 04:06 AM   #4
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-25-2012, 04:24 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,452

Rep: Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057
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
 
Old 07-25-2012, 04:51 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
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.
 
Old 07-25-2012, 07:09 AM   #7
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
Old 07-25-2012, 07:53 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,452

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

Last edited by NevemTeve; 07-25-2012 at 08:07 AM.
 
Old 07-26-2012, 06:01 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
I think you may find popen a good choice.
 
Old 07-26-2012, 06:21 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,452

Rep: Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057Reputation: 1057
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.
 
Old 07-26-2012, 09:01 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
popen in answer to the original question.
 
Old 08-28-2012, 12:38 AM   #12
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

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


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
Automating execution of commands inside a program deeptisjains Linux - Newbie 1 01-02-2012 12:21 AM
adding commands to a limited shell program khoh Programming 3 04-04-2011 09:50 AM
Issuing commands from c++ program to the shell cyberfox007 Programming 7 02-03-2008 04:06 AM
make c-program that accepts shell commands and execute them boeroe Programming 4 02-18-2005 08:41 AM
need help with system function in C program veereshai Programming 6 10-26-2004 03:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:27 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