miiworld2 miiworld2 - 4 months ago 16
C Question

C code not displaying output

So I'm working on a C program with UNIX as its terminal, and the program is supposed to call my directory recursively. The output should give me the directory themselves and the time period they were modified.

The code works, it compiles well, but it refuses to display its output.
And right now all I need is to display the output. But this is what it gives me...
I've tried ./compdir, ./compdir dir1 dir2, and also ./compdir dir2 dir1...the only possible solution in the UNIX terminal, but to no avail.

the output it gives me

Can someone help me out? Here is the code down below. I appreciate your help.

#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>

#define E_INVALIDSWITCH 100
#define E_TOOMANYPARAMS 101
#define E_TOOFEWPARAMS 102
#define E_OPENDIRFAIL 103

void status();
void greetDirectory(char * baseDir, char * modDir);

int main(int argc, char ** argv)
{
int R_switch = 0;
char * modDir = NULL;
char * baseDir = NULL;

int i;
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{

if (strcmp(argv[i], "-R") == 0)
{
R_switch = 1;
}

else
{
fprintf(stderr, "Error: Invalid switch %s\n", argv[i]);
status();
return E_INVALIDSWITCH;
}
}

else
{
if (baseDir == NULL)
{
baseDir = argv[i];
}

else if (modDir == NULL)
{
modDir = argv[i];
}

else
{
fprintf(stderr, "Error: Too many parameters\n");
status();
return E_TOOMANYPARAMS;
}
}
}

if (modDir == NULL)
{
fprintf(stderr, "Error: Too few parameters\n");
status();
return E_TOOFEWPARAMS;
}

if (R_switch)
{
greetDirectory(baseDir, modDir);
}
return 0;
}

void status()
{
printf("\t-R\tGreet all files in current directory\n");
printf("\t<modDir>\tName of person being greeted\n");
}

char *formatdate(char *buff, time_t val)
{
strftime(buff,200, "%d.%m.%Y %H:%M:%S", localtime(&val));
return buff;
}

void greetDirectory(char * baseDir, char * modDir)
{
DIR * directory;
struct dirent * dirInfo;

directory = opendir(baseDir);
if (directory == NULL)
{
fprintf(stderr,
"opendir() failed for '%s', errno=%d\n",
baseDir,
errno);
exit(E_OPENDIRFAIL);
}

while((dirInfo = readdir(directory)) != NULL)
{
if (strcmp(dirInfo->d_name, ".") != 0 &&
strcmp(dirInfo->d_name, "..") != 0)
{
/* BASE DIR*/
char basePathName[PATH_MAX];
strcpy(basePathName, baseDir);
strcat(basePathName, "/");
strcat(basePathName, dirInfo->d_name);

/* MOD DIR */
char modPathName[PATH_MAX];
strcpy(modPathName, modDir);
strcat(modPathName, "/");
strcat(modPathName, dirInfo->d_name);

struct stat statBuf;
struct stat otherStatBuf;
if (stat(basePathName, &statBuf) == -1)
{
/* error! */
fprintf(stderr, "stat() failed for '%s'", basePathName);
}
else
{
//display time
stat(modPathName, &otherStatBuf);
if (statBuf.st_mtime > otherStatBuf.st_mtime)
{
char date[36];
printf("Modify: %s\n", formatdate(date, otherStatBuf.st_mtime));
}

if (S_ISDIR(statBuf.st_mode))
{
/* if this is a directory, then recurse! */
greetDirectory(basePathName, modPathName);
}
else
{
/* else if it's a file, then greet it! */
printf("entry name = %s\n", modPathName);
}
}
}
}

closedir(directory);
}

Answer

Your code will give output only when you specify the -R switch.

If you do not want this behavior, you need to remove if block on line 72 and call greetDirectory function directly.

// Remove if block 

if (R_switch)
{
    greetDirectory(baseDir, modDir);
}

// Replace with 

greetDirectory(baseDir, modDir);