[IPCONFIG] Indentation and coding style fixes. No code changes!
[reactos.git] / base / applications / network / ipconfig / ipconfig.c
index 43c7390..d85dd31 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * PROJECT:     ReactOS ipconfig utility
  * LICENSE:     GPL - See COPYING in the top level directory
- * FILE:        apps/utils/net/ipconfig/ipconfig.c
+ * FILE:        base/applications/network/ipconfig/ipconfig.c
  * PURPOSE:     Display IP info for net adapters
  * PROGRAMMERS: Copyright 2005 - 2006 Ged Murphy (gedmurphy@gmail.com)
  */
@@ -30,23 +30,23 @@ HINSTANCE hInstance;
 HANDLE ProcessHeap;
 
 int LoadStringAndOem(HINSTANCE hInst,
-               UINT uID,
-               LPTSTR szNode,
-               int byteSize
-)
+                     UINT uID,
+                     LPTSTR szNode,
+                     int byteSize)
 {
-  TCHAR *szTmp;
-  int res;
+    TCHAR *szTmp;
+    int res;
 
-  szTmp = (LPTSTR)HeapAlloc(ProcessHeap, 0, byteSize);
-  if (szTmp == NULL)
-  {
-    return 0;
-  }
-  res = LoadString(hInst, uID, szTmp, byteSize); 
-  CharToOem(szTmp, szNode);
-  HeapFree(ProcessHeap, 0, szTmp);
-  return res;
+    szTmp = (LPTSTR)HeapAlloc(ProcessHeap, 0, byteSize);
+    if (szTmp == NULL)
+    {
+        return 0;
+    }
+
+    res = LoadString(hInst, uID, szTmp, byteSize); 
+    CharToOem(szTmp, szNode);
+    HeapFree(ProcessHeap, 0, szTmp);
+    return res;
 }
 
 LPTSTR GetNodeTypeName(UINT NodeType)
@@ -197,42 +197,47 @@ LPTSTR GetConnectionType(LPTSTR lpClass)
                      KEY_READ,
                      &hKey) == ERROR_SUCCESS)
     {
-        if(RegQueryValueEx(hKey,
-                           _T("Name"),
-                           NULL,
-                           &dwType,
-                           NULL,
-                           &dwDataSize) == ERROR_SUCCESS)
+        if (RegQueryValueEx(hKey,
+                            _T("Name"),
+                            NULL,
+                            &dwType,
+                            NULL,
+                            &dwDataSize) == ERROR_SUCCESS)
         {
             ConTypeTmp = (LPTSTR)HeapAlloc(ProcessHeap,
-                                        0,
-                                        dwDataSize);
+                                           0,
+                                           dwDataSize);
 
+            if (ConTypeTmp == NULL)
+                return NULL;
+                                        
             ConType = (LPTSTR)HeapAlloc(ProcessHeap,
                                         0,
                                         dwDataSize);
-            if (ConType && ConTypeTmp)
-            {
-                if(RegQueryValueEx(hKey,
-                                   _T("Name"),
-                                   NULL,
-                                   &dwType,
-                                   (PBYTE)ConTypeTmp,
-                                   &dwDataSize) != ERROR_SUCCESS)
-                {
-                    HeapFree(ProcessHeap,
-                             0,
-                             ConType);
 
-                    HeapFree(ProcessHeap,
-                             0,
-                             ConTypeTmp);
+            if (ConType == NULL)
+            {
+                HeapFree(ProcessHeap, 0, ConTypeTmp);
+                return NULL;
+            }
 
-                    ConType = NULL;
-                }
+            if (RegQueryValueEx(hKey,
+                                _T("Name"),
+                                NULL,
+                                &dwType,
+                                (PBYTE)ConTypeTmp,
+                                &dwDataSize) != ERROR_SUCCESS)
+            {
+                HeapFree(ProcessHeap,
+                         0,
+                         ConType);
 
-                if (ConType) CharToOem(ConTypeTmp, ConType);
+                ConType = NULL;
             }
+
+            if (ConType)
+                CharToOem(ConTypeTmp, ConType);
+            HeapFree(ProcessHeap, 0, ConTypeTmp);
         }
     }
 
@@ -264,7 +269,7 @@ LPTSTR GetConnectionDescription(LPTSTR lpClass)
         return NULL;
     }
 
