I am writing a program to print filenames in the current directory and print the time stamps next to them. I am loading the names into an array and then passing them to a function to print the stat and filenames. Here is the code.
#include <stdio.h> /* For printf, fprintf */
#include <string.h> /* For strcmp */
#include <ctype.h> /* For isdigit */
#include <fcntl.h> /* For O_RDONLY */
#include <dirent.h> /* For getdents */
#include <sys/stat.h> /* For IS macros */
#include <sys/types.h> /* For modet */
#include <time.h> /* For localtime, asctime */
/* #define Statements */
#define MAX_FILES 100
#define MAX_FILENAME 50
#define NOT_FOUND -1
#define FOREVER -1
#define DEFAULT_DELAY_TIME 10
#define DEFAULT_LOOP_COUNT FOREVER
/* Booleans */
enum { FALSE, TRUE };
/* Status structure, one per file. */
struct statStruct
{
char fileName [MAX_FILENAME]; /* File name */
int lastCycle, thisCycle; /* To detect changes */
struct stat status; /* Information from stat () */
};
/* Globals */
char* fileNames [MAX_FILES]; /* One per file on command line */
int fileCount; /* Count of files on command line */
struct statStruct stats [MAX_FILES]; /* One per matching file */
int loopCount = DEFAULT_LOOP_COUNT; /* Number of times to loop */
int delayTime = DEFAULT_DELAY_TIME; /* Seconds between loops */
/****************************************************************/
main (int argc,char* argv[])
{
parseCommandLine (argc, argv); /* Parse command line */
monitorLoop (); /* Execute main monitor loop */
return (/* EXIT_SUCCESS */ 0);
}
/****************************************************************/
parseCommandLine (int argc,char* argv[])
/* Parse command line arguments */
{
DIR *dp;
struct dirent *dirEntry;
char fileName [MAX_FILENAME];
fileCount = 0;
int i;
dp = opendir (".");
if (dp == NULL) fatalError();
while (dirEntry=readdir(dp)) /* Read all directory entries */
{
if((fnmatch("*c", dirEntry->d_name,0)) == 0)
fileNames[fileCount++] = dirEntry->d_name ;
}
closedir(dp);
if (fileCount == 0) usageError ();
}
/****************************************************************/
usageError ()
{
fprintf (stderr, "Usage: myls <Search-Pattern>");
exit (/* EXIT_FAILURE */ 1);
}
/****************************************************************/
monitorLoop ()
/* The main monitor loop */
{
monitorFiles (); /* Scan all files */
fflush (stdout); /* Flush standard output */
fflush (stderr); /* Flush standard error */
}
/****************************************************************/
monitorFiles ()
/* Process all files */
{
int i;
for (i = 0; i < fileCount; i++)
monitorFile (fileNames[i]);
}
/****************************************************************/
monitorFile (char* fileName)
/* Process a single file/directory*/
{
struct stat statBuf;
mode_t mode;
int result;
result = stat (fileName, &statBuf); /* Obtain file status */
mode = statBuf.st_mode; /* Mode of file */
if(S_ISDIR (mode)) /* Directory */
printf("D%s", fileName);
else if (S_ISREG (mode) || S_ISCHR (mode) || S_ISBLK (mode))
updateStat (fileName, &statBuf); /* Regular file */
}
/****************************************************************/
updateStat (char* fileName,struct stat* statBuf)
/* Add a status entry if necessary */
{
addEntry (fileName, statBuf); /* Add new entry */
}
/****************************************************************/
addEntry (char* fileName,struct stat* statBuf)
/* Add a new entry into the status array */
{
int index;
index = nextFree (); /* Find the next free entry */
if (index == NOT_FOUND) return (NOT_FOUND); /* None left */
strcpy (stats[index].fileName, fileName); /* Add filename */
stats[index].status = *statBuf; /* Add status information */
printEntry (index); /* Display status information */
return (index);
}
/****************************************************************/
nextFree ()
/* Return the nextfree index in the status array */
{
int i;
for (i = 0; i < MAX_FILES; i++)
return (i);
}
/****************************************************************/
printEntry (int index)
/* Display an entry of the status array */
{
printf ("%s", stats[index].fileName);
printStat (&stats[index].status);
}
/****************************************************************/
printStat (struct stat* statBuf)
/* Display a status buffer */
{
printf ("\t\t\t %s",
asctime (localtime (&statBuf->st_mtime)));
}
/****************************************************************/
fatalError ()
{
perror ("monitor: ");
exit (/* EXIT_FAILURE */ 1);
}