> It is...and I've just written a C program to call it.
Yes, writing a short wrapper around the C lib function had occured to
me, but I was wondering whether I was ignorant of some shell thing.
Thanks much Tim for doing the work for me :))
Unfortunately, it doesn't work.
off_t length;
signed long long temp;
length=atoll(argv[2]);
temp=length;
length=strtoll(argv[2], NULL, 10);
temp2=length;
printf("Arg 1: %s\nArg 2: %s\n", argv[1], argv[2]);
printf("%lld, %lld\n", temp, temp2);
gives:
./truncate bigfile 12345678901
Arg 1: bigfile
Arg 2: 12345678901
-539222987, -539222987
That can only mean one thing: off_t is only 32 bit. Running gcc -E and grep
gives:
typedef long int __off_t;
typedef __off_t off_t;
So I changed to signed long long length
and get
./truncate bigfile 2202009600
Arg 1: bigfile
Arg 2: 2202009600
bigfile: Invalid argument
Exit 1
When I read the man page of truncate this afternoon I was suspicious
because it didn't mention big files. It's certainly no always 64bit
clean. I can't find a LFS version of truncate(), but grep in
/usr/include finds some interesting info in features.h. Using
#define _FILE_OFFSET_BITS 64
as first line does switch off_t to 64bits, and then it works on large
files.
Is there a standard way of achieving this? I also find it interesting
that neither -std=c90 nor -std=c99 define off_t at all, and that std
lib functions use this type. Ehh, ok, truncate() is not a std lib
function.
Attached is the updated version with LFS.
Volker
--
Volker Kuhlmann is possibly list0570 with the domain in header
http://volker.dnsalias.net/ Please do not CC list postings to me.
/*
Truncate a given file at the given number of bytes.
Originally by Tim Wright <[EMAIL PROTECTED]>,
LFS (large file support) by Volker Kuhlmann <[EMAIL PROTECTED]>.
24 Jun 2003
*/
/*
Enable large file support (A better, standard way of doing this?)
Without this, off_t will be 32 bits only.
*/
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char* argv[])
{
off_t length;
signed long long temp;
/* Check number of arguments */
if (argc!=3)
{
printf("Usage: %s <file> <length>\n"
"With large file support. Length is in bytes.\n",
argv[0]);
return 1;
}
/* convert third argument to long long */
length=atoll(argv[2]);
temp=length;
/* debugging
length=strtoll(argv[2], NULL, 10);
temp2=length;
printf("Arg 1: %s\nArg 2: %s\n", argv[1], argv[2]);
printf("%lld, %lld\n", temp, temp2);
*/
/* Check we've got a valid length */
if (length<1)
{
temp=length;
printf("Error: can't truncate to %lli length.\n", temp);
return 1;
}
/* truncate the file and check for errors*/
if (truncate(argv[1], length)==-1)
{
perror(argv[1]);
return 1;
}
/* hay, we got here OK :) */
return 0;
}