summaryrefslogtreecommitdiff
path: root/src/ftrunc.c
blob: 67aa502cb4b12f6f4a6ed9eb058cd9ed8cbddcdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
 * All Rights Reserved.
 */

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

int
main(argc, argv)
int     argc;
char    **argv;

{
char *filename="testfile";
int c;
int fd, err;

if(argc != 3)
        {        printf("Usage: cxfs_ftrunc -f filename\n");
                exit(1);
        }

while((c=getopt(argc,argv,"f:"))!=EOF) {
                switch (c) {
                case 'f':
                        filename = optarg;
                        break;
                default:
                        fprintf(stderr,"Usage: ftrunc -f filename\n");
                        exit(1);
                }
        }




          /* open RDWR but with read-only perms */
          fd = open(filename, O_CREAT|O_RDWR, 0400);
          if (fd < 0) {
                perror("open failed");
                return 1;
                }

          err = ftruncate(fd, 1000);
          if (err < 0) {
              perror("ftruncate failed");
              return 1;
           }

          err = unlink(filename);
          if (err < 0) {
                perror("unlink failed");
                return 1;
          }     

          return 0;

}