LoginSignup
2
0

More than 3 years have passed since last update.

lsコマンド内部を見るまでの準備

Last updated at Posted at 2018-08-21

はじめに

普段使っているlsコマンドはどのように動いているのか疑問に思い、記事にしました。C言語の勉強に役立つと思うのでぜひtryしてみてください。

tarコマンドで展開

まずは下記のサイトからtarballを入手
https://directory.fsf.org/wiki/Coreutils
ダウンロードが完了したら適当なディレクトリに移動

hibi221b@hibi221b:~$ pwd
/home/hibi221b
hibi221b@hibi221b:~$ mkdir command
hibi221b@hibi221b:~$ cd command/
hibi221b@hibi221b:~/command$ mv ~/coreutils-8.29.tar.xz .
hibi221b@hibi221b:~/command$ ls
coreutils-8.29.tar.xz
hibi221b@hibi221b:~/command$ 

次にtarコマンドで展開します tar Jxvf coreutils-8.29.tar.xz

hibi221b@hibi221b:~/command$ tar Jxvf coreutils-8.29.tar.xz 
coreutils-8.29/
coreutils-8.29/.version
...
...
...
hibi221b@hibi221b:~/command$ ls
coreutils-8.29  coreutils-8.29.tar.xz
hibi221b@hibi221b:~/command$ cd coreutils-8.29/
hibi221b@hibi221b:~/command/coreutils-8.29$ pwd
/home/hibi221b/command/coreutils-8.29
hibi221b@hibi221b:~/command/coreutils-8.29$ 

展開後、coreutils-8.29ディレクトリに移動します。

コンパイル&インストール

コンパイルする前にMakefileを生成するconfigureコマンドを行います
これからの手順:
1.====>./configure
2.====>make
3.====>su
4.====>make install

hibi221b@hibi221b:~/command/coreutils-8.29$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
...
...
...
config.status: executing depfiles commands
config.status: executing po-directories commands
config.status: creating po/POTFILES
config.status: creating po/Makefile

makeコマンドでコンパイルします

hibi221b@hibi221b:~/command/coreutils-8.29$ make
  GEN      lib/alloca.h
  GEN      lib/arpa/inet.h
...
...
...
make[3]: Leaving directory '/home/hibi221b/command/coreutils-8.29/gnulib-tests'
make[2]: Leaving directory '/home/hibi221b/command/coreutils-8.29/gnulib-tests'
make[1]: Leaving directory '/home/hibi221b/command/coreutils-8.29'
hibi221b@hibi221b:~/command/coreutils-8.29$ 

インストールするための権限を取得するために
make installはrootユーザで行う必要があります。
(./configure --prefix=$HOME/適当なディレクトリを指定すれば一般ユーザでroot権限がなくてもインストールできるみたい)

hibi221b@hibi221b:~/command/coreutils-8.29$ su
パスワード: 
root@hibi221b:/home/hibi221b/command/coreutils-8.29# make install
make  install-recursive
make[1]: Entering directory '/home/hibi221b/command/coreutils-8.29'
...
...
...
make[2]: Leaving directory '/home/hibi221b/command/coreutils-8.29/gnulib-tests'
make[1]: Leaving directory '/home/hibi221b/command/coreutils-8.29'
root@hibi221b:/home/hibi221b/command/coreutils-8.29# exit
exit
hibi221b@hibi221b:~/command/coreutils-8.29$ 

lsコマンドの場所は?

hibi221b@hibi221b:~/command/coreutils-8.29$ ls -l
合計 7728
-rw-r--r--  1 hibi221b hibi221b   93787 11月 30  2016 ABOUT-NLS
-rw-r--r--  1 hibi221b hibi221b    3760 11月 23  2016 AUTHORS
-rw-r--r--  1 hibi221b hibi221b   35147  8月 23  2011 COPYING
...
...
...
drwxr-xr-x  4 hibi221b hibi221b   12288  8月 22 00:13 src
drwxr-xr-x 25 hibi221b hibi221b    4096 12月 28  2017 tests
-rwxr-xr-x  1 hibi221b hibi221b     441  5月 28  2012 thanks-gen
hibi221b@hibi221b:~/command/coreutils-8.29$ cd src

srcディレクトリに移動すると...

hibi221b@hibi221b:~/command/coreutils-8.29/src$ ls -l
合計 39136
...
...
...
-rwxr-xr-x 1 hibi221b hibi221b 617592  8月 22 00:13  ls
-rw-r--r-- 1 hibi221b hibi221b     44 10月 16  2016  ls-dir.c
-rw-r--r-- 1 hibi221b hibi221b   2144  8月 22 00:12  ls-dir.o
-rw-r--r-- 1 hibi221b hibi221b     37 10月 16  2016  ls-ls.c
-rw-r--r-- 1 hibi221b hibi221b   2144  8月 22 00:13  ls-ls.o
-rw-r--r-- 1 hibi221b hibi221b     46 10月 16  2016  ls-vdir.c
-rw-r--r-- 1 hibi221b hibi221b   2144  8月 22 00:13  ls-vdir.o
-rw-r--r-- 1 hibi221b hibi221b 161713 12月 11  2017  ls.c
-rw-r--r-- 1 hibi221b hibi221b    201 11月 18  2015  ls.h
-rw-r--r-- 1 hibi221b hibi221b 580368  8月 22 00:12  ls.o

lsコマンドを見つけることができました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0