├── .gitmodules ├── 16x9_w16_h8.c ├── 64bit ├── Makefile └── test.S ├── CIH ├── gcc-trace-func │ ├── g.c │ └── g.sh └── my_kill │ ├── g.c │ └── g.sh ├── DPRINTF ├── DPRINTF_With__VA_ARGS__ │ ├── README │ └── dprintf.c └── DPRINTF_Without__VA_ARGS__ │ ├── README │ ├── dprintf.cpp │ ├── dprintf.h │ ├── main_c99.c │ ├── thread_tool.c │ └── thread_tool.h ├── EmbeddedLinuxJollen ├── Appendix_A │ └── mw_hello.c ├── EmbeddedLinuxJollen.tar.gz ├── ch02 │ ├── hello.c │ ├── hello2.c │ ├── hello3.c │ ├── math.c │ ├── pascal.c │ └── prefix.c ├── ch04 │ ├── daemon.c │ ├── detached.c │ ├── execvp.c │ ├── fork.c │ ├── join_thread.c │ ├── join_thread2.c │ ├── join_thread3.c │ ├── module-1 │ │ ├── Makefile │ │ ├── module-1.c │ │ ├── module-1b.c │ │ └── module-1c.c │ ├── module-2 │ │ ├── Makefile │ │ └── module-2.c │ ├── module-3 │ │ ├── Makefile │ │ └── module-3.c │ ├── pid.c │ ├── sem.c │ ├── serial.c │ ├── serial.h │ ├── signal.c │ ├── thread.c │ ├── thread2.c │ ├── unix_spawn.c │ ├── wait.c │ └── zombie.c ├── ch05 │ ├── client.c │ ├── mmap_read.c │ ├── mmap_write.c │ ├── pipe.c │ ├── pipe_dup.c │ ├── pipe_shell.c │ ├── server.c │ ├── shm_allocate.c │ ├── shm_read.c │ └── web.c └── ch07 │ ├── Makefile │ ├── v4l.c │ └── v4l.h ├── ExampleLinuxProgramming ├── Example_Linux_programming.tar.gz └── Example_Linux_programming │ ├── 0_preface.pdf │ ├── 10_socket.pdf │ ├── 1_introduction.pdf │ ├── 2_tools.pdf │ ├── 3_file.pdf │ ├── 4_standard_io.pdf │ ├── 5_information.pdf │ ├── 6_process.pdf │ ├── 7_signal.pdf │ ├── 8_advance_io.pdf │ ├── 9_ipc.pdf │ ├── auto.tgz │ ├── chap1.tgz │ ├── chap10.tgz │ ├── chap10.tgz.1 │ ├── chap2.tgz │ ├── chap3.tgz │ ├── chap4.tgz │ ├── chap6.tgz │ ├── chap7.tgz │ ├── chap8.tgz │ ├── chap9.tgz │ ├── chapter1 │ ├── 1-1 │ │ ├── Makefile │ │ ├── README.TXT │ │ ├── error.c │ │ ├── ls1.c │ │ └── ourhdr.h │ ├── 1-2 │ │ ├── Makefile │ │ ├── README.TXT │ │ ├── error.c │ │ ├── getcputc.c │ │ ├── mycat.c │ │ └── ourhdr.h │ ├── 1-3 │ │ ├── Makefile │ │ ├── README.txt │ │ ├── error.c │ │ ├── ourhdr.h │ │ ├── process.c │ │ └── shell1.c │ ├── 1-4 │ │ ├── Makefile │ │ ├── README.TXT │ │ └── malloc.c │ ├── 1-5 │ │ ├── Makefile │ │ ├── README.TXT │ │ ├── ourhdr.h │ │ └── testerror.c │ ├── 1-6 │ │ ├── Makefile │ │ ├── README.TXT │ │ ├── ourhdr.h │ │ └── uidgid.c │ └── 1-7 │ │ ├── Makefile │ │ ├── README.TXT │ │ ├── error.c │ │ ├── ourhdr.h │ │ └── shell2.c │ ├── chapter10 │ ├── 10-1 │ │ ├── Makefile │ │ ├── client.c │ │ └── server.c │ ├── 10-2 │ │ ├── Makefile │ │ ├── client.c │ │ ├── client2.c │ │ ├── client3.c │ │ └── server.c │ ├── 10-3 │ │ ├── Makefile │ │ └── name.c │ ├── 10-4 │ │ ├── Makefile │ │ ├── client.c │ │ └── server.c │ └── 10-5 │ │ ├── Makefile │ │ ├── client.c │ │ └── server.c │ ├── chapter2 │ ├── 2-1 │ │ ├── README.txt │ │ └── hello.c │ ├── 2-2 │ │ ├── README.TXT │ │ ├── hello.c │ │ ├── hello.cpp │ │ └── hello.s │ ├── 2-3 │ │ ├── README.TXT │ │ ├── include │ │ │ └── test.h │ │ ├── lib │ │ │ └── testlib.c │ │ ├── test.c │ │ ├── test2.c │ │ └── test3.c │ ├── 2-4 │ │ ├── Makefile │ │ ├── README.TXT │ │ ├── inline.c │ │ ├── non_ansi.c │ │ └── non_ansi2.c │ ├── 2-5 │ │ ├── README.TXT │ │ ├── hello.c │ │ └── hello.h │ ├── 2-6 │ │ ├── README.TXT │ │ └── warning.c │ └── 2-7 │ │ ├── README.TXT │ │ ├── attribute.c │ │ ├── function.c │ │ ├── inline.c │ │ └── range.c │ ├── chapter3 │ └── 3-1 │ │ ├── Makefile │ │ └── setext2.c │ ├── chapter4 │ └── 4-1 │ │ ├── getc.c │ │ └── square.c │ ├── chapter6 │ ├── 6-1 │ │ ├── Makefile │ │ ├── doatexit.c │ │ ├── error.c │ │ └── ourhdr.h │ ├── 6-10 │ │ ├── Makefile │ │ └── thrdcancel.c │ ├── 6-2 │ │ ├── Makefile │ │ ├── error.c │ │ ├── fork1.c │ │ └── ourhdr.h │ ├── 6-3 │ │ ├── Makefile │ │ └── waiter.c │ ├── 6-4 │ │ ├── Makefile │ │ ├── error.c │ │ ├── ourhdr.h │ │ └── tellwait1.c │ ├── 6-5 │ │ ├── Makefile │ │ ├── error.c │ │ ├── exec1.c │ │ └── ourhdr.h │ ├── 6-6 │ │ ├── Makefile │ │ ├── error.c │ │ ├── ourhdr.h │ │ └── systest3.c │ ├── 6-7 │ │ ├── Makefile │ │ └── resusg2.c │ ├── 6-8 │ │ ├── Makefile │ │ └── resusg1.c │ └── 6-9 │ │ ├── Makefile │ │ └── thrdcreat.c │ ├── chapter7 │ ├── 7-1 │ │ ├── Makefile │ │ ├── error.c │ │ ├── fkill.c │ │ ├── ourhdr.h │ │ └── sigusr.c │ ├── 7-2 │ │ ├── Makefile │ │ └── mkset.c │ ├── 7-3 │ │ ├── Makefile │ │ └── block.c │ ├── 7-4 │ │ ├── Makefile │ │ └── pending.c │ ├── 7-5 │ │ ├── Makefile │ │ └── sigaction.c │ └── 7-6 │ │ ├── Makefile │ │ └── pthread_signal.c │ ├── chapter8 │ ├── 8-1 │ │ ├── Makefile │ │ ├── clrfl.c │ │ ├── error.c │ │ ├── nonblockw.c │ │ ├── ourhdr.h │ │ └── setfl.c │ ├── 8-2 │ │ ├── Makefile │ │ ├── lockfile.c │ │ └── test.tmp │ ├── 8-3 │ │ ├── Makefile │ │ └── select.c │ └── 8-4 │ │ ├── Makefile │ │ └── mmcat.c │ ├── chapter9 │ ├── 9-1 │ │ ├── Makefile │ │ └── pipe.c │ ├── 9-10 │ │ ├── Makefile │ │ └── msq.c │ ├── 9-11 │ │ ├── Makefile │ │ └── sendmsg.c │ ├── 9-12 │ │ ├── Makefile │ │ └── recvmsg.c │ ├── 9-13 │ │ ├── Makefile │ │ └── ctlmsg.c │ ├── 9-14 │ │ ├── Makefile │ │ └── semaphore.c │ ├── 9-15 │ │ ├── Makefile │ │ └── lock.c │ ├── 9-16 │ │ ├── Makefile │ │ └── del.c │ ├── 9-17 │ │ ├── reader.c │ │ ├── shared.c │ │ └── writer.c │ ├── 9-2 │ │ ├── Makefile │ │ └── pipe.c │ ├── 9-3 │ │ ├── Makefile │ │ └── pipe.c │ ├── 9-4 │ │ ├── Makefile │ │ └── pipe.c │ ├── 9-5 │ │ ├── Makefile │ │ └── pipe.c │ ├── 9-6 │ │ ├── Makefile │ │ └── pipe.c │ ├── 9-7 │ │ ├── Makefile │ │ └── pipe.c │ ├── 9-8 │ │ ├── Makefile │ │ └── popen.c │ └── 9-9 │ │ ├── Makefile │ │ ├── creator.c │ │ ├── reader.c │ │ └── writer.c │ ├── chat.c │ ├── echo.c │ ├── filelist_for_wget │ └── gdb.tgz ├── LearningTheBashShell2nd ├── bash2examples.tar.gz └── examples │ ├── Manifest │ ├── Readme │ ├── chapter4 │ ├── albums │ ├── ascript │ ├── ascript1 │ ├── highest │ ├── highest1 │ └── pushpop │ ├── chapter5 │ ├── cd1 │ ├── fileinfo │ ├── fileinfo1 │ ├── highest │ ├── highest1 │ ├── korncd │ ├── listpath │ ├── listpath1 │ ├── listpath2 │ ├── popd1 │ ├── procimage │ ├── procimage1 │ ├── pushd1 │ ├── pushd2 │ ├── recls │ ├── selectd │ ├── waitcp │ └── waitcp1 │ ├── chapter6 │ ├── accounts │ ├── highest │ ├── ndu │ ├── procimage │ └── pushpop │ ├── chapter7 │ ├── findterm │ ├── findterm1 │ ├── findterm2 │ ├── findterm3 │ ├── makecmd │ ├── procimage │ ├── procimage1 │ └── term │ ├── chapter8 │ ├── crtlC │ ├── ctrlC1 │ ├── ctrlC2 │ ├── hatter.mad │ ├── hatter.mad1 │ └── smail │ ├── chapter9 │ └── guess │ ├── debugger │ ├── bashdb │ ├── bashdb.fns │ └── bashdb.pre │ ├── ftl_language │ ├── Readme │ └── ftla.tgz │ └── somesolutions │ └── recls.extra ├── LinuxDeviceDriver ├── examples.tar.gz └── examples │ ├── LICENSE │ ├── Makefile │ ├── include │ └── lddbus.h │ ├── lddbus │ ├── Makefile │ └── lddbus.c │ ├── misc-modules │ ├── Makefile │ ├── complete.c │ ├── faulty.c │ ├── hello.c │ ├── hellop.c │ ├── jiq.c │ ├── jit.c │ ├── kdataalign.c │ ├── kdatasize.c │ ├── seq.c │ ├── silly.c │ └── sleepy.c │ ├── misc-progs │ ├── Makefile │ ├── asynctest.c │ ├── dataalign.c │ ├── datasize.c │ ├── gdbline │ ├── inp.c │ ├── load50.c │ ├── mapcmp.c │ ├── mapper.c │ ├── nbtest.c │ ├── netifdebug.c │ ├── outp.c │ ├── polltest.c │ ├── setconsole.c │ └── setlevel.c │ ├── pci │ ├── Makefile │ └── pci_skel.c │ ├── sbull │ ├── Makefile │ ├── sbull.c │ ├── sbull.h │ ├── sbull_load │ └── sbull_unload │ ├── scull │ ├── Makefile │ ├── access.c │ ├── main.c │ ├── pipe.c │ ├── scull.h │ ├── scull.init │ ├── scull_load │ └── scull_unload │ ├── scullc │ ├── Makefile │ ├── main.c │ ├── mmap.c │ ├── scullc.h │ ├── scullc_load │ └── scullc_unload │ ├── sculld │ ├── Makefile │ ├── main.c │ ├── mmap.c │ ├── sculld.h │ ├── sculld_load │ └── sculld_unload │ ├── scullp │ ├── Makefile │ ├── main.c │ ├── mmap.c │ ├── scullp.h │ ├── scullp_load │ └── scullp_unload │ ├── scullv │ ├── Makefile │ ├── main.c │ ├── mmap.c │ ├── scullv.h │ ├── scullv_load │ └── scullv_unload │ ├── short │ ├── Makefile │ ├── short.c │ ├── short_load │ └── short_unload │ ├── shortprint │ ├── Makefile │ ├── shortprint.c │ ├── shortprint.h │ ├── shortprint_load │ └── shortprint_unload │ ├── simple │ ├── Makefile │ ├── simple.c │ ├── simple_load │ └── simple_unload │ ├── skull │ ├── Makefile │ ├── skull_clean.c │ └── skull_init.c │ ├── snull │ ├── Makefile │ ├── snull.c │ ├── snull.h │ ├── snull_load │ └── snull_unload │ ├── tty │ ├── Makefile │ ├── tiny_serial.c │ └── tiny_tty.c │ └── usb │ ├── Makefile │ └── usb-skeleton.c ├── LinuxServerHacks ├── LSHcode.tgz ├── LSHcode.zip └── LinuxServerHacks-code │ ├── ch01 │ ├── Makefile.mail │ ├── Makefile.push │ ├── albumize │ └── dealbumize │ ├── ch03 │ ├── balance-push │ ├── balance.back │ ├── balance.front │ ├── daily_snapshot_rotate.sh │ └── make_snapshot.sh │ ├── ch04 │ ├── iptables.tricks │ ├── rc.firewall │ ├── vtund.conf-client │ ├── vtund.conf-server │ └── vtundconf │ ├── ch05 │ ├── diskage │ ├── go-ogle │ ├── httptop │ ├── movein.sh │ ├── takeover │ └── tl │ ├── ch06 │ ├── profile │ └── ssh-to │ ├── ch07 │ ├── mince │ └── rcg │ └── ch08 │ ├── logflume.pl │ └── referral-report.pl ├── Makefile ├── README.mkd ├── arm ├── arm_char.c ├── typedef_int.c └── typedef_short.c ├── array_offset.c ├── array_size.c ├── asm └── tmp.c ├── asm_nop.c ├── bash ├── Davinci_CodecChnage_Verify.sh ├── Defensive-BASH-Programming.sh ├── Fuser.sh ├── SDTest2.sh ├── acd_func.sh ├── audioin_sync_to_gain ├── audioin_sync_to_gainp ├── calc_interrupts.sh ├── chroot.sh ├── chrt.sh ├── cp_and_rename.sh ├── cpp_in_bash.sh ├── dialog.sh ├── eval_memory_arm.sh ├── exec_fork_source │ ├── 1.sh │ └── 2.sh ├── finddate.sh ├── fsdtest-pid ├── ftpfw-cisco.sh ├── ftpfw-vvtk.sh ├── func_exit_return.sh ├── func_exit_return2.sh ├── getSystemTime.cgi ├── oswin.sh ├── port_forward.sh ├── rtmp.sh ├── sd_test.sh ├── sed_practice ├── select.sh ├── serverreport.cgi ├── shell_expansion.sh ├── shopt_expand_aliases.sh ├── sleepsort.sh ├── test.sh ├── testargs.sh ├── thsrc_curl.sh ├── timediff.sh └── zero-padding.sh ├── bitops.h ├── buserror.c ├── c-file └── tmpfile.c ├── c-function-pointer ├── README.txt ├── function_pointer.c └── function_pointer_typedef.c ├── c-macro └── stringfication2.c ├── c-memory ├── leak.c └── mcheck.c ├── c-object-oriented ├── defineclass.c ├── encapsulation.c ├── inheritance.c └── polymorphism.c ├── c-string ├── sscanf.c └── unicode.c ├── c-struct ├── README.txt ├── linkedlistembed.c ├── linklistobj.c ├── structbits.c └── structptr.c ├── chfd_linux.c ├── comet-http-streaming ├── CDF.xml ├── getSystemTime.cgi ├── hiddenIframe.html ├── htmlfile.html ├── test.cgi └── volalarm.cgi ├── countbit.c ├── cyg-profile ├── README ├── cyg-profile.c ├── cyg-profile.h ├── cyg-resolve.pl ├── test.c └── test.cxx ├── daemon.c ├── date_by_gettimeofday.c ├── defunct.c ├── eServ ├── LICENSE ├── Makefile ├── README ├── cgi_custom.c ├── cgi_custom.h ├── libeserv │ ├── analysis.c │ ├── analysis.h │ ├── cgi.c │ ├── cgi.h │ ├── entry.c │ ├── entry.h │ ├── hash.c │ ├── hash.h │ ├── http.c │ ├── http.h │ ├── io.c │ ├── io.h │ ├── mempool.c │ ├── mempool.h │ ├── misc.c │ ├── misc.h │ └── request.c ├── main.c └── www │ ├── 1.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── alien.png │ ├── gallery.html │ ├── hello.txt │ ├── index.html │ └── jquery.min.js ├── fifo ├── SimpleFifoTest.txt ├── client.c ├── client.h ├── server.c └── testfifo │ ├── client.c │ └── server.c ├── file_lock ├── flockfile.c ├── flockfile.txt ├── fwrite.txt ├── readlock.c ├── test.c └── writelock.c ├── gdb ├── HappyGDB-PartI-2008-01-26.pdf ├── HappyGDB-PartI-2008-07-12.pdf ├── HappyGDB-PartII-2008-07-12.pdf ├── frame_before_coredump │ ├── before_die.c │ ├── makefile │ ├── nm_test_arm │ └── test_corruption.c ├── gdb-rocks │ ├── ......--help..... │ ├── .K....y..c* │ ├── daynamic_change_value.c │ ├── dump1.c │ ├── hello-loop.c │ ├── ibm_divide_zero.c │ ├── jump.c │ ├── ls │ ├── set_var.c │ └── stack-backtrace.c ├── gdb-samples │ ├── Makefile │ ├── README_C_INTERPRETER │ ├── dpm.cc │ ├── fibonacci.c │ ├── gdb-samples-2008-01-26.tar │ ├── hello.c │ ├── input.c │ └── quicksort.c ├── jserv_ptrace │ ├── dbg-int3.c │ ├── hello-int3.c │ └── ptraceme.c ├── kent │ ├── Makefile │ ├── backtrace.c │ ├── doublefree.c │ ├── dump1-dm365.c │ ├── dump1.c │ ├── gdb.txt │ ├── ibm_divide_zero.c │ ├── max_crypt.c │ ├── memorylayout.c │ ├── ptr1.asm │ ├── ptr1.c │ ├── ptr_objdump-D │ ├── ptr_objdump-DS │ └── thread.c ├── sample1 │ ├── Makefile │ ├── dpm.cc │ ├── fibonacci.c │ ├── hello.c │ ├── input.c │ ├── quicksort.c │ └── samples1-2008-07-12.tar ├── sample2 │ ├── Makefile │ ├── clone-mem-file │ ├── fib.c │ ├── hello-loop.c │ ├── hello.c │ ├── prefix.c │ ├── ptype.c │ ├── sample.c │ ├── samples2-2008-07-12.tar │ ├── save-restore.c │ ├── search.c │ ├── set.c │ ├── set2.c │ ├── simple.c │ ├── stack-backtrace.c │ └── stack.c └── whocallme │ ├── Makefile │ ├── bfd.c │ ├── bfd_dumpfun.c │ ├── hello.c │ ├── test-1.c │ ├── test-2.c │ ├── test.c │ ├── whocallme.c │ ├── whocallme.h │ ├── whocallme.html │ └── whocallme.tar.bz2 ├── getsockopt.c ├── gnu-linux-programming ├── README.txt ├── daemon.c ├── detached.c ├── dweb-r3 │ ├── Makefile │ ├── http.c │ ├── http.h │ ├── socket.c │ └── socket.h ├── dweb │ ├── Makefile │ ├── dweb │ ├── http.c │ ├── http.h │ ├── socket.c │ └── socket.h ├── execvp.c ├── fork.c ├── getpriority.c ├── helo.c ├── ipc │ ├── client.c │ ├── mmap_read.c │ ├── mmap_write.c │ ├── pipe.c │ ├── pipe_dup.c │ ├── pipe_shell.c │ ├── server.c │ ├── shm_allocate.c │ ├── shm_read.c │ └── web.c ├── join_thread.c ├── join_thread2.c ├── join_thread3.c ├── loader.c ├── pid.c ├── sem.c ├── serial.c ├── serial.h ├── setpriority.c ├── shm_race │ ├── COPYING │ ├── Makefile │ ├── README │ ├── shm_allocate.c │ ├── shm_read.c │ └── shm_write.c ├── signal.c ├── thread.c ├── thread2.c ├── unix_spawn.c ├── wait.c └── zombie.c ├── ibm ├── connection-oriented-socket │ ├── client.c │ ├── readme │ └── server.c ├── connectionless-socket │ ├── client.c │ ├── readme │ └── server.c └── non-blocking_IO-and-select │ └── server.c ├── junk_0.c ├── junk_1.c ├── kernel.h ├── localtime.c ├── lockfile ├── C_Bash_Lock.c ├── a.c ├── b.c ├── filelist.c ├── fwrite.c ├── fwrite.txt ├── fwrite2.c ├── fwrite_.c ├── lockf.c └── lockf.txt ├── mac_addr.c ├── malloc_check_double_free.c ├── mem_alloc_test.c ├── mmap-samp.c ├── mount.cifs-ramax.c ├── mount.cifs-samba-3.0.27a.c ├── pclint ├── fopen_close.c ├── memaccess.c └── uninitialized_memory.c ├── popen.c ├── prime.c ├── pthread_cond.c ├── pthread_producer_consumer.c ├── raw_socket.c ├── readdevstdin_list_authors.c ├── ringbuffer ├── AlwaysKeepOneSlotOpen.c ├── OptimizedPOSIXImplementation.c └── README.txt ├── safe_strncpy.c ├── sched_rr_get_interval.c ├── segfault.c ├── set_get_priority ├── getpriority.c ├── setpriority.c └── testecho.sh ├── signalstack.c ├── sizeof.c ├── strstr.c ├── systemIgnore.c ├── tcpkeepalive.c ├── trace.c ├── trace.h ├── tstamp.c ├── vvtk-template ├── Makefile ├── common.h ├── file.c ├── function ├── header.h └── typedef.h ├── vvtk-topic3 ├── C_Basic_Homework │ ├── 01_Salary │ │ ├── .kdbgrc.salary │ │ ├── salary │ │ └── salary.c │ ├── 02_Prime │ │ └── Prime.c │ ├── 03_Power │ │ └── power.c │ ├── 04_RecursivePower │ │ └── RecursivePower.c │ ├── 05_CopyArray │ │ └── CopyArray.c │ ├── 06_CalculateArray │ │ ├── CalculateArray.c │ │ └── test.c │ ├── 07_ParseURL │ │ └── ParseURL.c │ ├── 08_LowerCase2UpperCase │ │ └── Capital.c │ ├── 09_SortName │ │ └── NameSorting.c │ ├── file.c │ └── header.h ├── Handle_Base_Programming │ ├── Snapshot │ ├── Snapshot_NoWork │ │ ├── GetSnapshot.c │ │ ├── Makefile │ │ ├── Thumbs.db │ │ ├── file.c │ │ ├── header.h │ │ ├── inc │ │ │ └── osisolate.h │ │ ├── video1.jpg │ │ ├── video2.jpg │ │ ├── video3.jpg │ │ ├── video4.jpg │ │ └── video5.jpg │ ├── example │ │ ├── Base.c │ │ ├── Derived.c │ │ ├── Internal.h │ │ ├── Internal.h.gch │ │ ├── Module.c │ │ ├── Module.h │ │ └── header.h │ └── example2 │ │ ├── example.c │ │ └── header.h ├── JPEG │ ├── Makefile │ ├── README │ ├── RefURL.txt │ ├── bin │ │ ├── Thumbs.db │ │ ├── sample.gif │ │ └── sample.jpg │ ├── jpegapp │ │ ├── Makefile │ │ ├── jpegapp.c │ │ └── jpeginfo.h │ ├── jpeglib │ │ ├── .exrc │ │ ├── FileToBinaryStream.c │ │ ├── Makefile │ │ ├── jpeginfo.h │ │ ├── jpeginfo_component_number.c │ │ └── jpeginfo_width_height.c │ └── samples │ │ ├── Thumbs.db │ │ ├── sample.gif │ │ └── sample.jpg └── Socket │ ├── 01_UDP │ ├── README │ ├── udp_exercise │ │ ├── udp_exercise_linux.c │ │ └── udp_exercise_win32.c │ └── udp_peer.c │ ├── 02_TCPServer │ └── TcpEchoServer.c │ ├── 03_TCPClient │ └── TcpEchoClient.c │ ├── 04_NonBlockingTCPServer │ ├── TcpEchoClient.c │ ├── TcpSelectServer.c │ ├── header.h │ ├── select.c │ ├── test.c │ └── test.sh │ ├── 04_NonBlockingTCPServer_2 │ ├── Makefile │ ├── Note │ ├── client │ ├── client.c │ ├── client.sleep10000 │ ├── server │ └── server.c │ ├── 05_TCPClientHasTimeout │ ├── TcpEchoClient.c │ ├── TcpNOEchoServer.c │ └── abc.c │ ├── 06_Broadcast │ ├── 07_BroadcastServerAndClient │ │ ├── BroadcastClient.c │ │ └── BroadcastServer.c │ ├── Receiver.c │ ├── Sender.c │ └── example │ │ ├── bclient.c │ │ ├── bserver.c │ │ └── readme │ ├── 07_Multicast │ ├── makefile │ ├── receive │ ├── receive.c │ ├── send │ └── send.c │ ├── 08_CheckPublicIP │ └── GetPubicIP.c │ ├── ProgramminLinuxSocket_Example │ ├── Makefile │ ├── TcpEchoClient.c │ ├── TcpEchoServer.c │ ├── TcpFetchSnapshot.c │ ├── UdpEchoClient.c │ ├── UdpEchoServer.c │ ├── nslookup.c │ ├── nslookup.py │ ├── test.sh │ ├── test1.sh │ └── video1.jpg │ ├── README.mkd │ ├── file.c │ └── header.h ├── writev ├── writev1.c └── writev2.c ├── xhide.c ├── yuv420-nv21.c └── yuv420sp2yuv420p.c /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "junkcode_by_Tridge@Samba"] 2 | path = junkcode_by_Tridge@Samba 3 | url = git@github.com:tridge/junkcode.git 4 | -------------------------------------------------------------------------------- /16x9_w16_h8.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, const char *argv[]) 5 | { 6 | int x = 0, y = 0; 7 | 8 | for (x = 176; x <= 1920; x = x + 16) 9 | { 10 | for (y = 144; y <= 1080; y = y + 8) 11 | { 12 | if (x * 9 == y * 16) 13 | { 14 | printf("%d x %d\n", x, y); 15 | } 16 | } 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /64bit/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -Wall test.S -m32 -o test 3 | -------------------------------------------------------------------------------- /64bit/test.S: -------------------------------------------------------------------------------- 1 | //http://stackoverflow.com/questions/2500362/running-32-bit-assembly-code-on-a-64-bit-linux-64-bit-processor-explain-the 2 | #include 3 | #include 4 | #define STDOUT 1 5 | 6 | .data 7 | hellostr: 8 | .ascii "hello wolrd\n" ; 9 | helloend: 10 | 11 | .text 12 | .globl main 13 | 14 | main: 15 | movl $(SYS_write) , %eax //ssize_t write(int fd, const void *buf, size_t count); 16 | movl $(STDOUT) , %ebx 17 | movl $hellostr , %ecx 18 | movl $(helloend-hellostr) , %edx 19 | int $0x80 20 | 21 | movl $(SYS_exit), %eax //void _exit(int status); 22 | xorl %ebx, %ebx 23 | int $0x80 24 | 25 | ret 26 | -------------------------------------------------------------------------------- /CIH/gcc-trace-func/g.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # gcc g.c -finstrument-functions -Wall -g -o g 3 | # addr2line 0x12345678 -f -e ./g 4 | 5 | gcc g.c -finstrument-functions -Wall -g -o g 6 | 7 | ./g 2> log.txt 8 | 9 | for s in `cat log.txt` ; do 10 | if echo $s | grep -q "func" ; then 11 | f=`echo $s | cut -d ":" -f2` 12 | t=`addr2line $f -f -e ./g` 13 | if echo $s | grep -q "parent" ; then 14 | echo -n "parent:" 15 | fi 16 | echo -n $t")" | tr " " "(" 17 | echo -n " " 18 | else 19 | echo -n $s 20 | if echo $s | grep -q "time:" ; then 21 | echo 22 | fi 23 | fi 24 | done 25 | 26 | rm -f log.txt 27 | 28 | -------------------------------------------------------------------------------- /CIH/my_kill/g.c: -------------------------------------------------------------------------------- 1 | /* 2 | CIH IamCIH@gmail.com 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int kill(pid_t pid, int sig) 11 | { 12 | void *h; 13 | static int (*my_kill)(pid_t, int) = 0; 14 | 15 | fprintf(stderr, "CIH kill ==> pid %i sig %i\n", pid, sig); 16 | 17 | if ( my_kill == 0 ) 18 | { 19 | h = dlopen("/lib/libc.so.6", RTLD_LAZY); 20 | my_kill = dlsym(h, "kill"); 21 | } 22 | 23 | return my_kill(pid, sig); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /CIH/my_kill/g.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | gcc g.c -Wall -shared -fPIC -ldl -o libg.so 4 | 5 | export LD_PRELOAD=`pwd`/libg.so 6 | /bin/kill 1234 7 | 8 | -------------------------------------------------------------------------------- /DPRINTF/DPRINTF_With__VA_ARGS__/README: -------------------------------------------------------------------------------- 1 | Reference http://www.jeffhung.net/blog/articles/jeffhung/1013/ 2 | -------------------------------------------------------------------------------- /DPRINTF/DPRINTF_Without__VA_ARGS__/README: -------------------------------------------------------------------------------- 1 | Reference: 2 | http://www.jeffhung.net/blog/articles/jeffhung/1012/ 3 | -------------------------------------------------------------------------------- /DPRINTF/DPRINTF_Without__VA_ARGS__/main_c99.c: -------------------------------------------------------------------------------- 1 | #include "dprintf.h" 2 | #include "thread_tool.h" 3 | #include 4 | 5 | int g_enable = 1; 6 | int g_data = 3; 7 | 8 | void* main_thread(void* param) 9 | { 10 | ENTER_FUNCTION(); 11 | thread_sleep(1); 12 | DPRINTF(g_enable, "g_data == %d", g_data); 13 | return 0; 14 | } 15 | 16 | void* other_thread(void* param) 17 | { 18 | ENTER_FUNCTION(); 19 | thread_sleep(2); 20 | DPRINTF(g_enable, "g_data == %d", g_data); 21 | return 0; 22 | } 23 | 24 | int main() 25 | { 26 | pthread_t other_thr; 27 | pthread_create(&other_thr, NULL, other_thread, NULL); 28 | main_thread(0); 29 | pthread_join(other_thr, NULL); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /DPRINTF/DPRINTF_Without__VA_ARGS__/thread_tool.c: -------------------------------------------------------------------------------- 1 | #include "thread_tool.h" 2 | #include 3 | #include 4 | 5 | void thread_sleep(size_t s) 6 | { 7 | fprintf(stderr, "thread#%d: sleep for %d seconds...\n", 8 | pthread_self(), s); 9 | struct timespec ts = {0}; 10 | ts.tv_sec = s; 11 | nanosleep(&ts, 0); 12 | } 13 | -------------------------------------------------------------------------------- /DPRINTF/DPRINTF_Without__VA_ARGS__/thread_tool.h: -------------------------------------------------------------------------------- 1 | #ifndef THREAD_TOOL_H_INCLUDED 2 | #define THREAD_TOOL_H_INCLUDED 3 | 4 | #include 5 | #include 6 | 7 | #if defined(__cplusplus) 8 | extern "C" { 9 | #endif 10 | 11 | #define ENTER_FUNCTION() \ 12 | fprintf(stderr, "thread#%d: %s(): entering...\n", \ 13 | pthread_self(), __FUNCTION__); \ 14 | /**/ 15 | 16 | /** Sleep @p s seconds for current thread. */ 17 | void thread_sleep(size_t s); 18 | 19 | #if defined(__cplusplus) 20 | } // extern "C" 21 | #endif 22 | 23 | #endif /* THREAD_TOOL_H_INCLUDED */ 24 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/EmbeddedLinuxJollen.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/EmbeddedLinuxJollen/EmbeddedLinuxJollen.tar.gz -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch02/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | printf("Hello, %s!\n", OS); 6 | } 7 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch02/hello2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | printf("Hello, %s!\n", OS); 6 | } 7 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch02/hello3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | #ifdef LINUX 6 | printf("Hello, LINUX!\n"); 7 | #endif 8 | printf("Hello, World.\n"); 9 | } 10 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch02/math.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | main() 5 | { 6 | printf("%f\n", log(1024)); 7 | } 8 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch02/pascal.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | int i, j; 6 | int c, c0, c1; 7 | 8 | c0 = 0; 9 | c1 = 1; 10 | 11 | for (i = 0; i < 5; i++) { 12 | for (j = 0; j < i; j++) { 13 | c = c0+c1; 14 | printf("%3d", c); 15 | c0 = c1; 16 | c1 = c; 17 | } 18 | printf("\n"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/daemon.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/EmbeddedLinuxJollen/ch04/daemon.c -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/detached.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void *print_hello(void *argu) 5 | { 6 | while (1) { 7 | printf("Hello,\n"); 8 | sleep(1); 9 | } 10 | } 11 | 12 | void *print_world(void *argu) 13 | { 14 | while (1) { 15 | printf("World!\n"); 16 | sleep(1); 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | pthread_attr_t attr; 23 | pthread_t thread_id1, thread_id2; 24 | 25 | /* Create detached threads. */ 26 | pthread_attr_init(&attr); 27 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 28 | 29 | pthread_create(&thread_id1, &attr, &print_hello, NULL); 30 | pthread_create(&thread_id2, &attr, &print_world, NULL); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/execvp.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/EmbeddedLinuxJollen/ch04/execvp.c -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | pid_t self, child; 8 | 9 | self = getpid(); 10 | child = fork(); 11 | 12 | if (child != 0) { 13 | printf("Parent process, PID: %d\n", (int) self); 14 | printf("Child process ID: %d\n", (int) child); 15 | } else { 16 | printf("This is the child process. PID: %d\n", (int) getpid()); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/join_thread2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void *my_lower(void *argu) 6 | { 7 | char x; 8 | 9 | x = *((char *)argu); 10 | x |= 0x20; 11 | 12 | return (void *)x; 13 | } 14 | 15 | int main() 16 | { 17 | char x = 'R'; 18 | pthread_t thread_id; 19 | 20 | pthread_create(&thread_id, NULL, &my_lower, &x); 21 | pthread_join(thread_id, (void *)&x); 22 | 23 | printf("Return Value: %c\n", x); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/module-1/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -c -O -W 3 | CFLAGS += -DMODULE -D__KERNEL__ 4 | 5 | module-1.o: module-1.c 6 | $(CC) $(CFLAGS) module-1.c 7 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/module-1/module-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int init_module(void) 4 | { 5 | printk("<1>Hello, World.\n"); 6 | 7 | return 0; 8 | } 9 | 10 | void cleanup_module(void) 11 | { 12 | printk("<1>Goodbye...\n"); 13 | } 14 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/module-1/module-1b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int init_module(void) 5 | { 6 | printk(KERN_ALERT "Hello, World.\n"); 7 | 8 | return 0; 9 | } 10 | 11 | void cleanup_module(void) 12 | { 13 | printk(KERN_ALERT "Goodbye...\n"); 14 | } 15 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/module-1/module-1c.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #ifdef CONFIG_SMP 6 | # define __SMP__ 7 | #endif 8 | 9 | int init_module(void) 10 | { 11 | printk(KERN_ALERT "Hello, World.\n"); 12 | 13 | return 0; 14 | } 15 | 16 | void cleanup_module(void) 17 | { 18 | printk(KERN_ALERT "Goodbye...\n"); 19 | } 20 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/module-2/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -c -O -W 3 | CFLAGS += -DMODULE -D__KERNEL__ 4 | 5 | module-2.o: module-2.c 6 | $(CC) $(CFLAGS) module-2.c 7 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/module-2/module-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #ifdef CONFIG_SMP 7 | # define __SMP__ 8 | #endif 9 | 10 | int my_init_module(void) 11 | { 12 | printk(KERN_ALERT "Hello, World.\n"); 13 | 14 | return 0; 15 | } 16 | 17 | void my_cleanup_module(void) 18 | { 19 | printk(KERN_ALERT "Goodbye...\n"); 20 | } 21 | 22 | module_init(my_init_module); 23 | module_exit(my_cleanup_module); 24 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/module-3/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -c -O -W 3 | CFLAGS += -DMODULE -D__KERNEL__ 4 | 5 | module-3.o: module-3.c 6 | $(CC) $(CFLAGS) module-3.c 7 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/module-3/module-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #ifdef CONFIG_SMP 7 | # define __SMP__ 8 | #endif 9 | 10 | int my_init_module(void) 11 | { 12 | printk(KERN_ALERT "Hello, World.\n"); 13 | 14 | return 0; 15 | } 16 | 17 | void my_cleanup_module(void) 18 | { 19 | printk(KERN_ALERT "Goodbye...\n"); 20 | } 21 | 22 | module_init(my_init_module); 23 | module_exit(my_cleanup_module); 24 | 25 | MODULE_LICENSE("GPL"); 26 | MODULE_AUTHOR("Jollen "); 27 | MODULE_DESCRIPTION("A Linux kernel module programming example"); 28 | MODULE_SUPPORTED_DEVICE("testdevice"); 29 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/pid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | pid_t self, parent; 8 | 9 | self = getpid(); 10 | parent = getppid(); 11 | 12 | printf("PID: %d, Parent PID: %d\n", (int) self, (int) parent); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/signal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void sighup_handler() 7 | { 8 | printf("Receive SIGHUP signal.\n"); 9 | } 10 | 11 | int main() 12 | { 13 | struct sigaction sa; 14 | 15 | memset(&sa, 0, sizeof(sa)); 16 | sa.sa_handler = &sighup_handler; 17 | sigaction(SIGHUP, &sa, NULL); 18 | 19 | while (1); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/thread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void *print_hello(void *argu) 5 | { 6 | while (1) { 7 | printf("Hello,\n"); 8 | sleep(1); 9 | } 10 | return NULL; 11 | } 12 | 13 | void *print_world(void *argu) 14 | { 15 | while (1) { 16 | printf("World!\n"); 17 | sleep(1); 18 | } 19 | return NULL; 20 | } 21 | 22 | int main() 23 | { 24 | pthread_t thread_id1, thread_id2; 25 | 26 | pthread_create(&thread_id1, NULL, &print_hello, NULL); 27 | pthread_create(&thread_id2, NULL, &print_world, NULL); 28 | 29 | while (1) { 30 | printf("-------------------------\n"); 31 | sleep(1); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/unix_spawn.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/EmbeddedLinuxJollen/ch04/unix_spawn.c -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/wait.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/EmbeddedLinuxJollen/ch04/wait.c -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch04/zombie.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | pid_t child; 8 | 9 | child = fork(); 10 | 11 | if (child != 0) { 12 | sleep(60); 13 | } else { 14 | return 0; 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch05/mmap_read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define FILE_LENGTH 0x400 7 | 8 | int main() 9 | { 10 | int fd; 11 | void *map_memory; 12 | char buf[FILE_LENGTH]; 13 | 14 | /* Open mapped file. */ 15 | fd = open("/tmp/shared_file", O_RDWR, S_IRUSR | S_IWUSR); 16 | 17 | /* Create mapped memory. */ 18 | map_memory = mmap(0, FILE_LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 19 | close(fd); 20 | 21 | /* Read from mapped memory. */ 22 | sscanf((char *)map_memory, "%s", buf); 23 | printf("Read: %s\n", buf); 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch05/pipe_shell.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | FILE *stream; 8 | int fds[2]; 9 | int i; 10 | pid_t child; 11 | char *arg_list[] = { "more", NULL }; 12 | 13 | /* create a pipe */ 14 | pipe(fds); 15 | 16 | /* fork a child process */ 17 | child = fork(); 18 | 19 | if (child != 0) { 20 | close(fds[0]); 21 | 22 | dup2(fds[1], 1); 23 | execlp("ls", "ls", "-l", "/lib", 0); 24 | 25 | waitpid(child, NULL, 0); 26 | } else { 27 | close(fds[1]); 28 | 29 | dup2(fds[0], 0); 30 | execvp("more", arg_list); 31 | } 32 | 33 | exit(0); 34 | } 35 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch05/shm_allocate.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int seg_id; 8 | int seg_size = 0x4000; 9 | char *shared_memory; 10 | 11 | /* shared memory: allocate */ 12 | seg_id = shmget(IPC_PRIVATE, seg_size, 13 | IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); 14 | printf("Shared Memory Segment ID: %d\n", seg_id); 15 | 16 | /* shared memory: attach */ 17 | shared_memory = (char *)shmat(seg_id, 0, 0); 18 | 19 | /* write to shared memory */ 20 | sprintf(shared_memory, argv[1]); 21 | 22 | /* shared memory: detach */ 23 | shmdt(shared_memory); 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch05/shm_read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int seg_id; 8 | char *shared_memory; 9 | 10 | /* get shmid */ 11 | seg_id = atoi(argv[1]); 12 | 13 | /* shared memory: attach */ 14 | shared_memory = (char *)shmat(seg_id, 0, 0); 15 | 16 | /* write to shared memory */ 17 | printf("Message of Shared Memory: %s\n", shared_memory); 18 | 19 | /* shared memory: detach */ 20 | shmdt(shared_memory); 21 | 22 | /* Deallocate shared memory */ 23 | shmctl(seg_id, IPC_RMID, 0); 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /EmbeddedLinuxJollen/ch07/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # JoTV - Video Streaming Systems 3 | # (c) 2001 Jollen 4 | # 5 | # Makefile for v4l library 6 | # 7 | 8 | CC = gcc 9 | CFLAGS = -mpentiumpro -O3 -fomit-frame-pointer -funroll-loops 10 | LIBS = 11 | 12 | %.o: %.c 13 | $(CC) $(CFLAGS) -Wall -c -o $@ $< 14 | 15 | all-recursive: all 16 | all: libv4l.a 17 | 18 | install: libv4l.a 19 | cp -f libv4l.a /usr/local/lib/ 20 | cp -f v4l.h /usr/include/ 21 | 22 | libv4l.a: v4l.o 23 | ar rc libv4l.a v4l.o 24 | 25 | v4l.o: v4l.c v4l.h 26 | 27 | clean: 28 | rm -f *.o libv4l.a 29 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming.tar.gz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/0_preface.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/0_preface.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/10_socket.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/10_socket.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/1_introduction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/1_introduction.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/2_tools.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/2_tools.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/3_file.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/3_file.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/4_standard_io.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/4_standard_io.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/5_information.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/5_information.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/6_process.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/6_process.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/7_signal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/7_signal.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/8_advance_io.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/8_advance_io.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/9_ipc.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/9_ipc.pdf -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/auto.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/auto.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap1.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap1.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap10.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap10.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap10.tgz.1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap10.tgz.1 -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap2.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap2.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap3.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap3.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap4.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap4.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap6.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap6.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap7.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap7.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap8.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap8.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chap9.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chap9.tgz -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-1/Makefile: -------------------------------------------------------------------------------- 1 | 2 | ls1: ls1.c error.c 3 | gcc -o ls1 error.c ls1.c 4 | 5 | clean: 6 | rm -rf ls1 *.o 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-1/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-1/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-1/ls1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "ourhdr.h" 4 | 5 | int 6 | main(int argc, char *argv[]) 7 | { 8 | DIR *dp; 9 | struct dirent *dirp; 10 | 11 | if (argc != 2) 12 | err_quit("a single argument (the directory name) is required"); 13 | 14 | if ( (dp = opendir(argv[1])) == NULL) 15 | err_sys("can't open %s", argv[1]); 16 | 17 | while ( (dirp = readdir(dp)) != NULL) 18 | printf("%s\n", dirp->d_name); 19 | 20 | closedir(dp); 21 | exit(0); 22 | } 23 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-2/Makefile: -------------------------------------------------------------------------------- 1 | TARGET=mycat getcputc 2 | CC=gcc 3 | 4 | all: $(TARGET) 5 | 6 | mycat: mycat.c error.c 7 | $(CC) -o mycat mycat.c error.c 8 | 9 | getcputc: getcputc.c error.c 10 | $(CC) -o getcputc getcputc.c error.c 11 | 12 | clean: 13 | rm -rf $(TARGET) *.o 14 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-2/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-2/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-2/getcputc.c: -------------------------------------------------------------------------------- 1 | #include "ourhdr.h" 2 | 3 | int 4 | main(void) 5 | { 6 | int c; 7 | 8 | while ( (c = getc(stdin)) != EOF) 9 | if (putc(c, stdout) == EOF) 10 | err_sys("output error"); 11 | 12 | if (ferror(stdin)) 13 | err_sys("input error"); 14 | 15 | exit(0); 16 | } 17 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-2/mycat.c: -------------------------------------------------------------------------------- 1 | #include "ourhdr.h" 2 | 3 | #define BUFFSIZE 8192 4 | 5 | int 6 | main(void) 7 | { 8 | int n; 9 | char buf[BUFFSIZE]; 10 | 11 | while ( (n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) 12 | if (write(STDOUT_FILENO, buf, n) != n) 13 | err_sys("write error"); 14 | 15 | if (n < 0) 16 | err_sys("read error"); 17 | 18 | exit(0); 19 | } 20 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-3/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | 3 | all: shell1 process 4 | 5 | shell1: shell1.c error.c 6 | $(CC) -o $@ $^ 7 | 8 | process: process.c 9 | $(CC) -o $@ $< 10 | 11 | clean: 12 | rm -rf shell1 process *.o 13 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-3/README.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-3/README.txt -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-3/process.c: -------------------------------------------------------------------------------- 1 | 2 | int main(void){ 3 | printf("hello world from process ID %d\n",getpid()); 4 | } 5 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-4/Makefile: -------------------------------------------------------------------------------- 1 | 2 | malloc: malloc.c 3 | $(CC) -o $@ $< 4 | 5 | clean: 6 | $(RM) malloc *.o 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-4/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-4/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-4/malloc.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-4/malloc.c -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-5/Makefile: -------------------------------------------------------------------------------- 1 | 2 | testerror: testerror.c 3 | $(CC) -o $@ $< 4 | 5 | clean: 6 | $(RM) testerror 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-5/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-5/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-5/testerror.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ourhdr.h" 3 | 4 | int 5 | main(int argc, char *argv[]) 6 | { 7 | fprintf(stderr, "EACCES: %s\n", strerror(EACCES)); 8 | 9 | errno = ENOENT; 10 | perror(argv[0]); 11 | 12 | exit(0); 13 | } 14 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-6/Makefile: -------------------------------------------------------------------------------- 1 | uidgid: uidgid.c 2 | $(CC) -o $@ $^ 3 | 4 | clean: 5 | rm uidgid 6 | 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-6/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-6/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-6/uidgid.c: -------------------------------------------------------------------------------- 1 | #include "ourhdr.h" 2 | 3 | int 4 | main(void) 5 | { 6 | printf("uid = %d, gid = %d\n", getuid(), getgid()); 7 | exit(0); 8 | } 9 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-7/Makefile: -------------------------------------------------------------------------------- 1 | 2 | shell2: shell2.c error.c 3 | $(CC) -o $@ $^ 4 | 5 | clean: 6 | rm shell2 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-7/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter1/1-7/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-1/Makefile: -------------------------------------------------------------------------------- 1 | BIN=server client 2 | 3 | .PHONY: clean 4 | 5 | 6 | all: $(BIN) 7 | 8 | %: %.c 9 | gcc -o $@ $^ 10 | 11 | clean: 12 | rm -rf $(BIN) 13 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-1/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | struct sockaddr_in server; 6 | int sock; 7 | char buf[]="TCP TEST"; 8 | 9 | bzero(&server,sizeof(server)); 10 | server.sin_family=PF_INET; 11 | server.sin_addr.s_addr=inet_addr("127.0.0.1"); 12 | server.sin_port=htons(6789); 13 | 14 | sock=socket(PF_INET,SOCK_STREAM,0); 15 | connect(sock,(struct sockaddr *)&server,sizeof(server)); 16 | 17 | write(sock,buf,sizeof(buf)); 18 | } 19 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-2/Makefile: -------------------------------------------------------------------------------- 1 | BIN=server client client2 client3 2 | 3 | .PHONY: clean 4 | 5 | all: $(BIN) 6 | 7 | %: %.c 8 | gcc -o $@ $^ 9 | 10 | clean: 11 | rm -rf $(BIN) 12 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-2/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | struct sockaddr_in server; 6 | int sock; 7 | char buf[]="UDP TEST"; 8 | 9 | bzero(&server,sizeof(server)); 10 | server.sin_family=PF_INET; 11 | server.sin_addr.s_addr=inet_addr("127.0.0.1"); 12 | server.sin_port=htons(5678); 13 | 14 | sock=socket(PF_INET,SOCK_DGRAM,0); 15 | 16 | sendto(sock,buf,sizeof(buf),0,(struct sockaddr *)&server,sizeof(server)); 17 | } 18 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-2/client2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | struct sockaddr_in server; 6 | int sock; 7 | char buf[]="UDP TEST"; 8 | 9 | bzero(&server,sizeof(server)); 10 | server.sin_family=PF_INET; 11 | server.sin_addr.s_addr=inet_addr("127.0.0.1"); 12 | server.sin_port=htons(5678); 13 | 14 | sock=socket(PF_INET,SOCK_DGRAM,0); 15 | 16 | connect(sock,(struct sockaddr *)&server,sizeof(server)); 17 | 18 | sendto(sock,buf,sizeof(buf),0,0,0); 19 | } 20 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-2/client3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | struct sockaddr_in server; 6 | int sock; 7 | char buf[]="UDP TEST"; 8 | 9 | bzero(&server,sizeof(server)); 10 | server.sin_family=PF_INET; 11 | server.sin_addr.s_addr=inet_addr("127.0.0.1"); 12 | server.sin_port=htons(5678); 13 | 14 | sock=socket(PF_INET,SOCK_DGRAM,0); 15 | 16 | connect(sock,(struct sockaddr *)&server,sizeof(server)); 17 | 18 | write(sock,buf,sizeof(buf)); 19 | } 20 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-2/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | struct sockaddr_in server,client; 6 | int sock,readSize,addressSize; 7 | char buf[256]; 8 | 9 | bzero(&server,sizeof(server)); 10 | server.sin_family=PF_INET; 11 | server.sin_addr.s_addr=inet_addr("127.0.0.1"); 12 | server.sin_port=htons(5678); 13 | 14 | sock=socket(PF_INET,SOCK_DGRAM,0); 15 | bind(sock,(struct sockaddr *)&server,sizeof(server)); 16 | 17 | addressSize=sizeof(client); 18 | readSize=recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr *)&client,&addressSize); 19 | buf[readSize]=0; 20 | printf("read message:%s\n",buf); 21 | } 22 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-3/Makefile: -------------------------------------------------------------------------------- 1 | 2 | name: name.c 3 | gcc -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-3/name.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | struct sockaddr_in addr,addr2; 6 | int sock,addressSize; 7 | 8 | sock=socket(PF_INET, SOCK_STREAM, 0); 9 | bzero(&addr,sizeof(addr)); 10 | addr.sin_family=PF_INET; 11 | //addr.sin_addr.s_addr=htonl(INADDR_ANY); 12 | addr.sin_addr.s_addr=inet_addr("192.168.77.2"); 13 | addr.sin_port=htons(5678); 14 | bind(sock,(struct sockaddr *)&addr,sizeof(addr)); 15 | 16 | addressSize=sizeof(addr2); 17 | getsockname(sock,(struct sockaddr *)&addr2, &addressSize); 18 | printf("sock name=%s port=%d\n",inet_ntoa(addr2.sin_addr.s_addr),ntohs(addr2.sin_port)); 19 | } 20 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-4/Makefile: -------------------------------------------------------------------------------- 1 | BIN=server client 2 | 3 | .PHONY: clean 4 | 5 | all: $(BIN) 6 | 7 | %: %.c 8 | gcc -o $@ $^ 9 | 10 | clean: 11 | rm -rf $(BIN) 12 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-4/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void){ 7 | struct sockaddr_un unix_addr; 8 | char buf[128]; 9 | int len,readSize; 10 | 11 | int sock=socket(PF_UNIX, SOCK_STREAM, 0); 12 | 13 | bzero(&unix_addr,sizeof(unix_addr)); 14 | unix_addr.sun_family=PF_UNIX; 15 | strcpy(unix_addr.sun_path,"stream"); 16 | len=sizeof(unix_addr.sun_family)+strlen(unix_addr.sun_path); 17 | 18 | if(connect(sock,(struct sockaddr *)&unix_addr,len)<0){ 19 | perror("connect failed:"); 20 | } 21 | 22 | readSize=read(sock,buf,sizeof(buf)); 23 | buf[readSize]=0; 24 | printf("%s\n",buf); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter10/10-5/Makefile: -------------------------------------------------------------------------------- 1 | BIN=server client 2 | 3 | .PHONY: clean 4 | 5 | all: $(BIN) 6 | 7 | %: %.c 8 | gcc -o $@ $^ 9 | 10 | clean: 11 | rm -rf $(BIN) 12 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-1/README.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-1/README.txt -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-1/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | printf("Hello world\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-2/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-2/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-2/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | printf("Hello world\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-2/hello.s: -------------------------------------------------------------------------------- 1 | .file "hello.c" 2 | .section .rodata 3 | .LC0: 4 | .string "Hello world\n" 5 | .text 6 | .align 2 7 | .globl main 8 | .type main,@function 9 | main: 10 | pushl %ebp 11 | movl %esp, %ebp 12 | subl $8, %esp 13 | andl $-16, %esp 14 | movl $0, %eax 15 | subl %eax, %esp 16 | subl $12, %esp 17 | pushl $.LC0 18 | call printf 19 | addl $16, %esp 20 | movl $0, %eax 21 | leave 22 | ret 23 | .Lfe1: 24 | .size main,.Lfe1-main 25 | .ident "GCC: (GNU) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)" 26 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-3/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-3/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-3/include/test.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-3/include/test.h -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-3/lib/testlib.c: -------------------------------------------------------------------------------- 1 | 2 | void test(){ 3 | printf("this is a library test\n"); 4 | } 5 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-3/test.c: -------------------------------------------------------------------------------- 1 | 2 | int main(void){ 3 | #ifdef HAVE_MMAP 4 | printf("HAVE_MMAP\n"); 5 | #else 6 | printf("DON't HAVE_MMAP\n"); 7 | #endif 8 | } 9 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-3/test2.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | int main(void){ 4 | #ifdef HAVE_MMAP 5 | printf("HAVE_MMAP\n"); 6 | #else 7 | printf("DON't HAVE_MMAP\n"); 8 | #endif 9 | } 10 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-3/test3.c: -------------------------------------------------------------------------------- 1 | 2 | int main(void){ 3 | test(); 4 | } 5 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-4/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc -pedantic 2 | 3 | TARGET= inline non_ansi non_ansi2 4 | 5 | all: $(TARGET) 6 | .PHONY: $(TARGET) 7 | 8 | $(TARGET): 9 | $(CC) -o $@ $@.c 10 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-4/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-4/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-4/inline.c: -------------------------------------------------------------------------------- 1 | 2 | inline void test(void){ 3 | printf("test\n"); 4 | } 5 | 6 | //inline test 7 | int main(void){ 8 | test(); 9 | } 10 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-4/non_ansi.c: -------------------------------------------------------------------------------- 1 | 2 | enum data{A,B,C,D,E}; 3 | int main(){ 4 | enum data var; 5 | var=C; 6 | switch(var){ 7 | case A...E: 8 | printf("%d\n",var); 9 | break; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-4/non_ansi2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | long long int i=0l; 5 | } 6 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-5/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-5/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-5/hello.c: -------------------------------------------------------------------------------- 1 | #include "hello.h" 2 | 3 | int main(void){ 4 | printf("hello\n"); 5 | } 6 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-5/hello.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-5/hello.h -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-6/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-6/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-6/warning.c: -------------------------------------------------------------------------------- 1 | 2 | int main(void){ 3 | int i,j; 4 | 5 | printf("%c\n","not a character"); /* -Wformat */ 6 | 7 | if(i=10) printf("oops\n"); /* -Wparentheses */ 8 | 9 | if(j!=10) printf("oops\n"); /* -Wuninitialized */ 10 | 11 | /* /* */ /* -Wcomment */ 12 | 13 | no_decl(); /* -Wmissing-declarations */ 14 | } 15 | 16 | int no_decl(void){ 17 | printf("no decl\n"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-7/README.TXT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-7/README.TXT -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-7/attribute.c: -------------------------------------------------------------------------------- 1 | void die_on_error(void) __attribute__ ((noreturn)); 2 | 3 | int var __attribute__ ((aligned (16)))=0; 4 | 5 | void die_on_error(void){ 6 | exit(-1); 7 | } 8 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-7/function.c: -------------------------------------------------------------------------------- 1 | 2 | int main(void){ 3 | printf("%s %s\n",__FILE__,__FUNCTION__); 4 | } 5 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-7/inline.c: -------------------------------------------------------------------------------- 1 | 2 | inline void swap(int *a,int *b){ 3 | int tmp=*a; 4 | *a=*b; 5 | *b=tmp; 6 | } 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter2/2-7/range.c: -------------------------------------------------------------------------------- 1 | 2 | void test(){ 3 | int var; 4 | switch(var){ 5 | case 0 ... 10: 6 | break; 7 | case 11 ... 20: 8 | break; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter3/3-1/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS := -Wall 2 | 3 | setext2: setext2.c 4 | 5 | .PHONY: clean dist 6 | 7 | clean: 8 | $(RM) setext2 *.o foo 9 | 10 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter4/4-1/getc.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | FILE *a[]={stdin}; 5 | FILE **p=a; 6 | getc(*p++); 7 | } 8 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter4/4-1/square.c: -------------------------------------------------------------------------------- 1 | #define square(x) (x * x) 2 | 3 | int main(void){ 4 | int i=0; 5 | printf("%d\n",square(++i)); 6 | } 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-1/Makefile: -------------------------------------------------------------------------------- 1 | 2 | doatexit: doatexit.c error.c 3 | gcc -o doatexit doatexit.c error.c 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-1/doatexit.c: -------------------------------------------------------------------------------- 1 | #include "ourhdr.h" 2 | void my_exit1(void), my_exit2(void); 3 | int 4 | main(void) 5 | { 6 | if (atexit(my_exit2) != 0) 7 | err_sys("can't register my_exit2"); 8 | if (atexit(my_exit1) != 0) 9 | err_sys("can't register my_exit1"); 10 | if (atexit(my_exit1) != 0) 11 | err_sys("can't register my_exit1"); 12 | printf("main is done\n"); 13 | return(0); 14 | } 15 | void 16 | my_exit1(void) 17 | { 18 | printf("first exit handler\n"); 19 | } 20 | void 21 | my_exit2(void) 22 | { 23 | printf("second exit handler\n"); 24 | } 25 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-10/Makefile: -------------------------------------------------------------------------------- 1 | thrdcancel: thrdcancel.c 2 | $(CC) $(CFLAGS) thrdcancel.c -o thrdcancel -lpthread 3 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-2/Makefile: -------------------------------------------------------------------------------- 1 | 2 | fork1: fork1.c error.c 3 | gcc -o fork1 fork1.c error.c 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-3/Makefile: -------------------------------------------------------------------------------- 1 | waiter: waiter.c 2 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-4/Makefile: -------------------------------------------------------------------------------- 1 | 2 | tellwait1: tellwait1.c error.c 3 | gcc -o tellwait1 tellwait1.c error.c 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-4/tellwait1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ourhdr.h" 3 | 4 | static void charatatime(char *); 5 | 6 | int 7 | main(void) 8 | { 9 | pid_t pid; 10 | 11 | if ( (pid = fork()) < 0) 12 | err_sys("fork error"); 13 | else if (pid == 0) { 14 | charatatime("output from child\n"); 15 | } else { 16 | charatatime("output from parent\n"); 17 | } 18 | exit(0); 19 | } 20 | 21 | static void 22 | charatatime(char *str) 23 | { 24 | char *ptr; 25 | int c; 26 | 27 | setbuf(stdout, NULL); /* set unbuffered */ 28 | for (ptr = str; c = *ptr++; ){ 29 | putc(c, stdout); 30 | sleep(1); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-5/Makefile: -------------------------------------------------------------------------------- 1 | 2 | exec1: exec1.c error.c 3 | gcc -o exec1 exec1.c error.c 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-5/exec1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "ourhdr.h" 4 | 5 | char *env_init[] = { "USER=unknown", "PATH=/tmp", NULL }; 6 | 7 | int 8 | main(void) 9 | { 10 | pid_t pid; 11 | 12 | if ( (pid = fork()) < 0) 13 | err_sys("fork error"); 14 | else if (pid == 0) { /* specify pathname, specify environment */ 15 | if (execle("/bin/ls", 16 | "ls", "/", 0, 17 | env_init) < 0) 18 | err_sys("execle error"); 19 | } 20 | if (waitpid(pid, NULL, 0) < 0) 21 | err_sys("wait error"); 22 | 23 | exit(0); 24 | } 25 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-6/Makefile: -------------------------------------------------------------------------------- 1 | 2 | systest3: systest3.c error.c 3 | gcc -o systest3 systest3.c error.c 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-6/systest3.c: -------------------------------------------------------------------------------- 1 | #include "ourhdr.h" 2 | 3 | int 4 | main(int argc, char *argv[]) 5 | { 6 | int status; 7 | 8 | if (argc < 2) 9 | err_quit("command-line argument required"); 10 | 11 | if ( (status = system(argv[1])) < 0) 12 | err_sys("system() error"); 13 | 14 | exit(0); 15 | } 16 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-7/Makefile: -------------------------------------------------------------------------------- 1 | resusg2: resusg2.c 2 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-8/Makefile: -------------------------------------------------------------------------------- 1 | resusg1: resusg1.c 2 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter6/6-9/Makefile: -------------------------------------------------------------------------------- 1 | thrdcreat: thrdcreat.c 2 | $(CC) $(CFLAGS) thrdcreat.c -o thrdcreat -lpthread 3 | 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter7/7-1/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: sigusr fkill 3 | 4 | sigusr: sigusr.c error.c 5 | gcc -o sigusr sigusr.c error.c 6 | fkill: fkill.c 7 | gcc -o fkill fkill.c 8 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter7/7-1/sigusr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ourhdr.h" 3 | 4 | static void sig_usr(int); /* one handler for both signals */ 5 | 6 | int 7 | main(void) 8 | { 9 | if (signal(SIGUSR1, sig_usr) == SIG_ERR) 10 | err_sys("can't catch SIGUSR1"); 11 | if (signal(SIGUSR2, sig_usr) == SIG_ERR) 12 | err_sys("can't catch SIGUSR2"); 13 | 14 | for ( ; ; ) 15 | pause(); 16 | } 17 | 18 | static void 19 | sig_usr(int signo) /* argument is signal number */ 20 | { 21 | if (signo == SIGUSR1) 22 | printf("received SIGUSR1\n"); 23 | else if (signo == SIGUSR2) 24 | printf("received SIGUSR2\n"); 25 | else 26 | err_dump("received signal %d\n", signo); 27 | return; 28 | } 29 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter7/7-2/Makefile: -------------------------------------------------------------------------------- 1 | 2 | mkset: mkset.c 3 | $(CC) -o $@ $^ 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter7/7-3/Makefile: -------------------------------------------------------------------------------- 1 | 2 | block: block.c 3 | $(CC) -o $@ $^ 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter7/7-4/Makefile: -------------------------------------------------------------------------------- 1 | 2 | pending: pending.c 3 | $(CC) -o $@ $^ 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter7/7-5/Makefile: -------------------------------------------------------------------------------- 1 | sigaction: sigaction.c 2 | $(CC) -o $@ $^ 3 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter7/7-5/sigaction.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static void sig_alrm(int); 4 | 5 | int 6 | main(void) 7 | { 8 | int n; 9 | char line[256]; 10 | 11 | struct sigaction sig; 12 | 13 | sig.sa_handler=sig_alrm; 14 | sigemptyset(&sig.sa_mask); 15 | //sig.sa_flags=0; 16 | sig.sa_flags=SA_RESTART; 17 | 18 | sigaction(SIGALRM,&sig,0); 19 | 20 | 21 | alarm(3); 22 | if ( (n = read(0, line, 256)) < 0) 23 | printf("error\n"); 24 | alarm(0); 25 | 26 | if(n>=0) write(1, line, n); 27 | 28 | exit(0); 29 | } 30 | 31 | static void 32 | sig_alrm(int signo) 33 | { 34 | printf("receive SIGALRM\n"); 35 | return; /* nothing to do, just return to interrupt the read */ 36 | } 37 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter7/7-6/Makefile: -------------------------------------------------------------------------------- 1 | 2 | pthread_signal: pthread_signal.c 3 | gcc -o pthread_signal pthread_signal.c -lpthread 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter8/8-1/Makefile: -------------------------------------------------------------------------------- 1 | 2 | nonblockw: nonblockw.c error.c clrfl.c setfl.c 3 | gcc -o nonblockw nonblockw.c error.c clrfl.c setfl.c 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter8/8-1/clrfl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ourhdr.h" 3 | 4 | void 5 | clr_fl(int fd, int flags) 6 | /* flags are the file status flags to turn off */ 7 | { 8 | int val; 9 | 10 | if ( (val = fcntl(fd, F_GETFL, 0)) < 0) 11 | err_sys("fcntl F_GETFL error"); 12 | 13 | val &= ~flags; /* turn flags off */ 14 | 15 | if (fcntl(fd, F_SETFL, val) < 0) 16 | err_sys("fcntl F_SETFL error"); 17 | } 18 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter8/8-1/setfl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ourhdr.h" 3 | 4 | void 5 | set_fl(int fd, int flags) /* flags are file status flags to turn on */ 6 | { 7 | int val; 8 | 9 | if ( (val = fcntl(fd, F_GETFL, 0)) < 0) 10 | err_sys("fcntl F_GETFL error"); 11 | 12 | val |= flags; /* turn on flags */ 13 | 14 | if (fcntl(fd, F_SETFL, val) < 0) 15 | err_sys("fcntl F_SETFL error"); 16 | } 17 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter8/8-2/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: lockfile 3 | 4 | %: %.c 5 | gcc -o $@ $^ 6 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter8/8-2/lockfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | extern int errno; 5 | 6 | int main(int argc,char *argv[]){ 7 | int fd,status; 8 | struct flock lock; 9 | 10 | fd=open("test.tmp", O_RDWR); 11 | printf("file descriptor:%d\n",fd); 12 | 13 | lock.l_type=F_WRLCK; 14 | lock.l_whence=SEEK_SET; 15 | lock.l_start=0; 16 | lock.l_len=0; //lock whole file 17 | 18 | status=fcntl(fd,F_SETLKW,&lock); 19 | printf("get lock\n"); 20 | while(1) sleep(60); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter8/8-2/test.tmp: -------------------------------------------------------------------------------- 1 | 123asdad12:wasdasd#include 2 | #include 3 | aaa 4 | int main(int argc,char *argv[]){ 5 | int fd; 6 | struct flock lock; 7 | 8 | fd=open("test.tmp", O_RDONLY); 9 | 10 | lock.l_whence=SEEK_SET; 11 | lock.l_start=0; 12 | lock.l_len=0; //lock whole file 13 | 14 | fcntl(fd,F_SETLKW,&lock); 15 | while(1) sleep(60); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter8/8-3/Makefile: -------------------------------------------------------------------------------- 1 | 2 | select: select.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter8/8-4/Makefile: -------------------------------------------------------------------------------- 1 | 2 | mmcat: mmcat.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-1/Makefile: -------------------------------------------------------------------------------- 1 | 2 | pipe: pipe.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-1/pipe.c: -------------------------------------------------------------------------------- 1 | 2 | int main(void){ 3 | int fd[2]; 4 | char buf[128]; 5 | 6 | pipe(fd); 7 | 8 | write(fd[1],"test\n\0",6); 9 | read(fd[0],buf,128); 10 | printf("%s",buf); 11 | } 12 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-10/Makefile: -------------------------------------------------------------------------------- 1 | 2 | msq: msq.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-10/msq.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | int qid; 7 | key_t key=0x12345; 8 | 9 | qid=msgget(key,IPC_CREAT|0666); 10 | printf("queue id:%d\n",qid); 11 | } 12 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-11/Makefile: -------------------------------------------------------------------------------- 1 | 2 | sendmsg: sendmsg.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-11/sendmsg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct msgbuf { 6 | long mtype; /* message type, must be > 0 */ 7 | int a,b; /* data */ 8 | }; 9 | 10 | int main(void) { 11 | int qid; 12 | key_t key=0x12345; 13 | struct msgbuf buf; 14 | 15 | qid=msgget(key,0); 16 | printf("[%s] qid = %d\n", __func__, qid); 17 | 18 | buf.mtype=1; buf.a=10; buf.b=20; 19 | msgsnd(qid,&buf,sizeof(buf)-sizeof(long),0); 20 | 21 | buf.mtype=9; buf.a=-10; buf.b=-20; 22 | msgsnd(qid,&buf,sizeof(buf)-sizeof(long),0); 23 | } 24 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-12/Makefile: -------------------------------------------------------------------------------- 1 | 2 | recvmsg: recvmsg.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-12/recvmsg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct msgbuf { 6 | long mtype; /* message type, must be > 0 */ 7 | int a,b; /* data */ 8 | }; 9 | 10 | int main(void){ 11 | int qid; 12 | key_t key=0x12345; 13 | struct msgbuf buf; 14 | 15 | qid=msgget(key,0); 16 | printf("[%s] qid = %d\n", __func__, qid); 17 | 18 | msgrcv(qid,&buf,sizeof(buf)-sizeof(long),9,0); 19 | printf("msg type:%d a=%d b=%d\n",buf.mtype,buf.a,buf.b); 20 | 21 | msgrcv(qid,&buf,sizeof(buf)-sizeof(long),0,0); 22 | printf("msg type:%d a=%d b=%d\n",buf.mtype,buf.a,buf.b); 23 | } 24 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-13/Makefile: -------------------------------------------------------------------------------- 1 | 2 | ctlmsg: ctlmsg.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-13/ctlmsg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | int qid; 7 | key_t key=0x12345; 8 | struct msqid_ds buf; 9 | 10 | qid=msgget(key,0); 11 | 12 | msgctl(qid,IPC_STAT,&buf); 13 | 14 | printf("uid:%d\n",buf.msg_perm.uid); 15 | printf("gid:%d\n",buf.msg_perm.gid); 16 | printf("cuid:%d\n",buf.msg_perm.cuid); 17 | printf("cgid:%d\n",buf.msg_perm.cgid); 18 | printf("mode:%o\n",buf.msg_perm.mode); 19 | printf("size:%d\n",buf.msg_qbytes); 20 | } 21 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-14/Makefile: -------------------------------------------------------------------------------- 1 | 2 | semaphore: semaphore.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-14/semaphore.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void){ 7 | int sid; 8 | FILE *fp; 9 | fp=fopen("/tmp/sid.tmp","w"); 10 | 11 | sid=semget(IPC_PRIVATE,1,0660); 12 | printf("semaphore id:%d\n",sid); 13 | 14 | fprintf(fp,"%d",sid); 15 | } 16 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-15/Makefile: -------------------------------------------------------------------------------- 1 | 2 | lock: lock.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-15/lock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int sid; 8 | struct sembuf lockop[2]={ 9 | 0, 0, 0, 10 | 0, 1, SEM_UNDO 11 | }; 12 | struct sembuf unlockop[1]={ 13 | 0, -1, IPC_NOWAIT 14 | }; 15 | 16 | void unlock(int arg){ 17 | printf("unlock\n"); 18 | semop(sid,unlockop,1); 19 | } 20 | 21 | int main(void){ 22 | FILE *fp; 23 | signal(SIGUSR1,unlock); 24 | 25 | fp=fopen("/tmp/sid.tmp","r"); 26 | fscanf(fp,"%d",&sid); 27 | printf("semaphore id:%d\n",sid); 28 | 29 | semop(sid,lockop,2); 30 | printf("get lock\n"); 31 | for(;;) sleep(60); 32 | } 33 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-16/Makefile: -------------------------------------------------------------------------------- 1 | 2 | del: del.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-16/del.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void){ 7 | int sid; 8 | FILE *fp; 9 | fp=fopen("/tmp/sid.tmp","r"); 10 | fscanf(fp,"%d",&sid); 11 | 12 | printf("semaphore id:%d\n",sid); 13 | 14 | semctl(sid,0,IPC_RMID); 15 | } 16 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-17/reader.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc,char *argv[]){ 6 | char *buf; 7 | key_t key=0x12345; 8 | int mid; 9 | 10 | mid=shmget(key,0,0); 11 | printf("shared memory id:%d\n",mid); 12 | 13 | buf=shmat(mid,0,0); 14 | printf("%s\n",buf); 15 | shmdt(buf); 16 | } 17 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-17/shared.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | key_t key=0x12345; 6 | int mid; 7 | 8 | mid=shmget(key,1024,IPC_CREAT|0660); 9 | } 10 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-17/writer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc,char *argv[]){ 6 | char *buf; 7 | key_t key=0x12345; 8 | int mid,len; 9 | 10 | mid=shmget(key,0,0); 11 | printf("shared memory id:%d\n",mid); 12 | 13 | buf=shmat(mid,0,0); 14 | len=strlen(argv[1]); 15 | strncpy(buf,argv[1],len); 16 | buf[len]=0; 17 | shmdt(buf); 18 | } 19 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-2/Makefile: -------------------------------------------------------------------------------- 1 | 2 | pipe: pipe.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-2/pipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | int fd[2]; 5 | int pid; 6 | 7 | pipe(fd); //create pipe 8 | 9 | if((pid=fork())==0){ //child 10 | int n; 11 | char buf[128]; 12 | 13 | close(fd[1]); 14 | n=read(fd[0],buf,128); 15 | write(0,buf,n); 16 | }else{ //parent 17 | close(fd[0]); 18 | write(fd[1],"hello world\n",12); 19 | wait(NULL); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-3/Makefile: -------------------------------------------------------------------------------- 1 | 2 | pipe: pipe.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-3/pipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int fd[2],pid; 6 | 7 | pipe(fd); //create pipe 8 | if((pid=fork())==0){ //child 9 | char buf[128]; 10 | 11 | close(fd[1]); 12 | dup2(fd[0],0); 13 | close(fd[0]); 14 | fgets(buf,128,stdin); 15 | printf("%s",buf); 16 | }else{ //parent 17 | close(fd[0]); 18 | dup2(fd[1],1); 19 | close(fd[1]); 20 | printf("hello world\n"); 21 | wait(NULL); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-4/Makefile: -------------------------------------------------------------------------------- 1 | 2 | pipe: pipe.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-4/pipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int fd[2],pid; 6 | 7 | pipe(fd); //create pipe 8 | if((pid=fork())==0){ //child 9 | char buf[128]; 10 | 11 | close(fd[1]); 12 | dup2(fd[0],0); 13 | close(fd[0]); 14 | fgets(buf,128,stdin); 15 | printf("%s",buf); 16 | }else{ //parent 17 | close(fd[0]); 18 | dup2(fd[1],1); 19 | close(fd[1]); 20 | setvbuf(stdout, NULL, _IOLBF, 0); 21 | printf("hello world\n"); 22 | wait(NULL); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-5/Makefile: -------------------------------------------------------------------------------- 1 | 2 | pipe: pipe.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-5/pipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int fd[2],pid; 6 | 7 | pipe(fd); //create pipe 8 | if((pid=fork())==0){ //child 9 | char buf[128]; 10 | 11 | close(fd[1]); 12 | dup2(fd[0],0); 13 | close(fd[0]); 14 | fgets(buf,128,stdin); 15 | printf("%s",buf); 16 | }else{ //parent 17 | close(fd[0]); 18 | dup2(fd[1],1); 19 | close(fd[1]); 20 | printf("hello world\n"); 21 | fflush(stdout); 22 | wait(NULL); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-6/Makefile: -------------------------------------------------------------------------------- 1 | 2 | pipe: pipe.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-6/pipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int fd[2],pid; 6 | 7 | pipe(fd); //create pipe 8 | if((pid=fork())==0){ //child 9 | char buf[128]; 10 | 11 | close(fd[1]); 12 | dup2(fd[0],0); 13 | close(fd[0]); 14 | fgets(buf,128,stdin); 15 | printf("%s",buf); 16 | }else{ //parent 17 | close(fd[0]); 18 | dup2(fd[1],1); 19 | close(fd[1]); 20 | printf("hello world\n"); 21 | close(1); 22 | wait(NULL); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-7/Makefile: -------------------------------------------------------------------------------- 1 | 2 | pipe: pipe.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-7/pipe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | int fd[2],pid; 6 | 7 | pipe(fd); //create pipe 8 | if((pid=fork())==0){ //child 9 | char buf[128]; 10 | 11 | close(fd[1]); 12 | dup2(fd[0],0); 13 | close(fd[0]); 14 | fgets(buf,128,stdin); 15 | printf("%s",buf); 16 | }else{ //parent 17 | close(fd[0]); 18 | dup2(fd[1],1); 19 | close(fd[1]); 20 | printf("hello world\n"); 21 | fclose(stdout); 22 | wait(NULL); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-8/Makefile: -------------------------------------------------------------------------------- 1 | 2 | popen: popen.c 3 | $(CC) -o $@ $< 4 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-8/popen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | FILE *fp; 5 | char buf[256]; 6 | int line=1; 7 | fp=popen("/bin/ls -a" ,"r"); 8 | while(fgets(buf,256,fp)!=NULL){ 9 | printf("%d: %s",line++,buf); 10 | } 11 | 12 | pclose(fp); 13 | } 14 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-9/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | 3 | all: creator reader writer 4 | 5 | %: %.c 6 | $(CC) -o $@ $^ 7 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-9/creator.c: -------------------------------------------------------------------------------- 1 | 2 | int main(void){ 3 | mkfifo("fifo",0660); 4 | } 5 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-9/reader.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | char buf[256]; 7 | int fd=open("fifo",O_RDONLY); 8 | read(fd,buf,256); 9 | printf("%s",buf); 10 | } 11 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/chapter9/9-9/writer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void){ 6 | int fd=open("fifo",O_WRONLY); 7 | write(fd,"fifo test\n",10); 8 | } 9 | -------------------------------------------------------------------------------- /ExampleLinuxProgramming/Example_Linux_programming/gdb.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/ExampleLinuxProgramming/Example_Linux_programming/gdb.tgz -------------------------------------------------------------------------------- /LearningTheBashShell2nd/bash2examples.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/LearningTheBashShell2nd/bash2examples.tar.gz -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/Manifest: -------------------------------------------------------------------------------- 1 | Manifest - This file 2 | Readme - Readme file 3 | chapter4 - Bash book examples from Chapter 4 4 | chapter5 - Bash book examples from Chapter 5 5 | chapter6 - Bash book examples from Chapter 6 6 | chapter7 - Bash book examples from Chapter 7 7 | chapter8 - Bash book examples from Chapter 8 8 | chapter9 - Bash book examples from Chapter 9 9 | debugger - Bash Shell Debugger 10 | ftl_language - A compiler for a small language, written in bash 11 | somesolutions - Solutions to some of the bash book problems 12 | 13 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter4/albums: -------------------------------------------------------------------------------- 1 | 5 Depeche Mode 2 | 2 Split Enz 3 | 3 Simple Minds 4 | 1 Vivaldi, Antonio 5 | 1 INXS 6 | 3 Guns and Roses 7 | 2 Roxy Music 8 | 2 Queen 9 | 1 Sisters of Mercy 10 | 1 Lloyd Cole and the Commotions 11 | 1 AC/DC 12 | 1 Madonna 13 | 1 Recoil 14 | 1 Australian Crawl 15 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter4/ascript: -------------------------------------------------------------------------------- 1 | 2 | function afunc 3 | { 4 | echo in function: $0 $1 $2 5 | var1="in function" 6 | echo var1: $var1 7 | } 8 | 9 | var1="outside function" 10 | echo var1: $var1 11 | echo $0: $1 $2 12 | afunc funcarg1 funcarg2 13 | echo var1: $var1 14 | echo $0: $1 $2 15 | 16 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter4/ascript1: -------------------------------------------------------------------------------- 1 | 2 | function afunc 3 | { 4 | local var1 5 | echo in function: $0 $1 $2 6 | var1="in function" 7 | echo var1: $var1 8 | } 9 | 10 | var1="outside function" 11 | echo var1: $var1 12 | echo $0: $1 $2 13 | afunc funcarg1 funcarg2 14 | echo var1: $var1 15 | echo $0: $1 $2 16 | 17 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter4/highest: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # highest filename [howmany] 4 | # 5 | # Print howmany highest-numbered lines in file filename. 6 | # The input file is assumed to have lines that start with 7 | # numbers. Default for howmany is 10. 8 | # 9 | 10 | filename=$1 11 | howmany=${2:-10} 12 | 13 | sort -nr $filename | head -$howmany 14 | 15 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter4/highest1: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # highest filename [howmany] 4 | # 5 | # Print howmany highest-numbered lines in file filename. 6 | # The input file is assumed to have lines that start with 7 | # numbers. Default for howmany is 10. 8 | # 9 | 10 | filename=$1 11 | filename=${filename:?"missing."} 12 | howmany=$2 13 | 14 | sort -nr $filename | head -${howmany:=10} 15 | 16 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter4/pushpop: -------------------------------------------------------------------------------- 1 | 2 | pushd () 3 | { 4 | dirname=$1 5 | DIR_STACK="$dirname ${DIR_STACK:-$PWD' '}" 6 | cd ${dirname:?"missing directory name."} 7 | echo "$DIR_STACK" 8 | } 9 | 10 | popd () 11 | { 12 | DIR_STACK=${DIR_STACK#* } 13 | cd ${DIR_STACK%% *} 14 | echo "$PWD" 15 | } 16 | 17 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/cd1: -------------------------------------------------------------------------------- 1 | 2 | cd () 3 | { 4 | builtin cd "$@" 5 | es=$? 6 | echo "$OLDPWD --> $PWD" 7 | return $es 8 | } 9 | 10 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/highest: -------------------------------------------------------------------------------- 1 | 2 | if [ -z "$1" ]; then 3 | echo 'usage: highest filename [-N]' 4 | else 5 | filename=$1 6 | howmany=${2:--10} 7 | sort -nr $filename | head $howmany 8 | fi 9 | 10 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/highest1: -------------------------------------------------------------------------------- 1 | 2 | if [ -z "$1" ]; then 3 | echo 'usage: highest filename [-N]' 4 | exit 1 5 | fi 6 | 7 | filename=$1 8 | howmany=${2:--10} 9 | sort -nr $filename | head $howmany 10 | 11 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/korncd: -------------------------------------------------------------------------------- 1 | 2 | cd() 3 | { 4 | case "$#" in 5 | 0 | 1) builtin cd $1 ;; 6 | 2 ) newdir=$(echo $PWD | sed -e "s:$1:$2:g") 7 | case "$newdir" in 8 | $PWD) echo "bash: cd: bad substitution" >&2 ; return 1 ;; 9 | * ) builtin cd "$newdir" ;; 10 | esac ;; 11 | * ) echo "bash: cd: wrong arg count" 1>&2 ; return 1 ;; 12 | esac 13 | } 14 | 15 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/listpath: -------------------------------------------------------------------------------- 1 | 2 | IFS=: 3 | 4 | for dir in $PATH 5 | do 6 | ls -ld $dir 7 | done 8 | 9 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/listpath1: -------------------------------------------------------------------------------- 1 | 2 | IFS=: 3 | 4 | for dir in $PATH; do 5 | if [ -z "$dir" ]; then dir=.; fi 6 | 7 | if ! [ -e "$dir" ]; then 8 | echo "$dir doesn't exist" 9 | elif ! [ -d "$dir" ]; then 10 | echo "$dir isn't a directory" 11 | else 12 | ls -ld $dir 13 | fi 14 | done 15 | 16 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/listpath2: -------------------------------------------------------------------------------- 1 | 2 | path=$PATH: 3 | 4 | while [ $path ]; do 5 | ls -ld ${path%%:*} 6 | path=${path#*:} 7 | done 8 | 9 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/popd1: -------------------------------------------------------------------------------- 1 | 2 | popd () 3 | { 4 | if [ -n "$DIR_STACK" ]; then 5 | DIR_STACK=${DIR_STACK#* } 6 | cd ${DIR_STACK%% *} 7 | echo "$PWD" 8 | else 9 | echo "stack empty, still in $PWD." 10 | fi 11 | } 12 | 13 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/procimage1: -------------------------------------------------------------------------------- 1 | 2 | for filename in "$@"; do 3 | ppmfile=${filename%.*}.ppm 4 | 5 | case $filename in 6 | *.gif ) exit 0 ;; 7 | 8 | *.tga ) tgatoppm $filename > $ppmfile ;; 9 | 10 | *.xpm ) xpmtoppm $filename > $ppmfile ;; 11 | 12 | *.pcx ) pcxtoppm $filename > $ppmfile ;; 13 | 14 | *.tif ) tifftopnm $filename > $ppmfile ;; 15 | 16 | *.jpg ) djpeg $filename > $ppmfile ;; 17 | 18 | * ) echo "procfile: $filename is an unknown graphics file." 19 | exit 1 ;; 20 | esac 21 | 22 | outfile=${ppmfile%.ppm}.new.gif 23 | 24 | ppmquant -quiet 256 $ppmfile | ppmtogif -quiet > $outfile 25 | rm $ppmfile 26 | 27 | done 28 | 29 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/pushd1: -------------------------------------------------------------------------------- 1 | 2 | pushd () 3 | { 4 | dirname=$1 5 | if cd ${dirname:?"missing directory name."} # if cd was successful 6 | then 7 | DIR_STACK="$dirname ${DIR_STACK:-$PWD' '}" # push the directory 8 | echo $DIR_STACK 9 | else 10 | echo still in $PWD. # else do nothing 11 | fi 12 | } 13 | 14 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/pushd2: -------------------------------------------------------------------------------- 1 | 2 | pushd () 3 | { 4 | dirname=$1 5 | if [ -n "$dirname" ] && [ \( -d "$dirname" \) -a \( -x "$dirname" \) ]; 6 | then 7 | DIR_STACK="$dirname ${DIR_STACK:-$PWD' '}" 8 | cd $dirname 9 | echo "$DIR_STACK" 10 | else 11 | echo "still in $PWD." 12 | fi 13 | } 14 | 15 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/recls: -------------------------------------------------------------------------------- 1 | 2 | recdir () 3 | { 4 | tab=$tab$singletab 5 | 6 | for file in "$@"; do 7 | echo -e $tab$file 8 | thisfile=$thisfile/$file 9 | 10 | if [ -d "$thisfile" ]; then 11 | recdir $(command ls $thisfile) 12 | fi 13 | 14 | thisfile=${thisfile%/*} 15 | done 16 | 17 | tab=${tab%\\t} 18 | } 19 | 20 | recls () 21 | { 22 | singletab="\\t" 23 | 24 | for tryfile in "$@"; do 25 | echo $tryfile 26 | if [ -d "$tryfile" ]; then 27 | thisfile=$tryfile 28 | recdir $(command ls $tryfile) 29 | fi 30 | done 31 | 32 | unset dir singletab tab 33 | } 34 | 35 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/selectd: -------------------------------------------------------------------------------- 1 | 2 | selectd () 3 | { 4 | PS3='directory? ' 5 | dirstack=" $DIR_STACK " 6 | 7 | select selection in $dirstack; do 8 | if [ $selection ]; then 9 | DIR_STACK="$selection${dirstack%% $selection *}" 10 | DIR_STACK="$DIR_STACK ${dirstack##* $selection }" 11 | DIR_STACK=${DIR_STACK% } 12 | cd $selection 13 | break 14 | else 15 | echo 'invalid selection.' 16 | fi 17 | done 18 | } 19 | 20 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/waitcp: -------------------------------------------------------------------------------- 1 | 2 | until cp $1 $2; do 3 | echo 'Attempt to copy failed. waiting...' 4 | sleep 5 5 | done 6 | 7 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter5/waitcp1: -------------------------------------------------------------------------------- 1 | 2 | while ! cp $1 $2; do 3 | echo 'Attempt to copy failed. waiting...' 4 | sleep 5 5 | done 6 | 7 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter6/accounts: -------------------------------------------------------------------------------- 1 | 2 | for i in $(cut -f 1,3 -d: /etc/passwd) ; do 3 | array[${i#*:}]=${i%:*} 4 | done 5 | 6 | echo "User ID $1 is ${array[$1]}." 7 | echo "There are currently ${#array[@]} user accounts on the system." 8 | 9 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter6/highest: -------------------------------------------------------------------------------- 1 | 2 | if [ -n "$(echo $1 | grep '^-[0-9][0-9]*$')" ]; then 3 | howmany=$1 4 | shift 5 | elif [ -n "$(echo $1 | grep '^-')" ]; then 6 | print 'usage: highest [-N] filename' 7 | exit 1 8 | else 9 | howmany="-10" 10 | fi 11 | 12 | filename=$1 13 | sort -nr $filename | head $howmany 14 | 15 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter6/ndu: -------------------------------------------------------------------------------- 1 | for dir in ${*:-.}; do 2 | if [ -e $dir ]; then 3 | result=$(du -s $dir | cut -f 1) 4 | let total=$result*1024 5 | 6 | echo -n "Total for $dir = $total bytes" 7 | 8 | if [ $total -ge 1048576 ]; then 9 | echo " ($((total/1048576)) Mb)" 10 | elif [ $total -ge 1024 ]; then 11 | echo " ($((total/1024)) Kb)" 12 | fi 13 | fi 14 | done 15 | 16 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter7/findterm: -------------------------------------------------------------------------------- 1 | 2 | findterm () { 3 | TERM=vt100 # assume this as a default 4 | line=$(tty) 5 | while read dev termtype; do 6 | if [ $dev = $line ]; then 7 | TERM=$termtype 8 | echo "TERM set to $TERM." 9 | break; 10 | fi 11 | done 12 | } 13 | 14 | findterm < /etc/terms 15 | 16 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter7/findterm1: -------------------------------------------------------------------------------- 1 | 2 | findterm () { 3 | TERM=vt100 # assume this as a default 4 | line=$(tty) 5 | while read dev termtype; do 6 | if [ $dev = $line ]; then 7 | TERM=$termtype 8 | echo "TERM set to $TERM." 9 | break; 10 | fi 11 | done 12 | } < /etc/terms 13 | 14 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter7/findterm2: -------------------------------------------------------------------------------- 1 | 2 | TERM=vt100 # assume this as a default 3 | line=$(tty) 4 | while read dev termtype; do 5 | if [ $dev = $line ]; then 6 | TERM=$termtype 7 | echo "TERM set to $TERM." 8 | break; 9 | fi 10 | done < /etc/terms 11 | 12 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter7/findterm3: -------------------------------------------------------------------------------- 1 | 2 | { 3 | TERM=vt100 # assume this as a default 4 | line=$(tty) 5 | while read dev termtype; do 6 | if [ $dev = $line ]; then 7 | TERM=$termtype 8 | echo "TERM set to $TERM." 9 | break; 10 | fi 11 | done 12 | } < /etc/terms 13 | 14 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter7/makecmd: -------------------------------------------------------------------------------- 1 | 2 | makecmd () 3 | { 4 | read target colon sources 5 | for src in $sources; do 6 | if [ $src -nt $target ]; then 7 | while read cmd && [ $(grep \t* $cmd) ]; do 8 | echo "$cmd" 9 | eval ${cmd#\t} 10 | done 11 | break 12 | fi 13 | done 14 | } 15 | 16 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter7/term: -------------------------------------------------------------------------------- 1 | 2 | TERM=vt100 # assume this as a default 3 | line=$(tty) 4 | while read dev termtype; do 5 | if [ $dev = $line ]; then 6 | TERM=$termtype 7 | echo "TERM set to $TERM." 8 | break 9 | fi 10 | done 11 | 12 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter8/crtlC: -------------------------------------------------------------------------------- 1 | 2 | settrap () { 3 | trap "echo 'You hit control-C!'" INT 4 | } 5 | 6 | settrap 7 | while true; do 8 | sleep 60 9 | done 10 | 11 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter8/ctrlC1: -------------------------------------------------------------------------------- 1 | 2 | loop () { 3 | trap "echo 'How dare you!'" INT 4 | while true; do 5 | sleep 60 6 | done 7 | } 8 | 9 | trap "echo 'You hit control-C!'" INT 10 | loop 11 | 12 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter8/ctrlC2: -------------------------------------------------------------------------------- 1 | 2 | loop () { 3 | trap "echo 'How dare you!'" INT 4 | } 5 | 6 | trap "echo 'You hit control-C!'" INT 7 | loop 8 | while true; do 9 | sleep 60 10 | done 11 | 12 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter8/hatter.mad: -------------------------------------------------------------------------------- 1 | 2 | { 3 | hatter=mad 4 | trap "echo 'You hit CTRL-C!'" INT 5 | } 6 | while true; do 7 | echo "\$hatter is $hatter" 8 | sleep 60 9 | done 10 | 11 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter8/hatter.mad1: -------------------------------------------------------------------------------- 1 | 2 | ( 3 | hatter=mad 4 | trap "echo 'You hit CTRL-C!'" INT 5 | ) 6 | while true; do 7 | echo "\$hatter is $hatter" 8 | sleep 60 9 | done 10 | 11 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter8/smail: -------------------------------------------------------------------------------- 1 | 2 | function cleanup { 3 | if [ -e $msgfile ]; then 4 | mv $msgfile dead.letter 5 | fi 6 | exit 7 | } 8 | 9 | trap cleanup INT TERM 10 | 11 | msgfile=/tmp/msg$$ 12 | cat > $msgfile 13 | # send the contents of $msgfile to the specified mail address... 14 | rm $msgfile 15 | 16 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/chapter9/guess: -------------------------------------------------------------------------------- 1 | 2 | trap 'echo Thank you for playing!' EXIT 3 | 4 | magicnum=$(($RANDOM%10+1)) 5 | echo 'Guess a number between 1 and 10:' 6 | while echo -n 'Guess: ' >&2 ; read guess ; do 7 | sleep 4 8 | if [ "$guess" = $magicnum ]; then 9 | echo 'Right!' 10 | exit 11 | fi 12 | echo 'Wrong!' 13 | done 14 | 15 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/debugger/bashdb: -------------------------------------------------------------------------------- 1 | 2 | # bashdb - a bash debugger 3 | # Driver Script: concatenates the preamble and the target script 4 | # and then executes the new script. 5 | 6 | echo 'Bash Debugger version 1.0' 7 | 8 | _dbname=${0##*/} 9 | 10 | if (( $# < 1 )) ; then 11 | echo "$_dbname: Usage: $_dbname filename" >&2 12 | exit 1 13 | fi 14 | 15 | _guineapig=$1 16 | 17 | if [ ! -r $1 ]; then 18 | echo "$_dbname: Cannot read file '$_guineapig'." >&2 19 | exit 1 20 | fi 21 | 22 | shift 23 | 24 | _tmpdir=/tmp 25 | _libdir=. 26 | _debugfile=$_tmpdir/bashdb.$$ # temporary file for script being debugged 27 | cat $_libdir/bashdb.pre $_guineapig > $_debugfile 28 | exec bash $_debugfile $_guineapig $_tmpdir $_libdir "$@" 29 | 30 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/debugger/bashdb.pre: -------------------------------------------------------------------------------- 1 | 2 | # bashdb preamble 3 | # This file gets prepended to the shell script being debugged. 4 | # Arguments: 5 | # $1 = the name of the original guinea pig script 6 | # $2 = the directory where temporary files are stored 7 | # $3 = the directory where bashdb.pre and bashdb.fns are stored 8 | 9 | _debugfile=$0 10 | _guineapig=$1 11 | _tmpdir=$2 12 | _libdir=$3 13 | 14 | shift 3 15 | 16 | source $_libdir/bashdb.fns 17 | _linebp=() 18 | let _trace=0 19 | let _i=1 20 | 21 | while read; do 22 | _lines[$_i]=$REPLY 23 | let _i=$_i+1 24 | done < $_guineapig 25 | 26 | trap _cleanup EXIT 27 | let _steps=1 28 | LINENO=-2 29 | trap '_steptrap $LINENO' DEBUG 30 | : 31 | -------------------------------------------------------------------------------- /LearningTheBashShell2nd/examples/ftl_language/ftla.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/LearningTheBashShell2nd/examples/ftl_language/ftla.tgz -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/LinuxDeviceDriver/examples.tar.gz -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/Makefile: -------------------------------------------------------------------------------- 1 | 2 | SUBDIRS = misc-progs misc-modules \ 3 | skull scull scullc sculld scullp scullv sbull snull\ 4 | short shortprint pci simple usb tty lddbus 5 | 6 | all: subdirs 7 | 8 | subdirs: 9 | for n in $(SUBDIRS); do $(MAKE) -C $$n || exit 1; done 10 | 11 | clean: 12 | for n in $(SUBDIRS); do $(MAKE) -C $$n clean; done 13 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/misc-modules/hello.c: -------------------------------------------------------------------------------- 1 | /* 2 | * $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $ 3 | */ 4 | #include 5 | #include 6 | MODULE_LICENSE("Dual BSD/GPL"); 7 | 8 | static int hello_init(void) 9 | { 10 | printk(KERN_ALERT "Hello, world\n"); 11 | return 0; 12 | } 13 | 14 | static void hello_exit(void) 15 | { 16 | printk(KERN_ALERT "Goodbye, cruel world\n"); 17 | } 18 | 19 | module_init(hello_init); 20 | module_exit(hello_exit); 21 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/misc-progs/Makefile: -------------------------------------------------------------------------------- 1 | 2 | FILES = nbtest load50 mapcmp polltest mapper setlevel setconsole inp outp \ 3 | datasize dataalign netifdebug 4 | 5 | KERNELDIR ?= /lib/modules/$(shell uname -r)/build 6 | INCLUDEDIR = $(KERNELDIR)/include 7 | CFLAGS = -O2 -fomit-frame-pointer -Wall -I$(INCLUDEDIR) 8 | 9 | all: $(FILES) 10 | 11 | clean: 12 | rm -f $(FILES) *~ core 13 | 14 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/misc-progs/gdbline: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # $Id: gdbline,v 1.1 2004/08/02 16:27:55 corbet Exp $ 4 | # 5 | # gdbline module image 6 | # 7 | # Outputs an add-symbol-file line suitable for pasting into gdb to examine 8 | # a loaded module. 9 | # 10 | cd /sys/module/$1/sections 11 | echo -n add-symbol-file $2 `/bin/cat .text` 12 | 13 | for section in .[a-z]* *; do 14 | if [ $section != ".text" ]; then 15 | echo " \\" 16 | echo -n " -s" $section `/bin/cat $section` 17 | fi 18 | done 19 | echo 20 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/pci/Makefile: -------------------------------------------------------------------------------- 1 | obj-m := pci_skel.o 2 | 3 | KERNELDIR ?= /lib/modules/$(shell uname -r)/build 4 | PWD := $(shell pwd) 5 | 6 | all: 7 | $(MAKE) -C $(KERNELDIR) M=$(PWD) 8 | 9 | clean: 10 | rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions 11 | 12 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/sbull/sbull_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="sbull" 3 | device="sbull" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # Remove stale nodes 9 | rm -f /dev/${device}[a-d]* /dev/${device} 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/scull/scull_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="scull" 3 | device="scull" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # Remove stale nodes 9 | 10 | rm -f /dev/${device} /dev/${device}[0-3] 11 | rm -f /dev/${device}priv 12 | rm -f /dev/${device}pipe /dev/${device}pipe[0-3] 13 | rm -f /dev/${device}single 14 | rm -f /dev/${device}uid 15 | rm -f /dev/${device}wuid 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/scullc/scullc_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="scullc" 3 | device="scullc" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # remove nodes 9 | rm -f /dev/${device}[0-3] /dev/${device} 10 | 11 | exit 0 12 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/sculld/sculld_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="sculld" 3 | device="sculld" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # remove nodes 9 | rm -f /dev/${device}[0-3] /dev/${device} 10 | 11 | exit 0 12 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/scullp/scullp_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="scullp" 3 | device="scullp" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # remove nodes 9 | rm -f /dev/${device}[0-3] /dev/${device} 10 | 11 | exit 0 12 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/scullv/scullv_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="scullv" 3 | device="scullv" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # remove nodes 9 | rm -f /dev/${device}[0-3] /dev/${device} 10 | 11 | exit 0 12 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/short/short_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="short" 3 | device="short" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # Remove stale nodes 9 | 10 | rm -f /dev/${device}[0-7] /dev/${device}[0-7][ps] \ 11 | /dev/${device}int /dev/${device}print 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/shortprint/Makefile: -------------------------------------------------------------------------------- 1 | # Comment/uncomment the following line to disable/enable debugging 2 | #DEBUG = y 3 | 4 | CFLAGS += -O2 -I.. 5 | 6 | ifneq ($(KERNELRELEASE),) 7 | # call from kernel build system 8 | 9 | obj-m := shortprint.o 10 | 11 | else 12 | 13 | KERNELDIR ?= /lib/modules/$(shell uname -r)/build 14 | PWD := $(shell pwd) 15 | 16 | default: 17 | $(MAKE) -C $(KERNELDIR) M=$(PWD) modules 18 | 19 | endif 20 | 21 | 22 | clean: 23 | rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions 24 | 25 | depend .depend dep: 26 | $(CC) $(CFLAGS) -M *.c > .depend 27 | 28 | 29 | ifeq (.depend,$(wildcard .depend)) 30 | include .depend 31 | endif 32 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/shortprint/shortprint_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="shortprint" 3 | device="shortprint" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # Remove stale nodes 9 | rm -f /dev/${device} 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/simple/simple_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | module="simple" 3 | device="simple" 4 | 5 | # invoke rmmod with all arguments we got 6 | /sbin/rmmod $module $* || exit 1 7 | 8 | # Remove stale nodes 9 | rm -f /dev/${device}[rn] 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/skull/Makefile: -------------------------------------------------------------------------------- 1 | foo: 2 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/skull/skull_clean.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | void skull_release(unsigned int port, unsigned int range) 8 | { 9 | release_region(port,range); 10 | } 11 | 12 | void skull_cleanup(void) 13 | { 14 | /* should put real values here ... */ 15 | /* skull_release(0,0); */ 16 | } 17 | 18 | module_exit(skull_cleanup); 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/snull/snull_load: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | export PATH=/sbin:/bin 4 | 5 | # Use a pathname, as new modutils don't look in the current dir by default 6 | insmod ./snull.ko $* 7 | ifconfig sn0 local0 8 | ifconfig sn1 local1 9 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/snull/snull_unload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | /sbin/ifconfig sn0 down 4 | /sbin/ifconfig sn1 down 5 | /sbin/rmmod snull 6 | -------------------------------------------------------------------------------- /LinuxDeviceDriver/examples/usb/Makefile: -------------------------------------------------------------------------------- 1 | obj-m := usb-skeleton.o 2 | 3 | KERNELDIR ?= /lib/modules/$(shell uname -r)/build 4 | PWD := $(shell pwd) 5 | 6 | all: 7 | $(MAKE) -C $(KERNELDIR) M=$(PWD) 8 | 9 | clean: 10 | rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions 11 | 12 | -------------------------------------------------------------------------------- /LinuxServerHacks/LSHcode.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/LinuxServerHacks/LSHcode.zip -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch01/Makefile.mail: -------------------------------------------------------------------------------- 1 | M4= m4 2 | CFDIR= /usr/src/sendmail-8.12.5/cf 3 | CHMOD= chmod 4 | ROMODE= 444 5 | RM= rm -f 6 | 7 | .SUFFIXES: .mc .cf 8 | 9 | all: virtusers.db aliases.db access.db 10 | 11 | access.db: access.txt 12 | makemap -v hash access < access.txt 13 | 14 | aliases.db: aliases 15 | newaliases 16 | 17 | virtusers.db: virtusers.txt 18 | makemap -v hash virtusers < virtusers.txt 19 | 20 | .mc.cf: 21 | $(RM) $@ 22 | $(M4) ${CFDIR}/m4/cf.m4 $*.mc > $@ || ( $(RM) $@ && exit 1 ) 23 | $(CHMOD) $(ROMODE) $@ 24 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch01/Makefile.push: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile to push *.conf to the slave, as needed. 3 | # 4 | SLAVE= www2.oreillynet.com 5 | APACHE= /usr/local/apache 6 | RM= /bin/rm 7 | TOUCH= /bin/touch 8 | SSH= /usr/local/bin/ssh 9 | SCP= /usr/local/bin/scp 10 | 11 | .SUFFIXES: .conf .ts 12 | 13 | all: test restart sites.ts globals.ts httpd.ts 14 | 15 | configtest: test 16 | 17 | test: 18 | @echo -n "Testing Apache configuration: " 19 | @$(APACHE)/bin/apachectl configtest 20 | 21 | restart: 22 | $(APACHE)/bin/apachectl restart 23 | 24 | .conf.ts: 25 | @$(RM) -f $@ 26 | @$(SCP) $*.conf $(SLAVE):$(APACHE)/conf 27 | @$(SSH) $(SLAVE) $(APACHE)/bin/apachectl restart 28 | @$(TOUCH) $@ 29 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch01/albumize: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | if [ -z "$ALBUM" ]; then 3 | echo 'You must set the ALBUM name first (eg. export ALBUM="Greatest Hits")' 4 | exit 1 5 | fi 6 | for x in *; do 7 | echo -n $x; echo -ne '\000' 8 | echo -n `echo $x|cut -f 1 -d '-'` 9 | echo -n " - $ALBUM - " 10 | echo -n `echo $x|cut -f 2- -d '-'`; echo -ne '\000' 11 | done | xargs -0 -n2 mv 12 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch01/dealbumize: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | for x in *; do 3 | echo -n $x; echo -ne '\000' 4 | echo -n `echo $x|cut -f 1 -d '-'`; echo -n ' - ' 5 | echo -n `echo $x|cut -f 3- -d '-'`; echo -ne '\000' 6 | done | xargs -0 -n2 mv 7 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch03/balance.back: -------------------------------------------------------------------------------- 1 | ### Stuff we definitely don't want to mirror. 2 | # 3 | - logs/ 4 | - *.tmp 5 | - *.swp 6 | ### the entire document root 7 | # 8 | + /usr/ 9 | + /usr/local/ 10 | + /usr/local/apache/ 11 | + /usr/local/apache/htdocs/ 12 | + /usr/local/apache/htdocs/** 13 | # Exclude everything else. 14 | # 15 | - * 16 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch03/balance.front: -------------------------------------------------------------------------------- 1 | ### Stuff we definitely don't want to mirror. 2 | # 3 | - logs/ 4 | ### the entire document root 5 | # 6 | + /usr/ 7 | + /usr/local/ 8 | + /usr/local/apache/ 9 | + /usr/local/apache/htdocs/ 10 | + /usr/local/apache/htdocs/** 11 | ### user public_html directories 12 | # 13 | + /home/ 14 | + /home/*/ 15 | + /home/*/public_html/ 16 | + /home/*/public_html/** 17 | # Images, archives, etc. 18 | # 19 | + *.jpg 20 | + *.gif 21 | + *.png 22 | + *.pdf 23 | + *.mp3 24 | + *.zip 25 | + *.tgz 26 | + *.gz 27 | # Exclude everything else. 28 | # 29 | - * 30 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch04/vtund.conf-client: -------------------------------------------------------------------------------- 1 | options { 2 | port 5000; 3 | ifconfig /sbin/ifconfig; 4 | route /sbin/route; 5 | } 6 | 7 | default { 8 | compress no; 9 | speed 0; 10 | } 11 | 12 | home { 13 | type tun; 14 | proto tcp; 15 | keepalive yes; 16 | 17 | pass sHHH; # Password is REQUIRED. 18 | 19 | up { 20 | ifconfig "%% 208.201.239.33 pointopoint 208.201.239.32 arp"; 21 | 22 | route "add 208.201.239.5 gw 10.42.3.1"; 23 | route "del default"; 24 | route "add default gw 208.201.239.32"; 25 | 26 | }; 27 | 28 | down { 29 | route "del default"; 30 | route "del 208.201.239.5 gw 10.42.3.1"; 31 | route "add default gw 10.42.3.1"; 32 | }; 33 | } 34 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch05/movein.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -z "$1" ]; then 4 | echo "Usage: `basename $0` hostname" 5 | exit 6 | fi 7 | 8 | cd ~/.skel 9 | tar zhcf - . | ssh $1 "tar zpvxf -" 10 | 11 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch05/takeover: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | OTHER="brain" 3 | PUBLIC="208.201.239.37" 4 | 5 | PAUSE=3 6 | 7 | PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/sbin 8 | MISSED=0 9 | 10 | while true; do 11 | if ! ping -c 1 -w 1 $OTHER > /dev/null; then 12 | ((MISSED++)) 13 | else 14 | if [ $MISSED -gt 2 ]; then 15 | ifconfig eth0:$OTHER down 16 | fi 17 | MISSED=0 18 | fi; 19 | 20 | if [ $MISSED -eq 2 ]; then 21 | ifconfig eth0:$OTHER $PUBLIC 22 | # 23 | # ...but see discussion below... 24 | # 25 | fi 26 | sleep $PAUSE; 27 | done 28 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch05/tl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | use strict; 4 | $|++; 5 | 6 | my $host=`/bin/hostname`; 7 | chomp $host; 8 | 9 | while(1) { 10 | 11 | open(LOAD,"/proc/loadavg") || die "Couldn't open /proc/loadavg: $!\n"; 12 | 13 | my @load=split(/ /,); 14 | close(LOAD); 15 | 16 | print "\033]0;"; 17 | print "$host: $load[0] $load[1] $load[2] at ", scalar(localtime); 18 | print "\007"; 19 | 20 | sleep 2; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch06/profile: -------------------------------------------------------------------------------- 1 | if [ -f ~/.agent.env ]; then 2 | . ~/.agent.env -s > /dev/null 3 | 4 | if ! kill -0 $SSH_AGENT_PID > /dev/null 2>&1; then 5 | echo 6 | echo "Stale agent file found. Spawning new agent..." 7 | eval `ssh-agent -s | tee ~/.agent.env` 8 | ssh-add 9 | fi 10 | else 11 | echo "Starting ssh-agent..." 12 | eval `ssh-agent -s | tee ~/.agent.env` 13 | ssh-add 14 | fi 15 | 16 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch06/ssh-to: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # What's the -AC for? Forward the Agent, and do Compression. 4 | # 5 | ssh -AC `basename $0` $* 6 | -------------------------------------------------------------------------------- /LinuxServerHacks/LinuxServerHacks-code/ch07/mince: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -z "$2" -o ! -r "$1" ]; then 4 | echo "Usage: mince [file] [chunk size]" 5 | exit 255 6 | fi 7 | 8 | SIZE=`ls -l $1 | tr -s 'a-zA-Z -' ' ' | cut -f 3 -d ' '` 9 | echo "size = $SIZE" 10 | 11 | if [ $2 -gt $SIZE ]; then 12 | echo "Your chunk size must be smaller than the file size!" 13 | exit 254 14 | fi 15 | 16 | CHUNK=$2 17 | TOTAL=0 18 | PASS=0 19 | while [ $TOTAL -lt $SIZE ]; do 20 | PASS=$((PASS + 1)) 21 | echo "Creating $1.$PASS..." 22 | dd conv=noerror if=$1 of=$1.$PASS bs=$CHUNK skip=$((PASS - 1)) count=1 2> /dev/null 23 | TOTAL=$((TOTAL + CHUNK)) 24 | done 25 | 26 | echo "Created $PASS chunks out of $1." 27 | 28 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #Simple Makefile for JunkCode 2 | EXCEPT_FILE = 'chfd_linux|readdevstdin_list_authors|trace' 3 | SINGLE_FILE_JUNKCODE = $(shell find . -maxdepth 1 -iname "*.c" -exec basename {} .c \; | grep -v -E $(EXCEPT_FILE)) 4 | 5 | all: 6 | @echo ======== make junk code under this folder =========== 7 | make $(SINGLE_FILE_JUNKCODE) 8 | 9 | clean: 10 | rm $(SINGLE_FILE_JUNKCODE) 11 | find . -iname "*.out" -exec rm '{}' \; 12 | find . -iname "*.o" -exec rm '{}' \; 13 | -------------------------------------------------------------------------------- /README.mkd: -------------------------------------------------------------------------------- 1 | ABOUT 2 | ===== 3 | 4 | junk code / sample code / misc code 5 | 6 | Usage 7 | ===== 8 | 9 | 1. Check out from github 10 | 11 | git clone git://github.com/chenkaie/junkcode.git ~/Junkcode 12 | cd ~/Junkcode 13 | git submodule update --init 14 | 15 | AUTHOR 16 | ====== 17 | 18 | Kent Chen (chenkaie) 19 | 20 | * [@chenkaie](https://twitter.com/chenkaie) on Twitter 21 | * [@chenkaie](https://github.com/chenkaie) on GitHub 22 | * [@chenkaie](http://www.slideshare.net/chenkaie) on SlideShare 23 | * [@chenkaie](http://www.linkedin.com/in/chenkaie) on LinkedIn 24 | * [@chenkaie](http://www.plurk.com/chenkaie) on Plurk 25 | * 26 | 27 | -------------------------------------------------------------------------------- /arm/arm_char.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, const char *argv[]) 4 | { 5 | unsigned char c = -1; 6 | assert(c == -1); // This assertion fails. Yikes. 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /arm/typedef_int.c: -------------------------------------------------------------------------------- 1 | #include 2 | int test (short *data); 3 | 4 | int test (short *data) 5 | { 6 | unsigned int idx; 7 | int sum = 0; 8 | for (idx = 0; idx < 64; idx++) { 9 | sum += *(data++); 10 | } 11 | return sum; 12 | } 13 | 14 | int main(int argc, const char *argv[]) 15 | { 16 | /*unsigned int buff[32]; 17 | buff[0] = 0xFF; 18 | buff[1] = 0x04; 19 | buff[2] = 0x01; 20 | 21 | fprintf(stderr, "[%s] buf + %d\n", __FUNCTION__, buff[0]); 22 | fprintf(stderr, "[%s] buf + %d\n", __FUNCTION__, buff[1]); 23 | fprintf(stderr, "[%s] buf + %d\n", __FUNCTION__, buff[2]);*/ 24 | 25 | short x = 1; 26 | test(&x); 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /arm/typedef_short.c: -------------------------------------------------------------------------------- 1 | short test (int *data){ 2 | char idx; 3 | int sum = 0; 4 | for (idx = 0; idx < 64; idx++) { 5 | sum += data[idx]; 6 | } 7 | return sum; 8 | } 9 | -------------------------------------------------------------------------------- /array_size.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define NUM_DAYS 5 4 | typedef int days_t[ NUM_DAYS ]; 5 | #define SIZEOF_DAYS ( sizeof( days_t ) ) 6 | 7 | int main() { 8 | days_t days; 9 | days_t *ptr = &days; 10 | 11 | printf( "SIZEOF_DAYS: %u\n", SIZEOF_DAYS ); 12 | printf( "sizeof(days): %u\n", sizeof(days) ); 13 | printf( "sizeof(*ptr): %u\n", sizeof(*ptr) ); 14 | printf( "sizeof(ptr): %u\n", sizeof(ptr) ); 15 | printf( "sizeof(int): %u\n", sizeof(int) ); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /asm/tmp.c: -------------------------------------------------------------------------------- 1 | #include 2 | int tmp; 3 | int main(void) 4 | { 5 | int i; 6 | printf("tmp is originally %d\n",tmp); 7 | for(i=0;i<20;i++) 8 | { 9 | asm ( 10 | "cmpl $10,%ebx\n" 11 | "jne dont\n" 12 | "movl %ebx,tmp\n" 13 | "dont:\n" 14 | ); 15 | printf("%d\n",i); 16 | printf("\nnow tmp is %d\n",tmp); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /asm_nop.c: -------------------------------------------------------------------------------- 1 | //arm-linux-gcc testasm.c -S 2 | xxx(int a1, int a2, int a3, int a4, int a5, int a6) 3 | { 4 | } 5 | 6 | main() 7 | { 8 | asm("nop"); 9 | asm("nop"); 10 | asm("nop"); 11 | /****************/ 12 | /* 用夾擊法 */ 13 | xxx(1,2,3,4,5,6); 14 | /****************/ 15 | asm("nop"); 16 | asm("nop"); 17 | asm("nop"); 18 | } 19 | -------------------------------------------------------------------------------- /bash/Fuser.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | Waiting_For_Recorder_Update_Journal() 3 | { 4 | result=`ls -l /proc/${1}/fd | awk '{print $11}' | grep ${2}` 5 | while [ -n "${result}" ] 6 | do 7 | sleep 1 8 | result=`ls -l /proc/${1}/fd | awk '{print $11}' | grep ${2}` 9 | done 10 | } 11 | 12 | Waiting_For_Recorder_Update_Journal 6413 fwrite 13 | -------------------------------------------------------------------------------- /bash/audioin_sync_to_gain: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | RANGE=`expr $3 - $2 + 1` 4 | VAL=`expr $1 \* $RANGE / 100` 5 | #echo VAL=$VAL >> test 6 | /usr/sbin/confclient -s "cmdopt=2&audioin_c0_gain=$VAL" -p 7 & 7 | 8 | -------------------------------------------------------------------------------- /bash/audioin_sync_to_gainp: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | RANGE=`expr $3 - $2 + 1` 4 | PER=`expr $1 \* 100 / $RANGE` 5 | #echo PER=$PER >> test 6 | /usr/sbin/confclient -s "cmdopt=0&audioin_c0_gainp=$PER" -p 7 & 7 | 8 | -------------------------------------------------------------------------------- /bash/calc_interrupts.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | x=`cat /proc/interrupts | grep capture0 | awk '{print $2}'`; 4 | usleep 900000; 5 | y=`cat /proc/interrupts | grep capture0 | awk '{print $2}'`; 6 | expr $y - $x 7 | -------------------------------------------------------------------------------- /bash/chrt.sh: -------------------------------------------------------------------------------- 1 | mount -t nfs -o tcp,nolock 172.16.5.32:/home /home 2 | for x in `ps |grep venc | awk '{print $1}'`; do /home/kent.chen/Project/PlatMozart3/busybox-1.12.4_VVTK/chrt -o -p 0 $x; done 3 | 4 | -------------------------------------------------------------------------------- /bash/cp_and_rename.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #Bash One Liner: copy template_*.txt to foo_*.txt? 3 | 4 | for f in template_*.txt; do cp $f foo_${f#template_}; done 5 | -------------------------------------------------------------------------------- /bash/cpp_in_bash.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #define cpp # 3 | cpp $0 2> /dev/null | /bin/bash; exit $? 4 | #undef cpp 5 | 6 | #define HELLO_WORLD echo "hello, world" 7 | HELLO_WORLD 8 | 9 | #ifndef _OV7725 10 | echo "NotOV7725"; 11 | #else 12 | echo "OV7725"; 13 | #endif 14 | 15 | -------------------------------------------------------------------------------- /bash/dialog.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Message box 4 | dialog --title "Hello" --msgbox "Bharathi" 20 40 5 | # Input Box. 6 | dialog --title "Input Box" --inputbox "My Name" 10 20 Bharathi 7 | # Calendar 8 | dialog --title "CAL" --calendar "Set Date" 0 0 22 8 2005 9 | # YesNo box. 10 | dialog --title "Yes/No" --yesno "Say yes or no" 15 61 11 | # File Select Box. 12 | dialog --title "Please choose a file" --fselect $HOME/ 20 40 13 | # Time Box. 14 | dialog --title "TIMEBOX" --timebox "Set Time" 0 0 12 34 56 15 | 16 | -------------------------------------------------------------------------------- /bash/exec_fork_source/1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | A=B 3 | echo "PID for 1.sh before exec/source/fork:$$" 4 | export A 5 | case $1 in 6 | exec) 7 | echo "using exec..." 8 | exec ./2.sh ;; 9 | source) 10 | echo "using source..." 11 | . ./2.sh ;; 12 | *) 13 | echo "using fork by default..." 14 | ./2.sh ;; 15 | esac 16 | echo "PID for 1.sh after exec/source/fork:$$" 17 | echo "1.sh: $A" 18 | -------------------------------------------------------------------------------- /bash/exec_fork_source/2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "PID for 2.sh: $$" 3 | echo "2.sh get $A from 1.sh" 4 | A=C 5 | export A 6 | echo "2.sh: $A" 7 | -------------------------------------------------------------------------------- /bash/finddate.sh: -------------------------------------------------------------------------------- 1 | TODAY=`date +%Y%m%d`/ 2 | ls -d */ | egrep '^(19|20)[0-9][0-9](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])/$' 3 | -------------------------------------------------------------------------------- /bash/fsdtest-pid: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | while true; do ((n++)); echo $n; sleep 1; done 3 | 4 | -------------------------------------------------------------------------------- /bash/func_exit_return.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | retfunc() 4 | { 5 | echo "this is retfunc()" 6 | return 1 7 | } 8 | 9 | exitfunc() 10 | { 11 | echo "this is exitfunc()" 12 | exit 1 13 | } 14 | 15 | retfunc 16 | echo "We are still here" 17 | exitfunc 18 | echo "We will never see this" 19 | -------------------------------------------------------------------------------- /bash/func_exit_return2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | retfunc() 4 | { 5 | echo "this is retfunc()" 6 | return 1 7 | } 8 | 9 | exitfunc() 10 | { 11 | echo "this is exitfunc()" 12 | exit 2 13 | } 14 | 15 | ret1=`retfunc` 16 | echo "$?" 17 | echo "We are still here, ret1 = $ret1" 18 | 19 | ret2=`exitfunc` 20 | echo "$?" 21 | echo "We can still see this, ret2 = $ret2" 22 | -------------------------------------------------------------------------------- /bash/oswin.sh: -------------------------------------------------------------------------------- 1 | echo '>>>>>>> Test "$@"' 2 | for thing in "$@"; do 3 | echo $thing 4 | done 5 | 6 | echo '>>>>>>> Test "$@", after expansion' 7 | for thing in "$1" "$2" "$3"; do 8 | echo $thing 9 | done 10 | 11 | echo '>>>>>>> Test $@' 12 | for thing in $@; do 13 | echo $thing 14 | done 15 | 16 | echo '>>>>>>> Test $@, after expansion' 17 | for thing in $1 $2 $3; do 18 | echo $thing 19 | done 20 | -------------------------------------------------------------------------------- /bash/port_forward.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Run as root if your listening port is < 1024! 3 | 4 | FIFO=$(mktemp -u) 5 | trap "rm -f $FIFO; killall -9 nc; exit 255;" SIGINT SIGTERM SIGKILL 6 | mkfifo $FIFO 7 | 8 | while true; do 9 | # echo "Listening..." 10 | cat $FIFO | nc -k -l localhost $1 | nc localhost 6543 > $2 11 | done 12 | -------------------------------------------------------------------------------- /bash/rtmp.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #clone video files from cloudfront :) 3 | 4 | for (( i = 1; i < 5; i++ )); do 5 | for (( j = 1; j < 5; j++ )); do 6 | URL="rtmp://s3bx78qcamjui1.cloudfront.net/cfx/st/mp4:${i}-ideas-${j}-${i}.mp4" 7 | echo $URL 8 | rtmpdump -r ${URL} -o ~/Desktop/1-niche-types-2.mp4 9 | done 10 | done 11 | -------------------------------------------------------------------------------- /bash/sed_practice: -------------------------------------------------------------------------------- 1 | # Grap any *-14_consecutive_num-* 2 | echo "./12M-20120419010203-aaaa-bbb-ccc.log" | sed "s/\(.*\)-\([0-9]\{14\}\)-\(.*\)/\2/" 3 | echo "./12M-20120419010203-aaaa-bbb-ccc.log" | sed "s/.*-\([0-9]\{14\}\)-.*/\1/" 4 | -------------------------------------------------------------------------------- /bash/select.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Displays a list of files in current directory and prompt for which 3 | # file to edit 4 | 5 | # Set the prompt for the select command 6 | PS3="Type a number or 'q' to quit: " 7 | 8 | # Create a list of files to display 9 | fileList=$(find . -maxdepth 1 -type f) 10 | 11 | # Show a menu and ask for input. If the user entered a valid choice, 12 | # then invoke the editor on that file 13 | select fileName in $fileList; do 14 | if [ -n "$fileName" ]; then 15 | vim ${fileName} 16 | fi 17 | break 18 | done 19 | 20 | -------------------------------------------------------------------------------- /bash/shell_expansion.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Ref: 4 | # https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash 5 | # https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html 6 | 7 | # ~% FILE="example.tar.gz" 8 | # ~% echo "${FILE%%.*}" 9 | # example 10 | # ~% echo "${FILE%.*}" 11 | # example.tar 12 | # ~% echo "${FILE#*.}" 13 | # tar.gz 14 | # ~% echo "${FILE##*.}" 15 | # gz 16 | 17 | filename=$(basename "$1") 18 | extension1="${filename#*.}" 19 | extension2="${filename##*.}" 20 | filename1="${filename%.*}" 21 | filename2="${filename%%.*}" 22 | 23 | echo $filename 24 | echo $extension1 25 | echo $extension2 26 | echo $filename1 27 | echo $filename2 28 | -------------------------------------------------------------------------------- /bash/shopt_expand_aliases.sh: -------------------------------------------------------------------------------- 1 | #/bin/bash 2 | shopt 3 | echo "-------------------------" 4 | shopt -s expand_aliases 5 | echo "-------------------------" 6 | shopt 7 | echo "-------------------------" 8 | alias hi="echo 'hello world'" 9 | hi 10 | -------------------------------------------------------------------------------- /bash/sleepsort.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | function f() { 3 | sleep "$1" 4 | echo "$1" 5 | } 6 | while [ -n "$1" ] 7 | do 8 | f "$1" & 9 | shift 10 | done 11 | wait 12 | 13 | -------------------------------------------------------------------------------- /bash/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | NEED=10000 4 | [ ${NEED} -gt 111000 ] ? echo 'c'; : echo 'd'; 5 | if [ ${NEED} -gt 111000 ]; then 6 | echo 'a'; 7 | else 8 | echo 'b'; 9 | fi 10 | 11 | if [ ${Test} ]; then 12 | echo "ohya" 13 | fi 14 | 15 | if [ "$1" != "/mnt/samba/link0/" ] && [ `ls "$1" | wc -l` -eq 0 ] ; then 16 | echo "OHya" 17 | else 18 | echo "shit" 19 | fi 20 | 21 | 22 | -------------------------------------------------------------------------------- /bash/timediff.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | START=$(date +%s) 3 | # do something 4 | 5 | #time find /home/kent/practice > /dev/null & 6 | # start your script work here 7 | time find ./ > /dev/null 8 | # your logic ends here 9 | 10 | 11 | END=$(date +%s) 12 | DIFF=$(( $END - $START )) 13 | echo "It took $DIFF seconds" 14 | 15 | # start your script work here 16 | time find ./ > /dev/null 17 | # your logic ends here 18 | 19 | END2=$(date +%s) 20 | DIFF2=$(( $END2 - $END )) 21 | echo "It took $DIFF2 seconds" 22 | 23 | -------------------------------------------------------------------------------- /bash/zero-padding.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for ((x=1;x<=31;x+=1)); 3 | do touch `printf "%02d" $x`; 4 | done 5 | -------------------------------------------------------------------------------- /buserror.c: -------------------------------------------------------------------------------- 1 | // http://en.wikipedia.org/wiki/Segmentation_fault 2 | #include 3 | 4 | int main(int argc, char **argv) { 5 | int *iptr; 6 | char *cptr; 7 | 8 | #if defined(__GNUC__) 9 | # if defined(__i386__) 10 | /* Enable Alignment Checking on x86 */ 11 | __asm__("pushf\norl $0x40000,(%esp)\npopf"); 12 | # elif defined(__x86_64__) 13 | /* Enable Alignment Checking on x86_64 */ 14 | __asm__("pushf\norl $0x40000,(%rsp)\npopf"); 15 | # endif 16 | #endif 17 | 18 | /* malloc() always provides aligned memory */ 19 | cptr = malloc(sizeof(int) + 1); 20 | 21 | /* Increment the pointer by one, making it misaligned */ 22 | iptr = (int *) ++cptr; 23 | 24 | /* Dereference it as an int pointer, causing an unaligned access */ 25 | *iptr = 42; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /c-file/tmpfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void){ 4 | FILE *file; 5 | if((file=tmpfile())==NULL) { 6 | printf("Cannot open temporary work file.\n"); 7 | exit(1); 8 | } 9 | fprintf(file, "Hello! How are you."); 10 | fflush(file); 11 | 12 | char msg[10]; 13 | fseek(file, 0, SEEK_SET); 14 | fscanf(file, "%s", msg); 15 | printf("msg=%s", msg); 16 | fclose(file); 17 | } 18 | -------------------------------------------------------------------------------- /c-function-pointer/README.txt: -------------------------------------------------------------------------------- 1 | http://ccckmit.wikidot.com/cp:main 2 | -------------------------------------------------------------------------------- /c-function-pointer/function_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int add(int a, int b) { 4 | return a+b; 5 | } 6 | 7 | int mult(int a, int b) { 8 | return a*b; 9 | } 10 | 11 | int main() { 12 | int (*op)(int a, int b); 13 | op = add; 14 | printf("op(3,5)=%d\n", op(3,5)); 15 | op = mult; 16 | printf("op(3,5)=%d\n", op(3,5)); 17 | } 18 | -------------------------------------------------------------------------------- /c-function-pointer/function_pointer_typedef.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef int(*OP)(int,int); 4 | 5 | int add(int a, int b) { 6 | return a+b; 7 | } 8 | 9 | int mult(int a, int b) { 10 | return a*b; 11 | } 12 | 13 | int main() { 14 | OP op = add; 15 | printf("op(3,5)=%d\n", op(3,5)); 16 | op = mult; 17 | printf("op(3,5)=%d\n", op(3,5)); 18 | } 19 | -------------------------------------------------------------------------------- /c-macro/stringfication2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define WARN_IF(EXP) \ 4 | do { if (EXP) \ 5 | fprintf (stderr, "Warning: " #EXP "\n"); } \ 6 | while (0) 7 | 8 | typedef struct TCCSyms { 9 | char *str; 10 | void *ptr; 11 | } TCCSyms; 12 | 13 | #define TCCSYM(a) { #a, &a, }, 14 | /* add the symbol you want here if no dynamic linking is done */ 15 | static TCCSyms tcc_syms[] = { 16 | TCCSYM(printf) 17 | TCCSYM(fprintf) 18 | TCCSYM(fopen) 19 | TCCSYM(fclose) 20 | { NULL, NULL }, 21 | }; 22 | 23 | int main() { 24 | int x = 0; 25 | WARN_IF(x == 0); 26 | 27 | int i; 28 | for (i=0; tcc_syms[i].str != NULL; i++) 29 | printf("symbol:%-10s address:%d\n", tcc_syms[i].str, tcc_syms[i].ptr); 30 | } 31 | -------------------------------------------------------------------------------- /c-memory/leak.c: -------------------------------------------------------------------------------- 1 | /* 2 | * setenv MALLOC_TRACE /home/karthik/temp/trace.txt // 設定 mtrace 的環境變數 MALLOC_TRACE 3 | * $ gcc -g -Wall -ansi -pedantic leak.c -o leak.o // 編譯 4 | * $ ./leak // 執行 5 | * $ mtrace leak.o /home/karthik/temp/trace.txt // 追蹤記憶體漏洞 6 | * Memory not freed: 7 | * ----------------- 8 | * Address Size Caller 9 | * 0x08049910 0x4 at /home/karthik/tips/leak.c:9 10 | * 11 | */ 12 | #include 13 | #include // mtrace header 14 | 15 | int main() { 16 | int *a; 17 | mtrace(); 18 | a = malloc(sizeof(int)); 19 | *a = 7; 20 | // Forget to free() 21 | return EXIT_SUCCESS; 22 | } 23 | -------------------------------------------------------------------------------- /c-memory/mcheck.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int newSize = 0; 5 | int delSize = 0; 6 | #define new(TYPE, n) malloc(n*sizeof(TYPE)); newSize+=n*sizeof(TYPE) 7 | #define del(ptr, TYPE, n) free(ptr); delSize+=n*sizeof(TYPE) 8 | #define mcheck() printf("Memory:newSize=%d delSize=%d leakSize=%d\n", \ 9 | newSize, delSize, newSize-delSize); 10 | 11 | int main() 12 | { 13 | int *ip = new(int, 10); 14 | char *cp = new(char, 5); 15 | del(ip, int, 10); 16 | mcheck(); 17 | } 18 | -------------------------------------------------------------------------------- /c-object-oriented/encapsulation.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Circle { 4 | void (*new)(struct Circle*, float); 5 | float (*area)(struct Circle*); 6 | float r; 7 | }; 8 | 9 | float CircleArea(struct Circle *obj) { return 3.14 * obj->r * obj->r; } 10 | 11 | void CircleNew(struct Circle *obj, float r) { 12 | obj->new = CircleNew; 13 | obj->area = CircleArea; 14 | obj->r = r; 15 | } 16 | 17 | int main() { 18 | struct Circle c; 19 | CircleNew(&c, 3.0); 20 | printf("area() = %G\n", c.area(&c)); 21 | } 22 | -------------------------------------------------------------------------------- /c-struct/README.txt: -------------------------------------------------------------------------------- 1 | http://ccckmit.wikidot.com/cp:structure 2 | -------------------------------------------------------------------------------- /c-struct/structbits.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 請注意,寫在前面的欄位會被編在低位元部份,因此通常我們必須倒著寫,也就是將圖從右看到左。 4 | typedef struct 5 | { 6 | unsigned lo:4; // 像是 unsigned hi, lo:4; 7 | unsigned hi:4; // 請注意,不能把兩個欄位寫在同一行 8 | } byte; 9 | 10 | typedef struct 11 | { 12 | unsigned m:1; 13 | unsigned b2:5; 14 | unsigned t:1; 15 | unsigned i:1; 16 | unsigned b1:20; 17 | unsigned v:1; 18 | unsigned c:1; 19 | unsigned z:1; 20 | unsigned n:1; 21 | } StatusWord; 22 | 23 | int main() { 24 | byte b = { .hi=0x0F, .lo=0x0C }; 25 | unsigned char *bp = (unsigned char*) &b; 26 | printf("byte=%02x\n", *bp); 27 | 28 | StatusWord sw = { .n=1,.z=0,.c=1,.v=0,.i=1,.t=1,.m=0,.b1=0,.b2=0 }; 29 | unsigned int *psw = (unsigned int*) &sw; 30 | printf("SW=%08x\n", *psw); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /comet-http-streaming/test.cgi: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #Notice: this cgi file must be put at dir"/usr/share/www/cgi/" 3 | 4 | if [ "$REQUEST_METHOD" = "POST" ]; then 5 | strparam=`cat $stdin | cut -c1-$CONTENT_LENGTH` 6 | else 7 | strparam=$QUERY_STRING 8 | fi 9 | 10 | strparam=`/usr/bin/decode.sh $strparam` 11 | 12 | echo -ne "Content-type: text/html\r\n" 13 | #echo -ne "Transfer-Encoding: chunked\r\n" 14 | echo -ne "Cache-control: no-cache, must-revalidate\r\n" 15 | echo -ne "Pragma: no-cache\r\n" 16 | echo -ne "\r\n" # end of HTTP header 17 | 18 | /mnt/ramdisk/test.sh & 19 | #confclient -g system_test -p 9 20 | 21 | echo "testing...." 22 | 23 | -------------------------------------------------------------------------------- /cyg-profile/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "cyg-profile.h" 3 | 4 | int 5 | function1 (void) 6 | { 7 | return 0; 8 | } 9 | 10 | int 11 | function2 (void) 12 | { 13 | return function1 () + 1; 14 | } 15 | 16 | int 17 | function3 (void) 18 | { 19 | return function2 () + 1; 20 | } 21 | 22 | int 23 | main (void) 24 | { 25 | printf ("Logfile: %s\n", cygprofile_getfilename ()); 26 | cygprofile_enable (); 27 | return function3 (); 28 | } 29 | -------------------------------------------------------------------------------- /cyg-profile/test.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cyg-profile.h" 4 | 5 | using namespace std; 6 | 7 | class test { 8 | int function1 (long l); 9 | int function2 (int i); 10 | public: 11 | int function3 (char c); 12 | }; 13 | 14 | int 15 | test::function1 (long l) { 16 | return l; 17 | } 18 | 19 | int 20 | test::function2 (int i) { 21 | return test::function1 (i) + 1; 22 | } 23 | 24 | int 25 | test::function3 (char c) { 26 | return test::function2 (c) + 1; 27 | } 28 | 29 | int main () { 30 | class test *ptest; 31 | 32 | cout << "Logfile: " << cygprofile_getfilename () << endl; 33 | 34 | cygprofile_enable(); 35 | 36 | ptest = new test(); 37 | 38 | return ptest->function3 (1); 39 | } 40 | -------------------------------------------------------------------------------- /date_by_gettimeofday.c: -------------------------------------------------------------------------------- 1 | /* C routine: sample gettimeofday with time in milliseconds 2 | mmc mchirico@users.sourceforge.net 3 | 4 | Downloads: 5 | http://prdownloads.sourceforge.net/cpearls/date_calc.tar.gz?download 6 | 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | char buffer[30]; 16 | struct timeval tv; 17 | 18 | time_t curtime; 19 | 20 | gettimeofday(&tv, NULL); 21 | curtime=tv.tv_sec; 22 | 23 | strftime(buffer,30,"%m-%d-%Y %T.",localtime(&curtime)); 24 | printf("%s%ld\n",buffer,tv.tv_usec); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /defunct.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(){ 8 | 9 | system("/bin/ls -al; sleep 3"); 10 | printf("Alive!\n"); 11 | 12 | int status = -1; 13 | if(!fork()){ 14 | printf("child pid=%d\n", getpid()); 15 | exit(0); 16 | } 17 | 18 | // Comment below line to observe [defunct] status in "ps ux | grep defunct" 19 | // wait(&status); 20 | 21 | printf("status = %d, %d, %d\n", status, WIFEXITED(status), WEXITSTATUS(status)); 22 | sleep(20); 23 | 24 | printf("parent pid=%d \n", getpid()); 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /eServ/Makefile: -------------------------------------------------------------------------------- 1 | PROG = eserv 2 | 3 | CC = gcc 4 | 5 | ifeq ($(shell uname),linux) 6 | SOURCE_FLAGS = -D_POSIX_SOURCE -D_BSD_SOURCE 7 | endif 8 | 9 | CFLAGS = -Os -Wall -Wextra -Werror \ 10 | $(SOURCE_FLAGS) -g \ 11 | -std=gnu99 12 | # -D NDEBUG 13 | LDFLAGS = -lpthread 14 | 15 | OBJS = \ 16 | libeserv/mempool.o \ 17 | libeserv/hash.o \ 18 | libeserv/io.o \ 19 | libeserv/http.o \ 20 | libeserv/request.o \ 21 | libeserv/analysis.o \ 22 | libeserv/entry.o \ 23 | libeserv/cgi.o \ 24 | libeserv/misc.o \ 25 | main.o \ 26 | \ 27 | cgi_custom.o 28 | 29 | all: $(PROG) 30 | 31 | $(PROG): $(OBJS) 32 | $(CC) -o $(PROG) $(CFLAGS) $(LDFLAGS) $(OBJS) 33 | 34 | clean: 35 | rm -f $(PROG) $(OBJS) 36 | -------------------------------------------------------------------------------- /eServ/README: -------------------------------------------------------------------------------- 1 | Project: eServ 2 | 3 | namespace: ex 4 | 5 | Configurations: 6 | - file: libeserv/http.h 7 | - default listen port 8 | #define PORT 8000 9 | - default max threads 10 | #define EX_MAX_THREADS 10 11 | - directory www 12 | static const char *IndexFile = "index.html"; 13 | static const char *RootPath = "www"; 14 | -------------------------------------------------------------------------------- /eServ/cgi_custom.h: -------------------------------------------------------------------------------- 1 | #ifndef __CGI_CUSTOM_H__ 2 | #define __CGI_CUSTOM_H__ 3 | 4 | #include "libeserv/cgi.h" 5 | 6 | extern int cgi_page_sum(ExHttp *pHttp); 7 | extern int cgi_page_txt(ExHttp *pHttp); 8 | extern int cgi_page_login(ExHttp *pHttp); 9 | extern int cgi_page_gallery(ExHttp *pHttp); 10 | 11 | /* customized page handler declare here */ 12 | cgi_page cgi_pages[] = { 13 | { 14 | .name = "sum.cgi", 15 | .callback = cgi_page_sum, 16 | }, 17 | 18 | { 19 | .name = "txt.cgi", 20 | .callback = cgi_page_txt, 21 | }, 22 | 23 | { 24 | .name = "login.cgi", 25 | .callback = cgi_page_login, 26 | }, 27 | 28 | { 29 | .name = "gallery.cgi", 30 | .callback = cgi_page_gallery, 31 | }, 32 | }; 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /eServ/libeserv/analysis.h: -------------------------------------------------------------------------------- 1 | #include "http.h" 2 | 3 | #ifndef __ESERV_ANALYSIS_H__ 4 | #define __ESERV_ANALYSIS_H__ 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | int checkmethod(ExHttp *pHttp); 11 | int parseURL(ExHttp *pHttp); 12 | int parseHeader(ExHttp *pHttp); 13 | int checkpath(ExHttp *pHttp); 14 | int cacheCheck(const ExHttp *pHttp); 15 | int decodeParam(ExHttp *pHttp); 16 | 17 | #ifdef __cplusplus 18 | } 19 | #endif 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /eServ/libeserv/cgi.c: -------------------------------------------------------------------------------- 1 | #include "cgi.h" 2 | 3 | /* FIXME : flexible inclusion of customized CGI */ 4 | #include "../cgi_custom.h" 5 | 6 | extern cgi_page cgi_pages[]; 7 | 8 | void cgi_init() 9 | { 10 | size_t i; 11 | for (i = 0; i < sizeof(cgi_pages) / sizeof(cgi_page); i++) 12 | cgi_page_add(cgi_pages[i].name, 13 | cgi_pages[i].callback); 14 | } 15 | 16 | void cgi_uninit() 17 | { 18 | } 19 | 20 | int cgi_handler(ExHttp *pHttp, void *handle) 21 | { 22 | int (*pf)(ExHttp *) = handle; 23 | 24 | return pf(pHttp); 25 | } 26 | 27 | int errorLog(ExHttp *pHttp, const char *mess) 28 | { 29 | assert(pHttp); 30 | if (pHttp) 31 | printf("%s\n", mess); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /eServ/libeserv/cgi.h: -------------------------------------------------------------------------------- 1 | #ifndef __ESERV_CGI_H__ 2 | #define __ESERV_CGI_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "http.h" 9 | 10 | void cgi_init(); 11 | 12 | void cgi_uninit(); 13 | 14 | int cgi_handler(ExHttp *pHttp, void *handle); 15 | 16 | int errorLog(ExHttp *pHttp, const char *mess); 17 | 18 | typedef struct { 19 | char *name; 20 | int (*callback)(ExHttp *pHttp); 21 | } cgi_page; 22 | 23 | #define CGI_DECL_BEGIN \ 24 | /* customized page handler declare here */ \ 25 | cgi_page cgi_pages[] = { 26 | 27 | #define CGI_ENTRY(NAME) \ 28 | { \ 29 | .name = #NAME ".cgi" , \ 30 | .callback = cgi_page_ ## NAME, \ 31 | }, 32 | 33 | #define CGI_DECL_END \ 34 | }; 35 | 36 | #ifdef __cplusplus 37 | } 38 | #endif 39 | 40 | #endif 41 | 42 | -------------------------------------------------------------------------------- /eServ/libeserv/entry.c: -------------------------------------------------------------------------------- 1 | #include "entry.h" 2 | #include "misc.h" 3 | 4 | int start_thread(void *(*func)(void *), void *param) 5 | { 6 | pthread_t thread_id; 7 | pthread_attr_t attr; 8 | int retval; 9 | 10 | pthread_attr_init(&attr); 11 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 12 | 13 | if ((retval = pthread_create(&thread_id, &attr, func, param)) != 0) 14 | DBG("%s: %s", __func__, strerror(retval)); 15 | 16 | return (retval); 17 | } 18 | -------------------------------------------------------------------------------- /eServ/libeserv/io.h: -------------------------------------------------------------------------------- 1 | #ifndef __ESERV_IO_H__ 2 | #define __ESERV_IO_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "http.h" 9 | 10 | int ex_sock_nwrite(SOCKET sock, char *buf, size_t n); 11 | 12 | int ex_read_head(SOCKET sock, char *buf, size_t bufsize); 13 | int ex_load_body(ExHttp *pHttp); 14 | int sendFileStream(const ExHttp *pHttp, const char *filePath); 15 | 16 | #ifdef __cplusplus 17 | } 18 | #endif 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /eServ/libeserv/mempool.h: -------------------------------------------------------------------------------- 1 | #ifndef __ESERV_MPOOL_H__ 2 | #define __ESERV_MPOOL_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | typedef struct { 9 | char *begin; /* start pos */ 10 | size_t len; /* capacity */ 11 | int index; /* curIndex */ 12 | int cflag; /* clear flag */ 13 | } ex_mpool; 14 | 15 | void ex_mpool_init(ex_mpool *pmp, char *begin, size_t len); 16 | void *ex_mpool_malloc(ex_mpool *pmp, size_t mlen); 17 | void ex_mpool_free(ex_mpool *pmp, void *p); 18 | void ex_mpool_clear(ex_mpool *pmp); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /eServ/main.c: -------------------------------------------------------------------------------- 1 | #include "libeserv/http.h" 2 | 3 | int main() 4 | { 5 | char buf[16]; 6 | 7 | ex_init(); 8 | 9 | while (scanf("%16s", buf) > 0) { 10 | if (strncmp("quit", buf, 4) == 0) 11 | break; 12 | ex_sleep(200); 13 | } 14 | 15 | ex_uninit(); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /eServ/www/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/eServ/www/1.jpg -------------------------------------------------------------------------------- /eServ/www/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/eServ/www/2.jpg -------------------------------------------------------------------------------- /eServ/www/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/eServ/www/3.jpg -------------------------------------------------------------------------------- /eServ/www/alien.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/eServ/www/alien.png -------------------------------------------------------------------------------- /eServ/www/gallery.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Demo 4 | 5 | 11 | 12 | 13 | This will play gallery step-by-step. Return
14 | 15 |

16 |


17 | 18 | 19 | 20 | 30 | -------------------------------------------------------------------------------- /eServ/www/hello.txt: -------------------------------------------------------------------------------- 1 | Hello World! 2 | -------------------------------------------------------------------------------- /fifo/SimpleFifoTest.txt: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 1st. mkfifo foo 3 | 2nd. echo "Hello world" > foo 4 | 3rd. cat < foo 5 | 6 | -------------------------------------------------------------------------------- /fifo/client.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define SERVER_FIFO_NAME "/tmp/serv_fifo" 11 | #define CLIENT_FIFO_NAME "/tmp/cli_%d_fifo" 12 | 13 | #define BUFFER_SIZE 20 14 | 15 | struct data_to_pass_st { 16 | pid_t client_pid; 17 | char some_data[BUFFER_SIZE-1]; 18 | }; 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /fifo/testfifo/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | if (argc != 3) 10 | { 11 | printf("Usage: %s fifo_path message\n", argv[0]); 12 | return -1; 13 | } 14 | 15 | int ifd; 16 | 17 | if ( ( ifd = open(argv[1], O_WRONLY|O_NONBLOCK )) < 0 ) 18 | { 19 | printf("Failed to open fifo! %m\n"); 20 | return -1; 21 | } 22 | 23 | int iLen; 24 | 25 | iLen = strlen(argv[2]); 26 | 27 | printf("Write %s (%d bytes)\n", argv[2], iLen); 28 | 29 | write(ifd, argv[2], iLen); 30 | 31 | while(1) 32 | { 33 | sleep(1); 34 | } 35 | 36 | close(ifd); 37 | printf("%m\n"); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /file_lock/flockfile.txt: -------------------------------------------------------------------------------- 1 | Start write... 2 | [1] press any key to unlock the file. 3 | [1] press any key to unlock the file. 4 | [2] press any key to unlock the file. 5 | Start write... 6 | [1] press any key to unlock the file. 7 | [1] press any key to unlock the file. 8 | [2] press any key to unlock the file. 9 | -------------------------------------------------------------------------------- /file_lock/fwrite.txt: -------------------------------------------------------------------------------- 1 | 1111111111 2 | 2121212121 3 | 2222222222 4 | 3333333333 5 | 1111111111 6 | 2121212121 7 | 2222222222 8 | 3333333333 9 | 1111111111 10 | 2121212121 11 | 2222222222 12 | 3333333333 13 | 1111111111 14 | 2121212121 15 | 2222222222 16 | 3333333333 17 | 1111111111 18 | 2121212121 19 | 2222222222 20 | 3333333333 21 | 1111111111 22 | 2121212121 23 | 2222222222 24 | 3333333333 25 | -------------------------------------------------------------------------------- /file_lock/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int fd; 7 | 8 | fd = open("/mnt/ramdisk/video.txt", O_RDWR); 9 | 10 | if (flock(fd, LOCK_EX) < 0) 11 | { 12 | perror("flock"); 13 | return -1; 14 | } 15 | 16 | getchar(); 17 | 18 | if (flock(fd, LOCK_UN) < 0) 19 | { 20 | perror("flock"); 21 | return -1; 22 | } 23 | 24 | close(fd); 25 | 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /gdb/HappyGDB-PartI-2008-01-26.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gdb/HappyGDB-PartI-2008-01-26.pdf -------------------------------------------------------------------------------- /gdb/HappyGDB-PartI-2008-07-12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gdb/HappyGDB-PartI-2008-07-12.pdf -------------------------------------------------------------------------------- /gdb/HappyGDB-PartII-2008-07-12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gdb/HappyGDB-PartII-2008-07-12.pdf -------------------------------------------------------------------------------- /gdb/frame_before_coredump/makefile: -------------------------------------------------------------------------------- 1 | CC=gcc -Wall -g -O0 2 | #CC=arm-linux-gcc -Wall -g -O0 3 | 4 | all: before_die test_corruption 5 | $(CC) -o test test_corruption.o before_die.o -pthread 6 | 7 | before_die : before_die.c 8 | $(CC) -c before_die.c 9 | 10 | test_corruption : test_corruption.c 11 | $(CC) -pthread -finstrument-functions -c test_corruption.c 12 | 13 | clean : 14 | rm -rf *.o test core* 15 | 16 | .PHONY:all clean 17 | 18 | -------------------------------------------------------------------------------- /gdb/gdb-rocks/......--help.....: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gdb/gdb-rocks/......--help..... -------------------------------------------------------------------------------- /gdb/gdb-rocks/.K....y..c*: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gdb/gdb-rocks/.K....y..c* -------------------------------------------------------------------------------- /gdb/gdb-rocks/daynamic_change_value.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static char *my_str = "Hello World!"; 4 | int main() 5 | { 6 | puts(my_str); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /gdb/gdb-rocks/hello-loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(int argc, char **argv) 4 | { 5 | printf("Hello World!\n"); 6 | while (1) { 7 | sleep(5); 8 | printf("Hello World!\n"); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /gdb/gdb-rocks/ibm_divide_zero.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int wib(int no1, int no2) 4 | { 5 | int result, diff; 6 | diff = no1 - no2; 7 | result = no1 / diff; 8 | return result; 9 | } 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int value, div, result, i, total; 14 | 15 | value = 10; 16 | div = 6; 17 | total = 0; 18 | 19 | for(i = 0; i < 10; i++) 20 | { 21 | result = wib(value, div); 22 | total += result; 23 | div++; 24 | value--; 25 | } 26 | 27 | printf("%d wibed by %d equals %d\n", value, div, total); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /gdb/gdb-rocks/jump.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX_TMP 1000 4 | static char *my_str = "Hello World!"; 5 | 6 | inline test_printf(void) 7 | { 8 | printf("[%s] test\n", __FUNCTION__); 9 | } 10 | 11 | dummy_function (void) 12 | { 13 | unsigned char *ptr = 0x00; 14 | test_printf(); 15 | fprintf(stderr, "[%s] %d\n", __FUNCTION__, MAX_TMP); 16 | *ptr = 0x00; 17 | fprintf(stderr, "[%s] %c\n", __FUNCTION__, ptr); 18 | fprintf(stderr, "[%s] >>><<< \n", __FUNCTION__); 19 | } 20 | 21 | int main (void) 22 | { 23 | dummy_function (); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /gdb/gdb-rocks/set_var.c: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int a, b, c = 0; 4 | c = a + b; 5 | printf("%d\n", c); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /gdb/gdb-rocks/stack-backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | void func4() { puts("Output"); } 3 | void func3() { func4(); } 4 | void func2() { func3(); } 5 | void func1() { func2(); } 6 | 7 | int main() 8 | { 9 | int ret = 0; 10 | func1(); 11 | return ret; 12 | } 13 | -------------------------------------------------------------------------------- /gdb/gdb-samples/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -Wall -g \ 3 | -o dpm dpm.cc 4 | gcc -Wall -g \ 5 | -o hello hello.c 6 | gcc -Wall -g \ 7 | -o input input.c 8 | gcc -Wall -g \ 9 | -o fibonacci fibonacci.c 10 | gcc -Wall -g \ 11 | -o quicksort quicksort.c 12 | 13 | clean: 14 | rm -f \ 15 | dpm \ 16 | hello \ 17 | input \ 18 | fibonacci \ 19 | quicksort 20 | -------------------------------------------------------------------------------- /gdb/gdb-samples/README_C_INTERPRETER: -------------------------------------------------------------------------------- 1 | $ gdb `which gdb` 2 | GNU gdb 6.7.1-debian 3 | (gdb) start 4 | Breakpoint 1 at 0x807f55e 5 | Starting program: /usr/bin/gdb 6 | 0x0807f55e in main () 7 | (gdb) p 1+2 8 | $1 = 3 9 | (gdb) p abs(-50) 10 | $2 = 50 11 | (gdb) p puts("Hello World") 12 | Hello World 13 | $3 = 12 14 | (gdb) 15 | 16 | 17 | -------------------------------------------------------------- 18 | (gdb) p getenv("HOME") 19 | $4 = -1074340217 20 | (gdb) x/s $4 21 | 0xbff6de87: "/home/jserv" 22 | (gdb) x/s $ 23 | 0xbff6de87: "/home/jserv" 24 | (gdb) p (char)*$4 25 | $5 = 47 '/' 26 | (gdb) 27 | -------------------------------------------------------------------------------- /gdb/gdb-samples/dpm.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i; 6 | for (i = 0; i < 2; i++) 7 | { 8 | std::cout << i << std::endl; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /gdb/gdb-samples/fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fib(int n) 4 | { 5 | /* Ending condition of recursive */ 6 | if (n == 0) { 7 | return 0; 8 | } 9 | /* Ending condition of recursive */ 10 | else if (n == 1) { 11 | return 1; 12 | } 13 | else { 14 | return fib(n - 1) + fib(n - 2); 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | printf("%d\n", fib(5)); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /gdb/gdb-samples/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static char *my_str = "Hello World!"; 5 | int main() 6 | { 7 | const char *buff; 8 | const char* path = getenv ("PATH"); 9 | const char* home = getenv ("HOME"); 10 | buff = getenv("MANPATH"); 11 | printf("getenv : %s\n", buff); 12 | puts(my_str); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /gdb/gdb-samples/input.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define nobug 3 | static char buff [256]; 4 | static char* string; 5 | 6 | int main () 7 | { 8 | printf ("Please input a string: "); 9 | #ifdef bug 10 | gets (string); 11 | printf ("\nYour string is: %s\n", string); 12 | #else 13 | gets (buff); 14 | printf ("\nYour string is: %s\n", buff); 15 | #endif 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /gdb/jserv_ptrace/dbg-int3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static volatile sig_atomic_t being_debugged = 1; 5 | static void int3_handler(int signo) { being_debugged = 0; } 6 | 7 | int main() 8 | { 9 | signal(SIGTRAP, int3_handler); 10 | __asm__ __volatile__("int3"); 11 | if (being_debugged) { 12 | puts("No, I don't want to serve you."); 13 | while (1) { 14 | /* endless loop */ ; 15 | } 16 | } 17 | puts("Yes, real routines go here."); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /gdb/jserv_ptrace/hello-int3.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | puts("Hello"); 5 | __asm__ __volatile__("int3"); 6 | puts("World"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /gdb/jserv_ptrace/ptraceme.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0 ) { 7 | printf("No, I don't want to serve you.\n"); 8 | while (1) { 9 | /* endless loop */ ; 10 | } 11 | } 12 | printf("Yes, real routines go here\n"); 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /gdb/kent/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | $(info "===== Building All ====") 3 | gcc -g -fno-omit-frame-pointer -o max_crypt max_crypt.c -lcrypt 4 | gcc -g -fno-omit-frame-pointer -o dump1 dump1.c 5 | gcc -g -fno-omit-frame-pointer -o ptr1 ptr1.c 6 | gcc -g -fno-omit-frame-pointer -o backtrace backtrace.c 7 | gcc -g -fno-omit-frame-pointer ibm_divide_zero.c -o ibm_divide_zero 8 | gcc -g -fno-omit-frame-pointer thread.c -o thread -lpthread 9 | gcc -g -fno-omit-frame-pointer memorylayout.c -o memorylayout 10 | $(info "==== Stop Here ====") 11 | 12 | clean: 13 | $(info "Clean All") 14 | rm -f ptr1 max_crypt dump1 backtrace ibm_divide_zero thread 15 | -------------------------------------------------------------------------------- /gdb/kent/backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* Obtain a backtrace and print it to stdout. */ 6 | void print_trace (void) 7 | { 8 | void *array[10]; 9 | size_t size; 10 | char **strings; 11 | size_t i; 12 | 13 | size = backtrace (array, 10); 14 | strings = backtrace_symbols (array, size); 15 | 16 | printf ("Obtained %zd stack frames.\n", size); 17 | 18 | for (i = 0; i < size; i++) 19 | printf ("%s\n", strings[i]); 20 | 21 | free (strings); 22 | } 23 | 24 | /* A dummy function to make the backtrace more interesting. */ 25 | void dummy_function (void) 26 | { 27 | print_trace (); 28 | } 29 | 30 | int main (void) 31 | { 32 | dummy_function (); 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /gdb/kent/doublefree.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int *ptr; 6 | 7 | ptr = (int *)malloc(128 * 1024 - 30); 8 | free(ptr); 9 | free(ptr); 10 | 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /gdb/kent/gdb.txt: -------------------------------------------------------------------------------- 1 | 2 | warning: Current output protocol does not support redirection 3 | -------------------------------------------------------------------------------- /gdb/kent/ibm_divide_zero.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int wib(int no1, int no2) 4 | { 5 | int result, diff; 6 | diff = no1 - no2; 7 | result = no1 / diff; 8 | return result; 9 | } 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | int value, div, result, i, total; 14 | 15 | value = 10; 16 | div = 6; 17 | total = 0; 18 | 19 | for(i = 0; i < 10; i++) 20 | { 21 | result = wib(value, div); 22 | total += result; 23 | div++; 24 | value--; 25 | } 26 | 27 | printf("%d wibed by %d equals %d\n", value, div, total); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /gdb/kent/max_crypt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define _XOPEN_SOURCE 6 | int main() 7 | { 8 | char str[8] = ""; 9 | char *strCrypt; 10 | int iRet = -1; 11 | 12 | // strncpy(str, "$1$AD1", 6); 13 | // str[6] = 0; 14 | 15 | // strCrypt = crypt("", ""); 16 | 17 | /*printf("%s\n", crypt("", "$1$abc"));*/ 18 | 19 | //iRet = strcmp("111111", crypt("admin", "$1$")); 20 | //printf("%d\n", iRet); 21 | 22 | printf("%x\n", strcmp("", crypt("admin", "")) == 0); 23 | strcmp("", crypt("admin", "")); 24 | 25 | printf("shit\n"); 26 | /*if (strCrypt != NULL) 27 | { 28 | printf("%s\n", strCrypt); 29 | }*/ 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /gdb/kent/memorylayout.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(int argc, const char *argv[]) 5 | { 6 | char cmd[32]; 7 | brk((void *)0x8051000); 8 | sprintf(cmd, "cat /proc/self/maps"); 9 | system(cmd); 10 | } 11 | -------------------------------------------------------------------------------- /gdb/kent/ptr1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX_TMP 1000 4 | static char *my_str = "Hello World!"; 5 | 6 | inline test_printf(void) 7 | { 8 | printf("[%s] test\n", __FUNCTION__); 9 | } 10 | 11 | dummy_function (void) 12 | { 13 | unsigned char *ptr = 0x00; 14 | test_printf(); 15 | fprintf(stderr, "[%s] %d\n", __FUNCTION__, MAX_TMP); 16 | *ptr = 0x00; 17 | fprintf(stderr, "[%s] %c\n", __FUNCTION__, ptr); 18 | fprintf(stderr, "[%s] >>><<< \n", __FUNCTION__); 19 | } 20 | 21 | int main (void) 22 | { 23 | dummy_function (); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /gdb/kent/thread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define NUM_THREADS 5 4 | 5 | void *thread_function(void *arg) 6 | { 7 | printf("Hello World from %d!\n", arg); 8 | sleep(10); 9 | pthread_exit(NULL); 10 | } 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | pthread_t threads[NUM_THREADS]; 15 | int i; 16 | 17 | for (i = 0; i < NUM_THREADS; i++) 18 | pthread_create(&threads[i], NULL, thread_function, (void *)i); 19 | 20 | for (i = 0; i < NUM_THREADS; i++) 21 | pthread_join(threads[i], NULL); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /gdb/sample1/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -Wall -g \ 3 | -o dpm dpm.cc 4 | gcc -Wall -g \ 5 | -o hello hello.c 6 | gcc -Wall -g \ 7 | -o input input.c 8 | gcc -Wall -g \ 9 | -o fibonacci fibonacci.c 10 | gcc -Wall -g \ 11 | -o quicksort quicksort.c 12 | 13 | clean: 14 | rm -f \ 15 | dpm \ 16 | hello \ 17 | input \ 18 | fibonacci \ 19 | quicksort 20 | -------------------------------------------------------------------------------- /gdb/sample1/dpm.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i; 6 | for (i = 0; i < 2; i++) 7 | { 8 | std::cout << i << std::endl; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /gdb/sample1/fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fib(int n) 4 | { 5 | /* Ending condition of recursive */ 6 | if (n == 0) { 7 | return 0; 8 | } 9 | /* Ending condition of recursive */ 10 | else if (n == 1) { 11 | return 1; 12 | } 13 | else { 14 | return fib(n - 1) + fib(n - 2); 15 | } 16 | } 17 | 18 | int main() 19 | { 20 | printf("%d\n", fib(5)); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /gdb/sample1/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static char *my_str = "Hello World!"; 4 | int main() 5 | { 6 | puts(my_str); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /gdb/sample1/input.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static char buff [256]; 4 | static char* string; 5 | 6 | int main () 7 | { 8 | printf ("Please input a string: "); 9 | *string = "string"; 10 | printf ("\nYour string is: %s\n", string); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /gdb/sample2/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -g -o ptype ptype.c 3 | gcc -g -o set set.c 4 | gcc -g -o set2 set2.c 5 | gcc -g -o save-restore save-restore.c 6 | gcc -g -o stack-backtrace stack-backtrace.c 7 | gcc -g -o stack stack.c 8 | gcc -g -o fib fib.c 9 | gcc -g -o prefix prefix.c 10 | 11 | clean: 12 | rm -f ptype set set2 save-restore stack-backtrace stack fib prefix 13 | -------------------------------------------------------------------------------- /gdb/sample2/clone-mem-file: -------------------------------------------------------------------------------- 1 | 1234567890 -------------------------------------------------------------------------------- /gdb/sample2/fib.c: -------------------------------------------------------------------------------- 1 | int fib (int n) 2 | { 3 | int result; 4 | 5 | if (n <= 2) 6 | result = 1; 7 | else 8 | result = fib(n-2) + 9 | fib(n-1); 10 | 11 | return result; 12 | } 13 | 14 | int main() 15 | { 16 | int ret = fib(5); 17 | return ret; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /gdb/sample2/hello-loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(int argc, char **argv) 4 | { 5 | printf("Hello World!\n"); 6 | while (1) { 7 | sleep(5); 8 | printf("Hello World!\n"); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /gdb/sample2/hello.c: -------------------------------------------------------------------------------- 1 | unsigned char shellcode[] = 2 | "\xeb\x0e\x90\x5e\x31\xc9\xb1\x56\x80\x36\x40\x46\xe2\xfa\xeb\x05\xe8\xee\xff" 3 | "\xff\xff\x15\xc9\xa5\x17\x16\x13\xa8\x40\x40\x40\x40\x1b\xc1\x83\xb5\xbf\xbf" 4 | "\xbf\xcd\xd3\x08\x40\x40\x40\xc3\xac\x4c\xc9\x91\xc3\xa4\xb0\xff\x41\x40\x40" 5 | "\x40\xf8\x44\x40\x40\x40\xfa\x4e\x40\x40\x40\x13\xc9\xbb\x8d\xc0\x1b\xc9\xb8" 6 | "\x13\xfb\x45\x40\x40\x40\x8d\xc0\x1b\xcd\x25\xb4\x1b\x1e\x1f\x89\x83\x08\x25" 7 | "\x2c\x2c\x2f\x60\x37\x2f\x32\x2c\x24\x61\x4a\x40"; 8 | 9 | int main(void) 10 | { 11 | ((void (*)())shellcode)(); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /gdb/sample2/ptype.c: -------------------------------------------------------------------------------- 1 | struct Rect { 2 | int x; 3 | int y; 4 | }; 5 | 6 | int main() 7 | { 8 | struct Rect rect = { -10, 20 }; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /gdb/sample2/save-restore.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char * str = 4 | #if 1 5 | "1234567890"; 6 | #else 7 | "0987654321"; 8 | #endif 9 | 10 | int main() 11 | { 12 | puts(str); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /gdb/sample2/search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct element { 4 | int data; 5 | struct element *next; 6 | } node_t; 7 | 8 | node_t *NewNode(char *label, int cost) { 9 | node_t *p = (node_t *) malloc(sizeof(node_t)); 10 | p->data 11 | 12 | void KillNode (node_t *node); 13 | 14 | node_t *Search(node_t *head, int data) 15 | { 16 | node_t *p; 17 | 18 | p = head; 19 | do { 20 | if (p->data = data) 21 | return (p); 22 | else 23 | p = p->next; 24 | } while (p != NULL); 25 | return (NULL); 26 | } 27 | 28 | int main() 29 | { 30 | } 31 | 32 | -------------------------------------------------------------------------------- /gdb/sample2/set.c: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int a, b, c = 0; 4 | c = a + b; 5 | printf("%d\n", c); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /gdb/sample2/set2.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | printf("Hello World!\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /gdb/sample2/simple.c: -------------------------------------------------------------------------------- 1 | int main() { printf("Hello World\n"); return 0; } 2 | -------------------------------------------------------------------------------- /gdb/sample2/stack-backtrace.c: -------------------------------------------------------------------------------- 1 | #include 2 | void func4() { puts("Output"); } 3 | void func3() { func4(); } 4 | void func2() { func3(); } 5 | void func1() { func2(); } 6 | 7 | int main() 8 | { 9 | int ret = 0; 10 | func1(); 11 | return ret; 12 | } 13 | -------------------------------------------------------------------------------- /gdb/sample2/stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int subtract(int a, int b) 4 | { 5 | return (a - b); 6 | } 7 | 8 | int main() 9 | { 10 | printf("out = %d\n", subtract(3, 2)); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /gdb/whocallme/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -o test test.c 3 | gcc -o test-1 test-1.c 4 | gcc -o bfd bfd.c -lbfd 5 | gcc -o bfd_dumpfun bfd_dumpfun.c -lbfd 6 | gcc -c whocallme.c -Wall 7 | ar -q libwhocallme.a whocallme.o 8 | gcc -o test-2 test-2.c -lbfd -L. -lwhocallme 9 | 10 | clean: 11 | rm -f \ 12 | test test-1 bfd bfd_dumpfun \ 13 | whocallme.o libwhocallme.a test-2 14 | -------------------------------------------------------------------------------- /gdb/whocallme/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define DUMP(func, call) \ 3 | printf("%s: func = %p, called by = %p\n", __FUNCTION__, func, call) 4 | 5 | void __attribute__((__no_instrument_function__)) 6 | __cyg_profile_func_enter(void *this_func, void *call_site) 7 | { 8 | DUMP(this_func, call_site); 9 | } 10 | void __attribute__((__no_instrument_function__)) 11 | __cyg_profile_func_exit(void *this_func, void *call_site) 12 | { 13 | DUMP(this_func, call_site); 14 | } 15 | 16 | main() 17 | { 18 | puts("Hello World!"); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /gdb/whocallme/test-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | void test() 3 | { 4 | unsigned int *stack; 5 | asm ("movl %%ebp, %0\n" 6 | : "=g"(stack)); 7 | printf("Return address = 0x%x\n", *(stack+1)); 8 | } 9 | 10 | int main() 11 | { 12 | test(); 13 | } 14 | -------------------------------------------------------------------------------- /gdb/whocallme/test-2.c: -------------------------------------------------------------------------------- 1 | #include "whocallme.h" 2 | 3 | void test() 4 | { 5 | who_call_me(); 6 | } 7 | void test_a() 8 | { 9 | test_b(); 10 | test_c(); 11 | } 12 | void test_b() 13 | { 14 | test(); 15 | } 16 | void test_c() 17 | { 18 | who_call_me(); 19 | } 20 | int main(int argc, char *argv[]) 21 | { 22 | init_function_table(argv[0]); 23 | test(); 24 | test_a(); 25 | test_b(); 26 | test_c(); 27 | } 28 | -------------------------------------------------------------------------------- /gdb/whocallme/test.c: -------------------------------------------------------------------------------- 1 | void test() 2 | { 3 | } 4 | 5 | int main() 6 | { 7 | test(); 8 | } 9 | -------------------------------------------------------------------------------- /gdb/whocallme/whocallme.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define FUNCTION_NAME_MAXLEN 80 4 | 5 | #define who_call_me() \ 6 | do { \ 7 | unsigned int *stack; \ 8 | asm ("movl %%ebp, %0\n" \ 9 | : "=g"(stack)); \ 10 | fprintf(stderr, \ 11 | ": function <%s> call me <%s>!\n", \ 12 | find_function_by_addr(*(stack+1)), who_am_i()); \ 13 | } while(0) 14 | 15 | extern int init_function_table(char *); 16 | -------------------------------------------------------------------------------- /gdb/whocallme/whocallme.tar.bz2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gdb/whocallme/whocallme.tar.bz2 -------------------------------------------------------------------------------- /gnu-linux-programming/README.txt: -------------------------------------------------------------------------------- 1 | Ref: https://github.com/jollen/gnu-linux-programming 2 | -------------------------------------------------------------------------------- /gnu-linux-programming/daemon.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gnu-linux-programming/daemon.c -------------------------------------------------------------------------------- /gnu-linux-programming/detached.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void *print_hello(void *argu) 5 | { 6 | while (1) { 7 | printf("Hello,\n"); 8 | sleep(1); 9 | } 10 | } 11 | 12 | void *print_world(void *argu) 13 | { 14 | while (1) { 15 | printf("World!\n"); 16 | sleep(1); 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | pthread_attr_t attr; 23 | pthread_t thread_id1, thread_id2; 24 | 25 | /* Create detached threads. */ 26 | pthread_attr_init(&attr); 27 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 28 | 29 | pthread_create(&thread_id1, &attr, &print_hello, NULL); 30 | pthread_create(&thread_id2, &attr, &print_world, NULL); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /gnu-linux-programming/dweb-r3/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | OBJS = socket.o http.o 3 | CFLAGS = -g -D__USE_FORK__ 4 | LIBS = -lpthread 5 | 6 | all: clean $(OBJS) 7 | $(CC) $(CFLAGS) -o dweb $(OBJS) $(LIBS) 8 | 9 | clean: 10 | rm -rf $(OBJS) 11 | -------------------------------------------------------------------------------- /gnu-linux-programming/dweb-r3/http.h: -------------------------------------------------------------------------------- 1 | #ifndef _HTTP_H_ 2 | #define _HTTP_H_ 3 | 4 | struct http_operations { 5 | int (*open)(struct http_operations *); 6 | int (*read)(struct http_operations *, char *); 7 | int (*write)(struct http_operations *, char *, int n); 8 | void (*close)(struct http_operations *); 9 | void *priv; 10 | }; 11 | 12 | #define MAX_OPS 5 13 | #define SOCKET_OPS 0 14 | #define FILE_OPS 1 15 | 16 | struct http_data_t { 17 | struct http_operatons *fops; 18 | }; 19 | 20 | static struct http_data_t http_data[MAX_OPS]; 21 | 22 | /* APIs */ 23 | int http_register(struct http_operations *, int); 24 | int http_unregister(struct http_operations *); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /gnu-linux-programming/dweb-r3/socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #define PORT 2000 5 | 6 | struct socket_data { 7 | int sockfd; 8 | int client_sockfd; 9 | int len; 10 | }; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /gnu-linux-programming/dweb/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | OBJS = socket.o http.o 3 | CFLAGS = -g 4 | LIBS = -lpthread 5 | 6 | all: clean $(OBJS) 7 | $(CC) $(CFLAGS) -o dweb $(OBJS) $(LIBS) 8 | 9 | clean: 10 | rm -rf $(OBJS) 11 | -------------------------------------------------------------------------------- /gnu-linux-programming/dweb/dweb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gnu-linux-programming/dweb/dweb -------------------------------------------------------------------------------- /gnu-linux-programming/dweb/http.h: -------------------------------------------------------------------------------- 1 | #ifndef _HTTP_H_ 2 | #define _HTTP_H_ 3 | 4 | struct http_operations { 5 | int (*open)(struct http_operations *); 6 | int (*read)(struct http_operations *, char *); 7 | int (*write)(struct http_operations *, char *, int n); 8 | void (*close)(struct http_operations *); 9 | void *priv; 10 | }; 11 | 12 | #define MAX_OPS 5 13 | #define SOCKET_OPS 0 14 | #define FILE_OPS 1 15 | 16 | struct http_data { 17 | struct http_operatons *fops; 18 | } http_data[MAX_OPS]; 19 | 20 | 21 | int http_register(struct http_operations *, int); 22 | int http_unregister(struct http_operations *); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /gnu-linux-programming/dweb/socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #define PORT 2000 5 | 6 | struct socket_data { 7 | int sockfd; 8 | int client_sockfd; 9 | int len; 10 | }; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /gnu-linux-programming/execvp.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gnu-linux-programming/execvp.c -------------------------------------------------------------------------------- /gnu-linux-programming/fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | pid_t self, child; 8 | 9 | self = getpid(); 10 | child = fork(); 11 | 12 | if (child != 0) { 13 | printf("Parent process, PID: %d\n", (int) self); 14 | printf("Child process ID: %d\n", (int) child); 15 | } else { 16 | printf("This is the child process. PID: %d\n", (int) getpid()); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /gnu-linux-programming/getpriority.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gnu-linux-programming/getpriority.c -------------------------------------------------------------------------------- /gnu-linux-programming/helo.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int x = 10; 4 | int y = 20; 5 | int z = 30; 6 | 7 | extern int __data_start; 8 | 9 | int main(void) 10 | { 11 | int *ds = &__data_start; 12 | 13 | printf("%p\n", ds); 14 | printf("now x = %d\n", x); 15 | 16 | ds+=3; 17 | *ds = 100; 18 | printf("now x = %d\n", x); 19 | } 20 | -------------------------------------------------------------------------------- /gnu-linux-programming/ipc/mmap_read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define FILE_LENGTH 0x400 7 | 8 | int main() 9 | { 10 | int fd; 11 | void *map_memory; 12 | char buf[FILE_LENGTH]; 13 | 14 | /* Open mapped file. */ 15 | fd = open("/tmp/shared_file", O_RDWR, S_IRUSR | S_IWUSR); 16 | 17 | /* Create mapped memory. */ 18 | map_memory = mmap(0, FILE_LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 19 | close(fd); 20 | 21 | /* Read from mapped memory. */ 22 | sscanf((char *)map_memory, "%s", buf); 23 | printf("Read: %s\n", buf); 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /gnu-linux-programming/ipc/pipe_shell.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | FILE *stream; 8 | int fds[2]; 9 | int i; 10 | pid_t child; 11 | char *arg_list[] = { "more", NULL }; 12 | 13 | /* create a pipe */ 14 | pipe(fds); 15 | 16 | /* fork a child process */ 17 | child = fork(); 18 | 19 | if (child != 0) { 20 | close(fds[0]); 21 | 22 | dup2(fds[1], 1); 23 | execlp("ls", "ls", "-l", "/lib", 0); 24 | 25 | waitpid(child, NULL, 0); 26 | } else { 27 | close(fds[1]); 28 | 29 | dup2(fds[0], 0); 30 | execvp("more", arg_list); 31 | } 32 | 33 | exit(0); 34 | } 35 | -------------------------------------------------------------------------------- /gnu-linux-programming/ipc/shm_allocate.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int seg_id; 8 | int seg_size = 0x4000; 9 | char *shared_memory; 10 | 11 | /* shared memory: allocate */ 12 | seg_id = shmget(IPC_PRIVATE, seg_size, 13 | IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); 14 | printf("Shared Memory Segment ID: %d\n", seg_id); 15 | 16 | /* shared memory: attach */ 17 | shared_memory = (char *)shmat(seg_id, 0, 0); 18 | 19 | /* write to shared memory */ 20 | sprintf(shared_memory, argv[1]); 21 | 22 | /* shared memory: detach */ 23 | shmdt(shared_memory); 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /gnu-linux-programming/ipc/shm_read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int seg_id; 8 | char *shared_memory; 9 | 10 | /* get shmid */ 11 | seg_id = atoi(argv[1]); 12 | 13 | /* shared memory: attach */ 14 | shared_memory = (char *)shmat(seg_id, 0, 0); 15 | 16 | /* write to shared memory */ 17 | printf("Message of Shared Memory: %s\n", shared_memory); 18 | 19 | /* shared memory: detach */ 20 | shmdt(shared_memory); 21 | 22 | /* Deallocate shared memory */ 23 | shmctl(seg_id, IPC_RMID, 0); 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /gnu-linux-programming/join_thread2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void *my_lower(void *argu) 6 | { 7 | char x; 8 | 9 | x = *((char *)argu); 10 | x |= 0x20; 11 | 12 | return (void *)x; 13 | } 14 | 15 | int main() 16 | { 17 | char x = 'R'; 18 | pthread_t thread_id; 19 | 20 | pthread_create(&thread_id, NULL, &my_lower, &x); 21 | pthread_join(thread_id, (void *)&x); 22 | 23 | printf("Return Value: %c\n", x); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /gnu-linux-programming/pid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | pid_t self, parent; 8 | 9 | self = getpid(); 10 | parent = getppid(); 11 | 12 | printf("PID: %d, Parent PID: %d\n", (int) self, (int) parent); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /gnu-linux-programming/setpriority.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | int errno, prio; 10 | pid_t pid; 11 | 12 | if (argc != 3) { 13 | printf("Usage: %s [pid] [priority (-20~20)]\n", argv[0]); 14 | return -1; 15 | } 16 | 17 | pid = atoi(argv[1]); 18 | prio = atoi(argv[2]); 19 | errno = setpriority(PRIO_PROCESS, pid, prio); 20 | 21 | printf("OK.\n"); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /gnu-linux-programming/shm_race/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | BINS = shm_allocate shm_read shm_write 3 | 4 | all: $(BINS) 5 | 6 | clean: 7 | rm -rf $(BINS) 8 | -------------------------------------------------------------------------------- /gnu-linux-programming/shm_race/README: -------------------------------------------------------------------------------- 1 | 2 | See: http://www.jollen.org/blog/2007/01/shared_memory_race_condition.html 3 | 4 | 5 | -------------------------------------------------------------------------------- /gnu-linux-programming/shm_race/shm_allocate.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int seg_id; 8 | int seg_size = 0x4000; 9 | char *shared_memory; 10 | 11 | /* shared memory: allocate */ 12 | seg_id = shmget(IPC_PRIVATE, seg_size, 13 | IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); 14 | printf("Shared Memory Segment ID: %d\n", seg_id); 15 | 16 | /* shared memory: attach */ 17 | shared_memory = (char *)shmat(seg_id, 0, 0); 18 | 19 | /* write to shared memory */ 20 | sprintf(shared_memory, argv[1]); 21 | 22 | /* shared memory: detach */ 23 | shmdt(shared_memory); 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /gnu-linux-programming/shm_race/shm_read.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int seg_id; 8 | char *shared_memory; 9 | 10 | /* get shmid */ 11 | seg_id = atoi(argv[1]); 12 | 13 | /* shared memory: attach */ 14 | shared_memory = (char *)shmat(seg_id, 0, 0); 15 | 16 | /* read shared memory */ 17 | for (;;) { 18 | printf("Message of Shared Memory: %s\n", shared_memory); 19 | sleep(1); 20 | } 21 | 22 | /* shared memory: detach */ 23 | shmdt(shared_memory); 24 | 25 | exit(0); 26 | } 27 | -------------------------------------------------------------------------------- /gnu-linux-programming/shm_race/shm_write.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | int i; 8 | int seg_id; 9 | char *shared_memory; 10 | 11 | /* get shmid */ 12 | seg_id = atoi(argv[1]); 13 | 14 | /* shared memory: attach */ 15 | shared_memory = (char *)shmat(seg_id, 0, 0); 16 | 17 | /* write to shared memory */ 18 | for (i = 0; ;i++, i %= 10) { 19 | sprintf(shared_memory, "%d%d%d%d%d%d%d%d", i,i,i,i,i,i,i,i); 20 | } 21 | 22 | exit(0); 23 | } 24 | -------------------------------------------------------------------------------- /gnu-linux-programming/signal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void sighup_handler() 7 | { 8 | printf("Receive SIGHUP signal.\n"); 9 | } 10 | 11 | int main() 12 | { 13 | struct sigaction sa; 14 | 15 | memset(&sa, 0, sizeof(sa)); 16 | sa.sa_handler = &sighup_handler; 17 | sigaction(SIGHUP, &sa, NULL); 18 | 19 | while (1); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /gnu-linux-programming/thread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void *print_hello(void *argu) 5 | { 6 | while (1) { 7 | printf("Hello,\n"); 8 | sleep(1); 9 | } 10 | return NULL; 11 | } 12 | 13 | void *print_world(void *argu) 14 | { 15 | while (1) { 16 | printf("World!\n"); 17 | sleep(1); 18 | } 19 | return NULL; 20 | } 21 | 22 | int main() 23 | { 24 | pthread_t thread_id1, thread_id2; 25 | 26 | pthread_create(&thread_id1, NULL, &print_hello, NULL); 27 | pthread_create(&thread_id2, NULL, &print_world, NULL); 28 | 29 | while (1) { 30 | printf("-------------------------\n"); 31 | sleep(1); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /gnu-linux-programming/unix_spawn.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gnu-linux-programming/unix_spawn.c -------------------------------------------------------------------------------- /gnu-linux-programming/wait.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/gnu-linux-programming/wait.c -------------------------------------------------------------------------------- /gnu-linux-programming/zombie.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | pid_t child; 8 | 9 | child = fork(); 10 | 11 | if (child != 0) { 12 | sleep(60); 13 | } else { 14 | return 0; 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /ibm/connection-oriented-socket/readme: -------------------------------------------------------------------------------- 1 | http://publib.boulder.ibm.com/infocenter/iseries/v5r3/topic/rzab6/rzab6connectionor.htm 2 | -------------------------------------------------------------------------------- /ibm/connectionless-socket/readme: -------------------------------------------------------------------------------- 1 | http://publib.boulder.ibm.com/infocenter/iseries/v5r3/topic/rzab6/rzab6connectionless.htm 2 | -------------------------------------------------------------------------------- /localtime.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, const char *argv[]) 5 | { 6 | time_t timep; 7 | struct tm *p; 8 | time(&timep); 9 | p=localtime(&timep); 10 | printf("%d/%d/%d ",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday ); 11 | printf("%d:%d:%d\n", p->tm_hour, p->tm_min, p->tm_sec); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /lockfile/C_Bash_Lock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | //#include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | //Touch and Remove "./touch_file" by yourself to run this program 11 | int main(int argc, char *argv[]) 12 | { 13 | FILE *f; 14 | 15 | f = fopen("./touch_file", "r"); 16 | while(f) 17 | { 18 | printf("file exist f= %d\n",f); 19 | fclose(f); 20 | sleep(1); 21 | f = fopen("./touch_file", "r"); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /lockfile/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() { 4 | FILE *fp=fopen("asd","w"); 5 | #if 1 6 | printf("ret =%d\n",ftrylockfile(fp)); 7 | flockfile(fp); 8 | getchar(); 9 | funlockfile(fp); 10 | #endif 11 | fclose(fp); 12 | } 13 | -------------------------------------------------------------------------------- /lockfile/b.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() { 3 | FILE *f=fopen("asd","w"); 4 | printf("ftrylockfile = %d\n",ftrylockfile(f)); 5 | flockfile(f); 6 | getchar(); 7 | fprintf(f,"shitshishit"); 8 | funlockfile(f); 9 | fclose(f); 10 | } 11 | -------------------------------------------------------------------------------- /lockfile/fwrite.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/lockfile/fwrite.txt -------------------------------------------------------------------------------- /lockfile/lockf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | FILE *fp=fopen("lockf.txt","a+"); 8 | int ret = 0; 9 | while((ret = lockf(fileno(fp), F_TLOCK, 0)) < 0) 10 | { 11 | printf("lockf trylock ret = %d\n",ret); 12 | sleep(1); 13 | } 14 | printf("lock success\n"); 15 | sleep(3); 16 | 17 | time_t now; 18 | struct tm *tms; 19 | time(&now); 20 | tms = localtime(&now); 21 | printf("%02d:%02d:%02d\n", tms->tm_hour, tms->tm_min, tms->tm_sec); 22 | 23 | fprintf(fp,"Time : %02d:%02d:%02d\n", tms->tm_hour, tms->tm_min, tms->tm_sec); 24 | getchar(); 25 | lockf(fileno(fp), F_ULOCK, 0); 26 | fclose(fp); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /lockfile/lockf.txt: -------------------------------------------------------------------------------- 1 | Time : 12:35:13 2 | Time : 12:35:17 3 | Time : 14:20:58 4 | -------------------------------------------------------------------------------- /popen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | FILE *fp; 6 | char buf[256]; 7 | int line=1; 8 | fp = popen("ls -al" ,"r"); 9 | while (fgets(buf,256,fp) != NULL) 10 | { 11 | printf("%d: %s", line++, buf); 12 | } 13 | 14 | pclose(fp); 15 | } 16 | -------------------------------------------------------------------------------- /readdevstdin_list_authors.c: -------------------------------------------------------------------------------- 1 | // http://blog.linux.org.tw/~jserv/archives/2011/06/include_devstdi.html 2 | // Compile by: 3 | // echo '"jserv, chenkaie"' | gcc list_authors.c 4 | 5 | #include 6 | 7 | int main() { 8 | puts( 9 | #include "/dev/stdin" 10 | ); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /ringbuffer/README.txt: -------------------------------------------------------------------------------- 1 | http://en.wikipedia.org/wiki/Circular_buffer 2 | -------------------------------------------------------------------------------- /segfault.c: -------------------------------------------------------------------------------- 1 | //Reference: http://en.wikipedia.org/wiki/Segmentation_fault 2 | #include 3 | #include 4 | 5 | #define SEG 1 //0, 1, 2 6 | 7 | int main(void) 8 | { 9 | 10 | #if SEG == 0 11 | puts("\n"); 12 | char *s = "hello world"; 13 | *s = 'H'; 14 | #endif 15 | 16 | #if SEG == 1 17 | puts("\n"); 18 | int *ptr = NULL; 19 | *ptr = 1; 20 | #endif 21 | 22 | #if SEG == 2 23 | puts(""); 24 | main(); 25 | #endif 26 | 27 | } 28 | 29 | -------------------------------------------------------------------------------- /set_get_priority/getpriority.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | int prio_process, prio_pgroup, prio_user; 10 | pid_t pid; 11 | 12 | if (argc != 2) 13 | return -1; 14 | 15 | pid = atoi(argv[1]); 16 | prio_process = getpriority(PRIO_PROCESS, pid); 17 | 18 | printf("Process (%d) Priority is %d.\n", pid, prio_process); 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /set_get_priority/setpriority.c: -------------------------------------------------------------------------------- 1 | /* Program: setpriority.c */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | int errno, prio; 12 | pid_t pid; 13 | 14 | if (argc != 3) { 15 | printf("Usage: %s [pid] [priority (-20~19)]\n", argv[0]); 16 | return -1; 17 | } 18 | pid = atoi(argv[1]); 19 | prio = atoi(argv[2]); 20 | errno = setpriority(PRIO_PROCESS, pid, prio); 21 | 22 | printf("OK. errno = %d\n", errno); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /set_get_priority/testecho.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | random=1 3 | while [ 1 ] 4 | do 5 | 6 | echo $random 7 | random=`expr $random + 1` 8 | usleep 1000000 9 | 10 | done 11 | 12 | 13 | -------------------------------------------------------------------------------- /strstr.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | main() 5 | { 6 | char kent[16] = ""; 7 | char * s="aaa&method=get&name=ccc&test=aaa"; 8 | char *a, *b, *p, *q; 9 | printf("%d, %02x\n",('\0' == kent[0]), kent[0]); 10 | printf("%d, %02x\n",('\0' == ""), '\0'); 11 | printf("%d, %02x\n",(kent[0] == ""), ""); 12 | a =strchr(s,'&'); 13 | b =strchr(s,'='); 14 | 15 | p= strstr(s,"name="); 16 | q= strstr(p,"&"); 17 | printf("%c, %s\n",p[0], q); 18 | } 19 | -------------------------------------------------------------------------------- /vvtk-template/function: -------------------------------------------------------------------------------- 1 | /*! 2 | ********************************************************************* 3 | * \brief 4 | * Description the functionality of this API. 5 | * 6 | * \param phObject 7 | * \a (o) pointer to receive the instance's handle of this object. 8 | * 9 | * \param Param2 10 | * \a (i) description of parameter 2 11 | * 12 | * \retval S_OK 13 | * Initialize \b Module OK. 14 | * \retval S_FAIL 15 | * Initialize \b Module failed. 16 | * 17 | * \remark 18 | * Remarks of this API. 19 | * 20 | * \see See also to other functions 21 | * 22 | ******************************************************************** 23 | */ 24 | 25 | -------------------------------------------------------------------------------- /vvtk-topic3/C_Basic_Homework/01_Salary/.kdbgrc.salary: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/C_Basic_Homework/01_Salary/.kdbgrc.salary -------------------------------------------------------------------------------- /vvtk-topic3/C_Basic_Homework/01_Salary/salary: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/C_Basic_Homework/01_Salary/salary -------------------------------------------------------------------------------- /vvtk-topic3/C_Basic_Homework/06_CalculateArray/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int nbytes = 100; 6 | char *my_string; 7 | int int1, int2, int3; 8 | int args_assigned; 9 | 10 | args_assigned = 0; 11 | 12 | while (args_assigned != 3) 13 | { 14 | puts ("Please enter three integers separated by whitespace."); 15 | my_string = (char *) malloc (nbytes + 1); 16 | getline (&my_string, &nbytes, stdin); 17 | args_assigned = sscanf (my_string, "%d %d %d", &int1, &int2, &int3); 18 | if (args_assigned != 3) 19 | puts ("\nInput invalid!"); 20 | } 21 | 22 | printf ("\nThanks!\n%d\n%d\n%d\n", int1, int2, int3); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /vvtk-topic3/Handle_Base_Programming/Snapshot: -------------------------------------------------------------------------------- 1 | /home/kent/ProjectRoot/osisolate/app/Snapshot/ -------------------------------------------------------------------------------- /vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/Thumbs.db -------------------------------------------------------------------------------- /vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video1.jpg -------------------------------------------------------------------------------- /vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video2.jpg -------------------------------------------------------------------------------- /vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video3.jpg -------------------------------------------------------------------------------- /vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video4.jpg -------------------------------------------------------------------------------- /vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Handle_Base_Programming/Snapshot_NoWork/video5.jpg -------------------------------------------------------------------------------- /vvtk-topic3/Handle_Base_Programming/example/Internal.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Handle_Base_Programming/example/Internal.h.gch -------------------------------------------------------------------------------- /vvtk-topic3/Handle_Base_Programming/example/Module.h: -------------------------------------------------------------------------------- 1 | #include "header.h" 2 | SCODE InitBase(HANDLE *phHandle, TBaseOptions *ptOpt); 3 | SCODE InitDerived(HANDLE *phHandle, TDerivedOptions *ptOpt); 4 | 5 | SCODE Work1(HANDLE hHandle, int nTimes); 6 | SCODE Work2(HANDLE hHandle, const char *pszName); 7 | 8 | SCODE Release(HANDLE *phHandle); 9 | -------------------------------------------------------------------------------- /vvtk-topic3/JPEG/README: -------------------------------------------------------------------------------- 1 | Reference Web: 2 | http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/ 3 | -------------------------------------------------------------------------------- /vvtk-topic3/JPEG/RefURL.txt: -------------------------------------------------------------------------------- 1 | http://blog.hsian.org/?p=19 2 | http://blog.hsian.org/?p=13 3 | -------------------------------------------------------------------------------- /vvtk-topic3/JPEG/bin/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/JPEG/bin/Thumbs.db -------------------------------------------------------------------------------- /vvtk-topic3/JPEG/bin/sample.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/JPEG/bin/sample.gif -------------------------------------------------------------------------------- /vvtk-topic3/JPEG/bin/sample.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/JPEG/bin/sample.jpg -------------------------------------------------------------------------------- /vvtk-topic3/JPEG/jpegapp/jpeginfo.h: -------------------------------------------------------------------------------- 1 | ../jpeglib/jpeginfo.h -------------------------------------------------------------------------------- /vvtk-topic3/JPEG/samples/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/JPEG/samples/Thumbs.db -------------------------------------------------------------------------------- /vvtk-topic3/JPEG/samples/sample.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/JPEG/samples/sample.gif -------------------------------------------------------------------------------- /vvtk-topic3/JPEG/samples/sample.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/JPEG/samples/sample.jpg -------------------------------------------------------------------------------- /vvtk-topic3/Socket/01_UDP/README: -------------------------------------------------------------------------------- 1 | [HowTo] 2 | Peer1: $ ./udp_peer.out 127.0.0.1 12345 23456 3 | Peer2: $ ./udp_peer.out 127.0.0.1 23456 12345 4 | -------------------------------------------------------------------------------- /vvtk-topic3/Socket/01_UDP/udp_exercise/udp_exercise_linux.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Socket/01_UDP/udp_exercise/udp_exercise_linux.c -------------------------------------------------------------------------------- /vvtk-topic3/Socket/01_UDP/udp_exercise/udp_exercise_win32.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Socket/01_UDP/udp_exercise/udp_exercise_win32.c -------------------------------------------------------------------------------- /vvtk-topic3/Socket/04_NonBlockingTCPServer/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | declare -i i 3 | declare -i s 4 | while [ "$i" != "5" ] 5 | do 6 | s=s+i 7 | i=i+1 8 | ./TcpEchoClient.out 172.16.5.31 12345678901 7333 9 | done 10 | echo "The count is==> $s" 11 | -------------------------------------------------------------------------------- /vvtk-topic3/Socket/04_NonBlockingTCPServer_2/Makefile: -------------------------------------------------------------------------------- 1 | BIN=server client 2 | 3 | .PHONY: clean 4 | 5 | all: $(BIN) 6 | 7 | %: %.c 8 | gcc -o $@ $^ 9 | 10 | clean: 11 | rm -rf $(BIN) 12 | -------------------------------------------------------------------------------- /vvtk-topic3/Socket/04_NonBlockingTCPServer_2/Note: -------------------------------------------------------------------------------- 1 | repeat 100 "./client &" 2 | 3 | -------------------------------------------------------------------------------- /vvtk-topic3/Socket/04_NonBlockingTCPServer_2/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Socket/04_NonBlockingTCPServer_2/client -------------------------------------------------------------------------------- /vvtk-topic3/Socket/04_NonBlockingTCPServer_2/client.sleep10000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Socket/04_NonBlockingTCPServer_2/client.sleep10000 -------------------------------------------------------------------------------- /vvtk-topic3/Socket/04_NonBlockingTCPServer_2/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Socket/04_NonBlockingTCPServer_2/server -------------------------------------------------------------------------------- /vvtk-topic3/Socket/06_Broadcast/example/readme: -------------------------------------------------------------------------------- 1 | http://www.tack.ch/multicast/broadcast.shtml 2 | -------------------------------------------------------------------------------- /vvtk-topic3/Socket/07_Multicast/makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -o send send.c 3 | gcc -o receive receive.c 4 | clean: 5 | rm send receive 6 | -------------------------------------------------------------------------------- /vvtk-topic3/Socket/07_Multicast/receive: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Socket/07_Multicast/receive -------------------------------------------------------------------------------- /vvtk-topic3/Socket/07_Multicast/send: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Socket/07_Multicast/send -------------------------------------------------------------------------------- /vvtk-topic3/Socket/ProgramminLinuxSocket_Example/nslookup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | "USAGE: nslookup.py " 3 | import socket, sys 4 | print socket.gethostbyname(sys.argv[1]) 5 | -------------------------------------------------------------------------------- /vvtk-topic3/Socket/ProgramminLinuxSocket_Example/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | declare -i i 3 | declare -i s 4 | while [ "$i" != "5" ] 5 | do 6 | s=s+i 7 | i=i+1 8 | ./TcpEchoClient.out 172.16.5.31 12345678901 7333 9 | done 10 | echo "The count is==> $s" 11 | -------------------------------------------------------------------------------- /vvtk-topic3/Socket/ProgramminLinuxSocket_Example/test1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | declare -i i 3 | declare -i s 4 | while [ "$i" != "5" ] 5 | do 6 | s=s+i 7 | i=i+1 8 | ./TcpEchoClient.out 172.16.5.31 RD1-211111111111111 7333 9 | done 10 | echo "The count is==> $s" 11 | -------------------------------------------------------------------------------- /vvtk-topic3/Socket/ProgramminLinuxSocket_Example/video1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenkaie/junkcode/7134fb63eecf32fefc47d613a7f2f37d4eee05fb/vvtk-topic3/Socket/ProgramminLinuxSocket_Example/video1.jpg -------------------------------------------------------------------------------- /writev/writev1.c: -------------------------------------------------------------------------------- 1 | /* Trivial test of writev. 2 | #notarget: cris*-*-elf 3 | #output: abcdefghijklmn\npass\n 4 | */ 5 | #include 6 | #include 7 | #include 8 | 9 | #define X(x) {x, sizeof (x) -1} 10 | struct iovec v[] = { 11 | X("a"), 12 | X("bcd"), 13 | X("efghi"), 14 | X("j"), 15 | X("klmn\n"), 16 | }; 17 | 18 | int main (void) 19 | { 20 | if (writev (1, v, sizeof v / sizeof (v[0])) != 15) 21 | abort (); 22 | 23 | printf ("pass\n"); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /writev/writev2.c: -------------------------------------------------------------------------------- 1 | /* Trivial test of failing writev: invalid file descriptor. 2 | #notarget: cris*-*-elf 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define X(x) {x, sizeof (x) -1} 10 | struct iovec v[] = { 11 | X("a"), 12 | X("bcd"), 13 | X("efghi"), 14 | X("j"), 15 | X("klmn\n"), 16 | }; 17 | 18 | int main (void) 19 | { 20 | if (writev (99, v, sizeof v / sizeof (v[0])) != -1 21 | /* The simulator write gives EINVAL instead of EBADF; let's 22 | cope. */ 23 | || (errno != EBADF && errno != EINVAL)) 24 | abort (); 25 | 26 | printf ("pass\n"); 27 | return 0; 28 | } 29 | --------------------------------------------------------------------------------