Added -r switch to objdir to recurse the system name space.

svn path=/trunk/; revision=1871
This commit is contained in:
Emanuele Aliberti 2001-05-02 20:50:06 +00:00
parent 7de8ff7c3c
commit ff2adbb728

View file

@ -1,4 +1,4 @@
/* $Id: objdir.c,v 1.6 2001/05/01 21:43:45 ea Exp $ /* $Id: objdir.c,v 1.7 2001/05/02 20:50:06 ea Exp $
* *
* DESCRIPTION: Object Manager Simple Explorer * DESCRIPTION: Object Manager Simple Explorer
* PROGRAMMER: David Welch * PROGRAMMER: David Welch
@ -13,6 +13,8 @@
* Fixed entries counter. Added more * Fixed entries counter. Added more
* error codes check. Removed wprintf, * error codes check. Removed wprintf,
* because it does not work in .17. * because it does not work in .17.
* 2001-05-02 (ea)
* Added -r option.
*/ */
#include <ddk/ntddk.h> #include <ddk/ntddk.h>
@ -21,8 +23,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
BYTE DirectoryEntry [32768]; #define MAX_DIR_ENTRY 256
POBJDIR_INFORMATION pDirectoryEntry = (POBJDIR_INFORMATION) DirectoryEntry;
static static
PCHAR PCHAR
@ -40,6 +42,22 @@ RawUszAsz (
} }
static
PWCHAR
STDCALL
RawAszUsz (
PCHAR szA,
PWCHAR szW
)
{
register PWCHAR w = szW;
while (*szA) {*szW++ = (WCHAR) *szA++;}
*szW = L'\0';
return w;
}
static static
const char * const char *
STDCALL STDCALL
@ -142,14 +160,19 @@ ExpandSymbolicLink (
} }
BOOL
int main(int argc, char* argv[]) STDCALL
ListDirectory (
IN PUNICODE_STRING DirectoryNameW,
IN BOOL Recurse
)
{ {
UNICODE_STRING DirectoryNameW; CHAR DirectoryNameA [MAX_PATH];
ANSI_STRING DirectoryNameA;
OBJECT_ATTRIBUTES ObjectAttributes; OBJECT_ATTRIBUTES ObjectAttributes;
NTSTATUS Status; NTSTATUS Status;
HANDLE DirectoryHandle; HANDLE DirectoryHandle;
BYTE DirectoryEntry [MAX_DIR_ENTRY * sizeof(OBJDIR_INFORMATION)];
POBJDIR_INFORMATION pDirectoryEntry = (POBJDIR_INFORMATION) DirectoryEntry;
ULONG Context = 0; ULONG Context = 0;
ULONG ReturnLength = 0; ULONG ReturnLength = 0;
ULONG EntryCount = 0; ULONG EntryCount = 0;
@ -162,41 +185,20 @@ int main(int argc, char* argv[])
TargetName TargetName
}; };
/* /* Convert to ANSI the directory's name */
* Check user arguments. RawUszAsz (DirectoryNameW->Buffer, DirectoryNameA);
*/
if (2 != argc)
{
fprintf (
stderr,
"Usage: %s directory\n\n"
" directory a directory name in the system namespace\n",
argv [0]
);
return EXIT_FAILURE;
}
/* /*
* Prepare parameters for next call. * Prepare parameters for next call.
*/ */
RtlInitAnsiString (
& DirectoryNameA,
argv [1]
);
RtlAnsiStringToUnicodeString (
& DirectoryNameW,
& DirectoryNameA,
TRUE
);
InitializeObjectAttributes ( InitializeObjectAttributes (
& ObjectAttributes, & ObjectAttributes,
& DirectoryNameW, DirectoryNameW,
0, 0,
NULL, NULL,
NULL NULL
); );
/* /*
* Try opening the directory the * Try opening the directory.
* user requested.
*/ */
Status = NtOpenDirectoryObject ( Status = NtOpenDirectoryObject (
& DirectoryHandle, & DirectoryHandle,
@ -206,12 +208,13 @@ int main(int argc, char* argv[])
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
printf ( printf (
"Failed to open directory object (Status: %s)\n", "Failed to open directory object \"%s\" (Status: %s)\n",
DirectoryNameA,
StatusToName (Status) StatusToName (Status)
); );
return EXIT_FAILURE; return (FALSE);
} }
printf ("\n Directory of %s\n\n", argv[1]); printf ("\n Directory of %s\n\n", DirectoryNameA);
/* /*
* Enumerate each item in the directory. * Enumerate each item in the directory.
*/ */
@ -231,7 +234,7 @@ int main(int argc, char* argv[])
StatusToName (Status) StatusToName (Status)
); );
NtClose (DirectoryHandle); NtClose (DirectoryHandle);
return EXIT_FAILURE; return (FALSE);
} }
while (0 != pDirectoryEntry->ObjectTypeName.Length) while (0 != pDirectoryEntry->ObjectTypeName.Length)
{ {
@ -242,7 +245,7 @@ int main(int argc, char* argv[])
if (0 == wcscmp (L"SymbolicLink", pDirectoryEntry->ObjectTypeName.Buffer)) if (0 == wcscmp (L"SymbolicLink", pDirectoryEntry->ObjectTypeName.Buffer))
{ {
if (TRUE == ExpandSymbolicLink ( if (TRUE == ExpandSymbolicLink (
& DirectoryNameW, DirectoryNameW,
& pDirectoryEntry->ObjectName, & pDirectoryEntry->ObjectName,
& TargetObjectName & TargetObjectName
) )
@ -275,16 +278,86 @@ int main(int argc, char* argv[])
} }
++ EntryCount; ++ EntryCount;
++ pDirectoryEntry; ++ pDirectoryEntry;
} }
printf ("\n\t%d object(s)\n", EntryCount); printf ("\n\t%d object(s)\n", EntryCount);
/* /*
* Free any resource. * Free any resource.
*/ */
NtClose (DirectoryHandle); NtClose (DirectoryHandle);
/*
* Recurse into, if required so.
*/
if (FALSE != Recurse)
{
pDirectoryEntry = (POBJDIR_INFORMATION) DirectoryEntry;
while (0 != pDirectoryEntry->ObjectTypeName.Length)
{
if (0 == wcscmp (L"Directory", pDirectoryEntry->ObjectTypeName.Buffer))
{
WCHAR CurrentName [MAX_PATH];
UNICODE_STRING CurrentDirectory;
return EXIT_SUCCESS; CurrentName [0] = L'\0';
wcscpy (CurrentName, DirectoryNameW->Buffer);
if (wcslen (CurrentName) > 1)
{
wcscat (CurrentName, L"\\");
}
wcscat (CurrentName, pDirectoryEntry->ObjectName.Buffer);
RtlInitUnicodeString (& CurrentDirectory, CurrentName);
ListDirectory (& CurrentDirectory, Recurse);
}
++ pDirectoryEntry;
}
}
return (TRUE);
}
int main(int argc, char* argv[])
{
WCHAR DirectoryNameW [MAX_PATH];
UNICODE_STRING DirectoryName;
BOOL Recurse = FALSE;
/*
* Check user arguments.
*/
switch (argc)
{
case 2:
RawAszUsz (argv[1], DirectoryNameW);
break;
case 3:
if (strcmp (argv[1], "-r"))
{
fprintf (
stderr,
"%s: unknown option '%s'.\n",
argv [0], argv[1]
);
return EXIT_FAILURE;
}
RawAszUsz (argv[2], DirectoryNameW);
Recurse = TRUE;
break;
default:
fprintf (
stderr,
"\nUsage: %s [-r] directory\n\n"
" -r recurse\n"
" directory a directory name in the system namespace\n\n",
argv [0]
);
return EXIT_FAILURE;
}
/*
* List the directory.
*/
RtlInitUnicodeString (& DirectoryName, DirectoryNameW);
return (FALSE == ListDirectory (& DirectoryName, Recurse))
? EXIT_FAILURE
: EXIT_SUCCESS;
} }