Skip to content

Instantly share code, notes, and snippets.

@akkijp
Created March 3, 2016 20:15
Show Gist options
  • Save akkijp/fd2a4e4c5d8f93b4c35f to your computer and use it in GitHub Desktop.
Save akkijp/fd2a4e4c5d8f93b4c35f to your computer and use it in GitHub Desktop.
lsコマンドを作ってみた
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
static void do_ls(char *path){
DIR *d;
struct dirent *ent;
d = opendir(path);
if(!d){
perror(path);
exit(1);
}
while( (ent = readdir(d)) ){
printf("%s\n", ent->d_name);
}
closedir(d);
}
int main(int argc, char *argv[]){
int i;
if(argc < 2){
fprintf(stderr, "%s: no arguments\n", argv[0]);
exit(1);
}
for(i=1; i<argc; i++){
do_ls(argv[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment