fileno

filenoは、指定されたファイルポインタの指すストリームのファイルディスクリプタを返す。

Man page of FERROR

stdio.hだが、標準関数一覧には何故かない。
fileno.cで、

stdin, stdout, stderr, そして新たに開いた"test.txt"のファイルディスクリプタを出力。

$ vi fileno.c 
$ gcc fileno.c -o fileno
$ ./fileno 
fileno stdin = 0
fileno stdout = 1
fileno stderr = 2
fileno fp = 3
$

0, 1, 2, 3, と割り振られている。

Sample/unixsyscall/fileno/fileno/src/fileno at master · bg1bgst333/Sample · GitHub

perror

perrorは、OSが返すシステムエラーメッセージを出力する。

Man page of PERROR

システムコールっぽいが標準関数。
perror.cで、

存在しないファイルを開こうとしてわざと失敗させる。

$ vi perror.c
$ gcc perror.c -o perror
$ ./perror 
fopen error!: No such file or directory
$

"fopen error!"はプログラム側で指定した出力。
"No such file or directory"はOS側のシステムエラーメッセージ。

Sample/c/perror/perror/src/perror at master · bg1bgst333/Sample · GitHub

iconv

iconvは、文字コード変換をする。

Man page of ICONV

「どの文字コードからどの文字コードへ変換するか」といった情報を指定して、変換ディスクリプタを取得しないといけない。

test1.txtを読み込みbufに格納。
"utf-8"から"Shift_JIS"へ変換するディスクリプタiconv_dscを取得。
iconvにiconv_dsc、bufのポインタ、bufの長さなどを渡すと、buf2_ptrに変換後の文字列ポインタが入るので、これをtest2.txtとして保存。

$ vi iconv.c 
$ cat test1.txt 
あいうえお
$ gcc iconv.c -o iconv
$ ./iconv 
iconv error!
iconv error detail: Invalid or incomplete multibyte or wide character
$ vi test1.txt 
$ cat test1.txt 
あかさたな
$ gcc iconv.c -o iconv
$ ./iconv 
$ ls
iconv  iconv.c  test1.txt  test2.txt
$ cat test2.txt 
縺ゅ°縺輔◆縺ェ$ 
$ cat test2.txt | iconv -f utf8 -t Shift_JIS
あかさたな$ 
$

test2.txtはShift_JISになってしまって文字化けしてるので、utf8に戻して表示。
たしかに文字コード変換できてる。

Sample/unixsyscall/iconv/iconv/src/iconv at master · bg1bgst333/Sample · GitHub

stat

statは、ファイルの情報や状態を取得する。

Man page of STAT

指定されたパスのファイル情報をstat構造体に格納する。

"test.txt"のファイルサイズを調べる。

$ ls
stat  stat.c  test.txt
$ vi test.txt 
$ ls -al
合計 28
drwxrwxr-x. 2 h.miura h.miura 4096  8月 14 11:10 .
drwxrwxr-x. 3 h.miura h.miura 4096  8月 14 10:53 ..
-rwxrwxr-x. 1 h.miura h.miura 8736  8月 14 11:05 stat
-rw-rw-r--. 1 h.miura h.miura  712  8月 14 11:05 stat.c
-rw-rw-r--. 1 h.miura h.miura    6  8月 14 11:10 test.txt
$ hexdump -C test.txt 
00000000  41 42 43 44 45 0a                                 |ABCDE.|
00000006
$ vi stat.c 
$ gcc stat.c -o stat
$ ./stat 
sst.st_size = 6
$

ls -alでもわかるが、いちおうhexdump -C test.txtでバイナリの中身まで確認。
そして、最終的にstatでもサイズが6であることが証明された。

Sample/unixsyscall/stat/stat/src/stat at master · bg1bgst333/Sample · GitHub

Interpreter

Interpreterパターンは、翻訳者(Interpreter)によって翻訳された指示に従って処理する構造。

23.Interpreter パターン | TECHSCORE(テックスコア)

main.cppで、

式を解析して、計算結果を出力する簡単なインタプリター実装した。

$ 
$ vi main.cpp 
$ g++ -o main main.cpp term.cpp expression.cpp 
$ ./main 
str = ((1 + 2) * 3 - 4) / 5
str = ((1 + 2) * 3 - 4) / 5
str = ((1 + 2) * 3 - 4)
str = (1 + 2) * 3
str = (1 + 2) * 3
str = (1 + 2)
str = 1
str = 1
operand1_str = 1
operand2_str = 
operator_ = 
operand1_str = 1
operand2_str = 
operator_ = 
str = 2
str = 2
operand1_str = 2
operand2_str = 
operator_ = 
operand1_str = 2
operand2_str = 
operator_ = 
operand1_str = 1
operand2_str = 2
operator_ = +
str = 3
str = 3
operand1_str = 3
operand2_str = 
operator_ = 
operand1_str = 3
operand2_str = 
operator_ = 
operand1_str = (1 + 2)
operand2_str = 3
operator_ = *
operand1_str = (1 + 2) * 3
operand2_str = 
operator_ = 
str = 4
str = 4
operand1_str = 4
operand2_str = 
operator_ = 
operand1_str = 4
operand2_str = 
operator_ = 
operand1_str = (1 + 2) * 3
operand2_str = 4
operator_ = -
str = 5
str = 5
operand1_str = 5
operand2_str = 
operator_ = 
operand1_str = 5
operand2_str = 
operator_ = 
operand1_str = ((1 + 2) * 3 - 4)
operand2_str = 5
operator_ = /
operand1_str = ((1 + 2) * 3 - 4) / 5
operand2_str = 
operator_ = 
result = 1
$

とりあえず、この場合は動いた。

Sample/designpattern/interpreter/interpreter/src/interpreter at master · bg1bgst333/Sample · GitHub

Command

Commandパターンは、条件と要求を渡し、条件が整ったら、要求を実行してもらうような構造。

22.Commandパターン | TECHSCORE(テックスコア)

main.cppで、

"print"と"exit"コマンドを追加。

$ vi main.cpp 
$ g++ -o main main.cpp command_loop.cpp print_command.cpp exit_command.cpp 
$ ./main 
print ABCDE
>>ABCDE
print XYZ
>>XYZ
exit
$ ./main 
hoge
not found!
$

"print"コマンドだと、">>"を付けて文字列を返すし、exitコマンドだと、コマンドループを終了する。

Sample/designpattern/command/command/src/command at master · bg1bgst333/Sample · GitHub