xfs/178: fix mkfs success test
[xfstests-dev.git] / src / ftrunc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 int
16 main(argc, argv)
17 int     argc;
18 char    **argv;
19
20 {
21 char *filename="testfile";
22 int c;
23 int fd, err;
24
25 if(argc != 3)
26         {        printf("Usage: cxfs_ftrunc -f filename\n");
27                 exit(1);
28         }
29
30 while((c=getopt(argc,argv,"f:"))!=EOF) {
31                 switch (c) {
32                 case 'f':
33                         filename = optarg;
34                         break;
35                 default:
36                         fprintf(stderr,"Usage: ftrunc -f filename\n");
37                         exit(1);
38                 }
39         }
40
41
42
43
44           /* open RDWR but with read-only perms */
45           fd = open(filename, O_CREAT|O_RDWR, 0400);
46           if (fd < 0) {
47                 perror("open failed");
48                 return 1;
49                 }
50
51           err = ftruncate(fd, 1000);
52           if (err < 0) {
53               perror("ftruncate failed");
54               return 1;
55            }
56
57           err = unlink(filename);
58           if (err < 0) {
59                 perror("unlink failed");
60                 return 1;
61           }     
62
63           return 0;
64
65 }