Skip to content

Instantly share code, notes, and snippets.

View bachue's full-sized avatar

Bachue Zhou bachue

View GitHub Profile
@bachue
bachue / proc_file_read_test.c
Created December 17, 2015 06:30
As you can see, when you read file in /proc, any error could happen. E.g. When you read the status from a process, you may get ESRCH
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <linux/limits.h>
#define ERROR_PRINTF(fmt, args...) {\
fprintf(stderr, fmt, args); \
@bachue
bachue / rw_lock_test.c
Created December 16, 2015 16:10
Test POSIX thread rwlock
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#define ERROR_PRINTF(fmt, args...) { \
fprintf(stderr, fmt, args); \
@bachue
bachue / download.rb
Created January 25, 2015 05:23
OpenLanguage english tutorials downloader
require 'httparty' # `gem install httparty`
require 'cgi'
require 'digest/md5'
require 'nokogiri' # `gem install nokogiri`
require 'pathname'
require 'set'
ROOT = Pathname File.expand_path(__dir__)
DOWNLOAD_PATH = ROOT.join 'Downloads'
@bachue
bachue / sshtunnel.sh
Last active August 29, 2015 14:10
An utility to create ssh tunnel
sshtunnel() {
OPTIND=1
PROGRAM=$0
# Common default parameters
PROXY_USER=`whoami`
PROXY_SSH_PORT=22
MONITOR_PORT=18524
PROXY_PORT=8080
@bachue
bachue / install_linux_manpages.sh
Last active August 29, 2015 14:08
Script to install Linux manpage to Mac OS X
#/bin/bash
cd `mktemp -dt tmp`
git clone http://git.kernel.org/pub/scm/docs/man-pages/man-pages
cd man-pages
for f in man?/**/*.?; do mv -v $f `dirname $f`/linux.`basename $f`; done
sudo make install
cd ..
rm -rf man-pages
@bachue
bachue / check_duplicates.rb
Created September 6, 2014 09:58
文件去重复
require 'digest/md5'
Dir['**/*'].
select {|f| File.file? f }.
group_by {|f| Digest::MD5.hexdigest(File.binread(f)).tap {|md5| STDERR.puts "#{f}: #{md5}" } }.
reject {|_, list| list.size == 1 }.
each {|_, v| puts "---\n#{v.join("\n")}" }
@bachue
bachue / test1_ok.go
Last active August 29, 2015 14:05
Golang Interface Match
package main
import (
"fmt"
)
type Handler func(int)
func test(handler Handler) {
handler(1)
@bachue
bachue / int8(float64).go
Last active August 29, 2015 14:04
Go could throw exception when type overflow, but not always.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("%d\n", int8(math.Pow(2, 10))) // => 0
}
@bachue
bachue / https_server.rb
Last active August 29, 2015 14:02
To create a basic HTTPSServer by Ruby
#!/usr/bin/ruby
require "socket"
require "openssl"
require "thread"
listeningPort = Integer(ARGV[0])
server = TCPServer.new(listeningPort)
sslContext = OpenSSL::SSL::SSLContext.new
@bachue
bachue / example.html
Created June 10, 2014 14:21
How to enable Autocomplete in the Ace editor
<html>
<body>
<div id="editor" style="height: 500px; width: 800px">Type in a word like "will" below and press ctrl+space or alt+space to get "rhyme completion"</div>
<div id="commandline" style="position: absolute; bottom: 10px; height: 20px; width: 800px;"></div>
</body>
<script src="https://rawgithub.com/ajaxorg/ace-builds/master/src/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="https://rawgithub.com/ajaxorg/ace-builds/master/src/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
var langTools = ace.require("ace/ext/language_tools");