/* 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/types.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) {
        printf("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){ 
      printf("cannot open file");
      exit(1);
    }
    write(fildes,buf,SIZE);
    fsync(fildes);   /* flush the buffer and store the data to
                        disk at once */
    fstat(fildes,&filestatus);                        
    if(close(fildes)) printf("error in closing file\n");
    printf("The file size before truncate is %d\n", filestatus.st_size);
    truncate(name,SIZE/2);
    stat(name,&filestatus);                  
    printf("The file size after truncate is %d\n", filestatus.st_size);
    remove(name);
}
