diff options
Diffstat (limited to 'dev/c/src')
-rw-r--r-- | dev/c/src/basic/Makefile | 4 | ||||
-rw-r--r-- | dev/c/src/basic/shell.c | 57 | ||||
-rw-r--r-- | dev/c/src/fork/Makefile | 11 | ||||
-rw-r--r-- | dev/c/src/fork/fork.c | 38 | ||||
-rw-r--r-- | dev/c/src/fork/fork_exec.c | 40 | ||||
-rw-r--r-- | dev/c/src/hello/Makefile | 3 | ||||
-rw-r--r-- | dev/c/src/hello/hello.c | 6 | ||||
-rw-r--r-- | dev/c/src/linux/Makefile | 13 | ||||
-rw-r--r-- | dev/c/src/linux/init.c | 8 | ||||
-rw-r--r-- | dev/c/src/linux/rungdb.sh | 12 |
10 files changed, 187 insertions, 5 deletions
diff --git a/dev/c/src/basic/Makefile b/dev/c/src/basic/Makefile index f165c15..88f7890 100644 --- a/dev/c/src/basic/Makefile +++ b/dev/c/src/basic/Makefile @@ -1,7 +1,7 @@ CC=gcc CFLAGS=-Wall -basic-c: main.o basic.o +shell: shell.c clean: - rm -f *.o basic-c + rm -f *.o shell diff --git a/dev/c/src/basic/shell.c b/dev/c/src/basic/shell.c new file mode 100644 index 0000000..addc8a7 --- /dev/null +++ b/dev/c/src/basic/shell.c @@ -0,0 +1,57 @@ +#include <sys/types.h> +#include <sys/wait.h> +#include <errno.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sysexits.h> +#include <unistd.h> + +char *getinput(char *buffer, size_t buflen) { + printf("$$ "); + return fgets(buffer, buflen, stdin); +} + +void sig_int(int signo) { + printf("\nCaught SIGINT (Signal #%d)!\n$$ ", signo); + (void)fflush(stdout); +} + +int main(int argc, char **argv) { + char buf[BUFSIZ]; + pid_t pid; + int status; + + /* cast to void to silence compiler warnings */ + (void)argc; + (void)argv; + + if (signal(SIGINT, sig_int) == SIG_ERR) { + fprintf(stderr, "signal error: %s\n", strerror(errno)); + exit(1); + } + + while (getinput(buf, sizeof(buf))) { + buf[strlen(buf) - 1] = '\0'; + + if((pid=fork()) == -1) { + fprintf(stderr, "shell: can't fork: %s\n", + strerror(errno)); + continue; + } else if (pid == 0) { /* child */ + execlp(buf, buf, (char *)0); + fprintf(stderr, "shell: couldn't exec %s: %s\n", buf, + strerror(errno)); + exit(EX_UNAVAILABLE); + } + + /* parent waits */ + if ((pid=waitpid(pid, &status, 0)) < 0) { + fprintf(stderr, "shell: waitpid error: %s\n", + strerror(errno)); + } + } + + exit(EX_OK); +} diff --git a/dev/c/src/fork/Makefile b/dev/c/src/fork/Makefile new file mode 100644 index 0000000..a737794 --- /dev/null +++ b/dev/c/src/fork/Makefile @@ -0,0 +1,11 @@ + +progs=fork fork_exec +all: $(progs) + +fork: fork.c + +fork_exec: fork_exec.c + + +clean: + rm $(progs) diff --git a/dev/c/src/fork/fork.c b/dev/c/src/fork/fork.c new file mode 100644 index 0000000..25e4909 --- /dev/null +++ b/dev/c/src/fork/fork.c @@ -0,0 +1,38 @@ + +#include <sys/types.h> +#include <sys/wait.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> + +int main(){ + + pid_t cpid,ppid; + char errbuf[1024]; + + cpid = fork(); + if (cpid == -1) { + (void) snprintf(errbuf, sizeof(errbuf), + "fork: %s", strerror(errno)); + printf("%s\n", errbuf); + exit(1); + } + + if (cpid == 0) { + //child + ppid = getppid(); + if(ppid == 1){ + printf("parent died ?\n"); + _exit(1); + } + printf("I'm child with %i, parent %i\n", getpid(), ppid); + _exit(0); + } +/* parent */ + wait(NULL); + printf("Child id: %i\n", cpid); + return 0; +} + diff --git a/dev/c/src/fork/fork_exec.c b/dev/c/src/fork/fork_exec.c new file mode 100644 index 0000000..7f87c84 --- /dev/null +++ b/dev/c/src/fork/fork_exec.c @@ -0,0 +1,40 @@ +#include <sys/types.h> +#include <sys/wait.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> + +int main(){ + + pid_t cpid,ppid; + char errbuf[1024]; + + char *prog = "vim"; + char *const args[3]= {"vim", "fork_exec.c", NULL}; + + cpid = fork(); + if (cpid == -1) { + (void) snprintf(errbuf, sizeof(errbuf), + "fork: %s", strerror(errno)); + printf("%s\n", errbuf); + exit(1); + } + + if (cpid == 0) { + //child + ppid = getppid(); + if(ppid == 1){ + printf("parent died ?\n"); + _exit(1); + } + execvp(prog, args); + _exit(0); + } +/* parent */ + wait(NULL); + printf("Child id: %i\n", cpid); + return 0; +} + diff --git a/dev/c/src/hello/Makefile b/dev/c/src/hello/Makefile index a6d9f07..2c0ff2d 100644 --- a/dev/c/src/hello/Makefile +++ b/dev/c/src/hello/Makefile @@ -1,7 +1,6 @@ -CC=gcc CFLAGS=-Wall -hello: hello.o +hello: hello.c clean: rm -f *.o hello diff --git a/dev/c/src/hello/hello.c b/dev/c/src/hello/hello.c index df66493..092efa5 100644 --- a/dev/c/src/hello/hello.c +++ b/dev/c/src/hello/hello.c @@ -1,6 +1,10 @@ #include <stdio.h> +#include <unistd.h> int main() { - printf("hello World!"); + char *name; + name = getlogin(); + + printf("hello %s!\n", name); return 0; } diff --git a/dev/c/src/linux/Makefile b/dev/c/src/linux/Makefile new file mode 100644 index 0000000..e026551 --- /dev/null +++ b/dev/c/src/linux/Makefile @@ -0,0 +1,13 @@ +CC=gcc +CFLAGS=-Wall -static +#LDFLAGS=-lc -lnss_files -lnss_dns -lresolv + +all: init ramdisk + +init: init.c + +ramdisk: + find . | cpio -o -H newc | gzip > rootfs.cpio.gz + +clean: + rm -f *.o init rootfs.cpio.gz diff --git a/dev/c/src/linux/init.c b/dev/c/src/linux/init.c new file mode 100644 index 0000000..10af0be --- /dev/null +++ b/dev/c/src/linux/init.c @@ -0,0 +1,8 @@ +#include <stdio.h> +#include <unistd.h> + +int main() { + printf("FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n"); + sleep(0xFFFFFFFF); + return 0; +} diff --git a/dev/c/src/linux/rungdb.sh b/dev/c/src/linux/rungdb.sh new file mode 100644 index 0000000..9408d7f --- /dev/null +++ b/dev/c/src/linux/rungdb.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +gdb \ + -ex "add-auto-load-safe-path $(pwd)" \ + -ex "file vmlinux" \ + -ex 'set arch i386:x86-64:intel' \ + -ex 'target remote localhost:1234' \ + -ex 'hbreak start_kernel' \ + -ex 'continue' \ + -ex 'disconnect' \ + -ex 'set arch i386:x86-64' \ + -ex 'target remote localhost:1234' |