Last active
November 26, 2019 04:12
-
-
Save chrisvrose/6ab74a4c281a9b7456ff9c92ce32a31b to your computer and use it in GitHub Desktop.
ls -l simplification
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<sys/types.h> | |
#include<sys/stat.h> | |
#include<pwd.h> | |
#include<string.h> | |
#include<grp.h> | |
#include<time.h> | |
#include<dirent.h> | |
#include<unistd.h> | |
//char* joinPaths(char* dirname,char*) | |
int main(int argc,char * argv[]){ | |
if(argc!=2){printf("Usage: %s <dirname>\n",argv[0]);return 1;} | |
DIR* d = opendir(argv[1]); | |
char fileEntryName[32]; | |
struct dirent* dirEntry;struct stat stats; | |
char perms[] = "rwx"; | |
if(!d){printf("Could not open\n"); return 1;} | |
while(dirEntry = readdir(d)){ | |
//printf("%s\t",dirEntry->d_name); | |
strcpy(fileEntryName,argv[1]); | |
strcat(fileEntryName,"/"); | |
strcat(fileEntryName,dirEntry->d_name); | |
stat(fileEntryName,&stats); | |
///Figure out what kind of entry | |
switch(S_IFMT&stats.st_mode){ | |
case S_IFIFO: | |
printf("f"); | |
break; | |
case S_IFBLK: | |
printf("b"); | |
break; | |
case S_IFDIR: | |
printf("d"); | |
break; | |
default: | |
printf("-"); | |
} | |
///Parsing the permission bits. its of the format(bitwise): (rwx rwx rwx) | |
for(int j=8;j>=0;j--){ | |
printf("%c",( ( (1<<j) & stats.st_mode)?perms[(8-j)%3]:'-' ) ); | |
} | |
printf("\t%d\t%s\t",stats.st_nlink,getpwuid(stats.st_uid)->pw_name); | |
printf("%s\t",getgrgid(stats.st_gid)->gr_name); | |
printf("%d\t",stats.st_size); | |
char *time = ctime( &(stats.st_atime)); | |
time[strlen(time)-2] = 0; | |
printf("%s\t",time ); | |
printf("%s\n",dirEntry->d_name); | |
printf("\n"); | |
} | |
closedir(d); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment