From 17c02484470c80ea97cc194d11d111cc1ca5b8b4 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 12 Mar 2009 14:17:59 +0100 Subject: [PATCH] lib: pttype - extend the API to work with file descriptors Signed-off-by: Karel Zak --- include/pttype.h | 1 + lib/pttype.c | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/pttype.h b/include/pttype.h index d6fe8128..d345501c 100644 --- a/include/pttype.h +++ b/include/pttype.h @@ -6,5 +6,6 @@ * libdisk from xfsprogs to util-linux-ng. */ extern const char *get_pt_type(const char *device); +extern const char *get_pt_type_pt(int fd); #endif diff --git a/lib/pttype.c b/lib/pttype.c index 7e1657de..c2294f13 100644 --- a/lib/pttype.c +++ b/lib/pttype.c @@ -235,15 +235,12 @@ bsd_parttable(unsigned char *base) } const char * -get_pt_type(const char *device) +get_pt_type_fd(int fd) { - int fd; char *type = NULL; unsigned char buf[PTTYPE_BUFSIZ]; - if ((fd = open(device, O_RDONLY)) < 0) - ; - else if (read(fd, buf, PTTYPE_BUFSIZ) != PTTYPE_BUFSIZ) + if (read(fd, buf, PTTYPE_BUFSIZ) != PTTYPE_BUFSIZ) ; else { if (sgi_parttable(buf)) @@ -259,9 +256,20 @@ get_pt_type(const char *device) else if (bsd_parttable(buf)) type = "BSD"; } + return type; +} + +const char * +get_pt_type(const char *device) +{ + int fd; + const char *type; - if (fd >= 0) - close(fd); + fd = open(device, O_RDONLY); + if (fd == -1) + return NULL; + type = get_pt_type_fd(fd); + close(fd); return type; } -- 2.39.5