GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
unistd-wrappers.c
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2016-2017 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 // These functions may be provided by gnulib. We don't include gnulib
24 // headers directly in Octave's C++ source files to avoid problems that
25 // may be caused by the way that gnulib overrides standard library
26 // functions.
27 
28 #if defined (HAVE_CONFIG_H)
29 # include "config.h"
30 #endif
31 
32 #include <stdio.h>
33 
34 #include <stdio.h>
35 
36 #include <sys/types.h>
37 #include <unistd.h>
38 
39 #if defined (__WIN32__) && ! defined (__CYGWIN__)
40 # include <process.h>
41 #endif
42 
43 #include "unistd-wrappers.h"
44 
45 int
47 {
48  return F_OK;
49 }
50 
51 int
53 {
54  return R_OK;
55 }
56 
57 int
59 {
60  return W_OK;
61 }
62 
63 int
65 {
66  return X_OK;
67 }
68 
69 int
70 octave_access_wrapper (const char *nm, int mode)
71 {
72  return access (nm, mode);
73 }
74 
75 int
76 octave_chdir_wrapper (const char *nm)
77 {
78  return chdir (nm);
79 }
80 
81 int
83 {
84  return close (fd);
85 }
86 
87 const char *
89 {
90 #if defined (HAVE_CTERMID)
91  return ctermid (0);
92 #else
93  return "/dev/tty";
94 #endif
95 }
96 
97 int
98 octave_dup2_wrapper (int fd1, int fd2)
99 {
100  return dup2 (fd1, fd2);
101 }
102 
103 #if defined (__WIN32__) && ! defined (__CYGWIN__)
104 
105 // Adapted from libtool wrapper.
106 
107 /* Prepares an argument vector before calling spawn().
108 
109  Note that spawn() does not by itself call the command interpreter
110  (getenv ("COMSPEC") != NULL ? getenv ("COMSPEC") :
111  ({ OSVERSIONINFO v; v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
112  GetVersionEx(&v);
113  v.dwPlatformId == VER_PLATFORM_WIN32_NT;
114  }) ? "cmd.exe" : "command.com").
115 
116  Instead it simply concatenates the arguments, separated by ' ', and calls
117  CreateProcess(). We must quote the arguments since Win32 CreateProcess()
118  interprets characters like ' ', '\t', '\\', '"' (but not '<' and '>') in a
119  special way:
120 
121  - Space and tab are interpreted as delimiters. They are not treated as
122  delimiters if they are surrounded by double quotes: "...".
123 
124  - Unescaped double quotes are removed from the input. Their only effect is
125  that within double quotes, space and tab are treated like normal
126  characters.
127 
128  - Backslashes not followed by double quotes are not special.
129 
130  - But 2*n+1 backslashes followed by a double quote become
131  n backslashes followed by a double quote (n >= 0):
132  \" -> "
133  \\\" -> \"
134  \\\\\" -> \\"
135  */
136 
137 #define SHELL_SPECIAL_CHARS \
138  "\"\\ \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
139 
140 #define SHELL_SPACE_CHARS \
141  " \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
142 
143 static char **
144 prepare_spawn (char **argv)
145 {
146  size_t argc;
147  char **new_argv;
148  size_t i;
149 
150  /* Count number of arguments. */
151  for (argc = 0; argv[argc] != NULL; argc++)
152  ;
153 
154  /* Allocate new argument vector. */
155  new_argv = (char **) malloc (argc + 1);
156 
157  /* Put quoted arguments into the new argument vector. */
158  for (i = 0; i < argc; i++)
159  {
160  const char *string = argv[i];
161 
162  if (string[0] == '\0')
163  new_argv[i] = strdup ("\"\"");
164  else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL)
165  {
166  int quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL);
167  size_t length;
168  unsigned int backslashes;
169  const char *s;
170  char *quoted_string;
171  char *p;
172 
173  length = 0;
174  backslashes = 0;
175  if (quote_around)
176  length++;
177  for (s = string; *s != '\0'; s++)
178  {
179  char c = *s;
180  if (c == '"')
181  length += backslashes + 1;
182  length++;
183  if (c == '\\')
184  backslashes++;
185  else
186  backslashes = 0;
187  }
188  if (quote_around)
189  length += backslashes + 1;
190 
191  quoted_string = (char *) malloc (length + 1);
192 
193  p = quoted_string;
194  backslashes = 0;
195  if (quote_around)
196  *p++ = '"';
197  for (s = string; *s != '\0'; s++)
198  {
199  char c = *s;
200  if (c == '"')
201  {
202  unsigned int j;
203  for (j = backslashes + 1; j > 0; j--)
204  *p++ = '\\';
205  }
206  *p++ = c;
207  if (c == '\\')
208  backslashes++;
209  else
210  backslashes = 0;
211  }
212  if (quote_around)
213  {
214  unsigned int j;
215  for (j = backslashes; j > 0; j--)
216  *p++ = '\\';
217  *p++ = '"';
218  }
219  *p = '\0';
220 
221  new_argv[i] = quoted_string;
222  }
223  else
224  new_argv[i] = strdup (string);
225  }
226 
227  new_argv[argc] = NULL;
228 
229  return new_argv;
230 }
231 
232 #endif
233 
234 int
235 octave_execv_wrapper (const char *file, char *const *argv)
236 {
237 #if defined (__WIN32__) && ! defined (__CYGWIN__)
238 
239  char **sanitized_argv = prepare_spawn (argv);
240 
241  int status = spawnv (P_OVERLAY, file, sanitized_argv);
242 
243  // This only happens if spawnv fails.
244 
245  char **p = sanitized_argv;
246  while (*p)
247  free (*p++);
248  free (sanitized_argv);
249 
250  return status;
251 
252 #else
253 
254  return execv (file, argv);
255 
256 #endif
257 }
258 
259 int
260 octave_execvp_wrapper (const char *file, char *const *argv)
261 {
262  return execvp (file, argv);
263 }
264 
265 pid_t
267 {
268 #if defined (HAVE_FORK)
269  return fork ();
270 #else
271  return -1;
272 #endif
273 }
274 
275 int
277 {
278  return ftruncate (fd, sz);
279 }
280 
281 char *
282 octave_getcwd_wrapper (char *nm, size_t len)
283 {
284  return getcwd (nm, len);
285 }
286 
287 gid_t
289 {
290 #if defined (HAVE_GETEGID)
291  return getegid ();
292 #else
293  return -1;
294 #endif
295 }
296 
297 uid_t
299 {
300 #if defined (HAVE_GETEUID)
301  return geteuid ();
302 #else
303  return -1;
304 #endif
305 }
306 
307 gid_t
309 {
310 #if defined (HAVE_GETGID)
311  return getgid ();
312 #else
313  return -1;
314 #endif
315 }
316 
317 int
318 octave_gethostname_wrapper (char *nm, size_t len)
319 {
320  return gethostname (nm, len);
321 }
322 
323 pid_t
325 {
326 #if defined (HAVE_GETPGRP)
327  return getpgrp ();
328 #else
329  return -1;
330 #endif
331 }
332 
333 pid_t
335 {
336 #if defined (HAVE_GETPID)
337  return getpid ();
338 #else
339  return -1;
340 #endif
341 }
342 
343 pid_t
345 {
346 #if defined (HAVE_GETPPID)
347  return getppid ();
348 #else
349  return -1;
350 #endif
351 }
352 
353 uid_t
355 {
356 #if defined (HAVE_GETUID)
357  return getuid ();
358 #else
359  return -1;
360 #endif
361 }
362 
363 int
365 {
366  return isatty (fd);
367 }
368 
369 int
370 octave_link_wrapper (const char *nm1, const char *nm2)
371 {
372  return link (nm1, nm2);
373 }
374 
375 int
377 {
378  return pipe (fd);
379 }
380 
381 int
383 {
384  return rmdir (nm);
385 }
386 
387 pid_t
389 {
390 #if defined (HAVE_SETSID)
391  return setsid ();
392 #else
393  return -1;
394 #endif
395 }
396 
397 int
399 {
400  return STDIN_FILENO;
401 }
402 
403 int
405 {
406  return STDOUT_FILENO;
407 }
408 
409 int
410 octave_symlink_wrapper (const char *nm1, const char *nm2)
411 {
412  return symlink (nm1, nm2);
413 }
414 
415 int
417 {
418  return unlink (nm);
419 }
420 
421 pid_t
423 {
424 #if defined (HAVE_VFORK)
425  return vfork ();
426 #else
427  return -1;
428 #endif
429 }
430 
431 bool
433 {
434 #if defined (HAVE_FORK)
435  return true;
436 #else
437  return false;
438 #endif
439 }
440 
441 bool
443 {
444 #if defined (HAVE_VFORK)
445  return true;
446 #else
447  return false;
448 #endif
449 }
int dup2(int old_fd, int new_fd)
Definition: oct-syscalls.cc:50
uid_t octave_geteuid_wrapper(void)
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:120
pid_t octave_vfork_wrapper(void)
int octave_link_wrapper(const char *nm1, const char *nm2)
int unlink(const std::string &name)
Definition: file-ops.cc:648
int octave_rmdir_wrapper(const char *nm)
pid_t octave_getpid_wrapper(void)
int symlink(const std::string &old_name, const std::string &new_name)
Definition: file-ops.cc:470
int octave_ftruncate_wrapper(int fd, off_t sz)
int argc
Definition: load-save.cc:633
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6386
uid_t octave_getuid_wrapper(void)
int octave_stdout_fileno(void)
#define isatty
pid_t getpid(void)
gid_t getgid(void)
pid_t getppid(void)
#define STDIN_FILENO
Definition: sysdep.cc:93
int octave_pipe_wrapper(int *fd)
s
Definition: file-io.cc:2682
int octave_access_wrapper(const char *nm, int mode)
string_vector argv
Definition: load-save.cc:635
int octave_access_r_ok(void)
int octave_access_f_ok(void)
int octave_symlink_wrapper(const char *nm1, const char *nm2)
static octave_idx_type link(octave_idx_type s, octave_idx_type t, octave_idx_type *pp)
Definition: colamd.cc:105
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:941
int octave_isatty_wrapper(int fd)
gid_t octave_getgid_wrapper(void)
std::string getcwd(void)
Definition: lo-sysdep.cc:39
int octave_access_x_ok(void)
int octave_unlink_wrapper(const char *nm)
gid_t octave_getegid_wrapper(void)
bool octave_have_vfork(void)
uid_t geteuid(void)
sz
Definition: data.cc:5342
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
pid_t octave_fork_wrapper(void)
pid_t octave_setsid_wrapper(void)
int octave_execvp_wrapper(const char *file, char *const *argv)
bool octave_have_fork(void)
int rmdir(const std::string &name)
Definition: file-ops.cc:543
void free(void *)
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
int octave_stdin_fileno(void)
int execvp(const std::string &file, const string_vector &argv)
Definition: oct-syscalls.cc:72
int octave_gethostname_wrapper(char *nm, size_t len)
int chdir(const std::string &path_arg)
Definition: lo-sysdep.cc:59
int octave_close_wrapper(int fd)
pid_t octave_getppid_wrapper(void)
int octave_execv_wrapper(const char *file, char *const *argv)
gid_t getegid(void)
pid_t fork(std::string &msg)
Definition: oct-syscalls.cc:97
const char * octave_ctermid_wrapper(void)
void * malloc(size_t)
uid_t getuid(void)
int pipe(int *fildes)
pid_t octave_getpgrp_wrapper(void)
char * octave_getcwd_wrapper(char *nm, size_t len)
int octave_chdir_wrapper(const char *nm)
int octave_dup2_wrapper(int fd1, int fd2)
int octave_access_w_ok(void)
pid_t getpgrp(std::string &msg)
pid_t vfork(std::string &msg)