posix_w32.c 17.2 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7
#include "../posix.h"
Russell Belfer committed
8
#include "../fileops.h"
9
#include "path.h"
10
#include "utf-conv.h"
11
#include "repository.h"
12
#include "reparse.h"
Vicent Marti committed
13
#include <errno.h>
14
#include <io.h>
15
#include <fcntl.h>
16
#include <ws2tcpip.h>
Vicent Marti committed
17

18
#ifndef FILE_NAME_NORMALIZED
19 20 21
# define FILE_NAME_NORMALIZED 0
#endif

22 23 24 25
#ifndef IO_REPARSE_TAG_SYMLINK
#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
#endif

26 27 28 29 30 31 32 33 34
/* Options which we always provide to _wopen.
 *
 * _O_BINARY - Raw access; no translation of CR or LF characters
 * _O_NOINHERIT - Do not mark the created handle as inheritable by child processes.
 *    The Windows default is 'not inheritable', but the CRT's default (following
 *    POSIX convention) is 'inheritable'. We have no desire for our handles to be
 *    inheritable on Windows, so specify the flag to get default behavior back. */
#define STANDARD_OPEN_FLAGS (_O_BINARY | _O_NOINHERIT)

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/* GetFinalPathNameByHandleW signature */
typedef DWORD(WINAPI *PFGetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, DWORD);

/* Helper function which converts UTF-8 paths to UTF-16.
 * On failure, errno is set. */
static int utf8_to_16_with_errno(git_win32_path dest, const char *src)
{
	int len = git_win32_path_from_utf8(dest, src);

	if (len < 0) {
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
			errno = ENAMETOOLONG;
		else
			errno = EINVAL; /* Bad code point, presumably */
	}

	return len;
}

54 55 56 57 58 59 60 61 62
int p_ftruncate(int fd, long size)
{
#if defined(_MSC_VER) && _MSC_VER >= 1500
	return _chsize_s(fd, size);
#else
	return _chsize(fd, size);
#endif
}

63 64 65 66 67 68 69 70 71 72 73 74
int p_mkdir(const char *path, mode_t mode)
{
	git_win32_path buf;

	GIT_UNUSED(mode);

	if (utf8_to_16_with_errno(buf, path) < 0)
		return -1;

	return _wmkdir(buf);
}

75 76 77 78 79 80 81 82
int p_link(const char *old, const char *new)
{
	GIT_UNUSED(old);
	GIT_UNUSED(new);
	errno = ENOSYS;
	return -1;
}

Vicent Marti committed
83 84
int p_unlink(const char *path)
{
85
	git_win32_path buf;
86 87 88 89 90 91 92 93 94
	int error;

	if (utf8_to_16_with_errno(buf, path) < 0)
		return -1;

	error = _wunlink(buf);

	/* If the file could not be deleted because it was
	 * read-only, clear the bit and try again */
95
	if (error == -1 && errno == EACCES) {
96 97 98 99 100
		_wchmod(buf, 0666);
		error = _wunlink(buf);
	}

	return error;
Vicent Marti committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
}

int p_fsync(int fd)
{
	HANDLE fh = (HANDLE)_get_osfhandle(fd);

	if (fh == INVALID_HANDLE_VALUE) {
		errno = EBADF;
		return -1;
	}

	if (!FlushFileBuffers(fh)) {
		DWORD code = GetLastError();

		if (code == ERROR_INVALID_HANDLE)
			errno = EINVAL;
		else
			errno = EIO;

		return -1;
	}

	return 0;
}

GIT_INLINE(time_t) filetime_to_time_t(const FILETIME *ft)
{
	long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
	winTime -= 116444736000000000LL; /* Windows to Unix Epoch conversion */
	winTime /= 10000000;		 /* Nano to seconds resolution */
	return (time_t)winTime;
}

134 135 136 137 138
/* On success, returns the length, in characters, of the path stored in dest.
 * On failure, returns a negative value. */
static int readlink_w(
	git_win32_path dest,
	const git_win32_path path)
Vicent Marti committed
139
{
140 141 142 143 144 145 146 147
	BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
	GIT_REPARSE_DATA_BUFFER *reparse_buf = (GIT_REPARSE_DATA_BUFFER *)buf;
	HANDLE handle = NULL;
	DWORD ioctl_ret;
	wchar_t *target;
	size_t target_len;

	int error = -1;
148

149 150 151 152
	handle = CreateFileW(path, GENERIC_READ,
		FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
		FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);

153
	if (handle == INVALID_HANDLE_VALUE) {
154
		errno = ENOENT;
155
		return -1;
156 157 158 159 160 161 162
	}

	if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0,
		reparse_buf, sizeof(buf), &ioctl_ret, NULL)) {
		errno = EINVAL;
		goto on_error;
	}
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177
	switch (reparse_buf->ReparseTag) {
	case IO_REPARSE_TAG_SYMLINK:
		target = reparse_buf->SymbolicLinkReparseBuffer.PathBuffer +
			(reparse_buf->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(WCHAR));
		target_len = reparse_buf->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
		break;
	case IO_REPARSE_TAG_MOUNT_POINT:
		target = reparse_buf->MountPointReparseBuffer.PathBuffer +
			(reparse_buf->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR));
		target_len = reparse_buf->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
		break;
	default:
		errno = EINVAL;
		goto on_error;
178
	}
Vicent Marti committed
179

180 181
	if (target_len) {
		/* The path may need to have a prefix removed. */
182
		target_len = git_win32__canonicalize_path(target, target_len);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

		/* Need one additional character in the target buffer
		 * for the terminating NULL. */
		if (GIT_WIN_PATH_UTF16 > target_len) {
			wcscpy(dest, target);
			error = (int)target_len;
		}
	}

on_error:
	CloseHandle(handle);
	return error;
}

#define WIN32_IS_WSEP(CH) ((CH) == L'/' || (CH) == L'\\')

static int lstat_w(
	wchar_t *path,
	struct stat *buf,
	bool posix_enotdir)
{
	WIN32_FILE_ATTRIBUTE_DATA fdata;

	if (GetFileAttributesExW(path, GetFileExInfoStandard, &fdata)) {
Vicent Marti committed
207 208
		int fMode = S_IREAD;

209 210 211
		if (!buf)
			return 0;

Vicent Marti committed
212 213 214 215 216 217 218 219 220 221 222 223 224
		if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			fMode |= S_IFDIR;
		else
			fMode |= S_IFREG;

		if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
			fMode |= S_IWRITE;

		buf->st_ino = 0;
		buf->st_gid = 0;
		buf->st_uid = 0;
		buf->st_nlink = 1;
		buf->st_mode = (mode_t)fMode;
nulltoken committed
225
		buf->st_size = ((git_off_t)fdata.nFileSizeHigh << 32) + fdata.nFileSizeLow;
Vicent Marti committed
226 227 228 229
		buf->st_dev = buf->st_rdev = (_getdrive() - 1);
		buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
		buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
		buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
230

231 232
		if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
			git_win32_path target;
233

234 235
			if (readlink_w(target, path) >= 0) {
				buf->st_mode = (buf->st_mode & ~S_IFMT) | S_IFLNK;
236

237 238 239 240 241
				/* st_size gets the UTF-8 length of the target name, in bytes,
				 * not counting the NULL terminator */
				if ((buf->st_size = git__utf16_to_8(NULL, 0, target)) < 0)
					return -1;
			}
242
		}
243

244
		return 0;
Vicent Marti committed
245 246
	}

Eduardo Bart committed
247
	errno = ENOENT;
248

249 250
	/* To match POSIX behavior, set ENOTDIR when any of the folders in the
	 * file path is a regular file, otherwise set ENOENT.
251
	 */
