Project

General

Profile

Bug #48202 » just_ftruncate.c

Comparision - John Mulligan, 11/11/2020 06:47 PM

 

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

int main(void) {
int failed = 0;
int fd;
int ret;
char *path = "/tmp/test_file";

fprintf(stdout, "Testing %s ...\n", path);

fprintf(stdout, "Create file ...\n");
fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0644);
if (fd < 0) {
fprintf(stderr, "open failed\n");
failed = 1;
goto done;
}
ret = close(fd);
if (ret != 0) {
fprintf(stderr, "close failed\n");
failed = 1;
goto done;
}

fprintf(stdout, "Testing truncate and write ...\n");
fd = open(path, O_RDONLY, 0644);
if (fd < 0) {
fprintf(stderr, "open failed\n");
failed = 1;
goto done;
}
ret = ftruncate(fd, 4096);
if (ret != 0) {
fprintf(stderr, "ftruncate failed\n");
failed = 1;
goto close;
}
ret = pwrite(fd, "this is just a test.", 20, 0);
if (ret < 0) {
fprintf(stderr, "pwrite failed\n");
failed = 1;
goto close;
}
close:
ret = close(fd);
if (ret != 0) {
fprintf(stderr, "close failed\n");
failed = 1;
goto done;
}

done:
return failed;
}
(2-2/2)