#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    3
    Rep 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
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    3
    Rep 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! :)
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    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
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    3
    Rep 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:D
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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'.

IMN logo majestic logo threadwatch logo seochat tools logo