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 #if HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
36 #endif
37 
38 #include "e2p.h"
39 
40 #ifdef O_LARGEFILE
41 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
42 #else
43 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
44 #endif
45 
fgetversion(const char * name,unsigned long * version)46 int fgetversion(const char *name, unsigned long *version)
47 {
48 	unsigned int ver = -1;
49 	int rc = -1;
50 #if HAVE_EXT2_IOCTLS
51 # if !APPLE_DARWIN
52 	int fd, save_errno = 0;
53 
54 	fd = open(name, OPEN_FLAGS);
55 	if (fd == -1)
56 		return -1;
57 
58 	rc = ioctl(fd, EXT2_IOC_GETVERSION, &ver);
59 	if (rc == -1)
60 		save_errno = errno;
61 	close(fd);
62 	if (rc == -1)
63 		errno = save_errno;
64 # else /* APPLE_DARWIN */
65 	rc = syscall(SYS_fsctl, name, EXT2_IOC_GETVERSION, &ver, 0);
66 # endif /* !APPLE_DARWIN */
67 #else /* ! HAVE_EXT2_IOCTLS */
68 	extern int errno;
69 
70 	errno = EOPNOTSUPP;
71 #endif /* ! HAVE_EXT2_IOCTLS */
72 	if (rc == 0)
73 		*version = ver;
74 
75 	return rc;
76 }
77