/////////////////////////////////////////////////////////////////////////////// // jak.cpp // // // // Copyright (c) Jan Knepper, 1998. All Rights Reserved. // // Written by Jan Knepper // // // // Redistribution and use in source and binary forms, with or without // // modification, are permitted provided that the following conditions // // are met: // // 1. Redistributions of source code must retain the above copyright // // notice, this list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright // // notice, this list of conditions and the following disclaimer in the // // documentation and/or other materials provided with the distribution. // // 3. The name of the author may not be used to endorse or promote products // // derived from this software without specific prior written permission. // // // // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND // // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE // // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF // // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN // // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF // // THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include "Txt2RTF.h" #if defined(I_WIN_32) //#define DEBUG 1 #undef WIN32_LEAN_AND_MEAN #include #define Message(fmt,f,l,msg) static const char *THISFILE = __FILE__; void delay ( unsigned long milliseconds ) { Sleep ( milliseconds ); } void nosound () { } void sound ( unsigned long frequency ) { } typedef struct _BLOCK { size_t size; void *ptr; char *file; int line; struct _BLOCK *next; long check; } BLOCK; static BLOCK *_memblocklist = NULL; static const char *_memblockerror = "MEMORY ERROR %s %d : %s"; static void i_memblockinsert ( BLOCK * ); static void i_memblockdelete ( BLOCK * ); static BLOCK *i_memblockfind ( const void * ); static BLOCK *i_memblockfindin ( const void * ); static void i_memblockcheck ( BLOCK * ); static long i_memblockchecksum ( const BLOCK * ); static int i_memblockdata ( const BLOCK *, const void *, int * ); static int i_strnlen ( const char *, int ); static const char *_format = "%s %5d : %s"; int i_strcmp ( const char *file, int line, const char *s0, const char *s1 ) { BLOCK *s_block; BLOCK *d_block; int s_size; int d_size; if ( s0 == NULL ) { Message ( _format, file, line, "i_strcmp: argument 's0' == NULL" ); return ( -1 ); } if ( s1 == NULL ) { Message ( _format, file, line, "i_strcmp: argument 's1' == NULL" ); return ( 1 ); } return ( strcmp ( s0, s1 ) ); } char *i_strcpy ( const char *file, int line, char *destin, const char *source ) { BLOCK *s_block; BLOCK *d_block; int s_size; int d_size; int s_len; int d_len; if ( destin == NULL ) { Message ( _format, file, line, "i_strcpy: argument 'destin' == NULL" ); return ( NULL ); } if ( source == NULL ) { Message ( _format, file, line, "i_strcpy: argument 'source' == NULL" ); return ( NULL ); } // Look the 'source' and 'destin' pointers up. They may be allocated blocks, or even // halfway allocated blocks. if ( ( s_block = i_memblockfind ( source ) ) == NULL ) s_block = i_memblockfindin ( source ); if ( ( d_block = i_memblockfind ( destin ) ) == NULL ) d_block = i_memblockfindin ( destin ); s_size = i_memblockdata ( s_block, source, 0 ); d_size = i_memblockdata ( d_block, destin, 0 ); s_len = i_strnlen ( source, s_size ); d_len = i_strnlen ( destin, d_size ); if ( d_size >= s_len ) // No problem, just concatenate. return ( strcpy ( destin, source ) ); Message ( _format, file, line, "i_strcpy: d_size???, s_size???" ); iLog ( THISFILE, __LINE__, "d_size:%d, d_len:%d, s_size:%d, s_len:%d", d_size, d_len, s_size, s_len ); return ( strcpy ( destin, source ) ); } char *i_strncpy ( const char *file, int line, char *destin, const char *source, size_t size ) { BLOCK *s_block; BLOCK *d_block; int s_size; int d_size; int s_len; int d_len; if ( destin == NULL ) { Message ( _format, file, line, "i_strncpy: argument 'destin' == NULL" ); return ( NULL ); } if ( source == NULL ) { Message ( _format, file, line, "i_strncpy: argument 'source' == NULL" ); return ( NULL ); } // Look the 'source' and 'destin' pointers up. They may be allocated blocks, or even // halfway allocated blocks. if ( ( s_block = i_memblockfind ( source ) ) == NULL ) s_block = i_memblockfindin ( source ); if ( ( d_block = i_memblockfind ( destin ) ) == NULL ) d_block = i_memblockfindin ( destin ); i_memblockdata ( s_block, source, &s_size ); i_memblockdata ( d_block, destin, &d_size ); s_len = i_strnlen ( source, s_size ); d_len = i_strnlen ( destin, d_size ); if ( ( d_size >= size ) && // No problem, just concatenate. ( s_size >= size ) ) return ( strncpy ( destin, source, size ) ); if ( ( s_size == 0 ) && ( d_size >= size ) ) { Message ( _format, file, line, "i_strncpy: s_size???" ); iLog ( THISFILE, __LINE__, "d_size:%d, s_size:%d, size:%d", d_size, s_size, size ); return ( strncpy ( destin, source, size ) ); } if ( ( d_size == 0 ) && ( s_size >= size ) ) { Message ( _format, file, line, "i_strncpy: d_size???" ); iLog ( THISFILE, __LINE__, "d_size:%d, s_size:%d, size:%d", d_size, s_size, size ); return ( strncpy ( destin, source, size ) ); } Message ( _format, file, line, "i_strncpy: d_size???, s_size???" ); iLog ( THISFILE, __LINE__, "d_size:%d, s_size:%d, size:%de", d_size, s_size, size ); return ( strncpy ( destin, source, size ) ); } char *i_strcat ( const char *file, int line, char *destin, const char *source ) { BLOCK *s_block; BLOCK *d_block; int s_size; int d_size; int s_len; int d_len; if ( destin == NULL ) { Message ( _format, file, line, "i_strcat: argument 'destin' == NULL" ); return ( NULL ); } if ( source == NULL ) { Message ( _format, file, line, "i_strcat: argument 'source' == NULL" ); return ( NULL ); } // Look the 'source' and 'destin' pointers up. They may be allocated blocks, or even // halfway allocated blocks. if ( ( s_block = i_memblockfind ( source ) ) == NULL ) s_block = i_memblockfindin ( source ); if ( ( d_block = i_memblockfind ( destin ) ) == NULL ) d_block = i_memblockfindin ( destin ); i_memblockdata ( s_block, source, &s_size ); i_memblockdata ( d_block, destin, &d_size ); s_len = i_strnlen ( source, s_size ); d_len = i_strnlen ( destin, d_size ); if ( d_size > ( d_len + s_len ) ) // No problem, just concatenate. return ( strcat ( destin, source ) ); if ( d_size == 0 ) { Message ( _format, file, line, "i_strcat: d_size???" ); return ( strcat ( destin, source ) ); } if ( d_size > d_len ) { Message ( _format, file, line, "i_strcat: d_size > d_len" ); iLog ( THISFILE, __LINE__, "d_size:%d, d_len:%d, s_len:%d, 'strncat' with %d", d_size, d_len, s_len, d_size - d_len ); return ( strncat ( destin, source, d_size - d_len ) ); } Message ( _format, file, line, "i_strcat: do nothing!" ); iLog ( THISFILE, __LINE__, "d_size:%d, d_len:%d, s_size:%d, s_len:%d", d_size, d_len, s_size, s_len ); return ( destin ); } void *i_memcpy ( const char *file, int line, void *destin, const void *source, size_t size ) { BLOCK *s_block; BLOCK *d_block; int s_size; int d_size; if ( destin == NULL ) { Message ( _format, file, line, "i_memcpy: argument 'destin' == NULL" ); return ( NULL ); } if ( source == NULL ) { Message ( _format, file, line, "i_memcpy: argument 'source' == NULL" ); return ( NULL ); } if ( ( s_block = i_memblockfind ( source ) ) == NULL ) s_block = i_memblockfindin ( source ); if ( ( d_block = i_memblockfind ( destin ) ) == NULL ) d_block = i_memblockfindin ( destin ); i_memblockdata ( s_block, source, &s_size ); i_memblockdata ( d_block, destin, &d_size ); if ( ( d_size >= size ) && // No problem, just copy. ( s_size >= size ) ) return ( memcpy ( destin, source, size ) ); if ( ( s_size == 0 ) && ( d_size >= size ) ) { Message ( _format, file, line, "i_memcpy: s_size???" ); iLog ( THISFILE, __LINE__, "d_size:%d, s_size:%d, size:%d", d_size, s_size, size ); return ( memcpy ( destin, source, size ) ); } if ( ( d_size == 0 ) && ( s_size >= size ) ) { Message ( _format, file, line, "i_memcpy: d_size???" ); iLog ( THISFILE, __LINE__, "d_size:%d, s_size:%d, size:%d", d_size, s_size, size ); return ( memcpy ( destin, source, size ) ); } Message ( _format, file, line, "i_memcpy: d_size???, s_size???" ); iLog ( THISFILE, __LINE__, "d_size:%d, s_size:%d, size:%de", d_size, s_size, size ); return ( memcpy ( destin, source, size ) ); } int i_atoi ( const char *file, int line, const char *str ) { if ( str == NULL ) { Message ( _format, file, line, "i_atoi: with NULL pointer" ); return ( 0 ); } return ( atoi ( str ) ); } long i_atol ( const char *file, int line, const char *str ) { if ( str == NULL ) { Message ( _format, file, line, "i_atol: with NULL pointer" ); return ( 0 ); } return ( atol ( str ) ); } double i_atof ( const char *file, int line, const char *str ) { if ( str == NULL ) { Message ( _format, file, line, "i_atof: with NULL pointer" ); return ( 0 ); } return ( atof ( str ) ); } void *i_malloc ( const char *file, int line, size_t size ) { BLOCK *block; if ( size <= 0 ) { Message ( _memblockerror, file, line, "malloc with invalid block size" ); return ( NULL ); } block = ( BLOCK * ) calloc ( 1, sizeof ( BLOCK ) ); block -> size = size; block -> ptr = malloc ( size ); block -> file = strdup ( file ); block -> line = line; block -> next = NULL; i_memblockinsert ( block ); // Member 'next' wil be set here. i_memblockcheck ( block ); // Calculate check sum. return ( block -> ptr ); } void *i_calloc ( const char *file, int line, size_t items, size_t size ) { BLOCK *block; if ( size <= 0 ) { Message ( _memblockerror, file, line, "calloc with invalid block size" ); return ( NULL ); } if ( items <= 0 ) { Message ( _memblockerror, file, line, "calloc with invalid block number" ); return ( NULL ); } block = ( BLOCK * ) calloc ( 1, sizeof ( BLOCK ) ); block -> size = size; block -> ptr = calloc ( items, size ); block -> file = strdup ( file ); block -> line = line; block -> next = NULL; i_memblockinsert ( block ); // Member 'next' wil be set here. i_memblockcheck ( block ); // Calculate check sum. return ( block -> ptr ); } void *i_realloc ( const char *file, int line, void *ptr, size_t size ) { BLOCK *block; if ( ptr == NULL ) { Message ( _memblockerror, file, line, "realloc of NULL pointer" ); return ( NULL ); } if ( ptr == NULL ) { Message ( _memblockerror, file, line, "realloc with invalid block size" ); return ( NULL ); } if ( ( block = i_memblockfind ( ptr ) ) == NULL ) { Message ( _memblockerror, file, line, "realloc of unknown pointer" ); return ( NULL ); } block -> size = size; block -> ptr = realloc ( ptr, size ); free ( block -> file ); block -> file = strdup ( file ); block -> line = line; // i_memblockinsert ( block ); // Member 'next' wil be set here. i_memblockcheck ( block ); // Calculate check sum. return ( block -> ptr ); } void i_free ( const char *file, int line, void *ptr ) { BLOCK *block; if ( ptr == NULL ) { Message ( _memblockerror, file, line, "free of NULL pointer" ); return; } if ( ( block = i_memblockfind ( ptr ) ) == NULL ) { Message ( _memblockerror, file, line, "free of not know pointer" ); return; } i_memblockdelete ( block ); } char *i_strdup ( const char *file, int line, const char *str ) { BLOCK *block; if ( str == NULL ) { Message ( _memblockerror, file, line, "strdup of NULL pointer" ); return ( NULL ); } block = ( BLOCK * ) calloc ( 1, sizeof ( BLOCK ) ); // block -> size = strlen ( str ) + 1; block -> ptr = strdup ( str ); block -> size = strlen ( ( char * ) block -> ptr ) + 1; block -> file = strdup ( file ); block -> line = line; block -> next = NULL; i_memblockinsert ( block ); // Member 'next' wil be set here. i_memblockcheck ( block ); // Calculate check sum. return ( ( char * ) block -> ptr ); } char *i_tempnam ( const char *file, int line, const char *dir, const char *prefix ) { BLOCK *block; if ( dir == NULL ) { Message ( _memblockerror, file, line, "tempnam with dir of NULL" ); return ( NULL ); } if ( prefix == NULL ) { Message ( _memblockerror, file, line, "tempnam with prefix of NULL" ); return ( NULL ); } block = ( BLOCK * ) calloc ( 1, sizeof ( BLOCK ) ); // block -> size = size; block -> ptr = tempnam ( ( char * ) dir, ( char * ) prefix ); block -> size = strlen ( ( char * ) block -> ptr ) + 1; block -> file = strdup ( file ); block -> line = line; block -> next = NULL; i_memblockinsert ( block ); // Member 'next' wil be set here. i_memblockcheck ( block ); // Calculate check sum. return ( ( char * ) block -> ptr ); } void i_check () { BLOCK *help; help = _memblocklist; while ( help != 0 ) { iLog ( __FILE__, __LINE__, "i_check: this:%08lX, ptr:%08lX, size:%6d, next:%08lX", help, help -> ptr, help -> size, help -> next ); if ( help -> check != i_memblockchecksum ( help ) ) iLog ( __FILE__, __LINE__, "i_check: checksum error %ld != %ld!", help -> check, i_memblockchecksum ( help ) ); help = help -> next; } } void i_close () { } static void i_memblockinsert ( BLOCK *block ) { BLOCK *help; block -> next = _memblocklist; // JAK 19971020, just add at the head of the list. _memblocklist = block; /* if ( _memblocklist == NULL ) { _memblocklist = block; return; } help = _memblocklist; while ( help -> next != NULL ) help = help -> next; help -> next = block; */ } static void i_memblockdelete ( BLOCK *block ) { BLOCK *help; if ( _memblocklist == NULL ) return; if ( block == _memblocklist ) { _memblocklist = _memblocklist -> next; // Unlink. if ( _memblocklist != NULL ) i_memblockcheck ( _memblocklist ); // Recalculate check sum. free ( block -> ptr ); free ( block -> file ); free ( block ); return; } help = _memblocklist; // '_memblocklist' points to a block, so // there is a 'next' member! while ( ( help -> next != NULL ) && ( help -> next != block ) ) help = help -> next; if ( help -> next != NULL ) { help -> next = block -> next; // Unlink. i_memblockcheck ( help ); // Recalculate check sum. free ( block -> ptr ); free ( block -> file ); free ( block ); } } static BLOCK *i_memblockfind ( const void *ptr ) { BLOCK *help; if ( _memblocklist == NULL ) return ( NULL ); help = _memblocklist; while ( ( help != 0 ) && ( ptr != help -> ptr ) ) help = help -> next; return ( help ); } static BLOCK *i_memblockfindin ( const void *ptr ) { BLOCK *help; if ( _memblocklist == NULL ) return ( NULL ); help = _memblocklist; while ( help != 0 ) { if ( ( ptr >= help -> ptr ) && ( ptr < ( ( ( const char * ) help -> ptr ) + help -> size ) ) ) break; help = help -> next; } return ( help ); } static void i_memblockcheck ( BLOCK *block ) { block -> check = i_memblockchecksum ( block ); } static long i_memblockchecksum ( const BLOCK *block ) { return ( ( long ) block -> size + ( long ) block -> ptr + ( long ) block -> file + ( long ) block -> line + ( long ) block -> next ); } static int i_memblockdata ( const BLOCK *block, const void *ptr, int *size ) { int help = 0; if ( block != 0 ) // 'source' is allocated. { if ( block -> ptr == ptr ) // 'source' points to begin of block. help = block -> size; else // 'source' points in block. help = block -> size - ( ( ( char * ) ptr ) - ( ( char * ) block -> ptr ) ); } else help = 0; // Not known! if ( size != 0 ) *size = help; return ( help ); } static int i_strnlen ( const char *str, int size ) { int i = 0; if ( str == ( char * ) 0 ) return ( 0 ); if ( size == 0 ) return ( strlen ( str ) ); while ( ( i < size ) && ( *( str + i ) != '\0' ) ) i++; return ( i ); } char *novell_username () { static char username [ 80 ]; DWORD size = sizeof ( username ) - 1; if ( ! GetUserName ( username, &size ) ) memset ( username, 0, sizeof ( username ) ); #if defined(DEBUG) Message ( "novell_username %s", username ); #endif return ( username ); } static DWORD WINAPI DoWordPadThread ( LPVOID ); //#include void DoWordPad ( const char *file ) { // ShellExecute ( 0, 0, "WordPad", file, 0, SW_SHOWNORMAL ); // spawnl ( P_WAIT, "C:\\Program Files\\Windows NT\\Accessories\\WordPad.exe", file, NULL ); /* HANDLE thread; DWORD threadid; thread = CreateThread ( 0, NULL, DoWordPadThread, file, 0, &threadid ); SetThreadPriority ( thread, THREAD_PRIORITY_ABOVE_NORMAL ); CloseHandle ( thread ); */ if ( access ( file, R_OK ) == 0 ) { const char *rtf = Txt2RTF ( file ); // DoWordPadThread ( ( LPVOID ) file ); DoWordPadThread ( ( LPVOID ) rtf ); unlink ( rtf ); } else MessageBox ( 0, file, "WordPad", MB_OK ); } static DWORD WINAPI DoWordPadThread ( LPVOID _filename ) { const char *error_title = "ERROR"; const char *error_message = "Could not find WORDPAD.EXE!"; STARTUPINFO startupinfo; PROCESS_INFORMATION process_information; DWORD result; HKEY hkey; DWORD size; DWORD type; char wordpad [ 512 ]; char filename [ 256 ]; char command [ 512 ]; memset ( &startupinfo, 0, sizeof ( startupinfo ) ); startupinfo.cb = sizeof ( startupinfo ); strcpy ( filename, ( char * ) _filename ); strcpy ( command , " " ); strcat ( command , filename ); if ( RegOpenKeyEx ( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\WORDPAD.EXE", 0L, KEY_READ, &hkey ) != ERROR_SUCCESS ) { MessageBox ( 0, error_message, error_title, MB_OK ); return ( -1 ); } size = sizeof ( wordpad ) - 1; if ( RegQueryValueEx ( hkey, "", NULL, &type, ( UCHAR * ) wordpad, &size ) != ERROR_SUCCESS ) { MessageBox ( 0, error_message, error_title, MB_OK ); return ( -1 ); } RegCloseKey ( hkey ); // MessageBox ( 0, filename, "FILE", MB_OK ); // if ( ! CreateProcess ( "C:\\Program Files\\Windows NT\\Accessories\\WordPad.exe", command, NULL, NULL, FALSE, /* CREATE_SUSPENDED */ 0, NULL, NULL, &startupinfo, &process_information ) ) if ( ! CreateProcess ( wordpad, command, NULL, NULL, FALSE, /* CREATE_SUSPENDED */ 0, NULL, NULL, &startupinfo, &process_information ) ) MessageBox ( 0, "Could not start WordPad!", "ERROR", MB_OK ); else { CloseHandle ( process_information.hThread ); WaitForSingleObject ( process_information.hProcess, -1 ); GetExitCodeProcess ( process_information.hProcess, &result ); CloseHandle ( process_information.hProcess ); } unlink ( filename ); return ( 0 ); } enum OS_Type SystemType () { OSVERSIONINFO versioninfo; versioninfo.dwOSVersionInfoSize = sizeof ( OSVERSIONINFO ); if ( GetVersionEx ( &versioninfo ) ) { switch ( versioninfo.dwPlatformId ) { case VER_PLATFORM_WIN32_NT : return ( OS_Type_Windows_NT ); case VER_PLATFORM_WIN32s : return ( OS_Type_Windows_32s ); case VER_PLATFORM_WIN32_WINDOWS : return ( OS_Type_Windows_95 ); default : return ( OS_Type_UNKNOWN ); } } return ( OS_Type_UNKNOWN ); } #else char *novell_username () { static char username [ 80 ]; memset ( username, 0, sizeof ( username ) ); return ( username ); } enum OS_Type SystemType () { return ( OS_Type_DOS ); } #endif