GNU Octave  9.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-2024 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 
56 int
58 {
59  return F_OK;
60 }
61 
62 int
64 {
65  return R_OK;
66 }
67 
68 int
70 {
71  return W_OK;
72 }
73 
74 int
76 {
77  return X_OK;
78 }
79 
80 int
81 octave_access_wrapper (const char *nm, int mode)
82 {
83  return access (nm, mode);
84 }
85 
86 int
87 octave_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 
99 int
101 {
102  return close (fd);
103 }
104 
105 const char *
107 {
108 #if defined (HAVE_CTERMID)
109  return ctermid (0);
110 #else
111  return "/dev/tty";
112 #endif
113 }
114 
115 int
116 octave_dup2_wrapper (int fd1, int fd2)
117 {
118  return dup2 (fd1, fd2);
119 }
120 
121 int
122 octave_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 
180 int
181 octave_execvp_wrapper (const char *file, char *const *argv)
182 {
183  return execvp (file, argv);
184 }
185 
186 pid_t
188 {
189 #if defined (HAVE_FORK)
190  return fork ();
191 #else
192  return -1;
193 #endif
194 }
195 
196 int
197 octave_ftruncate_wrapper (int fd, off_t sz)
198 {
199  return ftruncate (fd, sz);
200 }
201 
202 char *
203 octave_getcwd_wrapper (char *nm, size_t len)
204 {
205 #if defined (OCTAVE_USE_WINDOWS_API)
206  wchar_t *tmp = _wgetcwd (NULL, 0);
207  char *retval = NULL;
208 
209  if (! tmp)
210  return retval;
211 
212  retval = u8_from_wchar (tmp);
213  if (! nm)
214  return retval;
215  else
216  {
217  if (strlen (retval) > len)
218  return NULL;
219 
220  memcpy (nm, retval, len);
221  free (retval);
222  return nm;
223  }
224 #else
225  return getcwd (nm, len);
226 #endif
227 }
228 
229 gid_t
231 {
232 #if defined (HAVE_GETEGID)
233  return getegid ();
234 #else
235  return -1;
236 #endif
237 }
238 
239 uid_t
241 {
242 #if defined (HAVE_GETEUID)
243  return geteuid ();
244 #else
245  return -1;
246 #endif
247 }
248 
249 gid_t
251 {
252 #if defined (HAVE_GETGID)
253  return getgid ();
254 #else
255  return -1;
256 #endif
257 }
258 
259 int
260 octave_gethostname_wrapper (char *nm, size_t len)
261 {
262  return gethostname (nm, len);
263 }
264 
265 pid_t
267 {
268 #if defined (HAVE_GETPGRP)
269  return getpgrp ();
270 #else
271  return -1;
272 #endif
273 }
274 
275 pid_t
277 {
278 #if defined (HAVE_GETPID)
279  return getpid ();
280 #else
281  return -1;
282 #endif
283 }
284 
285 pid_t
287 {
288 #if defined (HAVE_GETPPID)
289  return getppid ();
290 #else
291  return -1;
292 #endif
293 }
294 
295 uid_t
297 {
298 #if defined (HAVE_GETUID)
299  return getuid ();
300 #else
301  return -1;
302 #endif
303 }
304 
305 int
307 {
308  return isatty (fd);
309 }
310 
311 int
312 octave_link_wrapper (const char *nm1, const char *nm2)
313 {
314  return link (nm1, nm2);
315 }
316 
317 int
319 {
320  return pipe (fd);
321 }
322 
323 int
324 octave_rmdir_wrapper (const char *nm)
325 {
326 #if defined (OCTAVE_USE_WINDOWS_API)
327  wchar_t *wnm = u8_to_wchar (nm);
328  int status = _wrmdir (wnm);
329  free ((void *) wnm);
330  return status;
331 #else
332  return rmdir (nm);
333 #endif
334 }
335 
336 pid_t
338 {
339 #if defined (HAVE_SETSID)
340  return setsid ();
341 #else
342  return -1;
343 #endif
344 }
345 
346 int
348 {
349  return STDIN_FILENO;
350 }
351 
352 int
354 {
355  return STDOUT_FILENO;
356 }
357 
358 int
359 octave_symlink_wrapper (const char *nm1, const char *nm2)
360 {
361  return symlink (nm1, nm2);
362 }
363 
364 int
365 octave_unlink_wrapper (const char *nm)
366 {
367 #if defined (OCTAVE_USE_WINDOWS_API)
368  wchar_t *wnm = u8_to_wchar (nm);
369 
370  // _wunlink fails on files with the read-only flag set. Try to un-set it.
371  DWORD file_attributes = GetFileAttributesW (wnm);
372  if (file_attributes != INVALID_FILE_ATTRIBUTES
373  && file_attributes & FILE_ATTRIBUTE_READONLY)
374  SetFileAttributesW (wnm, file_attributes & ~FILE_ATTRIBUTE_READONLY);
375 
376  int status = _wunlink (wnm);
377  free ((void *) wnm);
378 
379  return status;
380 #else
381  return unlink (nm);
382 #endif
383 }
384 
385 pid_t
387 {
388 #if defined (HAVE_VFORK)
389  return vfork ();
390 #else
391  return -1;
392 #endif
393 }
394 
395 bool
397 {
398 #if defined (HAVE_FORK)
399  return true;
400 #else
401  return false;
402 #endif
403 }
404 
405 bool
407 {
408 #if defined (HAVE_VFORK)
409  return true;
410 #else
411  return false;
412 #endif
413 }
int chdir(const std::string &path_arg)
Definition: lo-sysdep.cc:107
std::string getcwd()
Definition: lo-sysdep.cc:72
int symlink(const std::string &old_name, const std::string &new_name)
Definition: file-ops.cc:531
int link(const std::string &old_name, const std::string &new_name)
Definition: file-ops.cc:508
int unlink(const std::string &name)
Definition: file-ops.cc:727
int rmdir(const std::string &name)
Definition: file-ops.cc:631
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)
Definition: oct-syscalls.cc:99
int execvp(const std::string &file, const string_vector &argv)
Definition: oct-syscalls.cc:74
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)
Definition: oct-syscalls.cc:52
#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)
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)
char * octave_getcwd_wrapper(char *nm, size_t len)
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