Changeset 38413

Show
Ignore:
Timestamp:
09/05/10 03:52:54 (3 years ago)
Author:
saturday06
Message:

mendo3

Location:
lang/c/NetworkUpdater/trunk/Build
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/c/NetworkUpdater/trunk/Build/Copy.cpp

    r38404 r38413  
    33#endif 
    44 
     5#undef  STRICT 
     6#define STRICT 
    57#define _WIN32_WINNT 0x0500 /* Windows 2000 */ 
    68 
     
    6062        SYSTEMTIME st; 
    6163        int len = 0; 
    62         TCHAR* buf = NULL; 
     64        Buffer<TCHAR> buf; 
    6365        GetLocalTime(&st); 
    6466        len = GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, time_format, NULL, 0); 
    6567        if (len >= 1) { 
    66             buf = (TCHAR*)calloc(len * sizeof(TCHAR), 1); 
     68            buf.setBytes(len * sizeof(TCHAR)); 
    6769            if (buf) { 
    6870                GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, time_format, buf, len); 
    6971                WriteFile(StdOut, buf, (len - 1) * sizeof(buf[0]), &written, NULL); 
    70                 free(buf); 
    7172            } 
    7273        } 
     
    8990        } 
    9091    }; 
    91     HANDLE mutex; 
    92     HANDLE map; 
     92    Handle mutex; 
     93    Handle map; 
    9394 
    9495public: 
     
    103104        } 
    104105    }; 
    105     void lock() const { 
     106    void lock() { 
    106107        WaitForSingleObject(mutex, WAIT_LIMIT); 
    107108    } 
    108     void unlock() const { 
     109    void unlock() { 
    109110        ReleaseMutex(mutex); 
    110111    } 
    111112    SharedMemory() { 
    112         TCHAR* name = NULL; 
    113113        size_t i = 0; 
    114114        size_t len = 0; 
    115115         
    116116        len = GetCurrentDirectory(0, NULL); 
    117         name = (TCHAR*)calloc((len + (/* object sign */ 1)) * sizeof(TCHAR), 1); 
     117        Buffer<TCHAR> name(len); 
    118118        if (!name) { 
    119119            Log(_T("calloc() name failed")); 
    120             goto clean_up; 
     120            return; 
    121121        } 
    122122        GetCurrentDirectory(len, name); 
    123         Log(_T("cd=%s"), name); 
     123        Log(_T("cd=%s"), (TCHAR*)name); 
    124124        for (i = 0; i < len; ++i) { 
    125125            if (!IsCharAlphaNumeric(name[i])) { 
     
    128128        } 
    129129        name[0] = _T('M'); 
    130         Log(_T("mapping name=%s"), name); 
     130        Log(_T("mapping name=%s"), static_cast<TCHAR*>(name)); 
    131131         
    132132        map = CreateFileMapping( 
     
    139139 
    140140        name[0] = _T('X'); 
    141         Log(_T("mutex name=%s"), name); 
     141        Log(_T("mutex name=%s"), static_cast<TCHAR*>(name)); 
    142142        mutex = CreateMutex(NULL, FALSE, name); 
    143  
    144     clean_up: 
    145         free(name); 
    146143    } 
    147144 
    148145    ~SharedMemory() { 
    149         if (map) { 
    150             CloseHandle(map); 
    151         } 
    152         if (mutex) { 
    153             CloseHandle(mutex); 
    154         } 
    155     } 
    156  
    157     Data read() const { 
     146    } 
     147 
     148    Data read() { 
    158149        Data data; 
    159150        memset(&data, 0, sizeof(data)); 
    160         void* read = NULL; 
    161         read = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0); 
    162         if (read) { 
    163             memcpy(&data, read, sizeof(data)); 
    164             UnmapViewOfFile(read); 
     151        void* r = NULL; 
     152        r = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0); 
     153        if (r) { 
     154            memcpy(&data, r, sizeof(data)); 
     155            UnmapViewOfFile(r); 
    165156        } 
    166157        return data; 
    167158    } 
    168159 
    169     void write(Data data) const { 
    170         void* write = NULL; 
    171         write = MapViewOfFile(map, FILE_MAP_WRITE, 0, 0, 0); 
    172         if (write) { 
    173             memcpy(write, &data, sizeof(data)); 
    174             UnmapViewOfFile(write); 
     160    void write(Data data) { 
     161        void* w = NULL; 
     162        w = MapViewOfFile(map, FILE_MAP_WRITE, 0, 0, 0); 
     163        if (w) { 
     164            memcpy(w, &data, sizeof(data)); 
     165            UnmapViewOfFile(w); 
    175166        } 
    176167    } 
     
    413404BOOL CopyDirectory(const TCHAR* from, const TCHAR* to, SharedMemory& shared) 
    414405{ 
    415     TCHAR* glob = NULL; 
    416406    WIN32_FIND_DATA find_data; 
    417     HANDLE hFind = NULL; 
    418407    BOOL result = TRUE; 
    419408 
     
    423412    } 
    424413 
    425     glob = PathAppendDup(from, _T("*")); 
     414    Buffer<TCHAR> glob(PathAppendDup(from, _T("*"))); 
    426415    if (!glob) { 
    427416        Log(_T("CopyDirectory(): (!glob)")); 
     
    429418    /* Log(_T("glob: %s"), glob); */ 
    430419 
    431     hFind = FindFirstFile(glob, &find_data); 
    432     if (hFind == INVALID_HANDLE_VALUE) { 
     420    Handle hFind(FindFirstFile(glob, &find_data)); 
     421    if (!hFind) { 
    433422        Log(_T("CopyDirectory(): (hFind == INVALID_HANDLE_VALUE)")); 
    434423        return FALSE; 
     
    436425 
    437426    do { 
    438         TCHAR* next_from = NULL; 
    439         TCHAR* next_to = NULL; 
    440  
    441427        /* Log(_T("<%s>"), find_data.cFileName); */ 
    442428        if (lstrcmp(find_data.cFileName, _T(".")) == 0) { 
     
    447433        } 
    448434 
    449         next_from = PathAppendDup(from, find_data.cFileName); 
    450         next_to = PathAppendDup(to, find_data.cFileName); 
     435        Buffer<TCHAR> next_from(PathAppendDup(from, find_data.cFileName)); 
     436        Buffer<TCHAR> next_to(PathAppendDup(to, find_data.cFileName)); 
    451437 
    452438        /* Log(_T("%s -> %s"), next_from, next_to); */ 
     
    473459            } 
    474460        } 
    475  
    476         free(next_to); 
    477         free(next_from); 
    478461    } while(FindNextFile(hFind, &find_data)); 
    479462 
    480     FindClose(hFind); 
    481     free(glob); 
    482463    return result; 
    483464} 
    484465 
    485  
    486  
    487 void LaunchMainProcess2(const TCHAR* path, const TCHAR* cd, int complete) 
    488 { 
    489     BOOL se = FALSE; 
    490     DWORD e = 0; 
    491     int hInstApp = 0; 
    492     SHELLEXECUTEINFO sei; 
    493     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); 
    494  
    495     memset(&sei, 0, sizeof(sei)); 
    496     sei.cbSize = sizeof(sei); 
    497     sei.fMask  = 0; 
    498     sei.lpFile = path; 
    499     sei.lpVerb = _T("open"); 
    500     sei.lpDirectory = cd; 
    501     if (complete) { 
    502         sei.lpParameters = _T("UPDATE_COMPLETE"); 
    503     } else { 
    504         sei.lpParameters = _T("UPDATE_ERROR"); 
    505     } 
    506     Log(_T("ShellExecuteEx(): lpFile=%s, lpDirectory=%s"), sei.lpFile, sei.lpDirectory); 
    507     se = ShellExecuteEx(&sei); 
    508     e = GetLastError(); 
    509     hInstApp = (int)sei.hInstApp; /* http://msdn.microsoft.com/en-us/library/bb762154%28VS.85%29.aspx */ 
    510  
    511     Log(_T("ShellExecuteEx(): return=0x%x, GetLastError()=0x%x, hInstApp=0x%x"), 
    512             se, e, hInstApp); 
    513  
    514     if(!se || hInstApp <= 32) { 
    515         Log(_T("ShellExecuteEx(): error")); 
    516         goto clean_up; 
    517     } 
    518  
    519     Log(_T("ShellExecuteEx(): OK")); 
    520  
    521 clean_up: 
    522     CoUninitialize(); 
    523 } 
    524466 
    525467void LaunchMainProcess(const TCHAR* path, const TCHAR* cd, int complete) 
     
    533475    TCHAR error_arg[] = _T("UPDATE_ERROR"); 
    534476    int command_len = 0; 
    535     TCHAR* command = NULL; 
    536477 
    537478    if (complete) { 
     
    544485    command_len += lstrlen(arg); 
    545486 
    546     command = (TCHAR*)calloc(1, (unsigned)command_len * sizeof(TCHAR)); 
     487    Buffer<TCHAR> command((unsigned)command_len); 
    547488    if (!command) { 
    548489        goto clean_up; 
     
    559500    CloseHandle(pi.hProcess); 
    560501clean_up: 
    561     free(command); 
     502    ; 
    562503} 
    563504 
     
    567508    PROCESS_INFORMATION pi; 
    568509    DWORD e = 0; 
    569     TCHAR comspec[MAX_PATH]; 
    570510    TCHAR clean_command_format[] = _T("\"%s\" /c rd /s /q \"%s\""); 
    571     TCHAR* clean_command = NULL; 
    572     TCHAR* my_dir = NULL; 
     511 
    573512    size_t clean_command_len = 0; 
    574     TCHAR* system_temp_path = NULL; 
     513    Buffer<TCHAR> clean_command; 
     514    Buffer<TCHAR> my_dir; 
     515    Buffer<TCHAR> system_temp_path; 
     516    Buffer<TCHAR> comspec; 
     517    Handle hProcess; 
     518    Handle hThread; 
    575519 
    576520    memset(&pi, 0, sizeof(pi)); 
    577521 
    578     if (GetEnvironmentVariable(_T("COMSPEC"), comspec, sizeof(comspec) / sizeof(comspec[0])) == 0) { 
    579         Log(_T("no COMSPEC environment variable")); 
    580         goto clean_up; 
    581     } 
     522    { 
     523        size_t len = GetEnvironmentVariable(_T("COMSPEC"), NULL, 0); 
     524        if (len) { 
     525            Log(_T("no COMSPEC environment variable")); 
     526            return; 
     527        } 
     528        comspec.setBytes(len * sizeof(comspec[0])); 
     529        if (!comspec) { 
     530            Log(_T("calloc() comspec failed")); 
     531            return; 
     532        } 
     533        if (GetEnvironmentVariable(_T("COMSPEC"), comspec, len) == 0) { 
     534            Log(_T("no COMSPEC environment variable 2")); 
     535            return; 
     536        } 
     537    } 
     538 
    582539 
    583540    { 
    584541        size_t len = GetTempPath(0, NULL); 
    585         system_temp_path = (TCHAR*)calloc(1, len * sizeof(TCHAR)); 
     542        system_temp_path.setBytes(len * sizeof(TCHAR)); 
    586543        if (!system_temp_path) { 
    587544            Log(_T("calloc() system_temp_path failed")); 
    588             goto clean_up; 
     545            return; 
    589546        } 
    590547        GetTempPath(len, system_temp_path); 
     
    594551    { 
    595552        size_t len = GetCurrentDirectory(0, NULL); 
    596         my_dir = (TCHAR*)calloc(1, len * sizeof(TCHAR)); 
     553        my_dir.setBytes(len * sizeof(TCHAR)); 
    597554        if (!my_dir) { 
    598555            Log(_T("calloc() my_dir failed")); 
    599             goto clean_up; 
     556            return; 
    600557        } 
    601558        GetCurrentDirectory(len, my_dir); 
     
    604561 
    605562    clean_command_len = lstrlen(comspec) + lstrlen(clean_command_format) + lstrlen(my_dir); 
    606     clean_command = (TCHAR*)calloc(clean_command_len * sizeof(TCHAR), 1); 
     563    clean_command.setBytes(clean_command_len * sizeof(TCHAR)); 
    607564    if (!clean_command) { 
    608         goto clean_up; 
    609     } 
    610     wsprintf(clean_command, clean_command_format, comspec, my_dir); 
     565        return; 
     566    } 
     567    wsprintf(clean_command, clean_command_format, (TCHAR*)comspec, (TCHAR*)my_dir); 
    611568 
    612569    memset(&si, 0, sizeof(si)); 
     
    615572    si.wShowWindow = SW_HIDE; 
    616573    CreateProcess(NULL, clean_command, NULL, NULL, FALSE, 0, NULL, system_temp_path, &si, &pi); 
     574    hProcess = pi.hProcess; 
     575    hThread = pi.hThread; 
     576 
    617577    e = GetLastError(); 
    618578    Log(_T("CreateProcess(): \r\n command=%s \r\n cd=%s \r\n error=0x%x"), clean_command, system_temp_path, e); 
    619  
    620 clean_up: 
    621     if (pi.hThread) { 
    622         CloseHandle(pi.hThread); 
    623     } 
    624     if (pi.hProcess) { 
    625         CloseHandle(pi.hProcess); 
    626     } 
    627     free(system_temp_path); 
    628     free(my_dir); 
    629     free(clean_command); 
    630579} 
    631580 
     
    637586    DWORD bytes = 0; 
    638587    DWORD bytes_got = 0; 
    639     TCHAR* data = NULL; 
    640588    DWORD written = 0; 
    641     HANDLE config_file = CreateFile(ADMIN_OUTPUT_FILE_NAME, GENERIC_READ, FILE_SHARE_READ, NULL, 
    642                               OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
     589    Handle config_file(CreateFile(ADMIN_OUTPUT_FILE_NAME, GENERIC_READ, FILE_SHARE_READ, NULL, 
     590                                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); 
    643591    e = GetLastError(); 
    644592    if (config_file == INVALID_HANDLE_VALUE) { 
    645593        Log(_T("admin log open error 0x%x"), e); 
    646         goto log_end; 
     594        return; 
    647595    } 
    648596 
    649597    bytes = GetFileSize(config_file, NULL); 
    650     data = (TCHAR*)calloc(1, bytes + (/* null terminate */ sizeof(TCHAR))); 
     598    Buffer<char> data(bytes + (/* null terminate */ sizeof(TCHAR))); 
    651599    if (!data) { 
    652         goto log_end; 
     600        return; 
    653601    } 
    654602 
     
    656604        e = GetLastError(); 
    657605        Log(_T("admin log read error 0x%x"), e); 
    658         goto log_end; 
     606        return; 
    659607    } 
    660608    Log(_T("--- admin mode log start ---")); 
     
    665613        Log(_T("--- admin mode log error error=0x%x, written=0x%x ---"), e, written); 
    666614    } 
    667  
    668 log_end: 
    669     free(data); 
    670     CloseHandle(config_file); 
    671615} 
    672616 
     
    678622    SHELLEXECUTEINFO sei; 
    679623    size_t len = 33000; 
    680     TCHAR* my_path = NULL; 
     624    Buffer<TCHAR> my_path(len); 
     625    Handle hProcess; 
     626 
     627    struct CleanUp { 
     628        ~CleanUp() { 
     629            OutputAdminLog(); 
     630            CoUninitialize(); 
     631        } 
     632    } CleanUp; 
    681633 
    682634    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); 
    683     my_path = (TCHAR*)calloc(len * sizeof(TCHAR), 1); 
    684     GetModuleFileName(GetModuleHandle(NULL), my_path, len); 
     635    GetModuleFileName(GetModuleHandle(NULL), my_path, len - (1 /* for null terminate */)); 
    685636 
    686637    memset(&sei, 0, sizeof(sei)); 
     
    694645    e = GetLastError(); 
    695646    hInstApp = (int)sei.hInstApp; /* http://msdn.microsoft.com/en-us/library/bb762154%28VS.85%29.aspx */ 
     647    hProcess = sei.hProcess; 
    696648 
    697649    Log(_T("ShellExecuteEx(): return=0x%x, GetLastError()=0x%x, hInstApp=0x%x"), 
     
    700652    if(!se || hInstApp <= 32) { 
    701653        Log(_T("ShellExecuteEx(): error")); 
    702         goto clean_up; 
     654        return; 
    703655    } 
    704656 
     
    723675        break; 
    724676    } 
    725  
    726 clean_up: 
    727     if (sei.hProcess) { 
    728         CloseHandle(sei.hProcess); 
    729     } 
    730  
    731     CoUninitialize(); 
    732     free(my_path); 
    733  
    734     OutputAdminLog(); 
    735677} 
    736678 
    737679int WriteCheck(const TCHAR* dir) { 
    738680    unsigned int retry = 0; 
    739     TCHAR* check_path = NULL; 
    740681    for (retry = 10; retry != 0; --retry) { 
    741682        TCHAR name[1024]; 
     
    743684        wsprintf(name, _T("NetworkUpdater.AccessCheck.%1!x!.%2!x!.txt"),  
    744685            GetCurrentThreadId(), GetTickCount() - retry); 
    745         free(check_path); 
    746         check_path = PathAppendDup(dir, name); 
     686        Buffer<TCHAR> check_path(PathAppendDup(dir, name)); 
     687        if (!check_path) { 
     688            return 0; 
     689        } 
    747690        if (PathFileExists(check_path)) { 
    748691            continue; 
    749692        } 
    750693        if (check_path) { 
    751             HANDLE check = CreateFile( 
     694            Handle check(CreateFile( 
    752695                check_path, GENERIC_WRITE | DELETE, 0, NULL, CREATE_NEW,  
    753                 FILE_ATTRIBUTE_HIDDEN | FILE_FLAG_DELETE_ON_CLOSE, NULL); 
     696                FILE_ATTRIBUTE_HIDDEN | FILE_FLAG_DELETE_ON_CLOSE, NULL)); 
    754697            if (check != INVALID_HANDLE_VALUE) { 
    755698                TCHAR data[] = _T("write test data"); 
    756699                DWORD written = 0; 
    757700                if (WriteFile(check, data, lstrlen(data) * sizeof(data[0]), &written, NULL)) { 
    758                     CloseHandle(check); 
    759701                    break; 
    760702                } 
    761                 CloseHandle(check); 
    762             } 
    763         } 
    764     } 
    765     free(check_path); 
     703            } 
     704        } 
     705    } 
    766706 
    767707    return (retry != 0); 
     
    770710int main(void) 
    771711{ 
    772     HANDLE thread = NULL; 
    773     HANDLE config_file    = INVALID_HANDLE_VALUE; 
    774     HANDLE caller_process = NULL; 
    775     TCHAR* data = NULL; 
     712    Handle thread; 
     713    Handle config_file; 
     714    Handle caller_process; 
     715    Buffer<TCHAR> data; 
    776716    const TCHAR* new_program      = NULL; 
    777717    const TCHAR* new_program_cd   = NULL; 
     
    828768 
    829769        bytes = GetFileSize(config_file, NULL); 
    830         data = (TCHAR*)calloc(1, bytes + (/* null terminate */ sizeof(TCHAR))); 
     770        data.setBytes(bytes + (/* null terminate */ sizeof(TCHAR))); 
    831771        if (!data) { 
    832772            goto clean_up; 
     
    965905    } 
    966906 
    967     if (config_file != INVALID_HANDLE_VALUE) { 
    968         CloseHandle(config_file); 
    969     } 
    970  
    971     if (caller_process) { 
    972         CloseHandle(caller_process); 
    973     } 
    974  
    975     if (thread) { 
    976         CloseHandle(thread); 
    977     } 
    978  
    979907    if (data) { 
    980908        free(data); 
  • lang/c/NetworkUpdater/trunk/Build/MSW.am

    r38393 r38413  
    1717           -fstack-protector -Wno-missing-field-initializers 
    1818 
    19 copy_SOURCES  = Copy.c 
     19copy_SOURCES  = Copy.cpp 
    2020copy_CPPFLAGS = -DUNICODE -D_UNICODE -DSTRICT 
    21 copy_CFLAGS   = -O0 -g $(C_WARNINGS) 
     21copy_CXXFLAGS = -O0 -g $(CXX_WARNINGS) 
    2222copy_LDFLAGS  = -Wl,-nostdlib -L/usr/lib/w32api 
    2323 
  • lang/c/NetworkUpdater/trunk/Build/Nodefaultlib.h

    r38392 r38413  
    11 
    2 #define calloc(num, size) NUI_calloc(num, size) 
    3 #define free(memblock)    NUI_free(memblock) 
     2#define calloc(num, size) ((void*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num * size)) 
     3#define free(memblock)    (void)(memblock ? HeapFree(GetProcessHeap(), 0, memblock): 0) 
    44#define _wcsdup(s)        NUI_wcsdup(s) 
    55#define wcsdup(s)         NUI_wcsdup(s) 
     
    3636    } 
    3737    return dest; 
    38 } 
    39  
    40 /* http://msdn.microsoft.com/en-US/library/3f8w183e%28v=VS.80%29.aspx */ 
    41 void *NUI_calloc(size_t num, size_t size) 
    42 { 
    43     return (void*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num * size); 
    44 } 
    45  
    46 /* http://msdn.microsoft.com/en-US/library/we1whae7%28v=VS.80%29.aspx */ 
    47 void NUI_free(void *memblock) 
    48 { 
    49     if (memblock) { 
    50         HeapFree(GetProcessHeap(), 0, memblock); 
    51     } 
    5238} 
    5339 
     
    8773    return num; 
    8874} 
     75 
     76 
     77#ifdef __cplusplus 
     78 
     79#include <boost/utility.hpp> 
     80 
     81class Handle { 
     82    HANDLE h; 
     83 
     84public: 
     85    Handle(Handle& s) : h(NULL) { 
     86        DuplicateHandle(GetCurrentProcess(), 
     87            s.h, 
     88            GetCurrentProcess(), 
     89            &h, 
     90            0, 
     91            FALSE, 
     92            DUPLICATE_SAME_ACCESS); 
     93    } 
     94 
     95    Handle(HANDLE h_) : h(h_) { 
     96    } 
     97 
     98    Handle() : h(NULL) { 
     99    } 
     100 
     101    ~Handle() { 
     102        if (*this) { 
     103            CloseHandle(h); 
     104        } 
     105    } 
     106 
     107    operator HANDLE() { 
     108        return h; 
     109    } 
     110 
     111    operator bool() { 
     112        return h != INVALID_HANDLE_VALUE && h != NULL; 
     113    } 
     114 
     115    template <typename U> operator U() { 
     116        return static_cast<U>(h); 
     117    } 
     118}; 
     119 
     120template <typename T = char> 
     121class Buffer { 
     122    T* b; 
     123 
     124    explicit Buffer(const Buffer&) { 
     125    } 
     126 
     127    Buffer& operator=(const Buffer&) { 
     128        b = NULL; 
     129        return *this; 
     130    } 
     131 
     132public: 
     133     
     134    Buffer(size_t s) : b(NULL) { 
     135        setBytes(s * sizeof(T)); 
     136    } 
     137 
     138    Buffer(T* b_) : b(b_) { 
     139    } 
     140 
     141    Buffer& operator=(TCHAR* b_) { 
     142        b = b_; 
     143        return *this; 
     144    } 
     145 
     146    Buffer() : b(NULL) { 
     147    } 
     148 
     149    ~Buffer() { 
     150        free(b); 
     151    } 
     152 
     153    void setBytes(size_t bytes) { 
     154        b = (T*)calloc(bytes, 1); 
     155    } 
     156 
     157    operator T*() { 
     158        return b; 
     159    } 
     160 
     161    operator bool() { 
     162        return b != NULL; 
     163    } 
     164 
     165    template <typename U> operator U() { 
     166        return static_cast<U>(b); 
     167    } 
     168}; 
     169 
     170#endif 
     171 
  • lang/c/NetworkUpdater/trunk/Build/UnCab.cpp

    r38407 r38413  
     1#ifndef _UNICODE 
     2#error 
     3#endif 
     4 
     5#undef  STRICT 
     6#define STRICT 
     7 
    18#include <stdio.h> 
    29#include <windows.h> 
     
    2027FNSEEK(SeekProc); 
    2128 
    22 BOOL StartFDI(char* lpszOutputDirectory, char* lpszFileName); 
    23  
    2429int _tmain(int argc, TCHAR** argv) 
    2530{ 
    26     BOOL bResult = FALSE; 
    27  
    2831    if (argc < 2) { 
    2932        //printf("invalid argument count\n"); 
     
    3134    } 
    3235 
     36    ERF erf; 
     37    HFDI hfdi = FDICreate(AllocProc, FreeProc, OpenProc, ReadProc, WriteProc, CloseProc, SeekProc, cpu80386, &erf); 
     38    if (hfdi == NULL) { 
     39        //printf("FDICreate() failed erfOper=0x%x\n", erf.erfOper); 
     40        return FALSE; 
     41    } 
     42 
    3343    char dir[] = ".\\"; 
    34     char* file = NUI_TCharToMultiByteDup(argv[1]); 
     44    Buffer<char> file = NUI_TCharToMultiByteDup(argv[1]); 
     45     
     46    if (!FDICopy(hfdi, file, dir, 0, NotifyProc, NULL, NULL)) { 
     47        //printf("FDICopy() failed erfOper=0x%x\n", erf.erfOper); 
     48    } 
    3549 
    36     bResult = StartFDI(dir, file); 
    37     if (bResult) { 
    38         //printf("failed\n"); 
    39     } 
    40     return !bResult; 
     50    FDIDestroy(hfdi); 
    4151} 
    4252 
     
    5464} 
    5565 
    56 BOOL StartFDI(char* lpszDirectoryName, char* lpszFileName) 
    57 { 
    58     ERF erf; 
    59     BOOL bResult = FALSE; 
    60  
    61     HFDI hfdi = FDICreate(AllocProc, FreeProc, OpenProc, ReadProc, WriteProc, CloseProc, SeekProc, cpu80386, &erf); 
    62     if (hfdi == NULL) { 
    63         //printf("FDICreate() failed erfOper=0x%x\n", erf.erfOper); 
    64         return FALSE; 
    65     } 
    66  
    67     bResult = FDICopy(hfdi, lpszFileName, lpszDirectoryName, 0, NotifyProc, NULL, NULL); 
    68     if (!bResult) { 
    69         //printf("FDICopy() failed erfOper=0x%x\n", erf.erfOper); 
    70     } 
    71  
    72     FDIDestroy(hfdi); 
    73  
    74     return bResult; 
    75 } 
    76  
    7766FNFDINOTIFY(NotifyProc) 
    7867{ 
     
    8069 
    8170    case fdintCOPY_FILE: { 
    82         char szFileName[1024]; 
    83         lstrcpyA(szFileName, pfdin->psz1); 
    84         HANDLE hFile = CreateFileA(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
    85         if (hFile == INVALID_HANDLE_VALUE) { 
     71        Buffer<char> path(lstrlenA(pfdin->psz1) + (1 /* null terminate */)); 
     72        if (!path) { 
     73            return -1; 
     74        } 
     75        lstrcpyA(path, pfdin->psz1); 
     76        HANDLE file = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
     77        if (file == INVALID_HANDLE_VALUE) { 
    8678            //printf("CreateFileA() failed szFileName=%s\n", szFileName); 
    8779            return -1; 
    8880        } 
    89  
    90         return (int)hFile; 
     81        return (int)file; 
    9182    } 
    9283 
    9384    case fdintCLOSE_FILE_INFO: { 
    94         char szFileName[1024]; 
    9585        FILETIME fileTime; 
    9686        FILETIME localFileTime; 
     
    10292        CloseHandle((HANDLE)pfdin->hf); 
    10393 
    104         lstrcpyA(szFileName, pfdin->psz1); 
    105         SetFileAttributesA(szFileName, pfdin->attribs); 
     94        Buffer<char> path(lstrlenA(pfdin->psz1) + (1 /* null terminate */)); 
     95        lstrcpyA(path, pfdin->psz1); 
     96        SetFileAttributesA(path, pfdin->attribs); 
    10697 
    10798        return TRUE; 
     
    128119FNOPEN(OpenProc) 
    129120{ 
    130     HANDLE hFile = NULL; 
    131121    DWORD dwDesiredAccess = 0;  
    132122    DWORD dwCreationDisposition = 0; 
     
    134124    UNREFERENCED_PARAMETER(pmode); 
    135125 
    136     if ( oflag & _O_RDWR ) 
    137     { 
     126    if (oflag & _O_RDWR) { 
    138127        dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; 
    139     } 
    140     else if ( oflag & _O_WRONLY ) 
    141     { 
     128    } else if (oflag & _O_WRONLY) { 
    142129        dwDesiredAccess = GENERIC_WRITE; 
    143     } 
    144     else 
    145     { 
     130    } else { 
    146131        dwDesiredAccess = GENERIC_READ; 
    147132    } 
    148133 
    149     if ( oflag & _O_CREAT ) 
    150     { 
     134    if (oflag & _O_CREAT) { 
    151135        dwCreationDisposition = CREATE_ALWAYS; 
    152     } 
    153     else 
    154     { 
     136    } else { 
    155137        dwCreationDisposition = OPEN_EXISTING; 
    156138    } 
    157139 
    158     hFile = CreateFileA(pszFile,  
     140    HANDLE hFile = CreateFileA(pszFile,  
    159141                        dwDesiredAccess, 
    160142                        FILE_SHARE_READ, 
     
    172154{ 
    173155    DWORD dwBytesRead = 0; 
    174     if (ReadFile((HANDLE)hf, pv, cb, &dwBytesRead, NULL) == FALSE) 
    175     { 
     156    if (ReadFile((HANDLE)hf, pv, cb, &dwBytesRead, NULL) == FALSE) { 
    176157        dwBytesRead = (DWORD)-1L; 
    177158    }     
     
    183164{ 
    184165    DWORD dwBytesWritten = 0; 
    185     if (WriteFile((HANDLE)hf, pv, cb, &dwBytesWritten, NULL) == FALSE) 
    186     { 
     166    if (WriteFile((HANDLE)hf, pv, cb, &dwBytesWritten, NULL) == FALSE) { 
    187167        dwBytesWritten = (DWORD)-1; 
    188168    } 
     
    192172FNCLOSE(CloseProc) 
    193173{ 
    194     return ( CloseHandle((HANDLE)hf) == TRUE ) ? 0 : -1; 
     174    return (CloseHandle((HANDLE)hf) == TRUE) ? 0 : -1; 
    195175} 
    196  
    197176 
    198177FNSEEK(SeekProc) 
     
    200179    return SetFilePointer((HANDLE)hf, dist, 0, seektype); 
    201180} 
    202  
    203  
    204  
    205