GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
unistd-wrappers.c
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2016-2025 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26// These functions may be provided by gnulib. We don't include gnulib
27// headers directly in Octave's C++ source files to avoid problems that
28// may be caused by the way that gnulib overrides standard library
29// functions.
30
31#if defined (HAVE_CONFIG_H)
32# include "config.h"
33#endif
34
35#include <stdio.h>
36
37#include <sys/types.h>
38#include <unistd.h>
39
40#if defined (__WIN32__) && ! defined (__CYGWIN__)
41# include <process.h>
42#endif
43
44#if defined (OCTAVE_USE_WINDOWS_API)
45# include <windows.h>
46# include <wchar.h>
47#endif
48
49#if defined (__WIN32__) && ! defined (__CYGWIN__)
50# include "windows-spawn.h"
51#endif
52
53#include "uniconv-wrappers.h"
54#include "unistd-wrappers.h"
55
56int
58{
59 return F_OK;
60}
61
62int
64{
65 return R_OK;
66}
67
68int
70{
71 return W_OK;
72}
73
74int
76{
77 return X_OK;
78}
79
80int
81octave_access_wrapper (const char *nm, int mode)
82{
83 return access (nm, mode);
84}
85
86int
87octave_chdir_wrapper (const char *nm)
88{
89#if defined (OCTAVE_USE_WINDOWS_API)
90 wchar_t *wnm = u8_to_wchar (nm);
91 int status = _wchdir (wnm);
92 free ((void *) wnm);
93 return status;
94#else
95 return chdir (nm);
96#endif
97}
98
99int
101{
102 return close (fd);
103}
104
105const char *
107{
108#if defined (HAVE_CTERMID)
109 return ctermid (0);
110#else
111 return "/dev/tty";
112#endif
113}
114
115int
116octave_dup2_wrapper (int fd1, int fd2)
117{
118 return dup2 (fd1, fd2);
119}
120
121int
122octave_execv_wrapper (const char *file, char *const *argv)
123{
124#if defined (__WIN32__) && ! defined (__CYGWIN__)
125
126 char *argv_mem_to_free;
127 const char **sanitized_argv = prepare_spawn ((const char * const *) argv,
128 &argv_mem_to_free);
129
130# if defined (OCTAVE_USE_WINDOWS_API) && defined (_UNICODE)
131
132 // count number of arguments
133 size_t argc;
134 for (argc = 0; sanitized_argv[argc] != NULL; argc++)
135 ;
136
137 wchar_t *wfile = u8_to_wchar (file);
138 const wchar_t **wargv = malloc ((argc + 1) * sizeof (wchar_t *));
139
140 // convert multibyte UTF-8 strings to wide character strings
141 for (size_t i_arg = 0; i_arg < argc; i_arg++)
142 wargv[i_arg] = u8_to_wchar (sanitized_argv[i_arg]);
143
144 wargv[argc] = NULL;
145
146 free (sanitized_argv);
147 free (argv_mem_to_free);
148
149 int status = _wspawnv (P_WAIT, wfile, wargv+1);
150
151 // This happens when the spawned child process terminates.
152
153 free (wfile);
154 const wchar_t **wp = wargv;
155 // Casting away the const in the loop is ok here.
156 while (*wp)
157 free ((wchar_t *) *wp++);
158 free (wargv);
159
160# else
161
162 int status = spawnv (P_OVERLAY, file, sanitized_argv);
163
164 // This only happens if spawnv fails.
165
166 free (sanitized_argv);
167 free (argv_mem_to_free);
168
169# endif
170
171 return status;
172
173#else
174
175 return execv (file, argv);
176
177#endif
178}
179
180int
181octave_execvp_wrapper (const char *file, char *const *argv)
182{
183#if defined (OCTAVE_USE_WINDOWS_API)
184 return execvp (file, (const char *const *) argv);
185#else
186 return execvp (file, argv);
187#endif
188}
189
190pid_t
192{
193#if defined (HAVE_FORK)
194 return fork ();
195#else
196 return -1;
197#endif
198}
199
200int
201octave_ftruncate_wrapper (int fd, off_t sz)
202{
203 return ftruncate (fd, sz);
204}
205
206char *
207octave_getcwd_wrapper (char *nm, size_t len)
208{
209#if defined (OCTAVE_USE_WINDOWS_API)
210 wchar_t *tmp = _wgetcwd (NULL, 0);
211 char *retval = NULL;
212
213 if (! tmp)
214 return retval;
215
216 retval = u8_from_wchar (tmp);
217 if (! nm)
218 return retval;
219 else
220 {
221 if (strlen (retval) > len)
222 return NULL;
223
224 memcpy (nm, retval, len);
225 free (retval);
226 return nm;
227 }
228#else
229 return getcwd (nm, len);
230#endif
231}
232
233gid_t
235{
236#if defined (HAVE_GETEGID)
237 return getegid ();
238#else
239 return -1;
240#endif
241}
242
243uid_t
245{
246#if defined (HAVE_GETEUID)
247 return geteuid ();
248#else
249 return -1;
250#endif
251}
252
253gid_t
255{
256#if defined (HAVE_GETGID)
257 return getgid ();
258#else
259 return -1;
260#endif
261}
262
263int
265{
266 return gethostname (nm, len);
267}
268
269pid_t
271{
272#if defined (HAVE_GETPGRP)
273 return getpgrp ();
274#else
275 return -1;
276#endif
277}
278
279pid_t
281{
282#if defined (HAVE_GETPID)
283 return getpid ();
284#else
285 return -1;
286#endif
287}
288
289pid_t
291{
292#if defined (HAVE_GETPPID)
293 return getppid ();
294#else
295 return -1;
296#endif
297}
298
299uid_t
301{
302#if defined (HAVE_GETUID)
303 return getuid ();
304#else
305 return -1;
306#endif
307}
308
309int
311{
312 return isatty (fd);
313}
314
315int
316octave_link_wrapper (const char *nm1, const char *nm2)
317{
318 return link (nm1, nm2);
319}
320
321int
323{
324 return pipe (fd);
325}
326
327int
328octave_rmdir_wrapper (const char *nm)
329{
330#if defined (OCTAVE_USE_WINDOWS_API)
331 wchar_t *wnm = u8_to_wchar (nm);
332 int status = _wrmdir (wnm);
333 free ((void *) wnm);
334 return status;
335#else
336 return rmdir (nm);
337#endif
338}
339
340pid_t
342{
343#if defined (HAVE_SETSID)
344 return setsid ();
345#else
346 return -1;
347#endif
348}
349
350int
352{
353 return STDIN_FILENO;
354}
355
356int
358{
359 return STDOUT_FILENO;
360}
361
362int
364{
365 return STDERR_FILENO;
366}
367
368int
369octave_symlink_wrapper (const char *nm1, const char *nm2)
370{
371 return symlink (nm1, nm2);
372}
373
374int
375octave_unlink_wrapper (const char *nm)
376{
377#if defined (OCTAVE_USE_WINDOWS_API)
378 wchar_t *wnm = u8_to_wchar (nm);
379
380 // _wunlink fails on files with the read-only flag set. Try to un-set it.
381 DWORD file_attributes = GetFileAttributesW (wnm);
382 if (file_attributes != INVALID_FILE_ATTRIBUTES
383 && file_attributes & FILE_ATTRIBUTE_READONLY)
384 SetFileAttributesW (wnm, file_attributes & ~FILE_ATTRIBUTE_READONLY);
385
386 int status = _wunlink (wnm);
387 free ((void *) wnm);
388
389 return status;
390#else
391 return unlink (nm);
392#endif
393}
394
395pid_t
397{
398#if defined (HAVE_VFORK)
399 return vfork ();
400#else
401 return -1;
402#endif
403}
404
405bool
407{
408#if defined (HAVE_FORK)
409 return true;
410#else
411 return false;
412#endif
413}
414
415bool
417{
418#if defined (HAVE_VFORK)
419 return true;
420#else
421 return false;
422#endif
423}
int unlink(const std::string &name)
Definition file-ops.cc:727
int symlink(const std::string &old_name, const std::string &new_name)
Definition file-ops.cc:531
int rmdir(const std::string &name)
Definition file-ops.cc:631
int link(const std::string &old_name, const std::string &new_name)
Definition file-ops.cc:508
int chdir(const std::string &path_arg)
Definition lo-sysdep.cc:107
std::string getcwd()
Definition lo-sysdep.cc:72
T::size_type strlen(const typename T::value_type *str)
Definition oct-string.cc:88
int pipe(int *fildes)
pid_t fork(std::string &msg)
int execvp(const std::string &file, const string_vector &argv)
pid_t getppid()
pid_t getpgrp(std::string &msg)
gid_t getegid()
uid_t getuid()
gid_t getgid()
pid_t vfork(std::string &msg)
pid_t getpid()
uid_t geteuid()
int dup2(int old_fd, int new_fd)
#define isatty
void * malloc(unsigned)
void free(void *)
#define STDIN_FILENO
Definition sysdep.cc:92
wchar_t * u8_to_wchar(const char *u8)
char * u8_from_wchar(const wchar_t *wc)
int octave_access_f_ok(void)
pid_t octave_setsid_wrapper(void)
gid_t octave_getgid_wrapper(void)
uid_t octave_geteuid_wrapper(void)
int octave_ftruncate_wrapper(int fd, off_t sz)
int octave_gethostname_wrapper(char *nm, size_t len)
int octave_rmdir_wrapper(const char *nm)
pid_t octave_fork_wrapper(void)
char * octave_getcwd_wrapper(char *nm, size_t len)
pid_t octave_getpgrp_wrapper(void)
pid_t octave_getppid_wrapper(void)
int octave_access_r_ok(void)
bool octave_have_vfork(void)
int octave_execvp_wrapper(const char *file, char *const *argv)
gid_t octave_getegid_wrapper(void)
int octave_execv_wrapper(const char *file, char *const *argv)
pid_t octave_getpid_wrapper(void)
int octave_close_wrapper(int fd)
int octave_access_wrapper(const char *nm, int mode)
int octave_unlink_wrapper(const char *nm)
int octave_chdir_wrapper(const char *nm)
int octave_symlink_wrapper(const char *nm1, const char *nm2)
uid_t octave_getuid_wrapper(void)
int octave_access_x_ok(void)
int octave_stderr_fileno(void)
int octave_link_wrapper(const char *nm1, const char *nm2)
int octave_access_w_ok(void)
const char * octave_ctermid_wrapper(void)
int octave_stdout_fileno(void)
pid_t octave_vfork_wrapper(void)
int octave_isatty_wrapper(int fd)
bool octave_have_fork(void)
int octave_dup2_wrapper(int fd1, int fd2)
int octave_stdin_fileno(void)
int octave_pipe_wrapper(int *fd)
F77_RET_T len
Definition xerbla.cc:61