Commit d3576a9f authored by Jon Griffiths's avatar Jon Griffiths Committed by Alexandre Julliard

- Implemented 73 CRT functions

- Reimplemented file I/O using Win32 calls - Set errno/doserrno in most calls
parent b56a8df9
......@@ -8,7 +8,14 @@ IMPORTS = kernel32 ntdll
C_SRCS = \
crtdll_main.c \
dir.c \
exit.c \
file.c \
mbstring.c \
memory.c \
spawn.c \
string.c \
time.c \
wcstring.c
@MAKE_DLL_RULES@
......
/*
* CRTDLL drive/directory functions
*
* Copyright 1996,1998 Marcus Meissner
* Copyright 1996 Jukka Iivonen
* Copyright 1997,2000 Uwe Bonnes
* Copyright 2000 Jon Griffiths
*
*
* Implementation Notes:
* MT Safe.
*/
#include "crtdll.h"
#include <errno.h>
#include "drive.h"
#include <time.h>
#include "file.h"
DEFAULT_DEBUG_CHANNEL(crtdll);
/* INTERNAL: Translate find_t to PWIN32_FIND_DATAA */
static void __CRTDLL__fttofd(LPWIN32_FIND_DATAA fd, find_t* ft);
static void __CRTDLL__fttofd(LPWIN32_FIND_DATAA fd, find_t* ft)
{
static DWORD dummy;
/* Tested with crtdll.dll Version 2.50.4170 (NT) from win98 SE:
* attrib 0x80 (FILE_ATTRIBUTE_NORMAL)is returned as 0.
*/
if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
ft->attrib = 0;
else
ft->attrib = fd->dwFileAttributes;
ft->time_create = DOSFS_FileTimeToUnixTime(&fd->ftCreationTime,&dummy);
ft->time_access = DOSFS_FileTimeToUnixTime(&fd->ftLastAccessTime,&dummy);
ft->time_write = DOSFS_FileTimeToUnixTime(&fd->ftLastWriteTime,&dummy);
ft->size = fd->nFileSizeLow;
strcpy(ft->name, fd->cFileName);
}
/*********************************************************************
* _chdir (CRTDLL.51)
*
* Change the current directory.
*
* PARAMS
* newdir [in] Directory to change to
*
* RETURNS
* Sucess: 0
*
* Failure: -1
*/
INT __cdecl CRTDLL__chdir(LPCSTR newdir)
{
if (!SetCurrentDirectoryA(newdir))
{
__CRTDLL__set_errno(newdir?GetLastError():0);
return -1;
}
return 0;
}
/*********************************************************************
* _chdrive (CRTDLL.52)
*
* Change the current drive.
*
* PARAMS
* newdrive [in] new drive to change to, A: =1, B: =2, etc
*
* RETURNS
* Sucess: 0
*
* Failure: 1
*/
BOOL __cdecl CRTDLL__chdrive(INT newdrive)
{
if (!DRIVE_SetCurrentDrive(newdrive-1))
{
__CRTDLL__set_errno(GetLastError());
if (newdrive <= 0)
CRTDLL_errno = EACCES;
return -1;
}
return 0;
}
/*********************************************************************
* _findclose (CRTDLL.098)
*
* Free the resources from a search handle created from _findfirst.
*
* PARAMS
* hand [in]: Search handle to close
*
* RETURNS
* Success: 0
*
* Failure: -1
*/
INT __cdecl CRTDLL__findclose(DWORD hand)
{
TRACE(":handle %ld\n",hand);
if (!FindClose((HANDLE)hand))
{
__CRTDLL__set_errno(GetLastError());
return -1;
}
return 0;
}
/*********************************************************************
* _findfirst (CRTDLL.099)
*
* Create and return a search handle for iterating through a file and
* directory list.
*
* PARAMS
* fspec [in] File specification string for search, e.g "C:\*.BAT"
*
* ft [out] A pointer to a find_t structure to populate.
*
* RETURNS
* Success: A handle for the search, suitable for passing to _findnext
* or _findclose. Populates the members of ft with the details
* of the first matching file.
*
* Failure: -1.
*/
DWORD __cdecl CRTDLL__findfirst(LPCSTR fspec, find_t* ft)
{
WIN32_FIND_DATAA find_data;
HANDLE hfind;
hfind = FindFirstFileA(fspec, &find_data);
if (hfind == INVALID_HANDLE_VALUE)
{
__CRTDLL__set_errno(GetLastError());
return -1;
}
__CRTDLL__fttofd(&find_data,ft);
TRACE(":got handle %d\n",hfind);
return hfind;
}
/*********************************************************************
* _findnext (CRTDLL.100)
*
* Return the next matching file/directory from a search hadle.
*
* PARAMS
* hand [in] Search handle from a pervious call to _findfirst
*
* ft [out] A pointer to a find_t structure to populate.
*
* RETURNS
* Success: 0. Populates the members of ft with the details
* of the first matching file
*
* Failure: -1
*/
INT __cdecl CRTDLL__findnext(DWORD hand, find_t * ft)
{
WIN32_FIND_DATAA find_data;
if (!FindNextFileA(hand, &find_data))
{
SetLastError(ERROR_INVALID_DRIVE);
__CRTDLL__set_errno(GetLastError());
return -1;
}
__CRTDLL__fttofd(&find_data,ft);
return 0;
}
/*********************************************************************
* _getcwd (CRTDLL.120)
*
* Get the current directory.
*
* PARAMS
* buf [out] A buffer to place the current directory name in
*
* size [in] The size of buf.
*
* RETURNS
* Success: buf, or if buf is NULL, an allocated buffer
*
* Failure: NULL
*/
CHAR* __cdecl CRTDLL__getcwd(LPSTR buf, INT size)
{
char dir[_MAX_PATH];
int dir_len = GetCurrentDirectoryA(_MAX_PATH,dir);
if (dir_len < 1)
return NULL; /* FIXME: Real return value untested */
if (!buf)
{
if (size < 0)
return CRTDLL__strdup(dir);
return __CRTDLL__strndup(dir,size);
}
if (dir_len >= size)
{
CRTDLL_errno = ERANGE;
return NULL; /* buf too small */
}
strcpy(buf,dir);
return buf;
}
/*********************************************************************
* _getdcwd (CRTDLL.121)
*
* Get the current directory on a drive. A: =1, B: =2, etc.
* Passing drive 0 means the current drive.
*/
CHAR* __cdecl CRTDLL__getdcwd(INT drive,LPSTR buf, INT size)
{
static CHAR* dummy;
if (!drive || --drive == DRIVE_GetCurrentDrive())
return CRTDLL__getcwd(buf,size); /* current */
else
{
char dir[_MAX_PATH];
char drivespec[4] = {'A', ':', '\\', 0};
int dir_len;
drivespec[0] += drive;
if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
{
CRTDLL_errno = EACCES;
return NULL;
}
dir_len = GetFullPathNameA(drivespec,_MAX_PATH,dir,&dummy);
if (dir_len >= size || dir_len < 1)
{
CRTDLL_errno = ERANGE;
return NULL; /* buf too small */
}
if (!buf)
return CRTDLL__strdup(dir); /* allocate */
strcpy(buf,dir);
}
return buf;
}
/*********************************************************************
* _getdiskfree (CRTDLL.122)
*
* Get free disk space on given drive or the current drive.
*
*/
UINT __cdecl CRTDLL__getdiskfree(UINT disk, diskfree_t* d)
{
char drivespec[4] = {'@', ':', '\\', 0};
DWORD ret[4];
UINT err;
if (disk > 26)
return ERROR_INVALID_PARAMETER; /* CRTDLL doesn't set errno here */
drivespec[0] += disk; /* make a drive letter */
if (GetDiskFreeSpaceA(disk==0?NULL:drivespec,ret,ret+1,ret+2,ret+3))
{
d->cluster_sectors = (unsigned)ret[0];
d->sector_bytes = (unsigned)ret[1];
d->available = (unsigned)ret[2];
d->num_clusters = (unsigned)ret[3];
return 0;
}
err = GetLastError();
__CRTDLL__set_errno(err);
return err;
}
/*********************************************************************
* _getdrive (CRTDLL.124)
*
* Return current drive, A: =1, B: =2, etc
*/
INT __cdecl CRTDLL__getdrive(VOID)
{
return DRIVE_GetCurrentDrive() + 1;
}
/*********************************************************************
* _mkdir (CRTDLL.234)
*
* Create a directory.
*/
INT __cdecl CRTDLL__mkdir(LPCSTR newdir)
{
if (CreateDirectoryA(newdir,NULL))
return 0;
__CRTDLL__set_errno(GetLastError());
return -1;
}
/*********************************************************************
* _rmdir (CRTDLL.255)
*
* Delete a directory
*
*/
INT __cdecl CRTDLL__rmdir(LPSTR dir)
{
if (RemoveDirectoryA(dir))
return 0;
__CRTDLL__set_errno(GetLastError());
return -1;
}
/*
* CRTDLL exit/abort/atexit functions
*
* Copyright 1996,1998 Marcus Meissner
* Copyright 1996 Jukka Iivonen
* Copyright 1997,2000 Uwe Bonnes
* Copyright 2000 Jon Griffiths
*
* exit functions differ in whether they perform cleanup
* and whether they return to the caller (really!).
* return do
* Name to caller? cleanup?
* _c_exit Y N
* _cexit Y Y
* _exit N N
* exit N Y
*
* Implementation Notes:
* Not MT Safe - Adding/Executing exit() functions should be locked
* for MT safety.
*
* FIXME:
* Need a better way of printing errors for GUI programs(MsgBox?).
* Is there really a difference between onexit/atexit?
*/
#include "crtdll.h"
#include <errno.h>
#include "process.h"
DEFAULT_DEBUG_CHANNEL(crtdll);
/* INTERNAL: Table of registered atexit() functions */
/* FIXME: This should be dynamically allocated */
#define CRTDLL_ATEXIT_TABLE_SIZE 16
static atexit_function atexit_table[CRTDLL_ATEXIT_TABLE_SIZE];
static int atexit_registered = 0; /* Points to free slot */
/* INTERNAL: call atexit functions */
void __CRTDLL__call_atexit(VOID);
void __CRTDLL__call_atexit(VOID)
{
/* Last registered gets executed first */
while (atexit_registered > 0)
{
atexit_registered--;
TRACE(":call function (%p)\n",atexit_table[atexit_registered]);
(*atexit_table[atexit_registered])();
}
}
/*********************************************************************
* __dllonexit (CRTDLL.25)
*/
VOID __cdecl CRTDLL___dllonexit ()
{
FIXME("stub\n");
}
/*********************************************************************
* _abnormal_termination (CRTDLL.36)
*
* Check if execution is processing during an exception.
*/
INT __cdecl CRTDLL__abnormal_termination(VOID)
{
TRACE("(void)\n");
return 0; /* FIME: Can we determine if we are in an exception? */
}
/*********************************************************************
* _amsg_exit (CRTDLL.040)
*
* Print an error message and terminate execution. Returns 255 to the
* host OS.
*/
VOID __cdecl CRTDLL__amsg_exit(INT err)
{
CRTDLL_fprintf(CRTDLL_stderr,"\nrun-time error:\nError Code %d\n",err);
CRTDLL__exit(255);
}
/*********************************************************************
* _assert (CRTDLL.041)
*
* Print an assertion message and call abort(). Really only present
* for win binaries. Winelib programs would typically use libc's
* version.
*/
VOID __cdecl CRTDLL__assert(LPVOID str, LPVOID file, UINT line)
{
CRTDLL_fprintf(CRTDLL_stderr,"Assertion failed: %s, file %s, line %d\n\n",
(char*)str,(char*)file, line);
CRTDLL_abort();
}
/*********************************************************************
* _c_exit (CRTDLL.047)
*/
VOID __cdecl CRTDLL__c_exit(VOID)
{
FIXME("not calling CRTDLL cleanup\n");
/* dont exit, return to caller */
}
/*********************************************************************
* _cexit (CRTDLL.049)
*/
VOID __cdecl CRTDLL__cexit(VOID)
{
FIXME("not calling CRTDLL cleanup\n");
/* dont exit, return to caller */
}
/*********************************************************************
* _exit (CRTDLL.087)
*/
VOID __cdecl CRTDLL__exit(LONG ret)
{
TRACE(":exit code %ld\n",ret);
CRTDLL__c_exit();
ExitProcess(ret);
}
/*********************************************************************
* _onexit (CRTDLL.236)
*
* Register a function to be called when the process terminates.
*/
atexit_function __cdecl CRTDLL__onexit( atexit_function func)
{
TRACE("registering function (%p)\n",func);
if (func && atexit_registered <= CRTDLL_ATEXIT_TABLE_SIZE - 1)
{
atexit_table[atexit_registered] = (atexit_function)func;
atexit_registered++;
return func; /* successful */
}
ERR(":Too many onexit() functions, or NULL function - not registered!\n");
return NULL;
}
/*********************************************************************
* exit (CRTDLL.359)
*/
void __cdecl CRTDLL_exit(DWORD ret)
{
TRACE(":exit code %ld\n",ret);
__CRTDLL__call_atexit();
CRTDLL__cexit();
ExitProcess(ret);
}
/*********************************************************************
* abort (CRTDLL.335)
*
* Terminate the progam with an abnormal termination message. Returns
* 3 to the host OS.
*/
VOID __cdecl CRTDLL_abort()
{
CRTDLL_fprintf(CRTDLL_stderr,"\nabnormal program termination\n");
CRTDLL__exit(3);
}
/*********************************************************************
* atexit (CRTDLL.345)
*
* Register a function to be called when the process terminates.
*/
INT __cdecl CRTDLL_atexit( atexit_function func)
{
return CRTDLL__onexit(func) == func ? 0 : -1;
}
......@@ -4,12 +4,31 @@
* Copyright 1999 Alexandre Julliard
*/
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "crtdll.h"
DEFAULT_DEBUG_CHANNEL(crtdll);
/*********************************************************************
* _mbsicmp (CRTDLL.204)
*/
int __cdecl CRTDLL__mbsicmp(unsigned char *x,unsigned char *y)
{
do {
if (!*x)
return !!*y;
if (!*y)
return !!*x;
/* FIXME: MBCS handling... */
if (*x!=*y)
return 1;
x++;
y++;
} while (1);
}
/*********************************************************************
* CRTDLL__mbsinc (CRTDLL.205)
*/
......@@ -31,6 +50,17 @@ INT __cdecl CRTDLL__mbslen( LPCSTR str )
}
/*********************************************************************
* _mbsrchr (CRTDLL.223)
*/
LPSTR __cdecl CRTDLL__mbsrchr(LPSTR s,CHAR x)
{
/* FIXME: handle multibyte strings */
return strrchr(s,x);
}
/*********************************************************************
* CRTDLL_mbtowc (CRTDLL.430)
*/
......@@ -44,3 +74,16 @@ INT __cdecl CRTDLL_mbtowc( WCHAR *dst, LPCSTR str, INT n )
if (n >= 2 && IsDBCSLeadByte(*str) && str[1]) return 2;
return 1;
}
/*********************************************************************
* _mbccpy (CRTDLL.??)
*
* Copy one multibyte character to another
*/
VOID __cdecl CRTDLL__mbccpy(LPSTR dest, LPSTR src)
{
FIXME("MBCS copy treated as ASCII\n");
*dest = *src;
}
/*
* CRTDLL memory functions
*
* Copyright 1996,1998 Marcus Meissner
* Copyright 1996 Jukka Iivonen
* Copyright 1997,2000 Uwe Bonnes
* Copyright 2000 Jon Griffiths
*
* Implementation Notes:
* MT Safe.
*/
#include "crtdll.h"
DEFAULT_DEBUG_CHANNEL(crtdll);
static new_handler_type new_handler;
/*********************************************************************
* new (CRTDLL.001)
*
* Allocate memory.
*/
LPVOID __cdecl CRTDLL_new(DWORD size)
{
VOID* result;
if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
(*new_handler)();
return result;
}
/*********************************************************************
* delete (CRTDLL.002)
*
* Free memory created with new.
*/
VOID __cdecl CRTDLL_delete(LPVOID ptr)
{
HeapFree(GetProcessHeap(),0,ptr);
}
/*********************************************************************
* set_new_handler(CRTDLL.003)
*/
new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
{
new_handler_type old_handler = new_handler;
new_handler = func;
return old_handler;
}
/*********************************************************************
* _expand (CRTDLL.088)
*
* Increase the size of a block of memory allocated with malloc()
* or realloc().
*/
LPVOID __cdecl CRTDLL__expand(LPVOID ptr, INT size)
{
return HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, ptr, size );
}
/*********************************************************************
* _msize (CRTDLL.234)
*
* Return the actual used size of an allocated block of memory.
*
*/
LONG __cdecl CRTDLL__msize(LPVOID mem)
{
LONG size = HeapSize(GetProcessHeap(),0,mem);
if (size == -1)
{
WARN(":Probably called with non wine-allocated memory, ret = -1\n");
/* At least the win98/nt crtdlls also return -1 in this case */
}
return size;
}
/*********************************************************************
* calloc (CRTDLL.350)
*
* Allocate memory from the heap and initialise it to zero.
*/
LPVOID __cdecl CRTDLL_calloc(DWORD size, DWORD count)
{
return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
}
/*********************************************************************
* free (CRTDLL.375)
*
* Free a block of memory allocated with malloc()
*/
VOID __cdecl CRTDLL_free(LPVOID ptr)
{
HeapFree(GetProcessHeap(),0,ptr);
}
/*********************************************************************
* malloc (CRTDLL.424)
*
* Alocate memory from the heap.
*/
LPVOID __cdecl CRTDLL_malloc(DWORD size)
{
LPVOID ret = HeapAlloc(GetProcessHeap(),0,size);
if (!ret)
__CRTDLL__set_errno(GetLastError());
return ret;
}
/*********************************************************************
* realloc (CRTDLL.444)
*
* Resize a block of memory allocated with malloc() or realloc().
*/
LPVOID __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
{
return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
}
/*
* CRTDLL spawn functions
*
* Copyright 1996,1998 Marcus Meissner
* Copyright 1996 Jukka Iivonen
* Copyright 1997,2000 Uwe Bonnes
* Copyright 2000 Jon Griffiths
*
* These functions differ in whether they pass arguments as an array
* (v in the name) or as varags (l in the name), whether they
* seach the path (p in the name) and/or whether they take an
* environment (e in the name) or pass the parents environment.
* Args as Search Take
* Name varargs? path? environment?
* spawnl N N N
* spawnle N N Y
* spawnlp N Y N
* spawnlpe N Y Y
* spawnv Y N N
* spawnve Y N Y
* spawnvp Y Y N
* spawnvpe Y Y Y
*
* Implementation Notes:
* MT Safe - But only because of missing functionality.
*
* After translating input arguments into the required format for
* CreateProcess(), the internal function __CRTDLL__spawn() is
* called to perform the actual spawning.
*
* FIXME:
* -File handles need some special handling. Sometimes children get
* open file handles, sometimes not. The docs are confusing.
* -No check for maximum path/argument/environment size is done.
* -Wine has a "process.h" which is not the same as any crt version.
* Unresolved issues Uwe Bonnes 970904:
* -system-call calls another wine process, but without debugging arguments
* and uses the first wine executable in the path
*/
#include "crtdll.h"
#include <errno.h>
#include "process.h"
#include "options.h"
#include <stdlib.h>
DEFAULT_DEBUG_CHANNEL(crtdll);
/* Process creation flags */
#define _P_WAIT 0
#define _P_NOWAIT 1
#define _P_OVERLAY 2
#define _P_NOWAITO 3
#define _P_DETACH 4
extern void __CRTDLL__set_errno(ULONG err);
extern LPVOID __cdecl CRTDLL_calloc(DWORD size, DWORD count);
extern VOID __cdecl CRTDLL_free(void *ptr);
extern VOID __cdecl CRTDLL__exit(LONG ret);
extern INT CRTDLL_doserrno;
/* INTERNAL: Spawn a child process */
static int __CRTDLL__spawn(INT flags, LPSTR exe, LPSTR args, LPSTR env);
static int __CRTDLL__spawn(INT flags, LPSTR exe, LPSTR args, LPSTR env)
{
STARTUPINFOA si;
PROCESS_INFORMATION pi;
if ((unsigned)flags > _P_DETACH)
{
CRTDLL_errno = EINVAL;
return -1;
}
FIXME(":must dup/kill streams for child process\n");
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
if (!CreateProcessA(exe, args, NULL, NULL, TRUE,
flags == _P_DETACH ? DETACHED_PROCESS : 0,
env, NULL, &si, &pi))
{
__CRTDLL__set_errno(GetLastError());
return -1;
}
switch(flags)
{
case _P_WAIT:
WaitForSingleObject(pi.hProcess,-1); /* wait forvever */
GetExitCodeProcess(pi.hProcess,&pi.dwProcessId);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return pi.dwProcessId;
case _P_DETACH:
CloseHandle(pi.hProcess);
pi.hProcess = 0;
/* fall through */
case _P_NOWAIT:
case _P_NOWAITO:
CloseHandle(pi.hThread);
return pi.hProcess;
case _P_OVERLAY:
CRTDLL__exit(0);
}
return -1; /* cant reach here */
}
/* INTERNAL: Convert argv list to a single 'delim'-seperated string */
static LPSTR __CRTDLL__argvtos(LPSTR *arg, CHAR delim);
static LPSTR __CRTDLL__argvtos(LPSTR *arg, CHAR delim)
{
LPSTR *search = arg;
LONG size = 0;
LPSTR ret;
if (!arg && !delim)
return NULL;
/* get length */
while(*search)
{
size += strlen(*search) + 1;
search++;
}
if (!(ret = (LPSTR)CRTDLL_calloc(size + 1, 1)))
return NULL;
/* fill string */
search = arg;
size = 0;
while(*search)
{
int strsize = strlen(*search);
memcpy(ret+size,*search,strsize);
ret[size+strsize] = delim;
size += strsize + 1;
search++;
}
return ret;
}
/*********************************************************************
* _spawnve (CRTDLL.274)
*
* Spawn a process.
*
*/
HANDLE __cdecl CRTDLL__spawnve(INT flags, LPSTR name, LPSTR *argv, LPSTR *envv)
{
LPSTR args = __CRTDLL__argvtos(argv,' ');
LPSTR envs = __CRTDLL__argvtos(envv,0);
LPSTR fullname = name;
FIXME(":not translating name %s to locate program\n",fullname);
TRACE(":call (%s), params (%s), env (%s)\n",name,args,envs?"Custom":"Null");
if (args)
{
HANDLE ret = __CRTDLL__spawn(flags, fullname, args, envs);
CRTDLL_free(args);
CRTDLL_free(envs);
return ret;
}
if (envs)
CRTDLL_free(envs);
WARN(":No argv[0] passed - process will not be executed");
return -1;
}
/*********************************************************************
* system (CRTDLL.485)
*/
INT __cdecl CRTDLL_system(LPSTR x)
{
#define SYSBUF_LENGTH 1500
char buffer[SYSBUF_LENGTH];
unsigned char *y = x;
unsigned char *bp;
int i;
strcpy(buffer, argv0);
bp = buffer + strlen(buffer);
*bp++ = ' ';
*bp++ = '"';
*bp++ = 0;
i = strlen(buffer) + strlen(x) +2;
/* Calculate needed buffer size to prevent overflow. */
while (*y) {
if (*y =='\\') i++;
y++;
}
/* If buffer too short, exit. */
if (i > SYSBUF_LENGTH) {
TRACE("_system buffer to small\n");
return 127;
}
y =x;
while (*y) {
*bp = *y;
bp++; y++;
if (*(y-1) =='\\') *bp++ = '\\';
}
/* Remove spaces from end of string. */
while (*(y-1) == ' ') {
bp--;y--;
}
*bp++ = '"';
*bp = 0;
TRACE("_system got '%s', executing '%s'\n",x,buffer);
return system(buffer);
}
/*
* CRTDLL string functions
*
* Copyright 1996,1998 Marcus Meissner
* Copyright 1996 Jukka Iivonen
* Copyright 1997,2000 Uwe Bonnes
* Copyright 2000 Jon Griffiths
*
* Implementation Notes:
* MT Safe.
*/
#include "crtdll.h"
DEFAULT_DEBUG_CHANNEL(crtdll);
/* INTERNAL: CRTDLL_malloc() based strndup */
LPSTR __CRTDLL__strndup(LPSTR buf, INT size);
LPSTR __CRTDLL__strndup(LPSTR buf, INT size)
{
char* ret;
int len = strlen(buf);
int max_len;
max_len = size <= len? size : len + 1;
ret = CRTDLL_malloc(max_len);
if (ret)
{
memcpy(ret,buf,max_len);
ret[max_len] = 0;
}
return ret;
}
/*********************************************************************
* _strdec (CRTDLL.282)
*
* Return the byte before str2 while it is >= to str1.
*
* PARAMS
* str1 [in] Terminating string
*
* sre2 [in] string to start searching from
*
* RETURNS
* The byte before str2, or str1, whichever is greater
*
* NOTES
* This function is implemented as tested with windows, which means
* it does not have a terminating condition. It always returns
* the byte before str2. Use with extreme caution!
*/
LPSTR __cdecl CRTDLL__strdec(LPSTR str1, LPSTR str2)
{
/* Hmm. While the docs suggest that the following should work... */
/* return (str2<=str1?0:str2-1); */
/* ...Version 2.50.4170 (NT) from win98 constantly decrements! */
return str2-1;
}
/*********************************************************************
* _strdup (CRTDLL.285)
*
* Duplicate a string.
*/
LPSTR __cdecl CRTDLL__strdup(LPCSTR ptr)
{
LPSTR ret = CRTDLL_malloc(strlen(ptr)+1);
if (ret) strcpy( ret, ptr );
return ret;
}
/*********************************************************************
* _strinc (CRTDLL.287)
*
* Return a pointer to the next character in a string
*/
LPSTR __cdecl CRTDLL__strinc(LPSTR str)
{
return str+1;
}
/*********************************************************************
* _strninc (CRTDLL.292)
*
* Return a pointer to the 'n'th character in a string
*/
LPSTR __cdecl CRTDLL__strninc(LPSTR str, INT n)
{
return str+n;
}
/*********************************************************************
* _strnset (CRTDLL.293)
*
* Fill a string with a character up to a certain length
*/
LPSTR __cdecl CRTDLL__strnset(LPSTR str, INT c, INT len)
{
if (len > 0 && str)
while (*str && len--)
*str++ = c;
return str;
}
/*********************************************************************
* _strrev (CRTDLL.294)
*
* Reverse a string in place
*/
LPSTR __cdecl CRTDLL__strrev (LPSTR str)
{
LPSTR p1;
LPSTR p2;
if (str && *str)
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
/*********************************************************************
* _strset (CRTDLL.295)
*
* Fill a string with a value.
*/
LPSTR __cdecl CRTDLL__strset (LPSTR str, INT set)
{
char *ptr = str;
while (*ptr)
*ptr++ = set;
return str;
}
/*********************************************************************
* _strncnt (CRTDLL.289)
*
* Return the length of a string or the maximum given length.
*/
LONG __cdecl CRTDLL__strncnt(LPSTR str, LONG max)
{
LONG len = strlen(str);
return (len > max? max : len);
}
/*********************************************************************
* _strspnp (CRTDLL.296)
*
*/
LPSTR __cdecl CRTDLL__strspnp(LPSTR str1, LPSTR str2)
{
str1 += strspn(str1,str2);
return *str1? str1 : 0;
}
/*********************************************************************
* _swab (CRTDLL.299)
*
* Copy from source to dest alternating bytes (i.e 16 bit big-to-little
* endian or vice versa).
*/
void __cdecl CRTDLL__swab(LPSTR src, LPSTR dst, INT len)
{
if (len > 1)
{
len = (unsigned)len >> 1;
while (len--) {
*dst++ = src[1];
*dst++ = *src++;
src++;
}
}
}
/*
* CRTDLL date/time functions
*
* Copyright 1996,1998 Marcus Meissner
* Copyright 1996 Jukka Iivonen
* Copyright 1997,2000 Uwe Bonnes
* Copyright 2000 Jon Griffiths
*
* Implementation Notes:
* MT Safe.
*/
#include "crtdll.h"
#include "process.h"
#include "options.h"
#include <stdlib.h>
#include <sys/times.h>
DEFAULT_DEBUG_CHANNEL(crtdll);
/* INTERNAL: Return formatted current time/date */
static LPSTR __CRTDLL__get_current_time(LPSTR out, const char * format);
static LPSTR __CRTDLL__get_current_time(LPSTR out, const char * format)
{
time_t t;
struct tm *_tm;
if ((time(&t) != ((time_t)-1)) &&
((_tm = localtime(&t)) != 0) &&
(strftime(out,9,format,_tm) == 8))
{
CRTDLL_free(_tm);
return out;
}
return NULL;
}
/*********************************************************************
* _ftime (CRTDLL.112)
*
* Get current time.
*/
VOID __cdecl CRTDLL__ftime (struct _timeb* t)
{
t->time = CRTDLL_time(NULL);
t->millitm = 0; /* FIXME */
t->timezone = 0;
t->dstflag = 0;
}
/**********************************************************************
* _strdate (CRTDLL.283)
*
* Return the current date as MM/DD/YY - (American Format)
*/
LPSTR __cdecl CRTDLL__strdate (LPSTR date)
{
return __CRTDLL__get_current_time(date,"%m/%d/%y");
}
/*********************************************************************
* _strtime (CRTDLL.299)
*
* Return the current time as HH:MM:SS
*/
LPSTR __cdecl CRTDLL__strtime (LPSTR date)
{
return __CRTDLL__get_current_time(date,"%H:%M:%S");
}
/*********************************************************************
* clock (CRTDLL.350)
*/
clock_t __cdecl CRTDLL_clock(void)
{
struct tms alltimes;
clock_t res;
times(&alltimes);
res = alltimes.tms_utime + alltimes.tms_stime+
alltimes.tms_cutime + alltimes.tms_cstime;
/* Fixme: We need some symbolic representation
for (Hostsystem_)CLOCKS_PER_SEC
and (Emulated_system_)CLOCKS_PER_SEC
10 holds only for Windows/Linux_i86)
*/
return 10*res;
}
/*********************************************************************
* difftime (CRTDLL.357)
*/
double __cdecl CRTDLL_difftime (time_t time1, time_t time2)
{
double timediff;
timediff = (double)(time1 - time2);
return timediff;
}
/*********************************************************************
* time (CRTDLL.488)
*/
time_t __cdecl CRTDLL_time(time_t *timeptr)
{
time_t curtime = time(NULL);
if (timeptr)
*timeptr = curtime;
return curtime;
}
......@@ -4,18 +4,13 @@
* Copyright 1999 Alexandre Julliard
*/
#include "config.h"
#include "crtdll.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "wine/unicode.h"
#include "crtdll.h"
#include "debugtools.h"
DEFAULT_DEBUG_CHANNEL(crtdll);
......
......@@ -11,6 +11,7 @@ HANDLE
HFILE
INT
LONG
ULONG
UINT
WCHAR
clock_t
......@@ -32,6 +33,7 @@ LPCVOID
LPDWORD
LPDWORD *
LPINT
LPUINT
LPSTR *
LPSTR **
LPVOID
......@@ -44,10 +46,13 @@ WCHAR *
_INITTERMFUN *
char *
jmp_buf
struct find_t *
struct stat *
find_t *
struct _stat *
struct win_stat *
struct _timeb *
time_t *
fpos_t *
diskfree_t *
unsigned char *
va_list
void *
......@@ -71,3 +76,7 @@ LPWSTR
new_handler_type
sig_handler_type
comp_func
struct complex
atexit_function
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment