improved printing the results and fixed bug where the filename was displayed as an empty string

Fri, 17 May 2013 14:41:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 17 May 2013 14:41:44 +0200
changeset 40
5938a9b74e8e
parent 39
4761bc1b0278
child 41
c2e73e175341

improved printing the results and fixed bug where the filename was displayed as an empty string

src/scanner.c file | annotate | diff | comparison | revisions
--- a/src/scanner.c	Thu Feb 14 22:26:36 2013 +0100
+++ b/src/scanner.c	Fri May 17 14:41:44 2013 +0200
@@ -40,6 +40,7 @@
 
   DIR *dirf;
   struct dirent *entry;
+  int entrynamelen;
   int lines, a;
   int lineSum = 0;
   bool bfile;
@@ -53,15 +54,9 @@
 
   while ((entry = readdir(dirf)) != NULL) {
     if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
-      /* Construct tree view and absolute pathname strings */
-      char entryname[strlen(entry->d_name)+scanner.spaces];
-      for (int t = 0 ; t < scanner.spaces ; t++) {
-        entryname[t]=' ';
-      }
-      entryname[scanner.spaces] = 0;
-      strcat(entryname, entry->d_name);
-  
-      char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
+      /* Construct absolute pathname string */
+      entrynamelen = strlen(entry->d_name);
+      char filename[(1+strlen(scanner.dir)+entrynamelen)];
       strcpy(filename, scanner.dir);
       strncat(filename, &settings->fileSeparator, 1);
       strcat(filename, entry->d_name);
@@ -69,7 +64,7 @@
       /* Check for subdirectory */
       if (stat(filename, &statbuf) == 0) {
         if (!(statbuf.st_mode & S_IFREG)) {
-          printf("%-60s\n", entryname);
+          printf("%*s\n", entrynamelen+scanner.spaces, entry->d_name);
           if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
             lineSum += scanDirectory(
                 (scanner_t) {filename, scanner.spaces+1}, settings);
@@ -93,7 +88,7 @@
 
         FILE *file = fopen(filename, "r");
         if (file == NULL) {
-          printf("%s", entryname);
+          printf("%*s", entrynamelen+scanner.spaces, entry->d_name);
           perror("  File acces failed");
           continue;
         }
@@ -129,16 +124,22 @@
         /* Print and sum line count */
         if (bfile) {
           if (!settings->matchesOnly) {
-            printf("%-60s%19s\n", entryname, "binary");
+            printf("%*s%*s%19s\n",
+                entrynamelen+scanner.spaces, entry->d_name,
+                60-entrynamelen-scanner.spaces, "", "binary");
           }
         } else {
           lineSum += lines;
-          printf("%-60s%13d lines\n", entryname, lines);
+          printf("%*s%*s%13d lines\n",
+              entrynamelen+scanner.spaces, entry->d_name,
+              60-entrynamelen-scanner.spaces, "", lines);
         }
       } else {
         if (!settings->matchesOnly) {
           /* Print hint */
-          printf("%-60s%19s\n", entryname, "no match");
+          printf("%*s%*s%19s\n",
+              entrynamelen+scanner.spaces, entry->d_name,
+              60-entrynamelen-scanner.spaces, "", "no match");
         }
       }
     }

mercurial