UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 29th, 2005, 05:57 PM
arun0000 arun0000 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 35 arun0000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 35 m 46 sec
Reputation Power: 4
execlp command

Hi guys,

I am trying to use the execlp command in my C program to run the PS command and send the output into a file specified in the Argument. Following is the command that I am trying to use without any luck.
any help would be greatly appreciated.
Thanks, Arun

PHP Code:
#include <stdio.h>
main()
{
char Argument[10]
printf ("Enter Argument\n");
gets(Argument);

execlp("/bin/ps","ps > "%s",Argument",NULL);


Reply With Quote
  #2  
Old June 30th, 2005, 10:27 AM
stdunbar stdunbar is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: May 2004
Location: Superior, CO, USA
Posts: 1,709 stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 18 h 11 m
Reputation Power: 338
Send a message via ICQ to stdunbar Send a message via Yahoo to stdunbar
I don't think all of your code is here. What is this line supposed to be?
Code:
execlp("/bin/ps","ps > "%s",Argument",NULL); 


execlp() does not take printf() like arguments. Also, your double quotes are all over the place. Lastly, gets() is one of the most dangerous method calls ever created in the standard C library. If this is just a school project, ok. If this is meant to ever be used by someone besides you then it needs to be removed.

Reply With Quote
  #3  
Old June 30th, 2005, 02:20 PM
arun0000 arun0000 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 35 arun0000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 35 m 46 sec
Reputation Power: 4
Quote:
Originally Posted by stdunbar
I don't think all of your code is here. What is this line supposed to be?
Code:
execlp("/bin/ps","ps > "%s",Argument",NULL); 


execlp() does not take printf() like arguments. Also, your double quotes are all over the place. Lastly, gets() is one of the most dangerous method calls ever created in the standard C library. If this is just a school project, ok. If this is meant to ever be used by someone besides you then it needs to be removed.


You are right, this is for a school project. I am trying to execute the UNIX's PS command in my C program. If I use the following command in my C program the PS command gets executed and the output of the PS command is written on the screen.
execlp("/bin/ps","ps",NULL);

But I need to change this command so the output of the PS command goes into a file, which is specified in the Argument as shown in the earliear code. I am not sure where to instert the Argument(file name) in this command.

Thanks

Reply With Quote
  #4  
Old July 1st, 2005, 11:43 AM
stdunbar stdunbar is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: May 2004
Location: Superior, CO, USA
Posts: 1,709 stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level)stdunbar User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 18 h 11 m
Reputation Power: 338
Send a message via ICQ to stdunbar Send a message via Yahoo to stdunbar
Take a look at the sprintf() man page. You'll want something on the order of:
Code:
char command[128];
sprintf( command, "ps > %s", argument );

execlp("/bin/sh", command, NULL);


You want to run the shell instead of ps as you want the redirect - that is a shell concept.

As a warning this is totally untested...

Reply With Quote
  #5  
Old July 12th, 2005, 08:13 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,089 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 3 h 43 m 40 sec
Reputation Power: 9
why not

Quote:
Originally Posted by arun0000
Hi guys,

I am trying to use the execlp command in my C program to run the PS command and send the output into a file specified in the Argument. Following is the command that I am trying to use without any luck.
any help would be greatly appreciated.
Thanks, Arun

PHP Code:
#include <stdio.h>
/* since early '90 , is main a int, declared as
int main(int argc,char **argv)
and returns|exits an int :(
*/
main()
{
char Argument[10]
printf ("Enter Argument\n");
gets(Argument); 
/*VERY. VERY, VERY dangerous !!!!!!
the chance of a core dump is 99,99%
prefer something like
fgets(Argument,sizeof(Argument),stdin);
*/
execlp("/bin/ps","ps > "%s",Argument",NULL); /* that's no C */
/* re-read man pages of execlp */
/*try
#define BUFF 512
FILE *ps;
char buff[BUFF];
ps = popen("/bin/ps mybe-whith-options","r");
while(fgets(buff,sizeof(bufff),ps)) printf("%s",buff);
pclose(ps);
*/



at least: think about color-blind peoble, don't play with colors!!
__________________
working on Solaris[5-9], preferred languages french and C.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > execlp command


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway