CFLAGS =

all: insmod rmmod lsmod sys_hello.o hello

insmod: insmod.c modules.h
	$(CC) $(CFLAGS) insmod.c -O6 -DLINUX -o insmod

rmmod: rmmod.c modules.h
	$(CC) $(CFLAGS) rmmod.c -O6 -DLINUX -o rmmod

lsmod:	Makefile
	echo "#!/bin/sh" >lsmod
	echo "echo \"Module:        #pages:\"" >>lsmod
	echo "cat /proc/modules" >>lsmod
	chmod a+x lsmod

sys_hello.o:	sys_hello.c modules.h
	$(CC) $(CFLAGS) sys_hello.c -c -O6 -DLINUX -o sys_hello.o

hello:	hello.c modules.h
	$(CC) $(CFLAGS) hello.c -O6 -DLINUX -o hello

modules.h:	/usr/include/linux/sys.h sys.awk Makefile
	echo -e "#define\tSYS_CREATE_MODULE\t(`gawk -f sys.awk \
	</usr/include/linux/sys.h | tr ',' '\n' | grep [a-zA-Z_] \
	| wc | cut -c 1-7`)" > modules.h
	echo -e "#define\tSYS_INIT_MODULE\t(SYS_CREATE_MODULE+1)" >> modules.h
	echo -e "#define\tSYS_DELETE_MODULE\t(SYS_CREATE_MODULE+2)" >> modules.h
	echo -e "#define\tSYS_GET_KERNEL_SYMS\t(SYS_CREATE_MODULE+3)" >> modules.h
	echo -e "#define\tSYS_FREE_ENTRY\t(SYS_CREATE_MODULE+4)" >> modules.h

clean:
	rm -f *.o modules.h hello insmod rmmod lsmod *~
