C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 6th, 2003, 01:04 PM
YodasToes YodasToes is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 3 YodasToes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What the system command returns. Is it safe to use?

I have a question that I have been pondering and would appreciate any replies on this issue. When using the system() command especially on multi-platform systems will you get the return code of the action command you pass to the system command?

For example if I do the following command on UNIX:

int rc = system("cp test.txt test1.txt");

Will the return code of the above command, let me know if the actual copy operation failed or whether just the system command was called successfully?

Isn't there a hole in this logic. To be 100% sure that the copy command completed successful I would have to write a copy routine correct? The system command does not guarantee me that the copy command worked correctly?

Thank you

Reply With Quote
  #2  
Old March 6th, 2003, 02:03 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
From the man page for system():

RETURN VALUE
The value returned is 127 if the execve() call for /bin/sh
fails, -1 if there was another error and the return code
of the command otherwise.

MS VC1.52 help lists some of the possible errno values, which I assume are also valid in the UNIX version, though that might not be the case:

A return value of -1 indicates an error, and errno is set to one of the following values:

E2BIG

In MS-DOS, the argument list exceeds 128 bytes, or the space required for the environment information exceeds 32K.

ENOENT

The command interpreter cannot be found.

ENOEXEC

The command-interpreter file has an invalid format and is not executable.

ENOMEM

Not enough memory is available to execute the command; the available memory has been corrupted; or an invalid block exists, indicating that the process making the call was not allocated properly.


You should read the man page for system(), because it warns of a few more things that could cause problems.

Reply With Quote
  #3  
Old March 6th, 2003, 04:12 PM
YodasToes YodasToes is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 3 YodasToes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
dwise thank you for your reply.

I am still a little confused if I have my basis covered when I use

int rc = system("cp test.txt test1.txt");

if (rc == -1)
{
"Alert" etc.
}

Because we have had an instance in the past when the file system was having troubles and the file data was not copied (test.txt to test1.txt) in the above example. However, we didn't receive any errors from our program stating there was an issue. Would the system call in this case return back if anything went wrong in the copy process. Could the system command be my culprit? If using the system command is unsafe then I will write my own copy routine. Thank you for any time!

Reply With Quote
  #4  
Old March 6th, 2003, 04:30 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
As I interpret the man page, system() returns a -1 if IT fails. Otherwise, it returns the return code of the command. So you should test for any non-zero value having been returned.

I'm not sure what cp's return code is for failure.

Reply With Quote
  #5  
Old March 6th, 2003, 04:32 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 49 m 40 sec
Reputation Power: 797
It entirely depends on whether the cp program returns error codes or not and what values you're checking at the return. IIRC cp returns 0 on success (at least on my system), so I would rewrite the check as:

int rc = system("cp test.txt test1.txt");
if (rc != 0)
alert

Reply With Quote
  #6  
Old March 6th, 2003, 04:38 PM
YodasToes YodasToes is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 3 YodasToes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you all very much for your replies.

Therefore, as long as I get my return codes down and have them covered using the system command should be fine? So in other words I will get the actual return code back from forked "cp" process? I was worried there might be a case in which I would not get the actual return code back from the forked process that the system command called but it seems that using the system command is safe and my basis are covered.

Thank you

Reply With Quote
  #7  
Old March 6th, 2003, 07:38 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
Of course, the final arbiter in the matter is the compiler. So, based on the information you've gathered, write a short program to test it out.

BTW, I did two copies, both with cp, one on a file that existed and the second on one that did not. Immediately after each cp command, I echoed the return code (echo $?): successful copy returned a '0' and the failed copy returned a '1'.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > What the system command returns. Is it safe to use?


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 1 hosted by Hostway