C++ and the linux API

Discussion in 'C++' started by Dave256000, Mar 4, 2007.

  1. Dave256000

    Dave256000 New Member

    Joined:
    Feb 28, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Using the skeleton below
    Code:
    #include <unistd.h> // read/write
    #include <sys/file.h> // open/close values
    #include <string.h> // strlen
    int main( int argc, char *argv[], char *env[] )
    {
    // C++ or C code
    }
    Write a C++ application myrm that removes (deletes) files passed as command line
    argument. Use only the Unix/Linux API in your program, do not use standard library
    functions.

    echo > File1
    ./myrm File1

    The answer for this turned out to be:
    Code:
    #include <unistd.h> // read/write
    #include <sys/file.h> // open/close values
    #include <string.h> // strlen
    int main( int argc, char *argv[], char *env[] )
    {
    // C++ or C code
        int i;
        for( i = 1; i < argc; i++ )
        {
            syscall( 10, argv[i] );
        }
        return 0;
    }
    Could anyone explain this to me, I'm having trouble understanding it. And if anyone's feeling really generous:

    using the same skeleton

    Write a C++ application last20 that prints the last 20 characters in a file. Use only
    the Unix/Linux API in your program, do not use standard library functions.

    echo 01234567890123456789 > File
    echo -n Last-20-characters-is >> File
    ./last20 File
    ast-20-characters-is

    Does this involve using the tail command?!

    Much appreciated,

    Dave
     
    Last edited by a moderator: Mar 5, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I am not a Linux expert but as I can understand syscall( 10, argv ); deletes the files passed as command line.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice