Initial public busybox upstream commit
[busybox4maemo] / e2fsprogs / old_e2fsprogs / e2p / fgetsetflags.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * fgetflags.c          - Get a file flags on an ext2 file system
4  * fsetflags.c          - Set a file flags on an ext2 file system
5  *
6  * Copyright (C) 1993, 1994  Remy Card <card@masi.ibp.fr>
7  *                           Laboratoire MASI, Institut Blaise Pascal
8  *                           Universite Pierre et Marie Curie (Paris VI)
9  *
10  * This file can be redistributed under the terms of the GNU Library General
11  * Public License
12  */
13
14 /*
15  * History:
16  * 93/10/30     - Creation
17  */
18
19 #ifdef HAVE_ERRNO_H
20 #include <errno.h>
21 #endif
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #ifdef HAVE_EXT2_IOCTLS
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #endif
31
32 #include "e2p.h"
33
34 #ifdef O_LARGEFILE
35 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
36 #else
37 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
38 #endif
39
40 int fgetsetflags (const char * name, unsigned long * get_flags, unsigned long set_flags)
41 {
42 #ifdef HAVE_EXT2_IOCTLS
43         struct stat buf;
44         int fd, r, f, save_errno = 0;
45
46         if (!stat(name, &buf) &&
47             !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
48                 goto notsupp;
49         }
50         fd = open (name, OPEN_FLAGS);
51         if (fd == -1)
52                 return -1;
53         if (!get_flags) {
54                 f = (int) set_flags;
55                 r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
56         } else {
57                 r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
58                 *get_flags = f;
59         }
60         if (r == -1)
61                 save_errno = errno;
62         close (fd);
63         if (save_errno)
64                 errno = save_errno;
65         return r;
66 notsupp:
67 #endif /* HAVE_EXT2_IOCTLS */
68         errno = EOPNOTSUPP;
69         return -1;
70 }