diff --git a/elks/arch/i86/boot/setup.S b/elks/arch/i86/boot/setup.S index ca86a563c..f79bbc1cb 100644 --- a/elks/arch/i86/boot/setup.S +++ b/elks/arch/i86/boot/setup.S @@ -79,6 +79,7 @@ */ #include +#include #include // Relocating loader debug option @@ -241,9 +242,9 @@ start_os: // Check for correct a.out header #ifdef CONFIG_ROMFS_FS - movw $0x0600,root_dev // ROMFS is device 6,0 + movw $DEV_ROM,root_dev // ROMFS #else - movw $0x0380,root_dev // floppy is device 3,128 + movw $DEV_FD0,root_dev // floppy 0 #endif mov $CONFIG_ROM_KERNEL_CODE,%ax // DS = ROM a.out image mov %ax,%ds diff --git a/elks/fs/msdos/inode.c b/elks/fs/msdos/inode.c index acd641424..ff10affcd 100644 --- a/elks/fs/msdos/inode.c +++ b/elks/fs/msdos/inode.c @@ -12,55 +12,56 @@ #include #include #include +#include #include #ifdef CONFIG_FS_DEV /* FAT device table, increase DEVDIR_SIZE and DEVINO_BASE to add entries*/ struct msdos_devdir_entry devnods[DEVDIR_SIZE] = { - { "hda", S_IFBLK | 0644, MKDEV(3, 0) }, - { "hda1", S_IFBLK | 0644, MKDEV(3, 1) }, - { "hda2", S_IFBLK | 0644, MKDEV(3, 2) }, - { "hda3", S_IFBLK | 0644, MKDEV(3, 3) }, - { "hda4", S_IFBLK | 0644, MKDEV(3, 4) }, - { "hdb", S_IFBLK | 0644, MKDEV(3, 32)}, - { "hdb1", S_IFBLK | 0644, MKDEV(3, 33)}, - { "hdb2", S_IFBLK | 0644, MKDEV(3, 34)}, - { "hdb3", S_IFBLK | 0644, MKDEV(3, 35)}, - { "hdb4", S_IFBLK | 0644, MKDEV(3, 36)}, - { "hdc", S_IFBLK | 0644, MKDEV(3, 64)}, - { "hdc1", S_IFBLK | 0644, MKDEV(3, 65)}, - { "hdc2", S_IFBLK | 0644, MKDEV(3, 66)}, - { "hdc3", S_IFBLK | 0644, MKDEV(3, 67)}, - { "hdb4", S_IFBLK | 0644, MKDEV(3, 68)}, - { "hdd", S_IFBLK | 0644, MKDEV(3, 96)}, - { "hdd1", S_IFBLK | 0644, MKDEV(3, 97)}, - { "hdd2", S_IFBLK | 0644, MKDEV(3, 98)}, - { "hdd3", S_IFBLK | 0644, MKDEV(3, 99)}, - { "hdd4", S_IFBLK | 0644, MKDEV(3,100)}, - { "fd0", S_IFBLK | 0644, MKDEV(3,128)}, - { "fd1", S_IFBLK | 0644, MKDEV(3,160)}, - { "fd2", S_IFBLK | 0644, MKDEV(3,192)}, - { "fd3", S_IFBLK | 0644, MKDEV(3,224)}, - { "rd0", S_IFBLK | 0644, MKDEV(1, 0) }, - { "ssd", S_IFBLK | 0644, MKDEV(2, 0) }, - { "kmem", S_IFCHR | 0644, MKDEV(1, 2) }, - { "null", S_IFCHR | 0644, MKDEV(1, 3) }, - { "zero", S_IFCHR | 0644, MKDEV(1, 5) }, - { "tty1", S_IFCHR | 0644, MKDEV(4, 0) }, - { "tty2", S_IFCHR | 0644, MKDEV(4, 1) }, - { "tty3", S_IFCHR | 0644, MKDEV(4, 2) }, - { "ttyS0", S_IFCHR | 0644, MKDEV(4, 64)}, - { "ttyS1", S_IFCHR | 0644, MKDEV(4, 65)}, - { "console",S_IFCHR | 0600, MKDEV(4, 254)}, - { "tty", S_IFCHR | 0666, MKDEV(4, 255)}, - { "ttyp0", S_IFCHR | 0644, MKDEV(4, 8) }, - { "ptyp0", S_IFCHR | 0644, MKDEV(2, 8) }, - { "ttyp1", S_IFCHR | 0644, MKDEV(4, 9) }, - { "ptyp1", S_IFCHR | 0644, MKDEV(2, 9) }, - { "tcpdev", S_IFCHR | 0644, MKDEV(8, 0) }, - { "ne0", S_IFCHR | 0644, MKDEV(9, 0) }, - { "wd0", S_IFCHR | 0644, MKDEV(9, 1) }, - { "3c0", S_IFCHR | 0644, MKDEV(9, 2) }, + { "hda", S_IFBLK | 0644, DEV_HDA }, + { "hda1", S_IFBLK | 0644, DEV_HDA+1 }, + { "hda2", S_IFBLK | 0644, DEV_HDA+2 }, + { "hda3", S_IFBLK | 0644, DEV_HDA+3 }, + { "hda4", S_IFBLK | 0644, DEV_HDA+4 }, + { "hdb", S_IFBLK | 0644, DEV_HDB }, + { "hdb1", S_IFBLK | 0644, DEV_HDB+1 }, + { "hdb2", S_IFBLK | 0644, DEV_HDB+2 }, + { "hdb3", S_IFBLK | 0644, DEV_HDB+3 }, + { "hdb4", S_IFBLK | 0644, DEV_HDB+4 }, + { "hdc", S_IFBLK | 0644, DEV_HDC }, + { "hdc1", S_IFBLK | 0644, DEV_HDC+1 }, + { "hdc2", S_IFBLK | 0644, DEV_HDC+2 }, + { "hdc3", S_IFBLK | 0644, DEV_HDC+3 }, + { "hdc4", S_IFBLK | 0644, DEV_HDC+4 }, + { "hdd", S_IFBLK | 0644, DEV_HDD }, + { "hdd1", S_IFBLK | 0644, DEV_HDD+1 }, + { "hdd2", S_IFBLK | 0644, DEV_HDD+2 }, + { "hdd3", S_IFBLK | 0644, DEV_HDD+3 }, + { "hdd4", S_IFBLK | 0644, DEV_HDD+4 }, + { "fd0", S_IFBLK | 0644, DEV_FD0 }, + { "fd1", S_IFBLK | 0644, DEV_FD1 }, + { "fd2", S_IFBLK | 0644, DEV_FD2 }, + { "fd3", S_IFBLK | 0644, DEV_FD3 }, + { "rd0", S_IFBLK | 0644, MKDEV(RAM_MAJOR, 0) }, + { "ssd", S_IFBLK | 0644, MKDEV(SSD_MAJOR, 0) }, + { "kmem", S_IFCHR | 0644, MKDEV(MEM_MAJOR, 2) }, + { "null", S_IFCHR | 0644, MKDEV(MEM_MAJOR, 3) }, + { "zero", S_IFCHR | 0644, MKDEV(MEM_MAJOR, 5) }, + { "tty1", S_IFCHR | 0644, MKDEV(TTY_MAJOR, 0) }, + { "tty2", S_IFCHR | 0644, MKDEV(TTY_MAJOR, 1) }, + { "tty3", S_IFCHR | 0644, MKDEV(TTY_MAJOR, 2) }, + { "ttyS0", S_IFCHR | 0644, MKDEV(TTY_MAJOR, 64) }, + { "ttyS1", S_IFCHR | 0644, MKDEV(TTY_MAJOR, 65) }, + { "console",S_IFCHR | 0600, MKDEV(TTY_MAJOR, 254) }, + { "tty", S_IFCHR | 0666, MKDEV(TTY_MAJOR, 255) }, + { "ttyp0", S_IFCHR | 0644, MKDEV(TTY_MAJOR, 8) }, + { "ttyp1", S_IFCHR | 0644, MKDEV(TTY_MAJOR, 9) }, + { "ptyp0", S_IFCHR | 0644, MKDEV(PTY_MASTER_MAJOR, 8) }, + { "ptyp1", S_IFCHR | 0644, MKDEV(PTY_MASTER_MAJOR, 9) }, + { "tcpdev", S_IFCHR | 0644, MKDEV(TCPDEV_MAJOR, 0) }, + { "ne0", S_IFCHR | 0644, MKDEV(ETH_MAJOR, 0) }, + { "wd0", S_IFCHR | 0644, MKDEV(ETH_MAJOR, 1) }, + { "3c0", S_IFCHR | 0644, MKDEV(ETH_MAJOR, 2) }, }; #endif diff --git a/elks/fs/super.c b/elks/fs/super.c index 896e9a403..352dfac2a 100644 --- a/elks/fs/super.c +++ b/elks/fs/super.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -491,7 +492,7 @@ void mount_root(void) } while (*(++fs_type) && !retval); #ifdef CONFIG_BLK_DEV_BIOS - if (ROOT_DEV == 0x0380) { + if (ROOT_DEV == DEV_FD0) { if (!filp->f_op->release) printk("Release not defined\n"); close_filp(d_inode, filp); diff --git a/elks/include/linuxmt/devnum.h b/elks/include/linuxmt/devnum.h new file mode 100644 index 000000000..e4fa07f04 --- /dev/null +++ b/elks/include/linuxmt/devnum.h @@ -0,0 +1,32 @@ +#ifndef __LINUXMT_DEVNUM_H +#define __LINUXMT_DEVNUM_H + +#include +#include + +/* + * Device numbers for block and character devices + * + * NOTE: If a block device MINOR_SHIFT changes, must be updated here also. + */ + +/* block devices */ +#define DEV_HDA MKDEV(BIOSHD_MAJOR, 0) +#define DEV_HDB MKDEV(BIOSHD_MAJOR, 32) +#define DEV_HDC MKDEV(BIOSHD_MAJOR, 64) +#define DEV_HDD MKDEV(BIOSHD_MAJOR, 96) +#define DEV_FD0 MKDEV(BIOSHD_MAJOR, 128) +#define DEV_FD1 MKDEV(BIOSHD_MAJOR, 160) +#define DEV_FD2 MKDEV(BIOSHD_MAJOR, 192) +#define DEV_FD3 MKDEV(BIOSHD_MAJOR, 224) +#define DEV_ROM MKDEV(ROMFLASH_MAJOR, 0) + +/* char devices */ +#define DEV_TTY1 MKDEV(TTY_MAJOR, 0) +#define DEV_TTY2 MKDEV(TTY_MAJOR, 1) +#define DEV_TTY3 MKDEV(TTY_MAJOR, 2) +#define DEV_TTYS0 MKDEV(TTY_MAJOR, 64) +#define DEV_TTYS1 MKDEV(TTY_MAJOR, 65) + + +#endif /* !__LINUXMT_DEVNUM_H */ diff --git a/elks/include/linuxmt/dirent.h b/elks/include/linuxmt/dirent.h index 5e9f7cf74..a115c36a4 100644 --- a/elks/include/linuxmt/dirent.h +++ b/elks/include/linuxmt/dirent.h @@ -2,8 +2,7 @@ #define __LINUXMT_DIRENT_H #include - -#define MAXNAMLEN 26 /* 14 for MINIX, 26 for FAT */ +#include struct dirent { u_ino_t d_ino; diff --git a/elks/include/linuxmt/fs.h b/elks/include/linuxmt/fs.h index aab6f44a4..a8c379c97 100644 --- a/elks/include/linuxmt/fs.h +++ b/elks/include/linuxmt/fs.h @@ -40,13 +40,6 @@ #endif /* __KERNEL__ */ -/* - * Now the file code is no longer dependent on bitmaps in unsigned - * longs, but uses the new fd_set structure.. - */ - -#define NR_OPEN 20 - #define BLOCK_SIZE 1024 #define BLOCK_SIZE_BITS 10 diff --git a/elks/include/linuxmt/kdev_t.h b/elks/include/linuxmt/kdev_t.h index 183c5a0b2..07b46a8a2 100644 --- a/elks/include/linuxmt/kdev_t.h +++ b/elks/include/linuxmt/kdev_t.h @@ -4,14 +4,20 @@ /* Some programs want their definitions of MAJOR and MINOR and MKDEV * from the kernel sources. Here we use unsigned short instead of __u16. */ +#ifdef __ASSEMBLER__ +#define _CAST_U16 +#else +#define _CAST_U16 (unsigned short) +#endif + #define MINORBITS 8 #define MINORMASK ((1 << MINORBITS) - 1) -#define MAJOR(dev) ((unsigned short) ((dev) >> MINORBITS)) -#define MINOR(dev) ((unsigned short) ((dev) & MINORMASK)) -#define MKDEV(major,minor) ((unsigned short) (((major) << MINORBITS) | (minor))) +#define MAJOR(dev) (_CAST_U16 ((dev) >> MINORBITS)) +#define MINOR(dev) (_CAST_U16 ((dev) & MINORMASK)) +#define MKDEV(major,minor) (_CAST_U16 (((major) << MINORBITS) | (minor))) #define NODEV MKDEV(0,0) -#ifdef __KERNEL__ +#if defined(__KERNEL__) && !defined(__ASSEMBLER__) #include typedef __u16 kdev_t; diff --git a/elks/include/linuxmt/limits.h b/elks/include/linuxmt/limits.h index e9e0aa6b2..f5b95ae67 100644 --- a/elks/include/linuxmt/limits.h +++ b/elks/include/linuxmt/limits.h @@ -12,7 +12,7 @@ #ifdef CONFIG_ASYNCIO #define KSTACK_BYTES 700 /* Size of kernel stacks w/async I/O */ #else -#define KSTACK_BYTES 640 /* Size of kernel stacks w/sync I/O */ +#define KSTACK_BYTES 640 /* Size of kernel stacks w/o async I/O */ #endif #endif @@ -34,10 +34,13 @@ /* filesystem */ #define NR_INODE 96 /* this should be bigger than NR_FILE */ #define NR_FILE 64 /* this can well be larger on a larger system */ -#define NR_SUPER 6 /* max mounts */ +#define NR_OPEN 20 /* Max open files per process */ +#define NR_SUPER 6 /* Max mounts */ #define PIPE_BUFSIZ 80 /* doesn't have to be power of two */ +#define MAXNAMLEN 26 /* Max filename, 14 for MINIX, 26 for FAT (not tunable) */ + #define NR_ALARMS 5 /* Max number of simultaneous alarms system-wide */ #define MAX_PACKET_ETH 1536 /* Max packet size, 6 blocks of 256 bytes */ diff --git a/elks/init/main.c b/elks/init/main.c index 274027377..8fbda79af 100644 --- a/elks/init/main.c +++ b/elks/init/main.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -252,17 +253,18 @@ static struct dev_name_struct { const char *name; int num; } devices[] = { - /* root_dev_name needs first 5 in order*/ - { "hda", 0x0300 }, - { "hdb", 0x0320 }, - { "hdc", 0x0340 }, - { "hdd", 0x0360 }, - { "fd0", 0x0380 }, - { "fd1", 0x03a0 }, - { "ttyS", 0x0440 }, - { "tty1", 0x0400 }, - { "tty2", 0x0401 }, - { "tty3", 0x0402 }, + /* the 4 partitionable drives must be first */ + { "hda", DEV_HDA }, + { "hdb", DEV_HDB }, + { "hdc", DEV_HDC }, + { "hdd", DEV_HDD }, + { "fd0", DEV_FD0 }, + { "fd1", DEV_FD1 }, + { "ttyS0", DEV_TTYS0 }, + { "ttyS1", DEV_TTYS1 }, + { "tty1", DEV_TTY1 }, + { "tty2", DEV_TTY2 }, + { "tty3", DEV_TTY3 }, { NULL, 0 } }; @@ -282,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; id_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 = devname(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 = devname(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/mount.c b/elkscmd/sys_utils/mount.c index 742ef65f9..4df212e2f 100644 --- a/elkscmd/sys_utils/mount.c +++ b/elkscmd/sys_utils/mount.c @@ -13,6 +13,7 @@ #include #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 +#include #define LINEARADDRESS(off, seg) ((off_t) (((off_t)seg << 4) + off)) @@ -69,36 +70,32 @@ void process_name(int fd, unsigned int off, unsigned int seg) } } - -char *devname(unsigned int minor) +/* fast cached version of devname() */ +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 "?"; @@ -115,7 +112,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) diff --git a/libc/include/limits.h b/libc/include/limits.h index 49d6e4d11..e8e2d6274 100644 --- a/libc/include/limits.h +++ b/libc/include/limits.h @@ -1,18 +1,18 @@ #ifndef __LIMITS_H #define __LIMITS_H +#include +#include __SYSINC__(limits.h) + /* Maximum number of bytes in a pathname, including the terminating null. */ -#define PATH_MAX 128 +#define PATH_MAX 128 /* Maximum number of bytes in a filename, not including terminating null. */ -/* copied from MAXNAMLEN in linuxmt/dirent.h */ -#define NAME_MAX 26 +#define NAME_MAX MAXNAMLEN -/* copied from PIPE_BUFSIZ in linuxmt/config.h */ -#define PIPE_BUF 80 +#define PIPE_BUF PIPE_BUFSIZ -/* copied from NR_OPEN in linuxmt/fs.h */ -#define OPEN_MAX 20 +#define OPEN_MAX NR_OPEN #ifndef _GCC_NEXT_LIMITS_H #include_next 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/include/stdlib.h b/libc/include/stdlib.h index 72a739e07..e76f34006 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -1,10 +1,9 @@ -/* stdlib.h */ -#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..9a51624fa --- /dev/null +++ b/libc/misc/devname.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +#include __SYSINC__(devnum.h) +/* + * 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; + 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 *__fast_devname(dev_t dev, mode_t type) +{ + int i; + unsigned mask; + + for (i = 0; i < ARRAYLEN(devices); i++) { + mask = (i < 4)? 0xfff8: 0xffff; + if (devices[i].type == type && devices[i].num == (dev & mask)) { + strcpy(&path[NAMEOFF], devices[i].name); + if (i < 4) { + if (dev & 0x07) { + path[NAMEOFF+3] = '0' + (dev & 7); + path[NAMEOFF+4] = '\0'; + } + } + return path; + } + } + return NULL; +} +#endif + +char *devname(dev_t dev, mode_t type) +{ +#if USE_FASTVERSION + char *s = __fast_devname(dev, type); + if (s) + return s; +#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; +} diff --git a/libc/termios/isatty.c b/libc/termios/isatty.c index 5afaf5089..5a8bdd77e 100644 --- a/libc/termios/isatty.c +++ b/libc/termios/isatty.c @@ -10,8 +10,7 @@ isatty(int fd) rv = (ioctl(fd, TCGETS, &term) == 0); - if(rv == 0 - && errno == ENOSYS) + if(rv == 0 && errno == ENOSYS) rv = (fd < 3); errno = err; diff --git a/libc/termios/ttyname.c b/libc/termios/ttyname.c index 225663064..4ad175641 100644 --- a/libc/termios/ttyname.c +++ b/libc/termios/ttyname.c @@ -5,45 +5,36 @@ #include #include -char * ttyname (int fd) +char *ttyname(int fd) { - static char dev[] = _PATH_DEV; - struct stat st, dst; - DIR *fp; - struct dirent *d; - static char name[MAXNAMLEN]; - int noerr = errno; + DIR *dp; + struct dirent *d; + struct stat src, dst; + static char path[MAXNAMLEN+6] = _PATH_DEVSL; /* /dev/ */ +#define NAMEOFF (sizeof(_PATH_DEVSL) - 1) - if (fstat(fd, &st) < 0) - return 0; - if (!isatty(fd)) - { - errno = ENOTTY; - return 0; - } + if (fstat(fd, &src) < 0) + return NULL; + if (!isatty(fd)) { + errno = ENOTTY; + return NULL; + } - fp = opendir(dev); - if (fp == 0) - return 0; - strcpy(name, dev); - strcat(name, "/"); + dp = opendir(_PATH_DEV); + if (!dp) + return NULL; - while ((d = readdir(fp)) != 0) - { - if (strlen(d->d_name) > sizeof(name) - sizeof(dev) - 1) - continue; - if (d->d_name[0] == '.') - continue; - strcpy(name + sizeof(dev), d->d_name); - if (stat(name, &dst) == 0 - && st.st_dev == dst.st_dev && st.st_ino == dst.st_ino) - { - closedir(fp); - errno = noerr; - return name; - } - } - closedir(fp); - errno = noerr; - return 0; + while ((d = readdir(dp)) != 0) { + if (d->d_name[0] == '.') + continue; + strcpy(&path[NAMEOFF], d->d_name); + if (stat(path, &dst) == 0) { + if (src.st_dev == dst.st_dev && src.st_ino == dst.st_ino) { + closedir(dp); + return path; + } + } + } + closedir(dp); + return NULL; }