Изменение структуры, вырезание ассемблера, добавление работы с аргументами в shell.c

This commit is contained in:
justuser 2025-01-24 23:39:14 +03:00
parent 593c5051ac
commit 8535d05433
25 changed files with 103 additions and 67 deletions

14
comp_run_iso.sh Executable file
View File

@ -0,0 +1,14 @@
back=$(pwd)
cd progs
cat files | cpio -H newc -o > init.cpio
cd $back
rm -rf linux-*/arch/x86/boot/image.iso
cd linux-*
#make isoimage FDARGS="initrd=/init.cpio" FDINITRD="../progs/init.cpio"
# \/ \/ Turn off system messages (very annoying)
make isoimage FDARGS="initrd=/init.cpio loglevel=0 quiet" FDINITRD="../progs/init.cpio"
qemu-system-x86_64 -cdrom arch/x86/boot/image.iso
cd $back

View File

@ -46,4 +46,4 @@ make -j 4
make isoimage FDARGS="initrd=/init.cpio" FDINITRD="../progs/init.cpio"
# Запуск ISO
# -smp 1 - одно ядро, иначе сбивается время
qemu-system-x86_64 -cdrom arch/x86/boot/image.iso -smp 1
qemu-system-x86_64 -cdrom arch/x86/boot/image.iso

BIN
progs/echo Executable file

Binary file not shown.

View File

@ -1 +1,3 @@
init
ls
echo

Binary file not shown.

Binary file not shown.

BIN
progs/ls Executable file

Binary file not shown.

BIN
progs/shell Executable file

Binary file not shown.

View File

@ -1,27 +0,0 @@
#include <unistd.h>
#include <sys/wait.h>
int real_waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options, void*);
int main()
{
char command[255];
for (;;) {
write(1, "# ", 2);
int count = read(0, command, 255);
// /bin/ls\n -> /bin/ls\0
command[count - 1] = 0;
pid_t fork_result = fork();
if (fork_result == 0) {
execve(command, 0, 0);
break;
} else {
// wait
//
siginfo_t info;
real_waitid(P_ALL, 0, &info, WEXITED, 0);
}
}
_exit(0);
}

Binary file not shown.

2
progs/src/echo/build.sh Executable file
View File

@ -0,0 +1,2 @@
gcc -static echo.c -o echo
cp echo ../../echo

BIN
progs/src/echo/echo Executable file

Binary file not shown.

12
progs/src/echo/echo.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
int main(int argc, char* argv[]) {
if (argc >= 2) {
for (int i = 1; i < argc; i++) {
printf("%s ", argv[i]);
}
printf("\n");
}
return 0;
}

2
progs/src/echo/local_run.sh Executable file
View File

@ -0,0 +1,2 @@
gcc -static echo.c -o echo
./echo test

2
progs/src/ls/build.sh Executable file
View File

@ -0,0 +1,2 @@
gcc -static ls.c -o ls
cp ls ../../ls

2
progs/src/ls/local_run.sh Executable file
View File

@ -0,0 +1,2 @@
gcc -static ls.c -o ls
./ls

BIN
progs/src/ls/ls Executable file

Binary file not shown.

29
progs/src/ls/ls.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int main(int argc, char* argv[]) {
DIR *dir;
struct dirent *entry;
if (argc == 2) {
const char *path = argv[1];
dir = opendir(path);
} else {
const char *path = ".";
dir = opendir(path);
}
if (dir == NULL) {
printf("No such file or directory\n");
return 1;
}
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
printf("%s\n", entry->d_name);
}
}
closedir(dir);
return 0;
}

2
progs/src/shell/build.sh Executable file
View File

@ -0,0 +1,2 @@
gcc -static shell.c -o shell
cp shell ../../init

2
progs/src/shell/local_run.sh Executable file
View File

@ -0,0 +1,2 @@
gcc -static shell.c -o shell
./shell

BIN
progs/src/shell/shell Executable file

Binary file not shown.

33
progs/src/shell/shell.c Normal file
View File

@ -0,0 +1,33 @@
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
int main()
{
char command[255];
for (;;) {
write(1, "# ", 2);
int count = read(0, command, 255);
command[count - 1] = 0;
char *argArray[100];
char *token = strtok(command, " ");
int argCount = 0;
while (token != NULL) {
argArray[argCount++] = token;
token = strtok(NULL, " ");
}
argArray[argCount] = NULL;
pid_t fork_result = fork();
if (fork_result == 0) {
execve(argArray[0], argArray, NULL);
_exit(1);
} else {
int status;
waitpid(fork_result, &status, 0);
}
}
_exit(0);
}

BIN
progs/src/shell/shell.o Normal file

Binary file not shown.

BIN
progs/sys

Binary file not shown.

View File

@ -1,39 +0,0 @@
.intel_syntax noprefix
.global write
.global read
.global fork
.global execve
.global real_waitid
.global _exit
write:
mov rax, 1
syscall
ret
read:
mov rax, 0
syscall
ret
execve:
mov rax, 59
syscall
ret
fork:
mov rax, 57
syscall
ret
real_waitid:
mov rax, 247
mov r10, rcx
syscall
ret
_exit:
mov rax, 60
syscall