|
Posted by Kishore on 09/28/66 12:02
/****************************************************************/
/* Copyright (c) 1998 CommVault. All Rights Reserved. */
/* CommVault Systems Inc, David A. Oshinsky */
/* Modified for Windows NT, Anand Prahlad */
/* */
/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF */
/* COMMVAULT SYSTEMS Inc. */
/* The copyright notice above does not evidence any */
/* actual or intended publication of such source code. */
/****************************************************************/
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <windows.h>
#define MAXPATHLEN 1024
#define MAXNAMLEN 255
static int ReadDirectory(char *);
static int CompareFunc(const void *, const void *);
static char simple_name[MAXNAMLEN+1];
static char child_name[MAXPATHLEN];
void
main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s directory\n", argv[0]);
exit(1);
}
if (ReadDirectory(argv[1]) < 0)
exit(4);
exit(0);
}
#define LIST_INCR 64
#define BUF_INCR 1024
static int
ReadDirectory(char* dir)
{
WIN32_FIND_DATA FindFileData;
HANDLE fileHandle;
struct stat buf;
char searchPath[MAXNAMLEN+1], errbuf[1024];
unsigned child_count=0, list_consumed=0, buf_consumed=0,len;
unsigned buf_allocated=BUF_INCR;
unsigned list_allocated=LIST_INCR*sizeof(char *);
char **child_list;
char *child_buffer, *cp;
int i;
static char *malloc_failed = "insufficient memory, directory %s\n";
if(0)
{
printf("Cannot stat %s, errno %d\n", dir, errno);
exit(2);
}
if(0)
{
printf("%s is not a directory.\n", dir);
exit(3);
}
sprintf(searchPath, "%s\\*.*", dir);
if( (fileHandle = FindFirstFile(searchPath, &FindFileData)) ==
INVALID_HANDLE_VALUE)
{
sprintf(errbuf, "FindFirstFile failed on %s, err %d\n",
searchPath, GetLastError());
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
return(-1);
}
child_list = (char **)malloc((unsigned)list_allocated);
child_buffer = (char *)malloc((unsigned)buf_allocated);
if (child_list == NULL || child_buffer == NULL) {
sprintf(errbuf, malloc_failed, dir);
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
return(-1);
}
strncpy(simple_name,FindFileData.cFileName, MAXNAMLEN);
simple_name[MAXNAMLEN-1] = '\0';
if (strcmp(simple_name, ".") && strcmp(simple_name, ".."))
{
list_consumed += sizeof(char *);
len = strlen(simple_name) + 1;
child_list[child_count++] = child_buffer + buf_consumed;
strcpy(child_buffer+buf_consumed, simple_name);
buf_consumed += len;
}
while (FindNextFile(fileHandle, &FindFileData) != FALSE)
{
strncpy(simple_name,FindFileData.cFileName, MAXNAMLEN);
simple_name[MAXNAMLEN-1] = '\0';
if (!strcmp(simple_name, ".") || !strcmp(simple_name, ".."))
continue;
list_consumed += sizeof(char *);
if (list_consumed > list_allocated) {
list_allocated += LIST_INCR*sizeof(char *);
child_list =
(char **)realloc(child_list, list_allocated);
if (child_list == NULL) {
sprintf(errbuf, malloc_failed, dir);
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
return(-1);
}
}
len = strlen(simple_name) + 1;
if (len + buf_consumed > buf_allocated) {
buf_allocated += BUF_INCR;
child_buffer =
(char *)realloc(child_buffer, buf_allocated);
if (child_buffer == NULL) {
sprintf(errbuf, malloc_failed, dir);
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
return(-1);
}
}
child_list[child_count++] = child_buffer + buf_consumed;
strcpy(child_buffer+buf_consumed, simple_name);
buf_consumed += len;
}
/* Sort the list */
//qsort((char *)child_list, (int)child_count, sizeof(char *),
CompareFunc);
/* Examine each child of dir (in reverse alphabetical order) */
for (i=child_count-1; i>=0; i--) {
cp = child_list[i];
/* Concatenate parent name with child to get full path */
if (strlen(dir) + strlen(cp) + 2 > MAXPATHLEN) {
sprintf(errbuf, "Parent %s child %s too long\n",
dir, cp);
MessageBox(NULL, errbuf, NULL, MB_ICONSTOP|MB_OK|MB_APPLMODAL);
continue;
}
char temp[1024];
strcpy(temp,dir);
if(dir[strlen(dir) - 1] == '\\')
sprintf(child_name, "%s%s", dir, cp);
else
sprintf(child_name,"%s\\%s",dir,cp);
if(stat(child_name, &buf) < 0)
{
printf("Stat on %s failed. Errno %d\n", child_name, errno);
return(-1);
}
switch(buf.st_mode & S_IFMT)
{
case S_IFDIR: // Directory
if(ReadDirectory(child_name) < 0)
return -1;
strcpy(child_name,temp);
printf("%s\\%s <Directory>\n", child_name,cp);
break;
case S_IFREG: // regular file
strcpy(child_name,temp);
printf("%s\\%s <Regular file>\n", child_name,cp);
break;
case S_IFCHR: // Character special file
printf("%s\\%s <Character special file>\n", child_name,cp);
break;
default:
printf("Bad file type 0x%X for %s\n",
(unsigned)(buf.st_mode & S_IFMT), child_name);
return -1;
}
}
return(0);
}
int
CompareFunc(const void *s1, const void *s2)
{
return(strcmp( (char *) s1, (char *) s2));
}
Navigation:
[Reply to this message]
|