Code:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <limits.h>
#include <ctype.h>
#include <dirent.h>
#define TRUE 1
#define FALSE 0
#define HEX_0a '\x0a'
#define HEX_00 '\x00'
extern int scandir(char *val, struct dirent ***namelist, int (*select) (), int (*compar) () );
extern int errno;
int main ( )
{
int num_entries, iresults, fh1, isize;
long i, ii;
int isize_beg, isize_end, start_brw_number, isize_num_start_pos;
long reportno;
unsigned int bytes, base;
int FIRST_NUMERIC_FLAG;
char debugLine[2000];
char *numStrFld;
char *val1, *p;
char report_num[256], report_stripped[256], reportfile[256];
char smisc_text[10], file_record[256], error_msg[200];
char char_brw_number[10], cProcessDate[10], cProcessTime[10];
struct dirent **namelist, **list;
struct stat buf;
struct tm *tm;
time_t timestamp;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
val1=NULL;
val1 = getenv("dd_RPTDIR");
if (val1 == NULL)
val1 = getenv("dd_MIFILES");
if (( num_entries = scandir(val1, &list, NULL, NULL)) < 0)
{
printf ( "\nBad scandir call");
return 1;
}
fh1 = open ("dirlist.txt", O_TRUNC | O_CREAT | O_WRONLY, mode );
if ( fh1 == -1 )
{
snprintf ( smisc_text, 10,"%d", errno );
memset (error_msg, 0, sizeof(error_msg) );
strcat (error_msg, smisc_text );
printf ("\n%s", error_msg);
free(namelist);
return 1;
}
//********************* header record which contains the report directory path
memset (file_record, ' ', sizeof(file_record));
strncpy(file_record, val1, (strlen(val1) ) );
isize = strlen(val1);
file_record[isize] = HEX_0a; isize++;
file_record[isize] = HEX_00;
isize = strlen(file_record);
bytes = write (fh1, file_record, isize);
if ( bytes == -1 )
{
snprintf ( smisc_text, 10,"%d", errno );
close ( fh1 );
memset (error_msg, 0, sizeof(error_msg) );
strcpy (error_msg, "Funds System Error 705 - Unable to write to report header in dirlist.txt - ");
strcat (error_msg, smisc_text );
printf ("\n%s", error_msg);
return 1;
}
//*************************************************************
if (num_entries)
{
for (i=0, list=namelist; i<num_entries; i++)
{
memset (file_record, ' ', sizeof(file_record));
iresults = stat( ((*list)->d_name), &buf);
if ( ((*list)->d_name[0]) != '.' )
{
if ((strstr(((*list)->d_name), ".BRW") != NULL) || (strstr(((*list)->d_name), ".brw") != NULL))
{
// isize = ((*list)->d_namlen);
isize-=4;
strncpy(file_record, ((*list)->d_name), isize );
if ( isize < 8 )
{
do
{ file_record[isize] = ' ';
isize++;
} while ( isize < 8 );
}
file_record[isize] = HEX_0a; isize++;
file_record[isize] = HEX_00;
isize = strlen(file_record);
bytes = write (fh1, file_record, isize);
if ( bytes == -1 )
{
free(namelist);
snprintf ( smisc_text, 10,"%d", errno );
close ( fh1 );
memset (error_msg, 0, sizeof(error_msg) );
strcpy (error_msg, "Funds System Error 702 - Unable to write to report directory listing file, dirlist.txt - ");
strcat (error_msg, smisc_text );
printf ("\n%s", error_msg);
return 1;
}
}
}
free (*list);
list++;
}
}
free(namelist);
return 0;
}