System calls

This is a discussion on System calls within the C Programming forums, part of the General Programming Boards category; I want to use a system call to copy (cp) a source file to a backup directory. I need to ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    System calls

    I want to use a system call to copy (cp) a source file to a backup directory. I need to pass the source and destination paths via command prompt, but the only way I've been able to get the system call to work is by hard coding it (below). Can this be done?

    Working line of code:

    Code:
    system("cp -r /home/myuser/prog.c /home/myuser/backup/prog.c");
    Attempt to pass path via variable:

    Code:
    snprintf(destpath, MAX_LEN, "\"cp -r /home/myuser/prog.c /home/myuser/backup/prog.c\"");
    printf("This is the destpath: %s\n", destpath); 
    system(destpath);
    The output from the non-working attempt is:
    sh: cp -r /home/myuser/prog.c /home/myuser/backup/prog.c: not found.

    However, I can take the sh: error, copy the cp command and paste it on a unix prompt and successfully copy the file from source to destination.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    The problem in your code is the single quotes which you have escaped that,
    for ex:
    snprintf(destpath, MAX_LEN, "\"cp -r /home/myuser/prog.c /home/myuser/backup/prog.c\"");

    Actually it must be
    snprintf(destpath, MAX_LEN, "cp -r /home/myuser/prog.c /home/myuser/backup/prog.c");

    The above one is Ok when the command dont have the arguments
    for ex :
    system("\"ls\""); // Will work fine

    system("\"ls -lrt\"") ; // wont work

  3. #3
    Registered User ungalnanban's Avatar
    Join Date
    Feb 2010
    Location
    Chenai
    Posts
    12

    Thumbs up system calls

    Dear friend see the following example code, It will copy the file "plan" from one directory to another directory using the system call.

    Example Code
    Code:
    #include<stdio.h>
    #include<malloc.h>
    #define MAX_LEN 1024
    
    main()
    {
    
            //system("ls /home/sugumar/SUGUMAR/");
            char *destpath;
            destpath=(char *)malloc(1024);
            snprintf(destpath, MAX_LEN, "cp -r /home/sugumar/plan /home/sugumar/SUGUMAR/");
            printf("\n\n\nThis is the destpath: %s\n", destpath);
            system(destpath);
    
            //system("ls /home/sugumar/SUGUMAR/");
    
    }
    you try with this above example. here I didn't escape the directory separator [ / ].

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    Smile

    Thanks guys! This was a quick fix, I really appreciate your responses.

    Alexander jack: After removing the quote characters and their corresponding escape characters, I got the results that I expected.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    It would be preferable if you used API instead.
    system has a lot of overhead and is subject to security issues.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    It would be preferable if you used API instead.
    system has a lot of overhead and is subject to security issues.
    I still don't see what kind of security issues system() could possibly have?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by cpjust View Post
    I still don't see what kind of security issues system() could possibly have?
    Replace the called program with a user-defined one. i.e. your program is running as root, someone replaces "ls" with their own. Bang, root access.

    Inject strings if any part is user-defined.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by zacs7 View Post
    Replace the called program with a user-defined one. i.e. your program is running as root, someone replaces "ls" with their own. Bang, root access.

    Inject strings if any part is user-defined.
    But wouldn't you have exactly the same problems with OS API calls like CreateProcess...?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    CreateProcess() has a security attribute. But sure, I guess.

  10. #10
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by zacs7 View Post
    Replace the called program with a user-defined one. i.e. your program is running as root, someone replaces "ls" with their own. Bang, root access.

    Inject strings if any part is user-defined.
    Be real. If his box has been rooted to the point where ls et al has been replaced with trojans, he has far bigger problems than use of system() to run apps from /usr/bin, starting with his basic security habits and setup. The thing is, I think you know that.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,007
    The thing here is to avoid external programs and use API to do the dirty work instead.
    system and APIs such as CreateProcess are both vulnerable to security issues. Copy API is less so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Basic system calls help.
    By AmbliKai in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 08:18 AM
  3. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 05:15 AM
  4. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 08:38 AM
  5. System Calls && Variables
    By Okiesmokie in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2002, 09:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21