Eduardo Bart committed
252
	if (posix_enotdir) {
253 254
		size_t path_len = wcslen(path);

255 256
		/* scan up path until we find an existing item */
		while (1) {
257 258
			DWORD attrs;

259
			/* remove last directory component */
260
			for (path_len--; path_len > 0 && !WIN32_IS_WSEP(path[path_len]); path_len--);
261

262
			if (path_len <= 0)
263 264
				break;

265 266
			path[path_len] = L'\0';
			attrs = GetFileAttributesW(path);
267

268
			if (attrs != INVALID_FILE_ATTRIBUTES) {
269
				if (!(attrs & FILE_ATTRIBUTE_DIRECTORY))
Eduardo Bart committed
270
					errno = ENOTDIR;
271
				break;
272 273 274 275
			}
		}
	}

276
	return -1;
Vicent Marti committed
277 278
}

279
static int do_lstat(const char *path, struct stat *buf, bool posixly_correct)
Vicent Marti committed
280
{
281 282 283 284 285 286 287 288 289
	git_win32_path path_w;
	int len;

	if ((len = utf8_to_16_with_errno(path_w, path)) < 0)
		return -1;

	git_win32__path_trim_end(path_w, len);

	return lstat_w(path_w, buf, posixly_correct);
290
}
Vicent Marti committed
291

292
int p_lstat(const char *filename, struct stat *buf)
293
{
294
	return do_lstat(filename, buf, false);
Vicent Marti committed
295 296
}

297
int p_lstat_posixly(const char *filename, struct stat *buf)
298
{
299
	return do_lstat(filename, buf, true);
300
}
301

302
int p_readlink(const char *path, char *buf, size_t bufsiz)
Vicent Marti committed
303
{
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	git_win32_path path_w, target_w;
	git_win32_utf8_path target;
	int len;

	/* readlink(2) does not NULL-terminate the string written
	 * to the target buffer. Furthermore, the target buffer need
	 * not be large enough to hold the entire result. A truncated
	 * result should be written in this case. Since this truncation
	 * could occur in the middle of the encoding of a code point,
	 * we need to buffer the result on the stack. */

	if (utf8_to_16_with_errno(path_w, path) < 0 ||
		readlink_w(target_w, path_w) < 0 ||
		(len = git_win32_path_to_utf8(target, target_w)) < 0)
		return -1;
319

320 321
	bufsiz = min((size_t)len, bufsiz);
	memcpy(buf, target, bufsiz);
322

323
	return (int)bufsiz;
Vicent Marti committed
324 325
}

Ben Straub committed
326 327
int p_symlink(const char *old, const char *new)
{
328 329 330 331
	/* Real symlinks on NTFS require admin privileges. Until this changes,
	 * libgit2 just creates a text file with the link target in the contents.
	 */
	return git_futils_fake_symlink(old, new);
Ben Straub committed
332 333
}

334
int p_open(const char *path, int flags, ...)
335
{
336
	git_win32_path buf;
337 338
	mode_t mode = 0;

339 340
	if (utf8_to_16_with_errno(buf, path) < 0)
		return -1;
341

342
	if (flags & O_CREAT) {
343 344 345
		va_list arg_list;

		va_start(arg_list, flags);
liyuray committed
346
		mode = (mode_t)va_arg(arg_list, int);
347 348 349
		va_end(arg_list);
	}

350
	return _wopen(buf, flags | STANDARD_OPEN_FLAGS, mode);
351 352
}

353
int p_creat(const char *path, mode_t mode)
354
{
355
	git_win32_path buf;
356 357 358 359

	if (utf8_to_16_with_errno(buf, path) < 0)
		return -1;

360
	return _wopen(buf, _O_WRONLY | _O_CREAT | _O_TRUNC | STANDARD_OPEN_FLAGS, mode);
361 362 363 364
}

int p_getcwd(char *buffer_out, size_t size)
{
365 366
	git_win32_path buf;
	wchar_t *cwd = _wgetcwd(buf, GIT_WIN_PATH_UTF16);
367

368
	if (!cwd)
369 370
		return -1;

371 372 373
	/* Convert the working directory back to UTF-8 */
	if (git__utf16_to_8(buffer_out, size, cwd) < 0) {
		DWORD code = GetLastError();
374

375 376 377 378
		if (code == ERROR_INSUFFICIENT_BUFFER)
			errno = ERANGE;
		else
			errno = EINVAL;
379

380 381
		return -1;
	}
382

383
	return 0;
384 385
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/*
 * Returns the address of the GetFinalPathNameByHandleW function.
 * This function is available on Windows Vista and higher.
 */
static PFGetFinalPathNameByHandleW get_fpnbyhandle(void)
{
	static PFGetFinalPathNameByHandleW pFunc = NULL;
	PFGetFinalPathNameByHandleW toReturn = pFunc;

	if (!toReturn) {
		HMODULE hModule = GetModuleHandleW(L"kernel32");

		if (hModule)
			toReturn = (PFGetFinalPathNameByHandleW)GetProcAddress(hModule, "GetFinalPathNameByHandleW");

		pFunc = toReturn;
	}

	assert(toReturn);

	return toReturn;
}

static int getfinalpath_w(
	git_win32_path dest,
	const wchar_t *path)
{
	PFGetFinalPathNameByHandleW pgfp = get_fpnbyhandle();
	HANDLE hFile;
	DWORD dwChars;

	if (!pgfp)
		return -1;

	/* Use FILE_FLAG_BACKUP_SEMANTICS so we can open a directory. Do not
	* specify FILE_FLAG_OPEN_REPARSE_POINT; we want to open a handle to the
	* target of the link. */
	hFile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
		NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

426
	if (INVALID_HANDLE_VALUE == hFile)
427 428 429 430
		return -1;

	/* Call GetFinalPathNameByHandle */
	dwChars = pgfp(hFile, dest, GIT_WIN_PATH_UTF16, FILE_NAME_NORMALIZED);
431
	CloseHandle(hFile);
432

433
	if (!dwChars || dwChars >= GIT_WIN_PATH_UTF16)
434 435 436
		return -1;

	/* The path may be delivered to us with a prefix; canonicalize */
437
	return (int)git_win32__canonicalize_path(dest, dwChars);
438 439 440 441 442 443 444 445 446 447 448 449
}

static int follow_and_lstat_link(git_win32_path path, struct stat* buf)
{
	git_win32_path target_w;

	if (getfinalpath_w(target_w, path) < 0)
		return -1;

	return lstat_w(target_w, buf, false);
}

450 451
int p_stat(const char* path, struct stat* buf)
{
452 453
	git_win32_path path_w;
	int len;
454

455 456
	if ((len = utf8_to_16_with_errno(path_w, path)) < 0)
		return -1;
457

458
	git_win32__path_trim_end(path_w, len);
459

460 461 462 463 464 465 466 467 468
	if (lstat_w(path_w, buf, false) < 0)
		return -1;

	/* The item is a symbolic link or mount point. No need to iterate
	 * to follow multiple links; use GetFinalPathNameFromHandle. */
	if (S_ISLNK(buf->st_mode))
		return follow_and_lstat_link(path_w, buf);

	return 0;
469 470 471 472
}

int p_chdir(const char* path)
{
473
	git_win32_path buf;
474 475 476 477

	if (utf8_to_16_with_errno(buf, path) < 0)
		return -1;

478
	return _wchdir(buf);
479 480
}

481
int p_chmod(const char* path, mode_t mode)
482
{
483
	git_win32_path buf;
484 485 486 487

	if (utf8_to_16_with_errno(buf, path) < 0)
		return -1;

488
	return _wchmod(buf, mode);
489 490 491 492
}

int p_rmdir(const char* path)
{
493
	git_win32_path buf;
494 495 496 497
	int error;

	if (utf8_to_16_with_errno(buf, path) < 0)
		return -1;
498 499 500

	error = _wrmdir(buf);

501
	if (error == -1) {
502 503 504 505 506 507 508 509
		switch (GetLastError()) {
			/* _wrmdir() is documented to return EACCES if "A program has an open
			 * handle to the directory."  This sounds like what everybody else calls
			 * EBUSY.  Let's convert appropriate error codes.
			 */
			case ERROR_SHARING_VIOLATION:
				errno = EBUSY;
				break;
510

511 512 513 514 515 516
			/* This error can be returned when trying to rmdir an extant file. */
			case ERROR_DIRECTORY:
				errno = ENOTDIR;
				break;
		}
	}
517

518
	return error;
Vicent Marti committed
519 520
}

521
char *p_realpath(const char *orig_path, char *buffer)
522
{
523
	git_win32_path orig_path_w, buffer_w;
524

525 526
	if (utf8_to_16_with_errno(orig_path_w, orig_path) < 0)
		return NULL;
527

528 529 530 531 532 533 534 535
	/* Note that if the path provided is a relative path, then the current directory
	 * is used to resolve the path -- which is a concurrency issue because the current
	 * directory is a process-wide variable. */
	if (!GetFullPathNameW(orig_path_w, GIT_WIN_PATH_UTF16, buffer_w, NULL)) {
		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
			errno = ENAMETOOLONG;
		else
			errno = EINVAL;
536

537 538
		return NULL;
	}
539

540
	/* The path must exist. */
541
	if (GetFileAttributesW(buffer_w) == INVALID_FILE_ATTRIBUTES) {
542
		errno = ENOENT;
543
		return NULL;
544
	}
545

546 547 548 549 550 551 552 553 554 555 556
	/* Convert the path to UTF-8. */
	if (buffer) {
		/* If the caller provided a buffer, then it is assumed to be GIT_WIN_PATH_UTF8
		 * characters in size. If it isn't, then we may overflow. */
		if (git__utf16_to_8(buffer, GIT_WIN_PATH_UTF8, buffer_w) < 0)
			return NULL;
	} else {
		/* If the caller did not provide a buffer, then we allocate one for the caller
		 * from the heap. */
		if (git__utf16_to_8_alloc(&buffer, buffer_w) < 0)
			return NULL;
557 558
	}

559 560
	/* Convert backslashes to forward slashes */
	git_path_mkposix(buffer);
561

562
	return buffer;
563 564
}

565 566
int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr)
{
567
#if defined(_MSC_VER)
568 569
	int len;

570 571 572 573 574 575 576 577 578 579
	if (count == 0)
		return _vscprintf(format, argptr);

	#if _MSC_VER >= 1500
	len = _vsnprintf_s(buffer, count, _TRUNCATE, format, argptr);
	#else
	len = _vsnprintf(buffer, count, format, argptr);
	#endif

	if (len < 0)
580
		return _vscprintf(format, argptr);
581 582

	return len;
583 584 585 586
#else /* MinGW */
	return vsnprintf(buffer, count, format, argptr);
#endif
}
587 588 589 590 591 592 593 594 595 596 597 598

