src/scanner.c

changeset 40
5938a9b74e8e
parent 36
a7ff583e153f
child 41
c2e73e175341
equal deleted inserted replaced
39:4761bc1b0278 40:5938a9b74e8e
38 38
39 int scanDirectory(scanner_t scanner, settings_t* settings) { 39 int scanDirectory(scanner_t scanner, settings_t* settings) {
40 40
41 DIR *dirf; 41 DIR *dirf;
42 struct dirent *entry; 42 struct dirent *entry;
43 int entrynamelen;
43 int lines, a; 44 int lines, a;
44 int lineSum = 0; 45 int lineSum = 0;
45 bool bfile; 46 bool bfile;
46 struct stat statbuf; 47 struct stat statbuf;
47 48
51 return 0; 52 return 0;
52 } 53 }
53 54
54 while ((entry = readdir(dirf)) != NULL) { 55 while ((entry = readdir(dirf)) != NULL) {
55 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 56 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
56 /* Construct tree view and absolute pathname strings */ 57 /* Construct absolute pathname string */
57 char entryname[strlen(entry->d_name)+scanner.spaces]; 58 entrynamelen = strlen(entry->d_name);
58 for (int t = 0 ; t < scanner.spaces ; t++) { 59 char filename[(1+strlen(scanner.dir)+entrynamelen)];
59 entryname[t]=' ';
60 }
61 entryname[scanner.spaces] = 0;
62 strcat(entryname, entry->d_name);
63
64 char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
65 strcpy(filename, scanner.dir); 60 strcpy(filename, scanner.dir);
66 strncat(filename, &settings->fileSeparator, 1); 61 strncat(filename, &settings->fileSeparator, 1);
67 strcat(filename, entry->d_name); 62 strcat(filename, entry->d_name);
68 63
69 /* Check for subdirectory */ 64 /* Check for subdirectory */
70 if (stat(filename, &statbuf) == 0) { 65 if (stat(filename, &statbuf) == 0) {
71 if (!(statbuf.st_mode & S_IFREG)) { 66 if (!(statbuf.st_mode & S_IFREG)) {
72 printf("%-60s\n", entryname); 67 printf("%*s\n", entrynamelen+scanner.spaces, entry->d_name);
73 if (settings->recursive && (statbuf.st_mode & S_IFDIR)) { 68 if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
74 lineSum += scanDirectory( 69 lineSum += scanDirectory(
75 (scanner_t) {filename, scanner.spaces+1}, settings); 70 (scanner_t) {filename, scanner.spaces+1}, settings);
76 } 71 }
77 continue; 72 continue;
91 char line_buffer[REGEX_MAX_LINELENGTH]; 86 char line_buffer[REGEX_MAX_LINELENGTH];
92 int line_buffer_offset = 0; 87 int line_buffer_offset = 0;
93 88
94 FILE *file = fopen(filename, "r"); 89 FILE *file = fopen(filename, "r");
95 if (file == NULL) { 90 if (file == NULL) {
96 printf("%s", entryname); 91 printf("%*s", entrynamelen+scanner.spaces, entry->d_name);
97 perror(" File acces failed"); 92 perror(" File acces failed");
98 continue; 93 continue;
99 } 94 }
100 95
101 do { 96 do {
127 fclose(file); 122 fclose(file);
128 123
129 /* Print and sum line count */ 124 /* Print and sum line count */
130 if (bfile) { 125 if (bfile) {
131 if (!settings->matchesOnly) { 126 if (!settings->matchesOnly) {
132 printf("%-60s%19s\n", entryname, "binary"); 127 printf("%*s%*s%19s\n",
128 entrynamelen+scanner.spaces, entry->d_name,
129 60-entrynamelen-scanner.spaces, "", "binary");
133 } 130 }
134 } else { 131 } else {
135 lineSum += lines; 132 lineSum += lines;
136 printf("%-60s%13d lines\n", entryname, lines); 133 printf("%*s%*s%13d lines\n",
134 entrynamelen+scanner.spaces, entry->d_name,
135 60-entrynamelen-scanner.spaces, "", lines);
137 } 136 }
138 } else { 137 } else {
139 if (!settings->matchesOnly) { 138 if (!settings->matchesOnly) {
140 /* Print hint */ 139 /* Print hint */
141 printf("%-60s%19s\n", entryname, "no match"); 140 printf("%*s%*s%19s\n",
141 entrynamelen+scanner.spaces, entry->d_name,
142 60-entrynamelen-scanner.spaces, "", "no match");
142 } 143 }
143 } 144 }
144 } 145 }
145 } 146 }
146 147

mercurial