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
     1.1 --- a/src/scanner.c	Thu Feb 14 22:26:36 2013 +0100
     1.2 +++ b/src/scanner.c	Fri May 17 14:41:44 2013 +0200
     1.3 @@ -40,6 +40,7 @@
     1.4  
     1.5    DIR *dirf;
     1.6    struct dirent *entry;
     1.7 +  int entrynamelen;
     1.8    int lines, a;
     1.9    int lineSum = 0;
    1.10    bool bfile;
    1.11 @@ -53,15 +54,9 @@
    1.12  
    1.13    while ((entry = readdir(dirf)) != NULL) {
    1.14      if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    1.15 -      /* Construct tree view and absolute pathname strings */
    1.16 -      char entryname[strlen(entry->d_name)+scanner.spaces];
    1.17 -      for (int t = 0 ; t < scanner.spaces ; t++) {
    1.18 -        entryname[t]=' ';
    1.19 -      }
    1.20 -      entryname[scanner.spaces] = 0;
    1.21 -      strcat(entryname, entry->d_name);
    1.22 -  
    1.23 -      char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))];
    1.24 +      /* Construct absolute pathname string */
    1.25 +      entrynamelen = strlen(entry->d_name);
    1.26 +      char filename[(1+strlen(scanner.dir)+entrynamelen)];
    1.27        strcpy(filename, scanner.dir);
    1.28        strncat(filename, &settings->fileSeparator, 1);
    1.29        strcat(filename, entry->d_name);
    1.30 @@ -69,7 +64,7 @@
    1.31        /* Check for subdirectory */
    1.32        if (stat(filename, &statbuf) == 0) {
    1.33          if (!(statbuf.st_mode & S_IFREG)) {
    1.34 -          printf("%-60s\n", entryname);
    1.35 +          printf("%*s\n", entrynamelen+scanner.spaces, entry->d_name);
    1.36            if (settings->recursive && (statbuf.st_mode & S_IFDIR)) {
    1.37              lineSum += scanDirectory(
    1.38                  (scanner_t) {filename, scanner.spaces+1}, settings);
    1.39 @@ -93,7 +88,7 @@
    1.40  
    1.41          FILE *file = fopen(filename, "r");
    1.42          if (file == NULL) {
    1.43 -          printf("%s", entryname);
    1.44 +          printf("%*s", entrynamelen+scanner.spaces, entry->d_name);
    1.45            perror("  File acces failed");
    1.46            continue;
    1.47          }
    1.48 @@ -129,16 +124,22 @@
    1.49          /* Print and sum line count */
    1.50          if (bfile) {
    1.51            if (!settings->matchesOnly) {
    1.52 -            printf("%-60s%19s\n", entryname, "binary");
    1.53 +            printf("%*s%*s%19s\n",
    1.54 +                entrynamelen+scanner.spaces, entry->d_name,
    1.55 +                60-entrynamelen-scanner.spaces, "", "binary");
    1.56            }
    1.57          } else {
    1.58            lineSum += lines;
    1.59 -          printf("%-60s%13d lines\n", entryname, lines);
    1.60 +          printf("%*s%*s%13d lines\n",
    1.61 +              entrynamelen+scanner.spaces, entry->d_name,
    1.62 +              60-entrynamelen-scanner.spaces, "", lines);
    1.63          }
    1.64        } else {
    1.65          if (!settings->matchesOnly) {
    1.66            /* Print hint */
    1.67 -          printf("%-60s%19s\n", entryname, "no match");
    1.68 +          printf("%*s%*s%19s\n",
    1.69 +              entrynamelen+scanner.spaces, entry->d_name,
    1.70 +              60-entrynamelen-scanner.spaces, "", "no match");
    1.71          }
    1.72        }
    1.73      }

mercurial