[NET]
authorEric Kohl <eric.kohl@reactos.org>
Sat, 25 Oct 2014 21:27:05 +0000 (21:27 +0000)
committerEric Kohl <eric.kohl@reactos.org>
Sat, 25 Oct 2014 21:27:05 +0000 (21:27 +0000)
Implement and use console-aware print functions to print strings and resources.

svn path=/trunk/; revision=64998

reactos/base/applications/network/net/cmdAccounts.c
reactos/base/applications/network/net/cmdHelpMsg.c
reactos/base/applications/network/net/cmdLocalGroup.c
reactos/base/applications/network/net/cmdStart.c
reactos/base/applications/network/net/cmdUser.c
reactos/base/applications/network/net/main.c
reactos/base/applications/network/net/net.h

index c297da0..dcd93de 100644 (file)
@@ -48,7 +48,7 @@ cmdAccounts(
 
         if (_wcsicmp(argv[i], L"/domain") == 0)
         {
-            printf("The /DOMAIN option is not supported yet!\n");
+            PrintToConsole(L"The /DOMAIN option is not supported yet!\n");
 #if 0
             Domain = TRUE;
 #endif
@@ -74,7 +74,7 @@ cmdAccounts(
                 value = wcstoul(p, &endptr, 10);
                 if (*endptr != 0)
                 {
-                    printf("You entered an invalid value for the /FORCELOGOFF option.\n");
+                    PrintToConsole(L"You entered an invalid value for the /FORCELOGOFF option.\n");
                     result = 1;
                     goto done;
                 }
@@ -89,7 +89,7 @@ cmdAccounts(
             value = wcstoul(p, &endptr, 10);
             if (*endptr != 0)
             {
-                    printf("You entered an invalid value for the /MINPWLEN option.\n");
+                    PrintToConsole(L"You entered an invalid value for the /MINPWLEN option.\n");
                     result = 1;
                     goto done;
             }
@@ -111,7 +111,7 @@ cmdAccounts(
                 value = wcstoul(p, &endptr, 10);
                 if (*endptr != 0)
                 {
-                    printf("You entered an invalid value for the /MAXPWAGE option.\n");
+                    PrintToConsole(L"You entered an invalid value for the /MAXPWAGE option.\n");
                     result = 1;
                     goto done;
                 }
@@ -126,7 +126,7 @@ cmdAccounts(
             value = wcstoul(p, &endptr, 10);
             if (*endptr != 0)
             {
-                printf("You entered an invalid value for the /MINPWAGE option.\n");
+                PrintToConsole(L"You entered an invalid value for the /MINPWAGE option.\n");
                 result = 1;
                 goto done;
             }
@@ -140,7 +140,7 @@ cmdAccounts(
             value = wcstoul(p, &endptr, 10);
             if (*endptr != 0)
             {
-                printf("You entered an invalid value for the /UNIQUEPW option.\n");
+                PrintToConsole(L"You entered an invalid value for the /UNIQUEPW option.\n");
                 result = 1;
                 goto done;
             }
@@ -168,51 +168,51 @@ cmdAccounts(
 
         RtlGetNtProductType(&ProductType);
 
-        printf("Force logoff after: ");
+        PrintToConsole(L"Force logoff after: ");
         if (Info0->usrmod0_force_logoff == TIMEQ_FOREVER)
-            printf("Never\n");
+            PrintToConsole(L"Never\n");
         else
-            printf("%lu seconds\n", Info0->usrmod0_force_logoff);
+            PrintToConsole(L"%lu seconds\n", Info0->usrmod0_force_logoff);
 
-        printf("Minimum password age (in days): %lu\n", Info0->usrmod0_min_passwd_age / 86400);
-        printf("Maximum password age (in days): %lu\n", Info0->usrmod0_max_passwd_age / 86400);
-        printf("Minimum password length: %lu\n", Info0->usrmod0_min_passwd_len);
+        PrintToConsole(L"Minimum password age (in days): %lu\n", Info0->usrmod0_min_passwd_age / 86400);
+        PrintToConsole(L"Maximum password age (in days): %lu\n", Info0->usrmod0_max_passwd_age / 86400);
+        PrintToConsole(L"Minimum password length: %lu\n", Info0->usrmod0_min_passwd_len);
 
-        printf("Password history length: ");
+        PrintToConsole(L"Password history length: ");
         if (Info0->usrmod0_password_hist_len == 0)
-            printf("None\n");
+            PrintToConsole(L"None\n");
         else
-            printf("%lu\n", Info0->usrmod0_password_hist_len);
+            PrintToConsole(L"%lu\n", Info0->usrmod0_password_hist_len);
 
-        printf("Lockout threshold: ");
+        PrintToConsole(L"Lockout threshold: ");
         if (Info3->usrmod3_lockout_threshold == 0)
-            printf("Never\n");
+            PrintToConsole(L"Never\n");
         else
-            printf("%lu\n", Info3->usrmod3_lockout_threshold);
+            PrintToConsole(L"%lu\n", Info3->usrmod3_lockout_threshold);
 
-        printf("Lockout duration (in minutes): %lu\n", Info3->usrmod3_lockout_duration / 60);
-        printf("Lockout observation window (in minutes): %lu\n", Info3->usrmod3_lockout_observation_window / 60);
+        PrintToConsole(L"Lockout duration (in minutes): %lu\n", Info3->usrmod3_lockout_duration / 60);
+        PrintToConsole(L"Lockout observation window (in minutes): %lu\n", Info3->usrmod3_lockout_observation_window / 60);
 
-        printf("Computer role: ");
+        PrintToConsole(L"Computer role: ");
 
         if (Info1->usrmod1_role == UAS_ROLE_PRIMARY)
         {
             if (ProductType == NtProductLanManNt)
             {
-                printf("Primary server\n");
+                PrintToConsole(L"Primary server\n");
             }
             else if (ProductType == NtProductServer)
             {
-                printf("Standalone server\n");
+                PrintToConsole(L"Standalone server\n");
             }
             else
             {
-                printf("Workstation\n");
+                PrintToConsole(L"Workstation\n");
             }
         }
         else
         {
-            printf("Backup server\n");
+            PrintToConsole(L"Backup server\n");
         }
     }
 
index 8247836..d78d4e8 100644 (file)
@@ -49,12 +49,12 @@ INT cmdHelpMsg(INT argc, WCHAR **argv)
                        0,
                        NULL))
     {
-        printf("\n%S\n", lpBuffer);
+        PrintToConsole(L"\n%s\n", lpBuffer);
         LocalFree(lpBuffer);
     }
     else
     {
-        printf("Unrecognized error code: %ld\n", errNum);
+        PrintToConsole(L"Unrecognized error code: %ld\n", errNum);
     }
 
     return 0;
index 1995c26..3cd5af4 100644 (file)
@@ -38,8 +38,8 @@ EnumerateLocalGroups(VOID)
     if (Status != NERR_Success)
         return Status;
 
-    printf("\nAliases for \\\\%S\n\n", pServer->sv100_name);
-    printf("------------------------------------------\n");
+    PrintToConsole(L"\nAliases for \\\\%s\n\n", pServer->sv100_name);
+    PrintToConsole(L"------------------------------------------\n");
 
     NetApiBufferFree(pServer);
 
@@ -58,12 +58,10 @@ EnumerateLocalGroups(VOID)
           sizeof(PLOCALGROUP_INFO_0),
           CompareInfo);
 
-//    printf("dwRead: %lu  dwTotal: %lu\n", dwRead, dwTotal);
     for (i = 0; i < dwRead; i++)
     {
-//        printf("%p\n", pBuffer[i].lgrpi0_name);
          if (pBuffer[i].lgrpi0_name)
-            printf("*%S\n", pBuffer[i].lgrpi0_name);
+            PrintToConsole(L"*%s\n", pBuffer[i].lgrpi0_name);
     }
 
     NetApiBufferFree(pBuffer);
@@ -129,17 +127,17 @@ DisplayLocalGroup(LPWSTR lpGroupName)
              pNames[i] = pMembers[i].lgrmi3_domainandname;
     }
 
-    printf("Alias name        %S\n", pGroupInfo->lgrpi1_name);
-    printf("Comment           %S\n", pGroupInfo->lgrpi1_comment);
-    printf("\n");
-    printf("Members\n");
-    printf("\n");
-    printf("------------------------------------------\n");
+    PrintToConsole(L"Alias name        %s\n", pGroupInfo->lgrpi1_name);
+    PrintToConsole(L"Comment           %s\n", pGroupInfo->lgrpi1_comment);
+    PrintToConsole(L"\n");
+    PrintToConsole(L"Members\n");
+    PrintToConsole(L"\n");
+    PrintToConsole(L"------------------------------------------\n");
 
     for (i = 0; i < dwRead; i++)
     {
-         if (pNames[i])
-            printf("%S\n", pNames[i]);
+        if (pNames[i])
+            PrintToConsole(L"%s\n", pNames[i]);
     }
 
 done:
index 3407058..ad6021c 100644 (file)
@@ -57,11 +57,11 @@ EnumerateRunningServices(VOID)
                                     &dwServiceCount,
                                     &dwResumeHandle))
             {
-                printf("The following services hav been started:\n\n");
+                PrintToConsole(L"The following services hav been started:\n\n");
 
                 for (i = 0; i < dwServiceCount; i++)
                 {
-                    printf("  %S\n", lpServiceBuffer[i].lpDisplayName);
+                    PrintToConsole(L"  %s\n", lpServiceBuffer[i].lpDisplayName);
                 }
             }
 
index a8289ea..038df08 100644 (file)
@@ -37,11 +37,11 @@ EnumerateUsers(VOID)
     if (Status != NERR_Success)
         return Status;
 
-    printf("\nUser accounts for \\\\%S\n\n", pServer->sv100_name);
+    PrintToConsole(L"\nUser accounts for \\\\%s\n\n", pServer->sv100_name);
 
     NetApiBufferFree(pServer);
 
-    printf("------------------------------------------\n");
+    PrintToConsole(L"------------------------------------------\n");
 
     Status = NetUserEnum(NULL,
                          0,
@@ -64,7 +64,7 @@ EnumerateUsers(VOID)
     {
 //        printf("%p\n", pBuffer[i].lgrpi0_name);
          if (pBuffer[i].usri0_name)
-            printf("%S\n", pBuffer[i].usri0_name);
+            PrintToConsole(L"%s\n", pBuffer[i].usri0_name);
     }
 
     NetApiBufferFree(pBuffer);
@@ -102,7 +102,7 @@ PrintDateTime(DWORD dwSeconds)
                    TimeBuffer,
                    80);
 
-    printf("%S %S\n", DateBuffer, TimeBuffer);
+    PrintToConsole(L"%s %s\n", DateBuffer, TimeBuffer);
 }
 
 
