├── .gitignore ├── Chapter-1 └── README.md ├── Chapter-2 └── README.md ├── Chapter-3 └── README.md ├── Chapter-4 └── README.md ├── Chapter-5 └── README.md ├── Chapter-6 ├── README.md ├── gdtentry.png └── gdtr.png ├── Chapter-7 └── README.md ├── Chapter-8 ├── README.md ├── identitymapping.png ├── page_directory_entry.png ├── page_table_entry.png ├── paging_memory.png └── processes.png ├── LICENSE ├── README.md ├── SUMMARY.md ├── chapter9 ├── README.md ├── physicalmemory.png └── virtualmemory.png ├── cover.jpg ├── cover_small.jpg ├── preview.png └── src ├── Makefile ├── Vagrantfile ├── kernel ├── Makefile ├── arch │ └── x86 │ │ ├── Makefile │ │ ├── alloc.cc │ │ ├── architecture.cc │ │ ├── architecture.h │ │ ├── archprocess.h │ │ ├── config.make │ │ ├── io.cc │ │ ├── io.h │ │ ├── linker.ld │ │ ├── start.asm │ │ ├── switch.asm │ │ ├── vmm.cc │ │ ├── vmm.h │ │ ├── x86.cc │ │ ├── x86.h │ │ └── x86int.asm ├── config.h ├── core │ ├── Makefile │ ├── api.h │ ├── api │ │ ├── dev │ │ │ ├── clock.h │ │ │ ├── fb.h │ │ │ ├── ioctl.h │ │ │ ├── ipc.h │ │ │ ├── keyboard.h │ │ │ ├── proc.h │ │ │ └── tty.h │ │ └── kernel │ │ │ ├── syscall.h │ │ │ └── syscall_table.h │ ├── api_posix.cc │ ├── boot.h │ ├── class.cc │ ├── device.cc │ ├── device.h │ ├── elf_loader.cc │ ├── elf_loader.h │ ├── env.cc │ ├── env.h │ ├── file.cc │ ├── file.h │ ├── filesystem.cc │ ├── filesystem.h │ ├── kernel.cc │ ├── kernel.h │ ├── keyboard.h │ ├── modulelink.cc │ ├── modulelink.h │ ├── os.h │ ├── process.cc │ ├── process.h │ ├── signal.h │ ├── socket.cc │ ├── socket.h │ ├── syscalls.cc │ ├── syscalls.h │ ├── system.cc │ ├── system.h │ ├── user.cc │ └── user.h ├── modules │ ├── Makefile │ ├── bochsvbe.cc │ ├── bochsvbe.h │ ├── clock_x86.cc │ ├── clock_x86.h │ ├── dospartition.cc │ ├── dospartition.h │ ├── ext2.cc │ ├── ext2.h │ ├── ide.cc │ ├── ide.h │ ├── keys.cc │ ├── keys.h │ ├── module.cc │ ├── module.h │ ├── modules.conf │ ├── null.cc │ ├── null.h │ ├── stdtty.cc │ ├── stdtty.h │ ├── x86serial.cc │ └── x86serial.h └── runtime │ ├── Makefile │ ├── alloc.h │ ├── buffer.cc │ ├── buffer.h │ ├── cxx.cc │ ├── itoa.cc │ ├── libc.h │ ├── list.h │ ├── memory.cc │ ├── string.cc │ ├── string.h │ └── types.h ├── sdk ├── Makefile ├── bootdisk │ ├── bin │ │ ├── .gitkeep │ │ └── hello │ └── boot │ │ └── grub │ │ ├── grub.conf │ │ ├── menu.lst │ │ ├── stage1 │ │ ├── stage2 │ │ └── stage2_eltorito ├── build.mak ├── c.img ├── diskimage.sh ├── include │ ├── _ansi.h │ ├── alloca.h │ ├── arpa │ │ └── inet.h │ ├── assert.h │ ├── ctype.h │ ├── dirent.h │ ├── endian.h │ ├── errno.h │ ├── fcntl.h │ ├── float.h │ ├── getopt.h │ ├── inttypes.h │ ├── limits.h │ ├── linker.ld │ ├── locale.h │ ├── math.h │ ├── netinet │ │ └── in.h │ ├── os.h │ ├── pwd.h │ ├── setjmp.h │ ├── signal.h │ ├── stdarg.h │ ├── stddef.h │ ├── stdint.h │ ├── stdio.h │ ├── stdlib.h │ ├── string.h │ ├── strings.h │ ├── sys │ │ ├── cdefs.h │ │ ├── ioctl.h │ │ ├── mman.h │ │ ├── mount.h │ │ ├── param.h │ │ ├── resource.h │ │ ├── select.h │ │ ├── socket.h │ │ ├── stat.h │ │ ├── time.h │ │ ├── types.h │ │ └── wait.h │ ├── termios.h │ ├── time.h │ ├── unistd.h │ └── utime.h ├── lib │ ├── .gitkeep │ └── libc.a ├── qemu.sh └── src │ └── libc │ ├── Makefile │ ├── arch │ └── i386 │ │ ├── getpagesize.c │ │ ├── longjmp.S │ │ ├── math │ │ ├── e_atan2.S │ │ ├── e_exp.S │ │ ├── e_fmod.S │ │ ├── e_hypot.S │ │ ├── e_log.S │ │ ├── e_log10.S │ │ ├── e_pow.S │ │ ├── s_ceil.S │ │ ├── s_cos.S │ │ ├── s_fabs.S │ │ ├── s_finite.S │ │ ├── s_floor.S │ │ ├── s_frexp.S │ │ ├── s_scalbn.S │ │ └── s_sin.S │ │ └── setjmp.S │ ├── libc.a │ └── src │ ├── closedir.c │ ├── ctype │ ├── isalnum.c │ ├── isalpha.c │ ├── isascii.c │ ├── isblank.c │ ├── iscntrl.c │ ├── isdigit.c │ ├── isgraph.c │ ├── islower.c │ ├── isprint.c │ ├── ispunct.c │ ├── isspace.c │ ├── isupper.c │ ├── isxdigit.c │ ├── toascii.c │ ├── tolower.c │ └── toupper.c │ ├── fcntl │ ├── creat.c │ ├── fcntl.c │ └── open.c │ ├── getopt │ ├── getopt.c │ ├── getopt_int.h │ ├── getopt_long.c │ └── getopt_long_only.c │ ├── locale │ ├── localeconv.c │ └── setlocale.c │ ├── math │ ├── s_ldexp.c │ └── s_modf.c │ ├── network │ ├── inet_aton.c │ └── inet_ntoa.c │ ├── opendir.c │ ├── os │ ├── debug.c │ ├── ipc.c │ ├── module.c │ ├── os.c │ ├── region.c │ ├── semaphore.c │ ├── syscall.c │ ├── sysinfo.c │ └── thread.c │ ├── pwd │ ├── endpwent.c │ ├── getpwent.c │ ├── getpwnam.c │ ├── getpwuid.c │ └── setpwent.c │ ├── readdir.c │ ├── rewinddir.c │ ├── signal │ ├── kill.c │ ├── killpg.c │ ├── raise.c │ ├── sigaction.c │ ├── sigaddset.c │ ├── sigdelset.c │ ├── sigemptyset.c │ ├── sigfillset.c │ ├── sigismember.c │ ├── signal.c │ └── sigprocmask.c │ ├── sscanf.c │ ├── start.c │ ├── stdio │ ├── clearerr.c │ ├── fclose.c │ ├── fdopen.c │ ├── feof.c │ ├── ferror.c │ ├── fflush.c │ ├── fgetc.c │ ├── fgets.c │ ├── fileno.c │ ├── fopen.c │ ├── fpurge.c │ ├── fputc.c │ ├── fputs.c │ ├── fread.c │ ├── freopen.c │ ├── fseek.c │ ├── ftell.c │ ├── fwrite.c │ ├── getc.c │ ├── perror.c │ ├── putc.c │ ├── putchar.c │ ├── puts.c │ ├── remove.c │ ├── rename.c │ ├── rewind.c │ ├── setvbuf.c │ ├── stdio_internal.c │ ├── stdio_internal.h │ ├── streams.c │ ├── support_bufio.c │ ├── support_pf.c │ ├── support_supcon.c │ └── ungetc.c │ ├── stdlib │ ├── abort.c │ ├── abs.c │ ├── atof.c │ ├── atoi.c │ ├── atol.c │ ├── atoll.c │ ├── bsearch.c │ ├── getenv.c │ ├── labs.c │ ├── llabs.c │ ├── malloc.c │ ├── mkstemp.c │ ├── mktemp.c │ ├── qsort.c │ ├── rand.c │ ├── random.c │ ├── srand.c │ ├── srandom.c │ ├── strtod.c │ ├── strtol.c │ ├── strtoll.c │ ├── strtoul.c │ └── strtoull.c │ ├── string │ ├── memchr.c │ ├── memcmp.c │ ├── memcpy.c │ ├── memmove.c │ ├── memset.c │ ├── strcasecmp.c │ ├── strcat.c │ ├── strchr.c │ ├── strcmp.c │ ├── strcpy.c │ ├── strcspn.c │ ├── strdup.c │ ├── strerror.c │ ├── strlen.c │ ├── strncasecmp.c │ ├── strncat.c │ ├── strncmp.c │ ├── strncpy.c │ ├── strndup.c │ ├── strnlen.c │ ├── strpbrk.c │ ├── strrchr.c │ ├── strsignal.c │ ├── strspn.c │ ├── strstr.c │ ├── strtok.c │ └── strtok_r.c │ ├── sys │ ├── chmod.c │ ├── connect.c │ ├── fstat.c │ ├── ioctl.c │ ├── lstat.c │ ├── mkdir.c │ ├── mount.c │ ├── select.c │ ├── socket.c │ ├── stat.c │ ├── stime.c │ ├── umask.c │ ├── umount.c │ ├── utime.c │ ├── utimes.c │ ├── wait.c │ ├── wait3.c │ ├── wait4.c │ └── waitpid.c │ ├── termios │ ├── tcflow.c │ ├── tcflush.c │ ├── tcgetattr.c │ ├── tcgetpgrp.c │ ├── tcsetattr.c │ └── tcsetpgrp.c │ ├── time │ ├── asctime.c │ ├── asctime_r.c │ ├── ctime.c │ ├── ctime_r.c │ ├── gettimeofday.c │ ├── gmtime.c │ ├── gmtime_r.c │ ├── localtime.c │ ├── localtime_r.c │ ├── mktime.c │ ├── nanosleep.c │ ├── strftime.c │ ├── time.c │ ├── time_int.c │ ├── time_int.h │ └── tzset.c │ ├── trio │ ├── trio.c │ ├── trio.h │ ├── triodef.h │ ├── trionan.c │ ├── trionan.h │ ├── triop.h │ ├── triostr.c │ └── triostr.h │ ├── udivmoddi4.c │ └── unistd │ ├── access.c │ ├── alarm.c │ ├── chdir.c │ ├── chown.c │ ├── close.c │ ├── dup.c │ ├── dup2.c │ ├── execlp.c │ ├── execv.c │ ├── execve.c │ ├── execvp.c │ ├── exit.c │ ├── fchdir.c │ ├── fork.c │ ├── fpathconf.c │ ├── ftruncate.c │ ├── getcwd.c │ ├── getdents.c │ ├── getdtablesize.c │ ├── getegid.c │ ├── geteuid.c │ ├── getgid.c │ ├── gethostname.c │ ├── getpgid.c │ ├── getpgrp.c │ ├── getpid.c │ ├── getppid.c │ ├── gettid.c │ ├── getuid.c │ ├── isatty.c │ ├── link.c │ ├── lseek.c │ ├── mmap.c │ ├── pipe.c │ ├── pread.c │ ├── pwrite.c │ ├── read.c │ ├── readlink.c │ ├── rmdir.c │ ├── sbrk.c │ ├── setgid.c │ ├── setpgid.c │ ├── setpgrp.c │ ├── setregid.c │ ├── setreuid.c │ ├── setuid.c │ ├── sleep.c │ ├── symlink.c │ ├── ttyname.c │ ├── unlink.c │ └── write.c └── userland ├── Makefile └── helloworld ├── Makefile ├── hello └── main.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | 15 | # Elf binaries 16 | *.elf 17 | 18 | # Vagrant 19 | /src/.vagrant/ 20 | 21 | # Other 22 | /src/userland/helloworld/hello 23 | 24 | # SDK binaries 25 | /src/sdk/bootdisk/bin/hello 26 | /src/sdk/c.img 27 | 28 | node_modules 29 | .grunt 30 | _book 31 | -------------------------------------------------------------------------------- /Chapter-1/README.md: -------------------------------------------------------------------------------- 1 | ## Chapter 1: Introduction to the x86 architecture and about our OS 2 | 3 | ### What is the x86 architecture? 4 | 5 | > The term x86 denotes a family of backward compatible instruction set architectures based on the Intel 8086 CPU. 6 | 7 | The x86 architecture is the most common instruction set architecture since its introduction in 1981 for the IBM PC. A large amount of software, including operating systems (OS's) such as DOS, Windows, Linux, BSD, Solaris and Mac OS X, function with x86-based hardware. 8 | 9 | In this course we are not going to design an operating system for the x86-64 architecture but for x86-32, thanks to backward compatibility, our OS will be compatible with our newer PCs (but take caution if you want to test it on your real machine). 10 | 11 | ### Our Operating System 12 | 13 | The goal is to build a very simple UNIX-based operating system in C++, but the goal is not to just build a "proof-of-concept". The OS should be able to boot, start a userland shell and be extensible. 14 | 15 | The OS will be built for the x86 architecture, running on 32 bits, and compatible with IBM PCs. 16 | 17 | **Specifications:** 18 | 19 | * Code in C++ 20 | * x86, 32 bit architecture 21 | * Boot with Grub 22 | * Kind of modular system for drivers 23 | * Kind of UNIX style 24 | * Multitasking 25 | * ELF executable in userland 26 | * Modules (accessible in userland using /dev/...) : 27 | * IDE disks 28 | * DOS partitions 29 | * Clock 30 | * EXT2 (read only) 31 | * Boch VBE 32 | * Userland : 33 | * API Posix 34 | * LibC 35 | * "Can" run a shell or some executables (e.g., lua) 36 | -------------------------------------------------------------------------------- /Chapter-6/gdtentry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/Chapter-6/gdtentry.png -------------------------------------------------------------------------------- /Chapter-6/gdtr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/Chapter-6/gdtr.png -------------------------------------------------------------------------------- /Chapter-8/identitymapping.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/Chapter-8/identitymapping.png -------------------------------------------------------------------------------- /Chapter-8/page_directory_entry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/Chapter-8/page_directory_entry.png -------------------------------------------------------------------------------- /Chapter-8/page_table_entry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/Chapter-8/page_table_entry.png -------------------------------------------------------------------------------- /Chapter-8/paging_memory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/Chapter-8/paging_memory.png -------------------------------------------------------------------------------- /Chapter-8/processes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/Chapter-8/processes.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | How to Make a Computer Operating System 2 | ======================================= 3 | 4 | Online book about how to write a computer operating system in C/C++ from scratch. 5 | 6 | **Caution**: This repository is a remake of my old course. It was written several years ago [as one of my first projects when I was in High School](https://github.com/SamyPesse/devos), I'm still refactoring some parts. The original course was in French and I'm not an English native. I'm going to continue and improve this course in my free-time. 7 | 8 | **Book**: An online version is available at [http://samypesse.gitbooks.io/how-to-create-an-operating-system/](http://samypesse.gitbooks.io/how-to-create-an-operating-system/) (PDF, Mobi and ePub). It was generated using [GitBook](https://www.gitbook.com/). 9 | 10 | **Source Code**: All the system source code will be stored in the [src](https://github.com/SamyPesse/How-to-Make-a-Computer-Operating-System/tree/master/src) directory. Each step will contain links to the different related files. 11 | 12 | **Contributions**: This course is open to contributions, feel free to signal errors with issues or directly correct the errors with pull-requests. 13 | 14 | **Questions**: Feel free to ask any questions by adding issues or commenting sections. 15 | 16 | You can follow me on Twitter [@SamyPesse](https://twitter.com/SamyPesse) or [GitHub](https://github.com/SamyPesse). 17 | 18 | ### What kind of OS are we building? 19 | 20 | The goal is to build a very simple UNIX-based operating system in C++, not just a "proof-of-concept". The OS should be able to boot, start a userland shell, and be extensible. 21 | 22 | ![Screen](./preview.png) 23 | -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | * [Introduction](README.md) 4 | * [Introduction about the x86 architecture and about our OS](Chapter-1/README.md) 5 | * [Setup the development environment](Chapter-2/README.md) 6 | * [First boot with GRUB](Chapter-3/README.md) 7 | * [Backbone of the OS and C++ runtime](Chapter-4/README.md) 8 | * [Base classes for managing x86 architecture](Chapter-5/README.md) 9 | * [GDT](Chapter-6/README.md) 10 | * [IDT and interrupts](Chapter-7/README.md) 11 | * [Theory: physical and virtual memory](Chapter-8/README.md) 12 | * [Memory management: physical and virtual](chapter9/README.md) 13 | * Process management and multitasking 14 | * External program execution: ELF files 15 | * Userland and syscalls 16 | * Modular drivers 17 | * Some basics modules: console, keyboard 18 | * IDE Hard disks 19 | * DOS Partitions 20 | * EXT2 read-only filesystems 21 | * Standard C library (libC) 22 | * UNIX basic tools: sh, cat 23 | * Lua interpreter 24 | 25 | -------------------------------------------------------------------------------- /chapter9/README.md: -------------------------------------------------------------------------------- 1 | # Memory management: physical and virtual 2 | 3 | The kernel knows the size of the physical memory available thanks to [GRUB](../Chapter-3/README.md). 4 | 5 | In our implementation, the first 8 megabytes of physical memory will be reserved for use by the kernel and will contain: 6 | 7 | - The kernel 8 | - GDT, IDT et TSS 9 | - Kernel Stack 10 | - Some space reserved to hardware (video memory, ...) 11 | - Page directory and pages table for the kernel 12 | 13 | The rest of the physical memory is freely available to the kernel and applications. 14 | 15 | ![Physical Memory](physicalmemory.png) 16 | 17 | 18 | ### Virtual Memory Mapping 19 | 20 | The address space between the beginning of memory and `0x40000000` address is the kernel space, while the space between the address `0x40000000` and the end of the memory corresponds to user space: 21 | 22 | ![Virtual Memory](virtualmemory.png) 23 | 24 | The kernel space in virtual memory, which is using 1Gb of virtual memory, is common to all tasks (kernel and user). 25 | 26 | This is implemented by pointing the first 256 entries of the task page directory to the kernel page directory (In [vmm.cc](https://github.com/SamyPesse/How-to-Make-a-Computer-Operating-System/blob/master/src/kernel/arch/x86/vmm.cc#L204)): 27 | 28 | ```cpp 29 | /* 30 | * Kernel Space. v_addr < USER_OFFSET are addressed by the kernel pages table 31 | */ 32 | for (i=0; i<256; i++) 33 | pdir[i] = pd0[i]; 34 | ``` -------------------------------------------------------------------------------- /chapter9/physicalmemory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/chapter9/physicalmemory.png -------------------------------------------------------------------------------- /chapter9/virtualmemory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/chapter9/virtualmemory.png -------------------------------------------------------------------------------- /cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/cover.jpg -------------------------------------------------------------------------------- /cover_small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/cover_small.jpg -------------------------------------------------------------------------------- /preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/preview.png -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | SDKDIR=./sdk 2 | 3 | help: 4 | @echo "Makefile for Building Dev Operating System." 5 | @echo "Usage: make [ all | clean | help | build | run] " 6 | @echo "" 7 | @echo 8 | 9 | all: 10 | @echo "Building Kernel" 11 | make -C ./kernel 12 | @echo "Building SDK" 13 | make -C ./sdk 14 | @echo "Building Userland" 15 | make -C ./userland 16 | 17 | 18 | build: 19 | zip -r devos-$(VERSION).zip ./ 20 | 21 | 22 | run: 23 | @echo "Running Dev Operating System." 24 | cd ./sdk && sudo bash ./diskimage.sh 25 | cd ./sdk && ./qemu.sh 26 | 27 | clean: 28 | make -C ./kernel clean 29 | make -C ./userland clean 30 | -------------------------------------------------------------------------------- /src/kernel/Makefile: -------------------------------------------------------------------------------- 1 | ARCH=x86 2 | KERNEL=kernel.elf 3 | SDKDIR=../sdk 4 | INCDIR= -I ./ -I ./modules -I ./core -I ./arch/$(ARCH) 5 | 6 | 7 | include ./arch/$(ARCH)/config.make 8 | 9 | include ./runtime/Makefile 10 | include ./core/Makefile 11 | include ./modules/Makefile 12 | include ./arch/$(ARCH)/Makefile 13 | 14 | FLAG :=$(FLAG) -D__$(ARCH)__ 15 | PLATFORMS= `find ./arch/ -type d | sed "s/.*\///" | sort` 16 | 17 | 18 | all: $(KERNEL) 19 | 20 | $(KERNEL): $(OBJS) 21 | $(LD) $(LDFLAG) -o $@ $^ 22 | cp $(KERNEL) $(SDKDIR)/bootdisk/ 23 | 24 | help: 25 | @echo "Makefile for Kernel." 26 | @echo "Please see COPYING for licensing information." 27 | @echo "Output should be: "$(KERNEL) 28 | @echo "Usage: make [ all | clean] " 29 | @echo "Currently supported platforms:" 30 | @echo $(PLATFORMS) 31 | @echo 32 | 33 | tosdk: 34 | cp $(KERNEL) $(SDKDIR)/disk/ 35 | 36 | install: 37 | sudo cp $(KERNEL) /boot/ 38 | 39 | debug: 40 | $(NM) -n $(KERNEL) 41 | 42 | 43 | hinfo: 44 | $(OBJDUMP) -f $(KERNEL) 45 | 46 | dasm: 47 | $(OBJDUMP) -d $(KERNEL) > dasm.txt 48 | 49 | 50 | run: 51 | cd $(SDKDIR) && sh ./diskimage.sh 52 | cd $(SDKDIR) && sh ./qemu.sh 53 | 54 | geniso: 55 | cd $(SDKDIR) && sh ./cdrom.sh 56 | 57 | %.o: %.cc 58 | $(SC) $(FLAG) -c $< -o $@ 59 | 60 | %.o: %.S 61 | $(SC) $(FLAG) -c $< -o $@ 62 | 63 | %.o: %.asm 64 | $(ASM) $(ASMFLAG) -c $< -o $@ 65 | 66 | 67 | clean: 68 | rm -f $(OBJS) $(KERNEL) dasm.txt 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/kernel/arch/x86/Makefile: -------------------------------------------------------------------------------- 1 | OBJS:= arch/$(ARCH)/start.o $(OBJS) arch/$(ARCH)/alloc.o arch/$(ARCH)/architecture.o \ 2 | arch/$(ARCH)/io.o arch/$(ARCH)/vmm.o arch/$(ARCH)/x86.o arch/$(ARCH)/switch.o arch/$(ARCH)/x86int.o 3 | -------------------------------------------------------------------------------- /src/kernel/arch/x86/archprocess.h: -------------------------------------------------------------------------------- 1 | #ifndef APROC_H 2 | #define APROC_H 3 | 4 | #include 5 | 6 | extern "C" { 7 | 8 | #define KERNELMODE 0 9 | #define USERMODE 1 10 | 11 | /** info processor structure for a process */ 12 | struct process_st { 13 | int pid; 14 | 15 | struct { 16 | u32 eax, ecx, edx, ebx; 17 | u32 esp, ebp, esi, edi; 18 | u32 eip, eflags; 19 | u32 cs:16, ss:16, ds:16, es:16, fs:16, gs:16; 20 | u32 cr3; 21 | } regs __attribute__ ((packed)); 22 | 23 | struct { 24 | u32 esp0; 25 | u16 ss0; 26 | } kstack __attribute__ ((packed)); 27 | 28 | // Caution: with task switch 29 | struct page_directory *pd; 30 | 31 | list_head pglist; 32 | 33 | char *b_exec; 34 | char *e_exec; 35 | char *b_bss; 36 | char *e_bss; 37 | char *b_heap; 38 | char *e_heap; 39 | 40 | u32 signal; 41 | void* sigfn[32]; 42 | 43 | void* vinfo; 44 | 45 | } __attribute__ ((packed)); 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /src/kernel/arch/x86/config.make: -------------------------------------------------------------------------------- 1 | LDFLAG= -melf_i386 -static -L ./ -T ./arch/$(ARCH)/linker.ld 2 | SC=g++ 3 | FLAG= $(INCDIR) -g -O2 -w -trigraphs -fno-builtin -fno-exceptions -fno-stack-protector -O0 -m32 -fno-rtti -nostdlib -nodefaultlibs 4 | ASM=nasm 5 | ASMFLAG=-f elf -o 6 | LD=ld 7 | NM=nm 8 | OBJDUMP=objdump 9 | -------------------------------------------------------------------------------- /src/kernel/arch/x86/io.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/arch/x86/io.cc -------------------------------------------------------------------------------- /src/kernel/arch/x86/linker.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf32-i386) 2 | OUTPUT_ARCH(i386) 3 | ENTRY (_start) 4 | 5 | SECTIONS{ 6 | . = 0x00100000; 7 | 8 | .text :{ 9 | *(.text) 10 | } 11 | 12 | .data ALIGN (0x1000) : { 13 | start_ctors = .; 14 | *(.ctor*) 15 | end_ctors = .; 16 | start_dtors = .; 17 | *(.dtor*) 18 | end_dtors = .; 19 | *(.data) 20 | } 21 | 22 | 23 | .rodata ALIGN (0x1000) : { 24 | *(.rodata) 25 | } 26 | 27 | .data ALIGN (0x1000) : { 28 | *(.data) 29 | } 30 | 31 | .bss : { 32 | sbss = .; 33 | *(COMMON) 34 | *(.bss) 35 | ebss = .; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/kernel/arch/x86/start.asm: -------------------------------------------------------------------------------- 1 | global _start, _kmain 2 | extern kmain, start_ctors, end_ctors, start_dtors, end_dtors 3 | 4 | 5 | %define MULTIBOOT_HEADER_MAGIC 0x1BADB002 6 | %define MULTIBOOT_HEADER_FLAGS 0x00000003 7 | %define CHECKSUM -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) 8 | 9 | ;-- Entry point 10 | _start: 11 | jmp start 12 | 13 | ;-- Multiboot header -- 14 | align 4 15 | 16 | multiboot_header: 17 | dd MULTIBOOT_HEADER_MAGIC 18 | dd MULTIBOOT_HEADER_FLAGS 19 | dd CHECKSUM 20 | ;--/Multiboot header -- 21 | 22 | start: 23 | push ebx 24 | 25 | static_ctors_loop: 26 | mov ebx, start_ctors 27 | jmp .test 28 | .body: 29 | call [ebx] 30 | add ebx,4 31 | .test: 32 | cmp ebx, end_ctors 33 | jb .body 34 | 35 | call kmain ; call kernel proper 36 | 37 | static_dtors_loop: 38 | mov ebx, start_dtors 39 | jmp .test 40 | .body: 41 | call [ebx] 42 | add ebx,4 43 | .test: 44 | cmp ebx, end_dtors 45 | jb .body 46 | 47 | cli ; stop interrupts 48 | hlt ; halt the CPU 49 | -------------------------------------------------------------------------------- /src/kernel/arch/x86/switch.asm: -------------------------------------------------------------------------------- 1 | 2 | global do_switch 3 | 4 | do_switch: 5 | ; recuper l'adresse de *current 6 | mov esi, [esp] 7 | pop eax ; depile @current 8 | 9 | ; prepare les registres 10 | push dword [esi+4] ; eax 11 | push dword [esi+8] ; ecx 12 | push dword [esi+12] ; edx 13 | push dword [esi+16] ; ebx 14 | push dword [esi+24] ; ebp 15 | push dword [esi+28] ; esi 16 | push dword [esi+32] ; edi 17 | push dword [esi+48] ; ds 18 | push dword [esi+50] ; es 19 | push dword [esi+52] ; fs 20 | push dword [esi+54] ; gs 21 | 22 | ; enleve le mask du PIC 23 | mov al, 0x20 24 | out 0x20, al 25 | 26 | ; charge table des pages 27 | mov eax, [esi+56] 28 | mov cr3, eax 29 | 30 | ; charge les registres 31 | pop gs 32 | pop fs 33 | pop es 34 | pop ds 35 | pop edi 36 | pop esi 37 | pop ebp 38 | pop ebx 39 | pop edx 40 | pop ecx 41 | pop eax 42 | 43 | ; retourne 44 | iret 45 | 46 | -------------------------------------------------------------------------------- /src/kernel/arch/x86/x86.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/arch/x86/x86.cc -------------------------------------------------------------------------------- /src/kernel/arch/x86/x86int.asm: -------------------------------------------------------------------------------- 1 | extern isr_default_int, do_syscalls, isr_schedule_int 2 | 3 | 4 | %macro SAVE_REGS 0 5 | pushad 6 | push ds 7 | push es 8 | push fs 9 | push gs 10 | push ebx 11 | mov bx,0x10 12 | mov ds,bx 13 | pop ebx 14 | %endmacro 15 | 16 | %macro RESTORE_REGS 0 17 | pop gs 18 | pop fs 19 | pop es 20 | pop ds 21 | popad 22 | %endmacro 23 | 24 | %macro INTERRUPT 1 25 | global _asm_int_%1 26 | _asm_int_%1: 27 | SAVE_REGS 28 | push %1 29 | call isr_default_int 30 | pop eax ;;a enlever sinon 31 | mov al,0x20 32 | out 0x20,al 33 | RESTORE_REGS 34 | iret 35 | %endmacro 36 | 37 | extern isr_GP_exc, isr_PF_exc 38 | global _asm_syscalls, _asm_exc_GP, _asm_exc_PF 39 | _asm_syscalls: 40 | SAVE_REGS 41 | push eax ; transmission du numero d'appel 42 | call do_syscalls 43 | pop eax 44 | cli 45 | sti 46 | RESTORE_REGS 47 | iret 48 | 49 | 50 | _asm_exc_GP: 51 | SAVE_REGS 52 | call isr_GP_exc 53 | RESTORE_REGS 54 | add esp,4 55 | iret 56 | 57 | _asm_exc_PF: 58 | SAVE_REGS 59 | call isr_PF_exc 60 | RESTORE_REGS 61 | add esp,4 62 | iret 63 | 64 | global _asm_schedule 65 | _asm_schedule: 66 | SAVE_REGS 67 | call isr_schedule_int 68 | mov al,0x20 69 | out 0x20,al 70 | RESTORE_REGS 71 | iret 72 | 73 | INTERRUPT 1 74 | INTERRUPT 2 75 | -------------------------------------------------------------------------------- /src/kernel/config.h: -------------------------------------------------------------------------------- 1 | #ifndef CONFIG_H 2 | #define CONFIG_H 3 | 4 | #define KERNEL_NAME "devos" /* kernel name */ 5 | #define KERNEL_VERSION "1" /* kernel version */ 6 | #define KERNEL_DATE __DATE__ 7 | #define KERNEL_TIME __TIME__ 8 | #define KERNEL_LICENCE "Apache License" /* license */ 9 | #define KERNEL_COMPUTERNAME "test-pc" /* default name for the machine */ 10 | 11 | /* identifiant du processeur */ 12 | #ifdef __x86__ 13 | #define KERNEL_PROCESSOR_IDENTIFIER "x86" 14 | #else 15 | #define KERNEL_PROCESSOR_IDENTIFIER "(null)" 16 | #endif 17 | 18 | /* max open file */ 19 | #define CONFIG_MAX_FILE 32 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /src/kernel/core/Makefile: -------------------------------------------------------------------------------- 1 | OBJS:= $(OBJS) core/class.o core/elf_loader.o core/file.o \ 2 | core/filesystem.o core/kernel.o core/api_posix.o\ 3 | core/process.o core/syscalls.o core/device.o core/system.o \ 4 | core/env.o core/user.o core/modulelink.o core/socket.o 5 | 6 | -------------------------------------------------------------------------------- /src/kernel/core/api.h: -------------------------------------------------------------------------------- 1 | #ifndef API_H 2 | #define API_H 3 | 4 | //posix 5 | void call_open(); 6 | void call_close(); 7 | void call_read(); 8 | void call_write(); 9 | void call_sbrk(); 10 | void call_ioctl(); 11 | void call_exit(); 12 | void call_execv(); 13 | void call_symlink(); 14 | void call_getdents(); 15 | void call_wait(); 16 | void call_dup2(); 17 | void call_fork(); 18 | void call_chdir(); 19 | void call_mmap(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /src/kernel/core/api/dev/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef __API_CLOCK__ 2 | #define __API_CLOCK__ 3 | 4 | typedef unsigned int clock_d; 5 | 6 | struct clock_info{ 7 | clock_d h; 8 | clock_d m; 9 | clock_d s; 10 | 11 | clock_d day; 12 | clock_d month; 13 | clock_d year; 14 | }; 15 | 16 | #define API_CLOCK_GET_INFO 0x6122 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /src/kernel/core/api/dev/fb.h: -------------------------------------------------------------------------------- 1 | #ifndef __API_FB__ 2 | #define __API_FB__ 3 | 4 | struct fb_info{ 5 | unsigned int w; //largeur 6 | unsigned int h; //hauteur 7 | char bpp; //bit per pixel 8 | char state; //etat de la carte 9 | unsigned int* vmem; //video memory 10 | }; 11 | 12 | enum{ 13 | FB_NOT_ACTIVE=0, 14 | FB_ACTIVE=1, 15 | }; 16 | 17 | #define API_FB_IS_AVAILABLE 0x801 18 | #define API_FB_GET_INFO 0x802 //info actuel 19 | #define API_FB_GET_BINFO 0x803 //meilleur info 20 | #define API_FB_SET_INFO 0x804 21 | 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /src/kernel/core/api/dev/ioctl.h: -------------------------------------------------------------------------------- 1 | #ifndef __API_IOCTL__ 2 | #define __API_IOCTL__ 3 | 4 | 5 | #define DEV_GET_TYPE 0x01 /* Renvoie le type de peripherique */ 6 | #define DEV_GET_STATE 0x02 /* renvoie l'etat du peripherique */ 7 | #define DEV_GET_FORMAT 0x03 /* renvoie le format du peripherique */ 8 | 9 | //Type de peripherique : 10 | #define DEV_TYPE_TTY 0x01 11 | #define DEV_TYPE_DISK 0x02 12 | #define DEV_TYPE_FB 0x03 13 | #define DEV_TYPE_HID 0x04 //Added by NoMaitener (aka William). HID stand for Human Interface Device 14 | 15 | //Format du peripherique 16 | #define DEV_FORMAT_CHAR 0x01 17 | #define DEV_FORMAT_BLOCK 0x02 18 | #define DEV_FORMAT_FB 0x03 19 | 20 | //Etat du peripherique 21 | #define DEV_STATE_OK 0x01 22 | #define DEV_STATE_NOTREADY 0x02 //Added by NoMaitener (aka William). Discuss here of "NOTREADY" 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/kernel/core/api/dev/ipc.h: -------------------------------------------------------------------------------- 1 | #ifndef __API_IPC__ 2 | #define __API_IPC__ 3 | 4 | #define STDIPC_FILENO 3 5 | #define SIGIPC SIGUSR1 6 | 7 | 8 | //iotcl 9 | #define API_TTY_SWITCH_SCREEN 0xff52 10 | 11 | 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /src/kernel/core/api/dev/keyboard.h: -------------------------------------------------------------------------------- 1 | #ifndef __API_KEYBOARD__ 2 | #define __API_KEYBOARD__ 3 | 4 | 5 | //keyboard 6 | enum { 7 | KEY_TAB = 7, 8 | KEY_BACKSPACE = 8, 9 | KEY_ENTER = 10, 10 | KEY_ESCAPE = 27, 11 | KEY_F1 = 255, 12 | KEY_F2 = 254, 13 | KEY_F3 = 253, 14 | KEY_F4 = 252, 15 | KEY_F5 = 251, 16 | KEY_F6 = 250, 17 | KEY_F7 = 249, 18 | KEY_F8 = 248, 19 | KEY_F9 = 247, 20 | KEY_F10 = 246, 21 | KEY_F11 = 245, 22 | KEY_F12 = 244 23 | }; 24 | 25 | #define TABLE_KEYBOARD_SIZE 388 26 | 27 | #define API_KEYBOARD_SET_TABLE 0x4122 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /src/kernel/core/api/dev/proc.h: -------------------------------------------------------------------------------- 1 | #ifndef __API_PROC__ 2 | #define __API_PROC__ 3 | 4 | struct proc_info{ 5 | char name[32]; 6 | unsigned int pid; 7 | unsigned int tid; 8 | unsigned char state; 9 | unsigned int vmem; 10 | unsigned int pmem; 11 | }; 12 | 13 | enum{ 14 | PROC_STATE_RUN=0, 15 | PROC_STATE_ZOMBIE=1, 16 | PROC_STATE_THREAD=2, 17 | }; 18 | 19 | #define API_PROC_GET_PID 0x5200 20 | #define API_PROC_GET_INFO 0x5201 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /src/kernel/core/api/dev/tty.h: -------------------------------------------------------------------------------- 1 | #ifndef __API_TTY__ 2 | #define __API_TTY__ 3 | 4 | #include 5 | 6 | #define TTY_NAME_LEN 16 7 | 8 | //tty info 9 | struct tty_info_static{ 10 | char name[TTY_NAME_LEN]; 11 | char state; 12 | char type; 13 | unsigned int flags; 14 | }; 15 | 16 | struct tty_info_moving{ 17 | unsigned int x; 18 | unsigned int y; 19 | unsigned int attrf; 20 | unsigned int attrb; 21 | }; 22 | 23 | 24 | //tty type 25 | enum { 26 | TTY_TYPE_IOSTD=0, 27 | TTY_TYPE_SERIAL=1, 28 | TTY_TYPE_SCREEN=2, 29 | TTY_TYPE_VIRTUAL=3, 30 | TTY_TYPE_GUI=4 31 | }; 32 | 33 | //tty state 34 | enum { 35 | TTY_STATE_RUN=0, 36 | TTY_STATE_SWITCH=1, 37 | TTY_STATE_ERROR=2, 38 | TTY_STATE_PAUSE=3 39 | }; 40 | 41 | 42 | enum TTY_Colour 43 | { 44 | Black =0, 45 | Blue =1, 46 | Green =2, 47 | Cyan =3, 48 | Red =4, 49 | Magenta =5, 50 | Orange =6, 51 | LightGrey =7, 52 | DarkGrey =8, 53 | LightBlue =9, 54 | LightGreen =10, 55 | LightCyan =11, 56 | LightRed =12, 57 | LightMagenta=13, 58 | Yellow =14, 59 | White =15 60 | }; 61 | 62 | //iotcl 63 | #define API_TTY_SWITCH_SCREEN 0xff52 64 | #define API_TTY_CLEAR_SCREEN 0xff53 65 | #define API_TTY_GET_SINFO 0xff54 66 | #define API_TTY_GET_MINFO 0xff55 67 | #define API_TTY_SET_MINFO 0xff56 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /src/kernel/core/api/kernel/syscall.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _OS_SYSCALL_H_ 3 | #define _OS_SYSCALL_H_ 4 | 5 | 6 | int syscall0( int number ); 7 | int syscall1( int number, unsigned int p1 ); 8 | int syscall2( int number, unsigned int p1, unsigned int p2 ); 9 | int syscall3( int number, unsigned int p1, unsigned int p2, unsigned int p3 ); 10 | int syscall4( int number, unsigned int p1, unsigned int p2, unsigned int p3, unsigned int p4 ); 11 | int syscall5( int number, unsigned int p1, unsigned int p2, unsigned int p3, unsigned int p4, unsigned int p5 ); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /src/kernel/core/class.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | Static objects 5 | */ 6 | 7 | Io io; /* Input/Output interface */ 8 | Architecture arch; /* Cpu and architecture interface */ 9 | Vmm vmm; /* Virtual memory manager interface */ 10 | Filesystem fsm; /* Filesystem interface */ 11 | Module modm; /* Module manager */ 12 | Syscalls syscall; /* Syscalls manager */ 13 | System sys; /* System manager */ 14 | -------------------------------------------------------------------------------- /src/kernel/core/device.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | Device::~Device(){ 6 | 7 | } 8 | 9 | Device::Device(char* n) : File(n,TYPE_DEVICE) 10 | { 11 | fsm.addFile("/dev",this); 12 | } 13 | 14 | u32 Device::open(u32 flag){ 15 | return NOT_DEFINED; 16 | } 17 | 18 | u32 Device::close(){ 19 | return NOT_DEFINED; 20 | } 21 | 22 | u32 Device::read(u8* buffer,u32 size){ 23 | return NOT_DEFINED; 24 | } 25 | 26 | u32 Device::write(u8* buffer,u32 size){ 27 | return NOT_DEFINED; 28 | } 29 | 30 | u32 Device::ioctl(u32 id,u8* buffer){ 31 | return NOT_DEFINED; 32 | } 33 | 34 | u32 Device::remove(){ 35 | delete this; 36 | } 37 | 38 | void Device::scan(){ 39 | 40 | } 41 | 42 | -------------------------------------------------------------------------------- /src/kernel/core/device.h: -------------------------------------------------------------------------------- 1 | #ifndef DEVICE_H 2 | #define DEVICE_H 3 | 4 | #include 5 | #include 6 | 7 | 8 | class Device : public File 9 | { 10 | public: 11 | Device(char* n); 12 | ~Device(); 13 | 14 | virtual u32 open(u32 flag); 15 | virtual u32 close(); 16 | virtual u32 read(u8* buffer,u32 size); 17 | virtual u32 write(u8* buffer,u32 size); 18 | virtual u32 ioctl(u32 id,u8* buffer); 19 | virtual u32 remove(); 20 | virtual void scan(); 21 | 22 | 23 | protected: 24 | 25 | }; 26 | 27 | #endif -------------------------------------------------------------------------------- /src/kernel/core/elf_loader.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/core/elf_loader.cc -------------------------------------------------------------------------------- /src/kernel/core/env.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/core/env.cc -------------------------------------------------------------------------------- /src/kernel/core/env.h: -------------------------------------------------------------------------------- 1 | #ifndef ENV_H 2 | #define ENV_H 3 | 4 | #include 5 | #include 6 | 7 | 8 | class Variable : public File 9 | { 10 | public: 11 | Variable(char* n,char* v); 12 | ~Variable(); 13 | 14 | u32 open(u32 flag); 15 | u32 close(); 16 | u32 read(u32 pos,u8* buffer,u32 size); 17 | u32 write(u32 pos,u8* buffer,u32 size); 18 | u32 ioctl(u32 id,u8* buffer); 19 | u32 remove(); 20 | void scan(); 21 | 22 | 23 | 24 | 25 | protected: 26 | char* value; 27 | 28 | 29 | }; 30 | 31 | #endif -------------------------------------------------------------------------------- /src/kernel/core/file.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/core/file.cc -------------------------------------------------------------------------------- /src/kernel/core/filesystem.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/core/filesystem.cc -------------------------------------------------------------------------------- /src/kernel/core/filesystem.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef FILESYSTEM_H 3 | #define FILESYSTEM_H 4 | 5 | #include 6 | #include 7 | 8 | 9 | class Filesystem 10 | { 11 | public: 12 | Filesystem(); 13 | ~Filesystem(); 14 | 15 | void init(); 16 | void mknod(char* module,char* name,u32 flag); 17 | 18 | File* path(char* p); 19 | File* path_parent(char* p,char *fname); 20 | 21 | u32 link(char* fname,char *newf); 22 | 23 | 24 | u32 addFile(char* dir,File* fp); 25 | 26 | File* pivot_root(File* targetdir); 27 | 28 | File* getRoot(); 29 | 30 | private: 31 | File* root; 32 | File* dev; 33 | File* var; 34 | }; 35 | 36 | extern Filesystem fsm; 37 | #endif 38 | -------------------------------------------------------------------------------- /src/kernel/core/kernel.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef KERNEL_H 3 | #define KERNEL_H 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | 22 | #include 23 | 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/kernel/core/modulelink.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | 6 | ModLink::~ModLink(){ 7 | 8 | } 9 | 10 | ModLink::ModLink(char* n) : File(n,TYPE_FILE) 11 | { 12 | fsm.addFile("/sys/mods/",this); 13 | } 14 | 15 | u32 ModLink::open(u32 flag){ 16 | return RETURN_OK; 17 | } 18 | 19 | u32 ModLink::close(){ 20 | return RETURN_OK; 21 | } 22 | 23 | u32 ModLink::read(u8* buffer,u32 size){ 24 | return NOT_DEFINED; 25 | } 26 | 27 | u32 ModLink::write(u8* buffer,u32 size){ 28 | return NOT_DEFINED; 29 | } 30 | 31 | u32 ModLink::ioctl(u32 id,u8* buffer){ 32 | return NOT_DEFINED; 33 | } 34 | 35 | u32 ModLink::remove(){ 36 | delete this; 37 | } 38 | 39 | void ModLink::scan(){ 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/kernel/core/modulelink.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef MODLINK_H 3 | #define MODLINK_H 4 | 5 | #include 6 | #include 7 | 8 | 9 | class ModLink : public File 10 | { 11 | public: 12 | ModLink(char* n); 13 | ~ModLink(); 14 | 15 | u32 open(u32 flag); 16 | u32 close(); 17 | u32 read(u8* buffer,u32 size); 18 | u32 write(u8* buffer,u32 size); 19 | u32 ioctl(u32 id,u8* buffer); 20 | u32 remove(); 21 | void scan(); 22 | 23 | 24 | 25 | 26 | protected: 27 | 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/kernel/core/os.h: -------------------------------------------------------------------------------- 1 | #ifndef OS_H 2 | #define OS_H 3 | 4 | #include 5 | #include 6 | 7 | 8 | typedef File* (*device_driver) (char* name,u32 flag,File* dev); 9 | 10 | struct module_class{ 11 | int module_type; 12 | char* module_name; 13 | char* class_name; 14 | device_driver drive; 15 | }; 16 | 17 | 18 | 19 | 20 | /* 21 | * Module Macro 22 | */ 23 | #define MODULE_DEVICE 0 24 | #define MODULE_FILESYSTEM 1 25 | #define module(name,type,classe,mknod) module_class classe##_module={type,\ 26 | name, \ 27 | #classe, \ 28 | (device_driver)mknod}; 29 | 30 | #define import_module(classe) extern module_class classe##_module 31 | 32 | #define run_module_builder module_class* module_builder[]= 33 | #define build_module(classe) &classe##_module 34 | #define end_module() NULL 35 | 36 | #define std_buildin_module void Module::init() 37 | #define run_module(n,m,f) createDevice(#m,#n,f); 38 | 39 | /* 40 | * Asm Macro 41 | */ 42 | #define asm __asm__ 43 | #define asmv __asm__ __volatile__ 44 | 45 | 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /src/kernel/core/socket.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | 6 | Socket::~Socket(){ 7 | 8 | } 9 | 10 | Socket::Socket(char* n) : File(n,TYPE_FILE) 11 | { 12 | fsm.addFile("/sys/sockets/",this); 13 | } 14 | 15 | u32 Socket::open(u32 flag){ 16 | return RETURN_OK; 17 | } 18 | 19 | u32 Socket::close(){ 20 | return RETURN_OK; 21 | } 22 | 23 | u32 Socket::read(u8* buffer,u32 size){ 24 | return NOT_DEFINED; 25 | } 26 | 27 | u32 Socket::write(u8* buffer,u32 size){ 28 | return NOT_DEFINED; 29 | } 30 | 31 | u32 Socket::ioctl(u32 id,u8* buffer){ 32 | return NOT_DEFINED; 33 | } 34 | 35 | u32 Socket::remove(){ 36 | delete this; 37 | } 38 | 39 | void Socket::scan(){ 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/kernel/core/socket.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SOCKET_H 3 | #define SOCKET_H 4 | 5 | #include 6 | #include 7 | 8 | 9 | class Socket : public File 10 | { 11 | public: 12 | Socket(char* n); 13 | ~Socket(); 14 | 15 | u32 open(u32 flag); 16 | u32 close(); 17 | u32 read(u8* buffer,u32 size); 18 | u32 write(u8* buffer,u32 size); 19 | u32 ioctl(u32 id,u8* buffer); 20 | u32 remove(); 21 | void scan(); 22 | 23 | 24 | 25 | 26 | protected: 27 | 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/kernel/core/syscalls.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #define sysc(a,h) add(a,(syscall_handler)h) 8 | 9 | void Syscalls::init(){ 10 | int i; 11 | for (i=0;i 6 | #include 7 | 8 | 9 | #define NB_SYSCALLS 100 10 | 11 | 12 | typedef void (*syscall_handler)(void); 13 | 14 | class Syscalls 15 | { 16 | public: 17 | void init(); 18 | void add(u32 num,syscall_handler h); 19 | void call(u32 num); 20 | 21 | protected: 22 | syscall_handler calls[NB_SYSCALLS]; 23 | 24 | }; 25 | 26 | extern Syscalls syscall; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/kernel/core/system.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/core/system.cc -------------------------------------------------------------------------------- /src/kernel/core/system.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SYSTEM_H 3 | #define SYSTEM_H 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | class System 11 | { 12 | public: 13 | System(); 14 | ~System(); 15 | 16 | void init(); 17 | char* getvar(char* name); 18 | 19 | 20 | void addUserToList(User* us); 21 | 22 | User* getUser(char* nae); 23 | 24 | int login(User* us,char* pass); 25 | u32 isRoot(); //renvoie 1 si root 26 | 27 | 28 | private: 29 | User* listuser; 30 | 31 | File* var; 32 | 33 | User* actual; 34 | User* root; 35 | 36 | Variable* uservar; 37 | }; 38 | 39 | extern System sys; 40 | #endif 41 | -------------------------------------------------------------------------------- /src/kernel/core/user.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | 6 | User::~User(){ 7 | 8 | } 9 | 10 | User::User(char* n) : File(n,TYPE_FILE) 11 | { 12 | fsm.addFile("/sys/usr/",this); 13 | unext=0; 14 | sys.addUserToList(this); 15 | utype=USER_NORM; 16 | memset(password,0,512); 17 | } 18 | 19 | u32 User::open(u32 flag){ 20 | return RETURN_OK; 21 | } 22 | 23 | u32 User::close(){ 24 | return RETURN_OK; 25 | } 26 | 27 | u32 User::read(u8* buffer,u32 size){ 28 | return NOT_DEFINED; 29 | } 30 | 31 | u32 User::write(u8* buffer,u32 size){ 32 | return NOT_DEFINED; 33 | } 34 | 35 | u32 User::ioctl(u32 id,u8* buffer){ 36 | return NOT_DEFINED; 37 | } 38 | 39 | u32 User::remove(){ 40 | delete this; 41 | } 42 | 43 | void User::scan(){ 44 | 45 | } 46 | 47 | void User::setPassword(char *n){ 48 | if (n!=NULL) 49 | return; 50 | memset(password,0,512); 51 | strcpy(password,n); 52 | } 53 | 54 | char* User::getPassword(){ 55 | if (password[0]=0) 56 | return NULL; 57 | else 58 | return password; 59 | } 60 | 61 | User* User::getUNext(){ 62 | return unext; 63 | } 64 | 65 | void User::setUNext(User* us){ 66 | unext=us; 67 | } 68 | 69 | void User::setUType(u32 t){ 70 | utype=t; 71 | } 72 | 73 | u32 User::getUType(){ 74 | return utype; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/kernel/core/user.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef USER_H 3 | #define USER_H 4 | 5 | #include 6 | #include 7 | 8 | 9 | enum { 10 | USER_ROOT, //root 11 | USER_NORM //utilisateur normal 12 | }; 13 | 14 | class User : public File 15 | { 16 | public: 17 | User(char* n); 18 | ~User(); 19 | 20 | u32 open(u32 flag); 21 | u32 close(); 22 | u32 read(u8* buffer,u32 size); 23 | u32 write(u8* buffer,u32 size); 24 | u32 ioctl(u32 id,u8* buffer); 25 | u32 remove(); 26 | void scan(); 27 | 28 | 29 | void setPassword(char *n); 30 | char* getPassword(); 31 | 32 | User* getUNext(); 33 | void setUNext(User* us); 34 | 35 | void setUType(u32 t); 36 | u32 getUType(); 37 | 38 | protected: 39 | u32 utype; 40 | 41 | User* unext; 42 | char password[512]; 43 | 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/kernel/modules/Makefile: -------------------------------------------------------------------------------- 1 | OBJS:= $(OBJS) modules/module.o \ 2 | modules/null.o modules/stdtty.o modules/x86serial.o\ 3 | modules/ide.o modules/bochsvbe.o \ 4 | modules/ext2.o modules/dospartition.o \ 5 | modules/clock_x86.o modules/keys.o 6 | 7 | -------------------------------------------------------------------------------- /src/kernel/modules/bochsvbe.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __BOCHS_VBE__ 3 | #define __BOCHS_VBE__ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | 13 | #define VBE_DISPI_ENABLED (0x01) 14 | #define VBE_DISPI_DISABLED (0x00) 15 | #define VBE_DISPI_INDEX_ENABLE (4) 16 | #define VBE_DISPI_ID4 (0xB0C4) 17 | #define VBE_DISPI_LFB_ENABLED (0x40) 18 | #define VBE_DISPI_NOCLEARMEM (0x80) 19 | #define VBE_DISPI_IOPORT_INDEX (0x01CE) 20 | #define VBE_DISPI_INDEX_ID (0) 21 | #define VBE_DISPI_INDEX_XRES (1) 22 | #define VBE_DISPI_INDEX_YRES (2) 23 | #define VBE_DISPI_INDEX_BPP (3) 24 | #define VBE_DISPI_INDEX_ENABLE (4) 25 | #define VBE_DISPI_INDEX_BANK (5) 26 | #define VBE_DISPI_INDEX_VIRT_WIDTH (6) 27 | #define VBE_DISPI_INDEX_VIRT_HEIGHT (7) 28 | #define VBE_DISPI_INDEX_X_OFFSET (8) 29 | #define VBE_DISPI_INDEX_Y_OFFSET (9) 30 | #define VBE_DISPI_IOPORT_DATA (0x01CF) 31 | 32 | #define VBE_DISPI_LFB_PHYSICAL_ADDRESS 0xE0000000 33 | 34 | 35 | class Bochs : public Device 36 | { 37 | public: 38 | Bochs(char* n); 39 | ~Bochs(); 40 | 41 | 42 | u32 open(u32 flag); 43 | u32 close(); 44 | u32 read(u32 pos,u8* buffer,u32 size); 45 | u32 write(u32 pos,u8* buffer,u32 size); 46 | u32 ioctl(u32 id,u8* buffer); 47 | u32 remove(); 48 | void scan(); 49 | 50 | 51 | private: 52 | fb_info fbinfo_best; 53 | fb_info fbinfo; 54 | }; 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /src/kernel/modules/clock_x86.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __CLOCK__ 3 | #define __CLOCK__ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | class Clock_x86 : public Device 12 | { 13 | public: 14 | Clock_x86(char* n); 15 | ~Clock_x86(); 16 | 17 | 18 | u32 open(u32 flag); 19 | u32 close(); 20 | u32 read(u32 pos,u8* buffer,u32 size); 21 | u32 write(u32 pos,u8* buffer,u32 size); 22 | u32 ioctl(u32 id,u8* buffer); 23 | u32 remove(); 24 | void scan(); 25 | 26 | void reset_info(); 27 | private: 28 | clock_info cinfo; 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/kernel/modules/dospartition.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __DOS_PARTITION__ 3 | #define __DOS_PARTITION__ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define DOS_PART_1 0x01BE 10 | #define DOS_PART_2 0x01CE 11 | #define DOS_PART_3 0x01DE 12 | #define DOS_PART_4 0x01EE 13 | 14 | struct dos_partition { 15 | u8 bootable; /* 0 = no, 0x80 = bootable */ 16 | u8 s_head; /* Starting head */ 17 | u16 s_sector:6; /* Starting sector */ 18 | u16 s_cyl:10; /* Starting cylinder */ 19 | u8 id; /* System ID */ 20 | u8 e_head; /* Ending Head */ 21 | u16 e_sector:6; /* Ending Sector */ 22 | u16 e_cyl:10; /* Ending Cylinder */ 23 | u32 s_lba; /* Starting LBA value */ 24 | u32 size; /* Total Sectors in partition */ 25 | } __attribute__ ((packed)); 26 | 27 | /* 28 | * Driver class 29 | */ 30 | class DosPartition : public Device 31 | { 32 | public: 33 | DosPartition(char* n,File* dev,u32 num); 34 | ~DosPartition(); 35 | 36 | 37 | u32 open(u32 flag); 38 | u32 close(); 39 | u32 read(u32 pos,u8* buffer,u32 sizee); 40 | u32 write(u32 pos,u8* buffer,u32 sizee); 41 | u32 ioctl(u32 id,u8* buffer); 42 | u32 remove(); 43 | void scan(); 44 | 45 | 46 | private: 47 | u32 numpart; 48 | dos_partition* partition_info; 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /src/kernel/modules/ide.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __IDE__ 3 | #define __IDE__ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | class Ide : public Device 10 | { 11 | public: 12 | Ide(char* n); 13 | ~Ide(); 14 | 15 | 16 | u32 open(u32 flag); 17 | u32 close(); 18 | u32 read(u32 pos,u8* buffer,u32 size); 19 | u32 write(u32 pos,u8* buffer,u32 size); 20 | u32 ioctl(u32 id,u8* buffer); 21 | u32 remove(); 22 | void scan(); 23 | 24 | void setId(u32 flag); 25 | 26 | private: 27 | u32 id; 28 | 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/kernel/modules/keys.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | extern char* kbdmap; 8 | 9 | File* keys_mknod(char* name,u32 flag,File* dev){ 10 | Keyboard* cons=new Keyboard(name); 11 | return cons; 12 | } 13 | 14 | module("module.keyboard",MODULE_DEVICE,Keyboard,keys_mknod) 15 | 16 | Keyboard::~Keyboard(){ 17 | 18 | } 19 | 20 | Keyboard::Keyboard(char* n) : Device(n) 21 | { 22 | 23 | } 24 | 25 | void Keyboard::scan(){ 26 | 27 | } 28 | 29 | u32 Keyboard::close(){ 30 | return RETURN_OK; 31 | } 32 | 33 | u32 Keyboard::open(u32 flag){ 34 | return RETURN_OK; 35 | } 36 | 37 | u32 Keyboard::read(u32 pos,u8* buffer,u32 sizee){ 38 | return NOT_DEFINED; 39 | } 40 | 41 | u32 Keyboard::write(u32 pos,u8* buffer,u32 sizee){ 42 | return NOT_DEFINED; 43 | } 44 | 45 | u32 Keyboard::ioctl(u32 id,u8* buffer){ 46 | u32 ret=0; 47 | switch (id){ 48 | case DEV_GET_TYPE: 49 | ret=DEV_TYPE_TTY; 50 | break; 51 | 52 | case DEV_GET_STATE: 53 | ret=DEV_STATE_OK; 54 | break; 55 | 56 | case DEV_GET_FORMAT: 57 | ret=DEV_FORMAT_CHAR; 58 | break; 59 | 60 | case API_KEYBOARD_SET_TABLE: 61 | memcpy(scantable,(char*)buffer,TABLE_KEYBOARD_SIZE); 62 | kbdmap=scantable; 63 | ret=TABLE_KEYBOARD_SIZE; 64 | break; 65 | 66 | default: 67 | ret=NOT_DEFINED; 68 | } 69 | return ret; 70 | } 71 | 72 | u32 Keyboard::remove(){ 73 | delete this; 74 | return RETURN_OK; 75 | } 76 | -------------------------------------------------------------------------------- /src/kernel/modules/keys.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __KEYS__ 3 | #define __KEYS__ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | class Keyboard : public Device 12 | { 13 | public: 14 | Keyboard(char* n); 15 | ~Keyboard(); 16 | 17 | 18 | u32 open(u32 flag); 19 | u32 close(); 20 | u32 read(u32 pos,u8* buffer,u32 size); 21 | u32 write(u32 pos,u8* buffer,u32 size); 22 | u32 ioctl(u32 id,u8* buffer); 23 | u32 remove(); 24 | void scan(); 25 | 26 | private: 27 | char scantable[TABLE_KEYBOARD_SIZE]; 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/kernel/modules/module.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __MODULE__ 3 | #define __MODULE__ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | #define NO_FLAG 0 11 | 12 | class Module 13 | { 14 | public: 15 | Module(); 16 | ~Module(); 17 | 18 | void initLink(); 19 | void init(); 20 | 21 | File* createDevice(char* name,char* module,u32 flag); 22 | File* mount(char* dev,char* dir,char* module,u32 flag); 23 | File* install(char* dir,char* module,u32 flag,char* dev); 24 | }; 25 | 26 | extern Module modm; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/kernel/modules/modules.conf: -------------------------------------------------------------------------------- 1 | 2 | import_module(Console); 3 | import_module(Null); 4 | import_module(Ide); 5 | import_module(Bochs); 6 | import_module(Ext2); 7 | import_module(DosPartition); 8 | import_module(Keyboard); 9 | 10 | #ifdef __x86__ 11 | import_module(Clock_x86); 12 | import_module(X86Serial); 13 | #endif 14 | 15 | run_module_builder{ 16 | build_module(Console), 17 | build_module(Null), 18 | build_module(Ide), 19 | build_module(Bochs), 20 | build_module(X86Serial), 21 | build_module(Ext2), 22 | build_module(DosPartition), 23 | build_module(Clock_x86), 24 | build_module(Keyboard), 25 | end_module() 26 | }; 27 | 28 | std_buildin_module{ 29 | run_module(module.keyboard,key,NO_FLAG) 30 | 31 | run_module(module.stdio,tty,1) 32 | run_module(module.stdio,tty0,NO_FLAG) 33 | run_module(module.stdio,tty1,NO_FLAG) 34 | run_module(module.stdio,tty2,NO_FLAG) 35 | run_module(module.stdio,tty3,NO_FLAG) 36 | 37 | 38 | #ifdef __x86__ 39 | run_module(module.clock_x86,clock,NO_FLAG) /* clock info */ 40 | run_module(module.x86serial,ttyS,NO_FLAG) /* serial console */ 41 | run_module(module.ide,hda,0) /* hard disk 0 */ 42 | run_module(module.ide,hdb,1) /* hard disk 1 */ 43 | run_module(module.bvbe,fb0,0) /* BOCHS emulation vbe bios */ 44 | #endif 45 | 46 | run_module(module.null,null,0) 47 | run_module(module.zero,zero,0) 48 | 49 | 50 | } 51 | 52 | -------------------------------------------------------------------------------- /src/kernel/modules/null.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | File* null_mknod(char* name,u32 flag,File* dev){ 8 | Null* cons=new Null(name); 9 | return cons; 10 | } 11 | 12 | module("module.null",MODULE_DEVICE,Null,null_mknod) 13 | 14 | Null::~Null(){ 15 | 16 | } 17 | 18 | Null::Null(char* n) : Device(n) 19 | { 20 | 21 | } 22 | 23 | void Null::scan(){ 24 | 25 | } 26 | 27 | u32 Null::close(){ 28 | return RETURN_OK; 29 | } 30 | 31 | u32 Null::open(u32 flag){ 32 | return RETURN_OK; 33 | } 34 | 35 | u32 Null::read(u32 pos,u8* buffer,u32 size){ 36 | memset((char*)buffer,0,size); 37 | return size; 38 | } 39 | 40 | u32 Null::write(u32 pos,u8* buffer,u32 size){ 41 | return size; 42 | } 43 | 44 | u32 Null::ioctl(u32 id,u8* buffer){ 45 | u32 ret=0; 46 | switch (id){ 47 | case DEV_GET_TYPE: 48 | ret=DEV_TYPE_TTY; 49 | break; 50 | 51 | case DEV_GET_STATE: 52 | ret=DEV_STATE_OK; 53 | break; 54 | 55 | case DEV_GET_FORMAT: 56 | ret=DEV_FORMAT_CHAR; 57 | break; 58 | 59 | default: 60 | ret=NOT_DEFINED; 61 | } 62 | return ret; 63 | } 64 | 65 | u32 Null::remove(){ 66 | delete this; 67 | return RETURN_OK; 68 | } 69 | -------------------------------------------------------------------------------- /src/kernel/modules/null.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __NULL__ 3 | #define __NULL__ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | class Null : public Device 10 | { 11 | public: 12 | Null(char* n); 13 | ~Null(); 14 | 15 | 16 | u32 open(u32 flag); 17 | u32 close(); 18 | u32 read(u32 pos,u8* buffer,u32 size); 19 | u32 write(u32 pos,u8* buffer,u32 size); 20 | u32 ioctl(u32 id,u8* buffer); 21 | u32 remove(); 22 | void scan(); 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/kernel/modules/stdtty.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __CONSOLE__ 3 | #define __CONSOLE__ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | class Console : public Device 13 | { 14 | public: 15 | Console(char* n,u32 flag); 16 | ~Console(); 17 | 18 | 19 | u32 open(u32 flag); 20 | u32 close(); 21 | u32 read(u32 pos,u8* buffer,u32 size); 22 | u32 write(u32 pos,u8* buffer,u32 size); 23 | u32 ioctl(u32 id,u8* buffer); 24 | u32 remove(); 25 | void scan(); 26 | 27 | void reset_info(); 28 | 29 | private: 30 | tty_info_static sinfo; 31 | tty_info_moving minfo; 32 | 33 | Io* iotty; 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/kernel/modules/x86serial.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __X86Serial__ 3 | #define __X86Serial__ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define COM1 0x3F8 10 | #define IRQ_COM1 4 11 | #define COM2 2F8 12 | #define IRQ_COM2 3 13 | #define COM3 3E8 14 | #define IRQ_COM3 4 15 | #define COM4 2E8 16 | #define IRQ_COM4 3 17 | 18 | 19 | class X86Serial : public Device 20 | { 21 | public: 22 | X86Serial(char* n); 23 | ~X86Serial(); 24 | 25 | void putc(char c); 26 | char getc(); 27 | 28 | u32 open(u32 flag); 29 | u32 close(); 30 | u32 read(u32 pos,u8* buffer,u32 size); 31 | u32 write(u32 pos,u8* buffer,u32 size); 32 | u32 ioctl(u32 id,u8* buffer); 33 | u32 remove(); 34 | void scan(); 35 | 36 | private: 37 | static u8 init_serial; 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/kernel/runtime/Makefile: -------------------------------------------------------------------------------- 1 | OBJS:= $(OBJS) runtime/cxx.o runtime/itoa.o runtime/buffer.o \ 2 | runtime/memory.o runtime/string.o 3 | 4 | -------------------------------------------------------------------------------- /src/kernel/runtime/alloc.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ALLOC_H 3 | #define ALLOC_H 4 | 5 | 6 | extern "C" { 7 | void *ksbrk(int); 8 | void *kmalloc(unsigned long); 9 | void kfree(void *); 10 | } 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/kernel/runtime/buffer.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | 6 | 7 | Buffer::Buffer(char* n,u32 siz){ 8 | map=(char*)kmalloc(siz); 9 | size=siz; 10 | memcpy(map,n,siz); 11 | } 12 | 13 | Buffer::Buffer(){ 14 | size=0; 15 | map=NULL; 16 | } 17 | 18 | Buffer::~Buffer(){ 19 | if (map!=NULL) 20 | kfree(map); 21 | } 22 | 23 | void Buffer::add(u8* c,u32 s){ 24 | char* old=map; 25 | map=(char*)kmalloc(size+s); 26 | memcpy(map,old,size); 27 | kfree(old); 28 | memcpy((char*)(map+size),(char*)c,s); 29 | size=size+s; 30 | } 31 | 32 | u32 Buffer::get(u8* c,u32 s){ 33 | if( s>size) 34 | s=size; 35 | memcpy((char*)c,(char*)(map+(size-s)),s); 36 | char*old=map; 37 | map=(char*)kmalloc(size-s); 38 | memcpy(map,old,(size-s)); 39 | kfree(old); 40 | size=size-s; 41 | return s; 42 | } 43 | 44 | u32 Buffer::isEmpty(){ 45 | if (size==0) 46 | return 1; 47 | else 48 | return 0; 49 | } 50 | 51 | void Buffer::clear(){ 52 | size=0; 53 | if (map!=NULL) 54 | kfree(map); 55 | } 56 | 57 | Buffer &Buffer::operator>>(char *c) 58 | { 59 | memcpy(c,map,size); 60 | return *this; 61 | } 62 | -------------------------------------------------------------------------------- /src/kernel/runtime/buffer.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef BUFFER_H 3 | #define BUFFER_H 4 | 5 | 6 | class Buffer 7 | { 8 | public: 9 | Buffer(char* n,u32 siz); 10 | Buffer(); 11 | ~Buffer(); 12 | 13 | void add(u8* c,u32 s); 14 | u32 get(u8* c,u32 s); 15 | void clear(); 16 | u32 isEmpty(); 17 | 18 | 19 | Buffer &operator>>(char *c); 20 | 21 | 22 | u32 size; 23 | char* map; 24 | 25 | }; 26 | 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/kernel/runtime/itoa.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/runtime/itoa.cc -------------------------------------------------------------------------------- /src/kernel/runtime/libc.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef LIBC_H 3 | #define LIBC_H 4 | 5 | #include 6 | 7 | extern "C" { 8 | void itoa(char *buf, unsigned long int n, int base); 9 | 10 | void * memset(char *dst,char src, int n); 11 | void * memcpy(char *dst, char *src, int n); 12 | 13 | 14 | int strlen(char *s); 15 | int strcmp(const char *dst, char *src); 16 | int strcpy(char *dst,const char *src); 17 | void strcat(void *dest,const void *src); 18 | char * strncpy(char *destString, const char *sourceString,int maxLength); 19 | int strncmp( const char* s1, const char* s2, int c ); 20 | } 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /src/kernel/runtime/list.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef __LIST__ 4 | #define __LIST__ 5 | 6 | 7 | struct list_head { 8 | struct list_head *next, *prev; 9 | }; 10 | 11 | #define LIST_HEAD_INIT(name) { &(name), &(name) } 12 | 13 | #define LIST_HEAD(name) \ 14 | struct list_head name = LIST_HEAD_INIT(name) 15 | 16 | static inline void INIT_LIST_HEAD(struct list_head *list) 17 | { 18 | list->next = list; 19 | list->prev = list; 20 | } 21 | 22 | static inline void list_add(struct list_head *neww, struct list_head *head) 23 | { 24 | neww->next = head->next; 25 | neww->prev = head; 26 | (head->next)->prev = neww; 27 | head->next = neww; 28 | } 29 | 30 | static inline void list_del(struct list_head *p) 31 | { 32 | (p->next)->prev = p->prev; 33 | (p->prev)->next = p->next; 34 | p->next = 0; 35 | p->prev = 0; 36 | } 37 | 38 | static inline int list_empty(const struct list_head *head) 39 | { 40 | return head->next == head; 41 | } 42 | 43 | #define list_entry(ptr, type, member) \ 44 | (type*) ((char*) ptr - (char*) &((type*)0)->member) 45 | 46 | #define list_first_entry(head, type, member) \ 47 | list_entry((head)->next, type, member) 48 | 49 | #define list_for_each(p, head) \ 50 | for (p = (head)->next; p != (head); p = p->next) 51 | 52 | #define list_for_each_safe(p, n, head) \ 53 | for (p = (head)->next, n = p->next; p != (head); p = n, n = n->next) 54 | 55 | #define list_for_each_entry(p, head, member) \ 56 | for (p = list_entry((head)->next, typeof(*p), member); \ 57 | &p->member != (head); \ 58 | p = list_entry(p->member.next, typeof(*p), member)) \ 59 | 60 | 61 | #endif /* __LIST__ */ 62 | -------------------------------------------------------------------------------- /src/kernel/runtime/memory.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/kernel/runtime/memory.cc -------------------------------------------------------------------------------- /src/kernel/runtime/string.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | 6 | extern "C" 7 | { 8 | int strlen(char *s) 9 | { 10 | int i = 0; 11 | while (*s++) 12 | i++; 13 | return i; 14 | } 15 | 16 | char *strncpy(char *destString, const char *sourceString,int maxLength) 17 | { 18 | unsigned count; 19 | 20 | if ((destString == (char *) NULL) || (sourceString == (char *) NULL)) 21 | { 22 | return (destString = NULL); 23 | } 24 | 25 | if (maxLength > 255) 26 | maxLength = 255; 27 | 28 | for (count = 0; (int)count < (int)maxLength; count ++) 29 | { 30 | destString[count] = sourceString[count]; 31 | 32 | if (sourceString[count] == '\0') 33 | break; 34 | } 35 | 36 | if (count >= 255) 37 | { 38 | return (destString = NULL); 39 | } 40 | 41 | return (destString); 42 | } 43 | 44 | int strcmp(const char *dst, char *src) 45 | { 46 | int i = 0; 47 | 48 | while ((dst[i] == src[i])) { 49 | if (src[i++] == 0) 50 | return 0; 51 | } 52 | 53 | return 1; 54 | } 55 | 56 | 57 | int strcpy(char *dst,const char *src) 58 | { 59 | int i = 0; 60 | while ((dst[i] = src[i++])); 61 | 62 | return i; 63 | } 64 | 65 | 66 | void strcat(void *dest,const void *src) 67 | { 68 | memcpy((char*)((int)dest+(int)strlen((char*)dest)),(char*)src,strlen((char*)src)); 69 | } 70 | 71 | 72 | int strncmp( const char* s1, const char* s2, int c ) { 73 | int result = 0; 74 | 75 | while ( c ) { 76 | result = *s1 - *s2++; 77 | 78 | if ( ( result != 0 ) || ( *s1++ == 0 ) ) { 79 | break; 80 | } 81 | 82 | c--; 83 | } 84 | 85 | return result; 86 | } 87 | } 88 | 89 | -------------------------------------------------------------------------------- /src/kernel/runtime/string.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef STRING_H 3 | #define STRING_H 4 | 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/kernel/runtime/types.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef TYPES_H 3 | #define TYPES_H 4 | 5 | /* 6 | * General C-Types 7 | */ 8 | typedef unsigned char u8; 9 | typedef unsigned short u16; 10 | typedef unsigned int u32; 11 | typedef unsigned long long u64; 12 | 13 | 14 | typedef signed char s8; 15 | typedef signed short s16; 16 | typedef signed int s32; 17 | typedef signed long long s64; 18 | 19 | 20 | typedef unsigned char u_char; 21 | 22 | typedef unsigned int size_t; 23 | typedef int pid_t; 24 | typedef s64 ino_t; 25 | typedef s64 off_t; 26 | typedef int dev_t; 27 | typedef int mode_t; 28 | typedef int nlink_t; 29 | typedef int uid_t; 30 | typedef int gid_t; 31 | typedef int blksize_t; 32 | typedef s64 blkcnt_t; 33 | #define time_t s64 34 | 35 | struct stat_fs { 36 | dev_t st_dev; 37 | ino_t st_ino; 38 | mode_t st_mode; 39 | nlink_t st_nlink; 40 | uid_t st_uid; 41 | gid_t st_gid; 42 | dev_t st_rdev; 43 | off_t st_size; 44 | blksize_t st_blksize; 45 | blkcnt_t st_blocks; 46 | time_t st_atime; 47 | time_t st_mtime; 48 | time_t st_ctime; 49 | }; 50 | 51 | 52 | /* 53 | * Return code 54 | */ 55 | enum{ 56 | RETURN_OK=0, 57 | NOT_DEFINED=-1, //If not implemented 58 | ERROR_MEMORY=-2, 59 | PARAM_NULL=-3, 60 | ERROR_PARAM=-4, 61 | RETURN_FAILURE=-128 //Added by NoMaintener aka William. In case of error 62 | }; 63 | 64 | 65 | /* 66 | * Interruption handler 67 | */ 68 | typedef void (*int_handler)(void); 69 | 70 | 71 | #define NULL 0 72 | #define true 1 73 | #define false 0 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /src/sdk/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | @echo "Building LibC" 4 | make -C ./src/libc 5 | 6 | clean: 7 | make -C ./src/libc clean 8 | -------------------------------------------------------------------------------- /src/sdk/bootdisk/bin/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/bootdisk/bin/.gitkeep -------------------------------------------------------------------------------- /src/sdk/bootdisk/bin/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/bootdisk/bin/hello -------------------------------------------------------------------------------- /src/sdk/bootdisk/boot/grub/grub.conf: -------------------------------------------------------------------------------- 1 | default=0 2 | timeout=30 3 | 4 | root (hd0,0) 5 | 6 | title=Dev Operating System (with init) 7 | kernel /kernel.elf 8 | module /bin/hello 9 | boot 10 | 11 | title=Dev Operating System 12 | kernel /kernel.elf 13 | boot 14 | -------------------------------------------------------------------------------- /src/sdk/bootdisk/boot/grub/menu.lst: -------------------------------------------------------------------------------- 1 | default=0 2 | timeout=30 3 | 4 | root (cd) 5 | 6 | title=Dev Operating System (with init) 7 | kernel /kernel.elf 8 | module /bin/hello 9 | boot 10 | 11 | title=Dev Operating System 12 | kernel /kernel.elf 13 | boot 14 | -------------------------------------------------------------------------------- /src/sdk/bootdisk/boot/grub/stage1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/bootdisk/boot/grub/stage1 -------------------------------------------------------------------------------- /src/sdk/bootdisk/boot/grub/stage2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/bootdisk/boot/grub/stage2 -------------------------------------------------------------------------------- /src/sdk/bootdisk/boot/grub/stage2_eltorito: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/bootdisk/boot/grub/stage2_eltorito -------------------------------------------------------------------------------- /src/sdk/build.mak: -------------------------------------------------------------------------------- 1 | CP = cp 2 | RM = rm 3 | MKDIR = mkdir 4 | TRUE = true 5 | 6 | CC = gcc 7 | CXX = g++ 8 | AS = nasm 9 | LD = ld 10 | AR = ar 11 | RANLIB = ranlib 12 | STRIP = strip 13 | 14 | INCDIR = $(SDKDIR)/include 15 | LIBDIR = $(SDKDIR)/lib 16 | 17 | DEFS:= $(DEFS) 18 | 19 | CFLAGS = -I $(INCDIR) -Wall -fno-builtin -g -O2 -w -trigraphs -fno-exceptions -fno-stack-protector -O0 -m32 -fno-rtti $(DEFS) 20 | CXXFLAGS = $(CFLAGS) 21 | ASFLAGS = $(CFLAGS) 22 | 23 | ifeq ($(CRT_FILE),) 24 | CRT_FILE = crt_c.o 25 | endif 26 | 27 | LDFLAGS = -melf_i386 -L $(INCDIR) -T ./linker.ld --entry=_start -nostdlib -L $(LIBDIR) -lc $(LIBS) 28 | MYOS_VERSION=500 29 | 30 | 31 | all: $(TARGET) 32 | 33 | $(TARGET): $(OBJS) 34 | $(LD) -o $@ $^ $(SDKDIR)/lib/$(CRT_FILE) $(LDFLAGS) 35 | cp $(TARGET) $(SDKDIR)/bootdisk/bin/$(TARGET) 36 | 37 | install: 38 | cp $(TARGET) $(SDKDIR)/bootdisk/bin/$(TARGET) 39 | 40 | run: 41 | cd $(SDKDIR) && sh ./diskimage.sh 42 | cd $(SDKDIR) && sh ./qemu.sh 43 | 44 | %.o: %.c 45 | $(CC) $(CFLAGS) -c $< 46 | 47 | %.o: %.cpp 48 | $(CXX) $(CXXFLAGS) -c $< 49 | 50 | %.o: %.cc 51 | $(CXX) $(CXXFLAGS) -c $< 52 | 53 | .PHONY: clean 54 | 55 | clean: 56 | rm -rf *.o $(TARGET) 57 | 58 | -------------------------------------------------------------------------------- /src/sdk/c.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/c.img -------------------------------------------------------------------------------- /src/sdk/diskimage.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | qemu-img create c.img 2M 3 | fdisk ./c.img << EOF 4 | x 5 | c 6 | 4 7 | h 8 | 16 9 | s 10 | 63 11 | r 12 | n 13 | p 14 | 1 15 | 1 16 | 4 17 | a 18 | 1 19 | w 20 | EOF 21 | fdisk -l -u ./c.img 22 | losetup -o 32256 /dev/loop1 ./c.img 23 | 24 | mke2fs /dev/loop1 25 | mount /dev/loop1 /mnt/ 26 | cp -R bootdisk/* /mnt/ 27 | umount /mnt/ 28 | grub --device-map=/dev/null << EOF 29 | device (hd0) ./c.img 30 | geometry (hd0) 4 16 63 31 | root (hd0,0) 32 | setup (hd0) 33 | quit 34 | EOF 35 | 36 | losetup -d /dev/loop1 37 | -------------------------------------------------------------------------------- /src/sdk/include/_ansi.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _ANSIDECL_H_ 4 | #define _ANSIDECL_H_ 5 | 6 | 7 | 8 | /* ISO C++. */ 9 | 10 | #ifdef __cplusplus 11 | #if !(defined(_BEGIN_STD_C) && defined(_END_STD_C)) 12 | #ifdef _HAVE_STD_CXX 13 | #define _BEGIN_STD_C namespace std { extern "C" { 14 | #define _END_STD_C } } 15 | #else 16 | #define _BEGIN_STD_C extern "C" { 17 | #define _END_STD_C } 18 | #endif 19 | #if defined(__GNUC__) && \ 20 | ( (__GNUC__ >= 4) || \ 21 | ( (__GNUC__ >= 3) && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ >= 3) ) ) 22 | #define _NOTHROW __attribute__ ((nothrow)) 23 | #else 24 | #define _NOTHROW throw() 25 | #endif 26 | #endif 27 | #else 28 | #define _BEGIN_STD_C 29 | #define _END_STD_C 30 | #define _NOTHROW 31 | #endif 32 | 33 | 34 | #endif /* _ANSIDECL_H_ */ 35 | -------------------------------------------------------------------------------- /src/sdk/include/alloca.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _ALLOCA_H_ 5 | #define _ALLOCA_H_ 6 | 7 | #undef alloca 8 | #define alloca(size) __builtin_alloca(size) 9 | 10 | #endif /* _ALLOCA_H_ */ 11 | -------------------------------------------------------------------------------- /src/sdk/include/arpa/inet.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _ARPA_INET_H_ 4 | #define _ARPA_INET_H_ 5 | 6 | #include /* struct in_addr */ 7 | 8 | int inet_aton( const char* cp, struct in_addr* inp ); 9 | 10 | in_addr_t inet_addr( const char* cp ); 11 | 12 | in_addr_t inet_network( const char* cp ); 13 | 14 | char* inet_ntoa( struct in_addr in ); 15 | 16 | struct in_addr inet_makeaddr( int net, int host ); 17 | 18 | in_addr_t inet_lnaof( struct in_addr in ); 19 | 20 | in_addr_t inet_netof( struct in_addr in ); 21 | 22 | #endif /* _ARPA_INET_H_ */ 23 | -------------------------------------------------------------------------------- /src/sdk/include/assert.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #undef assert 5 | 6 | #ifdef NDEBUG 7 | #define assert(expr) ((void)0) 8 | #else 9 | #define assert(expr) \ 10 | if ( !(expr) ) { __assert_fail( #expr, __FILE__, __LINE__ ); } 11 | #endif /* NDEBUG */ 12 | 13 | #ifndef _ASSERT_H_ 14 | #define _ASSERT_H_ 15 | 16 | #include 17 | #include 18 | 19 | static inline void __assert_fail( const char* expr, const char* file, int line ) { 20 | printf( "Assertion (%s) failed at %s:%d\n", expr, file, line ); 21 | abort(); 22 | } 23 | 24 | #endif /* _ASSERT_H_ */ 25 | -------------------------------------------------------------------------------- /src/sdk/include/ctype.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _CTYPE_H_ 4 | #define _CTYPE_H_ 5 | 6 | int isupper( int c ); 7 | int islower( int c ); 8 | int isalpha( int c ); 9 | int isdigit( int c ); 10 | int isxdigit( int c ); 11 | int isalnum( int c ); 12 | int isblank( int c ); 13 | int isspace( int c ); 14 | int isprint( int c ); 15 | int iscntrl( int c ); 16 | int isgraph( int c ); 17 | int ispunct( int c ); 18 | int isascii( int c ); 19 | 20 | int tolower( int c ); 21 | int toupper( int c ); 22 | 23 | typedef struct { 24 | long quot; 25 | long rem; 26 | }ldiv_t; 27 | 28 | typedef struct { 29 | int quot; 30 | int rem; 31 | }div_t; 32 | 33 | div_t div ( int numerator, int denominator ); 34 | ldiv_t ldiv ( long numerator, long denominator ); 35 | 36 | 37 | 38 | 39 | #endif // _CTYPE_H_ 40 | -------------------------------------------------------------------------------- /src/sdk/include/dirent.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _DIRENT_H_ 5 | #define _DIRENT_H_ 6 | 7 | #include 8 | 9 | 10 | typedef struct DIR { 11 | int fd; 12 | struct dirent entry; 13 | } DIR; 14 | 15 | DIR* opendir( const char* name ); 16 | int closedir( DIR* dir ); 17 | 18 | struct dirent* readdir( DIR* dir ); 19 | int readdir_r( DIR* dir, struct dirent* entry, struct dirent** result ); 20 | 21 | void rewinddir( DIR* dir ); 22 | 23 | #endif /* _DIRENT_H_ */ 24 | -------------------------------------------------------------------------------- /src/sdk/include/endian.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _ENDIAN_H_ 5 | #define _ENDIAN_H_ 6 | 7 | #define __LITTLE_ENDIAN 1234 8 | #define __BIG_ENDIAN 4321 9 | 10 | #define __BYTE_ORDER __LITTLE_ENDIAN 11 | 12 | #endif /* _ENDIAN_H_ */ 13 | -------------------------------------------------------------------------------- /src/sdk/include/errno.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _ERRNO_H_ 5 | #define _ERRNO_H_ 6 | 7 | #define ENOMEM 1 8 | #define EINVAL 2 9 | #define EIO 3 10 | #define ETIME 4 11 | #define ENOSYS 5 12 | #define ENOENT 6 13 | #define EEXIST 7 14 | #define EBUSY 8 15 | #define EISDIR 9 16 | #define ENOINO 10 17 | #define ENOEXEC 11 18 | #define EBADF 12 19 | #define EHW 13 20 | #define ERANGE 14 21 | #define ENXIO 15 22 | #define EDOM 16 23 | #define ENODEV 17 24 | #define EINTR 18 25 | #define ENOTTY 19 26 | #define EPERM 20 27 | #define EROFS 21 28 | #define ELOOP 22 29 | #define ENOTDIR 23 30 | #define ENOTEMPTY 24 31 | #define EAGAIN 25 32 | #define E2BIG 26 33 | #define ETIMEDOUT 27 34 | #define EOVERFLOW 28 35 | #define ENOSPC 29 36 | #define ECHILD 30 37 | #define ENAMETOOLONG 31 38 | #define ESPIPE 32 39 | #define EACCES 33 40 | 41 | extern int errno; 42 | 43 | #endif /* _ERRNO_H_ */ 44 | -------------------------------------------------------------------------------- /src/sdk/include/fcntl.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _FCNTL_H_ 5 | #define _FCNTL_H_ 6 | 7 | #include 8 | #include 9 | 10 | #define O_RDONLY 0x01 11 | #define O_WRONLY 0x02 12 | #define O_RDWR 0x03 13 | #define O_CREAT 0x04 14 | #define O_TRUNC 0x08 15 | #define O_APPEND 0x10 16 | #define O_EXCL 0x20 17 | #define O_NONBLOCK 0x40 18 | 19 | #define F_DUPFD 0 20 | #define F_GETFD 1 21 | #define F_SETFD 2 22 | #define F_GETFL 3 23 | #define F_SETFL 4 24 | 25 | #define FD_CLOEXEC 1 26 | 27 | int open( const char* filename, int flags, ... ) __nonnull((1)); 28 | int creat( const char* pathname, mode_t mode ) __nonnull((1)); 29 | int fcntl( int fd, int cmd, ... ); 30 | 31 | #endif /* _FCNTL_H_ */ 32 | -------------------------------------------------------------------------------- /src/sdk/include/getopt.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _GETOPT_H_ 4 | #define _GETOPT_H_ 5 | 6 | typedef struct option { 7 | const char *name; 8 | int has_arg; 9 | int* flag; 10 | int val; 11 | } option_t ; 12 | 13 | #define no_argument 0 14 | #define required_argument 1 15 | #define optional_argument 2 16 | 17 | 18 | extern char *optarg; 19 | extern int optind, opterr, optopt; 20 | 21 | int getopt( int argc, char* const * argv, const char* opstring ); 22 | 23 | int getopt_long( int argc, char* const * argv, const char* shortopts, 24 | const struct option* longopts, int* longind ); 25 | 26 | int getopt_long_only( int argc, char* const * argv, 27 | const char* shortopts, const struct option* longopts, int* longind ); 28 | 29 | #endif // _GETOPT_H_ 30 | -------------------------------------------------------------------------------- /src/sdk/include/inttypes.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _INTTYPES_H_ 5 | #define _INTTYPES_H_ 6 | 7 | #include 8 | 9 | intmax_t strtoimax( const char *nptr, char** endptr, int base ); 10 | uintmax_t strtoumax( const char *nptr, char** endptr, int base ); 11 | 12 | #endif /* _INTTYPES_H_ */ 13 | -------------------------------------------------------------------------------- /src/sdk/include/limits.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _LIMITS_H_ 5 | #define _LIMITS_H_ 6 | 7 | #ifndef __INT_MAX__ 8 | #define __INT_MAX__ 2147483647 9 | #endif 10 | #ifndef __LONG_MAX__ 11 | #if __WORDSIZE == 64 12 | #define __LONG_MAX__ 9223372036854775807L 13 | #else 14 | #define __LONG_MAX__ 2147483647L 15 | #endif 16 | #endif 17 | 18 | #define CHAR_BIT 8 19 | 20 | #define SCHAR_MIN (-128) 21 | #define SCHAR_MAX 127 22 | 23 | #define UCHAR_MAX 255 24 | 25 | #ifdef __CHAR_UNSIGNED__ 26 | #define CHAR_MIN 0 27 | #define CHAR_MAX UCHAR_MAX 28 | #else 29 | #define CHAR_MIN SCHAR_MIN 30 | #define CHAR_MAX SCHAR_MAX 31 | #endif 32 | 33 | #define SHRT_MIN (-32768) 34 | #define SHRT_MAX 32767 35 | 36 | #define USHRT_MAX 65535 37 | 38 | #define INT_MIN (-1 - INT_MAX) 39 | #define INT_MAX (__INT_MAX__) 40 | #define UINT_MAX (INT_MAX * 2U + 1U) 41 | 42 | #define LONG_MIN (-1L - LONG_MAX) 43 | #define LONG_MAX ((__LONG_MAX__) + 0L) 44 | #define ULONG_MAX (LONG_MAX * 2UL + 1UL) 45 | 46 | #define LLONG_MAX 9223372036854775807LL 47 | #define LLONG_MIN (-LLONG_MAX - 1LL) 48 | #define ULLONG_MAX 18446744073709551615ULL 49 | 50 | #define PATH_MAX 256 51 | #define MB_LEN_MAX 16 52 | 53 | #endif // _LIMITS_H_ 54 | -------------------------------------------------------------------------------- /src/sdk/include/linker.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT(elf32-i386) 2 | OUTPUT_ARCH(i386) 3 | ENTRY (_start) 4 | 5 | SECTIONS{ 6 | . = 0x40000000; 7 | 8 | .text :{ 9 | *(.text) 10 | } 11 | 12 | .data ALIGN (0x1000) : { 13 | start_ctors = .; 14 | *(.ctor*) 15 | end_ctors = .; 16 | start_dtors = .; 17 | *(.dtor*) 18 | end_dtors = .; 19 | *(.data) 20 | } 21 | 22 | 23 | .rodata ALIGN (0x1000) : { 24 | *(.rodata) 25 | } 26 | 27 | .data ALIGN (0x1000) : { 28 | *(.data) 29 | } 30 | 31 | .bss : { 32 | sbss = .; 33 | *(COMMON) 34 | *(.bss) 35 | ebss = .; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/sdk/include/locale.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _LOCALE_H_ 5 | #define _LOCALE_H_ 6 | 7 | enum { 8 | LC_CTYPE = 0, 9 | LC_NUMERIC = 1, 10 | LC_TIME = 2, 11 | LC_COLLATE = 3, 12 | LC_MONETARY = 4, 13 | LC_MESSAGES = 5, 14 | LC_ALL = 6, 15 | LC_PAPER = 7, 16 | LC_NAME = 8, 17 | LC_ADDRESS = 9, 18 | LC_TELEPHONE = 10, 19 | LC_MEASUREMENT = 11, 20 | LC_IDENTIFICATION = 12 21 | }; 22 | 23 | struct lconv { 24 | char* decimal_point; 25 | char* thousands_sep; 26 | char* grouping; 27 | char* int_curr_symbol; 28 | char* currency_symbol; 29 | char* mon_decimal_point; 30 | char* mon_thousands_sep; 31 | char* mon_grouping; 32 | char* positive_sign; 33 | char* negative_sign; 34 | char int_frac_digits; 35 | char frac_digits; 36 | char p_cs_precedes; 37 | char p_sep_by_space; 38 | char n_cs_precedes; 39 | char n_sep_by_space; 40 | char p_sign_posn; 41 | char n_sign_posn; 42 | char int_p_cs_precedes; 43 | char int_p_sep_by_space; 44 | char int_n_cs_precedes; 45 | char int_n_sep_by_space; 46 | char int_p_sign_posn; 47 | char int_n_sign_posn; 48 | }; 49 | 50 | struct lconv* localeconv( void ); 51 | 52 | char* setlocale( int category, const char* locale ); 53 | 54 | #endif /* _LOCALE_H_ */ 55 | -------------------------------------------------------------------------------- /src/sdk/include/math.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _MATH_H_ 5 | #define _MATH_H_ 6 | 7 | #define HUGE_VAL \ 8 | (__extension__ \ 9 | ((union { unsigned __l __attribute__((__mode__(__DI__))); double __d; }) \ 10 | { __l: 0x7ff0000000000000ULL }).__d) 11 | 12 | int finite( double x ); 13 | 14 | double sin( double x ); 15 | double cos( double x ); 16 | double log( double x ); 17 | double log10( double x ); 18 | double exp( double x ); 19 | double floor( double x ); 20 | double ceil( double x ); 21 | double frexp( double x, int* exp ); 22 | double fabs( double x ); 23 | double fmod( double x, double y ); 24 | double atan2( double y, double x ); 25 | double hypot( double x, double y ); 26 | double pow( double x, double y ); 27 | double ldexp( double x, int exp ); 28 | double scalbn( double x, int exp ); 29 | double modf( double x, double* iptr ); 30 | 31 | #endif // _MATH_H_ 32 | -------------------------------------------------------------------------------- /src/sdk/include/os.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _OS_H_ 3 | #define _OS_H_ 4 | 5 | #include 6 | 7 | #include "../../kernel/core/api/kernel/syscall.h" 8 | #include "../../kernel/core/api/kernel/syscall_table.h" 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /src/sdk/include/pwd.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _PWD_H_ 5 | #define _PWD_H_ 6 | 7 | #include 8 | 9 | struct passwd { 10 | char* pw_name; 11 | char* pw_passwd; 12 | uid_t pw_uid; 13 | gid_t pw_gid; 14 | char* pw_gecos; 15 | char* pw_dir; 16 | char* pw_shell; 17 | }; 18 | 19 | struct passwd* getpwnam( const char* name ); 20 | struct passwd* getpwent( void ); 21 | struct passwd* getpwuid( uid_t uid ); 22 | 23 | void setpwent( void ); 24 | void endpwent( void ); 25 | 26 | #endif /* _PWD_H_ */ 27 | -------------------------------------------------------------------------------- /src/sdk/include/setjmp.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _SETJMP_H_ 5 | #define _SETJMP_H_ 6 | 7 | typedef unsigned long jmp_buf[ 6 ]; 8 | 9 | int setjmp( jmp_buf env ); 10 | void longjmp( jmp_buf env, int val ); 11 | 12 | #endif // _SETJMP_H_ 13 | -------------------------------------------------------------------------------- /src/sdk/include/stdarg.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _STDARG_H_ 4 | #define _STDARG_H_ 5 | 6 | typedef __builtin_va_list va_list; 7 | 8 | #define va_start(a,b) __builtin_va_start(a,b) 9 | #define va_end(a) __builtin_va_end(a) 10 | #define va_arg(a,b) __builtin_va_arg(a,b) 11 | #define __va_copy(d,s) __builtin_va_copy((d),(s)) 12 | 13 | #endif // _STDARG_H_ 14 | -------------------------------------------------------------------------------- /src/sdk/include/stddef.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _STDDEF_H 3 | #define _STDDEF_H 4 | 5 | #ifndef NULL 6 | #define NULL 0 7 | #endif 8 | 9 | 10 | #ifndef _HAVE_PTRDIFF_T 11 | #define _HAVE_PTRDIFF_T 12 | typedef signed long int ptrdiff_t; 13 | #endif 14 | 15 | #ifndef _HAVE_SIZE_T 16 | #define _HAVE_SIZE_T 17 | //typedef unsigned int size_t; 18 | typedef long unsigned int size_t; 19 | #endif 20 | 21 | 22 | #if ( (! defined _HAVE_WCHAR_T) && (! defined __cplusplus ) ) 23 | #define _HAVE_WCHAR_T 24 | typedef int wchar_t; 25 | #endif 26 | 27 | #define offsetof(struct_type, member) \ 28 | (size_t) &(((struct_type *)0)->member) 29 | 30 | /*#define offsetof(type, memberdesig) \ 31 | ((const unsigned int)((ptrdiff_t)&(type.memberdesig) - (ptrdiff_t)&type)) 32 | */ 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /src/sdk/include/string.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _STRING_H_ 4 | #define _STRING_H_ 5 | 6 | #define __need_size_t 7 | #define __need_NULL 8 | #include 9 | 10 | void* memset( void* s, int c, size_t n ); 11 | void* memcpy( void* d, const void* s, size_t n ); 12 | int memcmp( const void* p1, const void* p2, size_t c ); 13 | void* memmove( void* dest, const void* src, size_t n ); 14 | void* memchr( const void* s, int c, size_t n ); 15 | 16 | size_t strlen( const char* s ); 17 | size_t strnlen( const char* s, size_t count ); 18 | char* strchr( const char* s, int c ); 19 | char* strrchr( const char* s, int c ); 20 | char* strstr( const char* s1, const char* s2 ); 21 | int strcmp( const char* s1, const char* s2 ); 22 | int strncmp( const char* s1, const char* s2, size_t c ); 23 | int strcasecmp( const char* s1, const char* s2 ); 24 | int strncasecmp( const char* s1, const char* s2, size_t c ); 25 | char* strcpy( char* d, const char* s ); 26 | char* strncpy( char* d, const char* s, size_t c ); 27 | char* strcat( char* d, const char* s ); 28 | char* strncat( char* d, const char* s, size_t c ); 29 | char* strpbrk( const char* s, const char* accept ); 30 | size_t strspn( const char* s, const char* accept ); 31 | size_t strcspn( const char* s, const char* reject ); 32 | char* strtok_r( char* s, const char* delim, char** ptrptr ); 33 | char* strtok( char* s, const char* delim ); 34 | 35 | char* strdup( const char* s ); 36 | char* strndup( const char* s, size_t n); 37 | 38 | char* strerror( int errnum ); 39 | char* strsignal( int signum ); 40 | 41 | #endif /* _STRING_H_ */ 42 | -------------------------------------------------------------------------------- /src/sdk/include/strings.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _STRINGS_H_ 5 | #define _STRINGS_H_ 6 | 7 | #endif /* _STRINGS_H_ */ 8 | -------------------------------------------------------------------------------- /src/sdk/include/sys/cdefs.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _SYS_CDEFS_H_ 4 | #define _SYS_CDEFS_H_ 5 | 6 | #define __nonnull(params) __attribute__((__nonnull__ params)) 7 | 8 | #endif // _SYS_CDEFS_H_ 9 | -------------------------------------------------------------------------------- /src/sdk/include/sys/ioctl.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _SYS_IOCTL_H_ 4 | #define _SYS_IOCTL_H_ 5 | 6 | #include 7 | 8 | #include "../../kernel/core/api/dev/ioctl.h" 9 | 10 | int ioctl( int fd, int request, ... ); 11 | 12 | #endif /* _SYS_IOCTL_H_ */ 13 | -------------------------------------------------------------------------------- /src/sdk/include/sys/mount.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _SYS_MOUNT_H_ 4 | #define _SYS_MOUNT_H_ 5 | 6 | #define MOUNT_NONE 0 7 | #define MOUNT_RO 1 8 | #define MOUNT_NOATIME 2 9 | 10 | int mount( 11 | const char* source, 12 | const char* target, 13 | const char* filesystemtype, 14 | unsigned long mountflags, 15 | const void* data 16 | ); 17 | 18 | int umount( 19 | const char* dir 20 | ); 21 | 22 | #endif /* _SYS_MOUNT_H_ */ 23 | -------------------------------------------------------------------------------- /src/sdk/include/sys/param.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _SYS_PARAM_H_ 4 | #define _SYS_PARAM_H_ 5 | 6 | #include 7 | 8 | #undef MIN 9 | #define MIN(a,b) ((a)<(b)?(a):(b)) 10 | 11 | #undef MAX 12 | #define MAX(a,b) ((a)>(b)?(a):(b)) 13 | 14 | #define MAXPATHLEN PATH_MAX 15 | 16 | #endif // _SYS_PARAM_H_ 17 | -------------------------------------------------------------------------------- /src/sdk/include/sys/resource.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _SYS_RESOURCE_H_ 4 | #define _SYS_RESOURCE_H_ 5 | 6 | #include 7 | 8 | struct rusage { 9 | struct timeval ru_utime; /* user time used */ 10 | struct timeval ru_stime; /* system time used */ 11 | long ru_maxrss; /* maximum resident set size */ 12 | long ru_ixrss; /* integral shared memory size */ 13 | long ru_idrss; /* integral unshared data size */ 14 | long ru_isrss; /* integral unshared stack size */ 15 | long ru_minflt; /* page reclaims */ 16 | long ru_majflt; /* page faults */ 17 | long ru_nswap; /* swaps */ 18 | long ru_inblock; /* block input operations */ 19 | long ru_oublock; /* block output operations */ 20 | long ru_msgsnd; /* messages sent */ 21 | long ru_msgrcv; /* messages received */ 22 | long ru_nsignals; /* signals received */ 23 | long ru_nvcsw; /* voluntary context switches */ 24 | long ru_nivcsw; /* involuntary context switches */ 25 | }; 26 | 27 | #endif /* _SYS_RESOURCE_H_ */ 28 | -------------------------------------------------------------------------------- /src/sdk/include/sys/select.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _SYS_SELECT_H_ 4 | #define _SYS_SELECT_H_ 5 | 6 | #include 7 | #include 8 | 9 | #define FD_ZERO(set) \ 10 | memset( (set)->fds, 0, 1024 / 32 ); 11 | 12 | #define FD_CLR(fd,set) \ 13 | (set)->fds[fd/32] &= ~(1<<(fd%32)); 14 | 15 | #define FD_SET(fd,set) \ 16 | (set)->fds[fd/32] |= (1<<(fd%32)); 17 | 18 | #define FD_ISSET(fd,set) \ 19 | ((set)->fds[fd/32] & (1<<(fd%32))) 20 | 21 | typedef struct fd_set { 22 | uint32_t fds[ 1024 / 32 ]; 23 | } fd_set; 24 | 25 | int select( int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout ); 26 | 27 | #endif // _SYS_SELECT_H_ 28 | -------------------------------------------------------------------------------- /src/sdk/include/sys/time.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _SYS_TIME_H_ 4 | #define _SYS_TIME_H_ 5 | 6 | #include 7 | 8 | int gettimeofday( struct timeval* tv, struct timezone* tz ); 9 | 10 | #endif // _SYS_TIME_H_ 11 | -------------------------------------------------------------------------------- /src/sdk/include/sys/types.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _SYS_TYPES_H_ 4 | #define _SYS_TYPES_H_ 5 | 6 | #include 7 | 8 | #define __need_size_t 9 | #include 10 | 11 | #ifndef NULL 12 | #ifdef __cplusplus 13 | #define NULL 0 14 | #else 15 | #define NULL ((void*)0) 16 | #endif /* __cplusplus */ 17 | #endif /* NULL */ 18 | 19 | #define INFINITE_TIMEOUT 18446744073709551615ULL 20 | 21 | typedef unsigned char u_char; 22 | 23 | typedef int ssize_t; 24 | typedef int pid_t; 25 | typedef int64_t ino_t; 26 | typedef int64_t off_t; 27 | typedef int dev_t; 28 | typedef int mode_t; 29 | typedef int nlink_t; 30 | typedef int uid_t; 31 | typedef int gid_t; 32 | typedef int blksize_t; 33 | typedef int64_t blkcnt_t; 34 | 35 | #endif /* _SYS_TYPES_H_ */ 36 | -------------------------------------------------------------------------------- /src/sdk/include/sys/wait.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _SYS_WAIT_H_ 4 | #define _SYS_WAIT_H_ 5 | 6 | #include 7 | #include 8 | 9 | #define WNOHANG 1 10 | #define WUNTRACED 2 11 | 12 | pid_t wait( int* status ); 13 | pid_t waitpid( pid_t pid, int* status, int options ); 14 | pid_t wait3( int* status, int options, struct rusage* rusage ); 15 | pid_t wait4( pid_t pid, int* status, int options, struct rusage* rusage ); 16 | 17 | #endif /* _SYS_WAIT_H_ */ 18 | -------------------------------------------------------------------------------- /src/sdk/include/utime.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _UTIME_H_ 5 | #define _UTIME_H_ 6 | 7 | #include 8 | 9 | struct utimbuf { 10 | time_t actime; /* access time */ 11 | time_t modtime; /* modification time */ 12 | }; 13 | 14 | int utime( const char* filename, const struct utimbuf* times ); 15 | int utimes( const char* filename, const timeval_t times[2] ); 16 | 17 | #endif // _UTIME_H_ 18 | -------------------------------------------------------------------------------- /src/sdk/lib/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/lib/.gitkeep -------------------------------------------------------------------------------- /src/sdk/lib/libc.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/lib/libc.a -------------------------------------------------------------------------------- /src/sdk/qemu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | qemu -m 1024 -s -hda ./c.img -curses -serial /dev/tty -redir tcp:2323::23 3 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/getpagesize.c: -------------------------------------------------------------------------------- 1 | /* getpagesize function 2 | * 3 | * Copyright (c) 2009 Zoltan Kovacs 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of version 2 of the GNU General Public License 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License along 15 | * with this program; if not, write to the Free Software Foundation, Inc., 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | */ 18 | 19 | #include 20 | 21 | int getpagesize( void ) { 22 | return 4096; 23 | } 24 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/longjmp.S: -------------------------------------------------------------------------------- 1 | /* longjmp function 2 | * 3 | * Copyright (c) 2009 Zoltan Kovacs 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of version 2 of the GNU General Public License 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License along 15 | * with this program; if not, write to the Free Software Foundation, Inc., 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | */ 18 | 19 | .section .text 20 | 21 | .global longjmp 22 | 23 | longjmp: 24 | movl 4(%esp), %edx 25 | movl 8(%esp), %eax 26 | 27 | /* Restore registers */ 28 | 29 | movl 4(%edx), %ebp 30 | movl 8(%edx), %esp 31 | movl 12(%edx), %ebx 32 | movl 16(%edx), %edi 33 | movl 20(%edx), %esi 34 | 35 | /* Restore return address */ 36 | 37 | movl 0(%edx), %ecx 38 | movl %ecx, 0(%esp) 39 | 40 | /* Check return code */ 41 | 42 | cmpl $0, %eax 43 | je 1f 44 | ret 45 | 46 | 1: 47 | movl $1, %eax 48 | ret 49 | 50 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/e_atan2.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by J.T. Conklin . 3 | * Public domain. 4 | */ 5 | 6 | .section .text 7 | 8 | .align 4 9 | 10 | .global atan2 11 | 12 | 13 | atan2: 14 | fldl 4(%esp) 15 | fldl 12(%esp) 16 | fpatan 17 | ret 18 | 19 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/e_exp.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by J.T. Conklin . 3 | * Public domain. 4 | */ 5 | 6 | /* e^x = 2^(x * log2(e)) */ 7 | 8 | .section .text 9 | 10 | .align 4 11 | 12 | .global exp 13 | 14 | 15 | exp: 16 | fldl 4(%esp) 17 | /* I added the following ugly construct because exp(+-Inf) resulted 18 | in NaN. The ugliness results from the bright minds at Intel. 19 | For the i686 the code can be written better. 20 | -- drepper@cygnus.com. */ 21 | fxam /* Is NaN or +-Inf? */ 22 | fstsw %ax 23 | movb $0x45, %dh 24 | andb %ah, %dh 25 | cmpb $0x05, %dh 26 | je 1f /* Is +-Inf, jump. */ 27 | fldl2e 28 | fmulp /* x * log2(e) */ 29 | fld %st 30 | frndint /* int(x * log2(e)) */ 31 | fsubr %st,%st(1) /* fract(x * log2(e)) */ 32 | fxch 33 | f2xm1 /* 2^(fract(x * log2(e))) - 1 */ 34 | fld1 35 | faddp /* 2^(fract(x * log2(e))) */ 36 | fscale /* e^x */ 37 | fstp %st(1) 38 | ret 39 | 40 | 1: testl $0x200, %eax /* Test sign. */ 41 | jz 2f /* If positive, jump. */ 42 | fstp %st 43 | fldz /* Set result to 0. */ 44 | 2: ret 45 | 46 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/e_fmod.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by J.T. Conklin . 3 | * Public domain. 4 | */ 5 | 6 | .section .text 7 | 8 | .align 4 9 | 10 | .global fmod 11 | 12 | 13 | fmod: 14 | fldl 12(%esp) 15 | fldl 4(%esp) 16 | 1: fprem 17 | fstsw %ax 18 | sahf 19 | jp 1b 20 | fstp %st(1) 21 | ret 22 | 23 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/e_log.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by J.T. Conklin . 3 | * Public domain. 4 | * 5 | * Changed to use fyl2xp1 for values near 1, . 6 | */ 7 | 8 | .section .rodata 9 | 10 | .align 4 11 | 12 | 13 | one: 14 | .double 1.0 15 | 16 | 17 | /* It is not important that this constant is precise. It is only 18 | a value which is known to be on the safe side for using the 19 | fyl2xp1 instruction. */ 20 | 21 | 22 | limit: 23 | .double 0.29 24 | 25 | 26 | 27 | #ifdef PIC 28 | #define MO(op) op##@GOTOFF(%edx) 29 | #else 30 | #define MO(op) op 31 | #endif 32 | 33 | .section .text 34 | 35 | .global log 36 | 37 | 38 | log: 39 | fldln2 // log(2) 40 | fldl 4(%esp) // x : log(2) 41 | fxam 42 | fnstsw 43 | #ifdef PIC 44 | LOAD_PIC_REG (dx) 45 | #endif 46 | fld %st // x : x : log(2) 47 | sahf 48 | jc 3f // in case x is NaN or +-Inf 49 | 4: fsubl MO(one) // x-1 : x : log(2) 50 | fld %st // x-1 : x-1 : x : log(2) 51 | fabs // |x-1| : x-1 : x : log(2) 52 | fcompl MO(limit) // x-1 : x : log(2) 53 | fnstsw // x-1 : x : log(2) 54 | andb $0x45, %ah 55 | jz 2f 56 | fstp %st(1) // x-1 : log(2) 57 | fyl2xp1 // log(x) 58 | ret 59 | 60 | 2: fstp %st(0) // x : log(2) 61 | fyl2x // log(x) 62 | ret 63 | 64 | 3: jp 4b // in case x is +-Inf 65 | fstp %st(1) 66 | fstp %st(1) 67 | ret 68 | 69 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/e_log10.S: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/src/libc/arch/i386/math/e_log10.S -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/e_pow.S: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/src/libc/arch/i386/math/e_pow.S -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/s_ceil.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by J.T. Conklin . 3 | * Public domain. 4 | */ 5 | 6 | .section .text 7 | 8 | .align 4 9 | 10 | .global ceil 11 | 12 | 13 | ceil: 14 | fldl 4(%esp) 15 | subl $8,%esp 16 | 17 | fstcw 4(%esp) /* store fpu control word */ 18 | 19 | /* We use here %edx although only the low 1 bits are defined. 20 | But none of the operations should care and they are faster 21 | than the 16 bit operations. */ 22 | movl $0x0800,%edx /* round towards +oo */ 23 | orl 4(%esp),%edx 24 | andl $0xfbff,%edx 25 | movl %edx,(%esp) 26 | fldcw (%esp) /* load modified control word */ 27 | 28 | frndint /* round */ 29 | 30 | fldcw 4(%esp) /* restore original control word */ 31 | 32 | addl $8,%esp 33 | ret 34 | 35 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/s_cos.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by J.T. Conklin . 3 | * Public domain. 4 | */ 5 | 6 | .section .text 7 | 8 | .align 4 9 | 10 | .global cos 11 | 12 | 13 | cos: 14 | fldl 4(%esp) 15 | fcos 16 | fnstsw %ax 17 | testl $0x400,%eax 18 | jnz 1f 19 | ret 20 | 21 | .align 4 22 | 23 | 1: fldpi 24 | fadd %st(0) 25 | fxch %st(1) 26 | 2: fprem1 27 | fnstsw %ax 28 | testl $0x400,%eax 29 | jnz 2b 30 | fstp %st(1) 31 | fcos 32 | ret 33 | 34 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/s_fabs.S: -------------------------------------------------------------------------------- 1 | .section .text 2 | 3 | .align 4 4 | 5 | .global fabs 6 | 7 | 8 | fabs: 9 | fldl 4(%esp) 10 | fabs 11 | ret 12 | 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/s_finite.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by Joe Keane . 3 | */ 4 | 5 | .section .text 6 | 7 | .align 4 8 | 9 | .global finite 10 | 11 | 12 | finite: 13 | movl 8(%esp),%eax 14 | movl $0xFFEFFFFF,%ecx 15 | subl %eax,%ecx 16 | xorl %ecx,%eax 17 | shrl $31, %eax 18 | ret 19 | 20 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/s_floor.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by J.T. Conklin . 3 | * Public domain. 4 | */ 5 | 6 | .section .text 7 | 8 | .align 4 9 | 10 | .global floor 11 | 12 | 13 | floor: 14 | fldl 4(%esp) 15 | subl $8,%esp 16 | 17 | fstcw 4(%esp) /* store fpu control word */ 18 | 19 | /* We use here %edx although only the low 1 bits are defined. 20 | But none of the operations should care and they are faster 21 | than the 16 bit operations. */ 22 | movl $0x400,%edx /* round towards -oo */ 23 | orl 4(%esp),%edx 24 | andl $0xf7ff,%edx 25 | movl %edx,(%esp) 26 | fldcw (%esp) /* load modified control word */ 27 | 28 | frndint /* round */ 29 | 30 | fldcw 4(%esp) /* restore original control word */ 31 | 32 | addl $8,%esp 33 | ret 34 | 35 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/s_scalbn.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by J.T. Conklin . 3 | * Public domain. 4 | */ 5 | 6 | .section .text 7 | 8 | .align 4 9 | 10 | .global scalbn 11 | 12 | 13 | scalbn: 14 | fildl 12(%esp) 15 | fldl 4(%esp) 16 | fscale 17 | fstp %st(1) 18 | ret 19 | 20 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/math/s_sin.S: -------------------------------------------------------------------------------- 1 | /* 2 | * Written by J.T. Conklin . 3 | * Public domain. 4 | */ 5 | 6 | .section .text 7 | 8 | .align 4 9 | 10 | .global sin 11 | 12 | 13 | sin: 14 | fldl 4(%esp) 15 | fsin 16 | fnstsw %ax 17 | testl $0x400,%eax 18 | jnz 1f 19 | ret 20 | 21 | .align 4 22 | 23 | 1: fldpi 24 | fadd %st(0) 25 | fxch %st(1) 26 | 2: fprem1 27 | fnstsw %ax 28 | testl $0x400,%eax 29 | jnz 2b 30 | fstp %st(1) 31 | fsin 32 | ret 33 | 34 | -------------------------------------------------------------------------------- /src/sdk/src/libc/arch/i386/setjmp.S: -------------------------------------------------------------------------------- 1 | /* setjmp function 2 | * 3 | * Copyright (c) 2009 Zoltan Kovacs 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of version 2 of the GNU General Public License 7 | * as published by the Free Software Foundation. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License along 15 | * with this program; if not, write to the Free Software Foundation, Inc., 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | */ 18 | 19 | .section .text 20 | 21 | .global setjmp 22 | 23 | setjmp: 24 | movl 4(%esp), %edx 25 | 26 | /* Save return address */ 27 | 28 | movl 0(%esp), %ecx 29 | movl %ecx, 0(%edx) 30 | 31 | /* Save registers: ebp, esp, ebx, edi and esi */ 32 | 33 | movl %ebp, 4(%edx) 34 | movl %esp, 8(%edx) 35 | movl %ebx, 12(%edx) 36 | movl %edi, 16(%edx) 37 | movl %esi, 20(%edx) 38 | 39 | /* Return 0 */ 40 | 41 | movl $0, %eax 42 | ret 43 | 44 | -------------------------------------------------------------------------------- /src/sdk/src/libc/libc.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/src/libc/libc.a -------------------------------------------------------------------------------- /src/sdk/src/libc/src/closedir.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int closedir( DIR* dir ) { 10 | if ( dir == NULL ) { 11 | errno = EBADF; 12 | return -1; 13 | } 14 | 15 | close( dir->fd ); 16 | free( dir ); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isalnum.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isalnum( int c ) { 7 | return ( ( isalpha( c ) ) || ( isdigit( c ) ) ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isalpha.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isalpha( int c ) { 7 | return ( islower( c ) || isupper( c ) ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isascii.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isascii( int c ) { 7 | return ( ( unsigned int )c < 128u ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isblank.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isblank( int c ) { 7 | return ( c == ' ' || c == '\t' ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/iscntrl.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int iscntrl( int c ) { 7 | return ( ( ( unsigned int )c < 32u ) || 8 | ( c == 127 ) ); 9 | } 10 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isdigit.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isdigit( int c ) { 7 | return ( ( c >= '0' ) && ( c <= '9' ) ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isgraph.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isgraph( int c ) { 7 | if ( c == ' ' ) { 8 | return 0; 9 | } 10 | 11 | return isprint( c ); 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/islower.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int islower( int c ) { 7 | return ( ( c >= 'a' ) && ( c <= 'z' ) ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isprint.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isprint( int c ) { 7 | c &= 0x7F; 8 | return ( ( c >= 0x20 ) && ( c < 0x7F ) ); 9 | } 10 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/ispunct.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int ispunct( int c ) { 7 | return ( isprint( c ) && 8 | !isalnum( c ) && 9 | !isspace( c ) ); 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isspace.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isspace( int c ) { 7 | return ( c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' ' ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isupper.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isupper( int c ) { 7 | return ( ( c >= 'A' ) && ( c <= 'Z' ) ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/isxdigit.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int isxdigit( int c ) { 7 | return ( ( ( c >= '0' ) && ( c <= '9' ) ) || 8 | ( ( c >= 'a' ) && ( c <= 'f' ) ) || 9 | ( ( c >= 'A' ) && ( c <= 'F' ) ) ); 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/toascii.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int toascii( int c ) { 7 | return ( c & ~0x80 ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/tolower.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int tolower( int c ) { 7 | if ( isupper( c ) ) { 8 | return c | 0x20; 9 | } else { 10 | return c; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/ctype/toupper.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | int toupper( int c ) { 6 | if ( islower( c ) ) { 7 | return c & ~0x20; 8 | } else { 9 | return c; 10 | } 11 | } 12 | 13 | ldiv_t ldiv(long numerator, long denominator) { 14 | ldiv_t x; 15 | x.quot=numerator/denominator; 16 | x.rem=numerator-x.quot*denominator; 17 | return x; 18 | } 19 | 20 | div_t div(int numerator, int denominator) { 21 | div_t x; 22 | x.quot=numerator/denominator; 23 | x.rem=numerator-x.quot*denominator; 24 | return x; 25 | } 26 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/fcntl/creat.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int creat( const char *pathname, mode_t mode ) { 7 | return open( pathname, O_CREAT | O_WRONLY | O_TRUNC, mode ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/fcntl/fcntl.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int fcntl( int fd, int cmd, ... ) { 10 | int error; 11 | 12 | error = syscall3( SYS_fcntl, fd, cmd, *( ( ( int* )&cmd ) + 1 ) ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/fcntl/open.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int open( const char* filename, int flags, ... ) { 10 | int error; 11 | 12 | error = syscall2( SYS_open, ( int )filename, flags ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/getopt/getopt_long.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include /* NULL */ 7 | 8 | #include 9 | #include "getopt_int.h" 10 | 11 | int getopt_long( int argc, char* const * argv, const char* shortopts, 12 | const struct option* longopts, int* longind ){ 13 | return _getopt_internal (argc, argv, shortopts, longopts, longind, 0); 14 | } 15 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/getopt/getopt_long_only.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include /* NULL */ 7 | 8 | #include 9 | #include "getopt_int.h" 10 | 11 | int getopt_long_only( int argc, char* const * argv, 12 | const char* shortopts, const struct option* longopts, int* longind ){ 13 | return _getopt_internal (argc, argv, shortopts, longopts, longind, 1); 14 | } 15 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/locale/localeconv.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | static const struct lconv _locale = { 8 | .decimal_point = ".", 9 | .thousands_sep = "", 10 | .grouping = "", 11 | .int_curr_symbol = "", 12 | .currency_symbol = "", 13 | .mon_decimal_point = ".", 14 | .mon_thousands_sep = "", 15 | .mon_grouping = "", 16 | .positive_sign = "+", 17 | .negative_sign = "-", 18 | .int_frac_digits = CHAR_MAX, 19 | .frac_digits = CHAR_MAX, 20 | .p_cs_precedes = CHAR_MAX, 21 | .p_sep_by_space = CHAR_MAX, 22 | .n_cs_precedes = CHAR_MAX, 23 | .n_sep_by_space = CHAR_MAX, 24 | .p_sign_posn = CHAR_MAX, 25 | .n_sign_posn = CHAR_MAX, 26 | .int_p_cs_precedes = CHAR_MAX, 27 | .int_p_sep_by_space = CHAR_MAX, 28 | .int_n_cs_precedes = CHAR_MAX, 29 | .int_n_sep_by_space = CHAR_MAX, 30 | .int_p_sign_posn = CHAR_MAX, 31 | .int_n_sign_posn = CHAR_MAX 32 | }; 33 | 34 | struct lconv* localeconv( void ) { 35 | return ( struct lconv* ) &_locale; 36 | } 37 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/locale/setlocale.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | char* setlocale( int category, const char* locale ) { 8 | /* TODO */ 9 | 10 | return NULL; 11 | } 12 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/math/s_ldexp.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | double ldexp( double value, int exp ) { 8 | if ( !finite(value) || value == 0.0 ) return value; 9 | value = scalbn(value,exp); 10 | if ( !finite(value) || value == 0.0 ) errno = ERANGE; 11 | return value; 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/network/inet_aton.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int inet_aton( const char* cp, struct in_addr* inp ) { 9 | int i; 10 | unsigned int ip = 0; 11 | char* tmp= ( char* )cp; 12 | 13 | for ( i = 24; ; ) { 14 | long j; 15 | 16 | j = strtoul( tmp, &tmp, 0 ); 17 | 18 | if ( *tmp == 0 ) { 19 | ip |= j; 20 | 21 | break; 22 | } else if ( *tmp == '.' ) { 23 | if ( j > 255 ) { 24 | return 0; 25 | } 26 | 27 | ip |= ( j << i ); 28 | 29 | if ( i > 0 ) { 30 | i -= 8; 31 | } 32 | 33 | ++tmp; 34 | 35 | continue; 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | inp->s_addr = htonl( ip ); 42 | 43 | return 1; 44 | } 45 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/network/inet_ntoa.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | char* inet_ntoa( struct in_addr in ) { 10 | unsigned int ip; 11 | static char __inet_ntoa_result[18]; 12 | int i; 13 | uint8_t bytes[4]; 14 | uint8_t* addrbyte; 15 | 16 | ip = in.s_addr; 17 | 18 | addrbyte = (uint8_t *)&ip; 19 | 20 | for(i = 0; i < 4; i++) { 21 | bytes[i] = *addrbyte++; 22 | } 23 | 24 | snprintf (__inet_ntoa_result, 18, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]); 25 | 26 | return __inet_ntoa_result; 27 | } 28 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/opendir.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | DIR* opendir( const char* name ) { 9 | DIR* dir; 10 | 11 | dir = ( DIR* )malloc( sizeof( DIR ) ); 12 | 13 | if ( dir == NULL ) { 14 | return NULL; 15 | } 16 | 17 | dir->fd = open( name, O_RDONLY ); 18 | 19 | if ( dir->fd < 0 ) { 20 | free( dir ); 21 | return NULL; 22 | } 23 | 24 | return dir; 25 | } 26 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/os/debug.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/os/ipc.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | #include 6 | 7 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/os/module.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | #include 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/os/os.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/os/region.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/os/semaphore.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/os/sysinfo.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | #include 7 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/os/thread.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | #include 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/pwd/endpwent.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | extern int _passwd_db_position; 7 | 8 | void endpwent( void ) { 9 | _passwd_db_position = 0; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/pwd/getpwent.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | 7 | typedef struct passwd_myos passwd_myos; 8 | struct passwd_myos { 9 | char name[64]; 10 | char password[64]; 11 | int uid; 12 | int gid; 13 | char gecos[64]; 14 | char dir[512]; 15 | char shell[64]; 16 | }; 17 | 18 | passwd_myos myos_pass; 19 | 20 | passwd_myos* myos_user_getN(const char* name){ 21 | int ret=syscall2(68,(uint32_t)name,(uint32_t)&myos_pass); 22 | if (ret!=0) 23 | return &myos_pass; 24 | else 25 | return NULL; 26 | } 27 | 28 | passwd_myos* myos_user_getID(int id){ 29 | int ret=syscall2(69,id,(uint32_t)&myos_pass); 30 | if (ret!=0) 31 | return &myos_pass; 32 | else 33 | return NULL; 34 | } 35 | 36 | int _passwd_db_position = 0; 37 | 38 | struct passwd __tmp_passwd; 39 | 40 | void build_tmp_passwd(){ 41 | __tmp_passwd.pw_name=myos_pass.name; 42 | __tmp_passwd.pw_passwd=myos_pass.password; 43 | __tmp_passwd.pw_uid=myos_pass.uid; 44 | __tmp_passwd.pw_gid=myos_pass.gid; 45 | __tmp_passwd.pw_gecos=myos_pass.gecos; 46 | __tmp_passwd.pw_dir=myos_pass.dir; 47 | __tmp_passwd.pw_shell=myos_pass.shell; 48 | } 49 | 50 | 51 | struct passwd* getpwent( void ) { 52 | 53 | passwd_myos* pass=myos_user_getID(_passwd_db_position); 54 | if (pass==NULL) 55 | return NULL; 56 | 57 | build_tmp_passwd(); 58 | _passwd_db_position++; 59 | return &__tmp_passwd; 60 | } 61 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/pwd/getpwnam.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | 7 | typedef struct passwd_myos passwd_myos; 8 | struct passwd_myos { 9 | char name[64]; 10 | char password[64]; 11 | int uid; 12 | int gid; 13 | char gecos[64]; 14 | char dir[512]; 15 | char shell[64]; 16 | }; 17 | 18 | extern struct passwd __tmp_passwd; 19 | passwd_myos* myos_user_getN(const char* name); 20 | void build_tmp_passwd(); 21 | 22 | struct passwd* getpwnam( const char* name ) { 23 | 24 | passwd_myos* pass=myos_user_getN(name); 25 | if (pass==NULL) 26 | return NULL; 27 | 28 | build_tmp_passwd(); 29 | return &__tmp_passwd; 30 | } 31 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/pwd/getpwuid.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | 7 | typedef struct passwd_myos passwd_myos; 8 | struct passwd_myos { 9 | char name[64]; 10 | char password[64]; 11 | int uid; 12 | int gid; 13 | char gecos[64]; 14 | char dir[512]; 15 | char shell[64]; 16 | }; 17 | 18 | extern struct passwd __tmp_passwd; 19 | 20 | passwd_myos* myos_user_getID(int id); 21 | 22 | 23 | struct passwd* getpwuid( uid_t uid ) { 24 | 25 | passwd_myos* pass=myos_user_getID(uid); 26 | if (pass==NULL) 27 | return NULL; 28 | 29 | build_tmp_passwd(); 30 | return &__tmp_passwd; 31 | } 32 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/pwd/setpwent.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | extern int _passwd_db_position; 7 | 8 | void setpwent( void ) { 9 | _passwd_db_position = 0; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/readdir.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | struct dirent* readdir( DIR* dir ) { 9 | int error; 10 | 11 | if ( dir == NULL ) { 12 | return NULL; 13 | } 14 | 15 | error = getdents( dir->fd, &dir->entry, sizeof( struct dirent ) ); 16 | 17 | if ( error == 0 ) { 18 | return NULL; 19 | } 20 | 21 | return &dir->entry; 22 | } 23 | 24 | int readdir_r( DIR* dir, struct dirent* entry, struct dirent** result ) { 25 | int error; 26 | 27 | if ( ( dir == NULL ) || 28 | ( entry == NULL ) ) { 29 | errno = -EINVAL; 30 | return -1; 31 | } 32 | 33 | error = getdents( dir->fd, entry, sizeof( struct dirent ) ); 34 | 35 | if ( error == 0 ) { 36 | *result = NULL; 37 | } else { 38 | *result = entry; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/rewinddir.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | void rewinddir( DIR* dirp ) { 10 | int error; 11 | 12 | error = syscall1( SYS_rewinddir, dirp->fd ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/kill.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int kill( pid_t pid, int signal ) { 10 | int error; 11 | 12 | error = syscall2( 13 | SYS_kill, 14 | pid, 15 | signal 16 | ); 17 | 18 | if ( error < 0 ) { 19 | errno = -error; 20 | return -1; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/killpg.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int killpg( int pgrp, int signal ) { 7 | return -1; 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/raise.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | 7 | 8 | int raise( int signal ) { 9 | printf( "raise(): Not yet implemented!\n" ); 10 | 11 | return -1; 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/sigaction.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int sigaction( int signum, const struct sigaction* act, struct sigaction* oldact ) { 10 | int error; 11 | 12 | error = syscall3( 13 | SYS_sigaction, 14 | signum, 15 | ( int )act, 16 | ( int )oldact 17 | ); 18 | 19 | if ( error < 0 ) { 20 | errno = -error; 21 | return -1; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/sigaddset.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int sigaddset( sigset_t* set, int signum ) { 8 | if ( ( set == NULL ) || 9 | ( signum < 1 ) || 10 | ( signum >= _NSIG ) ) { 11 | errno = -EINVAL; 12 | return -1; 13 | } 14 | 15 | *set |= ( 1ULL << ( signum - 1 ) ); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/sigdelset.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int sigdelset( sigset_t* set, int signum ) { 8 | if ( ( set == NULL ) || 9 | ( signum < 1 ) || 10 | ( signum >= _NSIG ) ) { 11 | errno = -EINVAL; 12 | return -1; 13 | } 14 | 15 | *set &= ~( 1ULL << ( signum - 1 ) ); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/sigemptyset.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int sigemptyset( sigset_t* set ) { 8 | if ( set == NULL ) { 9 | errno = -EINVAL; 10 | return -1; 11 | } 12 | 13 | *set = 0; 14 | 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/sigfillset.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int sigfillset( sigset_t* set ) { 9 | if ( set == NULL ) { 10 | errno = -EINVAL; 11 | return -1; 12 | } 13 | 14 | memset( ( void* )set, 0xFF, sizeof( sigset_t ) ); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/sigismember.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int sigismember( const sigset_t* set, int signum ) { 8 | if ( ( set == NULL ) || 9 | ( signum < 1 ) || 10 | ( signum >= _NSIG ) ) { 11 | errno = -EINVAL; 12 | return -1; 13 | } 14 | 15 | if ( ( ( *set ) & ( 1ULL << ( signum - 1 ) ) ) != 0 ) { 16 | return 1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/signal.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | sighandler_t signal( int signum, sighandler_t handler ) { 8 | int error; 9 | struct sigaction act; 10 | struct sigaction oldact; 11 | 12 | if ( ( handler == SIG_ERR ) || 13 | ( signum < 0 ) || 14 | ( signum >= _NSIG ) ) { 15 | errno = -EINVAL; 16 | return SIG_ERR; 17 | } 18 | 19 | act.sa_handler = handler; 20 | 21 | error = sigemptyset( &act.sa_mask ); 22 | 23 | if ( error < 0 ) { 24 | return SIG_ERR; 25 | } 26 | 27 | error = sigaddset( &act.sa_mask, signum ); 28 | 29 | if ( error < 0 ) { 30 | return SIG_ERR; 31 | } 32 | 33 | act.sa_flags = SA_RESTART; 34 | 35 | if ( sigaction( signum, &act, &oldact ) < 0 ) { 36 | return SIG_ERR; 37 | } 38 | 39 | return oldact.sa_handler; 40 | } 41 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/signal/sigprocmask.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int sigprocmask( int how, const sigset_t* set, sigset_t* oldset ) { 10 | int error; 11 | 12 | error = syscall3( 13 | SYS_sigprocmask, 14 | how, 15 | ( int )set, 16 | ( int )oldset 17 | ); 18 | 19 | if ( error < 0 ) { 20 | errno = -error; 21 | return -1; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sscanf.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/sdk/src/libc/src/sscanf.c -------------------------------------------------------------------------------- /src/sdk/src/libc/src/start.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define MAX_ENV_COUNT 256 7 | 8 | 9 | int main(int argc, char *argv []); 10 | 11 | int errno; 12 | 13 | char** environ={ 14 | "PATH","/bin/", 15 | NULL,NULL 16 | }; 17 | 18 | int __environ_allocated; 19 | 20 | 21 | void _start(int argc, char** argv) { 22 | stdout=fdopen(0,"rw"); 23 | stdin=fdopen(1,"rw"); 24 | stderr=fdopen(2,"rw"); 25 | int error; 26 | environ = 0; 27 | __environ_allocated = 0; 28 | 29 | /* Call the main function of the application */ 30 | 31 | error = main( argc, argv); 32 | 33 | /* Exit the process */ 34 | 35 | exit( error ); 36 | } 37 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/clearerr.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | void clearerr( FILE* stream ) { 7 | stream->flags &= ~( __FILE_EOF | __FILE_ERROR ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fclose.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int fclose( FILE* stream ) { 9 | int result; 10 | 11 | result = fflush( stream ); 12 | result |= close( stream->fd ); 13 | 14 | if ( ( ( stream->flags & __FILE_DONTFREEBUF ) == 0 ) && 15 | ( stream->buffer != NULL ) ) { 16 | free( stream->buffer ); 17 | } 18 | 19 | free( stream ); 20 | 21 | return result; 22 | } 23 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fdopen.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int __parse_mode( const char* mode ); 8 | FILE* __init_file( int fd, int close_on_error, int mode ); 9 | 10 | FILE* fdopen( int fd, const char* mode ) { 11 | int flags; 12 | 13 | if ( fd < 0 ) { 14 | errno = -EINVAL; 15 | return NULL; 16 | } 17 | 18 | flags = __parse_mode( mode ); 19 | 20 | return __init_file( fd, 0, flags ); 21 | } 22 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/feof.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int feof( FILE* stream ) { 7 | if ( stream->has_ungotten ) { 8 | return 0; 9 | } 10 | 11 | return ( stream->flags & __FILE_EOF ); 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/ferror.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int ferror( FILE* stream ) { 7 | return ( stream->flags & __FILE_ERROR ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fflush.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int fflush( FILE* stream ) { 8 | if ( stream->flags & __FILE_BUFINPUT ) { 9 | int tmp; 10 | 11 | tmp = ( int )stream->buffer_pos - ( int )stream->buffer_data_size; 12 | 13 | if ( tmp != 0 ) { 14 | lseek( stream->fd, tmp, SEEK_CUR ); 15 | } 16 | 17 | stream->buffer_pos = 0; 18 | stream->buffer_data_size = 0; 19 | } else { 20 | if ( stream->buffer_pos > 0 ) { 21 | if ( write( stream->fd, stream->buffer, stream->buffer_pos ) != stream->buffer_pos ) { 22 | write(0,"\nerror file \n",strlen("\nerror file \n")); 23 | stream->flags |= __FILE_ERROR; 24 | return -1; 25 | } 26 | memset(stream->buffer,0,_IO_BUFSIZE); 27 | stream->buffer_pos = 0; 28 | } 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fgetc.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include "stdio_internal.h" 8 | 9 | int fgetc( FILE* stream ) { 10 | unsigned char c; 11 | 12 | /* Check if we can read from the stream */ 13 | 14 | if ( ( ( stream->flags & __FILE_CAN_READ ) == 0 ) || 15 | ( __set_stream_flags( stream, __FILE_BUFINPUT ) ) ) { 16 | stream->flags |= __FILE_ERROR; 17 | printf("EOF ! \n"); 18 | return EOF; 19 | } 20 | 21 | /* Check the unget buffer */ 22 | 23 | if ( stream->has_ungotten ) { 24 | stream->has_ungotten = 0; 25 | printf("un get ! \n"); 26 | return stream->unget_buffer; 27 | } 28 | 29 | /* Check the end of the file */ 30 | 31 | if ( feof( stream ) ) { 32 | printf("EOF ! \n"); 33 | return EOF; 34 | } 35 | 36 | /* Fill the buffer if it's empty */ 37 | 38 | if ( stream->buffer_pos >= stream->buffer_data_size ) { 39 | ssize_t length; 40 | 41 | length = read( stream->fd, stream->buffer, stream->buffer_size ); 42 | if ( length == 0 ) { 43 | stream->flags |= __FILE_EOF; 44 | printf("EOF ! \n"); 45 | return EOF; 46 | } else if ( length < 0 ) { 47 | stream->flags |= __FILE_ERROR; 48 | printf("EOF ! \n"); 49 | return EOF; 50 | } 51 | stream->buffer_pos = 0; 52 | stream->buffer_data_size = length; 53 | } 54 | 55 | /* Get one character from the buffer */ 56 | 57 | c = stream->buffer[ stream->buffer_pos ]; 58 | 59 | stream->buffer_pos++; 60 | 61 | return c; 62 | } 63 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fgets.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* fgets( char* s, int size, FILE* stream ) { 7 | char* orig = s; 8 | int l; 9 | 10 | for ( l = size; l > 1; ) { 11 | register int c = fgetc( stream ); 12 | //printf("c: %c \n"); 13 | if ( c == EOF ) { 14 | break; 15 | } 16 | 17 | *s = c; 18 | ++s; 19 | --l; 20 | 21 | if ( c == '\n' ) { 22 | break; 23 | } 24 | } 25 | 26 | if ( ( l == size ) || ( ferror( stream ) ) ) { 27 | return 0; 28 | } 29 | 30 | *s = 0; 31 | 32 | return orig; 33 | } 34 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fileno.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int fileno( FILE* stream ) { 7 | return stream->fd; 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fpurge.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | 7 | 8 | int fpurge( FILE* stream ) { 9 | if ( stream->flags & __FILE_NOBUF ) { 10 | return 0; 11 | } 12 | 13 | stream->has_ungotten = 0; 14 | 15 | if ( stream->flags & __FILE_BUFINPUT ) { 16 | stream->buffer_pos = stream->buffer_data_size; 17 | } else { 18 | stream->buffer_pos = 0; 19 | } 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fputc.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include "stdio_internal.h" 8 | 9 | int fputc( int c, FILE* stream ) { 10 | /* Check if we can write to the stream */ 11 | 12 | if ( ( ( stream->flags & __FILE_CAN_WRITE ) == 0 ) || 13 | ( __set_stream_flags( stream, 0 ) ) ) { 14 | stream->flags |= __FILE_ERROR; 15 | return EOF; 16 | } 17 | 18 | /* Make sure we have free space in the buffer */ 19 | 20 | if ( stream->buffer_pos >= stream->buffer_size - 1 ) { 21 | if ( fflush( stream ) ) { 22 | stream->flags |= __FILE_ERROR; 23 | return EOF; 24 | } 25 | } 26 | 27 | if ( stream->flags & __FILE_NOBUF ) { 28 | if ( write( stream->fd, &c, 1 ) != 1 ) { 29 | stream->flags |= __FILE_ERROR; 30 | return EOF; 31 | } 32 | 33 | return 0; 34 | } 35 | 36 | stream->buffer[ stream->buffer_pos++ ] = c; 37 | 38 | if ( ( ( stream->flags & __FILE_BUFLINEWISE ) && ( c == '\n' ) ) || 39 | ( stream->flags & __FILE_NOBUF ) ) { 40 | if ( fflush( stream ) ) { 41 | stream->flags |= __FILE_ERROR; 42 | return EOF; 43 | } 44 | } 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fputs.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int fputs( const char* s, FILE* stream ) { 8 | while ( *s ) { 9 | fputc( *s++, stream ); 10 | } 11 | 12 | fflush( stream ); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fread.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | size_t fread( void* ptr, size_t size, size_t nmemb, FILE* stream ) { 9 | int res; 10 | unsigned long i, j; 11 | 12 | j = size * nmemb; 13 | i = 0; 14 | 15 | if ( ( stream->flags & __FILE_CAN_READ ) == 0 ) { 16 | stream->flags |= __FILE_ERROR; 17 | return 0; 18 | } 19 | 20 | if ( ( j == 0 ) || 21 | ( ( j / nmemb ) != size ) ) { 22 | return 0; 23 | } 24 | 25 | if ( stream->has_ungotten ) { 26 | stream->has_ungotten = 0; 27 | *( char* )ptr = stream->unget_buffer; 28 | ++i; 29 | 30 | if ( j == 1 ) { 31 | return 1; 32 | } 33 | } 34 | 35 | for ( ; i < j; ++i ) { 36 | res = fgetc( stream ); 37 | 38 | if ( res == EOF ) { 39 | return i / size; 40 | } else { 41 | ( ( unsigned char* )ptr )[ i ] = ( unsigned char )res; 42 | } 43 | } 44 | 45 | return nmemb; 46 | } 47 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/freopen.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int __parse_mode( const char* mode ); 10 | 11 | FILE* freopen( const char* path, const char* mode, FILE* stream ) { 12 | int flags; 13 | 14 | if ( stream == NULL ) { 15 | errno = -EINVAL; 16 | return NULL; 17 | } 18 | 19 | flags = __parse_mode( mode ); 20 | 21 | fflush( stream ); 22 | close( stream->fd ); 23 | 24 | stream->fd = open( path, flags, 0666 ); 25 | 26 | if ( stream->fd != -1 ) { 27 | stream->flags = 0; 28 | 29 | switch ( flags & 3 ) { 30 | case O_RDWR : stream->flags |= ( __FILE_CAN_READ | __FILE_CAN_WRITE ); break; 31 | case O_RDONLY : stream->flags |= __FILE_CAN_READ; break; 32 | case O_WRONLY : stream->flags |= __FILE_CAN_WRITE; break; 33 | } 34 | } 35 | 36 | return stream; 37 | } 38 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fseek.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int fseeko( FILE* stream, off_t offset, int whence ) { 8 | fflush( stream ); 9 | 10 | stream->buffer_pos = 0; 11 | stream->buffer_data_size = 0; 12 | stream->flags &= ~( __FILE_EOF | __FILE_ERROR ); 13 | stream->has_ungotten = 0; 14 | 15 | return ( lseek( stream->fd, offset, whence ) != -1 ? 0 : -1 ); 16 | } 17 | 18 | int fseek( FILE* stream, long offset, int whence ) { 19 | return fseeko( stream, ( off_t )offset, whence ); 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/ftell.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | off_t ftello( FILE* stream ) { 10 | off_t l; 11 | 12 | if ( stream->flags & ( __FILE_EOF | __FILE_ERROR ) ) { 13 | return -1; 14 | } 15 | 16 | l = lseek( stream->fd, 0, SEEK_CUR ); 17 | 18 | if ( l == ( off_t )-1 ) { 19 | return -1; 20 | } 21 | 22 | if ( stream->flags & __FILE_BUFINPUT ) { 23 | return l - ( stream->buffer_data_size - stream->buffer_pos ) - stream->has_ungotten; 24 | } else { 25 | return l + stream->buffer_pos; 26 | } 27 | } 28 | 29 | long ftell( FILE* stream ) { 30 | off_t l; 31 | 32 | l = ftello( stream ); 33 | 34 | if ( l > LONG_MAX ) { 35 | errno = EOVERFLOW; 36 | return -1; 37 | } 38 | 39 | return ( long )l; 40 | } 41 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/fwrite.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | size_t fwrite( const void* ptr, size_t size, size_t nmemb, FILE* stream ) { 10 | ssize_t res; 11 | unsigned long len=size * nmemb; 12 | long i; 13 | 14 | if ( ( stream->flags & __FILE_CAN_WRITE ) == 0 ) { 15 | stream->flags |= __FILE_ERROR; 16 | return 0; 17 | } 18 | 19 | if ( ( nmemb == 0 ) || 20 | ( ( len / nmemb ) != size ) ) { 21 | return 0; 22 | } 23 | 24 | if ( ( len > stream->buffer_size ) || ( stream->flags & __FILE_NOBUF ) ) { 25 | if ( fflush( stream ) ) { 26 | return 0; 27 | } 28 | 29 | res = write( stream->fd, ptr, len ); 30 | } else { 31 | register const unsigned char* c = ptr; 32 | 33 | for ( i = len; i > 0; --i, ++c ) { 34 | if ( fputc( *c, stream ) ) { 35 | res = len - i; 36 | goto abort; 37 | } 38 | } 39 | 40 | res = len; 41 | } 42 | 43 | if ( res < 0 ) { 44 | stream->flags |= __FILE_ERROR; 45 | return 0; 46 | } 47 | 48 | abort: 49 | return size ? res / size : 0; 50 | } 51 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/getc.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | int getc( FILE* stream ) { 6 | return fgetc( stream ); 7 | } 8 | 9 | int gets(char* buf){ 10 | fgets(buf,512,stdin); 11 | return 1; 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/perror.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | void perror( const char* s ) { 9 | if ( s != NULL ) { 10 | fprintf( stderr, "%s", s ); 11 | } 12 | 13 | fprintf( stderr, ": %s\n", strerror( errno ) ); 14 | } 15 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/putc.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int putc( int c, FILE* stream ) { 9 | return fputc( c, stream ); 10 | } 11 | 12 | 13 | 14 | int support_vfprintf(FILE* stream, const char* format, va_list ap); 15 | 16 | int vfprintf(FILE* stream, const char* format, va_list arg) 17 | { 18 | return support_vfprintf( stream, format, arg ); 19 | } 20 | 21 | int printf(const char* format, ...) 22 | { 23 | int rc; 24 | va_list ap; 25 | va_start(ap, format); 26 | rc = vfprintf( stdout, format, ap ); 27 | va_end(ap); 28 | return rc; 29 | } 30 | 31 | 32 | int fprintf(FILE*stream,const char* format, ...) 33 | { 34 | int rc; 35 | va_list ap; 36 | va_start(ap, format); 37 | rc = vfprintf(stream, format, ap ); 38 | va_end(ap); 39 | return rc; 40 | } 41 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/putchar.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int putchar( int c ) { 7 | return fputc( c, stdout ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/puts.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int puts( const char* s ) { 7 | printf("%s\n", s ); 8 | //fflush( stdout ); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/remove.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int remove( const char* path ) { 10 | struct stat st; 11 | 12 | if ( stat( path, &st) != 0 ) { 13 | return -1; 14 | } 15 | 16 | if ( S_ISDIR( st.st_mode ) ) { 17 | return rmdir( path ); 18 | } else { 19 | return unlink( path ); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/rename.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | 8 | int rename( const char* oldpath, const char* newpath ) { 9 | printf( "TODO: rename not yet implemented! (from: %s to: %s)\n", oldpath, newpath ); 10 | 11 | errno = -ENOSYS; 12 | 13 | return -1; 14 | } 15 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/rewind.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | void rewind( FILE* stream ) { 7 | clearerr( stream ); 8 | fseek( stream, 0L, SEEK_SET ); 9 | } 10 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/setvbuf.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | static int set_flags( FILE* stream, int flags ) { 10 | switch ( flags ) { 11 | case _IONBF : 12 | stream->flags = ( stream->flags & ~( __FILE_BUFLINEWISE ) ) | __FILE_NOBUF; 13 | return 0; 14 | 15 | case _IOLBF : 16 | stream->flags = ( stream->flags & ~( __FILE_NOBUF ) ) | __FILE_BUFLINEWISE; 17 | return 0; 18 | 19 | case _IOFBF : 20 | stream->flags = stream->flags & ~( __FILE_NOBUF | __FILE_BUFLINEWISE ); 21 | return 0; 22 | 23 | default : 24 | return -1; 25 | } 26 | } 27 | 28 | int setvbuf( FILE* stream, char* buf, int flags, size_t size ) { 29 | if ( buf != NULL ) { 30 | if ( ( stream->flags & __FILE_DONTFREEBUF ) == 0 ) { 31 | free( stream->buffer ); 32 | } 33 | 34 | stream->buffer = buf; 35 | stream->flags |= __FILE_DONTFREEBUF; 36 | } else { 37 | char *tmp; 38 | 39 | if ( size == 0 ) { 40 | return set_flags( stream, flags ); 41 | } 42 | 43 | tmp = ( char* )malloc( size ); 44 | 45 | if ( tmp == NULL ) { 46 | return -1; 47 | } 48 | 49 | if ( ( stream->flags & __FILE_DONTFREEBUF ) == 0 ) { 50 | free( stream->buffer ); 51 | } 52 | 53 | stream->buffer = tmp; 54 | stream->flags &= ~__FILE_DONTFREEBUF; 55 | } 56 | 57 | stream->buffer_size = size; 58 | stream->buffer_pos = 0; 59 | stream->buffer_data_size = 0; 60 | 61 | return set_flags( stream, flags ); 62 | } 63 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/stdio_internal.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | #include "stdio_internal.h" 7 | 8 | int __set_stream_flags( FILE* stream, int new_flags ) { 9 | if ( ( stream->flags & __FILE_BUFINPUT ) != new_flags) { 10 | int error; 11 | 12 | error = fflush( stream ); 13 | 14 | stream->flags = ( stream->flags & ~__FILE_BUFINPUT ) | new_flags; 15 | 16 | return error; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/stdio_internal.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #ifndef _STDIO_INTERNAL_H_ 5 | #define _STDIO_INTERNAL_H_ 6 | 7 | int __set_stream_flags( FILE* stream, int new_flags ); 8 | 9 | #endif /* _STDIO_INTERNAL_H_ */ 10 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/streams.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | static char __stdin_buffer[ _IO_BUFSIZE ]; 7 | static char __stdout_buffer[ _IO_BUFSIZE ]; 8 | static char __stderr_buffer[ _IO_BUFSIZE ]; 9 | 10 | static FILE _stdin = { 11 | .fd = 0, 12 | .flags = __FILE_CAN_READ | __FILE_DONTFREEBUF | __FILE_BUFLINEWISE | __FILE_BUFINPUT, 13 | .buffer = __stdin_buffer, 14 | .buffer_pos = 0, 15 | .buffer_size = _IO_BUFSIZE, 16 | .buffer_data_size = 0, 17 | .has_ungotten = 0, 18 | .unget_buffer = -1 19 | }; 20 | 21 | static FILE _stdout = { 22 | .fd = 1, 23 | .flags = __FILE_CAN_WRITE | __FILE_BUFLINEWISE | __FILE_DONTFREEBUF, 24 | .buffer = __stdout_buffer, 25 | .buffer_pos = 0, 26 | .buffer_size = _IO_BUFSIZE, 27 | .buffer_data_size = 0, 28 | .has_ungotten = 0, 29 | .unget_buffer = -1 30 | }; 31 | 32 | static FILE _stderr = { 33 | .fd = 2, 34 | .flags = __FILE_CAN_WRITE | __FILE_DONTFREEBUF | __FILE_NOBUF, 35 | .buffer = __stderr_buffer, 36 | .buffer_pos = 0, 37 | .buffer_size = _IO_BUFSIZE, 38 | .buffer_data_size = 0, 39 | .has_ungotten = 0, 40 | .unget_buffer = -1 41 | }; 42 | 43 | FILE* stdin = &_stdin; 44 | FILE* stdout = &_stdout; 45 | FILE* stderr = &_stderr; 46 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdio/ungetc.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | int ungetc( int c, FILE* stream ) { 6 | if ( ( stream->has_ungotten ) || 7 | ( c < 0 ) || 8 | ( c > 255 ) ) { 9 | return EOF; 10 | } 11 | 12 | stream->has_ungotten = 1; 13 | stream->unget_buffer = ( unsigned char )c; 14 | stream->flags &= ~( __FILE_EOF | __FILE_ERROR ); 15 | 16 | return c; 17 | } 18 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/abort.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | void abort( void ) { 9 | fprintf( stderr, "Application aborted!\n" ); 10 | exit( -1 ); 11 | } 12 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/abs.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int abs( int j ) { 7 | if ( j < 0 ) { 8 | return -j; 9 | } else { 10 | return j; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/atof.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | double atof( const char* s ) { 7 | return strtod( s, ( char** )NULL ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/atoi.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int atoi( const char* s ) { 8 | int v = 0; 9 | int sign = 0; 10 | 11 | while ( ( *s == ' ' ) || ( ( unsigned int )( *s - 9 ) < 5u ) ) { 12 | ++s; 13 | } 14 | 15 | switch ( *s ) { 16 | case '-' : sign = -1; 17 | case '+' : ++s; 18 | } 19 | 20 | while ( ( unsigned int )( *s - '0' ) < 10u ) { 21 | v = v * 10 + *s - '0'; 22 | ++s; 23 | } 24 | 25 | return sign ? -v : v; 26 | } 27 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/atol.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | long atol( const char* s ) { 8 | long v = 0; 9 | int sign = 0; 10 | 11 | while ( ( *s == ' ' ) || ( ( unsigned int )( *s - 9 ) < 5u ) ) { 12 | ++s; 13 | } 14 | 15 | switch ( *s ) { 16 | case '-' : sign = -1; 17 | case '+' : ++s; 18 | } 19 | 20 | while ( ( unsigned int )( *s - '0' ) < 10u ) { 21 | v = v * 10 + *s - '0'; 22 | ++s; 23 | } 24 | 25 | return sign ? -v : v; 26 | } 27 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/atoll.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | long long int atoll( const char* s ) { 8 | long long int v = 0; 9 | int sign = 1; 10 | 11 | while ( *s == ' ' || ( unsigned int )( *s - 9 ) < 5u ) { 12 | ++s; 13 | } 14 | 15 | switch ( *s ) { 16 | case '-': sign = -1; 17 | case '+': ++s; 18 | } 19 | 20 | while ( ( unsigned int )( *s - '0' ) < 10u ) { 21 | v= v * 10 + *s - '0'; 22 | ++s; 23 | } 24 | 25 | return sign == -1 ? -v : v; 26 | } 27 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/bsearch.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | void* bsearch( const void* key, const void* base, size_t nmemb, size_t size, int ( *compare )( const void*, const void* ) ) { 6 | size_t m; 7 | 8 | while ( nmemb ) { 9 | int tmp; 10 | void* p; 11 | 12 | m = nmemb / 2; 13 | p = ( void* )( ( ( const char* )base ) + ( m * size ) ); 14 | 15 | if ( ( tmp = ( *compare )( key, p ) ) < 0 ) { 16 | nmemb = m; 17 | } else if ( tmp > 0 ) { 18 | base = p + size; 19 | nmemb -= m + 1; 20 | } else { 21 | return p; 22 | } 23 | } 24 | 25 | return NULL; 26 | } 27 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/getenv.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | extern char** environ; 8 | 9 | char* getenv( const char* name ) { 10 | return NULL; 11 | 12 | int i; 13 | size_t length; 14 | size_t name_length; 15 | 16 | name_length = strlen( name ); 17 | 18 | for ( i = 0; environ[ i ] != NULL; i++ ) { 19 | length = strlen( environ[ i ] ); 20 | 21 | if ( length < ( name_length + 1 ) ) { 22 | continue; 23 | } 24 | 25 | if ( ( strncmp( environ[ i ], name, name_length ) == 0 ) && 26 | ( environ[ i ][ name_length ] == '=' ) ) { 27 | return &environ[ i ][ name_length + 1 ]; 28 | } 29 | } 30 | 31 | return NULL; 32 | } 33 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/labs.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | long labs( long j ) { 7 | if ( j < 0 ) { 8 | return -j; 9 | } else { 10 | return j; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/llabs.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | long long llabs( long long j ) { 7 | if ( j < 0 ) { 8 | return -j; 9 | } else { 10 | return j; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/mkstemp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #ifndef O_NOFOLLOW 8 | #define O_NOFOLLOW 0 9 | #endif 10 | 11 | int mkstemp( char* template ) { 12 | int i; 13 | char* tmp; 14 | int result; 15 | unsigned int rnd; 16 | 17 | tmp = template + strlen( template ) - 6; 18 | 19 | if ( tmp < template ) { 20 | errno = EINVAL; 21 | return -1; 22 | } 23 | 24 | for ( i = 0; i < 6; i++ ) { 25 | if ( tmp[ i ] != 'X' ) { 26 | errno = EINVAL; 27 | return -1; 28 | } 29 | } 30 | 31 | for ( ;; ) { 32 | for ( i = 0; i < 6; i++ ) { 33 | int hexdigit; 34 | 35 | rnd = random(); 36 | hexdigit = ( rnd >> ( i * 5 ) ) & 0x1F; 37 | tmp[ i ] = hexdigit > 9 ? ( hexdigit + 'a' - 10 ) : ( hexdigit + '0' ); 38 | } 39 | 40 | result = open( template, O_CREAT | O_RDWR | O_EXCL | O_NOFOLLOW, 0600 ); 41 | 42 | if ( ( result >= 0 ) || 43 | ( errno != EEXIST ) ) { 44 | break; 45 | } 46 | } 47 | 48 | return result; 49 | } 50 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/mktemp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | char* mktemp( char* template ) { 7 | int fd; 8 | 9 | fd = mkstemp( template ); 10 | 11 | if ( fd < 0 ) { 12 | return NULL; 13 | } 14 | 15 | close( fd ); 16 | 17 | unlink( template ); 18 | 19 | return template; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/rand.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | unsigned int _seed2 = 0xDEADBEEF; 7 | 8 | int rand( void ) { 9 | unsigned int next = _seed2; 10 | unsigned int result; 11 | 12 | next *= 1103515245; 13 | next += 12345; 14 | result = ( unsigned int ) ( next / 65536 ) % 2048; 15 | 16 | next *= 1103515245; 17 | next += 12345; 18 | result <<= 10; 19 | result ^= ( unsigned int ) ( next / 65536 ) % 1024; 20 | 21 | next *= 1103515245; 22 | next += 12345; 23 | result <<= 10; 24 | result ^= ( unsigned int ) ( next / 65536 ) % 1024; 25 | 26 | _seed2 = next; 27 | 28 | return result; 29 | } 30 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/random.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | unsigned int _seed = 0xDEADBEEF; 5 | 6 | long int random( void ) { 7 | unsigned int next = _seed; 8 | unsigned long int result; 9 | 10 | next *= 1103515245; 11 | next += 12345; 12 | result = ( unsigned int ) ( next / 65536 ) % 2048; 13 | 14 | next *= 1103515245; 15 | next += 12345; 16 | result <<= 10; 17 | result ^= ( unsigned int ) ( next / 65536 ) % 1024; 18 | 19 | next *= 1103515245; 20 | next += 12345; 21 | result <<= 10; 22 | result ^= ( unsigned int ) ( next / 65536 ) % 1024; 23 | 24 | _seed = next; 25 | 26 | return result; 27 | } 28 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/srand.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | extern unsigned int _seed2; 7 | 8 | void srand(unsigned int seed){ 9 | _seed2 = seed; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/srandom.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | extern unsigned int _seed; 7 | 8 | void srandom(unsigned int seed){ 9 | _seed = seed; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/strtol.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #if __WORDSIZE == 64 10 | #define ABS_LONG_MIN 9223372036854775808UL 11 | #else 12 | #define ABS_LONG_MIN 2147483648UL 13 | #endif 14 | 15 | long int strtol( const char* nptr, char** endptr, int base ) { 16 | int neg = 0; 17 | unsigned long int v; 18 | const char* orig = nptr; 19 | 20 | while ( isspace( *nptr ) ) { 21 | nptr++; 22 | } 23 | 24 | if ( ( *nptr == '-' ) && ( isalnum( nptr[ 1 ] ) ) ) { 25 | neg = -1; 26 | ++nptr; 27 | } 28 | 29 | v = strtoul( nptr, endptr, base ); 30 | 31 | if ( ( endptr ) && ( *endptr == nptr) ) { 32 | *endptr = ( char* )orig; 33 | } 34 | 35 | if ( v >= ABS_LONG_MIN ) { 36 | if ( ( v == ABS_LONG_MIN ) && ( neg ) ) { 37 | return v; 38 | } 39 | 40 | return ( neg ? LONG_MIN : LONG_MAX ); 41 | } 42 | 43 | return ( neg ? -v : v ); 44 | } 45 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/stdlib/strtoll.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | long long int strtoll( const char* nptr, char** endptr, int base ) { 11 | int neg = 0; 12 | unsigned long long int v; 13 | const char* orig = nptr; 14 | 15 | while ( isspace( *nptr ) ) { 16 | nptr++; 17 | } 18 | 19 | if ( *nptr == '-' && isalnum( nptr[ 1 ] ) ) { 20 | neg = -1; 21 | nptr++; 22 | } 23 | 24 | v = strtoull( nptr, endptr, base ); 25 | 26 | if ( endptr && *endptr == nptr ) { 27 | *endptr = ( char* )orig; 28 | } 29 | 30 | if ( v > LLONG_MAX ) { 31 | if ( v == 0x8000000000000000ull && neg ) { 32 | errno = 0; 33 | return v; 34 | } 35 | 36 | errno=ERANGE; 37 | 38 | return ( neg ? LLONG_MIN : LLONG_MAX ); 39 | } 40 | 41 | return ( neg ? -v : v ); 42 | } 43 | 44 | intmax_t strtoimax( const char* nptr, char** endptr, int base ) __attribute__(( alias( "strtoll" ) )); 45 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/memchr.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | void* memchr( const void* s, int c, size_t n ) { 7 | const unsigned char* src = ( const unsigned char* )s; 8 | unsigned char ch = c; 9 | 10 | for ( ; n != 0; n-- ) { 11 | if ( *src == ch ) { 12 | return ( void* )src; 13 | } 14 | 15 | src++; 16 | } 17 | 18 | return NULL; 19 | } 20 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/memcmp.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int memcmp( const void* p1, const void* p2, size_t c ) { 7 | const unsigned char* su1, *su2; 8 | signed char res = 0; 9 | 10 | for ( su1 = p1, su2 = p2; 0 < c; ++su1, ++su2, c-- ) { 11 | if ( ( res = *su1 - *su2 ) != 0 ) { 12 | break; 13 | } 14 | } 15 | 16 | return res; 17 | } 18 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/memcpy.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | void* memcpy( void* d, const void* s, size_t n ) { 7 | char* dest; 8 | char* src; 9 | 10 | dest = ( char* )d; 11 | src = ( char* )s; 12 | 13 | while ( n-- ) { 14 | *dest++ = *src++; 15 | } 16 | 17 | return d; 18 | } 19 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/memmove.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | void* memmove( void* dest, const void* src, size_t n ) { 7 | char* _dest; 8 | char* _src; 9 | 10 | if ( dest < src ) { 11 | _dest = ( char* )dest; 12 | _src = ( char* )src; 13 | 14 | while ( n-- ) { 15 | *_dest++ = *_src++; 16 | } 17 | } else { 18 | _dest = ( char* )dest + n; 19 | _src = ( char* )src + n; 20 | 21 | while ( n-- ) { 22 | *--_dest = *--_src; 23 | } 24 | } 25 | 26 | return dest; 27 | } 28 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/memset.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | void* memset( void* s, int c, size_t n ) { 7 | char* _src; 8 | 9 | _src = ( char* )s; 10 | 11 | while ( n-- ) { 12 | *_src++ = c; 13 | } 14 | 15 | return s; 16 | } 17 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strcasecmp.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int strcasecmp( const char* s1, const char* s2 ) { 8 | int result; 9 | 10 | while ( 1 ) { 11 | result = tolower( *s1 ) - tolower( *s2++ ); 12 | 13 | if ( ( result != 0 ) || ( *s1++ == 0 ) ) { 14 | break; 15 | } 16 | } 17 | 18 | return result; 19 | } 20 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strcat.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* strcat( char* d, const char* s ) { 7 | char* tmp = d; 8 | 9 | while ( *d ) d++; 10 | while ( ( *d++ = *s++ ) != 0 ) ; 11 | 12 | return tmp; 13 | } 14 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strchr.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* strchr( const char* s, int c ) { 7 | for ( ; *s != ( char )c; s++ ) { 8 | if ( *s == 0 ) { 9 | return NULL; 10 | } 11 | } 12 | 13 | return ( char* )s; 14 | } 15 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strcmp.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int strcmp( const char* s1, const char* s2 ) { 7 | int result; 8 | 9 | while ( 1 ) { 10 | result = *s1 - *s2++; 11 | 12 | if ( ( result != 0 ) || ( *s1++ == 0 ) ) { 13 | break; 14 | } 15 | } 16 | 17 | return result; 18 | } 19 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strcpy.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | 8 | char* strcpy( char* d, const char* s ) { 9 | char* tmp = d; 10 | 11 | while ( ( *d++ = *s++ ) != 0 ) ; 12 | 13 | return tmp; 14 | } 15 | 16 | int support_vsprintf(char* buffer, const char* format, va_list ap); 17 | 18 | int vsprintf(char* s, const char* format, va_list arg) 19 | { 20 | return support_vsprintf( s, format, arg ); 21 | } 22 | 23 | 24 | int sprintf(char *buffer, const char* format, ...) 25 | { 26 | int rc; 27 | va_list ap; 28 | va_start(ap, format); 29 | rc = vsprintf( buffer, format, ap ); 30 | va_end(ap); 31 | return 0; 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strcspn.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | size_t strcspn( const char* s, const char* reject ) { 8 | size_t l = 0; 9 | int a = 1; 10 | int i; 11 | int al = strlen( reject ); 12 | 13 | while ( ( a ) && ( *s ) ) { 14 | for ( i = 0; ( a ) && ( i < al ); i++ ) { 15 | if ( *s == reject[ i ] ) { 16 | a = 0; 17 | } 18 | } 19 | 20 | if ( a ) { 21 | l++; 22 | } 23 | 24 | s++; 25 | } 26 | 27 | return l; 28 | } 29 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strdup.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | char* strdup( const char* s ) { 8 | char* s2; 9 | size_t length; 10 | 11 | length = strlen( s ); 12 | s2 = ( char* )malloc( length + 1 ); 13 | 14 | if ( s2 == NULL ) { 15 | return NULL; 16 | } 17 | 18 | memcpy( s2, s, length + 1 ); 19 | 20 | return s2; 21 | } 22 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strlen.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | size_t strlen( const char* s ) { 7 | size_t r = 0; 8 | 9 | for( ; *s++ != 0; r++ ) { } 10 | 11 | return r; 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strncasecmp.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | int strncasecmp( const char* s1, const char* s2, size_t c ) { 8 | int result = 0; 9 | 10 | while ( c ) { 11 | result = toupper( *s1 ) - toupper( *s2++ ); 12 | 13 | if ( ( result != 0 ) || ( *s1++ == 0 ) ) { 14 | break; 15 | } 16 | 17 | c--; 18 | } 19 | 20 | return result; 21 | } 22 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strncat.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* strncat( char* d, const char* s, size_t c ) { 7 | char* tmp = d; 8 | 9 | if ( c > 0 ) { 10 | while ( *d ) d++; 11 | while ( ( *d++ = *s++ ) ) { 12 | if ( --c == 0 ) { 13 | *d = 0; 14 | break; 15 | } 16 | } 17 | } 18 | 19 | return tmp; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strncmp.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int strncmp( const char* s1, const char* s2, size_t c ) { 7 | int result = 0; 8 | 9 | while ( c ) { 10 | result = *s1 - *s2++; 11 | 12 | if ( ( result != 0 ) || ( *s1++ == 0 ) ) { 13 | break; 14 | } 15 | 16 | c--; 17 | } 18 | 19 | return result; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strncpy.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* strncpy( char* d, const char* s, size_t c ) { 7 | size_t i; 8 | char* tmp = d; 9 | 10 | for ( i = 0; i < c; i++ ) { 11 | *d++ = *s; 12 | 13 | if ( *s++ == 0 ) { 14 | break; 15 | } 16 | } 17 | 18 | for ( ; i < c; i++ ) { 19 | *d++ = 0; 20 | } 21 | 22 | return tmp; 23 | } 24 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strndup.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | char* strndup( const char* s, size_t n ) { 9 | char* s2; 10 | size_t len; 11 | 12 | len = strlen( s ); 13 | len = MIN( len, n ); 14 | 15 | s2 = ( char* )malloc( len + 1 ); 16 | 17 | if ( s2 == NULL ) { 18 | return NULL; 19 | } 20 | 21 | memcpy( s2, s, len ); 22 | s2[ len ] = '\0'; 23 | 24 | return s2; 25 | } 26 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strnlen.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | size_t strnlen( const char* s, size_t count ) { 7 | const char* sc; 8 | 9 | for ( sc = s; count-- && ( *sc != 0 ); ++sc ) { } 10 | 11 | return ( sc - s ); 12 | } 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strpbrk.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | char* strpbrk( const char* s, const char* accept ) { 6 | register int i, l = strlen( accept ); 7 | 8 | for ( ; *s != 0; s++ ) 9 | for ( i = 0; i < l; i++ ) 10 | if (*s == accept[i]) 11 | return (char*)s; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strrchr.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* strrchr( const char* s, int c ) { 7 | const char* end = s + strlen( s ); 8 | 9 | do { 10 | if ( *end == ( char )c ) { 11 | return ( char * )end; 12 | } 13 | } while ( --end >= s ); 14 | 15 | return NULL; 16 | } 17 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strsignal.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | static char* signal_names[] = { 7 | "Hangup", 8 | "Interrupt", 9 | "Quit", 10 | "Illegal instruction", 11 | "Trace/breakpoint trap", 12 | "Aborted", 13 | "Aborted", 14 | "Bus error", 15 | "Floating point exception", 16 | "Killed", 17 | "User defined signal 1", 18 | "Segmentation fault", 19 | "User defined signal 2", 20 | "Broken pipe", 21 | "Alarm clock", 22 | "Terminated", 23 | "Stack fault", 24 | "Child exited", 25 | "Continued", 26 | "Stopped (signal)", 27 | "Stopped", 28 | "Stopped (tty input)", 29 | "Stopped (tty output)", 30 | "Urgent I/O condition", 31 | "CPU time limit exceeded", 32 | "File size limit exceeded", 33 | "Virtual timer expired", 34 | "Profiling timer expired", 35 | "Window changed", 36 | "I/O possible", 37 | "Power failure", 38 | "Bad system call" 39 | }; 40 | 41 | char* strsignal( int signum ) { 42 | if ( signum <= 0 ) { 43 | return NULL; 44 | } 45 | 46 | signum--; 47 | 48 | if ( signum >= ( sizeof( signal_names ) / sizeof( signal_names[ 0 ] ) ) ) { 49 | return NULL; 50 | } 51 | 52 | return signal_names[ signum ]; 53 | } 54 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strspn.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | size_t strspn( const char* s, const char* accept ) { 7 | size_t l = 0; 8 | int a = 1; 9 | int i; 10 | int al = strlen( accept ); 11 | 12 | while( ( a ) && ( *s ) ) { 13 | for ( a = i = 0; ( !a ) && ( i < al ); i++ ) { 14 | if ( *s == accept[ i ] ) { 15 | a = 1; 16 | } 17 | } 18 | 19 | if ( a ) { 20 | l++; 21 | } 22 | 23 | s++; 24 | } 25 | 26 | return l; 27 | } 28 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strstr.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* strstr( const char* s1, const char* s2 ) { 7 | int l1, l2; 8 | 9 | l2 = strlen( s2 ); 10 | 11 | if ( !l2 ) { 12 | return ( char * )s1; 13 | } 14 | 15 | l1 = strlen( s1 ); 16 | 17 | while ( l1 >= l2 ) { 18 | l1--; 19 | 20 | if ( !memcmp( s1, s2, l2 ) ) { 21 | return ( char * )s1; 22 | } 23 | 24 | s1++; 25 | } 26 | 27 | return NULL; 28 | } 29 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strtok.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | static char* strtok_pos; 7 | 8 | char* strtok( char* s, const char* delim ) { 9 | return strtok_r( s, delim, &strtok_pos ); 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/string/strtok_r.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* strtok_r( char* s, const char* delim, char** ptrptr ) { 7 | char* tmp = NULL; 8 | 9 | if ( s == NULL ) { 10 | s = *ptrptr; 11 | } 12 | 13 | s += strspn( s, delim ); 14 | 15 | if ( *s ) { 16 | tmp = s; 17 | s += strcspn( s, delim ); 18 | 19 | if ( *s ) { 20 | *s++ = 0; 21 | } 22 | } 23 | 24 | *ptrptr = s; 25 | 26 | return tmp; 27 | } 28 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/chmod.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int chmod( const char* path, mode_t mode ) { 7 | /* TODO */ 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/connect.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int connect( int fd, const struct sockaddr* address, socklen_t addrlen ) { 10 | int error; 11 | 12 | error = syscall3( SYS_connect, fd, ( int )address, addrlen ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/fstat.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int fstat( int fd, struct stat* stat ) { 10 | int error; 11 | 12 | error = syscall2( SYS_fstat, fd, ( int )stat ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/ioctl.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | int ioctl( int fd, int request, ... ) { 9 | int error; 10 | 11 | error = syscall3( SYS_ioctl, fd, request, *( ( ( ( int* )&request ) ) + 1 ) ); 12 | 13 | if ( error < 0 ) { 14 | errno = -error; 15 | return -1; 16 | } 17 | 18 | return error; 19 | } 20 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/lstat.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int lstat( const char* path, struct stat* stat ) { 10 | int error; 11 | 12 | error = syscall2( SYS_lstat, ( int )path, ( int )stat ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/mkdir.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int mkdir( const char* pathname, mode_t mode ) { 10 | int error; 11 | 12 | error = syscall2( SYS_mkdir, ( int )pathname, ( int )mode ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/mount.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int mount( 10 | const char* source, 11 | const char* target, 12 | const char* filesystemtype, 13 | unsigned long mountflags, 14 | const void* data 15 | ) { 16 | int error; 17 | 18 | error = syscall4( 19 | SYS_mount, 20 | ( int )source, 21 | ( int )target, 22 | ( int )filesystemtype, 23 | ( int )mountflags 24 | ); 25 | 26 | if ( error < 0 ) { 27 | errno = -error; 28 | return -1; 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/select.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int select( int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout ) { 10 | int error; 11 | 12 | error = syscall5( 13 | SYS_select, 14 | fds, 15 | ( int )readfds, 16 | ( int )writefds, 17 | ( int )exceptfds, 18 | ( int )timeout 19 | ); 20 | 21 | if ( error < 0 ) { 22 | errno = -error; 23 | return -1; 24 | } 25 | 26 | return error; 27 | } 28 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/socket.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int socket( int domain, int type, int protocol ) { 10 | int error; 11 | 12 | error = syscall3( SYS_socket, domain, type, protocol ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/stat.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | int stat( const char* path, struct stat* stat ) { 9 | int error; 10 | 11 | error = syscall2( SYS_stat, ( int )path, ( int )stat ); 12 | 13 | if ( error < 0 ) { 14 | errno = -error; 15 | return -1; 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/stime.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int stime( time_t *t ) { 10 | int error; 11 | 12 | error = syscall1( SYS_stime, ( int )t ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/umask.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | mode_t umask( mode_t mask ) { 7 | /* TODO */ 8 | return 0666; 9 | } 10 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/umount.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int umount( 10 | const char* dir 11 | ) { 12 | int error; 13 | 14 | error = syscall1( 15 | SYS_unmount, 16 | ( int )dir 17 | ); 18 | 19 | if ( error < 0 ) { 20 | errno = -error; 21 | return -1; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/utime.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int utime( const char* filename, const struct utimbuf* times ) { 10 | int error; 11 | struct utimbuf current; 12 | 13 | if ( times == NULL ) { 14 | current.actime = time( NULL ); 15 | current.modtime = current.actime; 16 | 17 | times = ( const struct utimbuf* )¤t; 18 | } 19 | 20 | error = syscall2( SYS_utime, ( int )filename, ( int )times ); 21 | 22 | if ( error < 0 ) { 23 | errno = -error; 24 | return -1; 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/utimes.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int utimes( const char* filename, const timeval_t times[2] ) { 10 | struct utimbuf tmp; 11 | 12 | /* NOTE: no microsecond precision */ 13 | tmp.actime = times[ 0 ].tv_sec; 14 | tmp.modtime = times[ 1 ].tv_sec; 15 | 16 | /* This is a wrapper around utime() */ 17 | return utime( filename, &tmp ); 18 | } 19 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/wait.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | pid_t wait( int* status ) { 7 | return wait4( -1, status, 0, NULL ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/wait3.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | pid_t wait3( int* status, int options, struct rusage* rusage ) { 7 | return wait4( -1, status, options, rusage ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/wait4.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | pid_t wait4( pid_t pid, int* status, int options, struct rusage* rusage ) { 10 | int error; 11 | 12 | error = syscall4( 13 | SYS_wait4, 14 | pid, 15 | ( int )status, 16 | options, 17 | ( int )rusage 18 | ); 19 | 20 | if ( error < 0 ) { 21 | errno = -error; 22 | return -1; 23 | } 24 | 25 | return error; 26 | } 27 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/sys/waitpid.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | #include 6 | 7 | #include 8 | 9 | pid_t waitpid( pid_t pid, int* status, int options ) { 10 | return wait4( pid, status, options, NULL ); 11 | } 12 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/termios/tcflow.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int tcflow( int fd, int action ) { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/termios/tcflush.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | int tcflush( int fd, int queue_selector ) { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/termios/tcgetattr.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int tcgetattr( int fd, struct termios* tio ) { 10 | int error; 11 | 12 | if ( tio == NULL ) { 13 | errno = EINVAL; 14 | return -1; 15 | } 16 | 17 | error = ioctl( fd, TCGETA, tio ); 18 | 19 | if ( error < 0 ) { 20 | errno = -error; 21 | return -1; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/termios/tcgetpgrp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | pid_t tcgetpgrp( int fd ) { 4 | /* NOTE: This is a temp. hack to get bash working :) */ 5 | 6 | return getpid(); 7 | } 8 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/termios/tcsetattr.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int tcsetattr( int fd, int optional_actions, const struct termios* tio ) { 10 | int error; 11 | 12 | if ( tio == NULL ) { 13 | errno = EINVAL; 14 | return -1; 15 | } 16 | 17 | switch ( optional_actions ) { 18 | case TCSANOW : 19 | error = ioctl( fd, TCSETA, tio ); 20 | break; 21 | 22 | case TCSADRAIN : 23 | error = ioctl( fd, TCSETAW, tio ); 24 | break; 25 | 26 | case TCSAFLUSH : 27 | error = ioctl( fd, TCSETAF, tio ); 28 | break; 29 | 30 | default : 31 | error = -EINVAL; 32 | break; 33 | } 34 | 35 | if ( error < 0 ) { 36 | errno = -error; 37 | return -1; 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/termios/tcsetpgrp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int tcsetpgrp( int fd, pid_t pgrp ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/asctime.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | static char asctime_buffer[ 32 ]; 7 | 8 | char* asctime( const tm_t* tm ) { 9 | return asctime_r( tm, asctime_buffer ); 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/asctime_r.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* asctime_r( const tm_t* tm, char* buf ) { 7 | strftime( buf, 26, "%a %b %d %H:%M:%S %Y\n", tm ); 8 | 9 | return buf; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/ctime.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* ctime( const time_t* timep ) { 7 | return asctime( localtime( timep ) ); 8 | } 9 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/ctime_r.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | char* ctime_r( const time_t* timep, char *buf) { 7 | buf = asctime( localtime( timep ) ); 8 | 9 | return buf; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/gettimeofday.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | #include 7 | 8 | int gettimeofday( struct timeval* tv, struct timezone* tz ) { 9 | int ret; 10 | uint64_t time; 11 | 12 | ret = syscall1( SYS_get_system_time, ( int )&time ); 13 | 14 | if ( ret >= 0 ) { 15 | if ( tv != NULL ) { 16 | tv->tv_sec = time / 1000000; 17 | tv->tv_usec = time % 1000000; 18 | } 19 | } 20 | 21 | return ret; 22 | } 23 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/gmtime.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | static tm_t ret; 7 | 8 | tm_t* gmtime( const time_t* timep ) { 9 | return gmtime_r( timep, &ret ); 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/gmtime_r.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | #include "time_int.h" 7 | 8 | tm_t* gmtime_r( const time_t* timep, tm_t* result ) { 9 | if ( _gmtime( *timep, result ) < 0 ) { 10 | return NULL; 11 | } 12 | 13 | return result; 14 | } 15 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/localtime.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | static tm_t ret; 6 | 7 | tm_t* localtime( const time_t* timep ) { 8 | gmtime_r( timep, &ret ); 9 | /* TODO: timezones, tzset(3), tzname, etc */ 10 | return &ret; 11 | } 12 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/localtime_r.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | tm_t* localtime_r( const time_t* timep, tm_t* result ) { 7 | gmtime_r( timep, result ); 8 | return result; 9 | } 10 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/mktime.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | #include "time_int.h" 7 | 8 | time_t mktime( tm_t* tm ) { 9 | if ( tm->tm_year > 2100 ) { 10 | return -1; 11 | } 12 | 13 | return daysdiff( tm->tm_year, tm->tm_mon, tm->tm_mday ) * SECONDS_PER_DAY + 14 | tm->tm_hour * SECONDS_PER_HOUR + tm->tm_min * SECONDS_PER_MINUTE + tm->tm_sec; 15 | } 16 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/nanosleep.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | int nanosleep( const struct timespec* req, struct timespec* rem ) { 11 | int error; 12 | uint64_t microsecs; 13 | uint64_t remaining; 14 | 15 | microsecs = ( uint64_t )req->tv_sec * 1000000 + ( uint64_t )req->tv_nsec / 1000; 16 | 17 | if ( microsecs == 0 ) { 18 | microsecs = 1; 19 | } 20 | 21 | error = syscall2( SYS_sleep_thread, ( int )µsecs, ( int )&remaining ); 22 | 23 | if ( error < 0 ) { 24 | errno = -error; 25 | 26 | if ( rem != NULL ) { 27 | rem->tv_sec = remaining / 1000000; 28 | rem->tv_nsec = ( remaining % 1000000 ) * 1000; 29 | } 30 | 31 | return -1; 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/time.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | 6 | #include 7 | 8 | time_t time( time_t* t ) { 9 | int ret; 10 | uint64_t time; 11 | 12 | ret = syscall1( SYS_get_system_time, ( int )&time ); 13 | 14 | if ( ret < 0 ) { 15 | time = 0; 16 | } else { 17 | time /= 1000000; 18 | } 19 | 20 | if ( t != NULL ) { 21 | *t = time; 22 | } 23 | 24 | return time; 25 | } 26 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/time_int.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef _TIME_INT_H_ 4 | #define _TIME_INT_H_ 5 | #include /* Time structures */ 6 | 7 | #define EPOCH 1970 /* Counting only time elapsed since 1 Jan 1970 */ 8 | 9 | #define SECONDS_PER_DAY 86400 10 | #define SECONDS_PER_HOUR 3600 11 | #define SECONDS_PER_MINUTE 60 12 | #define MINUTES_PER_HOUR 60 13 | #define HOURS_PER_DAY 24 14 | 15 | /* The number of days that come before each month */ 16 | extern const unsigned short int monthdays[ 13 ]; /* Common year */ 17 | extern const unsigned short int monthdays2[ 13 ]; /* Leap year */ 18 | /* The number of days passed in N years after 1970 or any leap year */ 19 | extern const unsigned short int sumofdays[ 60 ]; 20 | 21 | /* Converts a UNIX timestamp to a broken-down time */ 22 | int _gmtime(time_t time, tm_t* ret); 23 | 24 | /* Returns the day of the week, 0=Sunday */ 25 | int dayofweek(int year, int month, int day); 26 | 27 | /* Returns the number of days since the epoch */ 28 | int daysdiff(int year, int month, int day); 29 | 30 | #endif // _TIME_INT_H_ 31 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/time/tzset.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | void tzset( void ) { 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/access.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int access( const char* pathname, int mode ) { 6 | int error; 7 | 8 | error = syscall2( 9 | SYS_access, 10 | ( int )pathname, 11 | mode 12 | ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/alarm.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | unsigned int alarm( unsigned int seconds ) { 5 | /* TODO */ 6 | 7 | printf( "TODO: alarm() not yet implemented!\n" ); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/chdir.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int chdir( const char* path ) { 10 | int error; 11 | 12 | error = syscall1( SYS_chdir, ( int )path ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/chown.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int chown( const char* path, uid_t owner, gid_t group ) { 5 | /* TODO */ 6 | 7 | printf( "TODO: chown() not yet implemented!\n" ); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/close.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int close( int fd ) { 10 | int error; 11 | 12 | error = syscall1( SYS_close, fd ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/dup.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int dup( int old_fd ) { 10 | int error; 11 | 12 | error = syscall1( SYS_dup, old_fd ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/dup2.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int dup2( int old_fd, int new_fd ) { 10 | int error; 11 | 12 | error = syscall2( SYS_dup2, old_fd, new_fd ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/execlp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int execlp( const char* file, const char* arg, ... ) { 7 | int i; 8 | int n; 9 | va_list ap; 10 | va_list bak; 11 | char* tmp; 12 | char** argv; 13 | 14 | va_start( ap, arg ); 15 | __va_copy( bak, ap ); 16 | 17 | n = 2; 18 | 19 | while ( ( tmp = va_arg( ap, char* ) ) != NULL ) { 20 | ++n; 21 | } 22 | 23 | va_end( ap ); 24 | 25 | argv = ( char** )alloca( n * sizeof( char* ) ); 26 | 27 | if ( argv != NULL ) { 28 | argv[ 0 ]= ( char* )arg; 29 | 30 | for ( i = 0 ; i < n; ++i ) { 31 | argv[ i + 1 ] = va_arg( bak, char* ); 32 | } 33 | 34 | va_end( bak ); 35 | 36 | return execvp( file, argv ); 37 | } 38 | 39 | va_end( bak ); 40 | 41 | errno = ENOMEM; 42 | 43 | return -1; 44 | } 45 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/execv.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern char** environ; 4 | 5 | int execv( const char* file, char* const argv[] ) { 6 | return execve( file, argv, environ ); 7 | } 8 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/execve.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int execve( const char* filename, char* const argv[], char* const envp[] ) { 10 | int error; 11 | 12 | error = syscall3( SYS_execve, ( int )filename, ( int )argv, ( int )envp ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/execvp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | extern char** environ; 8 | 9 | int execvp( const char* filename, char* const argv[] ) { 10 | char* path; 11 | char* separator; 12 | char tmp_path[ 128 ]; 13 | char tmp_exec[ 128 ]; 14 | 15 | execve( filename, argv, environ ); 16 | 17 | path = getenv( "PATH" ); 18 | 19 | if ( path == NULL ) { 20 | return -1; 21 | } 22 | 23 | do { 24 | separator = strchr( path, ':' ); 25 | 26 | if ( separator == NULL ) { 27 | memcpy( tmp_path, path, strlen( path ) + 1 ); 28 | } else { 29 | size_t length = ( separator - path ); 30 | 31 | memcpy( tmp_path, path, length ); 32 | tmp_path[ length ] = 0; 33 | } 34 | 35 | snprintf( tmp_exec, sizeof( tmp_exec ), "%s/%s", tmp_path, filename ); 36 | execve( tmp_exec, argv, environ ); 37 | 38 | path = separator + 1; 39 | } while ( separator != NULL ); 40 | 41 | return -1; 42 | } 43 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | typedef void ( *atexit_func_t )( void ); 9 | 10 | static uint32_t atexit_count = 0; 11 | static atexit_func_t atexit_functions[ ATEXIT_MAX ]; 12 | 13 | int atexit( void ( *function )( void ) ) { 14 | if ( atexit_count >= ATEXIT_MAX ) { 15 | return -1; 16 | } 17 | 18 | atexit_functions[ atexit_count++ ] = function; 19 | 20 | return 0; 21 | } 22 | 23 | 24 | 25 | void exit( int status ) { 26 | uint32_t i; 27 | 28 | for ( i = 0; i < atexit_count; i++ ) { 29 | atexit_functions[ i ](); 30 | } 31 | 32 | fflush( stdout ); 33 | syscall1( SYS_exit, status ); 34 | for (;;); 35 | } 36 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/fchdir.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int fchdir( int fd ) { 10 | int error; 11 | 12 | error = syscall1( SYS_fchdir, fd ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | pid_t fork( void ) { 6 | return syscall0( SYS_fork ); 7 | } 8 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/fpathconf.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | long fpathconf( int fd, int name ) { 8 | /* TODO */ 9 | 10 | return -1; 11 | } 12 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/ftruncate.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int ftruncate( int fd, off_t length ) { 5 | /* TODO */ 6 | 7 | printf( "TODO: ftruncate() not yet implemented!\n" ); 8 | 9 | return -1; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/getdents.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int getdents( int fd, struct dirent* entry, unsigned int count ) { 10 | int error; 11 | 12 | error = syscall3( SYS_getdents, fd, ( int )entry, count ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/getdtablesize.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int getdtablesize( void ) { 4 | return 1024; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/getegid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | gid_t getegid( void ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/geteuid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | uid_t geteuid( void ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/getgid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | gid_t getgid( void ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/gethostname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int gethostname( char* name, size_t len ) { 5 | /* TODO */ 6 | 7 | snprintf( name, len, "localhost" ); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/getpgid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | pid_t getpgid( pid_t pid ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/getpgrp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | pid_t getpgrp( void ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/getpid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | pid_t getpid( void ) { 6 | return syscall0( SYS_getpid ); 7 | } 8 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/getppid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | pid_t getppid( void ) { 4 | /* TODO! */ 5 | 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/gettid.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | pid_t gettid( void ) { 10 | return syscall0( SYS_gettid ); 11 | } 12 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/getuid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | uid_t getuid( void ) { 5 | return syscall0( SYS_getuid ); 6 | } 7 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/isatty.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int isatty( int fd ) { 10 | int error; 11 | 12 | error = syscall1( SYS_isatty, fd ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return 0; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/link.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int link( const char* oldpath, const char* newpath ) { 5 | /* TODO */ 6 | 7 | printf( "TODO: link() not yet implemented!\n" ); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/lseek.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | off_t lseek( int fd, off_t offset, int whence ) { 10 | int error; 11 | off_t result; 12 | 13 | error = syscall4( SYS_lseek, fd, ( int )&offset, whence, ( int )&result ); 14 | 15 | if ( error < 0 ) { 16 | errno = -error; 17 | return ( off_t )-1; 18 | } 19 | 20 | return result; 21 | } 22 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/mmap.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | void * mmap (void *addr,size_t len,int prot,int flags,int fd,off_t offset){ 10 | return (void*)syscall5(SYS_mmap,(uint32_t) len, (uint32_t) prot, (uint32_t) flags,(uint32_t) fd,(uint32_t) offset); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/pipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int pipe( int pipefd[2] ) { 5 | /* TODO */ 6 | 7 | printf( "TODO: pipe() not yet implemented!\n" ); 8 | 9 | return -1; 10 | } 11 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/pread.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | ssize_t pread( int fd, void* buf, size_t count, off_t offset ) { 10 | int error; 11 | 12 | error = syscall4( SYS_pread, fd, ( int )buf, count, ( int )&offset ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/pwrite.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | ssize_t pwrite( int fd, const void* buf, size_t count, off_t offset ) { 10 | int error; 11 | 12 | error = syscall4( SYS_pwrite, fd, ( int )buf, count, ( int )&offset ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/read.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | ssize_t read( int fd, void* buf, size_t count ) { 10 | int error; 11 | 12 | error = syscall3( SYS_read, fd, ( int )buf, count ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return error; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/readlink.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | ssize_t readlink( const char* path, char* buf, size_t bufsiz ) { 11 | int error; 12 | 13 | error = syscall3( SYS_readlink, ( int )path, ( int )buf, bufsiz ); 14 | 15 | if ( error < 0 ) { 16 | errno = -error; 17 | return -1; 18 | } 19 | 20 | return error; 21 | } 22 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/rmdir.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int rmdir( const char* pathname ) { 10 | int error; 11 | 12 | error = syscall1( SYS_rmdir, ( int )pathname ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/sbrk.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | void* sbrk( int increment ) { 6 | return ( void* )syscall1( SYS_sbrk, increment ); 7 | } 8 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/setgid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int setgid( gid_t gid ) { 4 | return 0; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/setpgid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int setpgid( pid_t pid, pid_t pgid ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/setpgrp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int setpgrp( void ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/setregid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int setregid( gid_t rgid, gid_t egid ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/setreuid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int setreuid( uid_t ruid, uid_t euid ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/setuid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int setuid( uid_t uid ) { 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/sleep.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | unsigned int sleep( unsigned int seconds ) { 6 | uint64_t time; 7 | 8 | time = seconds * 1000000; 9 | 10 | syscall2( SYS_sleep_thread, ( int )&time, ( int )NULL ); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/symlink.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | int symlink( const char* oldpath, const char* newpath ) { 7 | int error; 8 | 9 | error = syscall2( SYS_symlink, ( int )oldpath, ( int )newpath ); 10 | 11 | if ( error < 0 ) { 12 | errno = -error; 13 | return -1; 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/ttyname.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static char ttyname_buffer[ 128 ]; 7 | 8 | int ttyname_r( int fd, char* buf, size_t buflen ) { 9 | int dir; 10 | int error; 11 | int found; 12 | 13 | struct stat st; 14 | struct dirent entry; 15 | 16 | error = fstat( fd, &st ); 17 | 18 | if ( error < 0 ) { 19 | return -1; 20 | } 21 | 22 | dir = open( "/device/terminal", O_RDONLY ); 23 | 24 | if ( dir < 0 ) { 25 | return -1; 26 | } 27 | 28 | found = 0; 29 | 30 | while ( getdents( dir, &entry, sizeof( struct dirent ) ) == 1 ) { 31 | if ( entry.d_ino == st.st_ino ) { 32 | snprintf( buf, buflen, "/device/terminal/%s", entry.d_name ); 33 | found = 1; 34 | break; 35 | } 36 | } 37 | 38 | close( dir ); 39 | 40 | if ( !found ) { 41 | return -1; 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | char* ttyname( int fd ) { 48 | int error; 49 | 50 | error = ttyname_r( fd, ttyname_buffer, sizeof( ttyname_buffer ) ); 51 | 52 | if ( error < 0 ) { 53 | return NULL; 54 | } 55 | 56 | return ttyname_buffer; 57 | } 58 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/unlink.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | int unlink( const char* pathname ) { 10 | int error; 11 | 12 | error = syscall1( SYS_unlink, ( int )pathname ); 13 | 14 | if ( error < 0 ) { 15 | errno = -error; 16 | return -1; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/sdk/src/libc/src/unistd/write.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | ssize_t write( int fd, const void* buf, size_t count ) { 7 | int error; 8 | 9 | error = syscall3( SYS_write, fd, ( int )buf, count ); 10 | 11 | if ( error < 0 ) { 12 | errno = -error; 13 | return -1; 14 | } 15 | 16 | return error; 17 | } 18 | -------------------------------------------------------------------------------- /src/userland/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | make -C "helloworld" all 4 | 5 | clean: 6 | make -C "helloworld" clean 7 | 8 | -------------------------------------------------------------------------------- /src/userland/helloworld/Makefile: -------------------------------------------------------------------------------- 1 | SDKDIR=../../sdk 2 | TARGET = hello 3 | OBJS=main.o 4 | 5 | include $(SDKDIR)/build.mak 6 | 7 | -------------------------------------------------------------------------------- /src/userland/helloworld/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SamyPesse/How-to-Make-a-Computer-Operating-System/eb30f8802fac9f0f1c28d3a96bb3d402bdfc4687/src/userland/helloworld/hello -------------------------------------------------------------------------------- /src/userland/helloworld/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main(int argc,char **argv){ 10 | printf("hello world ! \n"); 11 | return 0; 12 | } --------------------------------------------------------------------------------