Skip to content

Commit

Permalink
Add libc devname function, convert df and mount to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Sep 10, 2023
1 parent 33e13fb commit 273c641
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 100 deletions.
4 changes: 2 additions & 2 deletions elks/init/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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;
Expand Down
50 changes: 4 additions & 46 deletions elkscmd/disk_utils/df.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
Expand All @@ -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)))
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<sizeof(devices)/sizeof(devices[0])-1; i++) {
if (devices[i].num == (dev & 0xfff0)) {
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;
}
6 changes: 3 additions & 3 deletions elkscmd/sys_utils/makeboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ char *rootdevice;
char *fsname[3] = { "Unknown", "Minix", "FAT" };

/* return /dev name of device*/
char *devname(dev_t dev)
char *dev_name(dev_t dev)
{
DIR *dp;
struct dirent *d;
Expand Down Expand Up @@ -398,7 +398,7 @@ int main(int ac, char **av)
}

rootdev = sbuf.st_dev;
rootdevice = devname(rootdev);
rootdevice = dev_name(rootdev);

if (opt_writebb == 1) {
get_bootblock(bootfile);
Expand Down Expand Up @@ -431,7 +431,7 @@ int main(int ac, char **av)
if (opt_writembr) {
int ffd;

char *rawtargetdevice = devname(targetdev & ~BIOS_MINOR_MASK);
char *rawtargetdevice = dev_name(targetdev & ~BIOS_MINOR_MASK);
if (!rawtargetdevice)
fatalmsg("Can't find raw target device\n");
ffd = open(rawtargetdevice, O_RDWR);
Expand Down
46 changes: 3 additions & 43 deletions elkscmd/sys_utils/mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <linuxmt/fs.h>
#include <linuxmt/limits.h>

Expand All @@ -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<sizeof(devices)/sizeof(devices[0])-1; i++) {
if (devices[i].num == (dev & 0xfff0)) {
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;
}

static int show_mount(dev_t dev)
{
struct statfs statfs;
Expand All @@ -72,11 +32,11 @@ static int show_mount(dev_t dev)

if (statfs.f_type < FST_MSDOS)
printf("%-9s (%5s) blocks %6lu free %6lu mount %s\n",
dev_name(statfs.f_dev), fs_typename[statfs.f_type], statfs.f_blocks,
devname(statfs.f_dev, S_IFBLK), fs_typename[statfs.f_type], statfs.f_blocks,
statfs.f_bfree, statfs.f_mntonname);
else
printf("%-9s (%5s) mount %s\n",
dev_name(statfs.f_dev), fs_typename[statfs.f_type], statfs.f_mntonname);
devname(statfs.f_dev, S_IFBLK), fs_typename[statfs.f_type], statfs.f_mntonname);
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions elkscmd/sys_utils/ps.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void process_name(int fd, unsigned int off, unsigned int seg)
}


char *devname(unsigned int minor)
char *dev_name(unsigned int minor)
{
struct dirent *d;
dev_t ttydev = MKDEV(TTY_MAJOR, minor);
Expand Down Expand Up @@ -115,7 +115,7 @@ char *tty_name(int fd, unsigned int off, unsigned int seg)

if (read(fd, &tty, sizeof(tty)) != sizeof(tty)) return "?";

return devname(tty.minor);
return dev_name(tty.minor);
}

int main(int argc, char **argv)
Expand Down
8 changes: 4 additions & 4 deletions libc/include/stdlib.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* stdlib.h <[email protected]> */
#include <features.h>
#include <sys/types.h>

#ifndef __STDLIB_H
#define __STDLIB_H

/* stdlib.h <[email protected]> */
#include <features.h>
#include <sys/types.h>
#include <malloc.h>

/* Don't overwrite user definitions of NULL */
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions libc/misc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ OBJS = \
atol.o \
basename.o \
crypt.o \
devname.o \
dirname.o \
dtostr.o \
ecvt.o \
Expand Down
68 changes: 68 additions & 0 deletions libc/misc/devname.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.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.
*/

#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 "??";
}

0 comments on commit 273c641

Please sign in to comment.