diff --git a/elks/init/main.c b/elks/init/main.c index c7d463c8e..8fbda79af 100644 --- a/elks/init/main.c +++ b/elks/init/main.c @@ -253,7 +253,7 @@ static struct dev_name_struct { const char *name; int num; } devices[] = { - /* root_dev_name needs first 5 in order*/ + /* the 4 partitionable drives must be first */ { "hda", DEV_HDA }, { "hdb", DEV_HDB }, { "hdc", DEV_HDC }, @@ -284,7 +284,7 @@ static char * INITPROC root_dev_name(int dev) if (i < 4) { if (dev & 0x07) { name[NAMEOFF+3] = '0' + (dev & 7); - name[NAMEOFF+4] = 0; + name[NAMEOFF+4] = '\0'; } } return name; diff --git a/elkscmd/disk_utils/df.c b/elkscmd/disk_utils/df.c index de0847b9b..15ae52eab 100644 --- a/elkscmd/disk_utils/df.c +++ b/elkscmd/disk_utils/df.c @@ -37,8 +37,7 @@ struct dnames { char *name; char *mpoint; }; -struct dnames *devname(char *); -static char *dev_name(dev_t); +struct dnames *device_path(char *); int df_fat(char *, int); int iflag= 0; /* Focus on inodes instead of blocks. */ @@ -85,7 +84,7 @@ int main(int argc, char *argv[]) if (argc > 1) device = argv[1]; - dname = devname(device); + dname = device_path(device); if (!(blockdev = dname->name)) { fprintf(stderr, "Can't find /dev/ device for %s\n", device); exit(1); @@ -105,7 +104,7 @@ int main(int argc, char *argv[]) int i; for (i = 0; i < NR_SUPER; i++) { if (ustatfs(i, &statfs, UF_NOFREESPACE) >= 0) { - char *nm = dev_name(statfs.f_dev); + char *nm = devname(statfs.f_dev, S_IFBLK); char *filler = " "; /* Text alignment */ if (Pflag) filler = ""; if (statfs.f_type > FST_MSDOS || (statfs.f_type == FST_MSDOS && (Pflag||iflag))) @@ -277,7 +276,7 @@ bit_t bit_count(unsigned blocks, bit_t bits, int fd) */ /* return /dev/ device from dirname */ -struct dnames *devname(char *dirname) +struct dnames *device_path(char *dirname) { static char dev[] = "/dev"; struct stat st, dst; @@ -322,44 +321,3 @@ struct dnames *devname(char *dirname) closedir(fp); return NULL; } - -/* - * Convert a block device number to name. - * From mount.c - */ -static struct dev_name_struct { - char *name; - dev_t num; -} devices[] = { - /* root_dev_name needs first 5 in order*/ - { "hda", 0x0300 }, - { "hdb", 0x0320 }, - { "hdc", 0x0340 }, - { "hdd", 0x0360 }, - { "fd0", 0x0380 }, - { "fd1", 0x03a0 }, - { "ssd", 0x0200 }, - { "rd", 0x0100 }, - { NULL, 0 } -}; - -static char *dev_name(dev_t dev) -{ - int i; -#define NAMEOFF 5 - static char name[10] = "/dev/"; - - for (i=0; i #include #include +#include #include #include @@ -22,47 +23,6 @@ static char *fs_typename[] = { 0, "minix", "msdos", "romfs" }; -static struct dev_name_struct { - char *name; - dev_t num; -} devices[] = { - /* root_dev_name needs first 5 in order*/ - { "hda", 0x0300 }, - { "hdb", 0x0320 }, - { "hdc", 0x0340 }, - { "hdd", 0x0360 }, - { "fd0", 0x0380 }, - { "fd1", 0x03a0 }, - { "ssd", 0x0200 }, - { "rd0", 0x0100 }, - { "rd1", 0x0101 }, - { NULL, 0 } -}; - -/* - * Convert a block device number to name. - */ -static char *dev_name(dev_t dev) -{ - int i; -#define NAMEOFF 5 - static char name[10] = "/dev/"; - - for (i=0; i */ -#include -#include - #ifndef __STDLIB_H #define __STDLIB_H +/* stdlib.h */ +#include +#include #include /* Don't overwrite user definitions of NULL */ @@ -59,6 +58,7 @@ void exit (int status); int system(const char *command); void qsort(void *base, size_t nel, size_t width, int (*compar)(/*const void *, const void * */)); +char *devname(dev_t dev, mode_t type); #ifndef __STRICT_ANSI__ void breakpoint(); diff --git a/libc/misc/Makefile b/libc/misc/Makefile index 5f6f793f9..5b2178862 100644 --- a/libc/misc/Makefile +++ b/libc/misc/Makefile @@ -12,6 +12,7 @@ OBJS = \ atol.o \ basename.o \ crypt.o \ + devname.o \ dirname.o \ dtostr.o \ ecvt.o \ diff --git a/libc/misc/devname.c b/libc/misc/devname.c new file mode 100644 index 000000000..7a2ffe7fa --- /dev/null +++ b/libc/misc/devname.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include __SYSINC__(devnum.h) + +/* + * Convert a block or character device number to name. + * + * This version uses known device numbers to avoid disk reads + * for floppy systems. + */ + +#define ARRAYLEN(a) (sizeof(a)/sizeof(a[0])) + +static struct dev_name_struct { + char *name; + mode_t type; + dev_t num; +} devices[] = { + /* the 4 partitionable drives must be first */ + { "hda", S_IFBLK, DEV_HDA }, + { "hdb", S_IFBLK, DEV_HDB }, + { "hdc", S_IFBLK, DEV_HDC }, + { "hdd", S_IFBLK, DEV_HDD }, + { "fd0", S_IFBLK, DEV_FD0 }, + { "fd1", S_IFBLK, DEV_FD1 }, + { "ssd", S_IFBLK, MKDEV(SSD_MAJOR, 0) }, + { "rd0", S_IFBLK, MKDEV(RAM_MAJOR, 0) }, + { "ttyS0", S_IFCHR, DEV_TTYS0 }, + { "ttyS1", S_IFCHR, DEV_TTYS1 }, + { "tty1", S_IFCHR, DEV_TTY1 }, + { "tty2", S_IFCHR, DEV_TTY2 }, + { "tty3", S_IFCHR, DEV_TTY3 }, +}; + +static char name[NAME_MAX+6] = "/dev/"; + +static char *__fast_devname(dev_t dev, mode_t type) +{ + int i; + unsigned mask; +#define NAMEOFF 5 + + for (i = 0; i < ARRAYLEN(devices); i++) { + mask = (i <= 4)? 0xfff8: 0xffff; + if (devices[i].type == type && devices[i].num == (dev & mask)) { + strcpy(&name[NAMEOFF], devices[i].name); + if (i < 4) { + if (dev & 0x07) { + name[NAMEOFF+3] = '0' + (dev & 7); + name[NAMEOFF+4] = '\0'; + } + } + return name; + } + } + return NULL; +} + +char *devname(dev_t dev, mode_t type) +{ + char *s; + + if ((s = __fast_devname(dev, type)) != NULL) + return s; + return "??"; +}