GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
unistd-wrappers.c
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2016-2022 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# if 0
152 // Code snippet from gnulib execute.c
153
154 // Executing arbitrary files as shell scripts is unsecure.
155 if (status == -1 && errno == ENOEXEC)
156 {
157 // prog is not a native executable. Try to execute it as a
158 // shell script. Note that prepare_spawn() has already prepended
159 // a hidden element "sh.exe" to argv.
160 argv[1] = prog_path;
161 status = _wspawnv (P_WAIT, wargv[0], wargv);
162 }
163# endif
164
165 // This happens when the spawned child process terminates.
166
167 free (wfile);
168 const wchar_t **wp = wargv;
169 // Casting away the const in the loop is ok here.
170 while (*wp)
171 free ((wchar_t *) *wp++);
172 free (wargv);
173
174# else
175
176 int status = spawnv (P_OVERLAY, file, sanitized_argv);
177
178 // This only happens if spawnv fails.
179
180 free (sanitized_argv);
181 free (argv_mem_to_free);
182
183# endif
184
185 return status;
186
187#else
188
189 return execv (file, argv);
190
191#endif
192}
193
194int
195octave_execvp_wrapper (const char *file, char *const *argv)
196{
197 return execvp (file, argv);
198}
199
200pid_t
202{
203#if defined (HAVE_FORK)
204 return fork ();
205#else
206 return -1;
207#endif
208}
209
210int
211octave_ftruncate_wrapper (int fd, off_t sz)
212{
213 return ftruncate (fd, sz);
214}
215
216char *
217octave_getcwd_wrapper (char *nm, size_t len)
218{
219#if defined (OCTAVE_USE_WINDOWS_API)
220 wchar_t *tmp = _wgetcwd (NULL, 0);
221 char *retval = NULL;
222
223 if (! tmp)
224 return retval;
225
226 retval = u8_from_wchar (tmp);
227 if (! nm)
228 return retval;
229 else
230 {
231 if (strlen (retval) > len)
232 return NULL;
233
234 memcpy (nm, retval, len);
235 free (retval);
236 return nm;
237 }
238#else
239 return getcwd (nm, len);
240#endif
241}
242
243gid_t
245{
246#if defined (HAVE_GETEGID)
247 return getegid ();
248#else
249 return -1;
250#endif
251}
252
253uid_t
255{
256#if defined (HAVE_GETEUID)
257 return geteuid ();
258#else
259 return -1;
260#endif
261}
262
263gid_t
265{
266#if defined (HAVE_GETGID)
267 return getgid ();
268#else
269 return -1;
270#endif
271}
272
273int
275{
276 return gethostname (nm, len);
277}
278
279pid_t
281{
282#if defined (HAVE_GETPGRP)
283 return getpgrp ();
284#else
285 return -1;
286#endif
287}
288
289pid_t
291{
292#if defined (HAVE_GETPID)
293 return getpid ();
294#else
295 return -1;
296#endif
297}
298
299pid_t
301{
302#if defined (HAVE_GETPPID)
303 return getppid ();
304#else
305 return -1;
306#endif
307}
308
309uid_t
311{
312#if defined (HAVE_GETUID)
313 return getuid ();
314#else
315 return -1;
316#endif
317}
318
319int
321{
322 return isatty (fd);
323}
324
325int
326octave_link_wrapper (const char *nm1, const char *nm2)
327{
328 return link (nm1, nm2);
329}
330
331int
333{
334 return pipe (fd);
335}
336
337int
338octave_rmdir_wrapper (const char *nm)
339{
340#if defined (OCTAVE_USE_WINDOWS_API)
341 wchar_t *wnm = u8_to_wchar (nm);
342 int status = _wrmdir (wnm);
343 free ((void *) wnm);
344 return status;
345#else
346 return rmdir (nm);
347#endif
348}
349
350pid_t
352{
353#if defined (HAVE_SETSID)
354 return setsid ();
355#else
356 return -1;
357#endif
358}
359
360int
362{
363 return STDIN_FILENO;
364}
365
366int
368{
369 return STDOUT_FILENO;
370}
371
372int
373octave_symlink_wrapper (const char *nm1, const char *nm2)
374{
375 return symlink (nm1, nm2);
376}
377
378int
379octave_unlink_wrapper (const char *nm)
380{
381#if defined (OCTAVE_USE_WINDOWS_API)
382 wchar_t *wnm = u8_to_wchar (nm);
383 int status = _wunlink (wnm);
384 free ((void *) wnm);
385 return status;
386#else
387 return unlink (nm);
388#endif
389}
390
391pid_t
393{
394#if defined (HAVE_VFORK)
395 return vfork ();
396#else
397 return -1;
398#endif
399}
400
401bool
403{
404#if defined (HAVE_FORK)
405 return true;
406#else
407 return false;
408#endif
409}
410
411bool
413{
414#if defined (HAVE_VFORK)
415 return true;
416#else
417 return false;
418#endif
419}
static octave_idx_type link(octave_idx_type s, octave_idx_type t, octave_idx_type *pp)
Definition: colamd.cc:99
pid_t getppid(void)
std::string getcwd(void)
Definition: lo-sysdep.cc:71
pid_t getpid(void)
int pipe(int *fildes)
gid_t getegid(void)
gid_t getgid(void)
pid_t getpgrp(std::string &msg)
int symlink(const std::string &old_name, const std::string &new_name)
Definition: file-ops.cc:456
int rmdir(const std::string &name)
Definition: file-ops.cc:530
int execvp(const std::string &file, const string_vector &argv)
Definition: oct-syscalls.cc:74
uid_t getuid(void)
int dup2(int old_fd, int new_fd)
Definition: oct-syscalls.cc:52
int unlink(const std::string &name)
Definition: file-ops.cc:621
uid_t geteuid(void)
pid_t fork(std::string &msg)
Definition: oct-syscalls.cc:99
int chdir(const std::string &path_arg)
Definition: lo-sysdep.cc:106
pid_t vfork(std::string &msg)
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:85
#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_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