文章分类 » code

backup:在linux下使用clang的闭包特性

FROM:http://stackoverflow.com/questions/5907071/clang-block-in-linux
On Ubuntu Linux:

sudo apt-get install llvm
sudo apt-get install clang
sudo apt-get install libblocksruntime-dev

test.c:

#include 

int main() {
    void (^hello)(void) = ^(void) {
        printf("Hello, block!\n");
    };
    hello();
    return 0;
}

compile:

clang test.c -fblocks -lBlocksRuntime -o test
./test

Hello, block!

works fine.

backup:linux下不输密码执行高权限命令

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, const char **argv) {
    if (setuid(0) != 0) {
        fprintf(stderr, "setuid error\n");
        return 1;
    }
    char *oldpath = getenv("PATH");
    char *tmp = malloc(sizeof(char) * strlen(oldpath) + 100);
    tmp[0] = '\0';
    strcat(tmp, oldpath);
    strcat(tmp, ":/sbin:/usr/sbin");
    setenv("PATH", tmp, 1);
    free(tmp);
    execl("/usr/bin/pon", "/usr/bin/pon", "myvpn", NULL);
    return 0;
}
gcc conn_vpn.c -o conn_vpn
sudo chown root:root conn_vpn
sudo chmod +s conn_vpn

backup:alexa query

#!/usr/bin/env python

import sys
import urllib
import re
import Queue
import threading
import time
import socket

RANK = re.compile(r'<img src="/images/icons/globe-sm.jpg" alt="Global" style="margin-bottom:-2px;"/>([^<]+)</div>')
CHINA = re.compile(r'<img class="dynamic-icon"[^>]+"China Flag"/>([^<]+)</div>')
WWW = re.compile(r'^www\.')
def get_alexa(domain):
    d = WWW.sub('', domain)
    html = urllib.urlopen('http://www.alexa.com/siteinfo/' + d).read()
    rt = RANK.findall(html)
    output = domain + ','
    if len(rt):
        output += rt[0].strip().replace(',', '')
    else:
        output += '-'
    output += ','
    rt = CHINA.findall(html)
    if len(rt):
        output += rt[0].strip().replace(',', '')
    else:
        output += '-'
    output += '\n'
    sys.stdout.write(output)


class Alexa(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    
    def run(self):
        while True:
            domain = self.queue.get()
            try:
                get_alexa(domain)
            except socket.error:
                time.sleep(2)
                self.queue.put(domain)
                self.queue.task_done()
                continue
            except:
                self.queue.task_done()
                continue
            self.queue.task_done()

queue = Queue.Queue()
for i in xrange(20):
    t = Alexa(queue)
    t.setDaemon(True)
    t.start()

for line in open('./input.txt'):
    queue.put(line.strip())
queue.join()

backcup:unbuffer implementation in python

#!/usr/bin/env python
import os,sys,pty
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)

pid, fd = pty.fork()
if pid < 0:
    print >>sys.stderr, 'fork error'
#child
elif pid == 0:
    os.execvp(sys.argv[1], sys.argv[1:])
#parent
else:
    while True:
        try:
            s = os.read(fd, 1)
        except OSError, e:
            break
        if s == '':
            break
        sys.stdout.write(s)
    os.wait()

backup:window批处理随机乱序文件内容

@echo off
setlocal enabledelayedexpansion

set input=.\input.txt
set output=.\output.txt

if exist .\tmp.txt del .\tmp.txt
if exist .\tmp2.txt del .\tmp2.txt
if exist %output% del %output%

for /F "delims=" %%i in (%input%) do (
	echo !random!#%%i >> .\tmp.txt
)

sort .\tmp.txt > .\tmp2.txt
del .\tmp.txt

for /F "tokens=2 delims=#" %%i in (.\tmp2.txt) do (
	echo %%i >> %output%
)
del .\tmp2.txt
pause