在之前的一些看書或者學習中,一直有一種感覺有問題的態度,那就是認為看懂了,但是不動手,感覺這樣看書的效果不是很大。ls命令估計是我們在linux/unix里面用的最多的一個命令了,我們就用c來簡單的實現一下ls命令。
//// ls.c// apue//// Created by chenqing on 13-8-22.// Copyright (c) 2013年 chenqing. All rights reserved.// #include "/usr/include/apue.h"#include "dirent.h" int main(int argc,char *argv[]){ DIR *dp ; //創建一個DIR結構的指針 //更多信息參考http://www.gnu.org/software/libc/manual/html_node/Opening-a-Directory.html struct dirent *dirp; if (argc != 2) { err_sys("需要兩個參數"); //err_sys 是在error.c中定義的一個函數 } if ((dp = opendir(argv[1])) == NULL) { err_quit("讀取目錄出錯了!"); } while ((dirp = readdir(dp)) != NULL) { printf("%s/n",dirp->d_name); } closedir(dp); exit(0); }
新聞熱點
疑難解答