C言語

glob.cのコントリビューター

libtarのglob.cは、元はOpenBSDのものらしい。 で、何となくヘッダのコメントを見ていたら「This code is derived from software contributed to Berkeley by Guido van Rossum」とか書いてあった。 へーへーへー。Wikipediaにもちゃんと載ってる。

glob

libtarのWindowsへの移植を始めたが、早速glob(3)でつまずいた。 libtarは自前でglobを用意しているけど、Windowsには移植しにくい感じ…。 RubyやGaucheは自前で実装しているみたい。 Windows用のglobってどっかにないかなぁ。

link: パス フィールドの制限

http://msdn.microsoft.com/ja-jp/library/930f87yf(VS.80).aspx

gccかどうかを判定する

少なくともCygwin上のgccでは判定できた。 int main() { #ifdef __GNUC__ puts("gcc"); #else puts("not gcc") #endif }

link: テンポラリファイルから情報が漏れる

http://www.ipa.go.jp/security/awareness/vendor/programming/b07_08_main.html メモメモ。

link: Manpage of MAKECONTEXT

http://www.linux.or.jp/JM/html/LDP_man-pages/man3/makecontext.3.html Cygwinでは動かず。残念。 なんか、実際にアプリで使われているコードはないかな… #include <ucontext.h> #include <stdio.h> #include <stdlib.h> #define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE);</stdlib.h></stdio.h></ucontext.h>…

GNU Pthでコルーチン

Cygwinで動いた。 GNU Pthが一般的なのかな…libfiberもあったけど。 #include <stdio.h> #include <pth.h> void *handler(void *arg) { pth_t mth = (pth_t) arg; while (1) { puts("spring"); pth_yield(mth); puts("summer"); pth_yield(mth); puts("autumn"); pth_yield(m</pth.h></stdio.h>…

共有メモリを使う

OSの差異はどうとでも吸収できそう。 #include <windows.h> #include <stdio.h> #include <stdlib.h> #define SHM_SIZE 256 #define die(fmt, ...) \ do { \ fprintf(stderr, fmt, __VA_ARGS__); \ exit(1); \ } while(0) int main() { HANDLE hShm; void *p; hShm = CreateFileMappingA((H</stdlib.h></stdio.h></windows.h>…

Fiberでコルーチン

RubyじゃなくてWindows APIのほう。 Unixだとノンプリエンティブなスレッドってどうやって使うんだろう? #include <windows.h> #include <stdio.h> #include <stdlib.h> #define die(fmt, ...) \ do { \ fprintf(stderr, fmt, __VA_ARGS__); \ exit(1); \ } while(0) void _stdcall fiber(</stdlib.h></stdio.h></windows.h>…

とりあえず共有メモリを使ってみる

開いて閉じるだけ。munmapは必要なのかな? #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/mman.h> #include <errno.h> #define SHM_NAME "foo" #define SHM_SIZE 256 int main() { int fd; char *p; if ((fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666)) </errno.h></sys/mman.h></sys/types.h></unistd.h></fcntl.h></stdlib.h></stdio.h>…

共有メモリ

それはそれとして、共有メモリは面白そうだから、今度調べてみよう。 shm_openはWindowsではなんだろう?追記 google:Windows 共有メモリ

screenのコピー&ペーストをWindowsのクリップボードと同期させる

Cygwinでscreenを使っているが、Windowsのクリップボードとscreenのコピペが同期したら便利かも…と思ったので少しいじってみた。 クリップボードの操作 まず、クリップボードを操作する関数を書く。 (see http://nienie.com/~masapico/api_GetClipboardData.…

Info-ZIPのCRC32とzlibのcrc32

Info-ZIPのCRC32とzlibのcrc32でなぜか戻り値が違う。 printf("%x", CRC32(305419896L, 'x')); //=> 123456 printf("%x", crc32(305419896L, "x", 1)); //=> d210dbdb で、zlibのcrc32のソースを読む。 /* ================================================…

Traditional PKWARE Encryptionの復号処理を書いてみる

データを処理してみないとなんとも。 #include <string.h> #include "zlib.h" void init_keys(uLong *keys) { keys[0] = 305419896L; keys[1] = 591751049L; keys[2] = 878082192L; } void update_keys(uLong *keys, char c) { keys[0] = crc32(keys[0], &c, 1); keys[</string.h>…

off_tがらみのバグ

off_tがらみのバグが。 ruby.hのインクルードを最後にしたら直った。どっかで2重に定義されてる?

CRTのrename

http://practical-scheme.net/wiliki/wiliki.cgi?Gauche%3aWindows%2fVC%2b%2b%3alog%3adetail_2005_2#H-igextc 同じ名前のファイルがあっても上書きしてくれないらしい。しかたないのでzip_close.cのrenameを置き換え。 #ifdef _WIN32 #include <windows.h> #include <io.h> </io.h></windows.h>…

libzip win32 patch(修正版)

アーカイブを作るとやたらとCRCエラーが出るので、なんで?なんで?と思ってたら、fwriteでバイナリデータの改行コードが書き換えられていたせいだった…orzfopenではバイナリモードで開いているので、openを修正。 #define open(p, f, m) _open((p), ((f) | …

ICUで和暦

こちらのエントリからコードを拝借。コードページとか。 C++難しいなぁ…。ちゅーか元号変わったら、ICUバージョンアップするのかな? #include <string> #include <iostream> #include "unicode/calendar.h" #include "unicode/smpdtfmt.h" void format(char *dst, const char </iostream></string>…

StringValuePtrとStringValueCStr

StringValueCStrは使うことあるかなー? char * rb_string_value_ptr(ptr) volatile VALUE *ptr; { return RSTRING(rb_string_value(ptr))->ptr; } char * rb_string_value_cstr(ptr) volatile VALUE *ptr; { VALUE str = rb_string_value(ptr); char *s = R…

C++の関数をCから呼び出す

zoo.cpp #include <iostream> extern "C" { int add(int a, int b); } using namespace std; int add(int a, int b) { cout << "add()" << endl; return a + b; } bar.c #include <stdio.h> int add(int a, int b); int main() { printf("%d\n", add(1, 2)); } ビルド/実行 ~$ g</stdio.h></iostream>…

libzipをwin32に移植中

libzipをwin32に移植中。 とりあえず、ビルドは通ったので要点を整理。 config.hはconfig.h.inをリネーム typedef int ssize_t strcasecmpはstricmpで代用 _getpid #define getpid _getpid typedef int pid_t; S_ISDIR #ifndef S_ISDIR #define S_ISDIR(m) (…

libzip win32 patch

※このパッチは問題があります。修正版を作成しました。 しばらく動かしてみて問題なければパッチ送ろう。 diff -rup libzip-0.8.orig/lib/mkstemp.c libzip-0.8/lib/mkstemp.c --- libzip-0.8.orig/lib/mkstemp.c 2007-05-16 10:41:34.000000000 +0900 +++ l…

POSIX Threads for Win32

http://sourceware.org/pthreads-win32/ とりあえずメモだけ。

svn_string.c

http://svn.collab.net/repos/svn/trunk/subversion/libsvn_subr/svn_string.c APRのメモリプールを使って、Stringを実現しようとしている…ように見える。 apr_preallocってないのか…古いメモリは破棄できないのかな?

echod_f: 標準入出力関数を使ってechoサーバ

recv(2)、send(2)を使わない。fdopenしたときに、close/fcloseの両方呼び出すべきなのかどうか、はっきりしないなぁ。 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/types.h> #include <unistd.h> #include <errno.h> #define ECHO_PORT 7 #define MAX_BACKLOG 5 #define</errno.h></unistd.h></sys/types.h></arpa/inet.h></sys/socket.h></string.h></stdlib.h></stdio.h>…

WinSock: socketにfgetsを使えない… と思ったら出来た

_open_osfhandleしてfdopenすれば使えると思ったのに。 追ってみると、ReadFileでコケてるみたい。 ひょっとしてサーバ側だと使えないのかな?うーん… socketじゃなくてWSASocketを使ったらできた! #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> #include <io.h> #include <fcntl.h> #inc</fcntl.h></io.h></stdio.h></ws2tcpip.h></winsock2.h>…

libevを使った低パフォーマンスなCOMETサーバ(試作)

「これはひどい」だ…orz。 my_commetd.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <alloca.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <ev.h> #define PORT 10080 #define MAX_BACKLOG 5 #define …</ev.h></errno.h></fcntl.h></unistd.h></sys/types.h></arpa/inet.h></sys/socket.h></alloca.h></string.h></stdlib.h></stdio.h>

Safe C String Library v1.0.3

http://www.zork.org/safestr/ なんかライブラリがありそうだと思ったら、あった。ほかにもいろいろあるみたい。 使ってみよう。 追記 こんなのも。 (via IPA ISEC セキュア・プログラミング講座:C/C++言語編 第1章 総論:ツールの利用)

XXL v1.0.1 (January 30, 2005)

http://www.zork.org/xxl/ 面白いけど、言語が変わってしまいそうで、使うのをためらうな…

Safe C String Libraryを使ってみる

フツーに使える。よいかも… #include <stdio.h> #include "safestr.h" int main() { safestr_t str; str = safestr_create("hello, world", 0); printf("%s\n", str); safestr_append(&str, SAFESTR_TEMP(". hello, safestr.")); printf("%s\n", str); safestr_replac</stdio.h>…