Initial Spring 2016 commit.

This commit is contained in:
Geoffrey Challen
2015-12-23 00:50:04 +00:00
commit cafa9f5690
732 changed files with 92195 additions and 0 deletions

28
userland/Makefile Normal file
View File

@@ -0,0 +1,28 @@
#
# Toplevel userland makefile for OS/161.
#
TOP=..
.include "$(TOP)/mk/os161.config.mk"
# lib must be first.
SUBDIRS=lib .WAIT bin sbin testbin
INCLUDES=\
include include \
include/sys include/sys \
include/test include/test \
include/types include/types
INCLUDELINKS=\
kern/machine include/machine \
$(MACHINE) include/kern/machine
.include "$(TOP)/mk/os161.subdir.mk"
.include "$(TOP)/mk/os161.includes.mk"
includes: hostincludes
hostincludes:
(cd lib/hostcompat && $(MAKE) includes)
.PHONY: includes hostincludes

10
userland/bin/Makefile Normal file
View File

@@ -0,0 +1,10 @@
#
# Makefile for src/bin (sources for programs installed in /bin)
#
TOP=../..
.include "$(TOP)/mk/os161.config.mk"
SUBDIRS=true false sync mkdir rmdir pwd cat cp ln mv rm ls sh tac
.include "$(TOP)/mk/os161.subdir.mk"

12
userland/bin/cat/Makefile Normal file
View File

@@ -0,0 +1,12 @@
# Makefile for cat
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=cat
SRCS=cat.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

120
userland/bin/cat/cat.c Normal file
View File

@@ -0,0 +1,120 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <string.h>
#include <err.h>
/*
* cat - concatenate and print
* Usage: cat [files]
*/
/* Print a file that's already been opened. */
static
void
docat(const char *name, int fd)
{
char buf[1024];
int len, wr, wrtot;
/*
* As long as we get more than zero bytes, we haven't hit EOF.
* Zero means EOF. Less than zero means an error occurred.
* We may read less than we asked for, though, in various cases
* for various reasons.
*/
while ((len = read(fd, buf, sizeof(buf)))>0) {
/*
* Likewise, we may actually write less than we attempted
* to. So loop until we're done.
*/
wrtot = 0;
while (wrtot < len) {
wr = write(STDOUT_FILENO, buf+wrtot, len-wrtot);
if (wr<0) {
err(1, "stdout");
}
wrtot += wr;
}
}
/*
* If we got a read error, print it and exit.
*/
if (len<0) {
err(1, "%s", name);
}
}
/* Print a file by name. */
static
void
cat(const char *file)
{
int fd;
/*
* "-" means print stdin.
*/
if (!strcmp(file, "-")) {
docat("stdin", STDIN_FILENO);
return;
}
/*
* Open the file, print it, and close it.
* Bail out if we can't open it.
*/
fd = open(file, O_RDONLY);
if (fd<0) {
err(1, "%s", file);
}
docat(file, fd);
close(fd);
}
int
main(int argc, char *argv[])
{
if (argc==1) {
/* No args - just do stdin */
docat("stdin", STDIN_FILENO);
}
else {
/* Print all the files specified on the command line. */
int i;
for (i=1; i<argc; i++) {
cat(argv[i]);
}
}
return 0;
}

12
userland/bin/cp/Makefile Normal file
View File

@@ -0,0 +1,12 @@
# Makefile for cp
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=cp
SRCS=cp.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

113
userland/bin/cp/cp.c Normal file
View File

@@ -0,0 +1,113 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <err.h>
/*
* cp - copy a file.
* Usage: cp oldfile newfile
*/
/* Copy one file to another. */
static
void
copy(const char *from, const char *to)
{
int fromfd;
int tofd;
char buf[1024];
int len, wr, wrtot;
/*
* Open the files, and give up if they won't open
*/
fromfd = open(from, O_RDONLY);
if (fromfd<0) {
err(1, "%s", from);
}
tofd = open(to, O_WRONLY|O_CREAT|O_TRUNC);
if (tofd<0) {
err(1, "%s", to);
}
/*
* As long as we get more than zero bytes, we haven't hit EOF.
* Zero means EOF. Less than zero means an error occurred.
* We may read less than we asked for, though, in various cases
* for various reasons.
*/
while ((len = read(fromfd, buf, sizeof(buf)))>0) {
/*
* Likewise, we may actually write less than we attempted
* to. So loop until we're done.
*/
wrtot = 0;
while (wrtot < len) {
wr = write(tofd, buf+wrtot, len-wrtot);
if (wr<0) {
err(1, "%s", to);
}
wrtot += wr;
}
}
/*
* If we got a read error, print it and exit.
*/
if (len<0) {
err(1, "%s", from);
}
if (close(fromfd) < 0) {
err(1, "%s: close", from);
}
if (close(tofd) < 0) {
err(1, "%s: close", to);
}
}
int
main(int argc, char *argv[])
{
/*
* Just do it.
*
* We don't allow the Unix model where you can do
* cp file1 file2 file3 destination-directory
*
* although this would be pretty easy to add.
*/
if (argc!=3) {
errx(1, "Usage: cp OLDFILE NEWFILE");
}
copy(argv[1], argv[2]);
return 0;
}

View File

@@ -0,0 +1,12 @@
# Makefile for false
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=false
SRCS=false.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <stdlib.h>
/*
* false - fail.
*
* "All software sucks. Ok, so maybe /bin/true doesn't. But /bin/false
* sure does - it fails all the time."
*/
int
main(void)
{
/* Just exit with a failure code. */
exit(1);
}

12
userland/bin/ln/Makefile Normal file
View File

@@ -0,0 +1,12 @@
# Makefile for ln
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=ln
SRCS=ln.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

93
userland/bin/ln/ln.c Normal file
View File

@@ -0,0 +1,93 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
/*
* ln - hardlink or symlink files
*
* Usage: ln oldfile newfile
* ln -s symlinkcontents symlinkfile
*/
/*
* Create a symlink with filename PATH that contains text TEXT.
* When fed to ls -l, this produces something that looks like
*
* lrwxrwxrwx [stuff] PATH -> TEXT
*/
static
void
dosymlink(const char *text, const char *path)
{
if (symlink(text, path)) {
err(1, "%s", path);
}
}
/*
* Create a hard link such that NEWFILE names the same file as
* OLDFILE. Since it's a hard link, the two names for the file
* are equal; both are the "real" file.
*/
static
void
dohardlink(const char *oldfile, const char *newfile)
{
if (link(oldfile, newfile)) {
err(1, "%s or %s", oldfile, newfile);
exit(1);
}
}
int
main(int argc, char *argv[])
{
/*
* Just do whatever was asked for.
*
* We don't allow the Unix model where you can do
* ln [-s] file1 file2 file3 destination-directory
*/
if (argc==4 && !strcmp(argv[1], "-s")) {
dosymlink(argv[2], argv[3]);
}
else if (argc==3) {
dohardlink(argv[1], argv[2]);
}
else {
warnx("Usage: ln oldfile newfile");
errx(1, " ln -s symlinkcontents symlinkfile\n");
}
return 0;
}

12
userland/bin/ls/Makefile Normal file
View File

@@ -0,0 +1,12 @@
# Makefile for ls
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=ls
SRCS=ls.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

345
userland/bin/ls/ls.c Normal file
View File

@@ -0,0 +1,345 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <err.h>
/*
* ls - list files.
* Usage: ls [-adlRs] [files]
* -a Show files whose names begin with a dot.
* -d Don't list contents of directories specified on the command line.
* -l Long format listing.
* -R Recurse into subdirectories found.
* -s (with -l) Show block counts.
*/
/* Flags for which options we're using. */
static int aopt=0;
static int dopt=0;
static int lopt=0;
static int Ropt=0;
static int sopt=0;
/* Process an option character. */
static
void
option(int ch)
{
switch (ch) {
case 'a': aopt=1; break;
case 'd': dopt=1; break;
case 'l': lopt=1; break;
case 'R': Ropt=1; break;
case 's': sopt=1; break;
default:
errx(1, "Unknown option -%c", ch);
}
}
/*
* Utility function to find the non-directory part of a pathname.
*/
static
const char *
basename(const char *path)
{
const char *s;
s = strrchr(path, '/');
if (s) {
return s+1;
}
return path;
}
/*
* Utility function to check if a name refers to a directory.
*/
static
int
isdir(const char *path)
{
struct stat buf;
int fd;
/* Assume stat() may not be implemented; use fstat */
fd = open(path, O_RDONLY);
if (fd<0) {
err(1, "%s", path);
}
if (fstat(fd, &buf)<0) {
err(1, "%s: fstat", path);
}
close(fd);
return S_ISDIR(buf.st_mode);
}
/*
* When listing one of several subdirectories, show the name of the
* directory.
*/
static
void
printheader(const char *file)
{
/* No blank line before the first header */
static int first=1;
if (first) {
first = 0;
}
else {
printf("\n");
}
printf("%s:\n", file);
}
/*
* Show a single file.
* We don't do the neat multicolumn listing that Unix ls does.
*/
static
void
print(const char *path)
{
struct stat statbuf;
const char *file;
int typech;
if (lopt || sopt) {
int fd;
fd = open(path, O_RDONLY);
if (fd<0) {
err(1, "%s", path);
}
if (fstat(fd, &statbuf)<0) {
err(1, "%s: fstat", path);
}
close(fd);
}
file = basename(path);
if (sopt) {
printf("%3d ", statbuf.st_blocks);
}
if (lopt) {
if (S_ISREG(statbuf.st_mode)) {
typech = '-';
}
else if (S_ISDIR(statbuf.st_mode)) {
typech = 'd';
}
else if (S_ISLNK(statbuf.st_mode)) {
typech = 'l';
}
else if (S_ISCHR(statbuf.st_mode)) {
typech = 'c';
}
else if (S_ISBLK(statbuf.st_mode)) {
typech = 'b';
}
else {
typech = '?';
}
printf("%crwx------ %2d root %-8llu",
typech,
statbuf.st_nlink,
statbuf.st_size);
}
printf("%s\n", file);
}
/*
* List a directory.
*/
static
void
listdir(const char *path, int showheader)
{
int fd;
char buf[1024];
char newpath[1024];
ssize_t len;
if (showheader) {
printheader(path);
}
/*
* Open it.
*/
fd = open(path, O_RDONLY);
if (fd<0) {
err(1, "%s", path);
}
/*
* List the directory.
*/
while ((len = getdirentry(fd, buf, sizeof(buf)-1)) > 0) {
buf[len] = 0;
/* Assemble the full name of the new item */
snprintf(newpath, sizeof(newpath), "%s/%s", path, buf);
if (aopt || buf[0]!='.') {
/* Print it */
print(newpath);
}
}
if (len<0) {
err(1, "%s: getdirentry", path);
}
/* Done */
close(fd);
}
static
void
recursedir(const char *path)
{
int fd;
char buf[1024];
char newpath[1024];
int len;
/*
* Open it.
*/
fd = open(path, O_RDONLY);
if (fd<0) {
err(1, "%s", path);
}
/*
* List the directory.
*/
while ((len = getdirentry(fd, buf, sizeof(buf)-1)) > 0) {
buf[len] = 0;
/* Assemble the full name of the new item */
snprintf(newpath, sizeof(newpath), "%s/%s", path, buf);
if (!aopt && buf[0]=='.') {
/* skip this one */
continue;
}
if (!strcmp(buf, ".") || !strcmp(buf, "..")) {
/* always skip these */
continue;
}
if (!isdir(newpath)) {
continue;
}
listdir(newpath, 1 /*showheader*/);
if (Ropt) {
recursedir(newpath);
}
}
if (len<0) {
err(1, "%s", path);
}
close(fd);
}
static
void
listitem(const char *path, int showheader)
{
if (!dopt && isdir(path)) {
listdir(path, showheader || Ropt);
if (Ropt) {
recursedir(path);
}
}
else {
print(path);
}
}
int
main(int argc, char *argv[])
{
int i,j, items=0;
/*
* Go through the arguments and count how many non-option args.
*/
for (i=1; i<argc; i++) {
if (argv[i][0]!='-') {
items++;
}
}
/*
* Now go through the options for real, processing them.
*/
for (i=1; i<argc; i++) {
if (argv[i][0]=='-') {
/*
* This word is an option.
* Process all the option characters in it.
*/
for (j=1; argv[i][j]; j++) {
option(argv[i][j]);
}
}
else {
/*
* This word isn't an option; list it.
*/
listitem(argv[i], items>1);
}
}
/*
* If no filenames were specified to list, list the current
* directory.
*/
if (items==0) {
listitem(".", 0);
}
return 0;
}

View File

@@ -0,0 +1,12 @@
# Makefile for mkdir
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=mkdir
SRCS=mkdir.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <err.h>
/*
* mkdir - create a directory.
* Usage: mkdir DIRECTORY
*
* Just calls the mkdir() system call.
*/
int
main(int argc, char *argv[])
{
if (argc!=2) {
errx(1, "Usage: mkdir DIRECTORY");
}
if (mkdir(argv[1], 0775)) {
err(1, "%s", argv[1]);
}
return 0;
}

12
userland/bin/mv/Makefile Normal file
View File

@@ -0,0 +1,12 @@
# Makefile for mv
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=mv
SRCS=mv.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

64
userland/bin/mv/mv.c Normal file
View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <err.h>
/*
* mv - move (rename) files.
* Usage: mv oldfile newfile
*
* Just calls rename() on them. If it fails, we don't attempt to
* figure out which filename was wrong or what happened.
*
* In certain circumstances, Unix mv will fall back to copying and
* deleting the old copy. We don't do that.
*
* We also don't allow the Unix form of
* mv file1 file2 file3 destination-dir
*/
static
void
dorename(const char *oldfile, const char *newfile)
{
if (rename(oldfile, newfile)) {
err(1, "%s or %s", oldfile, newfile);
}
}
int
main(int argc, char *argv[])
{
if (argc!=3) {
errx(1, "Usage: mv oldfile newfile");
}
dorename(argv[1], argv[2]);
return 0;
}

12
userland/bin/pwd/Makefile Normal file
View File

@@ -0,0 +1,12 @@
# Makefile for pwd
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=pwd
SRCS=pwd.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

55
userland/bin/pwd/pwd.c Normal file
View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <limits.h>
/*
* pwd - print working directory.
* Usage: pwd
*
* Just uses the getcwd library call (which in turn uses the __getcwd
* system call.)
*/
int
main(void)
{
char buf[PATH_MAX+1], *p;
p = getcwd(buf, sizeof(buf));
if (p == NULL) {
err(1, ".");
}
printf("%s\n", buf);
return 0;
}

12
userland/bin/rm/Makefile Normal file
View File

@@ -0,0 +1,12 @@
# Makefile for rm
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=rm
SRCS=rm.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

64
userland/bin/rm/rm.c Normal file
View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <err.h>
/*
* rm - remove (delete) files
* Usage: rm file...
*/
/* Delete a single file. */
static
void
doremove(const char *file)
{
if (remove(file)) {
err(1, "%s", file);
}
}
int
main(int argc, char *argv[])
{
int i;
if (argc<2) {
/* Must have at least one file. */
errx(1, "Usage: rm FILES");
}
/* Just delete everything on the command line. */
for (i=1; i<argc; i++) {
doremove(argv[i]);
}
return 0;
}

View File

@@ -0,0 +1,12 @@
# Makefile for rmdir
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=rmdir
SRCS=rmdir.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <err.h>
/*
* rmdir - remove a directory
* Usage: rmdir DIRECTORY
*
* Just calls the rmdir() system call.
*/
int
main(int argc, char *argv[])
{
if (argc!=2) {
errx(1, "Usage: rmdir DIRECTORY");
}
if (rmdir(argv[1])) {
err(1, "%s", argv[1]);
}
return 0;
}

13
userland/bin/sh/Makefile Normal file
View File

@@ -0,0 +1,13 @@
# Makefile for sh
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=sh
SRCS=sh.c
BINDIR=/bin
HOSTBINDIR=/hostbin
.include "$(TOP)/mk/os161.prog.mk"
#.include "$(TOP)/mk/os161.hostprog.mk"

591
userland/bin/sh/sh.c Normal file
View File

