Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add courses demos #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions courses/curs-01-demo/syscall-libc-call-asm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/syscall-libc-call
14 changes: 14 additions & 0 deletions courses/curs-01-demo/syscall-libc-call-asm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
LDFLAGS = -m32

.PHONY: all clean

all: syscall-libc-call

syscall-libc-call: syscall-libc-call.o

syscall-libc-call.o: syscall-libc-call.asm
nasm -f elf32 -o $@ $<

clean:
-rm -f *~
-rm -f syscall-libc-call.o syscall-libc-call
26 changes: 26 additions & 0 deletions courses/curs-01-demo/syscall-libc-call-asm/syscall-libc-call.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern printf

section .rodata
message db "anaaremere", 10, 0
len equ $-message

section .text

global main

main:
push ebp
mov ebp, esp

push message
call printf
add esp, 4

mov ebx, 1
mov ecx, message
mov edx, len
mov eax, 4
int 0x80

leave
ret
3 changes: 3 additions & 0 deletions courses/curs-02-demo/buffered-system-io/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/buffered
/system
/*.txt
18 changes: 18 additions & 0 deletions courses/curs-02-demo/buffered-system-io/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CC = gcc
CPPFLAGS = -I../utils
CFLAGS = -Wall -Wextra -g

.PHONY: all clean

all: buffered system

buffered: buffered.o

buffered.o: buffered.c ../utils/utils.h

system: system.o

system.o: system.c ../utils/utils.h

clean:
-rm -f *.o *~ buffered system *.txt
32 changes: 32 additions & 0 deletions courses/curs-02-demo/buffered-system-io/buffered.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "utils.h"

int main(void)
{
FILE *f;
size_t i;

for (i = 0; i < 10; i++) {
printf("a");
sleep(1);
}
printf("\n");

f = fopen("f.txt", "w+");
DIE(f == NULL, "fopen");

for (i = 0; i < 10; i++) {
fprintf(f, "a");
fprintf(f, "\n");
sleep(1);
}
fflush(f);

fclose(f);

return 0;
}
33 changes: 33 additions & 0 deletions courses/curs-02-demo/buffered-system-io/system.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>

#include "utils.h"

int main(void)
{
int fd;
size_t i;

for (i = 0; i < 10; i++) {
write(STDOUT_FILENO, "a", 1);
sleep(1);
}
write(STDOUT_FILENO, "\n", 1);

fd = open("f.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
DIE(fd < 0, "open");

for (i = 0; i < 10; i++) {
write(fd, "a", 1);
write(fd, "\n", 1);
sleep(1);
}

close(fd);

return 0;
}
2 changes: 2 additions & 0 deletions courses/curs-02-demo/c-file-ops/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/c-file-ops
/f.txt
14 changes: 14 additions & 0 deletions courses/curs-02-demo/c-file-ops/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CC = gcc
CPPFLAGS = -I../utils
CFLAGS = -Wall -Wextra -g

.PHONY: all clean

all: c-file-ops

c-file-ops: c-file-ops.o

c-file-ops.o: c-file-ops.c ../utils/utils.h

clean:
-rm -f *.o *~ c-file-ops *.txt
60 changes: 60 additions & 0 deletions courses/curs-02-demo/c-file-ops/c-file-ops.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>

#include "utils.h"

#define BUFSIZE 1024

static char buffer[BUFSIZE] = { '1', };

static void msg_and_wait(const char *msg)
{
char buf[256];

puts(msg);
fputs(" Press ENTER to continue ...", stdout); fflush(stdout);

while(1) {
fgets(buf, 256, stdin);
if (buf[strlen(buf)-1] == '\n')
break;
}
}

int main(void)
{
int fd;
ssize_t n;
int ret;

msg_and_wait("started program");

fd = open("f.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
DIE(fd < 0, "open");
msg_and_wait("opened f.txt");

n = write(fd, buffer, BUFSIZE);
DIE(n < 0, "write");
msg_and_wait("written 1024 bytes");

n = lseek(fd, -512, SEEK_CUR);
DIE(n < 0, "lseek");
msg_and_wait("went backwards 512 bytes");

n = read(fd, buffer, 256);
DIE(n < 0, "read");
msg_and_wait("read 256 bytes");

ret = ftruncate(fd, 256);
DIE(ret < 0, "ftruncate");
msg_and_wait("truncated file to 256 bytes");

close(fd);
msg_and_wait("closed files");

return 0;
}
1 change: 1 addition & 0 deletions courses/curs-02-demo/fdtsize/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/fdtsize
14 changes: 14 additions & 0 deletions courses/curs-02-demo/fdtsize/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CFLAGS = -Wall -Wextra -g

.PHONY: all clean

all: fdtsize

fdtsize: fdtsize.o

fdtsize.o: fdtsize.c

clean:
-rm -f *~
-rm -f fdtsize.o fdtsize

Empty file.
21 changes: 21 additions & 0 deletions courses/curs-02-demo/fdtsize/fdtsize.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
FILE *f;
size_t i;

for (i = 0; i < 1030; i++) {
printf("i = %zu, open file\n", i);
f = fopen("aaa", "r");
if (f == NULL) {
perror("fopen");
break;
}
}

printf("Opened %zu file descriptors.\n", i);

return 0;
}
1 change: 1 addition & 0 deletions courses/curs-02-demo/fopen-perm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/fopen-perm
15 changes: 15 additions & 0 deletions courses/curs-02-demo/fopen-perm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CPPFLAGS = -I../utils
CFLAGS = -Wall -Wextra -g

.PHONY: all clean

all: fopen-perm

fopen-perm: fopen-perm.o

fopen-perm.o: fopen-perm.c

clean:
-rm -f *~
-rm -f fopen-perm.o fopen-perm

Empty file.
38 changes: 38 additions & 0 deletions courses/curs-02-demo/fopen-perm/fopen-perm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>

#include "utils.h"

int main(void)
{
FILE *f;

f = fopen("aaa", "r");
fclose(f);

f = fopen("aaa", "w");
DIE(f == NULL, "fopen");
fclose(f);

f = fopen("aaa", "a");
DIE(f == NULL, "fopen");
fclose(f);

f = fopen("aaa", "rw");
DIE(f == NULL, "fopen");
fclose(f);

f = fopen("aaa", "r+");
DIE(f == NULL, "fopen");
fclose(f);

f = fopen("aaa", "w+");
DIE(f == NULL, "fopen");
fclose(f);

f = fopen("aaa", "a+");
DIE(f == NULL, "fopen");
fclose(f);

return 0;
}
2 changes: 2 additions & 0 deletions courses/curs-02-demo/open-dup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dup
/open
18 changes: 18 additions & 0 deletions courses/curs-02-demo/open-dup/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CC = gcc
CPPFLAGS = -I../utils
CFLAGS = -Wall -Wextra -g

.PHONY: all clean

all: open dup

open: open.o

open.o: open.c ../utils/utils.h

dup: dup.o

dup.o: dup.c ../utils/utils.h

clean:
-rm -f *.o *~ open dup *.txt
53 changes: 53 additions & 0 deletions courses/curs-02-demo/open-dup/dup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "utils.h"

static void msg_and_wait(const char *msg)
{
char buf[256];

puts(msg);
fputs(" Press ENTER to continue ...", stdout); fflush(stdout);

while(1) {
fgets(buf, 256, stdin);
if (buf[strlen(buf)-1] == '\n')
break;
}
}

int main(void)
{

int fd1, fd2, rc, pos;

msg_and_wait("open start");

fd1 = open("Makefile", O_RDWR);
DIE(fd1 < 0, "open file.txt");
msg_and_wait("open Makefile");

fd2 = dup(fd1);
DIE(fd2 < 0, "dup");
msg_and_wait("fd2 = dup(fd1)");

pos = lseek(fd1, 100, SEEK_SET);
DIE(pos < 0, "lseek");
msg_and_wait("lseek on fd1");

rc = close(fd1);
DIE(rc < 0, "fd1");
msg_and_wait("close fd1");

rc = close(fd2);
DIE(rc < 0, "fd2");
msg_and_wait("close fd2");

return 0;
}
Loading