From 235d575e341040e94b8480dca756209fcfc36a10 Mon Sep 17 00:00:00 2001
From: Black <yitianbuji@gmail.com>
Date: Sun, 22 Dec 2013 14:21:34 +0800
Subject: cpp codes removed.

---
 programs/ntldd/Makefile.in |   3 +-
 programs/ntldd/ntldd.cpp   | 109 ++++++++++++++++++++++-----------------------
 2 files changed, 56 insertions(+), 56 deletions(-)

diff --git a/programs/ntldd/Makefile.in b/programs/ntldd/Makefile.in
index 5e986c3..fd3f10e 100644
--- a/programs/ntldd/Makefile.in
+++ b/programs/ntldd/Makefile.in
@@ -1,6 +1,7 @@
 MODULE    = ntldd.exe
 APPMODE   = -mconsole
-IMPORTS   = stdc++ user32 
+IMPORTS   = msvcrt user32
+EXTRAINCL = -I$(top_srcdir)/include/msvcrt
 
 C_SRCS = \
 	ntldd.cpp
diff --git a/programs/ntldd/ntldd.cpp b/programs/ntldd/ntldd.cpp
index 0690b4e..c524985 100644
--- a/programs/ntldd/ntldd.cpp
+++ b/programs/ntldd/ntldd.cpp
@@ -1,84 +1,80 @@
 #include <stdio.h>
 #include <windows.h>
-#include <iostream>
-
-#include <vector>
-#include <map>
-#include <string>
+#include <malloc.h>
 #define _MAX_PATH 512
 
-typedef std::map<std::string, std::string> DepMapType;
-
 //------------------------------------------------------------------------------
 
-const std::string getDicectory ( const std::string &libPath )
-{
-    unsigned found = libPath.find_last_of ( "/\\" );
-    return libPath.substr ( 0,found+1 );
-}
-
-const DepMapType getDependencies ( const HMODULE hMod )
+int getDirectory ( char* libPath, char *dir )
 {
-    DepMapType depsMap;
-
-    IMAGE_DOS_HEADER* pDosHeader = ( IMAGE_DOS_HEADER* ) hMod;
-    IMAGE_OPTIONAL_HEADER * pOptHeader = ( IMAGE_OPTIONAL_HEADER* ) ( ( BYTE* ) hMod + pDosHeader->e_lfanew +24 );
-    IMAGE_IMPORT_DESCRIPTOR* pImportDesc = ( IMAGE_IMPORT_DESCRIPTOR* ) ( ( BYTE* ) hMod + pOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress );
+    char* last_slash= strrchr ( libPath,'/' );
+    char* last_backslash =strrchr ( libPath,'\\' );
 
-    while ( pImportDesc->FirstThunk )
-    {
-        char* dllName = ( char* ) ( ( BYTE* ) hMod + pImportDesc->Name );
+    if ( last_slash==NULL &&last_backslash==NULL )
+        return -1;
 
-        std::string dllPath = "not found";
-        HMODULE hModDep = ::LoadLibraryEx (
-                              dllName, NULL,
-                              DONT_RESOLVE_DLL_REFERENCES|LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
-                          );
-        if ( hModDep != 0 )
-        {
-            LPTSTR  strDLLPath = new TCHAR[_MAX_PATH];
-            ::GetModuleFileName ( hModDep, strDLLPath, _MAX_PATH );
-            dllPath = std::string ( strDLLPath );
-            delete strDLLPath;
-        }
-        FreeLibrary ( hModDep );
-        depsMap[std::string ( dllName )] = dllPath;
+    char *splitPosition= ( last_slash>last_backslash ) ?last_slash:last_backslash;
 
-        pImportDesc++;
-    }
+    strncpy ( dir,libPath, splitPosition-libPath );
 
-    return depsMap;
+    return 0;
 }
 
 //------------------------------------------------------------------------------
 
