lsのソースコードを読む 2017

lsのソースコードを読んでみたくなったのでメモ。
lsコマンドをハックしてみよう - Yahoo! JAPAN Tech Blog

lsのソースを読みました - mfumiの日記

https://svnweb.freebsd.org/base/head/bin/ls/

当初の目標
- Visual Studio Codeでステップ実行
- ソースを書き換えてファイル名がabc.txtのファイルは表示しないようにする

色々試してMacOS Sierra 10.12.2 とxcodeでビルドとステップ実行ができたので、
Visual Studio Codeじゃなくてxcodeにした。

https://opensource.apple.com/

ファイルの取得

MacOSのlsコマンドのソースコードを取得する。
自分のMac(Sierra 10.12.2)
file_cmds-242.tar.gz
をダウンロード。
https://opensource.apple.com/tarballs/file_cmds/file_cmds-264.30.2.tar.gz

後で、
libutil-47.30.1 と Libinfo-503.30.1
も必要になった。

xcode

AppStoreから無料ダウンロード。

cocoa - Unable to build using Xcode 4 - The selected run destination is not valid for this action - Stack Overflow

ls ビルド設定 (Build Settings)

Project navigator トップ(root)の file_cmds をクリック。
2段目のfile_cmdsをクリックしてlsを選択。
f:id:kubotti:20170120153242p:plain

Debug構成

Project navigator トップ(root)の file_cmds をクリック。 2段目でfile_cmdsを選択。
Info > Configurations
で + をクリックして、Duplicate
Debugという構成を作成。
optimization
 

エラー

何回かやっていたら以下のエラーが出た。

The run destination My Mac is not valid for Running

Build Settings
Base SDKmacOS 10.12
に変更。

ClipStudioPluginSDKのビルドが出来ない[mac] - Qiita

membershipPriv.h not found

ビルドしたらエラーが出た。

/Users/kubotad/codes/ls/file_cmds-264.30.2/ls/print.c:56:10: 'membershipPriv.h' file not found

EI Capitanでsudo付けているOperation not permittedが出た時の対処法 - いつかエンジニアになりたい

https://opensource.apple.com/release/macos-10122.html
libutil-47.30.1 と Libinfo-503.30.1
をダウンロード。
解凍
http://www.ict-fractal.com/blog/2012/07/xcode.html

Search Pathsに追記

2.Build Settingsの一覧が表示されますので、Search PathsカテゴリのHeader Search Pathsという項目を見つけて下さい。

3. Header Search Pathsをダブルクリックすると項目の編集画面が開きます。
 編集画面で+ボタンをクリックすると項目が追加されますので、そこにパスを入力して下さい。
 ※パスはダブルクォーテーションで括りましょう。

Header Search Path
"../Libinfo-503.30.1/membership.subproj"
"../libutil-47.30.1"

How do I use a relative path in Xcode project settings? - Stack Overflow

実行ファイル出力先

Project navigator
に Products というディレクトリ的なものが表示されているけど実物はない。
詳細の Full Pathに以下のPathと書いてあった。

~/Library/Developer/Xcode/DerivedData/file_cmds-bocccevvdifphpdyuxszwxcbgtxo/Build/Products/Release

実行時引数

http://stackoverflow.com/questions/1698830/giving-command-line-arguments-in-xcode-in-c-program

Keyboard shortcut is 
command  shift <

メニューの
Product > Scheme > Edit Scheme...

ステップ実行のためのoptimization変更

ls was compiled with optimization - stepping may behave oddly; variables may not be available.

Optimization Level
BasicじゃなくてAllにしないと表示されない。

f:id:kubotti:20170120170909p:plain

メニューの
Product > Scheme > Edit Scheme...
で Run > Info > Build Configuration
をDebugに変更。

ls was compiled with optimization
のメッセージが消えた。

ソースコード書き換え

Linked lists - Learn C - Free Interactive C Tutorial

 // kubotti customize
    FTSENT *cur_temp = NULL;
    for (cur = list; cur; cur = cur->fts_link) {
        if (!strcmp(cur->fts_name, "kubotti.txt")){
            if(cur_temp == NULL){
                list = cur->fts_link;
                //free(cur);
            }else{
                cur_temp->fts_link = cur->fts_link;
            }
        }
        cur_temp = cur;
    }

code modification. not display if the name is kubotti.txt · kubotti/ls_cmd_mod@816e9a6 · GitHub

動作確認

~/Library/Developer/Xcode/DerivedData/file_cmds-boccc ....
にできたlsの実行ファイルを適当なディレクトリにコピー。

./ls /tmp/dir1

で実行。

kubotti.txt というファイルは表示しないlsコマンドができた。

f:id:kubotti:20170120231443p:plain

以上、終了。

(メモ)Visual Studio Code

最初にVisual Studio Codeを試した時のメモ。

.tar.gzを解凍して、Visual Studio Codeで開いた。
C/C++ extensionをインストールすることを推奨 と出たので、インストール。
ms-vscode.cpptools publisher:"Microsoft"

launch.json
"program": "enter program name, for example ${workspaceRoot}/a.out",

ソースの取得

https://opensource.apple.com/release/os-x-10105.html

file_cmds-242.tar.gz (Yosemite 10.10.5)
を解凍したけど、 にMakefileがない。

https://opensource.apple.com/source/file_cmds/file_cmds-188/ls/Makefile にはあるけど。

https://opensource.apple.com/source/file_cmds/file_cmds-212/Makefileがある中で一番数字が大きい。

ls/mac212% make
Makefile:16: /Library/Developer/CommandLineTools/Makefiles/CoreOS/ReleaseControl/BSDCommon.make: No such file or directory
make: *** No rule to make target `/Library/Developer/CommandLineTools/Makefiles/CoreOS/ReleaseControl/BSDCommon.make'.  Stop.