/[meta-cvs]/meta-cvs/F-83F0E6A90D5BBCDFB58440970E850925.c
ViewVC logotype

Contents of /meta-cvs/F-83F0E6A90D5BBCDFB58440970E850925.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (show annotations)
Fri Jun 24 07:22:53 2005 UTC (8 years, 9 months ago) by kaz
Branch: MAIN
CVS Tags: asdf-import-branch~merged-to-HEAD-0, mcvs-1-1-98, asdf-import-branch~branch-point, HEAD
Branch point for: asdf-import-branch
Changes since 1.15: +35 -0 lines
File MIME type: text/plain
Add hack to get our own main() function which calls the one
inside CLISP.

* code/unix-bindings/link.sh: Add objcopy hack.

* code/unix-bindings/wrap.c (main): New function.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <signal.h>
6 #include <dirent.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/wait.h>
11 #ifdef __CYGWIN__
12 #include <process.h>
13 #endif
14
15 /*
16 * <errno.h> stuff
17 */
18
19 int mcvs_get_errno(void)
20 {
21 return errno;
22 }
23
24 int mcvs_set_errno(int value)
25 {
26 return errno = value;
27 }
28
29 /*
30 * <dirent.h> stuff
31 */
32
33 typedef struct {
34 unsigned long d_ino;
35 char d_name[1024];
36 } mcvs_dirent;
37
38 mcvs_dirent *mcvs_readdir(DIR *dir)
39 {
40 static mcvs_dirent dw;
41 struct dirent *de = readdir(dir);
42 if (de != 0) {
43 strncpy(dw.d_name, de->d_name, sizeof dw.d_name - 1);
44 dw.d_ino = de->d_ino;
45 return &dw;
46 }
47 return 0;
48 }
49
50 /*
51 * <unistd.h> stuff
52 */
53
54 char *mcvs_readlink(const char *path)
55 {
56 size_t size = 256;
57 char *str = malloc(size);
58 char *temp;
59
60 if (str == 0)
61 goto bail;
62
63 for (;;) {
64 int result = readlink(path, str, size);
65 if (result == -1)
66 goto bail;
67 if (result < size) {
68 str[result] = 0;
69 break;
70 }
71 if (size * 2 < size)
72 goto bail;
73 size *= 2;
74 if ((temp = realloc(str, size)) == 0)
75 goto bail;
76 str = temp;
77 }
78
79 /* No need to realloc to actual size, since CLISP will free this anyway */
80 return str;
81
82 bail:
83 free(str);
84 return 0;
85 }
86
87 /*
88 * <sys/stat.h> stuff
89 */
90
91 struct mcvs_stat {
92 unsigned long dev;
93 unsigned long ino;
94 unsigned long mode;
95 unsigned int nlink;
96 unsigned int uid;
97 unsigned int gid;
98 unsigned long rdev;
99 unsigned long blksize;
100 unsigned long blocks;
101 unsigned long atime;
102 unsigned long mtime;
103 unsigned long ctime;
104 };
105
106 static void stat_to_wrap(struct mcvs_stat *out, const struct stat *in)
107 {
108 out->dev = in->st_dev;
109 out->ino = in->st_ino;
110 out->mode = in->st_mode;
111 out->nlink = in->st_nlink;
112 out->uid = in->st_uid;
113 out->gid = in->st_gid;
114 out->rdev = in->st_rdev;
115 out->blksize = in->st_blksize;
116 out->blocks = in->st_blocks;
117 out->atime = in->st_atime;
118 out->mtime = in->st_mtime;
119 out->ctime = in->st_ctime;
120 }
121
122 #define IMPL_STAT(FUNC, ARGTYPE) \
123 int mcvs_ ## FUNC(ARGTYPE arg, struct mcvs_stat *buf) \
124 { \
125 struct stat sbuf; \
126 int retval = FUNC(arg, &sbuf); \
127 if (retval == 0) \
128 stat_to_wrap(buf, &sbuf); \
129 return retval; \
130 }
131
132 IMPL_STAT(stat, const char *)
133 IMPL_STAT(lstat, const char *)
134 IMPL_STAT(fstat, int)
135
136 /*
137 * <unistd.h> -- getcwd
138 */
139
140 const char *mcvs_getcwd(void)
141 {
142 size_t size = 256;
143 char *str = malloc(size);
144 char *temp;
145
146 if (str == 0)
147 goto bail;
148
149 while (getcwd(str, size) == 0) {
150 if (errno != ERANGE)
151 goto bail;
152 if (size * 2 < size)
153 goto bail;
154 size *= 2;
155 if ((temp = realloc(str, size)) == 0)
156 goto bail;
157 str = temp;
158 }
159
160 /* No need to realloc to actual size, since CLISP will free this anyway */
161 return str;
162
163 bail:
164 free(str);
165 return 0;
166 }
167
168 /*
169 * We need this because CLISP sets it to SIG_IGN, which is
170 * inherited by the exec'd image, and causes the wait()
171 * functions to have behavior that programs don't expect.
172 */
173
174 void mcvs_default_sigchld()
175 {
176 signal(SIGCHLD, SIG_DFL);
177 }
178
179 /*
180 * <unistd.h> -- fork, wait*, exec*
181 */
182
183 #ifdef __CYGWIN__
184 /*
185 * On Cygwin, we have a straightforward alternative to fork exec
186 * and wait, so let's use it.
187 */
188
189 int mcvs_spawn(const char *name, const char *const *argv)
190 {
191 return spawnvp(_P_WAIT, name, argv);
192 }
193 #else
194 int mcvs_spawn(const char *name, char *const *argv)
195 {
196 int result = -1;
197 int status = 0;
198 pid_t child;
199
200 mcvs_default_sigchld();
201
202 child = fork();
203
204 if (child == -1)
205 goto out;
206
207 if (child == 0) {
208 execvp(name, argv);
209 _exit(EXIT_FAILURE);
210 }
211
212 do {
213 result = waitpid(child, &status, 0);
214 } while (result == -1 && errno == EINTR);
215
216 if (result == -1)
217 goto out;
218
219 if (WIFEXITED(status))
220 result = WEXITSTATUS(status);
221
222 out:
223 free((void *) argv);
224 return result;
225 }
226 #endif
227
228 /*
229 * Terminal handling
230 */
231
232 char *mcvs_ctermid(void)
233 {
234 char *buf = malloc(L_ctermid + 1);
235 if (buf == 0)
236 return 0;
237 return ctermid(buf);
238 }
239
240 /*
241 * Main function. We arrange this by a hack; when we build the
242 * Lisp image, we rename the main function in the lisp.a archive
243 * to clisp_main using the objcopy utility. Then we can have our
244 * own main. See lisp.sh file.
245 */
246
247 int main(int argc, char **argv)
248 {
249 extern int clisp_main(int argc, char **argv);
250 int new_argc = argc + 2;
251 char **new_argv = malloc(sizeof *new_argv * (new_argc + 1));
252 int i;
253 char E_option[] = "-Eforeign";
254 char E_arg[] = "iso-8859-1";
255 int retcode;
256
257 if (argv == 0) {
258 fprintf(stderr, "%s: out of memory\n", argv[0]);
259 return EXIT_FAILURE;
260 }
261
262 new_argv[0] = argv[0];
263 new_argv[1] = E_option;
264 new_argv[2] = E_arg;
265 memcpy(&new_argv[3], &argv[1], sizeof argv[1] * (argc - 1));
266 new_argv[new_argc] = 0;
267
268 retcode = clisp_main(new_argc, new_argv);
269
270 free(new_argv);
271
272 return retcode;
273 }

  ViewVC Help
Powered by ViewVC 1.1.5