From 5733cc4bcb436bbb7c676173c72db182ae00fab9 Mon Sep 17 00:00:00 2001 From: Raghavendra Bhat Date: Fri, 29 Jul 2011 12:56:59 +0530 Subject: programs to truncate a file based on path as well as fd. Can be enhanced to write some truncate based application. --- c_pgms/truncate/fd_truncate.c | 30 ++++++++++++++++++++++++++++++ c_pgms/truncate/truncate_file.c | 26 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 c_pgms/truncate/fd_truncate.c create mode 100644 c_pgms/truncate/truncate_file.c (limited to 'c_pgms') diff --git a/c_pgms/truncate/fd_truncate.c b/c_pgms/truncate/fd_truncate.c new file mode 100644 index 0000000..1425ed0 --- /dev/null +++ b/c_pgms/truncate/fd_truncate.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main () +{ + int ret = -1; + int fd = -1; + char *str = "This is a string"; + char pwd[255] = {0, }; + + fd = open ("test_file", O_RDWR); + if (fd == -1) { + fprintf (stderr, "ERROR: open failed on the file test_file (%s)", + strerror (errno)); + } + ret = ftruncate (fd, 10); + if (ret == -1) { + fprintf (stderr, "ERROR: truncate failed on the file %s (%s)", + pwd, strerror (errno)); + } + + if (fd > 0) + close (fd); +} diff --git a/c_pgms/truncate/truncate_file.c b/c_pgms/truncate/truncate_file.c new file mode 100644 index 0000000..d6f0b60 --- /dev/null +++ b/c_pgms/truncate/truncate_file.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main () +{ + int ret = -1; + int fd = -1; + char *str = "This is a string"; + char pwd[255] = {0, }; + + getcwd (pwd, sizeof (pwd)); + + strcat (pwd, "/test_file"); + + ret = truncate (pwd, 10); + if (ret == -1) { + fprintf (stderr, "ERROR: truncate failed on the file %s (%s)", + pwd, strerror (errno)); + } +} -- cgit