blob: 37946f77c31d426c3caddbd224ef4702657040af [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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 *
Theodore Ts'o543547a2010-05-17 21:31:56 -04008 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Library
10 * General Public License, version 2.
11 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000012 */
13
14/*
15 * History:
16 * 93/10/30 - Creation
17 */
18
Theodore Ts'o66d8c3f2000-12-02 06:11:44 +000019#define _LARGEFILE_SOURCE
20#define _LARGEFILE64_SOURCE
21
Theodore Ts'o50e1e101997-04-26 13:58:21 +000022#if HAVE_ERRNO_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <errno.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000024#endif
25#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <unistd.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000027#endif
28#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include <sys/ioctl.h>
30
Theodore Ts'o3839e651997-04-26 13:21:57 +000031#include "e2p.h"
32
Theodore Ts'o66d8c3f2000-12-02 06:11:44 +000033#ifdef O_LARGEFILE
34#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
35#else
36#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
37#endif
38
Theodore Ts'o3839e651997-04-26 13:21:57 +000039int fgetversion (const char * name, unsigned long * version)
40{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000041#if HAVE_EXT2_IOCTLS
Theodore Ts'o3c203cb2004-01-31 21:16:35 -050042#if !APPLE_DARWIN
Theodore Ts'o593c6ea2002-07-14 15:58:38 -040043 int fd, r, ver, save_errno = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000044
Theodore Ts'o66d8c3f2000-12-02 06:11:44 +000045 fd = open (name, OPEN_FLAGS);
Theodore Ts'o3839e651997-04-26 13:21:57 +000046 if (fd == -1)
Theodore Ts'o593c6ea2002-07-14 15:58:38 -040047 return -1;
Theodore Ts'o17dba281998-03-30 01:16:26 +000048 r = ioctl (fd, EXT2_IOC_GETVERSION, &ver);
Theodore Ts'o593c6ea2002-07-14 15:58:38 -040049 if (r == -1)
50 save_errno = errno;
Theodore Ts'occe382b1998-03-09 13:07:09 +000051 *version = ver;
Theodore Ts'o3839e651997-04-26 13:21:57 +000052 close (fd);
Theodore Ts'o593c6ea2002-07-14 15:58:38 -040053 if (save_errno)
54 errno = save_errno;
Theodore Ts'o3839e651997-04-26 13:21:57 +000055 return r;
Theodore Ts'o3c203cb2004-01-31 21:16:35 -050056#else
57 int ver=-1, err;
58 err = syscall(SYS_fsctl, name, EXT2_IOC_GETVERSION, &ver, 0);
59 *version = ver;
60 return(err);
61#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000062#else /* ! HAVE_EXT2_IOCTLS */
63 extern int errno;
64 errno = EOPNOTSUPP;
65 return -1;
66#endif /* ! HAVE_EXT2_IOCTLS */
Theodore Ts'o3839e651997-04-26 13:21:57 +000067}