Created
December 6, 2013 08:32
-
-
Save sota1235/7820410 to your computer and use it in GitHub Desktop.
エセlsコマンド@シスプロ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<time.h> | |
#include<dirent.h> | |
#include<sys/types.h> | |
#include<sys/stat.h> | |
#include<string.h> | |
/* Created by sota1235 | |
* Date : 2013/12/6 | |
* for SystemProgramming homework */ | |
// 関数宣言 | |
void ls_r(char *directory,int tab_num); | |
void tab_writer(int num); | |
int main(int argc,char *argv[]){ | |
// 引数が無い時もしくは多すぎるときのエラー処理 | |
if(argc != 2){ | |
fprintf(stderr, "main : 実行引数の数が不当です\n"); | |
exit(EXIT_FAILURE); | |
} | |
fprintf(stdout,"%s\n",argv[1]); | |
ls_r(argv[1],1); | |
} | |
void ls_r(char *directory,int tab_num){ | |
DIR *dirp = opendir(directory); | |
struct dirent *dp; // ディレクトリ一覧の構造体 | |
struct stat stat_buf; // ファイル情報の構造体 | |
char *path,r_path[512]; | |
// statが失敗した場合return | |
if(stat(directory,&stat_buf)==-1){ | |
//perror("stat"); | |
return; | |
} | |
while((dp = readdir(dirp)) != NULL){ | |
if((stat_buf.st_mode & S_IFMT)==S_IFDIR){ | |
path = dp->d_name; | |
if(strncmp(path,".",1)!=0 && strncmp(path,"..",2)!=0){ | |
tab_writer(tab_num); | |
fprintf(stdout,"%s\n",path); | |
// rootディレクトリの場合はdirecotry変数をつなげない | |
if(strncmp(directory,"/",1)){ | |
sprintf(r_path,"/%s",path); | |
} else { | |
sprintf(r_path,"%s/%s",directory,path); | |
} | |
ls_r(r_path,tab_num+1); | |
} | |
} | |
} | |
closedir(dirp); | |
} | |
void tab_writer(int num){ | |
int i; | |
for(i=0;i<num;i++){ | |
fprintf(stdout," "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment