home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Beijing Paradise BBS Backup
/
PARADISE.ISO
/
software
/
BBSDOORW
/
NETCLB35.ZIP
/
NETCLB35.EXE
/
EXAMPLES
/
WHATHAS.C
< prev
Wrap
C/C++ Source or Header
|
1996-01-03
|
11KB
|
341 lines
/***************************************************************************/
/* File: WHATHAS.C */
/* */
/* Function: List all files that are owned by the specified user. */
/* */
/* Usage: whathas <username> */
/* */
/* Functions Called: GetBinderyObjectID */
/* GetVolumeName */
/* ScanDirectoryInformation */
/* ScanFileInformation */
/* GetPreferredConnectionID */
/* GetDefaultConnectionID */
/* GetPrimaryConnectionID */
/* SetPreferredConnectionID */
/* ISShellLoaded */
/* */
/***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#ifndef TURBOC
#include <malloc.h>
#endif
#include "netware.h"
void usage ( void );
void display_dir( char *this_dir );
void do_files( byte dirhandle,char *this_dir,char *scandir );
void do_directory( char *this_dir , int dirname_length );
char drive_letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ[/]^_,";
char requested_user[256];
long requserid;
byte fileattr,extfileattr;
long filesize;
long fileowner;
int sequence=-1;
char fname[15];
byte maxrightsmask;
char dname[17];
char ownername[49];
word objecttype;
int dir_len;
long dirownerid;
nw_date creationdate,lastaccessdate,lastupdatedate,
lastarchivedate;
nw_time lastupdatetime,lastarchivetime,creationtime;
unsigned long totalfilesize,totalfiles;
char default_output[] = "whathas.txt";
char output_name[256];
FILE *ofile;
void display_dir( char *this_dir )
{
char dir_to_show[81];
char *d = this_dir;
while (strlen(d) > 79)
d=(strchr(d,'\\')+1);
sprintf(dir_to_show,"%-79.79s\r",d);
printf(dir_to_show);
}
void do_files( byte dirhandle,char *this_dir,char *scandir )
{
int dirshown = 0;
char allfiles[2] = "*";
char *dirtoscan;
unsigned long loctotalfilesize=0,loctotalfiles=0;
if (dirhandle == 0)
dirtoscan = scandir;
else
dirtoscan = allfiles;
sequence = -1;
while( ScanFileInformation( dirhandle,dirtoscan,6,&sequence,fname,
&fileattr,&extfileattr,
&filesize,&creationdate,
&lastaccessdate,
&lastupdatedate,&lastupdatetime,
&lastarchivedate,&lastarchivetime,
&fileowner ) == 0)
{
if (fileowner == requserid)
{
if (!dirshown)
{
fprintf(ofile,"\ndirectory: %s\n\n",this_dir);
dirshown = 1;
}
totalfiles++;
totalfilesize += filesize;
loctotalfiles++;
loctotalfilesize += filesize;
fprintf(ofile," %-13.13s %10ld\n",fname,filesize);
}
}
if (loctotalfiles)
{
fprintf(ofile," ==========\n");
fprintf(ofile," Total: %10ld (%ld files)\n",
loctotalfilesize,loctotalfiles);
fprintf(ofile," ==========\n");
}
}
void do_directory( char *this_dir , int dirname_length )
{
int rcode=0;
int subdirno=1;
char *scandir;
char *nextdir;
byte dirhandle,effrights;
/* This procedure is called recursively in order to process every directory */
/* and subdirectory found. Therefore we use malloc as much as possible in */
/* order to allocate required storage, this will stop us blowing the stack. */
scandir = (char *)malloc(dirname_length+3); /* Get enough space to store */
/* current directory name + */
/* '\*' + terminating NULL */
strcpy(scandir,this_dir); /* Construct scan dir path */
if (AllocTemporaryDirectoryHandle(0,scandir,'?',&dirhandle,
&effrights) != 0)
{
printf("Failed to allocate handle\n");
dirhandle = 0;
}
strcpy(scandir+dirname_length,"\\*");
display_dir( this_dir ); /* Display directory name */
/* on console */
do_files( dirhandle,this_dir,scandir ); /* Display files in this */
/* directory */
DeallocateDirectoryHandle(dirhandle);
while ( rcode == 0 ) /* Now get all sub_directories */
{
rcode = ScanDirectoryInformation( 0,scandir,&subdirno,
dname,&creationdate,
&creationtime,
&dirownerid,
&maxrightsmask);
if (rcode == 0)
{
dir_len = strlen(dname);
/* Get enough space to store full path name of found sub_directory */
nextdir = (char *)malloc(dirname_length+dir_len+2);
strcpy(nextdir,this_dir); /* Construct full path name */
*(nextdir+dirname_length) = '\\';
strcpy(nextdir+dirname_length+1,dname);
do_directory(nextdir,dirname_length + 1 + dir_len); /* do it !! */
free(nextdir);
}
}
free(scandir);
}
void usage ( void )
{
printf("\nWhathas - (c) Adrian M. Cunnelly 1993\n\n");
printf(" Scan all directories on all volumes");
printf(" attached to the current server\n");
printf(" and produce a listing of all files owned by the");
printf(" specified Netware User.\n\n");
printf(" Usage: Whathas <switches> Netware_User\n\n");
printf(" Switches:\n");
printf(" -ofile_name - Produce output to 'file_name'\n");
printf(" (default WHATHAS.TXT)\n");
printf("\n");
}
void main(int argc,char *argv[])
{
char volume[17];
int this_one;
char *this_param;
int param=1;
char *o_name = default_output; /* Set output name to default */
char underline[80];
int prefserver;
int thisserver;
totalfilesize = totalfiles = 0;
requested_user[0]='\0';
output_name[0] = '\0';
if (IsShellLoaded() != SUCCESS)
{
printf("*** No netware shell loaded ***\n");
exit(255);
}
else
if ((prefserver = GetPreferredConnectionID()) == 0)
{
if ((thisserver = GetDefaultConnectionID()) == 0)
thisserver = GetPrimaryConnectionID();
SetPreferredConnectionID( thisserver );
}
else
thisserver = prefserver;
if (argc < 2) /* Not enough parameters */
{
usage();
if (thisserver != prefserver) /* reset preferred server */
SetPreferredConnectionID( prefserver );
exit(0);
}
while(--argc)
{
this_param = argv[param++];
if(*this_param == '-' || *this_param == '/')
{
switch(toupper(*(++this_param)))
{
case '?':
usage();
if (thisserver != prefserver)
SetPreferredConnectionID( prefserver );
exit(0);
break;
case 'O':
if (output_name[0])
{
printf("Output name already specified\n");
if (thisserver != prefserver)
SetPreferredConnectionID( prefserver );
exit(255);
}
else
{
strcpy(output_name,this_param+1);
o_name = output_name;
}
break;
default:
printf("Invalid switch specified\n\n");
usage();
if (thisserver != prefserver)
SetPreferredConnectionID( prefserver );
exit(255);
}
}
else
if (requested_user[0])
{
printf("Username already specified\n");
if (thisserver != prefserver)
SetPreferredConnectionID( prefserver );
exit(255);
}
else
{
strcpy(requested_user,this_param);
strupr(requested_user);
if (GetBinderyObjectID(USER,requested_user,&requserid) != 0)
{
printf("\nUser: %s - not found\n",requested_user);
if (thisserver != prefserver)
SetPreferredConnectionID( prefserver );
exit(255);
}
}
}
if (!requested_user[0])
{
printf("A username must be specified\n\n");
usage();
if (thisserver != prefserver)
SetPreferredConnectionID( prefserver );
exit(255);
}
strupr(o_name); /* Convert output filename to uppercase */
if ((ofile = fopen(o_name,"w")) == NULL)
{
printf("Failed to open output file: %s\n",o_name);
if (thisserver != prefserver)
SetPreferredConnectionID( prefserver );
exit(255);
}
/* Write headings to output file */
fprintf(ofile,"\fFiles owned by Netware user %s\n",requested_user);
fputs( "============================",ofile );
memset((void *)&underline,'\0',80);
memset((void *)&underline,'=',strlen(requested_user));
fputs(underline,ofile);
fputc('\n',ofile);
/* Loop round all volumes */
for(this_one=0;this_one<31;this_one++)
if ((GetVolumeName(this_one,volume) == 0) && (volume[0]))
{
strcat(volume,":");
do_directory(volume,strlen(volume));
}
display_dir(" "); /* Clear display line on console */
fprintf(ofile,"\n%s has:\n",requested_user);
fprintf(ofile," %lu bytes in %lu files\n",totalfilesize,totalfiles);
fclose(ofile);
printf("File: %s - produced OK for User: %s\n",o_name,requested_user);
if (thisserver != prefserver)
SetPreferredConnectionID( prefserver );
exit(0);
}