@@ -0,0 +1,591 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* sh - shell
*
* Usage:
* sh
* sh -c command
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <err.h>
#ifdef HOST
#include "hostcompat.h"
#endif
#ifndef NARG_MAX
/* no NARG_MAX on most unixes */
#define NARG_MAX 1024
#endif
/* avoid making this unreasonably large; causes problems under dumbvm */
#define CMDLINE_MAX 4096
/* struct to (portably) hold exit info */
struct exitinfo {
unsigned val:8,
signaled:1,
stopped:1,
coredump:1;
};
/* set to nonzero if __time syscall seems to work */
static int timing = 0;
/* array of backgrounded jobs (allows "foregrounding") */
#define MAXBG 128
static pid_t bgpids[MAXBG];
/*
* can_bg
* just checks for an open slot.
*/
static
int
can_bg(void)
{
int i;
for (i = 0; i < MAXBG; i++) {
if (bgpids[i] == 0) {
return 1;
}
}
return 0;
}
/*
* remember_bg
* sticks the pid in an open slot in the background array. note the assert --
* better check can_bg before calling this.
*/
static
void
remember_bg(pid_t pid)
{
int i;
for (i = 0; i < MAXBG; i++) {
if (bgpids[i] == 0) {
bgpids[i] = pid;
return;
}
}
assert(0);
}
/*
* constructor for exitinfo
*/
static
void
exitinfo_exit(struct exitinfo *ei, int code)
{
ei->val = code;
ei->signaled = 0;
ei->stopped = 0;
ei->coredump = 0;
}
/*
* readstatus
* unpack results from wait
*/
static
void
readstatus(int status, struct exitinfo *ei)
{
if (WIFEXITED(status)) {
ei->val = WEXITSTATUS(status);
ei->signaled = 0;
ei->stopped = 0;
ei->coredump = 0;
}
else if (WIFSIGNALED(status) && WCOREDUMP(status)) {
ei->val = WTERMSIG(status);
ei->signaled = 1;
ei->stopped = 0;
ei->coredump = 1;
}
else if (WIFSIGNALED(status)) {
ei->val = WTERMSIG(status);
ei->signaled = 1;
ei->stopped = 0;
ei->coredump = 0;
}
else if (WIFSTOPPED(status)) {
ei->val = WSTOPSIG(status);
ei->signaled = 0;
ei->stopped = 1;
ei->coredump = 0;
}
else {
printf("Invalid status code %d", status);
ei->val = status;
ei->signaled = 0;
ei->stopped = 0;
ei->coredump = 0;
}
}
/*
* printstatus
* print results from wait
*/
static
void
printstatus(const struct exitinfo *ei, int printexitzero)
{
if (ei->signaled && ei->coredump) {
printf("Signal %d (core dumped)\n", ei->val);
}
else if (ei->signaled) {
printf("Signal %d\n", ei->val);
}
else if (ei->stopped) {
printf("Stopped on signal %d\n", ei->val);
}
else if (printexitzero || ei->val != 0) {
printf("Exit %d\n", ei->val);
}
}
/*
* dowait
* just does a waitpid.
*/
static
void
dowait(pid_t pid)
{
struct exitinfo ei;
int status;
if (waitpid(pid, &status, 0) < 0) {
warn("pid %d", pid);
}
else {
printf("pid %d: ", pid);
readstatus(status, &ei);
printstatus(&ei, 1);
}
}
#ifdef WNOHANG
/*
* dowaitpoll
* like dowait, but uses WNOHANG. returns true if we got something.
*/
static
int
dowaitpoll(pid_t pid)
{
struct exitinfo ei;
pid_t foundpid;
int status;
foundpid = waitpid(pid, &status, WNOHANG);
if (foundpid < 0) {
warn("pid %d", pid);
}
else if (foundpid != 0) {
printf("pid %d: ", pid);
readstatus(status, &ei);
printstatus(&ei, 1);
return 1;
}
return 0;
}
/*
* waitpoll
* poll all background jobs for having exited.
*/
static
void
waitpoll(void)
{
int i;
for (i=0; i < MAXBG; i++) {
if (bgpids[i] != 0) {
if (dowaitpoll(bgpids[i])) {
bgpids[i] = 0;
}
}
}
}
#endif /* WNOHANG */
/*
* wait
* allows the user to "foreground" a process by waiting on it. without ps to
* know the pids, this is a little tough to use with an arg, but without an
* arg it will wait for all the background jobs.
*/
static
void
cmd_wait(int ac, char *av[], struct exitinfo *ei)
{
int i;
pid_t pid;
if (ac == 2) {
pid = atoi(av[1]);
dowait(pid);
for (i = 0; i < MAXBG; i++) {
if (bgpids[i]==pid) {
bgpids[i] = 0;
}
}
exitinfo_exit(ei, 0);
return;
}
else if (ac == 1) {
for (i=0; i < MAXBG; i++) {
if (bgpids[i] != 0) {
dowait(bgpids[i]);
bgpids[i] = 0;
}
}
exitinfo_exit(ei, 0);
return;
}
printf("Usage: wait [pid]\n");
exitinfo_exit(ei, 1);
}
/*
* chdir
* just an interface to the system call. no concept of home directory, so
* require the directory.
*/
static
void
cmd_chdir(int ac, char *av[], struct exitinfo *ei)
{
if (ac == 2) {
if (chdir(av[1])) {
warn("chdir: %s", av[1]);
exitinfo_exit(ei, 1);
return;
}
exitinfo_exit(ei, 0);
return;
}
printf("Usage: chdir dir\n");
exitinfo_exit(ei, 1);
}
/*
* exit
* pretty simple. allow the user to choose the exit code if they want,
* otherwise default to 0 (success).
*/
static
void
cmd_exit(int ac, char *av[], struct exitinfo *ei)
{
int code;
if (ac == 1) {
code = 0;
}
else if (ac == 2) {
code = atoi(av[1]);
}
else {
printf("Usage: exit [code]\n");
exitinfo_exit(ei, 1);
return;
}
exit(code);
}
/*
* a struct of the builtins associates the builtin name with the function that
* executes it. they must all take an argc and argv.
*/
static struct {
const char *name;
void (*func)(int, char **, struct exitinfo *);
} builtins[] = {
{ "cd", cmd_chdir },
{ "chdir", cmd_chdir },
{ "exit", cmd_exit },
{ "wait", cmd_wait },
{ NULL, NULL }
};
/*
* docommand
* tokenizes the command line using strtok. if there aren't any commands,
* simply returns. checks to see if it's a builtin, running it if it is.
* otherwise, it's a standard command. check for the '&', try to background
* the job if possible, otherwise just run it and wait on it.
*/
static
void
docommand(char *buf, struct exitinfo *ei)
{
char *args[NARG_MAX + 1];
int nargs, i;
char *s;
pid_t pid;
int status;
int bg=0;
time_t startsecs, endsecs;
unsigned long startnsecs, endnsecs;
nargs = 0;
for (s = strtok(buf, " \t\r\n"); s; s = strtok(NULL, " \t\r\n")) {
if (nargs >= NARG_MAX) {
printf("%s: Too many arguments "
"(exceeds system limit)\n",
args[0]);
exitinfo_exit(ei, 1);
return;
}
args[nargs++] = s;
}
args[nargs] = NULL;
if (nargs==0) {
/* empty line */
exitinfo_exit(ei, 0);
return;
}
for (i=0; builtins[i].name; i++) {
if (!strcmp(builtins[i].name, args[0])) {
builtins[i].func(nargs, args, ei);
return;
}
}
/* Not a builtin; run it */
if (nargs > 0 && !strcmp(args[nargs-1], "&")) {
/* background */
if (!can_bg()) {
printf("%s: Too many background jobs; wait for "
"some to finish before starting more\n",
args[0]);
exitinfo_exit(ei, 1);
return;
}
nargs--;
args[nargs] = NULL;
bg = 1;
}
if (timing) {
__time(&startsecs, &startnsecs);
}
pid = fork();
switch (pid) {
case -1:
/* error */
warn("fork");
exitinfo_exit(ei, 255);
return;
case 0:
/* child */
execvp(args[0], args);
warn("%s", args[0]);
/*
* Use _exit() instead of exit() in the child
* process to avoid calling atexit() functions,
* which would cause hostcompat (if present) to
* reset the tty state and mess up our input
* handling.
*/
_exit(1);
default:
break;
}
/* parent */
if (bg) {
/* background this command */
remember_bg(pid);
printf("[%d] %s ... &\n", pid, args[0]);
exitinfo_exit(ei, 0);
return;
}
if (waitpid(pid, &status, 0) < 0) {
warn("waitpid");
exitinfo_exit(ei, 255);
}
else {
readstatus(status, ei);
}
if (timing) {
__time(&endsecs, &endnsecs);
if (endnsecs < startnsecs) {
endnsecs += 1000000000;
endsecs--;
}
endnsecs -= startnsecs;
endsecs -= startsecs;
warnx("subprocess time: %lu.%09lu seconds",
(unsigned long) endsecs, (unsigned long) endnsecs);
}
}
/*
* getcmd
* pulls valid characters off the console, filling the buffer.
* backspace deletes a character, simply by moving the position back.
* a newline or carriage return breaks the loop, which terminates
* the string and returns.
*
* if there's an invalid character or a backspace when there's nothing
* in the buffer, putchars an alert (bell).
*/
static
void
getcmd(char *buf, size_t len)
{
size_t pos = 0;
int done=0, ch;
/*
* In the absence of a <ctype.h>, assume input is 7-bit ASCII.
*/
while (!done) {
ch = getchar();
if ((ch == '\b' || ch == 127) && pos > 0) {
putchar('\b');
putchar(' ');
putchar('\b');
pos--;
}
else if (ch == '\r' || ch == '\n') {
putchar('\r');
putchar('\n');
done = 1;
}
else if (ch >= 32 && ch < 127 && pos < len-1) {
buf[pos++] = ch;
putchar(ch);
}
else {
/* alert (bell) character */
putchar('\a');
}
}
buf[pos] = 0;
}
/*
* interactive
* runs the interactive shell. basically, just infinitely loops, grabbing
* commands and running them (and printing the exit status if it's not
* success.)
*/
static
void
interactive(void)
{
char buf[CMDLINE_MAX];
struct exitinfo ei;
while (1) {
printf("OS/161$ ");
getcmd(buf, sizeof(buf));
docommand(buf, &ei);
printstatus(&ei, 0);
#ifdef WNOHANG
waitpoll();
#endif
}
}
static
void
check_timing(void)
{
time_t secs;
unsigned long nsecs;
if (__time(&secs, &nsecs) != -1) {
timing = 1;
warnx("Timing enabled.");
}
}
/*
* main
* if there are no arguments, run interactively, otherwise, run a program
* from within the shell, but immediately exit.
*/
int
main(int argc, char *argv[])
{
#ifdef HOST
hostcompat_init(argc, argv);
#endif
check_timing();
/*
* Allow argc to be 0 in case we're running on a broken kernel,
* or one that doesn't set argv when starting the first shell.
*/
if (argc == 0 || argc == 1) {
interactive();
}
else if (argc == 3 && !strcmp(argv[1], "-c")) {
struct exitinfo ei;
docommand(argv[2], &ei);
printstatus(&ei, 0);
if (ei.signaled || ei.stopped || ei.val != 0) {
exit(1);
}
}
else {
errx(1, "Usage: sh [-c command]");
}
return 0;
}

View File

@@ -0,0 +1,12 @@
# Makefile for sync
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=sync
SRCS=sync.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

43
userland/bin/sync/sync.c Normal file
View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
/*
* sync - force kernel buffers (write-back disk cache) to disk.
*
* Just calls the sync() system call.
*/
int
main(void)
{
sync();
return 0;
}

12
userland/bin/tac/Makefile Normal file
View File

@@ -0,0 +1,12 @@
# Makefile for tac
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=tac
SRCS=tac.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

294
userland/bin/tac/tac.c Normal file
View File

@@ -0,0 +1,294 @@
/*
* Copyright (c) 2014
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* tac - print file backwards line by line (reverse cat)
* usage: tac [files]
*
* This implementation copies the input to a scratch file, using a
* second scratch file to keep notes, and then prints the scratch file
* backwards. This is inefficient, but has the side effect of testing
* the behavior of scratch files that have been unlinked.
*
* Note that if the remove system call isn't implemented, unlinking
* the scratch files will fail and the scratch files will get left
* behind. To avoid unnecessary noise (e.g. on emufs) we won't
* complain about this.
*
* This program uses these system calls:
* getpid open read write lseek close remove _exit
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <err.h>
struct indexentry {
off_t pos;
off_t len;
};
static int datafd = -1, indexfd = -1;
static char dataname[64], indexname[64];
static char buf[4096];
////////////////////////////////////////////////////////////
// string ops
/* this is standard and should go into libc */
static
void *
memchr(const void *buf, int ch, size_t buflen)
{
const unsigned char *ubuf = buf;
size_t i;
for (i=0; i<buflen; i++) {
if (ubuf[i] == ch) {
/* this must launder const */
return (void *)(ubuf + i);
}
}
return NULL;
}
////////////////////////////////////////////////////////////
// syscall wrappers
static
size_t
doread(int fd, const char *name, void *buf, size_t len)
{
ssize_t r;
r = read(fd, buf, len);
if (r == -1) {
err(1, "%s: read", name);
}
return (size_t)r;
}
static
void
dowrite(int fd, const char *name, const void *buf, size_t len)
{
ssize_t r;
r = write(fd, buf, len);
if (r == -1) {
err(1, "%s: write", name);
}
else if ((size_t)r != len) {
errx(1, "%s: write: Unexpected short count %zd of %zu",
r, len);
}
}
static
off_t
dolseek(int fd, const char *name, off_t pos, int whence)
{
off_t ret;
ret = lseek(fd, pos, whence);
if (ret == -1) {
err(1, "%s: lseek", name);
}
return ret;
}
////////////////////////////////////////////////////////////
// file I/O
static
void
readfile(const char *name)
{
int fd, closefd;
struct indexentry x;
size_t len, remaining, here;
const char *s, *t;
if (name == NULL || !strcmp(name, "-")) {
fd = STDIN_FILENO;
closefd = -1;
}
else {
fd = open(name, O_RDONLY);
if (fd < 0) {
err(1, "%s", name);
}
closefd = fd;
}
x.pos = 0;
x.len = 0;
while (1) {
len = doread(fd, name, buf, sizeof(buf));
if (len == 0) {
break;
}
remaining = len;
for (s = buf; s != NULL; s = t) {
t = memchr(s, '\n', remaining);
if (t != NULL) {
t++;
here = (t - s);
x.len += here;
remaining -= here;
dowrite(indexfd, indexname, &x, sizeof(x));
x.pos += x.len;
x.len = 0;
}
else {
x.len += remaining;
}
}
dowrite(datafd, dataname, buf, len);
}
if (x.len > 0) {
dowrite(indexfd, indexname, &x, sizeof(x));
}
if (closefd != -1) {
close(closefd);
}
}
static
void
dumpdata(void)
{
struct indexentry x;
off_t indexsize, pos, done;
size_t amount, len;
indexsize = dolseek(indexfd, indexname, 0, SEEK_CUR);
pos = indexsize;
while (1) {
pos -= sizeof(x);
if (pos == 0) {
break;
}
assert(pos >= 0);
dolseek(indexfd, indexname, pos, SEEK_SET);
len = doread(indexfd, indexname, &x, sizeof(x));
if (len != sizeof(x)) {
errx(1, "%s: read: Unexpected EOF", indexname);
}
dolseek(datafd, dataname, x.pos, SEEK_SET);
for (done = 0; done < x.len; done += amount) {
amount = sizeof(buf);
if ((off_t)amount > x.len - done) {
amount = x.len - done;
}
len = doread(datafd, dataname, buf, amount);
if (len != amount) {
errx(1, "%s: read: Unexpected short count"
" %zu of %zu", dataname, len, amount);
}
dowrite(STDOUT_FILENO, "stdout", buf, len);
}
}
}
////////////////////////////////////////////////////////////
// main
static
int
openscratch(const char *name, int flags, mode_t mode)
{
int fd;
fd = open(name, flags, mode);
if (fd < 0) {
err(1, "%s", name);
}
if (remove(name) < 0) {
if (errno != ENOSYS) {
err(1, "%s: remove", name);
}
}
return fd;
}
static
void
openfiles(void)
{
pid_t pid;
pid = getpid();
snprintf(dataname, sizeof(dataname), ".tmp.tacdata.%d", (int)pid);
datafd = openscratch(dataname, O_RDWR|O_CREAT|O_TRUNC, 0664);
snprintf(indexname, sizeof(indexname), ".tmp.tacindex.%d", (int)pid);
indexfd = openscratch(indexname, O_RDWR|O_CREAT|O_TRUNC, 0664);
}
static
void
closefiles(void)
{
close(datafd);
close(indexfd);
indexfd = datafd = -1;
}
int
main(int argc, char *argv[])
{
int i;
openfiles();
if (argc > 1) {
for (i=1; i<argc; i++) {
readfile(argv[i]);
}
}
else {
readfile(NULL);
}
dumpdata();
closefiles();
return 0;
}

View File

@@ -0,0 +1,12 @@
# Makefile for true
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=true
SRCS=true.c
BINDIR=/bin
.include "$(TOP)/mk/os161.prog.mk"

42
userland/bin/true/true.c Normal file
View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <stdlib.h>
/*
* true - succeed.
*/
int
main(void)
{
/* Just exit with success. */
exit(0);
}

60
userland/include/assert.h Normal file
View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _ASSERT_H_
#define _ASSERT_H_
/*
* Required by the C standard
*/
#undef assert
/*
* Function to call when an assert fails.
*/
void __bad_assert(const char *file, int line, const char *msg);
/*
* Asserts are only "on" if NDEBUG isn't set. (This is standard C.)
*/
#ifdef NDEBUG
#if 0 /* not allowed by the C standard */
#define assert(x) ((void)(x)) /* retain any side effects of X */
#else
#define assert(x) ((void)0) /* mysteriously hide any side effects of X */
#endif
#else
#define assert(x) ((x) ? (void)0 : __bad_assert(__FILE__, __LINE__, #x))
#endif
#endif /* _ASSERT_H_ */

58
userland/include/err.h Normal file
View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _ERR_H_
#define _ERR_H_
#include <sys/cdefs.h> /* for __DEAD */
#include <kern/types.h> /* for __va_list */
/*
* 4.4BSD error-printing functions.
*
* These print the program name and the supplied message, and
* (non-*x versions only) the string for the error currently
* stored in "errno", and a newline. The err* versions then
* exit with the supplied exitcode.
*
* The v* versions are to the non-v* versions like vprintf is to
* printf.
*/
void warn(const char *fmt, ...);
void warnx(const char *fmt, ...);
__DEAD void err(int exitcode, const char *fmt, ...);
__DEAD void errx(int exitcode, const char *fmt, ...);
void vwarn(const char *fmt, __va_list);
void vwarnx(const char *fmt, __va_list);
__DEAD void verr(int exitcode, const char *fmt, __va_list);
__DEAD void verrx(int exitcode, const char *fmt, __va_list);
#endif /* _ERR_H_ */

