The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
c system call rangaswamy High Level Programming 1 02-19-2008 09:53 AM
how to differentiate system call from library call muru UNIX for Advanced & Expert Users 2 07-19-2007 08:20 PM
Problem in system call spmlingam High Level Programming 1 04-03-2006 03:02 AM
a system call for sed in a awk script seaten UNIX for Dummies Questions & Answers 6 05-20-2005 07:17 AM
wait system call a25khan High Level Programming 2 02-11-2004 07:56 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-14-2006
Registered User
 

Join Date: Aug 2006
Posts: 21
Stumble this Post!
can we send arguments to system() call

Hi friends

my C code is

int main()
{
system("cp <source> <destination>");
}

my question is
how to set variables for <source> and <destination>
how can we pass it to system() call.

can you suggest me
thankyou
kingskar
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-14-2006
Registered User
 

Join Date: Aug 2006
Posts: 69
Stumble this Post!
In such situations it's always better to do a combination of fork and one of the exec family member.

if(fork()==0){
//do nothing
}
else{

exec....( what ever suits you)

}

You can also check the exec man page to see the all members of exec family!!

regards
Apoorva kumar
Reply With Quote
  #3 (permalink)  
Old 08-14-2006
Registered User
 

Join Date: Aug 2006
Posts: 21
Stumble this Post!
Thankyou apoorva

Thankyou for your valuable suggestion

Cheers
Shekar
Reply With Quote
  #4 (permalink)  
Old 08-14-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,546
Stumble this Post!
vfork and exec - that is what is done in system function call..

and to answer your question, something like this would do..
Code:
/* source file in char source[512] and dest file in char dest[512] */
char str[512];
memset(str, '\0', sizeof(512));
sprintf(str, "/usr/bin/cp %s %s", source, dest);
ret=system(str);
Reply With Quote
  #5 (permalink)  
Old 08-17-2006
Registered User
 

Join Date: Aug 2006
Posts: 21
Stumble this Post!
Quote:
Originally Posted by matrixmadhan
vfork and exec - that is what is done in system function call..

and to answer your question, something like this would do..
Code:
/* source file in char source[512] and dest file in char dest[512] */
char str[512];
memset(str, '\0', sizeof(512));
sprintf(str, "/usr/bin/cp %s %s", source, dest);
ret=system(str);

Yes
your code is exactly fits for my requirement.
Thankyou
Cheers
kingskar
Reply With Quote
  #6 (permalink)  
Old 08-17-2006
Registered User
 

Join Date: May 2006
Posts: 95
Stumble this Post!
if you are using glibc it may be better to use:
#define _GNU_SOURCE
#include <stdio.h>

char *buf;
asprintf(&buf,"cp %s/%s",src,dst); /* do not forget error check */
system(buf);
free(buf);

the advantage is that you have no limits for pathlenght.
you can also emulate asprintf with 2 calls to snprintf()
Reply With Quote
  #7 (permalink)  
Old 08-17-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,546
Stumble this Post!
what is the need to use free here ?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0