blob: 752da0b5c9816822a06be4cba5a53d4bc75d933b [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * fgetflags.c - Get a file flags on an ext2 file system
3 *
4 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
7 *
8 * This file can be redistributed under the terms of the GNU Library General
9 * Public License
10 */
11
12/*
13 * History:
14 * 93/10/30 - Creation
15 */
16
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#if HAVE_ERRNO_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <errno.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#endif
20#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <unistd.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000022#endif
23#if HAVE_STAT_FLAGS
24#include <sys/stat.h>
25#else
26#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <sys/ioctl.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000028#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000029
30#include <linux/ext2_fs.h>
31
32#include "e2p.h"
33
34int fgetflags (const char * name, unsigned long * flags)
35{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000036#if HAVE_STAT_FLAGS
37 struct stat buf;
38
39 if (stat (name, &buf) == -1)
40 return -1;
41
42 *flags = 0;
43#ifdef UF_IMMUTABLE
44 if (buf.st_flags & UF_IMMUTABLE)
45 *flags |= EXT2_IMMUTABLE_FL;
46#endif
47#ifdef UF_APPEND
48 if (buf.st_flags & UF_APPEND)
49 *flags |= EXT2_APPEND_FL;
50#endif
51#ifdef UF_NODUMP
52 if (buf.st_flags & UF_NODUMP)
53 *flags |= EXT2_NODUMP_FL;
54#endif
55
56 return 0;
57#else
58#if HAVE_EXT2_IOCTLS
Theodore Ts'o3839e651997-04-26 13:21:57 +000059 int fd;
60 int r;
61
62 fd = open (name, O_RDONLY);
63 if (fd == -1)
64 return -1;
65 r = ioctl (fd, EXT2_IOC_GETFLAGS, flags);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000066
Theodore Ts'o3839e651997-04-26 13:21:57 +000067 close (fd);
68 return r;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000069#else /* ! HAVE_EXT2_IOCTLS */
70 extern int errno;
71 errno = EOPNOTSUPP;
72 return -1;
73#endif /* ! HAVE_EXT2_IOCTLS */
74#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000075}