1 /*
2  * fgetversion.c	- Get a file version 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  * %Begin-Header%
9  * This file may be redistributed under the terms of the GNU Library
10  * General Public License, version 2.
11  * %End-Header%
12  */
13 
14 /*
15  * History:
16  * 93/10/30	- Creation
17  */
18 
19 #ifndef _LARGEFILE_SOURCE
20 #define _LARGEFILE_SOURCE
21 #endif
22 #ifndef _LARGEFILE64_SOURCE
23 #define _LARGEFILE64_SOURCE
24 #endif
25 
26 #include "config.h"
27 #if HAVE_ERRNO_H
28 #include <errno.h>
29 #endif
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35 
36 #include "e2p.h"
37 
38 #ifdef O_LARGEFILE
39 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
40 #else
41 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
42 #endif
43 
fgetversion(const char * name,unsigned long * version)44 int fgetversion(const char *name, unsigned long *version)
45 {
46 	unsigned int ver = -1;
47 	int rc = -1;
48 #if HAVE_EXT2_IOCTLS
49 # if !APPLE_DARWIN
50 	int fd, save_errno = 0;
51 
52 	fd = open(name, OPEN_FLAGS);
53 	if (fd == -1)
54 		return -1;
55 
56 	rc = ioctl(fd, EXT2_IOC_GETVERSION, &ver);
57 	if (rc == -1)
58 		save_errno = errno;
59 	close(fd);
60 	if (rc == -1)
61 		errno = save_errno;
62 # else /* APPLE_DARWIN */
63 	rc = syscall(SYS_fsctl, name, EXT2_IOC_GETVERSION, &ver, 0);
64 # endif /* !APPLE_DARWIN */
65 #else /* ! HAVE_EXT2_IOCTLS */
66 	extern int errno;
67 
68 	errno = EOPNOTSUPP;
69 #endif /* ! HAVE_EXT2_IOCTLS */
70 	if (rc == 0)
71 		*version = ver;
72 
73 	return rc;
74 }
75