Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/types.h>
- char *getString(){
- int size = 30;
- char *string = malloc(size * sizeof(char));
- char c;
- int i = 0;
- c = getchar();
- while(c != '\n'){
- if(i == size - 2){
- size += 30;
- string = realloc(string, size * sizeof(char));
- }
- string[i] = c;
- i++;
- c = getchar();
- }
- string[i] = '\0';
- return string;
- }
- void recDirent(char *nameDir, int size, char letter, FILE *output){
- DIR *dir = opendir(nameDir);
- struct dirent *cur = readdir(dir);
- if(dir){
- while(cur){
- if((cur->d_type == DT_DIR) && strcmp(cur->d_name, ".") && strcmp(cur->d_name, "..")){
- int length = strlen(nameDir);
- if(length + strlen(cur->d_name) == size - 1){
- size += 100;
- nameDir = realloc(nameDir, size * sizeof(char));
- }
- strcat(nameDir, cur->d_name);
- strcat(nameDir, "/");
- recDirent(nameDir, size, letter, output);
- nameDir[length] = '\0';
- }
- if(cur->d_type == DT_REG){
- if((cur->d_name[0] == letter) && (cur->d_name[1] == '.')){
- fprintf(output, "%s%s\n", nameDir, cur->d_name);
- break;
- }
- }
- cur = readdir(dir);
- }
- }
- closedir(dir);
- }
- int main(){
- char *string = getString();
- int size = 100;
- char *nameDir = malloc(size * sizeof(char));
- FILE *output;
- output = fopen("result.txt", "w");
- for(int i = 0; i < strlen(string); i++){
- nameDir[0] = '\0';
- strcat(nameDir, "./");
- recDirent(nameDir, size, string[i], output);
- }
- fclose(output);
- free(string);
- free(nameDir);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement