Using the skeleton below

#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:

#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?!
any help much appreciated,

Dave

>syscall( 10, argv );
This says to send the interrupt 10 (which references the unlink operation) to the system for processing. It's directly equivalent to saying unlink ( argv[i] ); , which would be a much better solution:

#include <unistd.h> // read/write

int main( int argc, char *argv[] )
{
  int i;

  for ( i = 1; i < argc; i++ )
    unlink ( argv[i] );

  return 0;
}

>Does this involve using the tail command?!
tail doesn't have a matching C interface. Try saying man popen if you want to use it.

thanks, that's actually really helpful. so where can i find a list/examples of this type of code? as i can only use the linux api system calls for these questions.
also, any tips on the 'last 20' question? how would i find the system equivalent for tail()? because it would be tail that i'd use right?

thank you for your reply, you've enlightened me!

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.