1%%	options
2
3copyright owner	=	Dirk Krause
4copyright year	=	2019-xxxx
5SPDX-License-Identifier:	BSD-3-Clause
6
7
8%%	header
9
10/**	@file	dk4chdirw.h	Change current working directory.
11*/
12
13
14#ifndef	DK4CONF_H_INCLUDED
15#if	DK4_BUILDING_DKTOOLS4
16#include "dk4conf.h"
17#else
18#include <dktools-4/dk4conf.h>
19#endif
20#endif
21
22#ifndef DK4ERROR_H_INCLUDED
23#if	DK4_BUILDING_DKTOOLS4
24#include <libdk4base/dk4error.h>
25#else
26#include <dktools-4/dk4error.h>
27#endif
28#endif
29
30
31#ifdef	__cplusplus
32extern "C" {
33#endif
34
35
36/**	Change current working directory.
37	@param	nd	Name of new directory.
38	@param	erp	Error report, may be NULL.
39	@return	1 on success, 0 on error.
40
41	CRT on Windows: Optional.
42
43	Error codes:
44	- DK4_E_INVALID_ARGUMENTS<br>
45	  if nd is NULL,
46	- DK4_E_NOT_SUPPORTEDy<br>
47	  if the operation is not supported on the current system,
48	- DK4_E_SYSTEM<br>
49	  with error code in lDetails1 or iDetails1 if the operation
50	  is supported but did fail.
51*/
52
53int
54
55dk4chdir_wc(const wchar_t *nd, dk4_er_t *erp);
56
57
58#ifdef	__cplusplus
59}
60#endif
61
62
63
64/* vim: set ai sw=4 ts=4 : */
65
66%%	module
67
68
69#include "dk4conf.h"
70#include <libdk4c/dk4chdirw.h>
71
72
73#if DK4_ON_WINDOWS
74#ifndef WINDOWS_H_INCLUDED
75#include <windows.h>
76#define	WINDOWS_H_INCLUDED 1
77#endif
78#endif
79
80#if DK4_HAVE_UNISTD_H
81#ifndef UNISTD_H_INCLUDED
82#include <unistd.h>
83#define	UNISTD_H_INCLUDED 1
84#endif
85#endif
86
87#if DK4_HAVE_STDLIB_H
88#ifndef STDLIB_H_INCLUDED
89#include <stdlib.h>
90#define	STDLIB_H_INCLUDED 1
91#endif
92#endif
93
94#if DK4_HAVE_DIRECT_H
95#ifndef DIRECT_H_INCLUDED
96#include <direct.h>
97#define DIRECT_H_INCLUDED 1
98#endif
99#endif
100
101#if DK4_HAVE_ERRNO_H
102#ifndef ERRNO_H_INCLUDED
103#include <errno.h>
104#define	ERRNO_H_INCLUDED 1
105#endif
106#endif
107
108#if	DK4_HAVE_ASSERT_H
109#ifndef	ASSERT_H_INCLUDED
110#include <assert.h>
111#define	ASSERT_H_INCLUDED 1
112#endif
113#endif
114
115
116$!trace-include
117
118
119
120int
121
122dk4chdir_wc(const wchar_t *nd, dk4_er_t *erp)
123{
124	int		 back	= 0;
125	$? "+ dk4chdir_wc \"%!ws\"", TR_LSTR(nd)
126#if	DK4_USE_ASSERT
127  assert(NULL != nd);
128#endif
129	if (NULL != nd) {
130#if	DK4_ON_WINDOWS
131#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
132		if (SetCurrentDirectoryW(nd)) {
133			back = 1;
134		}
135		else {							$? "! SetCurrentDirectoryW"
136			dk4error_set_ldetails(erp, DK4_E_SYSTEM, GetLastError());
137		}
138#else
139		if (0 == _wchdir(nd)) {
140			back = 1;
141		}
142		else {							$? "! _wchdir"
143			dk4error_set_idetails(erp, DK4_E_SYSTEM, errno);
144		}
145#endif
146#else
147		$? "! not supported"
148		dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
149#endif
150	}
151	else {								$? "! nd"
152		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
153	}
154	$? "- dk4chdir_wc %d", back
155	return back;
156}
157
158
159
160/* vim: set ai sw=4 ts=4 : */
161