int p_snprintf(char *buffer, size_t count, const char *format, ...)
{
	va_list va;
	int r;

	va_start(va, format);
	r = p_vsnprintf(buffer, count, format, va);
	va_end(va);

	return r;
}
599 600 601

int p_mkstemp(char *tmp_path)
{
602
#if defined(_MSC_VER) && _MSC_VER >= 1500
603
	if (_mktemp_s(tmp_path, strlen(tmp_path) + 1) != 0)
604
		return -1;
605
#else
Vicent Marti committed
606
	if (_mktemp(tmp_path) == NULL)
607
		return -1;
Vicent Marti committed
608
#endif
609

610
	return p_open(tmp_path, O_RDWR | O_CREAT | O_EXCL, 0744); //-V536
611
}
612

613
int p_access(const char* path, mode_t mode)
614
{
615
	git_win32_path buf;
616 617 618 619

	if (utf8_to_16_with_errno(buf, path) < 0)
		return -1;

620
	return _waccess(buf, mode);
621
}
622

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
static int ensure_writable(wchar_t *fpath)
{
	DWORD attrs;

	attrs = GetFileAttributesW(fpath);
	if (attrs == INVALID_FILE_ATTRIBUTES) {
		if (GetLastError() == ERROR_FILE_NOT_FOUND)
			return 0;

		giterr_set(GITERR_OS, "failed to get attributes");
		return -1;
	}

	if (!(attrs & FILE_ATTRIBUTE_READONLY))
		return 0;

	attrs &= ~FILE_ATTRIBUTE_READONLY;
	if (!SetFileAttributesW(fpath, attrs)) {
		giterr_set(GITERR_OS, "failed to set attributes");
		return -1;
	}

	return 0;
}

