Added -r switch to objdir to recurse the system name space.
authorEmanuele Aliberti <ea@iol.it>
Wed, 2 May 2001 20:50:06 +0000 (20:50 +0000)
committerEmanuele Aliberti <ea@iol.it>
Wed, 2 May 2001 20:50:06 +0000 (20:50 +0000)
svn path=/trunk/; revision=1871

reactos/apps/utils/objdir/objdir.c

index 0304489..b327f76 100644 (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
  * PROGRAMMER:  David Welch
@@ -13,6 +13,8 @@
  *             Fixed entries counter. Added more
  *             error codes check. Removed wprintf,
  *             because it does not work in .17.
+ *     2001-05-02 (ea)
+ *             Added -r option.
  */
 
 #include <ddk/ntddk.h>
@@ -21,8 +23,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-BYTE DirectoryEntry [32768];
-POBJDIR_INFORMATION pDirectoryEntry = (POBJDIR_INFORMATION) DirectoryEntry;
+#define MAX_DIR_ENTRY 256
+
 
 static
 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
 const char *
 STDCALL
@@ -140,16 +158,21 @@ ExpandSymbolicLink (
        NtClose (hSymbolicLink);
        return TRUE;
 }
-       
 
 
-int main(int argc, char* argv[])
+BOOL
+STDCALL
+ListDirectory (
+       IN      PUNICODE_STRING DirectoryNameW,
+       IN      BOOL            Recurse
+       )
 {
-       UNICODE_STRING          DirectoryNameW;
-       ANSI_STRING             DirectoryNameA;
+       CHAR                    DirectoryNameA [MAX_PATH];
        OBJECT_ATTRIBUTES       ObjectAttributes;
        NTSTATUS                Status;
        HANDLE                  DirectoryHandle;
+       BYTE                    DirectoryEntry [MAX_DIR_ENTRY * sizeof(OBJDIR_INFORMATION)];
+       POBJDIR_INFORMATION     pDirectoryEntry = (POBJDIR_INFORMATION) DirectoryEntry;
        ULONG                   Context = 0;
        ULONG                   ReturnLength = 0;
        ULONG                   EntryCount = 0;
@@ -162,41 +185,20 @@ int main(int argc, char* argv[])
                                        TargetName
                                };
 
-       /*
-        * Check user arguments.
-        */
-       if (2 != argc)
-       {
-               fprintf (
-                       stderr,
-                       "Usage: %s directory\n\n"
-                       "  directory   a directory name in the system namespace\n",
-                       argv [0]
-                       );
-               return EXIT_FAILURE;
-       }
+       /* Convert to ANSI the directory's name */
+       RawUszAsz (DirectoryNameW->Buffer, DirectoryNameA);
        /*
         * Prepare parameters for next call.
         */
-       RtlInitAnsiString (
-               & DirectoryNameA,
-               argv [1]
-               );
-       RtlAnsiStringToUnicodeString (
-               & DirectoryNameW,
-               & DirectoryNameA,
-               TRUE
-               );
        InitializeObjectAttributes (
                & ObjectAttributes,
-               DirectoryNameW,
+               DirectoryNameW,
                0,
                NULL,
                NULL
                );
        /*
-        * Try opening the directory the
-        * user requested.
+        * Try opening the directory.
         */
        Status = NtOpenDirectoryObject (
                        & DirectoryHandle,
@@ -206,12 +208,13 @@ int main(int argc, char* argv[])
        if (!NT_SUCCESS(Status))
        {
                printf (
-                       "Failed to open directory object (Status: %s)\n",
+                       "Failed to open directory object \"%s\" (Status: %s)\n",
+                       DirectoryNameA,
                        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.
         */
@@ -231,7 +234,7 @@ int main(int argc, char* argv[])
                        StatusToName (Status)
                        );
                NtClose (DirectoryHandle);
-               return EXIT_FAILURE;
+               return (FALSE);
        }
        while (0 != pDirectoryEntry->ObjectTypeName.Length)
        {
@@ -242,7 +245,7 @@ int main(int argc, char* argv[])
                if (0 == wcscmp (L"SymbolicLink", pDirectoryEntry->ObjectTypeName.Buffer))
                {
                        if (TRUE == ExpandSymbolicLink (
-                                       DirectoryNameW,
+                                       DirectoryNameW,
                                        & pDirectoryEntry->ObjectName,
                                        & TargetObjectName
                                        )
@@ -275,16 +278,86 @@ int main(int argc, char* argv[])
                }
                ++ EntryCount;
                ++ pDirectoryEntry;
-               
-               
        }
        printf ("\n\t%d object(s)\n", EntryCount);
        /*
         * Free any resource.
         */
        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;
 }