OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [stdlib/] [system.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
/*
2
FUNCTION
3
<<system>>---execute command string
4
 
5
INDEX
6
        system
7
INDEX
8
        _system_r
9
 
10
ANSI_SYNOPSIS
11
        #include <stdlib.h>
12
        int system(char *<[s]>);
13
 
14
        int _system_r(void *<[reent]>, char *<[s]>);
15
 
16
TRAD_SYNOPSIS
17
        #include <stdlib.h>
18
        int system(<[s]>)
19
        char *<[s]>;
20
 
21
        int _system_r(<[reent]>, <[s]>)
22
        char *<[reent]>;
23
        char *<[s]>;
24
 
25
DESCRIPTION
26
 
27
Use <<system>> to pass a command string <<*<[s]>>> to <</bin/sh>> on
28
your system, and wait for it to finish executing.
29
 
30
Use `<<system(NULL)>>' to test whether your system has <</bin/sh>>
31
available.
32
 
33
The alternate function <<_system_r>> is a reentrant version.  The
34
extra argument <[reent]> is a pointer to a reentrancy structure.
35
 
36
RETURNS
37
<<system(NULL)>> returns a non-zero value if <</bin/sh>> is available, and
38
<<0>> if it is not.
39
 
40
With a command argument, the result of <<system>> is the exit status
41
returned by <</bin/sh>>.
42
 
43
PORTABILITY
44
ANSI C requires <<system>>, but leaves the nature and effects of a
45
command processor undefined.  ANSI C does, however, specify that
46
<<system(NULL)>> return zero or nonzero to report on the existence of
47
a command processor.
48
 
49
POSIX.2 requires <<system>>, and requires that it invoke a <<sh>>.
50
Where <<sh>> is found is left unspecified.
51
 
52
Supporting OS subroutines required: <<_exit>>, <<_execve>>, <<_fork_r>>,
53
<<_wait_r>>.
54
*/
55
 
56
#include <errno.h>
57
#include <stddef.h>
58
#include <stdlib.h>
59
#include <_syslist.h>
60
 
61
#if defined (unix) || defined (__CYGWIN32__)
62
static int do_system ();
63
#endif
64
 
65
int
66
_system_r (ptr, s)
67
     struct _reent *ptr;
68
     _CONST char *s;
69
{
70
#ifdef NO_EXEC
71
  if (s == NULL)
72
    return 0;
73
  errno = ENOSYS;
74
  return -1;
75
#else
76
 
77
  /* ??? How to handle (s == NULL) here is not exactly clear.
78
     If _fork_r fails, that's not really a justification for returning 0.
79
     For now we always return 0 and leave it to each target to explicitly
80
     handle otherwise (this can always be relaxed in the future).  */
81
 
82
#if defined (unix) || defined (__CYGWIN32__)
83
  if (s == NULL)
84
    return 1;
85
  return do_system (ptr, s);
86
#else
87
  if (s == NULL)
88
    return 0;
89
  errno = ENOSYS;
90
  return -1;
91
#endif
92
 
93
#endif
94
}
95
 
96
#ifndef _REENT_ONLY
97
 
98
int
99
system (s)
100
     _CONST char *s;
101
{
102
  return _system_r (_REENT, s);
103
}
104
 
105
#endif
106
 
107
#if defined (unix) && !defined (__CYGWIN32__)
108
static int
109
do_system (ptr, s)
110
     struct _reent *ptr;
111
     _CONST char *s;
112
{
113
  char *argv[4];
114
  int pid, status;
115
  extern char *environ[];
116
 
117
  argv[0] = "sh";
118
  argv[1] = "-c";
119
  argv[2] = (char *) s;
120
  argv[3] = NULL;
121
 
122
  if ((pid = _fork_r (ptr)) == 0)
123
    {
124
      _execve ("/bin/sh", argv, environ);
125
      exit (100);
126
    }
127
  else if (pid == -1)
128
    return -1;
129
  else
130
    {
131
      int rc = _wait_r (ptr, &status);
132
      if (rc == -1)
133
        return -1;
134
      status = (status >> 8) & 0xff;
135
      return status;
136
    }
137
}
138
#endif
139
 
140
#if defined (__CYGWIN32__)
141
static int
142
do_system (ptr, s)
143
     struct _reent *ptr;
144
     _CONST char *s;
145
{
146
  char *argv[4];
147
  int pid, status;
148
  extern char *environ[];
149
 
150
  argv[0] = "sh";
151
  argv[1] = "-c";
152
  argv[2] = (char *) s;
153
  argv[3] = NULL;
154
 
155
  if ((pid = vfork ()) == 0)
156
    {
157
      /* ??? It's not clear what's the right path to take (pun intended :-).
158
         There won't be an "sh" in any fixed location so we need each user
159
         to be able to say where to find "sh".  That suggests using an
160
         environment variable, but after a few more such situations we may
161
         have too many of them.  */
162
      char *sh = getenv ("SH_PATH");
163
      if (sh == NULL)
164
        sh = "/bin/sh";
165
      _execve (sh, argv, environ);
166
      exit (100);
167
    }
168
  else if (pid == -1)
169
    return -1;
170
  else
171
    {
172
      int rc = _wait (&status);
173
      if (rc == -1)
174
        return -1;
175
      status = (status >> 8) & 0xff;
176
      return status;
177
    }
178
}
179
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.