39
userland/include/errno.h Normal file
View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _ERRNO_H_
#define _ERRNO_H_
/* Get the error codes from the kernel. */
#include <kern/errno.h>
/* Declare the standard global variable errno. */
extern int errno;
#endif /* _ERRNO_H_ */

31
userland/include/fcntl.h Normal file
View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* This file is for UNIX compat. In OS/161, everything's in <unistd.h> */
#include <unistd.h>

53
userland/include/limits.h Normal file
View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _LIMITS_H_
#define _LIMITS_H_
/*
* System limits.
*/
/* Get the limits the kernel exports. libc doesn't have any limits :-) */
#include <kern/limits.h>
/* Provide the real names */
#define NAME_MAX __NAME_MAX
#define PATH_MAX __PATH_MAX
#define ARG_MAX __ARG_MAX
#define PID_MIN __PID_MIN
#define PID_MAX __PID_MAX
#define PIPE_BUF __PIPE_BUF
#define NGROUPS_MAX __NGROUPS_MAX
#define LOGIN_NAME_MAX __LOGIN_NAME_MAX
#define OPEN_MAX __OPEN_MAX
#define IOV_MAX __IOV_MAX
#endif /* _LIMITS_H_ */

52
userland/include/setjmp.h Normal file
View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _SETJMP_H_
#define _SETJMP_H_
/* Get the (machine-dependent) definition of jmp_buf. */
#include <machine/setjmp.h>
/*
* Functions.
*
* setjmp saves the current processor state in the jmp_buf and
* returns 0. A subsequent call to longjmp with the same jmp_buf
* causes execution to return to where setjmp was called. setjmp
* returns a second time, this time returning CODE. (If CODE is
* 0, it is forced to 1.)
*
* If the stack frame that called setjmp returns before longjmp is
* called, the results are undefined.
*/
int setjmp(jmp_buf jb);
void longjmp(jmp_buf jb, int code);
#endif /* _SETJMP_H_ */

30
userland/include/signal.h Normal file
View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <kern/signal.h>

54
userland/include/stdarg.h Normal file
View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _STDARG_H_
#define _STDARG_H_
#include <kern/types.h>
/*
* As of gcc 3.0, the stdarg declarations can be made machine-
* independent because gcc abstracts the implementations away for
* us. However, they went and changed __builtin_stdarg_start to
* __builtin_va_start sometime between gcc 4.1 and 4.8 (not sure
* when) so we need to check that.
*/
typedef __va_list va_list;
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
#define va_start(ap, fmt) __builtin_stdarg_start(ap, fmt)
#else
#define va_start(ap, fmt) __builtin_va_start(ap, fmt)
#endif
#define va_arg(ap,t) __builtin_va_arg(ap,t)
#define va_copy(ap1,ap2) __builtin_va_copy(ap1,ap2)
#define va_end(ap) __builtin_va_end(ap)
#endif /* _STDARG_H_ */

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2014
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _STDBOOL_H_
#define _STDBOOL_H_
typedef _Bool bool;
#define true 1
#define false 0
#endif /* _STDBOOL_H_ */

51
userland/include/stdint.h Normal file
View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _STDINT_H_
#define _STDINT_H_
/*
* <stdint.h> - C99 header file with sized integer types.
*/
#include <kern/types.h>
typedef __i8 int8_t;
typedef __i16 int16_t;
typedef __i32 int32_t;
typedef __i64 int64_t;
typedef __u8 uint8_t;
typedef __u16 uint16_t;
typedef __u32 uint32_t;
typedef __u64 uint64_t;
typedef __intptr_t intptr_t;
typedef __uintptr_t uintptr_t;
#endif /* _STDINT_H_ */

72
userland/include/stdio.h Normal file
View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _STDIO_H_
#define _STDIO_H_
/*
* Get the __-protected definition of va_list. We aren't supposed to
* include stdarg.h here.
*/
#include <kern/types.h>
#include <types/size_t.h>
#include <sys/null.h>
/* Constant returned by a bunch of stdio functions on error */
#define EOF (-1)
/*
* The actual guts of printf
* (for libc internal use only)
*/
int __vprintf(void (*sendfunc)(void *clientdata, const char *, size_t len),
void *clientdata,
const char *fmt,
__va_list ap);
/* Printf calls for user programs */
int printf(const char *fmt, ...);
int vprintf(const char *fmt, __va_list ap);
int snprintf(char *buf, size_t len, const char *fmt, ...);
int vsnprintf(char *buf, size_t len, const char *fmt, __va_list ap);
/* Print the argument string and then a newline. Returns 0 or -1 on error. */
int puts(const char *);
/* Like puts, but without the newline. Returns #chars written. */
/* Nonstandard C, hence the __. */
int __puts(const char *);
/* Writes one character. Returns it. */
int putchar(int);
/* Reads one character (0-255) or returns EOF on error. */
int getchar(void);
#endif /* _STDIO_H_ */

85
userland/include/stdlib.h Normal file
View File

@@ -0,0 +1,85 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _STDLIB_H_
#define _STDLIB_H_
#include <kern/types.h>
#include <types/size_t.h>
#include <sys/null.h>
/*
* Ascii to integer - turn a string holding a number into a number.
*/
int atoi(const char *);
/*
* Standard routine to bail out of a program in a severe error condition.
*/
void abort(void);
/*
* Routine to exit cleanly.
* (This does libc cleanup before calling the _exit system call.)
*/
void exit(int code);
/*
* Get the value of an environment variable. A default environment is
* provided if the kernel doesn't pass environment strings.
*/
char *getenv(const char *var);
/*
* Run a command. Returns its exit status as it comes back from waitpid().
*/
int system(const char *command);
/*
* Pseudo-random number generator.
*/
#define RAND_MAX 0x7fffffff
long random(void);
void srandom(unsigned long seed);
char *initstate(unsigned long, char *, size_t);
char *setstate(char *);
/*
* Memory allocation functions.
*/
void *malloc(size_t size);
void free(void *ptr);
/*
* Sort.
*/
void qsort(void *data, unsigned num, size_t size,
int (*f)(const void *, const void *));
#endif /* _STDLIB_H_ */

65
userland/include/string.h Normal file
View File

@@ -0,0 +1,65 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _STRING_H_
#define _STRING_H_
#include <kern/types.h>
#include <types/size_t.h>
#include <sys/null.h>
/*
* Standard C string functions.
*/
char *strcat(char *, const char *);
char *strcpy(char *, const char *);
char *strchr(const char *, int);
char *strrchr(const char *, int);
int strcmp(const char *, const char *);
size_t strlen(const char *);
char *strtok_r(char *, const char *, char **);
char *strtok(char *, const char *);
void *memset(void *, int c, size_t);
void *memcpy(void *, const void *, size_t);
void *memmove(void *, const void *, size_t);
int memcmp(const void *, const void *, size_t);
/*
* POSIX string functions.
*/
const char *strerror(int errcode);
/*
* BSD string functions.
*/
void bzero(void *, size_t);
#endif /* _STRING_H_ */

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2014
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* This file contains definitions for preprocessor macros needed for
* declaring things in the system include files. It must not define
* anything in the user namespace so it is safe to include anywhere.
*/
#ifndef _SYS_CDEFS_H_
#define _SYS_CDEFS_H_
#ifdef __GNUC__
#define __DEAD __attribute__((__noreturn__))
#else
#define __DEAD
#endif
#endif /* _SYS_CDEFS_H_ */

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _SYS_ENDIAN_H_
#define _SYS_ENDIAN_H_
#include <kern/endian.h>
#endif /* _SYS_ENDIAN_H_ */

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* This file is for UNIX compat. In OS/161, everything's in <unistd.h> */
#include <unistd.h>

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _SYS_NULL_H_
#define _SYS_NULL_H_
/*
* Null pointer.
*/
#define NULL ((void *)0)
#endif /* _SYS_NULL_H_ */

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* This file is for UNIX compat. In OS/161, everything's in <unistd.h> */
#include <unistd.h>

View File

@@ -0,0 +1,83 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _SYS_STAT_H_
#define _SYS_STAT_H_
/*
* Get struct stat and all the #defines from the kernel
*/
#include <kern/stat.h>
#include <kern/stattypes.h>
/*
* Test macros for object types.
*/
#define S_ISDIR(mode) ((mode & _S_IFMT) == _S_IFDIR)
#define S_ISREG(mode) ((mode & _S_IFMT) == _S_IFREG)
#define S_ISDIR(mode) ((mode & _S_IFMT) == _S_IFDIR)
#define S_ISLNK(mode) ((mode & _S_IFMT) == _S_IFLNK)
#define S_ISIFO(mode) ((mode & _S_IFMT) == _S_IFIFO)
#define S_ISSOCK(mode) ((mode & _S_IFMT) ==_S_IFSOCK)
#define S_ISCHR(mode) ((mode & _S_IFMT) == _S_IFCHR)
#define S_ISBLK(mode) ((mode & _S_IFMT) == _S_IFBLK)
/*
* Provide non-underscore names. These are not actually standard; for
* some reason only the test macros are.
*/
#define S_IFMT _S_IFMT
#define S_IFREG _S_IFREG
#define S_IFDIR _S_IFDIR
#define S_IFLNK _S_IFLNK
#define S_IFIFO _S_IFIFO
#define S_IFSOCK _S_IFSOCK
#define S_IFCHR _S_IFCHR
#define S_IFBLK _S_IFBLK
/*
* stat is the same as fstat, only on a file that isn't already
* open. lstat is the same as stat, only if the name passed names a
* symlink, information about the symlink is returned rather than
* information about the file it points to. You don't need to
* implement lstat unless you're implementing symbolic links.
*/
int fstat(int filehandle, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
/*
* The second argument to mkdir is the mode for the new directory.
* Unless you're implementing security and permissions, you can
* (and should) ignore it. See notes in unistd.h.
*/
int mkdir(const char *dirname, int ignore);
#endif /* _SYS_STAT_H_ */

View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _SYS_TYPES_H_
#define _SYS_TYPES_H_
/*
* This header file is supposed to define standard system types,
* stuff like size_t and pid_t, as well as define a few other
* standard symbols like NULL.
*
* There are no such types that are user-level only.
*/
/* Get the exported kernel definitions, protected with __ */
#include <kern/types.h>
/* Pick up stuff that needs to be defined individually due to standards. */
#include <types/size_t.h>
#include <sys/null.h>
/*
* Define the rest with user-visible names.
*
* Note that the standards-compliance stuff is not by any means
* complete here yet...
*/
typedef __ssize_t ssize_t;
typedef __ptrdiff_t ptrdiff_t;
/* ...and machine-independent from <kern/types.h>. */
typedef __blkcnt_t blkcnt_t;
typedef __blksize_t blksize_t;
typedef __daddr_t daddr_t;
typedef __dev_t dev_t;
typedef __fsid_t fsid_t;
typedef __gid_t gid_t;
typedef __in_addr_t in_addr_t;
typedef __in_port_t in_port_t;
typedef __ino_t ino_t;
typedef __mode_t mode_t;
typedef __nlink_t nlink_t;
typedef __off_t off_t;
typedef __pid_t pid_t;
typedef __rlim_t rlim_t;
typedef __sa_family_t sa_family_t;
typedef __time_t time_t;
typedef __uid_t uid_t;
typedef __nfds_t nfds_t;
typedef __socklen_t socklen_t;
/*
* Number of bits per byte.
*/
#define CHAR_BIT __CHAR_BIT
#endif /* _SYS_TYPES_H_ */

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* This file is for UNIX compat. In OS/161, everything's in <unistd.h> */
#include <unistd.h>

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2013
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
void quint(const char *prog);

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
void triple(const char *prog);

31
userland/include/time.h Normal file
View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* This file is for UNIX compat. In OS/161, everything's in <unistd.h> */
#include <unistd.h>

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _TYPES_SIZE_T_H_
#define _TYPES_SIZE_T_H_
#include <kern/types.h>
typedef __size_t size_t;
#endif /* _TYPES_SIZE_T_H_ */

162
userland/include/unistd.h Normal file
View File

@@ -0,0 +1,162 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _UNISTD_H_
#define _UNISTD_H_
#include <sys/cdefs.h>
#include <sys/types.h>
/*
* Get the various constants (flags, codes, etc.) for calls from
* kernel includes. This way user-level code doesn't need to know
* about the kern/ headers.
*/
#include <kern/fcntl.h>
#include <kern/ioctl.h>
#include <kern/reboot.h>
#include <kern/seek.h>
#include <kern/time.h>
#include <kern/unistd.h>
#include <kern/wait.h>
/*
* Prototypes for OS/161 system calls.
*
* Note that the following system calls are prototyped in other
* header files, as follows:
*
* stat: sys/stat.h
* fstat: sys/stat.h
* lstat: sys/stat.h
* mkdir: sys/stat.h
*
* If this were standard Unix, more prototypes would go in other
* header files as well, as follows:
*
* waitpid: sys/wait.h
* open: fcntl.h or sys/fcntl.h
* reboot: sys/reboot.h
* ioctl: sys/ioctl.h
* remove: stdio.h
* rename: stdio.h
* time: time.h
*
* Also note that the prototypes for open() and mkdir() contain, for
* compatibility with Unix, an extra argument that is not meaningful
* in OS/161. This is the "mode" (file permissions) for a newly created
* object. (With open, if no file is created, this is ignored, and the
* call prototype is gimmicked so it doesn't have to be passed either.)
*
* You should ignore these arguments in the OS/161 kernel unless you're
* implementing security and file permissions.
*
* If you are implementing security and file permissions and using a
* model different from Unix so that you need different arguments to
* these calls, you may make appropriate changes, or define new syscalls
* with different names and take the old ones out, or whatever.
*
* As a general rule of thumb, however, while you can make as many new
* syscalls of your own as you like, you shouldn't change the
* definitions of the ones that are already here. They've been written
* to be pretty much compatible with Unix, and the teaching staff has
* test code that expects them to behave in particular ways.
*
* Of course, if you want to redesign the user/kernel API and make a
* lot of work for yourself, feel free, just contact the teaching
* staff beforehand. :-)
*
* The categories (required/recommended/optional) are guesses - check
* the text of the various assignments for an authoritative list.
*/
/*
* NOTE NOTE NOTE NOTE NOTE
*
* This file is *not* shared with the kernel, even though in a sense
* the kernel needs to know about these prototypes. This is because,
* due to error handling concerns, the in-kernel versions of these
* functions will usually have slightly different signatures.
*/
/* Required. */
__DEAD void _exit(int code);
int execv(const char *prog, char *const *args);
pid_t fork(void);
pid_t waitpid(pid_t pid, int *returncode, int flags);
/*
* Open actually takes either two or three args: the optional third
* arg is the file mode used for creation. Unless you're implementing
* security and permissions, you can ignore it.
*/
int open(const char *filename, int flags, ...);
ssize_t read(int filehandle, void *buf, size_t size);
ssize_t write(int filehandle, const void *buf, size_t size);
int close(int filehandle);
int reboot(int code);
int sync(void);
/* mkdir - see sys/stat.h */
int rmdir(const char *dirname);
/* Recommended. */
pid_t getpid(void);
int ioctl(int filehandle, int code, void *buf);
off_t lseek(int filehandle, off_t pos, int code);
int fsync(int filehandle);
int ftruncate(int filehandle, off_t size);
int remove(const char *filename);
int rename(const char *oldfile, const char *newfile);
int link(const char *oldfile, const char *newfile);
/* fstat - see sys/stat.h */
int chdir(const char *path);
/* Optional. */
void *sbrk(__intptr_t change);
ssize_t getdirentry(int filehandle, char *buf, size_t buflen);
int symlink(const char *target, const char *linkname);
ssize_t readlink(const char *path, char *buf, size_t buflen);
int dup2(int filehandle, int newhandle);
int pipe(int filehandles[2]);
int __time(time_t *seconds, unsigned long *nanoseconds);
ssize_t __getcwd(char *buf, size_t buflen);
/* stat - see sys/stat.h */
/* lstat - see sys/stat.h */
/*
* These are not themselves system calls, but wrapper routines in libc.
*/
int execvp(const char *prog, char *const *args); /* calls execv */
char *getcwd(char *buf, size_t buflen); /* calls __getcwd */
time_t time(time_t *seconds); /* calls __time */
#endif /* _UNISTD_H_ */

13
userland/lib/Makefile Normal file
View File

@@ -0,0 +1,13 @@
#
# Makefile for src/lib (sources for libraries installed in /lib)
#
# Note that all includes are found in src/include - the "includes"
# make rule for installing .h files does not come here.
#
TOP=../..
.include "$(TOP)/mk/os161.config.mk"
SUBDIRS=crt0 libc libtest hostcompat
.include "$(TOP)/mk/os161.subdir.mk"

View File

@@ -0,0 +1,27 @@
#
# Makefile for crt0.o
#
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
SRCS=$(MACHINE)/crt0.S
MKDIRS=$(INSTALLTOP)/lib $(OSTREE)/lib $(MYBUILDDIR)
.include "$(TOP)/mk/os161.baserules.mk"
.include "$(TOP)/mk/os161.compile.mk"
all-local: $(MYBUILDDIR) .WAIT $(OBJS)
install-staging-local: $(INSTALLTOP)/lib .WAIT
install-local: $(OSTREE)/lib .WAIT
.for O in $(OBJS)
install-staging-local: $(INSTALLTOP)/lib/$(O:T)
$(INSTALLTOP)/lib/$(O:T): $(O)
cp $(O) $(INSTALLTOP)/lib/$(O:T)
install-local: $(OSTREE)/lib/$(O:T)
$(OSTREE)/lib/$(O:T): $(O)
cp $(O) $(OSTREE)/lib/$(O:T)
.endfor

View File

@@ -0,0 +1,97 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* crt0.o for MIPS r2000/r3000.
*
* crt stands for "C runtime".
*
* Basically, this is the startup code that gets invoked before main(),
* and regains control when main returns.
*
* All we really do is save copies of argv and environ for use by libc
* funcions (e.g. err* and warn*), and call exit when main returns.
*/
#include <kern/mips/regdefs.h>
#include <kern/syscall.h>
.set noreorder /* so we can use delay slots explicitly */
.text
.globl __start
.type __start,@function
.ent __start
__start:
/* Load the "global pointer" register */
la gp, _gp
/*
* We expect that the kernel passes argc in a0, argv in a1,
* and environ in a2. We do not expect the kernel to set up a
* complete stack frame, however.
*
* The MIPS ABI decrees that every caller will leave 16 bytes of
* space in the bottom of its stack frame for writing back the
* values of a0-a3, even when calling functions that take fewer
* than four arguments. It also requires the stack to be aligned
* to an 8-byte boundary. (This is because of 64-bit MIPS, which
* we're not dealing with... but we'll conform to the standard.)
*/
li t0, 0xfffffff8 /* mask for stack alignment */
and sp, sp, t0 /* align the stack */
addiu sp, sp, -16 /* create our frame */
sw a1, __argv /* save second arg (argv) in __argv for use later */
sw a2, __environ /* save third arg (environ) for use later */
jal main /* call main */
nop /* delay slot */
/*
* Now, we have the return value of main in v0.
*
* Move it to s0 (which is callee-save) so we still have
* it in case exit() returns.
*
* Also move it to a0 so it's the argument to exit.
*/
move s0, v0 /* save return value */
1:
jal exit /* call exit() */
move a0, s0 /* Set argument (in delay slot) */
/*
* exit() does not return. (Even if _exit() is not implemented
* so *it* returns, exit() is supposed to take care of that.)
* But just in case, loop and try again.
*/
b 1b /* loop back */
nop /* delay slot */
.end __start

View File

@@ -0,0 +1,55 @@
#
# Makefile for hostcompat library
#
# defs.mk contains two special settings for us.
#
# COMPAT_CFLAGS contains our configuration cflags.
# COMPAT_TARGETS are additional targets to run at install time.
#
# COMPAT_CFLAGS may include any of the following:
#
# -DNEED_ERR Compile err, errx, etc.
# -DNEED_NTOHLL Compile ntohll and htonll
#
# COMPAT_TARGETS may include any of the following:
#
# install-errh Install an <err.h>
#
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
LIB=hostcompat
SRCS=err.c ntohll.c time.c hostcompat.c
HOST_CFLAGS+=$(COMPAT_CFLAGS)
MKDIRS=$(INSTALLTOP)/hostinclude
.include "$(TOP)/mk/os161.hostlib.mk"
# XXX: installheaders.sh should be made flexible enough to be used here
includes: $(INSTALLTOP)/hostinclude .WAIT $(COMPAT_TARGETS)
@if diff hostcompat.h \
$(INSTALLTOP)/hostinclude/hostcompat.h > /dev/null 2>&1; then \
:; \
else \
echo cp hostcompat.h $(INSTALLTOP)/hostinclude/; \
cp hostcompat.h $(INSTALLTOP)/hostinclude/; \
fi
[ -h $(INSTALLTOP)/hostinclude/kern ] || \
ln -sf ../include/kern $(INSTALLTOP)/hostinclude/kern
install-errh:
@if diff host-err.h \
$(INSTALLTOP)/hostinclude/err.h > /dev/null 2>&1; then \
:; \
else \
echo cp host-err.h $(INSTALLTOP)/hostinclude/err.h; \
cp host-err.h $(INSTALLTOP)/hostinclude/err.h; \
fi
# Recompile these if the config changes, as they might change with it
err.o ntohll.o: $(TOP)/defs.mk
.PHONY: includes install-errh

View File

@@ -0,0 +1,167 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* 4.4BSD error printing functions.
*/
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "host-err.h"
#ifdef NEED_ERR
/*
* This is initialized by hostcompat_init
*/
extern const char *hostcompat_progname;
/*
* Common routine for all the *err* and *warn* functions.
*/
static
void
hostcompat_printerr(int use_errno, const char *fmt, va_list ap)
{
const char *errmsg;
/*
* Get the error message for the current errno.
* Do this early, before doing anything that might change the
* value in errno.
*/
errmsg = strerror(errno);
/*
* Look up the program name.
* Strictly speaking we should pull off the rightmost
* path component of argv[0] and use that as the program
* name (this is how BSD err* prints) but it doesn't make
* much difference.
*/
if (hostcompat_progname != NULL) {
fprintf(stderr, "%s: ", hostcompat_progname);
}
else {
fprintf(stderr, "libhostcompat: hostcompat_init not called\n");
fprintf(stderr, "libhostcompat-program: ");
}
/* process the printf format and args */
vfprintf(stderr, fmt, ap);
if (use_errno) {
/* if we're using errno, print the error string from above. */
fprintf(stderr, ": %s\n", errmsg);
}
else {
/* otherwise, just a newline. */
fprintf(stderr, "\n");
}
}
/*
* The va_list versions of the warn/err functions.
*/
/* warn/vwarn: use errno, don't exit */
void
vwarn(const char *fmt, va_list ap)
{
hostcompat_printerr(1, fmt, ap);
}
/* warnx/vwarnx: don't use errno, don't exit */
void
vwarnx(const char *fmt, va_list ap)
{
hostcompat_printerr(0, fmt, ap);
}
/* err/verr: use errno, then exit */
void
verr(int exitcode, const char *fmt, va_list ap)
{
hostcompat_printerr(1, fmt, ap);
exit(exitcode);
}
/* errx/verrx: don't use errno, but do then exit */
void
verrx(int exitcode, const char *fmt, va_list ap)
{
hostcompat_printerr(0, fmt, ap);
exit(exitcode);
}
/*
* The non-va_list versions of the warn/err functions.
* Just hand off to the va_list versions.
*/
void
warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
}
void
warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
void
err(int exitcode, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verr(exitcode, fmt, ap);
va_end(ap);
}
void
errx(int exitcode, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrx(exitcode, fmt, ap);
va_end(ap);
}
#endif /* NEED_ERR */

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef HOSTCOMPAT_ERR_H
#define HOSTCOMPAT_ERR_H
#include <stdarg.h>
/*
* 4.4BSD error-printing functions.
*
* These print the program name and the supplied message, and
* (non-*x versions only) the string for the error currently
* stored in "errno", and a newline. The err* versions then
* exit with the supplied exitcode.
*
* The v* versions are to the non-v* versions like vprintf is to
* printf.
*/
void warn(const char *fmt, ...);
void warnx(const char *fmt, ...);
void err(int exitcode, const char *fmt, ...);
void errx(int exitcode, const char *fmt, ...);
void vwarn(const char *fmt, va_list);
void vwarnx(const char *fmt, va_list);
void verr(int exitcode, const char *fmt, va_list);
void verrx(int exitcode, const char *fmt, va_list);
#endif /* HOSTCOMPAT_ERR_H */