-int printDependencies ( const std::string libPath )
+int printDependencies ( char* libPath )
 {
-    std::string curDir=getDicectory ( libPath );
+//-->>
+    char *curDir= ( char* ) calloc ( _MAX_PATH,sizeof ( char ) );
 
-    SetCurrentDirectory ( curDir.c_str() );
+    int res=getDirectory ( libPath,curDir );
+    if ( res==-1 )
+        return -1;
 
+    SetCurrentDirectory ( curDir );
+    free ( curDir );
+//<<--
     HMODULE hMod = ::LoadLibraryEx (
-                       libPath.c_str(), NULL,
+                       libPath, NULL,
                        DONT_RESOLVE_DLL_REFERENCES|LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
                    );
     if ( hMod == 0 )
     {
         // Retrieves the last error message.
         DWORD   lastError = GetLastError();
-        char    buffer[1024];
+        char    *buffer= ( char* ) calloc ( 1024,sizeof ( char ) );
         FormatMessage ( FORMAT_MESSAGE_FROM_SYSTEM, 0, lastError, 0, buffer, 1024, 0 );
-        std::cerr << "\t" << buffer << std::endl;
+        fprintf ( stderr,"\t%s\n",buffer );
+        free ( buffer );
         return -1;
     }
-    const DepMapType& depMap = getDependencies ( hMod );
-    FreeLibrary ( hMod );
-    DepMapType::const_iterator iter;
-    for ( iter = depMap.begin(); iter != depMap.end(); ++iter )
+    IMAGE_DOS_HEADER* pDosHeader = ( IMAGE_DOS_HEADER* ) hMod;
+    IMAGE_OPTIONAL_HEADER * pOptHeader = ( IMAGE_OPTIONAL_HEADER* ) ( ( BYTE* ) hMod + pDosHeader->e_lfanew +24 );
+    IMAGE_IMPORT_DESCRIPTOR* pImportDesc = ( IMAGE_IMPORT_DESCRIPTOR* ) ( ( BYTE* ) hMod + pOptHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress );
+
+    LPTSTR  strDLLPath = ( LPTSTR ) calloc ( _MAX_PATH,sizeof ( LPSTR ) );
+    char* dllName=0;
+
+    while ( pImportDesc->FirstThunk )
     {
-        std::cout << "\t" << iter->first << " => " << iter->second << std::endl;
-    }
+        dllName = ( char* ) ( ( BYTE* ) hMod + pImportDesc->Name );
+
+        HMODULE hModDep= ::LoadLibraryEx (
+                             dllName, NULL,
+                             DONT_RESOLVE_DLL_REFERENCES|LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
+                         );
+        if ( hModDep != 0 )
+            ::GetModuleFileName ( hModDep, strDLLPath, _MAX_PATH );
+
+        if ( strlen ( strDLLPath ) !=0 )
+            printf ( "\t%s => %s \n",dllName, strDLLPath );
+        else
+            printf ( "\t%s => %s \n",dllName, ( char* ) "not found" );
 
+        FreeLibrary ( hModDep );
+        memset ( strDLLPath,'\0',_MAX_PATH );
+        pImportDesc++;
+    }
     return 0;
 }
 
@@ -86,8 +82,9 @@ int printDependencies ( const std::string libPath )
 
 int printUsage()
 {
-    std::cout << "ldd for Windows PE files" << std::endl;
-    std::cout << "usage:\n ldd FILE..." << std::endl;
+    char *usage="ldd for Windows PE files\n"
+                "usage:\n ldd FILE...";
+    printf ( "%s",usage );
     return -1;
 }
 
@@ -95,6 +92,7 @@ int printUsage()
 
 int main ( int argc, char** argv )
 {
+
     if ( argc <= 1 )
     {
         return printUsage();
@@ -102,11 +100,12 @@ int main ( int argc, char** argv )
     int res = 0;
     for ( int i = 1; i < argc; ++i )
     {
-        std::cout << argv[i] << std::endl;
+        if ( argc>2 )
+            printf ( "%s \n",argv[i] );
         res = printDependencies ( argv[i] );
     }
+
     return res;
 }
 
 //------------------------------------------------------------------------------
-
-- 
1.8.3.2