@@ -110,6 +110,7 @@ static
 NET_API_STATUS
 DisplayUser(LPWSTR lpUserName)
 {
+    PUSER_MODALS_INFO_0 pUserModals = NULL;
     PUSER_INFO_4 pUserInfo = NULL;
     NET_API_STATUS Status;
 
@@ -121,39 +122,56 @@ DisplayUser(LPWSTR lpUserName)
     if (Status != NERR_Success)
         return Status;
 
-    printf("User name                    %S\n", pUserInfo->usri4_name);
-    printf("Full name                    %S\n", pUserInfo->usri4_full_name);
-    printf("Comment                      %S\n", pUserInfo->usri4_comment);
-    printf("User comment                 %S\n", pUserInfo->usri4_usr_comment);
-    printf("Country code                 %03ld ()\n", pUserInfo->usri4_country_code);
-    printf("Account active               %S\n", (pUserInfo->usri4_flags & UF_ACCOUNTDISABLE)? L"No" : ((pUserInfo->usri4_flags & UF_LOCKOUT) ? L"Locked" : L"Yes"));
-    printf("Account expires              ");
+    Status = NetUserModalsGet(NULL,
+                              0,
+                              (LPBYTE*)&pUserModals);
+    if (Status != NERR_Success)
+        goto done;
+
+    PrintToConsole(L"User name                    %s\n", pUserInfo->usri4_name);
+    PrintToConsole(L"Full name                    %s\n", pUserInfo->usri4_full_name);
+    PrintToConsole(L"Comment                      %s\n", pUserInfo->usri4_comment);
+    PrintToConsole(L"User comment                 %s\n", pUserInfo->usri4_usr_comment);
+    PrintToConsole(L"Country code                 %03ld ()\n", pUserInfo->usri4_country_code);
+    PrintToConsole(L"Account active               %s\n", (pUserInfo->usri4_flags & UF_ACCOUNTDISABLE)? L"No" : ((pUserInfo->usri4_flags & UF_LOCKOUT) ? L"Locked" : L"Yes"));
+    PrintToConsole(L"Account expires              ");
     if (pUserInfo->usri4_acct_expires == TIMEQ_FOREVER)
-        printf("Never\n");
+        PrintToConsole(L"Never\n");
+    else
+        PrintDateTime(pUserInfo->usri4_acct_expires);
+
+    PrintToConsole(L"\n");
+    PrintToConsole(L"Password last set            \n");
+
+    PrintToConsole(L"Password expires             ");
+    if (pUserModals->usrmod0_max_passwd_age == TIMEQ_FOREVER)
+        PrintToConsole(L"Never\n");
     else
         PrintDateTime(pUserInfo->usri4_acct_expires);
 
-    printf("\n");
-    printf("Password last set            \n");
-    printf("Password expires             \n");
-    printf("Password changeable          \n");
-    printf("Password required            \n");
-    printf("User may change password     \n");
-    printf("\n");
-    printf("Workstation allowed          %S\n", pUserInfo->usri4_workstations);
-    printf("Logon script                 %S\n", pUserInfo->usri4_script_path);
-    printf("User profile                 %S\n", pUserInfo->usri4_profile);
-    printf("Home directory               %S\n", pUserInfo->usri4_home_dir);
-    printf("Last logon                   ");
+    PrintToConsole(L"Password changeable          \n");
+    PrintToConsole(L"Password required            \n");
+    PrintToConsole(L"User may change password     \n");
+
+    PrintToConsole(L"\n");
+    PrintToConsole(L"Workstation allowed          %s\n", pUserInfo->usri4_workstations);
+    PrintToConsole(L"Logon script                 %s\n", pUserInfo->usri4_script_path);
+    PrintToConsole(L"User profile                 %s\n", pUserInfo->usri4_profile);
+    PrintToConsole(L"Home directory               %s\n", pUserInfo->usri4_home_dir);
+    PrintToConsole(L"Last logon                   ");
     if (pUserInfo->usri4_last_logon == 0)
-        printf("Never\n");
+        PrintToConsole(L"Never\n");
     else
         PrintDateTime(pUserInfo->usri4_last_logon);
-    printf("\n");
-    printf("Logon hours allowed          \n");
-    printf("\n");
-    printf("Local group memberships      \n");
-    printf("Global group memberships     \n");
+    PrintToConsole(L"\n");
+    PrintToConsole(L"Logon hours allowed          \n");
+    PrintToConsole(L"\n");
+    PrintToConsole(L"Local group memberships      \n");
+    PrintToConsole(L"Global group memberships     \n");
+
+done:
+    if (pUserModals != NULL)
+        NetApiBufferFree(pUserModals);
 
     if (pUserInfo != NULL)
         NetApiBufferFree(pUserInfo);
index cc93fea..1b489cc 100644 (file)
@@ -51,13 +51,70 @@ PrintResourceString(
     INT resID,
     ...)
 {
-    WCHAR szMsgBuf[MAX_BUFFER_SIZE];
+    WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
+    WCHAR szOutBuffer[MAX_BUFFER_SIZE];
     va_list arg_ptr;
 
     va_start(arg_ptr, resID);
-    LoadStringW(GetModuleHandle(NULL), resID, szMsgBuf, MAX_BUFFER_SIZE);
-    vwprintf(szMsgBuf, arg_ptr);
+    LoadStringW(GetModuleHandle(NULL), resID, szMsgBuffer, MAX_BUFFER_SIZE);
+    _vsnwprintf(szOutBuffer, MAX_BUFFER_SIZE, szMsgBuffer, arg_ptr);
     va_end(arg_ptr);
+
+    WriteToConsole(szOutBuffer);
+}
+
+
+VOID
+PrintToConsole(
+    LPWSTR lpFormat,
+    ...)
+{
+    WCHAR szBuffer[MAX_BUFFER_SIZE];
+    va_list arg_ptr;
+
+    va_start(arg_ptr, lpFormat);
+    _vsnwprintf(szBuffer, MAX_BUFFER_SIZE, lpFormat, arg_ptr);
+    va_end(arg_ptr);
+
+    WriteToConsole(szBuffer);
+}
+
+
+VOID
+WriteToConsole(
+    LPWSTR lpString)
+{
+    CHAR szOemBuffer[MAX_BUFFER_SIZE * 2];
+    HANDLE hOutput;
+    DWORD dwLength;
+
+    dwLength = wcslen(lpString);
+
+    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
+    if ((GetFileType(hOutput) & ~FILE_TYPE_REMOTE) == FILE_TYPE_CHAR)
+    {
+        WriteConsoleW(hOutput,
+                      lpString,
+                      dwLength,
+                      &dwLength,
+                      NULL);
+    }
+    else
+    {
+        dwLength = WideCharToMultiByte(CP_OEMCP,
+                                       0,
+                                       lpString,
+                                       dwLength,
+                                       szOemBuffer,
+                                       MAX_BUFFER_SIZE * 2,
+                                       NULL,
+                                       NULL);
+        WriteFile(hOutput,
+                  szOemBuffer,
+                  dwLength,
+                  &dwLength,
+                  NULL);
+    }
 }
 
 
index e100dc9..7f1d059 100644 (file)
@@ -14,6 +14,7 @@
 #include <windef.h>
 #include <winbase.h>
 #include <winnls.h>
+#include <wincon.h>
 #include <winuser.h>
 #include <winsvc.h>
 #include <stdio.h>
@@ -29,6 +30,15 @@ PrintResourceString(
     INT resID,
     ...);
 
+VOID
+PrintToConsole(
+    LPWSTR lpFormat,
+    ...);
+
+VOID
+WriteToConsole(
+    LPWSTR lpString);
+
 VOID help(VOID);
 INT unimplemented(INT argc, WCHAR **argv);