diff --git a/elkscmd/sys_utils/makeboot.c b/elkscmd/sys_utils/makeboot.c index 2694dcc2e..2ac79f840 100644 --- a/elkscmd/sys_utils/makeboot.c +++ b/elkscmd/sys_utils/makeboot.c @@ -77,39 +77,6 @@ char bootblock[1024]; /* 1024 for MINIX, 512 for FAT */ char *rootdevice; char *fsname[3] = { "Unknown", "Minix", "FAT" }; -/* return /dev name of device*/ -char *dev_name(dev_t dev) -{ - DIR *dp; - struct dirent *d; - struct stat st; - static char devdir[] = "/dev"; - static char name[16]; - - dp = opendir(devdir); - if (dp == 0) { - perror(devdir); - return NULL; - } - strcpy(name, devdir); - strcat(name, "/"); - - while ((d = readdir(dp)) != NULL) { - if (d->d_name[0] == '.') - continue; - strcpy(name + sizeof(devdir), d->d_name); - if (stat(name, &st) == 0) { - if (S_ISBLK(st.st_mode) && st.st_rdev == dev) { - closedir(dp); - return name; - } - } - } - closedir(dp); - fprintf(stderr, "Can't find device: 0x%x\n", dev); - return NULL; -} - /* determine and return filesystem type*/ int get_fstype(int fd) { @@ -398,7 +365,9 @@ int main(int ac, char **av) } rootdev = sbuf.st_dev; - rootdevice = dev_name(rootdev); + rootdevice = devname(rootdev, S_IFBLK); + if (!rootdevice) + fatalmsg("Can't find device: 0x%x\n", rootdev); if (opt_writebb == 1) { get_bootblock(bootfile); @@ -431,7 +400,7 @@ int main(int ac, char **av) if (opt_writembr) { int ffd; - char *rawtargetdevice = dev_name(targetdev & ~BIOS_MINOR_MASK); + char *rawtargetdevice = devname(targetdev & ~BIOS_MINOR_MASK, S_IFBLK); if (!rawtargetdevice) fatalmsg("Can't find raw target device\n"); ffd = open(rawtargetdevice, O_RDWR); diff --git a/elkscmd/sys_utils/ps.c b/elkscmd/sys_utils/ps.c index 07a607b74..decbcfdb5 100644 --- a/elkscmd/sys_utils/ps.c +++ b/elkscmd/sys_utils/ps.c @@ -30,6 +30,7 @@ #include #include #include +#include #define LINEARADDRESS(off, seg) ((off_t) (((off_t)seg << 4) + off)) @@ -69,7 +70,6 @@ void process_name(int fd, unsigned int off, unsigned int seg) } } - char *dev_name(unsigned int minor) { struct dirent *d; @@ -77,28 +77,24 @@ char *dev_name(unsigned int minor) static dev_t prevdev = -1; static DIR *fp = NULL; struct stat st; - static char dev[] = "/dev"; - static char name[MAXNAMLEN+1]; + static char path[MAXNAMLEN+6] = _PATH_DEVSL; /* /dev/ */ +#define NAMEOFF (sizeof(_PATH_DEVSL) - 1) - if (prevdev == ttydev) return name+8; + if (prevdev == ttydev) return path+NAMEOFF+3; if (!fp) { - if (!(fp = opendir(dev))) + if (!(fp = opendir(_PATH_DEV))) return "??"; } else rewinddir(fp); - strcpy(name, dev); - strcat(name, "/"); while ((d = readdir(fp)) != 0) { - if (strlen(d->d_name) > sizeof(name) - sizeof(dev) - 1) - continue; if (d->d_name[0] == '.') continue; if (strncmp(d->d_name, "tty", 3)) continue; - strcpy(name + sizeof(dev), d->d_name); - if (!stat(name, &st) && st.st_rdev == ttydev) { + strcpy(&path[NAMEOFF], d->d_name); + if (!stat(path, &st) && st.st_rdev == ttydev) { prevdev = ttydev; - return name+8; + return path+NAMEOFF+3; } } return "?"; diff --git a/libc/include/paths.h b/libc/include/paths.h index 668d919ee..4d6415c3b 100644 --- a/libc/include/paths.h +++ b/libc/include/paths.h @@ -1,36 +1,34 @@ -/* paths.h */ - #ifndef ___PATHS_H -#define ___PATHS_H - - -#define _PATH_GETTY "/bin/getty" -#define _PATH_LOGIN "/bin/login" -#define _PATH_BSHELL "/bin/sh" -#define _PATH_DEFPATH "/bin:." -#define _PATH_DEV "/dev" -#define _PATH_CONSOLE "/dev/console" -#define _PATH_TTY "/dev/tty" -#define _PATH_DEVNULL "/dev/null" -#define _PATH_GROUP "/etc/group" -#define _PATH_HOSTNAME "/etc/hostname" -#define _PATH_INITLVL "/etc/initlvl" -#define _PATH_INITTAB "/etc/inittab" -#define _PATH_ISSUE "/etc/issue" -#define _PATH_MOTD "/etc/motd" -#define _PATH_PASSWD "/etc/passwd" -#define _PATH_ERRSTRING "/etc/perror" -#define _PATH_TERMCAP "/etc/termcap" -#define _PATH_LOCALE "/lib/locale" -#define _PATH_MANPAGES "/lib" -#define _PATH_TMP "/tmp" -#define _PATH_UTMP "/var/utmp" -#define _PATH_WTMP "/var/log/wtmp" -#define _PATH_LASTLOG "/var/log/lastlog" -#define _PATH_DOCBASE "/var/www" -#define _PATH_CRONDIR "/var/cron" -#define _PATH_CRONTAB "/var/cron/root" -#define _PATH_CRONLOG "/var/cron/cron.log" +#define ___PATHS_H +/* paths.h */ +#define _PATH_GETTY "/bin/getty" +#define _PATH_LOGIN "/bin/login" +#define _PATH_BSHELL "/bin/sh" +#define _PATH_DEFPATH "/bin:." +#define _PATH_DEV "/dev" +#define _PATH_DEVSL "/dev/" +#define _PATH_CONSOLE "/dev/console" +#define _PATH_TTY "/dev/tty" +#define _PATH_DEVNULL "/dev/null" +#define _PATH_GROUP "/etc/group" +#define _PATH_HOSTNAME "/etc/hostname" +#define _PATH_INITLVL "/etc/initlvl" +#define _PATH_INITTAB "/etc/inittab" +#define _PATH_ISSUE "/etc/issue" +#define _PATH_MOTD "/etc/motd" +#define _PATH_PASSWD "/etc/passwd" +#define _PATH_ERRSTRING "/etc/perror" +#define _PATH_TERMCAP "/etc/termcap" +#define _PATH_LOCALE "/lib/locale" +#define _PATH_MANPAGES "/lib" +#define _PATH_TMP "/tmp" +#define _PATH_UTMP "/var/utmp" +#define _PATH_WTMP "/var/log/wtmp" +#define _PATH_LASTLOG "/var/log/lastlog" +#define _PATH_DOCBASE "/var/www" +#define _PATH_CRONDIR "/var/cron" +#define _PATH_CRONTAB "/var/cron/root" +#define _PATH_CRONLOG "/var/cron/cron.log" #endif /* __PATHS_H */ diff --git a/libc/misc/devname.c b/libc/misc/devname.c index 7a2ffe7fa..3104cd4a0 100644 --- a/libc/misc/devname.c +++ b/libc/misc/devname.c @@ -1,18 +1,22 @@ #include #include #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. + * Convert a block or character device number to /dev path. */ +#define USE_FASTVERSION 0 /* =1 to use compiled-in device numbers for floppy speed */ + +static char path[NAME_MAX+6] = _PATH_DEVSL; /* /dev/ */ + +#define NAMEOFF (sizeof(_PATH_DEVSL) - 1) #define ARRAYLEN(a) (sizeof(a)/sizeof(a[0])) +#if USE_FASTVERSION static struct dev_name_struct { char *name; mode_t type; @@ -34,35 +38,54 @@ static struct dev_name_struct { { "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); + strcpy(&path[NAMEOFF], devices[i].name); if (i < 4) { if (dev & 0x07) { - name[NAMEOFF+3] = '0' + (dev & 7); - name[NAMEOFF+4] = '\0'; + path[NAMEOFF+3] = '0' + (dev & 7); + path[NAMEOFF+4] = '\0'; } } - return name; + return path; } } return NULL; } +#endif char *devname(dev_t dev, mode_t type) { - char *s; - - if ((s = __fast_devname(dev, type)) != NULL) +#if USE_FASTVERSION + char *s = __fast_devname(dev, type); + if (s) return s; - return "??"; +#endif + DIR *dp; + struct dirent *d; + struct stat st; + + dp = opendir(_PATH_DEV); + if (!dp) + return NULL; + + while ((d = readdir(dp)) != NULL) { + if (d->d_name[0] == '.') + continue; + strcpy(&path[NAMEOFF], d->d_name); + if (stat(path, &st) == 0) { + if ((st.st_mode & S_IFMT) == type && st.st_rdev == dev) { + closedir(dp); + return path; + } + } + } + closedir(dp); + return NULL; }