View File

@@ -0,0 +1,238 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <termios.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include "hostcompat.h"
/*
* The program name.
* This is used in err.c.
*/
const char *hostcompat_progname = NULL;
/*
* Unix tty state, for when we're running and to put it back the way it was,
* respectively.
*/
static struct termios hostcompat_runtios;
static struct termios hostcompat_savetios;
/*
* Put the tty state back the way it was.
*/
static
void
hostcompat_ttyreset(void)
{
tcsetattr(STDIN_FILENO, TCSADRAIN, &hostcompat_savetios);
}
/*
* Set the tty state back to the way we want it for running.
*/
static
void
hostcompat_ttyresume(void)
{
tcsetattr(STDIN_FILENO, TCSADRAIN, &hostcompat_runtios);
}
/*
* Set up the tty state stuff.
*/
static
int
hostcompat_ttysetup(void)
{
struct termios tios;
/* Get the current tty state. */
if (tcgetattr(STDIN_FILENO, &tios) < 0) {
/* stdin is not a tty */
return -1;
}
hostcompat_savetios = tios;
/* Turn off canonical ("cooked") input. */
tios.c_lflag &= ~ICANON;
/*
* With canonical input off, this says how many characters must be
* typed before read() will return.
*/
tios.c_cc[VMIN] = 1;
/* This can be used to set up read timeouts, but we don't need that. */
tios.c_cc[VTIME] = 0;
/* Turn off echoing of keypresses. */
tios.c_lflag &= ~(ECHO|ECHONL|ECHOCTL);
/* Do not support XON/XOFF flow control. */
tios.c_iflag &= ~(IXON|IXOFF);
/* On input, we want no CR/LF translation. */
tios.c_iflag &= ~(INLCR|IGNCR|ICRNL);
/* However, on output we want LF ('\n') mapped to CRLF. */
#ifdef OCRNL /* missing on OS X */
tios.c_oflag &= ~(OCRNL);
#endif
tios.c_oflag |= OPOST|ONLCR;
/* Enable keyboard signals (^C, ^Z, etc.) because they're useful. */
tios.c_lflag |= ISIG;
/* Set the new tty state. */
hostcompat_runtios = tios;
tcsetattr(STDIN_FILENO, TCSADRAIN, &tios);
return 0;
}
/*
* Signal handler for all the fatal signals (SIGSEGV, SIGTERM, etc.)
*/
static
void
hostcompat_die(int sig)
{
/* Set the tty back to the way we found it */
hostcompat_ttyreset();
/* Make sure the default action will occur when we get another signal*/
signal(sig, SIG_DFL);
/* Post the signal back to ourselves, to cause the right exit status.*/
kill(getpid(), sig);
/* Just in case. */
_exit(255);
}
/*
* Signal handler for the stop signals (SIGTSTP, SIGTTIN, etc.)
*/
static
void
hostcompat_stop(int sig)
{
/* Set the tty back to the way we found it */
hostcompat_ttyreset();
/* Make sure the default action will occur when we get another signal*/
signal(sig, SIG_DFL);
/* Post the signal back to ourselves. */
kill(getpid(), sig);
}
/*
* Signal handler for SIGCONT.
*/
static
void
hostcompat_cont(int sig)
{
(void)sig;
/* Set the tty to the way we want it for running. */
hostcompat_ttyresume();
/*
* Reload the signal handlers for stop/continue signals, in case
* they were set up with one-shot signals.
*/
signal(SIGTTIN, hostcompat_stop);
signal(SIGTTOU, hostcompat_stop);
signal(SIGTSTP, hostcompat_stop);
signal(SIGCONT, hostcompat_cont);
}
/*
* Initialize the hostcompat library.
*/
void
hostcompat_init(int argc, char *argv[])
{
/* Set the program name */
if (argc > 0 && argv[0] != NULL) {
hostcompat_progname = argv[0];
}
/* Set the tty modes */
if (hostcompat_ttysetup() < 0) {
return;
}
/* When exit() is called, clean up */
atexit(hostcompat_ttyreset);
/* stdout/stderr should be unbuffered */
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
/* Catch all the fatal signals, so we can clean up */
signal(SIGHUP, hostcompat_die);
signal(SIGINT, hostcompat_die);
signal(SIGQUIT, hostcompat_die);
signal(SIGILL, hostcompat_die);
signal(SIGTRAP, hostcompat_die);
signal(SIGABRT, hostcompat_die);
#ifdef SIGEMT
signal(SIGEMT, hostcompat_die);
#endif
signal(SIGFPE, hostcompat_die);
signal(SIGBUS, hostcompat_die);
signal(SIGSEGV, hostcompat_die);
signal(SIGSYS, hostcompat_die);
signal(SIGPIPE, hostcompat_die);
signal(SIGALRM, hostcompat_die);
signal(SIGTERM, hostcompat_die);
signal(SIGXCPU, hostcompat_die);
signal(SIGXFSZ, hostcompat_die);
signal(SIGVTALRM, hostcompat_die);
signal(SIGPROF, hostcompat_die);
signal(SIGUSR1, hostcompat_die);
signal(SIGUSR2, hostcompat_die);
/* Catch the stop signals, so we can adjust the tty */
signal(SIGTTIN, hostcompat_stop);
signal(SIGTTOU, hostcompat_stop);
signal(SIGTSTP, hostcompat_stop);
/* Catch the continue signal, so we can adjust the tty */
signal(SIGCONT, hostcompat_cont);
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <stdint.h>
void hostcompat_init(int argc, char **argv);
time_t __time(time_t *secs, unsigned long *nsecs);
#ifdef DECLARE_NTOHLL
uint64_t ntohll(uint64_t);
#define htonll(x) (ntohll(x))
#endif

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2014
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <arpa/inet.h>
#include "hostcompat.h"
#ifdef NEED_NTOHLL
uint64_t
ntohll(uint64_t x)
{
uint32_t x0, x1, y0, y1;
if (ntohl(1) == 1) {
return x;
}
x0 = x & 0xffffffff;
y0 = ntohl(x0);
x1 = x >> 32;
y1 = ntohl(x1);
return ((uint64_t)y0 << 32) | y1;
}
#endif

View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* OS/161 __time implementation in terms of Unix gettimeofday().
*/
#include <sys/types.h>
#include <sys/time.h>
#include <string.h> /* sometimes required for NULL */
#include "hostcompat.h"
time_t
__time(time_t *secs, unsigned long *nsecs)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) {
return -1;
}
if (secs) {
*secs = tv.tv_sec;
}
if (nsecs) {
*nsecs = tv.tv_usec * 1000;
}
return tv.tv_sec;
}

132
userland/lib/libc/Makefile Normal file
View File

@@ -0,0 +1,132 @@
#
# Makefile for OS/161 C standard library
#
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
COMMON=$(TOP)/common/libc
# printf
SRCS+=\
$(COMMON)/printf/__printf.c \
$(COMMON)/printf/snprintf.c
# stdio
SRCS+=\
stdio/__puts.c \
stdio/getchar.c \
stdio/printf.c \
stdio/putchar.c \
stdio/puts.c
# stdlib
SRCS+=\
stdlib/abort.c \
$(COMMON)/stdlib/atoi.c \
stdlib/exit.c \
stdlib/getenv.c \
stdlib/malloc.c \
stdlib/qsort.c \
stdlib/random.c \
stdlib/system.c
# string
SRCS+=\
$(COMMON)/string/bzero.c \
string/memcmp.c \
$(COMMON)/string/memcpy.c \
$(COMMON)/string/memmove.c \
$(COMMON)/string/memset.c \
$(COMMON)/string/strcat.c \
$(COMMON)/string/strchr.c \
$(COMMON)/string/strcmp.c \
$(COMMON)/string/strcpy.c \
string/strerror.c \
$(COMMON)/string/strlen.c \
$(COMMON)/string/strrchr.c \
string/strtok.c \
$(COMMON)/string/strtok_r.c
# time
SRCS+=\
time/time.c
# system call stubs
SRCS+=\
$(MYBUILDDIR)/syscalls.S
# gcc support
COMMONGCC=$(TOP)/common/gcc-millicode
SRCS+=\
$(COMMONGCC)/adddi3.c \
$(COMMONGCC)/anddi3.c \
$(COMMONGCC)/ashldi3.c \
$(COMMONGCC)/ashrdi3.c \
$(COMMONGCC)/cmpdi2.c \
$(COMMONGCC)/divdi3.c \
$(COMMONGCC)/iordi3.c \
$(COMMONGCC)/lshldi3.c \
$(COMMONGCC)/lshrdi3.c \
$(COMMONGCC)/moddi3.c \
$(COMMONGCC)/muldi3.c \
$(COMMONGCC)/negdi2.c \
$(COMMONGCC)/notdi2.c \
$(COMMONGCC)/qdivrem.c \
$(COMMONGCC)/subdi3.c \
$(COMMONGCC)/ucmpdi2.c \
$(COMMONGCC)/udivdi3.c \
$(COMMONGCC)/umoddi3.c \
$(COMMONGCC)/xordi3.c
# other stuff
SRCS+=\
unix/__assert.c \
unix/err.c \
unix/errno.c \
unix/execvp.c \
unix/getcwd.c \
$(COMMON)/arch/mips/setjmp.S
# Name of the library.
LIB=c
# Let the templates do most of the work.
.include "$(TOP)/mk/os161.lib.mk"
#
# Generate syscall entry points from system call list.
#
# Note that this will bomb if the kernel headers haven't been
# installed into the staging area.
#
SYSCALL_H=$(INSTALLTOP)/include/kern/syscall.h
# This is not ideal as it won't rebuild syscalls.S if defs.mk is removed.
# But it's better than failing if defs.mk is not present.
.if exists($(TOP)/defs.mk)
$(MYBUILDDIR)/syscalls.S: $(TOP)/defs.mk
.endif
$(MYBUILDDIR)/syscalls.S: $(SYSCALL_H)
$(MYBUILDDIR)/syscalls.S: syscalls/gensyscalls.sh
$(MYBUILDDIR)/syscalls.S: arch/$(MACHINE)/syscalls-$(MACHINE).S
-rm -f $@ $@.tmp
echo '/* Automatically generated; do not edit */' > $@.tmp
cat arch/$(MACHINE)/syscalls-$(MACHINE).S >> $@.tmp
syscalls/gensyscalls.sh < $(SYSCALL_H) >> $@.tmp
mv -f $@.tmp $@
clean: cleanhere
cleanhere:
rm -f $(MYBUILDDIR)/syscalls.S
predepend:
$(MAKE) $(MYBUILDDIR)/syscalls.S
.PHONY: clean cleanhere depend predepend
# Have the machine-dependent stuff depend on defs.mk in case MACHINE
# or PLATFORM changes.
setjmp.o: $(TOP)/defs.mk

