/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* a sample program that truncates the size of file to 40 bytes. */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <sys/stat.h>
#define SIZE 80

int main() {
    int fildes;
    char buf[SIZE];
    char *name;
    struct stat filestatus;

    if((name = tmpnam(NULL)) == NULL) {
        fprintf(stderr, "temp file name could not be made. \n");
        exit(1);
    }
    strcpy(buf,"Welcome you! This is a test.");
    /* open file for reading only */
    if((fildes = open(name,O_RDWR | O_CREAT)) == -1){ 
        fprintf(stderr, "cannot open file\n");
        exit(1);
    }
    write(fildes,buf,SIZE);
    fsync(fildes);   /* flush the buffer and store the data to
                        disk at once */
    fstat(fildes,&filestatus);
    printf("The file size before truncate is %d\n", filestatus.st_size);
    ftruncate(fildes,SIZE/2);
    fstat(fildes,&filestatus);
    printf("The file size after truncate is %d\n", filestatus.st_size);
    if(close(fildes)) printf("error in closing file\n");
    remove(name);
}