-    for (i=0; ; i++)
+    for (i = 0; ; i++)
     {
         DWORD PathSize;
         LONG Status;
@@ -313,12 +318,12 @@ LPTSTR GetConnectionDescription(LPTSTR lpClass)
         HeapFree(ProcessHeap, 0, lpPath);
         lpPath = NULL;
 
-        if(RegQueryValueEx(hClassKey,
-                           _T("NetCfgInstanceId"),
-                           NULL,
-                           &dwType,
-                           NULL,
-                           &dwDataSize) == ERROR_SUCCESS)
+        if (RegQueryValueEx(hClassKey,
+                            _T("NetCfgInstanceId"),
+                            NULL,
+                            &dwType,
+                            NULL,
+                            &dwDataSize) == ERROR_SUCCESS)
         {
             lpKeyClass = (LPTSTR)HeapAlloc(ProcessHeap,
                                            0,
@@ -326,15 +331,15 @@ LPTSTR GetConnectionDescription(LPTSTR lpClass)
             if (lpKeyClass == NULL)
                 goto CLEANUP;
 
-            if(RegQueryValueEx(hClassKey,
-                               _T("NetCfgInstanceId"),
-                               NULL,
-                               &dwType,
-                               (PBYTE)lpKeyClass,
-                               &dwDataSize) != ERROR_SUCCESS)
+            if (RegQueryValueEx(hClassKey,
+                                _T("NetCfgInstanceId"),
+                                NULL,
+                                &dwType,
+                                (PBYTE)lpKeyClass,
+                                &dwDataSize) != ERROR_SUCCESS)
             {
-                lpKeyClass = NULL;
                 HeapFree(ProcessHeap, 0, lpKeyClass);
+                lpKeyClass = NULL;
                 continue;
             }
         }
@@ -346,12 +351,12 @@ LPTSTR GetConnectionDescription(LPTSTR lpClass)
             HeapFree(ProcessHeap, 0, lpKeyClass);
             lpKeyClass = NULL;
 
-            if(RegQueryValueEx(hClassKey,
-                               _T("DriverDesc"),
-                               NULL,
-                               &dwType,
-                               NULL,
-                               &dwDataSize) == ERROR_SUCCESS)
+            if (RegQueryValueEx(hClassKey,
+                                _T("DriverDesc"),
+                                NULL,
+                                &dwType,
+                                NULL,
+                                &dwDataSize) == ERROR_SUCCESS)
             {
                 lpConDesc = (LPTSTR)HeapAlloc(ProcessHeap,
                                               0,
@@ -359,19 +364,22 @@ LPTSTR GetConnectionDescription(LPTSTR lpClass)
                 if (lpConDesc == NULL)
                     goto CLEANUP;
 
-                if(RegQueryValueEx(hClassKey,
-                                   _T("DriverDesc"),
-                                   NULL,
-                                   &dwType,
-                                   (PBYTE)lpConDesc,
-                                   &dwDataSize) != ERROR_SUCCESS)
+                if (RegQueryValueEx(hClassKey,
+                                    _T("DriverDesc"),
+                                    NULL,
+                                    &dwType,
+                                    (PBYTE)lpConDesc,
+                                    &dwDataSize) != ERROR_SUCCESS)
                 {
+                    HeapFree(ProcessHeap, 0, lpConDesc);
                     lpConDesc = NULL;
                     goto CLEANUP;
                 }
             }
             else
+            {
                 lpConDesc = NULL;
+            }
 
             break;
         }
@@ -382,9 +390,9 @@ CLEANUP:
         RegCloseKey(hBaseKey);
     if (hClassKey != NULL)
         RegCloseKey(hClassKey);
-    if (lpConDesc != NULL)
+    if (lpPath != NULL)
         HeapFree(ProcessHeap, 0, lpPath);
-    if (lpConDesc != NULL)
+    if (lpKeyClass != NULL)
         HeapFree(ProcessHeap, 0, lpKeyClass);
 
     return lpConDesc;
@@ -419,7 +427,7 @@ VOID ShowInfo(BOOL bAll)
     }
     else
     {
-        if( ERROR_NO_DATA != ret )
+        if (ret != ERROR_NO_DATA)
         {
             DoFormatMessage(0);
             return;
@@ -427,7 +435,7 @@ VOID ShowInfo(BOOL bAll)
     }
 
     /* call GetNetworkParams to obtain the network info */
-    if(GetNetworkParams(pFixedInfo, &netOutBufLen) == ERROR_BUFFER_OVERFLOW)
+    if (GetNetworkParams(pFixedInfo, &netOutBufLen) == ERROR_BUFFER_OVERFLOW)
     {
         pFixedInfo = (FIXED_INFO *)HeapAlloc(ProcessHeap, 0, netOutBufLen);
         if (pFixedInfo == NULL)
@@ -475,6 +483,7 @@ VOID ShowInfo(BOOL bAll)
     while (pAdapter)
     {
         LPTSTR IntType, myConType;
+        BOOLEAN bConnected = TRUE;
 
         mibEntry.dwIndex = pAdapter->Index;
         GetIfEntry(&mibEntry);
@@ -489,12 +498,13 @@ VOID ShowInfo(BOOL bAll)
         /* check if the adapter is connected to the media */
         if (mibEntry.dwOperStatus != MIB_IF_OPER_STATUS_CONNECTED && mibEntry.dwOperStatus != MIB_IF_OPER_STATUS_OPERATIONAL)
         {
+            bConnected = FALSE;
             _tprintf(_T("\tMedia State . . . . . . . . . . . : Media disconnected\n"));
-            pAdapter = pAdapter->Next;
-            continue;
         }
-
-        _tprintf(_T("\tConnection-specific DNS Suffix. . : %s\n"), pFixedInfo->DomainName);
+        else
+        {
+            _tprintf(_T("\tConnection-specific DNS Suffix. . : %s\n"), pFixedInfo->DomainName);
+        }
 
         if (bAll)
         {
@@ -502,11 +512,20 @@ VOID ShowInfo(BOOL bAll)
             _tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), lpDesc);
             HeapFree(ProcessHeap, 0, lpDesc);
             _tprintf(_T("\tPhysical Address. . . . . . . . . : %s\n"), PrintMacAddr(pAdapter->Address));
-            if (pAdapter->DhcpEnabled)
-                _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
-            else
-                _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : No\n"));
-            _tprintf(_T("\tAutoconfiguration Enabled . . . . : \n"));
+            if (bConnected)
+            {
+                if (pAdapter->DhcpEnabled)
+                    _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
+                else
+                    _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : No\n"));
+                _tprintf(_T("\tAutoconfiguration Enabled . . . . : \n"));
+            }
+        }
+
+        if (!bConnected)
+        {
+            pAdapter = pAdapter->Next;
+            continue;
         }
 
         _tprintf(_T("\tIP Address. . . . . . . . . . . . : %s\n"), pAdapter->IpAddressList.IpAddress.String);
@@ -535,10 +554,10 @@ VOID ShowInfo(BOOL bAll)
             if (pAdapter->HaveWins)
             {
                 _tprintf(_T("\tPrimary WINS Server . . . . . . . : %s\n"), pAdapter->PrimaryWinsServer.IpAddress.String);
-                _tprintf(_T("\tSecondard WINS Server . . . . . . : %s\n"), pAdapter->SecondaryWinsServer.IpAddress.String);
+                _tprintf(_T("\tSecondary WINS Server . . . . . . : %s\n"), pAdapter->SecondaryWinsServer.IpAddress.String);
             }
 
-            if (pAdapter->DhcpEnabled)
+            if (pAdapter->DhcpEnabled && _tcscmp(pAdapter->DhcpServer.IpAddress.String, _T("255.255.255.255")))
             {
                 _tprintf(_T("\tLease Obtained. . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter->LeaseObtained)));
                 _tprintf(_T("\tLease Expires . . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter->LeaseExpires)));
@@ -577,15 +596,15 @@ VOID Release(LPTSTR Index)
             {
                 for (i = 0; i < pInfo->NumAdapters; i++)
                 {
-                     CopyMemory(&AdapterInfo, &pInfo->Adapter[i], sizeof(IP_ADAPTER_INDEX_MAP));
-                     _tprintf(_T("name - %S\n"), pInfo->Adapter[i].Name);
-
-                     /* Call IpReleaseAddress to release the IP address on the specified adapter. */
-                     if ((ret = IpReleaseAddress(&AdapterInfo)) != NO_ERROR)
-                     {
-                         _tprintf(_T("\nAn error occured while releasing interface %S : \n"), AdapterInfo.Name);
-                         DoFormatMessage(ret);
-                     }
+                    CopyMemory(&AdapterInfo, &pInfo->Adapter[i], sizeof(IP_ADAPTER_INDEX_MAP));
+                    _tprintf(_T("name - %ls\n"), pInfo->Adapter[i].Name);
+
+                    /* Call IpReleaseAddress to release the IP address on the specified adapter. */
+                    if ((ret = IpReleaseAddress(&AdapterInfo)) != NO_ERROR)
+                    {
+                        _tprintf(_T("\nAn error occured while releasing interface %ls : \n"), AdapterInfo.Name);
+                        DoFormatMessage(ret);
+                    }
                 }
 
                 HeapFree(ProcessHeap, 0, pInfo);
@@ -637,7 +656,7 @@ VOID Renew(LPTSTR Index)
 
         /* Make an initial call to GetInterfaceInfo to get
          * the necessary size into the ulOutBufLen variable */
-        if ( GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
+        if (GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
         {
             HeapFree(ProcessHeap, 0, pInfo);
             pInfo = (IP_INTERFACE_INFO *)HeapAlloc(ProcessHeap, 0, ulOutBufLen);
@@ -649,13 +668,12 @@ VOID Renew(LPTSTR Index)
         }
 
         /* Make a second call to GetInterfaceInfo to get the actual data we want */
-        if (GetInterfaceInfo(pInfo, &ulOutBufLen) == NO_ERROR )
+        if (GetInterfaceInfo(pInfo, &ulOutBufLen) == NO_ERROR)
         {
             for (i = 0; i < pInfo->NumAdapters; i++)
             {
                 CopyMemory(&AdapterInfo, &pInfo->Adapter[i], sizeof(IP_ADAPTER_INDEX_MAP));
-                _tprintf(_T("name - %S\n"), pInfo->Adapter[i].Name);
-
+                _tprintf(_T("name - %ls\n"), pInfo->Adapter[i].Name);
 
                 /* Call IpRenewAddress to renew the IP address on the specified adapter. */
                 if (IpRenewAddress(&AdapterInfo) != NO_ERROR)
@@ -714,7 +732,9 @@ VOID Usage(VOID)
                            Size))
             {
                 _tprintf(_T("%s"), lpUsage);
-            }            
+            }
+
+            HeapFree(ProcessHeap, 0, lpUsage);
         }
     }
 
@@ -737,41 +757,41 @@ int main(int argc, char *argv[])
     ProcessHeap = GetProcessHeap();
 
     /* Parse command line for options we have been given. */
-    if ( (argc > 1)&&(argv[1][0]=='/' || argv[1][0]=='-') )
+    if ((argc > 1) && (argv[1][0]=='/' || argv[1][0]=='-'))
     {
-        if( !_tcsicmp( &argv[1][1], _T("?") ))
+        if (!_tcsicmp(&argv[1][1], _T("?")))
         {
             DoUsage = TRUE;
         }
-        else if( !_tcsnicmp( &argv[1][1], _T("ALL"), _tcslen(&argv[1][1]) ))
+        else if (!_tcsnicmp(&argv[1][1], _T("ALL"), _tcslen(&argv[1][1])))
         {
            DoAll = TRUE;
         }
-        else if( !_tcsnicmp( &argv[1][1], _T("RELEASE"), _tcslen(&argv[1][1]) ))
+        else if (!_tcsnicmp(&argv[1][1], _T("RELEASE"), _tcslen(&argv[1][1])))
         {
             DoRelease = TRUE;
         }
-        else if( ! _tcsnicmp( &argv[1][1], _T("RENEW"), _tcslen(&argv[1][1]) ))
+        else if (!_tcsnicmp(&argv[1][1], _T("RENEW"), _tcslen(&argv[1][1])))
         {
             DoRenew = TRUE;
         }
-        else if( ! _tcsnicmp( &argv[1][1], _T("FLUSHDNS"), _tcslen(&argv[1][1]) ))
+        else if (!_tcsnicmp(&argv[1][1], _T("FLUSHDNS"), _tcslen(&argv[1][1])))
         {
             DoFlushdns = TRUE;
         }
-        else if( ! _tcsnicmp( &argv[1][1], _T("FLUSHREGISTERDNS"), _tcslen(&argv[1][1]) ))
+        else if (!_tcsnicmp(&argv[1][1], _T("FLUSHREGISTERDNS"), _tcslen(&argv[1][1])))
         {
             DoRegisterdns = TRUE;
         }
-        else if( ! _tcsnicmp( &argv[1][1], _T("DISPLAYDNS"), _tcslen(&argv[1][1]) ))
+        else if (!_tcsnicmp(&argv[1][1], _T("DISPLAYDNS"), _tcslen(&argv[1][1])))
         {
             DoDisplaydns = TRUE;
         }
-        else if( ! _tcsnicmp( &argv[1][1], _T("SHOWCLASSID"), _tcslen(&argv[1][1]) ))
+        else if (!_tcsnicmp(&argv[1][1], _T("SHOWCLASSID"), _tcslen(&argv[1][1])))
         {
             DoShowclassid = TRUE;
         }
-        else if( ! _tcsnicmp( &argv[1][1], _T("SETCLASSID"), _tcslen(&argv[1][1]) ))
+        else if (!_tcsnicmp(&argv[1][1], _T("SETCLASSID"), _tcslen(&argv[1][1])))
         {
             DoSetclassid = TRUE;
         }