View File

@@ -0,0 +1,95 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* This file is copied to syscalls.S, and then the actual syscalls are
* appended as lines of the form
* SYSCALL(symbol, number)
*
* Warning: gccs before 3.0 run cpp in -traditional mode on .S files.
* So if you use an older gcc you'll need to change the token pasting
* in SYSCALL().
*/
#include <kern/syscall.h>
#include <machine/regdefs.h>
/*
* Definition for each syscall.
* All we do is load the syscall number into v0, the register the
* kernel expects to find it in, and jump to the shared syscall code.
* (Note that the addiu instruction is in the jump's delay slot.)
*/
#define SYSCALL(sym, num) \
.set noreorder ; \
.globl sym ; \
.type sym,@function ; \
.ent sym ; \
sym: ; \
j __syscall ; \
addiu v0, $0, SYS_##sym ; \
.end sym ; \
.set reorder
/*
* Now, the shared system call code.
* The MIPS syscall ABI is as follows:
*
* On entry, call number in v0. The rest is like a normal function
* call: four args in a0-a3, the other args on the stack.
*
* On successful return, zero in a3 register; return value in v0
* (v0 and v1 for a 64-bit return value).
*
* On error return, nonzero in a3 register; errno value in v0.
*
* The use of a3 as a return register to hold the success flag is
* gross, but I didn't make it up.
*
* Note that by longstanding Unix convention and POSIX decree, errno
* is not to be set unless the call actually fails.
*/
.set noreorder
.text
.type __syscall,@function
.ent __syscall
__syscall:
syscall /* make system call */
beq a3, $0, 1f /* if a3 is zero, call succeeded */
nop /* delay slot */
sw v0, errno /* call failed: store errno */
li v1, -1 /* and force return value to -1 */
li v0, -1
1:
j ra /* return */
nop /* delay slot */
.end __syscall
.set reorder

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2015
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/*
* Nonstandard (hence the __) version of puts that doesn't append
* a newline.
*
* Returns the length of the string printed.
*/
int
__puts(const char *str)
{
size_t len;
ssize_t ret;
len = strlen(str);
ret = write(STDOUT_FILENO, str, len);
if (ret == -1) {
return EOF;
}
return len;
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <unistd.h>
/*
* C standard I/O function - read character from stdin
* and return it or the symbolic constant EOF (-1).
*/
int
getchar(void)
{
char ch;
int len;
len = read(STDIN_FILENO, &ch, 1);
if (len<=0) {
/* end of file or error */
return EOF;
}
/*
* Cast through unsigned char, to prevent sign extension. This
* sends back values on the range 0-255, rather than -128 to 127,
* so EOF can be distinguished from legal input.
*/
return (int)(unsigned char)ch;
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2015
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
/*
* printf - C standard I/O function.
*/
/*
* Function passed to __vprintf to do the actual output.
*/
static
void
__printf_send(void *mydata, const char *data, size_t len)
{
ssize_t ret;
int *err = mydata;
ret = write(STDOUT_FILENO, data, len);
*err = (ret == -1) ? errno : 0;
}
/* printf: hand off to vprintf */
int
printf(const char *fmt, ...)
{
int chars;
va_list ap;
va_start(ap, fmt);
chars = vprintf(fmt, ap);
va_end(ap);
return chars;
}
/* vprintf: call __vprintf to do the work. */
int
vprintf(const char *fmt, va_list ap)
{
int chars, err;
chars = __vprintf(__printf_send, &err, fmt, ap);
if (err) {
errno = err;
return -1;
}
return chars;
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <unistd.h>
/*
* C standard function - print a single character.
*
* Properly, stdio is supposed to be buffered, but for present purposes
* writing that code is not really worthwhile.
*/
int
putchar(int ch)
{
char c = ch;
int len;
len = write(STDOUT_FILENO, &c, 1);
if (len<=0) {
return EOF;
}
return ch;
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
/*
* C standard I/O function - print a string and a newline.
*/
int
puts(const char *s)
{
__puts(s);
putchar('\n');
return 0;
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdlib.h>
#include <unistd.h>
/*
* C standard function: panic exit from a user program.
*
* On most Unix systems, this sends the current process a fatal signal.
* We can't do that (no signals in OS/161) so we just exit with a
* nonzero exit code, skipping any libc cleanup.
*/
void
abort(void)
{
_exit(255);
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdlib.h>
#include <unistd.h>
/*
* C standard function: exit process.
*/
void
exit(int code)
{
/*
* In a more complicated libc, this would call functions registered
* with atexit() before calling the syscall to actually exit.
*/
#ifdef __mips__
/*
* Because gcc knows that _exit doesn't return, if we call it
* directly it will drop any code that follows it. This means
* that if _exit *does* return, as happens before it's
* implemented, undefined and usually weird behavior ensues.
*
* As a hack (this is quite gross) do the call by hand in an
* asm block. Then gcc doesn't know what it is, and won't
* optimize the following code out, and we can make sure
* that exit() at least really does not return.
*
* This asm block violates gcc's asm rules by destroying a
* register it doesn't declare ($4, which is a0) but this
* hopefully doesn't matter as the only local it can lose
* track of is "code" and we don't use it afterwards.
*/
__asm volatile("jal _exit;" /* call _exit */
"move $4, %0" /* put code in a0 (delay slot) */
: /* no outputs */
: "r" (code)); /* code is an input */
/*
* Ok, exiting doesn't work; see if we can get our process
* killed by making an illegal memory access. Use a magic
* number address so the symptoms are recognizable and
* unlikely to occur by accident otherwise.
*/
__asm volatile("li $2, 0xeeeee00f;" /* load magic addr into v0 */
"lw $2, 0($2)" /* fetch from it */
:: ); /* no args */
#else
_exit(code);
#endif
/*
* We can't return; so if we can't exit, the only other choice
* is to loop.
*/
while (1) { }
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (c) 2013
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
/*
* getenv(): ANSI C
*
* Get an environment variable.
*/
/*
* This is initialized by crt0, though it actually lives in errno.c
*/
extern char **__environ;
/*
* This is what we use by default if the kernel didn't supply an
* environment.
*/
static const char *__default_environ[] = {
"PATH=/bin:/sbin:/testbin",
"SHELL=/bin/sh",
"TERM=vt220",
NULL
};
char *
getenv(const char *var)
{
size_t varlen, thislen;
char *s;
unsigned i;
if (__environ == NULL) {
__environ = (char **)__default_environ;
}
varlen = strlen(var);
for (i=0; __environ[i] != NULL; i++) {
s = strchr(__environ[i], '=');
if (s == NULL) {
/* ? */
continue;
}
thislen = s - __environ[i];
if (thislen == varlen && !memcmp(__environ[i], var, thislen)) {
return s + 1;
}
}
return NULL;
}

View File

@@ -0,0 +1,591 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2014
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* User-level malloc and free implementation.
*
* This is a basic first-fit allocator. It's intended to be simple and
* easy to follow. It performs abysmally if the heap becomes larger than
* physical memory. To get (much) better out-of-core performance, port
* the kernel's malloc. :-)
*/
#include <stdlib.h>
#include <stdint.h> // for uintptr_t on non-OS/161 platforms
#include <unistd.h>
#include <err.h>
#include <assert.h>
#undef MALLOCDEBUG
#if defined(__mips__) || defined(__i386__)
#define MALLOC32
#elif defined(__alpha__) || defined(__x86_64__)
#define MALLOC64
#else
#error "please fix me"
#endif
/*
* malloc block header.
*
* mh_prevblock is the downwards offset to the previous header, 0 if this
* is the bottom of the heap.
*
* mh_nextblock is the upwards offset to the next header.
*
* mh_pad is unused.
* mh_inuse is 1 if the block is in use, 0 if it is free.
* mh_magic* should always be a fixed value.
*
* MBLOCKSIZE should equal sizeof(struct mheader) and be a power of 2.
* MBLOCKSHIFT is the log base 2 of MBLOCKSIZE.
* MMAGIC is the value for mh_magic*.
*/
struct mheader {
#if defined(MALLOC32)
#define MBLOCKSIZE 8
#define MBLOCKSHIFT 3
#define MMAGIC 2
/*
* 32-bit platform. size_t is 32 bits (4 bytes).
* Block size is 8 bytes.
*/
unsigned mh_prevblock:29;
unsigned mh_pad:1;
unsigned mh_magic1:2;
unsigned mh_nextblock:29;
unsigned mh_inuse:1;
unsigned mh_magic2:2;
#elif defined(MALLOC64)
#define MBLOCKSIZE 16
#define MBLOCKSHIFT 4
#define MMAGIC 6
/*
* 64-bit platform. size_t is 64 bits (8 bytes)
* Block size is 16 bytes.
*/
unsigned mh_prevblock:60;
unsigned mh_pad:1;
unsigned mh_magic1:3;
unsigned mh_nextblock:60;
unsigned mh_inuse:1;
unsigned mh_magic2:3;
#else
#error "please fix me"
#endif
};
/*
* Operator macros on struct mheader.
*
* M_NEXT/PREVOFF: return offset to next/previous header
* M_NEXT/PREV: return next/previous header
*
* M_DATA: return data pointer of a header
* M_SIZE: return data size of a header
*
* M_OK: true if the magic values are correct
*
* M_MKFIELD: prepare a value for mh_next/prevblock.
* (value should include the header size)
*/
#define M_NEXTOFF(mh) ((size_t)(((size_t)((mh)->mh_nextblock))<<MBLOCKSHIFT))
#define M_PREVOFF(mh) ((size_t)(((size_t)((mh)->mh_prevblock))<<MBLOCKSHIFT))
#define M_NEXT(mh) ((struct mheader *)(((char*)(mh))+M_NEXTOFF(mh)))
#define M_PREV(mh) ((struct mheader *)(((char*)(mh))-M_PREVOFF(mh)))
#define M_DATA(mh) ((void *)((mh)+1))
#define M_SIZE(mh) (M_NEXTOFF(mh)-MBLOCKSIZE)
#define M_OK(mh) ((mh)->mh_magic1==MMAGIC && (mh)->mh_magic2==MMAGIC)
#define M_MKFIELD(off) ((off)>>MBLOCKSHIFT)
/*
* System page size. In POSIX you're supposed to call
* sysconf(_SC_PAGESIZE). If _SC_PAGESIZE isn't defined, as on OS/161,
* assume 4K.
*/
#ifdef _SC_PAGESIZE
static size_t __malloc_pagesize;
#define PAGE_SIZE __malloc_pagesize
#else
#define PAGE_SIZE 4096
#endif
////////////////////////////////////////////////////////////
/*
* Static variables - the bottom and top addresses of the heap.
*/
static uintptr_t __heapbase, __heaptop;
/*
* Setup function.
*/
static
void
__malloc_init(void)
{
void *x;
/*
* Check various assumed properties of the sizes.
*/
if (sizeof(struct mheader) != MBLOCKSIZE) {
errx(1, "malloc: Internal error - MBLOCKSIZE wrong");
}
if ((MBLOCKSIZE & (MBLOCKSIZE-1))!=0) {
errx(1, "malloc: Internal error - MBLOCKSIZE not power of 2");
}
if (1<<MBLOCKSHIFT != MBLOCKSIZE) {
errx(1, "malloc: Internal error - MBLOCKSHIFT wrong");
}
/* init should only be called once. */
if (__heapbase!=0 || __heaptop!=0) {
errx(1, "malloc: Internal error - bad init call");
}
/* Get the page size, if needed. */
#ifdef _SC_PAGESIZE
__malloc_pagesize = sysconf(_SC_PAGESIZE);
#endif
/* Use sbrk to find the base of the heap. */
x = sbrk(0);
if (x==(void *)-1) {
err(1, "malloc: initial sbrk failed");
}
if (x==(void *) 0) {
errx(1, "malloc: Internal error - heap began at 0");
}
__heapbase = __heaptop = (uintptr_t)x;
/*
* Make sure the heap base is aligned the way we want it.
* (On OS/161, it will begin on a page boundary. But on
* an arbitrary Unix, it may not be, as traditionally it
* begins at _end.)
*/
if (__heapbase % MBLOCKSIZE != 0) {
size_t adjust = MBLOCKSIZE - (__heapbase % MBLOCKSIZE);
x = sbrk(adjust);
if (x==(void *)-1) {
err(1, "malloc: sbrk failed aligning heap base");
}
if ((uintptr_t)x != __heapbase) {
err(1, "malloc: heap base moved during init");
}
#ifdef MALLOCDEBUG
warnx("malloc: adjusted heap base upwards by %lu bytes",
(unsigned long) adjust);
#endif
__heapbase += adjust;
__heaptop = __heapbase;
}
}
////////////////////////////////////////////////////////////
#ifdef MALLOCDEBUG
/*
* Debugging print function to iterate and dump the entire heap.
*/
static
void
__malloc_dump(void)
{
struct mheader *mh;
uintptr_t i;
size_t rightprevblock;
warnx("heap: ************************************************");
rightprevblock = 0;
for (i=__heapbase; i<__heaptop; i += M_NEXTOFF(mh)) {
mh = (struct mheader *) i;
if (!M_OK(mh)) {
errx(1, "malloc: Heap corrupt; header at 0x%lx"
" has bad magic bits",
(unsigned long) i);
}
if (mh->mh_prevblock != rightprevblock) {
errx(1, "malloc: Heap corrupt; header at 0x%lx"
" has bad previous-block size %lu "
"(should be %lu)",
(unsigned long) i,
(unsigned long) mh->mh_prevblock << MBLOCKSHIFT,
(unsigned long) rightprevblock << MBLOCKSHIFT);
}
rightprevblock = mh->mh_nextblock;
warnx("heap: 0x%lx 0x%-6lx (next: 0x%lx) %s",
(unsigned long) i + MBLOCKSIZE,
(unsigned long) M_SIZE(mh),
(unsigned long) (i+M_NEXTOFF(mh)),
mh->mh_inuse ? "INUSE" : "FREE");
}
if (i!=__heaptop) {
errx(1, "malloc: Heap corrupt; ran off end");
}
warnx("heap: ************************************************");
}
#endif /* MALLOCDEBUG */
////////////////////////////////////////////////////////////
/*
* Get more memory (at the top of the heap) using sbrk, and
* return a pointer to it.
*/
static
void *
__malloc_sbrk(size_t size)
{
void *x;
x = sbrk(size);
if (x == (void *)-1) {
return NULL;
}
if ((uintptr_t)x != __heaptop) {
errx(1, "malloc: Internal error - "
"heap top moved itself from 0x%lx to 0x%lx",
(unsigned long) __heaptop,
(unsigned long) (uintptr_t) x);
}
__heaptop += size;
return x;
}
/*
* Make a new (free) block from the block passed in, leaving size
* bytes for data in the current block. size must be a multiple of
* MBLOCKSIZE.
*
* Only split if the excess space is at least twice the blocksize -
* one blocksize to hold a header and one for data.
*/
static
void
__malloc_split(struct mheader *mh, size_t size)
{
struct mheader *mhnext, *mhnew;
size_t oldsize;
if (size % MBLOCKSIZE != 0) {
errx(1, "malloc: Internal error (size %lu passed to split)",
(unsigned long) size);
}
if (M_SIZE(mh) - size < 2*MBLOCKSIZE) {
/* no room */
return;
}
mhnext = M_NEXT(mh);
oldsize = M_SIZE(mh);
mh->mh_nextblock = M_MKFIELD(size + MBLOCKSIZE);
mhnew = M_NEXT(mh);
if (mhnew==mhnext) {
errx(1, "malloc: Internal error (split screwed up?)");
}
mhnew->mh_prevblock = M_MKFIELD(size + MBLOCKSIZE);
mhnew->mh_pad = 0;
mhnew->mh_magic1 = MMAGIC;
mhnew->mh_nextblock = M_MKFIELD(oldsize - size);
mhnew->mh_inuse = 0;
mhnew->mh_magic2 = MMAGIC;
if (mhnext != (struct mheader *) __heaptop) {
mhnext->mh_prevblock = mhnew->mh_nextblock;
}
}
/*
* malloc itself.
*/
void *
malloc(size_t size)
{
struct mheader *mh;
uintptr_t i;
size_t rightprevblock;
size_t morespace;
void *p;
if (__heapbase==0) {
__malloc_init();
}
if (__heapbase==0 || __heaptop==0 || __heapbase > __heaptop) {
warnx("malloc: Internal error - local data corrupt");
errx(1, "malloc: heapbase 0x%lx; heaptop 0x%lx",
(unsigned long) __heapbase, (unsigned long) __heaptop);
}
#ifdef MALLOCDEBUG
warnx("malloc: about to allocate %lu (0x%lx) bytes",
(unsigned long) size, (unsigned long) size);
__malloc_dump();
#endif
/* Round size up to an integral number of blocks. */
size = ((size + MBLOCKSIZE - 1) & ~(size_t)(MBLOCKSIZE-1));
/*
* First-fit search algorithm for available blocks.
* Check to make sure the next/previous sizes all agree.
*/
rightprevblock = 0;
mh = NULL;
for (i=__heapbase; i<__heaptop; i += M_NEXTOFF(mh)) {
mh = (struct mheader *) i;
if (!M_OK(mh)) {
errx(1, "malloc: Heap corrupt; header at 0x%lx"
" has bad magic bits",
(unsigned long) i);
}
if (mh->mh_prevblock != rightprevblock) {
errx(1, "malloc: Heap corrupt; header at 0x%lx"
" has bad previous-block size %lu "
"(should be %lu)",
(unsigned long) i,
(unsigned long) mh->mh_prevblock << MBLOCKSHIFT,
(unsigned long) rightprevblock << MBLOCKSHIFT);
}
rightprevblock = mh->mh_nextblock;
/* Can't allocate a block that's in use. */
if (mh->mh_inuse) {
continue;
}
/* Can't allocate a block that isn't big enough. */
if (M_SIZE(mh) < size) {
continue;
}
/* Try splitting block. */
__malloc_split(mh, size);
/*
* Now, allocate.
*/
mh->mh_inuse = 1;
#ifdef MALLOCDEBUG
warnx("malloc: allocating at %p", M_DATA(mh));
__malloc_dump();
#endif
return M_DATA(mh);
}
if (i!=__heaptop) {
errx(1, "malloc: Heap corrupt; ran off end");
}
/*
* Didn't find anything. Expand the heap.
*
* If the heap is nonempty and the top block (the one mh is
* left pointing to after the above loop) is free, we can
* expand it. Otherwise we need a new block.
*/
if (mh != NULL && !mh->mh_inuse) {
assert(size > M_SIZE(mh));
morespace = size - M_SIZE(mh);
}
else {
morespace = MBLOCKSIZE + size;
}
/* Round the amount of space we ask for up to a whole page. */
morespace = PAGE_SIZE * ((morespace + PAGE_SIZE - 1) / PAGE_SIZE);
p = __malloc_sbrk(morespace);
if (p == NULL) {
return NULL;
}
if (mh != NULL && !mh->mh_inuse) {
/* update old header */
mh->mh_nextblock = M_MKFIELD(M_NEXTOFF(mh) + morespace);
mh->mh_inuse = 1;
}
else {
/* fill out new header */
mh = p;
mh->mh_prevblock = rightprevblock;
mh->mh_magic1 = MMAGIC;
mh->mh_magic2 = MMAGIC;
mh->mh_pad = 0;
mh->mh_inuse = 1;
mh->mh_nextblock = M_MKFIELD(morespace);
}
/*
* Either way, try splitting the block we got as because of
* the page rounding it might be quite a bit bigger than we
* needed.
*/
__malloc_split(mh, size);
#ifdef MALLOCDEBUG
warnx("malloc: allocating at %p", M_DATA(mh));
__malloc_dump();
#endif
return M_DATA(mh);
}
////////////////////////////////////////////////////////////
/*
* Clear a range of memory with 0xdeadbeef.
* ptr must be suitably aligned.
*/
static
void
__malloc_deadbeef(void *ptr, size_t size)
{
uint32_t *x = ptr;
size_t i, n = size/sizeof(uint32_t);
for (i=0; i<n; i++) {
x[i] = 0xdeadbeef;
}
}
/*
* Attempt to merge two adjacent blocks (mh below mhnext).
*/
static
void
__malloc_trymerge(struct mheader *mh, struct mheader *mhnext)
{
struct mheader *mhnextnext;
if (mh->mh_nextblock != mhnext->mh_prevblock) {
errx(1, "free: Heap corrupt (%p and %p inconsistent)",
mh, mhnext);
}
if (mh->mh_inuse || mhnext->mh_inuse) {
/* can't merge */
return;
}
mhnextnext = M_NEXT(mhnext);
mh->mh_nextblock = M_MKFIELD(MBLOCKSIZE + M_SIZE(mh) +
MBLOCKSIZE + M_SIZE(mhnext));
if (mhnextnext != (struct mheader *)__heaptop) {
mhnextnext->mh_prevblock = mh->mh_nextblock;
}
/* Deadbeef out the memory used by the now-obsolete header */
__malloc_deadbeef(mhnext, sizeof(struct mheader));
}
/*
* The actual free() implementation.
*/
void
free(void *x)
{
struct mheader *mh, *mhnext, *mhprev;
if (x==NULL) {
/* safest practice */
return;
}
/* Consistency check. */
if (__heapbase==0 || __heaptop==0 || __heapbase > __heaptop) {
warnx("free: Internal error - local data corrupt");
errx(1, "free: heapbase 0x%lx; heaptop 0x%lx",
(unsigned long) __heapbase, (unsigned long) __heaptop);
}
/* Don't allow freeing pointers that aren't on the heap. */
if ((uintptr_t)x < __heapbase || (uintptr_t)x >= __heaptop) {
errx(1, "free: Invalid pointer %p freed (out of range)", x);
}
#ifdef MALLOCDEBUG
warnx("free: about to free %p", x);
__malloc_dump();
#endif
mh = ((struct mheader *)x)-1;
if (!M_OK(mh)) {
errx(1, "free: Invalid pointer %p freed (corrupt header)", x);
}
if (!mh->mh_inuse) {
errx(1, "free: Invalid pointer %p freed (already free)", x);
}
/* mark it free */
mh->mh_inuse = 0;
/* wipe it */
__malloc_deadbeef(M_DATA(mh), M_SIZE(mh));
/* Try merging with the block above (but not if we're at the top) */
mhnext = M_NEXT(mh);
if (mhnext != (struct mheader *)__heaptop) {
__malloc_trymerge(mh, mhnext);
}
/* Try merging with the block below (but not if we're at the bottom) */
if (mh != (struct mheader *)__heapbase) {
mhprev = M_PREV(mh);
__malloc_trymerge(mhprev, mh);
}
#ifdef MALLOCDEBUG
warnx("free: freed %p", x);
__malloc_dump();
#endif
}

View File

@@ -0,0 +1,145 @@
/*
* Copyright (c) 2014
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/*
* qsort() for OS/161, where it isn't in libc.
*/
void
qsort(void *vdata, unsigned num, size_t size,
int (*f)(const void *, const void *))
{
unsigned pivot, head, tail;
char *data = vdata;
char tmp[size];
#define COMPARE(aa, bb) \
((aa) == (bb) ? 0 : f(data + (aa) * size, data + (bb) * size))
#define EXCHANGE(aa, bb) \
memcpy(tmp, data + (aa) * size, size); \
memcpy(data + (aa) * size, data + (bb) * size, size); \
memcpy(data + (bb) * size, tmp, size)
if (num <= 1) {
return;
}
if (num == 2) {
if (COMPARE(0, 1) > 0) {
EXCHANGE(0, 1);
return;
}
}
/*
* 1. Pick a pivot value. For simplicity, always use the
* middle of the array.
*/
pivot = num / 2;
/*
* 2. Shift all values less than or equal to the pivot value
* to the front of the array.
*/
head = 0;
tail = num - 1;
while (head < tail) {
if (COMPARE(head, pivot) <= 0) {
head++;
}
else if (COMPARE(tail, pivot) > 0) {
tail--;
}
else {
EXCHANGE(head, tail);
if (pivot == head) {
pivot = tail;
}
else if (pivot == tail) {
pivot = head;
}
head++;
tail--;
}
}
/*
* 3. If there's an even number of elements and we swapped the
* last two, the head and tail indexes will cross. In this
* case the first entry on the tail side is tail+1. If there's
* an odd number of elements, we stop with head == tail, and
* the first entry on the tail side is this value (hence,
* tail) if it's is greater than the pivot value, and the next
* element (hence, tail+1) if it's less than or equal to the
* pivot value.
*
* Henceforth use "tail" to hold the index of the first entry
* of the back portion of the array.
*/
if (head > tail || COMPARE(head, pivot) <= 0) {
tail++;
}
/*
* 4. If we got a bad pivot that gave us only one partition,
* because of the order of the advances in the loop above it
* will always put everything in the front portion of the
* array (so tail == num). This happens if we picked the
* largest value. Move the pivot to the end, if necessary, lop
* off all values equal to it, and recurse on the rest. (If
* there is no rest, the array is already sorted and we're
* done.)
*/
if (tail == num) {
if (pivot < num - 1) {
if (COMPARE(pivot, num - 1) > 0) {
EXCHANGE(pivot, num - 1);
}
}
tail = num - 1;
while (tail > 0 && COMPARE(tail - 1, tail) == 0) {
tail--;
}
if (tail > 0) {
qsort(vdata, tail, size, f);
}
return;
}
assert(tail > 0 && tail < num);
/*
* 5. Recurse on each subpart of the array.
*/
qsort(vdata, tail, size, f);
qsort((char *)vdata + tail * size, num - tail, size, f);
}

View File

@@ -0,0 +1,453 @@
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* From:
* NetBSD: random.c,v 1.19 2000/01/22 22:19:20 mycroft Exp
*
* Hacked gruesomely for OS/161.
*/
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
/*
* For a thread-safe libc, declare a lock for this file and change
* these to be nonempty.
*/
#define LOCKME()
#define UNLOCKME()
static void srandom_unlocked(unsigned long);
static long random_unlocked(void);
/*
* random.c:
*
* An improved random number generation package. In addition to the standard
* rand()/srand() like interface, this package also has a special state info
* interface. The initstate() routine is called with a seed, an array of
* bytes, and a count of how many bytes are being passed in; this array is
* then initialized to contain information for random number generation with
* that much state information. Good sizes for the amount of state
* information are 32, 64, 128, and 256 bytes. The state can be switched by
* calling the setstate() routine with the same array as was initiallized
* with initstate(). By default, the package runs with 128 bytes of state
* information and generates far better random numbers than a linear
* congruential generator. If the amount of state information is less than
* 32 bytes, a simple linear congruential R.N.G. is used.
*
* Internally, the state information is treated as an array of longs; the
* zeroeth element of the array is the type of R.N.G. being used (small
* integer); the remainder of the array is the state information for the
* R.N.G. Thus, 32 bytes of state information will give 7 longs worth of
* state information, which will allow a degree seven polynomial. (Note:
* the zeroeth word of state information also has some other information
* stored in it -- see setstate() for details).
*
* The random number generation technique is a linear feedback shift register
* approach, employing trinomials (since there are fewer terms to sum up that
* way). In this approach, the least significant bit of all the numbers in
* the state table will act as a linear feedback shift register, and will
* have period 2^deg - 1 (where deg is the degree of the polynomial being
* used, assuming that the polynomial is irreducible and primitive). The
* higher order bits will have longer periods, since their values are also
* influenced by pseudo-random carries out of the lower bits. The total
* period of the generator is approximately deg*(2**deg - 1); thus doubling
* the amount of state information has a vast influence on the period of the
* generator. Note: the deg*(2**deg - 1) is an approximation only good for
* large deg, when the period of the shift register is the dominant factor.
* With deg equal to seven, the period is actually much longer than the
* 7*(2**7 - 1) predicted by this formula.
*
* Modified 28 December 1994 by Jacob S. Rosenberg.
* The following changes have been made:
* All references to the type u_int have been changed to unsigned long.
* All references to type int have been changed to type long. Other
* cleanups have been made as well. A warning for both initstate and
* setstate has been inserted to the effect that on Sparc platforms
* the 'arg_state' variable must be forced to begin on word boundaries.
* This can be easily done by casting a long integer array to char *.
* The overall logic has been left STRICTLY alone. This software was
* tested on both a VAX and Sun SpacsStation with exactly the same
* results. The new version and the original give IDENTICAL results.
* The new version is somewhat faster than the original. As the
* documentation says: "By default, the package runs with 128 bytes of
* state information and generates far better random numbers than a linear
* congruential generator. If the amount of state information is less than
* 32 bytes, a simple linear congruential R.N.G. is used." For a buffer of
* 128 bytes, this new version runs about 19 percent faster and for a 16
* byte buffer it is about 5 percent faster.
*/
/*
* For each of the currently supported random number generators, we have a
* break value on the amount of state information (you need at least this
* many bytes of state info to support this random number generator), a degree
* for the polynomial (actually a trinomial) that the R.N.G. is based on, and
* the separation between the two lower order coefficients of the trinomial.
*/
#define TYPE_0 0 /* linear congruential */
#define BREAK_0 8
#define DEG_0 0
#define SEP_0 0
#define TYPE_1 1 /* x**7 + x**3 + 1 */
#define BREAK_1 32
#define DEG_1 7
#define SEP_1 3
#define TYPE_2 2 /* x**15 + x + 1 */
#define BREAK_2 64
#define DEG_2 15
#define SEP_2 1
#define TYPE_3 3 /* x**31 + x**3 + 1 */
#define BREAK_3 128
#define DEG_3 31
#define SEP_3 3
#define TYPE_4 4 /* x**63 + x + 1 */
#define BREAK_4 256
#define DEG_4 63
#define SEP_4 1
/*
* Array versions of the above information to make code run faster --
* relies on fact that TYPE_i == i.
*/
#define MAX_TYPES 5 /* max number of types above */
static const int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
static const int seps[MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
/*
* Initially, everything is set up as if from:
*
* initstate(1, &randtbl, 128);
*
* Note that this initialization takes advantage of the fact that srandom()
* advances the front and rear pointers 10*rand_deg times, and hence the
* rear pointer which starts at 0 will also end up at zero; thus the zeroeth
* element of the state information, which contains info about the current
* position of the rear pointer is just
*
* MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3.
*/
static long randtbl[DEG_3 + 1] = {
TYPE_3,
(long)0x9a319039L, (long)0x32d9c024L, (long)0x9b663182L,
(long)0x5da1f342L, (long)0xde3b81e0L, (long)0xdf0a6fb5L,
(long)0xf103bc02L, (long)0x48f340fbL, (long)0x7449e56bL,
(long)0xbeb1dbb0L, (long)0xab5c5918L, (long)0x946554fdL,
(long)0x8c2e680fL, (long)0xeb3d799fL, (long)0xb11ee0b7L,
(long)0x2d436b86L, (long)0xda672e2aL, (long)0x1588ca88L,
(long)0xe369735dL, (long)0x904f35f7L, (long)0xd7158fd6L,
(long)0x6fa6f051L, (long)0x616e6b96L, (long)0xac94efdcL,
(long)0x36413f93L, (long)0xc622c298L, (long)0xf5a42ab8L,
(long)0x8a88d77bL, (long)0xf5ad9d0eL, (long)0x8999220bL,
(long)0x27fb47b9L,
};
/*
* fptr and rptr are two pointers into the state info, a front and a rear
* pointer. These two pointers are always rand_sep places aparts, as they
* cycle cyclically through the state information. (Yes, this does mean we
* could get away with just one pointer, but the code for random() is more
* efficient this way). The pointers are left positioned as they would be
* from the call
*
* initstate(1, randtbl, 128);
*
* (The position of the rear pointer, rptr, is really 0 (as explained above
* in the initialization of randtbl) because the state table pointer is set
* to point to randtbl[1] (as explained below).
*/
static long *fptr = &randtbl[SEP_3 + 1];
static long *rptr = &randtbl[1];
/*
* The following things are the pointer to the state information table, the
* type of the current generator, the degree of the current polynomial being
* used, and the separation between the two pointers. Note that for efficiency
* of random(), we remember the first location of the state information, not
* the zeroeth. Hence it is valid to access state[-1], which is used to
* store the type of the R.N.G. Also, we remember the last location, since
* this is more efficient than indexing every time to find the address of
* the last element to see if the front and rear pointers have wrapped.
*/
static long *state = &randtbl[1];
static long rand_type = TYPE_3;
static int rand_deg = DEG_3;
static int rand_sep = SEP_3;
static long *end_ptr = &randtbl[DEG_3 + 1];
/*
* srandom:
*
* Initialize the random number generator based on the given seed. If the
* type is the trivial no-state-information type, just remember the seed.
* Otherwise, initializes state[] based on the given "seed" via a linear
* congruential generator. Then, the pointers are set to known locations
* that are exactly rand_sep places apart. Lastly, it cycles the state
* information a given number of times to get rid of any initial dependencies
* introduced by the L.C.R.N.G. Note that the initialization of randtbl[]
* for default usage relies on values produced by this routine.
*/
static
void
srandom_unlocked(unsigned long x)
{
int i;
if (rand_type == TYPE_0)
state[0] = x;
else {
state[0] = x;
for (i = 1; i < rand_deg; i++)
state[i] = 1103515245L * state[i - 1] + 12345L;
fptr = &state[rand_sep];
rptr = &state[0];
for (i = 0; i < 10 * rand_deg; i++)
(void)random_unlocked();
}
}
void
srandom(unsigned long x)
{
LOCKME();
srandom_unlocked(x);
UNLOCKME();
}
/*
* initstate:
*
* Initialize the state information in the given array of n bytes for future
* random number generation. Based on the number of bytes we are given, and
* the break values for the different R.N.G.'s, we choose the best (largest)
* one we can and set things up for it. srandom() is then called to
* initialize the state information.
*
* Note that on return from srandom(), we set state[-1] to be the type
* multiplexed with the current value of the rear pointer; this is so
* successive calls to initstate() won't lose this information and will be
* able to restart with setstate().
*
* Note: the first thing we do is save the current state, if any, just like
* setstate() so that it doesn't matter when initstate is called.
*
* Returns a pointer to the old state.
*
* Note: The Sparc platform requires that arg_state begin on a long
* word boundary; otherwise a bus error will occur. Even so, lint will
* complain about mis-alignment, but you should disregard these messages.
*/
char *
initstate(
unsigned long seed, /* seed for R.N.G. */
char *arg_state, /* pointer to state array */
size_t n) /* # bytes of state info */
{
void *ostate = (void *)(&state[-1]);
long *long_arg_state;
assert(arg_state != NULL);
long_arg_state = (long *)(void *)arg_state;
LOCKME();
if (rand_type == TYPE_0)
state[-1] = rand_type;
else
state[-1] = MAX_TYPES * (rptr - state) + rand_type;
if (n < BREAK_0) {
UNLOCKME();
return (NULL);
} else if (n < BREAK_1) {
rand_type = TYPE_0;
rand_deg = DEG_0;
rand_sep = SEP_0;
} else if (n < BREAK_2) {
rand_type = TYPE_1;
rand_deg = DEG_1;
rand_sep = SEP_1;
} else if (n < BREAK_3) {
rand_type = TYPE_2;
rand_deg = DEG_2;
rand_sep = SEP_2;
} else if (n < BREAK_4) {
rand_type = TYPE_3;
rand_deg = DEG_3;
rand_sep = SEP_3;
} else {
rand_type = TYPE_4;
rand_deg = DEG_4;
rand_sep = SEP_4;
}
state = (long *) (long_arg_state + 1); /* first location */
end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */
srandom_unlocked(seed);
if (rand_type == TYPE_0)
long_arg_state[0] = rand_type;
else
long_arg_state[0] = MAX_TYPES * (rptr - state) + rand_type;
UNLOCKME();
return((char *)ostate);
}
/*
* setstate:
*
* Restore the state from the given state array.
*
* Note: it is important that we also remember the locations of the pointers
* in the current state information, and restore the locations of the pointers
* from the old state information. This is done by multiplexing the pointer
* location into the zeroeth word of the state information.
*
* Note that due to the order in which things are done, it is OK to call
* setstate() with the same state as the current state.
*
* Returns a pointer to the old state information.
*
* Note: The Sparc platform requires that arg_state begin on a long
* word boundary; otherwise a bus error will occur. Even so, lint will
* complain about mis-alignment, but you should disregard these messages.
*/
char *
setstate(char *arg_state) /* pointer to state array */
{
long *new_state;
int type;
int rear;
void *ostate = (void *)(&state[-1]);
assert(arg_state != NULL);
new_state = (long *)(void *)arg_state;
type = (int)(new_state[0] % MAX_TYPES);
rear = (int)(new_state[0] / MAX_TYPES);
LOCKME();
if (rand_type == TYPE_0)
state[-1] = rand_type;
else
state[-1] = MAX_TYPES * (rptr - state) + rand_type;
switch(type) {
case TYPE_0:
case TYPE_1:
case TYPE_2:
case TYPE_3:
case TYPE_4:
rand_type = type;
rand_deg = degrees[type];
rand_sep = seps[type];
break;
default:
UNLOCKME();
return (NULL);
}
state = (long *) (new_state + 1);
if (rand_type != TYPE_0) {
rptr = &state[rear];
fptr = &state[(rear + rand_sep) % rand_deg];
}
end_ptr = &state[rand_deg]; /* set end_ptr too */
UNLOCKME();
return((char *)ostate);
}
/*
* random:
*
* If we are using the trivial TYPE_0 R.N.G., just do the old linear
* congruential bit. Otherwise, we do our fancy trinomial stuff, which is
* the same in all the other cases due to all the global variables that have
* been set up. The basic operation is to add the number at the rear pointer
* into the one at the front pointer. Then both pointers are advanced to
* the next location cyclically in the table. The value returned is the sum
* generated, reduced to 31 bits by throwing away the "least random" low bit.
*
* Note: the code takes advantage of the fact that both the front and
* rear pointers can't wrap on the same call by not testing the rear
* pointer if the front one has wrapped.
*
* Returns a 31-bit random number.
*/
static
long
random_unlocked(void)
{
long i;
long *f, *r;
if (rand_type == TYPE_0) {
i = state[0];
state[0] = i = (i * 1103515245L + 12345L) & 0x7fffffff;
} else {
/*
* Use local variables rather than static variables for speed.
*/
f = fptr; r = rptr;
*f += *r;
/* chucking least random bit */
i = ((unsigned long)*f >> 1) & 0x7fffffff;
if (++f >= end_ptr) {
f = state;
++r;
}
else if (++r >= end_ptr) {
r = state;
}
fptr = f; rptr = r;
}
return(i);
}
long
random(void)
{
long r;
LOCKME();
r = random_unlocked();
UNLOCKME();
return (r);
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
/*
* system(): ANSI C
*
* Run a command.
*/
#define MAXCMDSIZE 2048
#define MAXARGS 128
int
system(const char *cmd)
{
/*
* Ordinarily, you call the shell to process the command.
* But we don't know that the shell can do that. So, do it
* ourselves.
*/
char tmp[MAXCMDSIZE];
char *argv[MAXARGS+1];
int nargs=0;
char *s;
int pid, status;
if (strlen(cmd) >= sizeof(tmp)) {
errno = E2BIG;
return -1;
}
strcpy(tmp, cmd);
for (s = strtok(tmp, " \t"); s; s = strtok(NULL, " \t")) {
if (nargs < MAXARGS) {
argv[nargs++] = s;
}
else {
errno = E2BIG;
return 1;
}
}
argv[nargs] = NULL;
pid = fork();
switch (pid) {
case -1:
return -1;
case 0:
/* child */
execv(argv[0], argv);
/* exec only returns if it fails */
_exit(255);
default:
/* parent */
waitpid(pid, &status, 0);
return status;
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <string.h>
/*
* Standard C string function: compare two memory blocks and return
* their sort order.
*/
int
memcmp(const void *av, const void *bv, size_t len)
{
const unsigned char *a = av;
const unsigned char *b = bv;
size_t i;
for (i=0; i<len; i++) {
if (a[i] != b[i]) {
return (int)(a[i] - b[i]);
}
}
return 0;
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <string.h>
#include <kern/errmsg.h>
/*
* Standard C function to return a string for a given errno.
*/
const char *
strerror(int errcode)
{
if (errcode>=0 && errcode < sys_nerr) {
return sys_errlist[errcode];
}
return "Unknown error number";
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <string.h>
static char *__strtok_context;
char *
strtok(char *str, const char *seps)
{
return strtok_r(str, seps, &__strtok_context);
}

View File

@@ -0,0 +1,25 @@
#!/bin/sh
#
# gensyscalls.sh
# Usage: ./gensyscalls.sh < syscalls.h
#
# Parses the kernel's syscalls.h into the body of syscalls.S
#
# tabs to spaces, just in case
tr '\t' ' ' |\
awk '
# Do not read the parts of the file that are not between the markers.
/^\/\*CALLBEGIN\*\// { look=1; }
/^\/\*CALLEND\*\// { look=0; }
# And, do not read lines that do not match the approximate right pattern.
look && /^#define SYS_/ && NF==3 {
sub("^SYS_", "", $2);
# print the name of the call and the number.
print $2, $3;
}
' | awk '{
# output something simple that will work in syscalls.S.
printf "SYSCALL(%s, %s)\n", $1, $2;
}'

View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
/*
* POSIX C function: retrieve time in seconds since the epoch.
* Uses the OS/161 system call __time, which does the same thing
* but also returns nanoseconds.
*/
time_t
time(time_t *t)
{
return __time(t, NULL);
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
/*
* Function that gets called when an assert() fails.
* Print a message to stderr and bail out of the program.
*/
void
__bad_assert(const char *file, int line, const char *expr)
{
char buf[256];
snprintf(buf, sizeof(buf), "Assertion failed: %s (%s line %d)\n",
expr, file, line);
write(STDERR_FILENO, buf, strlen(buf));
abort();
}

View File

@@ -0,0 +1,191 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include <errno.h>
/*
* 4.4BSD error printing functions.
*/
/*
* This is initialized by crt0, though it actually lives in errno.c
*/
extern char **__argv;
/*
* Routine to print error message text to stderr.
*/
static
void
__senderr(void *junk, const char *data, size_t len)
{
(void)junk; /* not needed or used */
write(STDERR_FILENO, data, len);
}
/*
* Shortcut to call __senderr on a null-terminated string.
* (__senderr is set up to be called by __vprintf.)
*/
static
void
__senderrstr(const char *str)
{
__senderr(NULL, str, strlen(str));
}
/*
* Common routine for all the *err* and *warn* functions.
*/
static
void
__printerr(int use_errno, const char *fmt, va_list ap)
{
const char *errmsg;
const char *prog;
/*
* Get the error message for the current errno.
* Do this early, before doing anything that might change the
* value in errno.
*/
errmsg = strerror(errno);
/*
* Look up the program name.
* Strictly speaking we should pull off the rightmost
* path component of argv[0] and use that as the program
* name (this is how BSD err* prints) but it doesn't make
* much difference.
*/
if (__argv!=NULL && __argv[0]!=NULL) {
prog = __argv[0];
}
else {
prog = "(program name unknown)";
}
/* print the program name */
__senderrstr(prog);
__senderrstr(": ");
/* process the printf format and args */
__vprintf(__senderr, NULL, fmt, ap);
/* if we're using errno, print the error string from above. */
if (use_errno) {
__senderrstr(": ");
__senderrstr(errmsg);
}
/* and always add a newline. */
__senderrstr("\n");
}
/*
* The va_list versions of the warn/err functions.
*/
/* warn/vwarn: use errno, don't exit */
void
vwarn(const char *fmt, va_list ap)
{
__printerr(1, fmt, ap);
}
/* warnx/vwarnx: don't use errno, don't exit */
void
vwarnx(const char *fmt, va_list ap)
{
__printerr(0, fmt, ap);
}
/* err/verr: use errno, then exit */
void
verr(int exitcode, const char *fmt, va_list ap)
{
__printerr(1, fmt, ap);
exit(exitcode);
}
/* errx/verrx: don't use errno, but do then exit */
void
verrx(int exitcode, const char *fmt, va_list ap)
{
__printerr(0, fmt, ap);
exit(exitcode);
}
/*
* The non-va_list versions of the warn/err functions.
* Just hand off to the va_list versions.
*/
void
warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
}
void
warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
void
err(int exitcode, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verr(exitcode, fmt, ap);
va_end(ap);
}
void
errx(int exitcode, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrx(exitcode, fmt, ap);
va_end(ap);
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <errno.h>
/*
* Source file that declares the space for the global variable errno.
*
* We also declare the space for __argv, which is used by the err*
* functions, and __environ, which is used by getenv(). Since these
* are set by crt0, they are always referenced in every program;
* putting them here prevents gratuitously linking all the err* and
* warn* functions (and thus printf) into every program.
*/
char **__argv;
char **__environ;
int errno;

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 2013
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
/*
* POSIX C function: exec a program on the search path. Tries
* execv() repeatedly until one of the choices works.
*/
int
execvp(const char *prog, char *const *args)
{
const char *searchpath, *s, *t;
char progpath[PATH_MAX];
size_t len;
if (strchr(prog, '/') != NULL) {
execv(prog, args);
return -1;
}
searchpath = getenv("PATH");
if (searchpath == NULL) {
errno = ENOENT;
return -1;
}
for (s = searchpath; s != NULL; s = t) {
t = strchr(s, ':');
if (t != NULL) {
len = t - s;
/* advance past the colon */
t++;
}
else {
len = strlen(s);
}
if (len == 0) {
continue;
}
if (len >= sizeof(progpath)) {
continue;
}
memcpy(progpath, s, len);
snprintf(progpath + len, sizeof(progpath) - len, "/%s", prog);
execv(progpath, args);
switch (errno) {
case ENOENT:
case ENOTDIR:
case ENOEXEC:
/* routine errors, try next dir */
break;
default:
/* oops, let's fail */
return -1;
}
}
errno = ENOENT;
return -1;
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <errno.h>
/*
* POSIX C function: retrieve current working directory.
* Uses the system call __getcwd(), which does essentially
* all the work.
*/
char *
getcwd(char *buf, size_t buflen)
{
int r;
if (buflen < 1) {
errno = EINVAL;
return NULL;
}
r = __getcwd(buf, buflen-1);
if (r < 0) {
return NULL;
}
buf[r] = 0;
return buf;
}

View File

@@ -0,0 +1,11 @@
#
# libtest - library of common test support code
#
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
SRCS=triple.c quint.c
LIB=test
.include "$(TOP)/mk/os161.lib.mk"

View File

@@ -0,0 +1,111 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2013
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* quint.c
*
* Runs five copies of some subprogram.
*/
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <test/quint.h>
static
pid_t
spawnv(const char *prog, char **argv)
{
pid_t pid = fork();
switch (pid) {
case -1:
err(1, "fork");
case 0:
/* child */
execv(prog, argv);
err(1, "%s: execv", prog);
default:
/* parent */
break;
}
return pid;
}
static
int
dowait(int index, int pid)
{
int status;
if (waitpid(pid, &status, 0)<0) {
warn("waitpid for copy #%d (pid %d)", index, pid);
return 1;
}
else if (WIFSIGNALED(status)) {
warnx("copy #%d (pid %d): signal %d", index, pid,
WTERMSIG(status));
return 1;
}
else if (WEXITSTATUS(status) != 0) {
warnx("copy #%d (pid %d): exit %d", index, pid,
WEXITSTATUS(status));
return 1;
}
return 0;
}
void
quint(const char *prog)
{
pid_t pids[5];
int i, failures = 0;
char *args[2];
/* set up the argv */
args[0]=(char *)prog;
args[1]=NULL;
warnx("Starting: running five copies of %s...", prog);
for (i=0; i<5; i++) {
pids[i]=spawnv(args[0], args);
}
for (i=0; i<5; i++) {
failures += dowait(i, pids[i]);
}
if (failures > 0) {
warnx("%d failures", failures);
}
else {
warnx("Congratulations! You passed.");
}
}

View File

@@ -0,0 +1,111 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* triple.c
*
* Runs three copies of some subprogram.
*/
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <test/triple.h>
static
pid_t
spawnv(const char *prog, char **argv)
{
pid_t pid = fork();
switch (pid) {
case -1:
err(1, "fork");
case 0:
/* child */
execv(prog, argv);
err(1, "%s: execv", prog);
default:
/* parent */
break;
}
return pid;
}
static
int
dowait(int index, int pid)
{
int status;
if (waitpid(pid, &status, 0)<0) {
warn("waitpid for copy #%d (pid %d)", index, pid);
return 1;
}
else if (WIFSIGNALED(status)) {
warnx("copy #%d (pid %d): signal %d", index, pid,
WTERMSIG(status));
return 1;
}
else if (WEXITSTATUS(status) != 0) {
warnx("copy #%d (pid %d): exit %d", index, pid,
WEXITSTATUS(status));
return 1;
}
return 0;
}
void
triple(const char *prog)
{
pid_t pids[3];
int i, failures = 0;
char *args[2];
/* set up the argv */
args[0]=(char *)prog;
args[1]=NULL;
warnx("Starting: running three copies of %s...", prog);
for (i=0; i<3; i++) {
pids[i]=spawnv(args[0], args);
}
for (i=0; i<3; i++) {
failures += dowait(i, pids[i]);
}
if (failures > 0) {
warnx("%d failures", failures);
}
else {
warnx("Congratulations! You passed.");
}
}

10
userland/sbin/Makefile Normal file
View File

@@ -0,0 +1,10 @@
#
# Makefile for src/sbin (sources for programs installed in /sbin)
#
TOP=../..
.include "$(TOP)/mk/os161.config.mk"
SUBDIRS=reboot halt poweroff mksfs dumpsfs sfsck
.include "$(TOP)/mk/os161.subdir.mk"

View File

@@ -0,0 +1,15 @@
# Makefile for dumpsfs
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=dumpsfs
SRCS=dumpsfs.c ../mksfs/disk.c ../mksfs/support.c
CFLAGS+=-I../mksfs
HOST_CFLAGS+=-I../mksfs
BINDIR=/sbin
HOSTBINDIR=/hostbin
.include "$(TOP)/mk/os161.prog.mk"
.include "$(TOP)/mk/os161.hostprog.mk"

View File

@@ -0,0 +1,607 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2014
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <err.h>
#include "support.h"
#include "kern/sfs.h"
#ifdef HOST
/*
* OS/161 runs natively on a big-endian platform, so we can
* conveniently use the byteswapping functions for network byte order.
*/
#include <netinet/in.h> // for arpa/inet.h
#include <arpa/inet.h> // for ntohl
#include "hostcompat.h"
#define SWAP64(x) ntohll(x)
#define SWAP32(x) ntohl(x)
#define SWAP16(x) ntohs(x)
extern const char *hostcompat_progname;
#else
#define SWAP64(x) (x)
#define SWAP32(x) (x)
#define SWAP16(x) (x)
#endif
#include "disk.h"
#define ARRAYCOUNT(a) (sizeof(a) / sizeof((a)[0]))
#define DIVROUNDUP(a, b) (((a) + (b) - 1) / (b))
static bool dofiles, dodirs;
static bool doindirect;
static bool recurse;
////////////////////////////////////////////////////////////
// printouts
static unsigned dumppos;
static
void
dumpval(const char *desc, const char *val)
{
size_t dlen, vlen, used;
dlen = strlen(desc);
vlen = strlen(val);
printf(" ");
printf("%s: %s", desc, val);
used = dlen + 2 + vlen;
for (; used < 36; used++) {
putchar(' ');
}
if (dumppos % 2 == 1) {
printf("\n");
}
dumppos++;
}
static
void
dumpvalf(const char *desc, const char *valf, ...)
{
va_list ap;
char buf[128];
va_start(ap, valf);
vsnprintf(buf, sizeof(buf), valf, ap);
va_end(ap);
dumpval(desc, buf);
}
static
void
dumplval(const char *desc, const char *lval)
{
if (dumppos % 2 == 1) {
printf("\n");
dumppos++;
}
printf(" %s: %s\n", desc, lval);
dumppos += 2;
}
////////////////////////////////////////////////////////////
// fs structures
static void dumpinode(uint32_t ino, const char *name);
static
uint32_t
readsb(void)
{
struct sfs_superblock sb;
diskread(&sb, SFS_SUPER_BLOCK);
if (SWAP32(sb.sb_magic) != SFS_MAGIC) {
errx(1, "Not an sfs filesystem");
}
return SWAP32(sb.sb_nblocks);
}
static
void
dumpsb(void)
{
struct sfs_superblock sb;
unsigned i;
diskread(&sb, SFS_SUPER_BLOCK);
sb.sb_volname[sizeof(sb.sb_volname)-1] = 0;
printf("Superblock\n");
printf("----------\n");
dumpvalf("Magic", "0x%8x", SWAP32(sb.sb_magic));
dumpvalf("Size", "%u blocks", SWAP32(sb.sb_nblocks));
dumpvalf("Freemap size", "%u blocks",
SFS_FREEMAPBLOCKS(SWAP32(sb.sb_nblocks)));
dumpvalf("Block size", "%u bytes", SFS_BLOCKSIZE);
dumplval("Volume name", sb.sb_volname);
for (i=0; i<ARRAYCOUNT(sb.reserved); i++) {
if (sb.reserved[i] != 0) {
printf(" Word %u in reserved area: 0x%x\n",
i, SWAP32(sb.reserved[i]));
}
}
printf("\n");
}
static
void
dumpfreemap(uint32_t fsblocks)
{
uint32_t freemapblocks = SFS_FREEMAPBLOCKS(fsblocks);
uint32_t i, j, k, bn;
uint8_t data[SFS_BLOCKSIZE], mask;
char tmp[16];
printf("Free block bitmap\n");
printf("-----------------\n");
for (i=0; i<freemapblocks; i++) {
diskread(data, SFS_FREEMAP_START+i);
printf(" Freemap block #%u in disk block %u: blocks %u - %u"
" (0x%x - 0x%x)\n",
i, SFS_FREEMAP_START+i,
i*SFS_BITSPERBLOCK, (i+1)*SFS_BITSPERBLOCK - 1,
i*SFS_BITSPERBLOCK, (i+1)*SFS_BITSPERBLOCK - 1);
for (j=0; j<SFS_BLOCKSIZE; j++) {
if (j % 8 == 0) {
snprintf(tmp, sizeof(tmp), "0x%x",
i*SFS_BITSPERBLOCK + j*8);
printf("%-7s ", tmp);
}
for (k=0; k<8; k++) {
bn = i*SFS_BITSPERBLOCK + j*8 + k;
mask = 1U << k;
if (bn >= fsblocks) {
if (data[j] & mask) {
putchar('x');
}
else {
putchar('!');
}
}
else {
if (data[j] & mask) {
putchar('*');
}
else {
putchar('.');
}
}
}
if (j % 8 == 7) {
printf("\n");
}
else {
printf(" ");
}
}
}
printf("\n");
}
static
void
dumpindirect(uint32_t block)
{
uint32_t ib[SFS_BLOCKSIZE/sizeof(uint32_t)];
char tmp[128];
unsigned i;
if (block == 0) {
return;
}
printf("Indirect block %u\n", block);
diskread(ib, block);
for (i=0; i<ARRAYCOUNT(ib); i++) {
if (i % 4 == 0) {
printf("@%-3u ", i);
}
snprintf(tmp, sizeof(tmp), "%u (0x%x)",
SWAP32(ib[i]), SWAP32(ib[i]));
printf(" %-16s", tmp);
if (i % 4 == 3) {
printf("\n");
}
}
}
static
uint32_t
traverse_ib(uint32_t fileblock, uint32_t numblocks, uint32_t block,
void (*doblock)(uint32_t, uint32_t))
{
uint32_t ib[SFS_BLOCKSIZE/sizeof(uint32_t)];
unsigned i;
if (block == 0) {
memset(ib, 0, sizeof(ib));
}
else {
diskread(ib, block);
}
for (i=0; i<ARRAYCOUNT(ib) && fileblock < numblocks; i++) {
doblock(fileblock++, SWAP32(ib[i]));
}
return fileblock;
}
static
void
traverse(const struct sfs_dinode *sfi, void (*doblock)(uint32_t, uint32_t))
{
uint32_t fileblock;
uint32_t numblocks;
unsigned i;
numblocks = DIVROUNDUP(SWAP32(sfi->sfi_size), SFS_BLOCKSIZE);
fileblock = 0;
for (i=0; i<SFS_NDIRECT && fileblock < numblocks; i++) {
doblock(fileblock++, SWAP32(sfi->sfi_direct[i]));
}
if (fileblock < numblocks) {
fileblock = traverse_ib(fileblock, numblocks,
SWAP32(sfi->sfi_indirect), doblock);
}
assert(fileblock == numblocks);
}
static
void
dumpdirblock(uint32_t fileblock, uint32_t diskblock)
{
struct sfs_direntry sds[SFS_BLOCKSIZE/sizeof(struct sfs_direntry)];
int nsds = SFS_BLOCKSIZE/sizeof(struct sfs_direntry);
int i;
(void)fileblock;
if (diskblock == 0) {
printf(" [block %u - empty]\n", diskblock);
return;
}
diskread(&sds, diskblock);
printf(" [block %u]\n", diskblock);
for (i=0; i<nsds; i++) {
uint32_t ino = SWAP32(sds[i].sfd_ino);
if (ino==SFS_NOINO) {
printf(" [free entry]\n");
}
else {
sds[i].sfd_name[SFS_NAMELEN-1] = 0; /* just in case */
printf(" %u %s\n", ino, sds[i].sfd_name);
}
}
}
static
void
dumpdir(uint32_t ino, const struct sfs_dinode *sfi)
{
int nentries;
nentries = SWAP32(sfi->sfi_size) / sizeof(struct sfs_direntry);
if (SWAP32(sfi->sfi_size) % sizeof(struct sfs_direntry) != 0) {
warnx("Warning: dir size is not a multiple of dir entry size");
}
printf("Directory contents for inode %u: %d entries\n", ino, nentries);
traverse(sfi, dumpdirblock);
}
static
void
recursedirblock(uint32_t fileblock, uint32_t diskblock)
{
struct sfs_direntry sds[SFS_BLOCKSIZE/sizeof(struct sfs_direntry)];
int nsds = SFS_BLOCKSIZE/sizeof(struct sfs_direntry);
int i;
(void)fileblock;
if (diskblock == 0) {
return;
}
diskread(&sds, diskblock);
for (i=0; i<nsds; i++) {
uint32_t ino = SWAP32(sds[i].sfd_ino);
if (ino==SFS_NOINO) {
continue;
}
sds[i].sfd_name[SFS_NAMELEN-1] = 0; /* just in case */
dumpinode(ino, sds[i].sfd_name);
}
}
static
void
recursedir(uint32_t ino, const struct sfs_dinode *sfi)
{
int nentries;
nentries = SWAP32(sfi->sfi_size) / sizeof(struct sfs_direntry);
printf("Reading files in directory %u: %d entries\n", ino, nentries);
traverse(sfi, recursedirblock);
printf("Done with directory %u\n", ino);
}
static
void dumpfileblock(uint32_t fileblock, uint32_t diskblock)
{
uint8_t data[SFS_BLOCKSIZE];
unsigned i, j;
char tmp[128];
if (diskblock == 0) {
printf(" 0x%6x [sparse]\n", fileblock * SFS_BLOCKSIZE);
return;
}
diskread(data, diskblock);
for (i=0; i<SFS_BLOCKSIZE; i++) {
if (i % 16 == 0) {
snprintf(tmp, sizeof(tmp), "0x%x",
fileblock * SFS_BLOCKSIZE + i);
printf("%8s", tmp);
}
if (i % 8 == 0) {
printf(" ");
}
else {
printf(" ");
}
printf("%02x", data[i]);
if (i % 16 == 15) {
printf(" ");
for (j = i-15; j<=i; j++) {
if (data[j] < 32 || data[j] > 126) {
putchar('.');
}
else {
putchar(data[j]);
}
}
printf("\n");
}
}
}
static
void
dumpfile(uint32_t ino, const struct sfs_dinode *sfi)
{
printf("File contents for inode %u:\n", ino);
traverse(sfi, dumpfileblock);
}
static
void
dumpinode(uint32_t ino, const char *name)
{
struct sfs_dinode sfi;
const char *typename;
char tmp[128];
unsigned i;
diskread(&sfi, ino);
printf("Inode %u", ino);
if (name != NULL) {
printf(" (%s)", name);
}
printf("\n");
printf("--------------\n");
switch (SWAP16(sfi.sfi_type)) {
case SFS_TYPE_FILE: typename = "regular file"; break;
case SFS_TYPE_DIR: typename = "directory"; break;
default: typename = "invalid"; break;
}
dumpvalf("Type", "%u (%s)", SWAP16(sfi.sfi_type), typename);
dumpvalf("Size", "%u", SWAP32(sfi.sfi_size));
dumpvalf("Link count", "%u", SWAP16(sfi.sfi_linkcount));
printf("\n");
printf(" Direct blocks:\n");
for (i=0; i<SFS_NDIRECT; i++) {
if (i % 4 == 0) {
printf("@%-2u ", i);
}
/*
* Assume the disk size might be > 64K sectors (which
* would be 32M) but is < 1024K sectors (512M) so we
* need up to 5 hex digits for a block number. And
* assume it's actually < 1 million sectors so we need
* only up to 6 decimal digits. The complete block
* number print then needs up to 16 digits.
*/
snprintf(tmp, sizeof(tmp), "%u (0x%x)",
SWAP32(sfi.sfi_direct[i]), SWAP32(sfi.sfi_direct[i]));
printf(" %-16s", tmp);
if (i % 4 == 3) {
printf("\n");
}
}
if (i % 4 != 0) {
printf("\n");
}
printf(" Indirect block: %u (0x%x)\n",
SWAP32(sfi.sfi_indirect), SWAP32(sfi.sfi_indirect));
for (i=0; i<ARRAYCOUNT(sfi.sfi_waste); i++) {
if (sfi.sfi_waste[i] != 0) {
printf(" Word %u in waste area: 0x%x\n",
i, SWAP32(sfi.sfi_waste[i]));
}
}
if (doindirect) {
dumpindirect(SWAP32(sfi.sfi_indirect));
}
if (SWAP16(sfi.sfi_type) == SFS_TYPE_DIR && dodirs) {
dumpdir(ino, &sfi);
}
if (SWAP16(sfi.sfi_type) == SFS_TYPE_FILE && dofiles) {
dumpfile(ino, &sfi);
}
if (SWAP16(sfi.sfi_type) == SFS_TYPE_DIR && recurse) {
recursedir(ino, &sfi);
}
}
////////////////////////////////////////////////////////////
// main
static
void
usage(void)
{
warnx("Usage: dumpsfs [options] device/diskfile");
warnx(" -s: dump superblock");
warnx(" -b: dump free block bitmap");
warnx(" -i ino: dump specified inode");
warnx(" -I: dump indirect blocks");
warnx(" -f: dump file contents");
warnx(" -d: dump directory contents");
warnx(" -r: recurse into directory contents");
warnx(" -a: equivalent to -sbdfr -i 1");
errx(1, " Default is -i 1");
}
int
main(int argc, char **argv)
{
bool dosb = false;
bool dofreemap = false;
uint32_t dumpino = 0;
const char *dumpdisk = NULL;
int i, j;
uint32_t nblocks;
#ifdef HOST
/* Don't do this; it frobs the tty and you can't pipe to less */
/*hostcompat_init(argc, argv);*/
hostcompat_progname = argv[0];
#endif
for (i=1; i<argc; i++) {
if (argv[i][0] == '-') {
for (j=1; argv[i][j]; j++) {
switch (argv[i][j]) {
case 's': dosb = true; break;
case 'b': dofreemap = true; break;
case 'i':
if (argv[i][j+1] == 0) {
dumpino = atoi(argv[++i]);
}
else {
dumpino = atoi(argv[i]+j+1);
j = strlen(argv[i]);
}
/* XXX ugly */
goto nextarg;
case 'I': doindirect = true; break;
case 'f': dofiles = true; break;
case 'd': dodirs = true; break;
case 'r': recurse = true; break;
case 'a':
dosb = true;
dofreemap = true;
if (dumpino == 0) {
dumpino = SFS_ROOTDIR_INO;
}
doindirect = true;
dofiles = true;
dodirs = true;
recurse = true;
break;
default:
usage();
break;
}
}
}
else {
if (dumpdisk != NULL) {
usage();
}
dumpdisk = argv[i];
}
nextarg:
;
}
if (dumpdisk == NULL) {
usage();
}
if (!dosb && !dofreemap && dumpino == 0) {
dumpino = SFS_ROOTDIR_INO;
}
opendisk(dumpdisk);
nblocks = readsb();
if (dosb) {
dumpsb();
}
if (dofreemap) {
dumpfreemap(nblocks);
}
if (dumpino != 0) {
dumpinode(dumpino, NULL);
}
closedisk();
return 0;
}

View File

@@ -0,0 +1,12 @@
# Makefile for halt
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=halt
SRCS=halt.c
BINDIR=/sbin
.include "$(TOP)/mk/os161.prog.mk"

44
userland/sbin/halt/halt.c Normal file
View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
/*
* halt - shut down system, do not reboot, do not turn off power.
* Usage: halt
*
* Just calls reboot() with the RB_HALT flag.
*/
int
main(void)
{
reboot(RB_HALT);
return 0;
}

View File

@@ -0,0 +1,13 @@
# Makefile for mksfs
TOP=../../..
.include "$(TOP)/mk/os161.config.mk"
PROG=mksfs
SRCS=mksfs.c disk.c support.c
BINDIR=/sbin
HOSTBINDIR=/hostbin
.include "$(TOP)/mk/os161.prog.mk"
.include "$(TOP)/mk/os161.hostprog.mk"

200
userland/sbin/mksfs/disk.c Normal file
View File

@@ -0,0 +1,200 @@
/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
* The President and Fellows of Harvard College.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <err.h>
#include "support.h"
#include "disk.h"
#define HOSTSTRING "System/161 Disk Image"
#define BLOCKSIZE 512
#ifndef EINTR
#define EINTR 0
#endif
static int fd=-1;
static uint32_t nblocks;
/*
* Open a disk. If we're built for the host OS, check that it's a
* System/161 disk image, and then ignore the header block.
*/
void
opendisk(const char *path)
{
struct stat statbuf;
assert(fd<0);
fd = open(path, O_RDWR);
if (fd<0) {
err(1, "%s", path);
}
if (fstat(fd, &statbuf)) {
err(1, "%s: fstat", path);
}
nblocks = statbuf.st_size / BLOCKSIZE;
#ifdef HOST
nblocks--;
{
char buf[64];
int len;
do {
len = read(fd, buf, sizeof(buf)-1);
if (len < 0 && (errno==EINTR || errno==EAGAIN)) {
continue;
}
} while (0);
buf[len] = 0;
buf[strlen(HOSTSTRING)] = 0;
if (strcmp(buf, HOSTSTRING)) {
errx(1, "%s: Not a System/161 disk image", path);
}
}
#endif
}
/*
* Return the block size. (This is fixed, but still...)
*/
uint32_t
diskblocksize(void)
{
assert(fd>=0);
return BLOCKSIZE;
}
/*
* Return the device/image size in blocks.
*/
uint32_t
diskblocks(void)
{
assert(fd>=0);
return nblocks;
}
/*
* Write a block.
*/
void
diskwrite(const void *data, uint32_t block)
{
const char *cdata = data;
uint32_t tot=0;
int len;
assert(fd>=0);
#ifdef HOST
// skip over disk file header
block++;
#endif
if (lseek(fd, block*BLOCKSIZE, SEEK_SET)<0) {
err(1, "lseek");
}
while (tot < BLOCKSIZE) {
len = write(fd, cdata + tot, BLOCKSIZE - tot);
if (len < 0) {
if (errno==EINTR || errno==EAGAIN) {
continue;
}
err(1, "write");
}
if (len==0) {
err(1, "write returned 0?");
}
tot += len;
}
}
/*
* Read a block.
*/
void
diskread(void *data, uint32_t block)
{
char *cdata = data;
uint32_t tot=0;
int len;
assert(fd>=0);
#ifdef HOST
// skip over disk file header
block++;
#endif
if (lseek(fd, block*BLOCKSIZE, SEEK_SET)<0) {
err(1, "lseek");
}
while (tot < BLOCKSIZE) {
len = read(fd, cdata + tot, BLOCKSIZE - tot);
if (len < 0) {
if (errno==EINTR || errno==EAGAIN) {
continue;
}
err(1, "read");
}
if (len==0) {
err(1, "unexpected EOF in mid-sector");
}
tot += len;
}
}
/*
* Close the disk.
*/
void
closedisk(void)
{
assert(fd>=0);
if (close(fd)) {
err(1, "close");
}
fd = -1;
}

Some files were not shown because too many files have changed in this diff Show More