LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2007, 03:56 AM   #1
Dave256000
LQ Newbie
 
Registered: Feb 2007
Posts: 5

Rep: Reputation: 0
c++ and linux


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?!

Much appreciated,

Dave
 
Old 03-04-2007, 01:27 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Yes, tail can do this. There's also one very standard function (look into stdio) that would be very useful. I'm not giving the name, because it's a homework question, and finding that out is rather easy. Then function->syscall is also easy.
 
Old 03-04-2007, 01:39 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
look at man syscall
apart from
Quote:
using syscall() is not recom-
mended.
on my solaris machine
looking in syscall.h you can see that syscall( 10, argv[i] );
Code:
#define SYS_unlink      10
you probably need syscall( 19, blah );
Code:
#define SYS_lseek       19
look at man lseek for how to use it
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
link dies intermittently-seemingly at random- between win<->linux not linux<->linux?? takahaya Linux - Networking 10 03-09-2007 10:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration