Last active
December 16, 2015 18:49
-
-
Save spotco/5480134 to your computer and use it in GitHub Desktop.
ls | grep .txt | xargs cat IN C
This file contains hidden or 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 <dirent.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <fcntl.h> | |
| #include <sys/stat.h> | |
| #include <stdlib.h> | |
| #include <sys/mman.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| int ends_with(const char *str, const char *suffix); | |
| int main(int argc,char **argv) { | |
| if (argc != 2) printf("./ex7 FOLDER"); | |
| DIR *dirp; | |
| dirp = opendir(argv[1]); | |
| struct dirent *dptr; | |
| while(dptr=readdir(dirp)) { | |
| char fullpath[1024]; | |
| strcpy(fullpath,argv[1]); | |
| strcat(fullpath,"/"); | |
| strcat(fullpath,dptr->d_name); | |
| if (ends_with(fullpath,".txt")) { | |
| printf("FILE:%s\n",fullpath); | |
| int fd = open(fullpath,O_RDONLY); | |
| struct stat s; | |
| fstat(fd,&s); | |
| char *buffer = mmap(0,s.st_size,PROT_READ,MAP_PRIVATE,fd,0); | |
| printf("%s",buffer); | |
| close(fd); | |
| } | |
| } | |
| closedir(dirp); | |
| } | |
| int ends_with(const char *str, const char *suffix) { | |
| if (!str || !suffix) return 0; | |
| size_t lenstr = strlen(str); | |
| size_t lensuffix = strlen(suffix); | |
| if (lensuffix > lenstr) return 0; | |
| return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment