From 19727734d3af7b3b80733efeaa2a8d60d2015d99 Mon Sep 17 00:00:00 2001 From: ZeroCool32 Date: Sun, 6 Nov 2022 09:14:15 +0200 Subject: [PATCH 1/3] Add uname syscall --- usr/include/sys/utsname.h | 15 +++++++ usr/src/cmd/Makefile | 1 + usr/src/cmd/uname.c | 66 +++++++++++++++++++++++++++++ usr/src/libc/Makefile | 3 ++ usr/src/libc/gen/getopt.c | 75 +++++++++++++++++++++++++++++++++ usr/src/libc/gen/strchr.c | 28 ++++++++++++ usr/src/libc/sys/armv6k/uname.s | 25 +++++++++++ usr/sys/h/utsname.h | 13 ++++++ 8 files changed, 226 insertions(+) create mode 100644 usr/include/sys/utsname.h create mode 100644 usr/src/cmd/uname.c create mode 100644 usr/src/libc/gen/getopt.c create mode 100644 usr/src/libc/gen/strchr.c create mode 100644 usr/src/libc/sys/armv6k/uname.s create mode 100644 usr/sys/h/utsname.h diff --git a/usr/include/sys/utsname.h b/usr/include/sys/utsname.h new file mode 100644 index 0000000..1b009f9 --- /dev/null +++ b/usr/include/sys/utsname.h @@ -0,0 +1,15 @@ +/* @(#)utsname.h 1.1 */ + +#ifndef __V7_SYS_UTSNAME_H +#define __V7_SYS_UTSNAME_H + +struct utsname { + char sysname[9]; + char nodename[9]; + char release[9]; + char version[9]; + char machine[9]; +}; +extern struct utsname utsname; + +#endif /* __V7_SYS_UTSNAME_H */ diff --git a/usr/src/cmd/Makefile b/usr/src/cmd/Makefile index dda76a6..b459a7a 100644 --- a/usr/src/cmd/Makefile +++ b/usr/src/cmd/Makefile @@ -146,6 +146,7 @@ TARGETS = ac \ tsort \ tty \ umount \ + uname \ uniq \ units \ update \ diff --git a/usr/src/cmd/uname.c b/usr/src/cmd/uname.c new file mode 100644 index 0000000..41e4b64 --- /dev/null +++ b/usr/src/cmd/uname.c @@ -0,0 +1,66 @@ +/* uname - print operating system information */ +/* Taken from the System V sources here: https://github.com/calmsacibis995/sysv-src */ + +/* @(#)uname.c 1.2 */ +#include +#include + +struct utsname unstr, *un; + +int main(int argc, char **argv) +{ + register i; + int sflg=1, nflg=0, rflg=0, vflg=0, mflg=0, errflg=0; + int optlet; + + un = &unstr; + uname(un); + + while((optlet=getopt(argc, argv, "asnrvm")) != EOF) switch(optlet) { + case 'a': + sflg++; nflg++; rflg++; vflg++; mflg++; + break; + case 's': + sflg++; + break; + case 'n': + nflg++; + break; + case 'r': + rflg++; + break; + case 'v': + vflg++; + break; + case 'm': + mflg++; + break; + case '?': + errflg++; + } + if(errflg) { + fprintf(stderr, "usage: uname [-snrvma]\n"); + exit(1); + } + if(nflg | rflg | vflg | mflg) sflg--; + if(sflg) + fprintf(stdout, "%.9s", un->sysname); + if(nflg) { + if(sflg) putchar(' '); + fprintf(stdout, "%.9s", un->nodename); + } + if(rflg) { + if(sflg | nflg) putchar(' '); + fprintf(stdout, "%.9s", un->release); + } + if(vflg) { + if(sflg | nflg | rflg) putchar(' '); + fprintf(stdout, "%.9s", un->version); + } + if(mflg) { + if(sflg | nflg | rflg | vflg) putchar(' '); + fprintf(stdout, "%.9s", un->machine); + } + putchar('\n'); + exit(0); +} diff --git a/usr/src/libc/Makefile b/usr/src/libc/Makefile index 1d5b403..035bb04 100644 --- a/usr/src/libc/Makefile +++ b/usr/src/libc/Makefile @@ -47,6 +47,7 @@ GEN_OBJECTS = cuexit.o \ execvp.o \ getenv.o \ getlogin.o \ + getopt.o \ perror.o \ sleep.o \ timezone.o \ @@ -79,6 +80,7 @@ GEN_OBJECTS = cuexit.o \ rindex.o \ strcat.o \ strncat.o \ + strchr.o \ strcmp.o \ strncmp.o \ strcpy.o \ @@ -133,6 +135,7 @@ SYS_OBJECTS = access.o \ times.o \ umask.o \ umount.o \ + uname.o \ unlink.o \ utime.o \ wait.o \ diff --git a/usr/src/libc/gen/getopt.c b/usr/src/libc/gen/getopt.c new file mode 100644 index 0000000..0f2fbf8 --- /dev/null +++ b/usr/src/libc/gen/getopt.c @@ -0,0 +1,75 @@ +/* Taken from the Ultrix-11 sources, here: https://github.com/calmsacibis995/ultrix11-src/blob/main/src/libc/sysV/getopt.c */ + +/********************************************************************** + * Copyright (c) Digital Equipment Corporation 1984, 1985, 1986. * + * All Rights Reserved. * + * Reference "/usr/src/COPYRIGHT" for applicable restrictions. * + **********************************************************************/ + +/* SCCSID: @(#)getopt.c 3.0 4/22/86 */ +/* (System 5) 1.5 */ +/* 3.0 SID # 1.2 */ +/*LINTLIBRARY*/ +#define NULL 0 +#define EOF (-1) +#define ERR(s, c) if(opterr){\ + extern int strlen(), write();\ + char errbuf[2];\ + errbuf[0] = c; errbuf[1] = '\n';\ + (void) write(2, argv[0], (unsigned)strlen(argv[0]));\ + (void) write(2, s, (unsigned)strlen(s));\ + (void) write(2, errbuf, 2);} + +extern int strcmp(); +extern char *strchr(); + +int opterr = 1; +int optind = 1; +int optopt; +char *optarg; + +int +getopt(argc, argv, opts) +int argc; +char **argv, *opts; +{ + static int sp = 1; + register int c; + register char *cp; + + if(sp == 1) + if(optind >= argc || + argv[optind][0] != '-' || argv[optind][1] == '\0') + return(EOF); + else if(strcmp(argv[optind], "--") == NULL) { + optind++; + return(EOF); + } + optopt = c = argv[optind][sp]; + if(c == ':' || (cp=strchr(opts, c)) == NULL) { + ERR(": illegal option -- ", c); + if(argv[optind][++sp] == '\0') { + optind++; + sp = 1; + } + return('?'); + } + if(*++cp == ':') { + if(argv[optind][sp+1] != '\0') + optarg = &argv[optind++][sp+1]; + else if(++optind >= argc) { + ERR(": option requires an argument -- ", c); + sp = 1; + return('?'); + } else + optarg = argv[optind++]; + sp = 1; + } else { + if(argv[optind][++sp] == '\0') { + sp = 1; + optind++; + } + optarg = NULL; + } + return(c); +} diff --git a/usr/src/libc/gen/strchr.c b/usr/src/libc/gen/strchr.c new file mode 100644 index 0000000..5a3ff21 --- /dev/null +++ b/usr/src/libc/gen/strchr.c @@ -0,0 +1,28 @@ +/* Taken from the Ultrix-11 sources, here: https://github.com/calmsacibis995/ultrix11-src/blob/main/src/libc/sysV/strchr.c */ +/********************************************************************** + * Copyright (c) Digital Equipment Corporation 1984, 1985, 1986. * + * All Rights Reserved. * + * Reference "/usr/src/COPYRIGHT" for applicable restrictions. * + **********************************************************************/ + +/* SCCSID: @(#)strchr.c 3.0 4/22/86 */ +/* (System 5) 1.2 */ +/* 3.0 SID # 1.2 */ +/*LINTLIBRARY*/ +/* + * Return the ptr in sp at which the character c appears; + * NULL if not found + */ + +#define NULL 0 + +char * +strchr(sp, c) +register char *sp, c; +{ + do { + if(*sp == c) + return(sp); + } while(*sp++); + return(NULL); +} diff --git a/usr/src/libc/sys/armv6k/uname.s b/usr/src/libc/sys/armv6k/uname.s new file mode 100644 index 0000000..4dee868 --- /dev/null +++ b/usr/src/libc/sys/armv6k/uname.s @@ -0,0 +1,25 @@ +@ C library -- uname +@ uname(unixname); +@ unixname[0], ...unixname[7] contain the unixname + +.syntax unified +.arm +.section text + +.global uname +.extern errno + +.type uname,%function +uname: + push {fp, lr} @ setup the stack + add fp, sp, #0 @ adjust the frame pointer + push {r0} @ fname + mov lr, sp @ pointer to indirect arguments stored in lr + swi #57 @ uname syscall number + bcc 1f @ syscalls return carry-clear for success and carry-set for error + ldr r2, =errno @ load up the address of errno + str r0, [r2] @ stash the returned errno in the error case + mov r0, #-1 @ syscalls return -1 for error +1: sub sp, fp, #0 @ restore our stack pointer to point to our stack frame + pop {fp, pc} @ restore caller frame and return +.size uname, . - uname diff --git a/usr/sys/h/utsname.h b/usr/sys/h/utsname.h new file mode 100644 index 0000000..7429ac1 --- /dev/null +++ b/usr/sys/h/utsname.h @@ -0,0 +1,13 @@ +/* @(#)utsname.h 1.1 */ + +#ifndef __V7_SYS_UTSNAME_H +#define __V7_SYS_UTSNAME_H + +struct utsname { + char sysname[9]; + char nodename[9]; + char release[9]; + char version[9]; + char machine[9]; +}; +extern struct utsname utsname; \ No newline at end of file From fd00cb45399fb01ce50625cb00cfd92fda42538a Mon Sep 17 00:00:00 2001 From: Stefanos Stefanidis <46319359+calmsacibis995@users.noreply.github.com> Date: Fri, 9 Dec 2022 16:54:36 +0200 Subject: [PATCH 2/3] Add 'utsname.h' to header list. --- usr/include/sys/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/usr/include/sys/Makefile b/usr/include/sys/Makefile index 9ce7785..35f7e25 100644 --- a/usr/include/sys/Makefile +++ b/usr/include/sys/Makefile @@ -15,7 +15,8 @@ HFILES = acct.h \ timeb.h \ tty.h \ types.h \ - user.h + user.h \ + utsname.h \ all: $(HFILES) From 3f3373b39cd1d5e42632730e45e3449db47f96cd Mon Sep 17 00:00:00 2001 From: Stefanos Stefanidis <46319359+calmsacibis995@users.noreply.github.com> Date: Fri, 9 Dec 2022 16:55:06 +0200 Subject: [PATCH 3/3] Fix a typo. --- usr/include/sys/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usr/include/sys/Makefile b/usr/include/sys/Makefile index 35f7e25..663bab6 100644 --- a/usr/include/sys/Makefile +++ b/usr/include/sys/Makefile @@ -16,7 +16,7 @@ HFILES = acct.h \ tty.h \ types.h \ user.h \ - utsname.h \ + utsname.h all: $(HFILES)