diff options
| author | Raghavendra Bhat <raghavendrabhat@gluster.com> | 2011-07-29 12:56:59 +0530 | 
|---|---|---|
| committer | Raghavendra Bhat <raghavendrabhat@gluster.com> | 2011-07-29 12:56:59 +0530 | 
| commit | 5733cc4bcb436bbb7c676173c72db182ae00fab9 (patch) | |
| tree | e652125d7e7346bdbce6896524b660f36b7832e0 /c_pgms | |
| parent | bb3a674ba1965a71e31d40f1ae41eb833de53a31 (diff) | |
programs to truncate a file based on path as well as fd.
Can be enhanced to write some truncate based application.
Diffstat (limited to 'c_pgms')
| -rw-r--r-- | c_pgms/truncate/fd_truncate.c | 30 | ||||
| -rw-r--r-- | c_pgms/truncate/truncate_file.c | 26 | 
2 files changed, 56 insertions, 0 deletions
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 <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <errno.h> +#include <fcntl.h> + +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 <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <errno.h> +#include <fcntl.h> + +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)); +        } +}  | 
