/* * ls.c *打印出给定目录下文件名 * Created on: 2013-11-16 * Author: root */ #include <stdio.h> #include <stdlib.h> #include <dirent.h> int main(int argc, char **argv) { DIR *dp; struct dirent *dirp; if(argc != 2) { printf("\r\ninvliad param!\r\n"); return -1; } if((dp = opendir(argv[1])) == NULL) { printf("\r\ncan not open dir\r\n"); return -2; } while( (dirp = readdir(dp)) != NULL) { printf("%s\n", dirp->d_name); } closedir(dp); return 0; }