Skip to content

Commit

Permalink
Have devname use opendir instead of table lookup by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Sep 10, 2023
1 parent 273c641 commit a845387
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 95 deletions.
39 changes: 4 additions & 35 deletions elkscmd/sys_utils/makeboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 8 additions & 12 deletions elkscmd/sys_utils/ps.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <dirent.h>
#include <pwd.h>
#include <getopt.h>
#include <paths.h>

#define LINEARADDRESS(off, seg) ((off_t) (((off_t)seg << 4) + off))

Expand Down Expand Up @@ -69,36 +70,31 @@ void process_name(int fd, unsigned int off, unsigned int seg)
}
}


char *dev_name(unsigned int minor)
{
struct dirent *d;
dev_t ttydev = MKDEV(TTY_MAJOR, 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 "?";
Expand Down
62 changes: 30 additions & 32 deletions libc/include/paths.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
/* paths.h <[email protected]> */

#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 <[email protected]> */

#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 */
55 changes: 39 additions & 16 deletions libc/misc/devname.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <paths.h>
#include <sys/stat.h>
#include <dirent.h>
#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;
Expand All @@ -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;
}

0 comments on commit a845387

Please sign in to comment.