lsコマンドの簡単な実装
743 ワード
lsコマンドの簡単な実装
1.コマンドラインの最初のパラメータargv[1]は、リストする各ディレクトリ項目のディレクトリ名として使用される
2.opendir関数はDIR構造へのポインタを返す
3.readdir関数各ディレクトリ・アイテムを読み込む
4.関数exitはプログラムを終了するために用いられ、パラメータ0は正常終了を表し、パラメータ1~255はエラーを表す
5.実行ルーチン:$./a.out/dev/
#include
#include
#include
int main(int argc,char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
{
printf("suage: ls directory_name
");
exit(1);
}
if((dp = opendir(argv[1])) == NULL)
{
printf("can't open %s
",argv[1]);
exit(1);
}
while((dirp = readdir(dp)) != NULL)
printf("%s
",dirp->d_name);
closedir(dp);
exit(0);
}
1.コマンドラインの最初のパラメータargv[1]は、リストする各ディレクトリ項目のディレクトリ名として使用される
2.opendir関数はDIR構造へのポインタを返す
3.readdir関数各ディレクトリ・アイテムを読み込む
4.関数exitはプログラムを終了するために用いられ、パラメータ0は正常終了を表し、パラメータ1~255はエラーを表す
5.実行ルーチン:$./a.out/dev/