smdev

suckless mdev
git clone git://git.2f30.org/smdev.git
Log | Files | Refs | README | LICENSE

commit 4953771833f88278c3109e0b40d5746f9004fc9b
parent 733f60dba2b8c878945b856c23a6bc44de52fc11
Author: sin <sin@2f30.org>
Date:   Wed, 21 Aug 2013 09:13:20 +0100

Factor out devtomajmin() and devtype()

Diffstat:
Makefile | 1+
smdev.c | 40----------------------------------------
util.h | 2++
util/dev.c | 46++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 49 insertions(+), 40 deletions(-)

diff --git a/Makefile b/Makefile @@ -6,6 +6,7 @@ include config.mk LIB = \ util/agetcwd.o \ util/apathmax.o \ + util/dev.o \ util/eprintf.o \ util/estrtol.o \ util/recurse.o diff --git a/smdev.c b/smdev.c @@ -14,8 +14,6 @@ #include "config.h" #include "util.h" -static int devtomajmin(const char *path, int *maj, int *min); -static int devtype(const char *majmin); static int create_dev(const char *path); static void sysrecurse(const char *path); @@ -46,44 +44,6 @@ main(int argc, char *argv[]) return 0; } -/* Example `path' is /sys/devices/virtual/tty/tty0/dev */ -static int -devtomajmin(const char *path, int *maj, int *min) -{ - char buf[BUFSIZ]; - int fd; - ssize_t n; - - fd = open(path, O_RDONLY); - if (fd < 0) - eprintf("open %s:", path); - n = read(fd, buf, sizeof(buf) - 1); - close(fd); - if (n < 0) - eprintf("%s: read error:", path); - if (!n) - return -1; - if (buf[n - 1] == '\n') - buf[n - 1] = '\0'; - buf[n] = '\0'; - sscanf(buf, "%d:%d", maj, min); - return 0; -} - -static int -devtype(const char *majmin) -{ - char path[PATH_MAX]; - - snprintf(path, sizeof(path), "/sys/dev/block/%s", majmin); - if (!access(path, F_OK)) - return S_IFBLK; - snprintf(path, sizeof(path), "/sys/dev/char/%s", majmin); - if (!access(path, F_OK)) - return S_IFCHR; - return -1; -} - static int create_dev(const char *path) { diff --git a/util.h b/util.h @@ -7,6 +7,8 @@ extern char *argv0; char *agetcwd(void); void apathmax(char **, long *); +int devtomajmin(const char *path, int *maj, int *min); +int devtype(const char *majmin); void enprintf(int, const char *, ...); void eprintf(const char *, ...); long estrtol(const char *, int); diff --git a/util/dev.c b/util/dev.c @@ -0,0 +1,46 @@ +/* See LICENSE file for copyright and license details. */ +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <limits.h> +#include <stdio.h> +#include "../util.h" + +/* Example `path' is /sys/devices/virtual/tty/tty0/dev */ +int +devtomajmin(const char *path, int *maj, int *min) +{ + char buf[BUFSIZ]; + int fd; + ssize_t n; + + fd = open(path, O_RDONLY); + if (fd < 0) + eprintf("open %s:", path); + n = read(fd, buf, sizeof(buf) - 1); + close(fd); + if (n < 0) + eprintf("%s: read error:", path); + if (!n) + return -1; + if (buf[n - 1] == '\n') + buf[n - 1] = '\0'; + buf[n] = '\0'; + sscanf(buf, "%d:%d", maj, min); + return 0; +} + +/* `majmin' format is maj:min */ +int +devtype(const char *majmin) +{ + char path[PATH_MAX]; + + snprintf(path, sizeof(path), "/sys/dev/block/%s", majmin); + if (!access(path, F_OK)) + return S_IFBLK; + snprintf(path, sizeof(path), "/sys/dev/char/%s", majmin); + if (!access(path, F_OK)) + return S_IFCHR; + return -1; +}