648
int p_rename(const char *from, const char *to)
649
{
650 651
	git_win32_path wfrom;
	git_win32_path wto;
652 653 654
	int rename_tries;
	int rename_succeeded;
	int error;
655

656 657 658
	if (utf8_to_16_with_errno(wfrom, from) < 0 ||
		utf8_to_16_with_errno(wto, to) < 0)
		return -1;
659

660 661 662 663
	/* wait up to 50ms if file is locked by another thread or process */
	rename_tries = 0;
	rename_succeeded = 0;
	while (rename_tries < 10) {
664 665
		if (ensure_writable(wto) == 0 &&
		    MoveFileExW(wfrom, wto, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) != 0) {
666 667 668 669 670 671 672 673 674 675 676 677 678
			rename_succeeded = 1;
			break;
		}
		
		error = GetLastError();
		if (error == ERROR_SHARING_VIOLATION || error == ERROR_ACCESS_DENIED) {
			Sleep(5);
			rename_tries++;
		} else
			break;
	}
	
	return rename_succeeded ? 0 : -1;
679
}
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags)
{
	if ((size_t)((int)length) != length)
		return -1; /* giterr_set will be done by caller */

	return recv(socket, buffer, (int)length, flags);
}

int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags)
{
	if ((size_t)((int)length) != length)
		return -1; /* giterr_set will be done by caller */

	return send(socket, buffer, (int)length, flags);
}
Ben Straub committed
696 697 698 699 700

/**
 * Borrowed from http://old.nabble.com/Porting-localtime_r-and-gmtime_r-td15282276.html
 * On Win32, `gmtime_r` doesn't exist but `gmtime` is threadsafe, so we can use that
 */
Linquize committed
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
struct tm *
p_localtime_r (const time_t *timer, struct tm *result)
{
	struct tm *local_result;
	local_result = localtime (timer);

	if (local_result == NULL || result == NULL)
		return NULL;

	memcpy (result, local_result, sizeof (struct tm));
	return result;
}
struct tm *
p_gmtime_r (const time_t *timer, struct tm *result)
{
	struct tm *local_result;
	local_result = gmtime (timer);

	if (local_result == NULL || result == NULL)
		return NULL;

	memcpy (result, local_result, sizeof (struct tm));
	return result;
Ben Straub committed
724 725
}

726
int p_inet_pton(int af, const char *src, void *dst)
727
{
728 729 730 731
	struct sockaddr_storage sin;
	void *addr;
	int sin_len = sizeof(struct sockaddr_storage), addr_len;
	int error = 0;
732

733 734 735 736 737 738 739 740 741
	if (af == AF_INET) {
		addr = &((struct sockaddr_in *)&sin)->sin_addr;
		addr_len = sizeof(struct in_addr);
	} else if (af == AF_INET6) {
		addr = &((struct sockaddr_in6 *)&sin)->sin6_addr;
		addr_len = sizeof(struct in6_addr);
	} else {
		errno = EAFNOSUPPORT;
		return -1;
742 743
	}

744 745 746
	if ((error = WSAStringToAddressA((LPSTR)src, af, NULL, (LPSOCKADDR)&sin, &sin_len)) == 0) {
		memcpy(dst, addr, addr_len);
		return 1;
747 748
	}

749 750 751 752 753 754 755 756 757
	switch(WSAGetLastError()) {
	case WSAEINVAL:
		return 0;
	case WSAEFAULT:
		errno = ENOSPC;
		return -1;
	case WSA_NOT_ENOUGH_MEMORY:
		errno = ENOMEM;
		return -1;
758 759
	}

760 761
	errno = EINVAL;
	return -1;
762
}