On Tue, 24 Jun 2003, Rex wrote:
> Christopher Sawtell wrote:
> >
> > man 2 truncate
> >
> > System call, You'll have to write a trivial C program to call the kernel
> > function. Surprisingly, nothing in man(1).
>
> Nope, `tis a function. man 2 truncate.
It is...and I've just written a C program to call it.
You'll need to do something like this to the attachment:
gcc -Wall -o truncate truncate.c
(assuming you save the attachment in a file called "truncate.c" :)
Tim Wright
Assistant Lecturer
Department of Computer Science
University of Canterbury
"Language, like terrorism, targets civilians and generates fear to
effect political change."
-- "Collateral Language" John Collins and Ross Glover ed.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main( int argc, const char* argv[])
{
off_t length;
long long temp;
/* Check number of arguments */
if (argc!=3)
{
printf("Usage: %s <file> <length>\n", argv[0]);
return 1;
}
/* convert third argument to long long */
length=atoll(argv[2]);
/* Check we've got a valid length */
if (length<1)
{
temp=length;
printf("Error: can't truncate to %lld length.\n", temp);
return 1;
}
/* truncate the file and chekc for errors*/
if (truncate(argv[1], length)==-1)
{
perror(argv[1]);
return 1;
}
/*hay, we got here OK :) */
return 0;
}