1 /*----------------------------------------------------------------------------
2 --                                                                          --
3 --            FLORIST (FSU Implementation of POISX.5) COMPONENTS            --
4 --                                                                          --
5 --                                                                          --
6 --                       P O S I X - M A C R O S . C                        --
7 --                                                                          --
8 --  Copyright (c) 1996 Florida State University (FSU), All Rights Reserved. --
9 --                     Copyright (C) 1997-2010, AdaCore                     --
10 --                                                                          --
11 --  This file is a component of FLORIST, an  implementation of an  Ada API  --
12 --  for the POSIX OS services, for use with  the  GNAT  Ada  compiler  and  --
13 --  the FSU Gnu Ada Runtime Library (GNARL).   The  interface  is intended  --
14 --  to be close to that specified in  IEEE STD  1003.5: 1990  and IEEE STD  --
15 --  1003.5b: 1996.                                                          --
16 --                                                                          --
17 --  FLORIST is free software;  you can  redistribute  it and/or  modify it  --
18 --  under terms of the  GNU  General  Public  License as  published by the  --
19 --  Free Software Foundation;  either version  2, or (at  your option) any  --
20 --  later version.  FLORIST is distributed  in  the hope  that  it will be  --
21 --  useful, but WITHOUT ANY WARRANTY;  without  even the implied  warranty  --
22 --  of MERCHANTABILITY or FITNESS FOR A PARTICULAR  PURPOSE.  See  the GNU  --
23 --  General Public License for more details.  You  should have  received a  --
24 --  copy of the GNU General Public License  distributed  with  GNARL;  see  --
25 --  file  COPYING.  If not,  write to  the  Free  Software  Foundation, 59  --
26 --  Temple Place - Suite 330, Boston, MA 02111-1307, USA.                   --
27 --                                                                          --
28 --                                                                          --
29 --                                                                          --
30 --                                                                          --
31 --                                                                          --
32 --                                                                          --
33 --                                                                          --
34 --                                                                          --
35 ----------------------------------------------------------------------------*/
36 
37 /* file: posix-macros.c
38    --------------------
39    These subprograms provide access to POSIX functionality that is
40    provided for C programs via macros.
41  */
42 
43 #define _REENTRANT
44 
45 #include "pconfig.h"
46 #include <unistd.h>
47 #include <sys/stat.h>
48 #include <sys/wait.h>
49 #include <sys/types.h>
50 #include <sys/utsname.h>
51 #include <errno.h>
52 
53 /* This definition is need for multi-threaded error codes on Solaris */
54 
s_isdir(mode_t mode)55 int s_isdir(mode_t mode) {
56 #ifdef S_ISDIR
57   return S_ISDIR(mode);
58 #else
59   return -1;
60 #endif
61 }
62 
s_ischr(mode_t mode)63 int s_ischr(mode_t mode) {
64 #ifdef S_ISCHR
65   return S_ISCHR(mode);
66 #else
67   return -1;
68 #endif
69 }
70 
s_isblk(mode_t mode)71 int s_isblk(mode_t mode) {
72 #ifdef S_ISBLK
73   return S_ISBLK(mode);
74 #else
75   return -1;
76 #endif
77 }
78 
s_isreg(mode_t mode)79 int s_isreg(mode_t mode) {
80 #ifdef S_ISREG
81   return S_ISREG(mode);
82 #else
83   return -1;
84 #endif
85 }
86 
s_islnk(mode_t mode)87 int s_islnk(mode_t mode) {
88 #ifdef S_ISLNK
89   return S_ISLNK(mode);
90 #else
91   return -1;
92 #endif
93 }
94 
s_issock(mode_t mode)95 int s_issock(mode_t mode) {
96 #ifdef S_ISSOCK
97   return S_ISSOCK(mode);
98 #else
99   return -1;
100 #endif
101 }
102 
s_isfifo(mode_t mode)103 int s_isfifo(mode_t mode) {
104 #ifdef S_ISFIFO
105   return S_ISFIFO(mode);
106 #else
107   return -1;
108 #endif
109 }
110 
s_ismsg(mode_t mode)111 int s_ismsg(mode_t mode) {
112 #ifdef S_ISMSG
113   return S_ISMSG(mode);
114 #else
115   return -1;
116 #endif
117 }
118 
s_typeismq(struct stat * p)119 int s_typeismq(struct stat *p) {
120 #ifdef S_TYPEISMQ
121   return S_TYPEISMQ(p);
122 #else
123   return 0;
124 #endif
125 }
126 
s_issem(mode_t mode)127 int s_issem(mode_t mode) {
128 #ifdef S_ISSEM
129   return S_ISSEM(mode);
130 #else
131   return -1;
132 #endif
133 }
134 
s_typeissem(struct stat * p)135 int s_typeissem(struct stat *p) {
136 #ifdef S_TYPEISSEM
137   return S_TYPEISSEM(p);
138 #else
139   return 0;
140 #endif
141 }
142 
s_isshm(mode_t mode)143 int s_isshm(mode_t mode) {
144 #ifdef S_ISSHM
145   return S_ISSHM(mode);
146 #else
147   return -1;
148 #endif
149 }
150 
s_typeisshm(struct stat * p)151 int s_typeisshm(struct stat *p) {
152 #ifdef S_TYPEISSHM
153   return S_TYPEISSHM(p);
154 #else
155   return 0;
156 #endif
157 }
158 
wifexited(int stat_val)159 int wifexited(int stat_val) {
160 #ifdef WIFEXITED
161   return WIFEXITED(stat_val);
162 #else
163   return -1;
164 #endif
165 }
166 
wexitstatus(int stat_val)167 int wexitstatus(int stat_val) {
168 #ifdef WEXITSTATUS
169   return WEXITSTATUS(stat_val);
170 #else
171   return -1;
172 #endif
173 }
174 
wifsignaled(int stat_val)175 int wifsignaled(int stat_val) {
176 #ifdef WIFSIGNALED
177   return WIFSIGNALED(stat_val);
178 #else
179   return -1;
180 #endif
181 }
182 
wtermsig(int stat_val)183 int wtermsig(int stat_val) {
184 #ifdef WTERMSIG
185   return WTERMSIG(stat_val);
186 #else
187   return -1;
188 #endif
189 }
190 
wifstopped(int stat_val)191 int wifstopped(int stat_val) {
192 #ifdef WIFSTOPPED
193   return WIFSTOPPED(stat_val);
194 #else
195   return -1;
196 #endif
197 }
198 
wstopsig(int stat_val)199 int wstopsig(int stat_val) {
200 #ifdef WSTOPSIG
201   return WSTOPSIG(stat_val);
202 #else
203   return -1;
204 #endif
205 }
206 
fetch_errno()207 int fetch_errno() {
208   return errno;
209 }
210 
store_errno(int value)211 void store_errno(int value) {
212   errno = value;
213 }
214 
215 /* The following are variadic functions and on some platforms, for
216    instance x86-64, calling a variadic function directly from Ada can
217    cause problems. Here we provide wrappers that we import instead. */
218 
__gnat_florist_open(const char * path,int oflag,mode_t mode)219 int __gnat_florist_open(const char *path, int oflag, mode_t mode) {
220   return open (path, oflag, mode);
221 }
222 
__gnat_florist_sem_open(char * name,int oflag,mode_t mode,unsigned value)223 sem_t *__gnat_florist_sem_open
224 (char *name, int oflag, mode_t mode, unsigned value) {
225   return sem_open (name, oflag, mode, value);
226 }
227 
228 /* The following wrappers work around problems on systems where the
229    stat family of functions are implemented using macros. (eg. Tru64
230    5.1A and Linux.) */
231 
__gnat_florist_stat(const char * path,struct stat * buf)232 int __gnat_florist_stat(const char *path, struct stat *buf) {
233   return stat(path, buf);
234 }
235 
__gnat_florist_lstat(const char * path,struct stat * buf)236 int __gnat_florist_lstat(const char *path, struct stat *buf) {
237   return lstat(path, buf);
238 }
239 
__gnat_florist_fstat(int fd,struct stat * buf)240 int __gnat_florist_fstat(int fd, struct stat *buf) {
241   return fstat(fd, buf);
242 }
243 
244 /* The following wrapper ensures that uname(3) is mapped correctly even
245    when it is defined as an inlined function. */
__gnat_florist_uname(struct utsname * s)246 int __gnat_florist_uname (struct utsname *s) {
247   return uname (s);
248 }
249