[Previous]
[Contents]
[Next]

chsize()

change the size of a file

Synopsis:

#include <unistd.h>
int chsize( int filedes, long size );

Description:

The chsize() function changes the size of the file associated with filedes, by extending or truncating the file to the length specified by size. If the file needs to be extended, the file is padded with NULL ('\0') characters.


Note: The chsize() function call ignores advisory locks that may have been set by the fcntl(), lock(), or locking() functions.

Returns:

0
Success.
-1
An error occurred; errno is set to indicate the error.

Errors:

EACCES
The specified file is locked against access.
EBADF
Invalid file descriptor, or the file is not opened for writing.
ENOSPC
There isn't enough space left on the device to extend the file.

Examples:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

void main()
  {
    int  filedes;

    filedes= open( "file", O_RDWR | O_CREAT,
        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
    if( filedes!= -1 ) {
      if( chsize( filedes, 32 * 1024L ) != 0 ) {
          printf( "Error extending file\n" );
      }
      close( filedes);
    }
  }

Classification:

WATCOM

Safety:
Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

close(), creat(), errno, open()


[Previous]
[Contents]
[Next]