File: wrap.c

package info (click to toggle)
mcvs 1.0.13-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 668 kB
  • ctags: 648
  • sloc: lisp: 5,091; ansic: 223; sh: 190; makefile: 58
file content (247 lines) | stat: -rw-r--r-- 4,599 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifdef __CYGWIN__
#include <process.h>
#endif
#include "wrap.h"

#define MCVS_WRAP(FUNC, RETTYPE, ARGNAMES, ARGDECLS) \
RETTYPE mcvs_ ## FUNC ARGDECLS \
{ \
  return FUNC ARGNAMES; \
}

MCVS_WRAP(strerror, char *, (errnum), (int errnum))
MCVS_WRAP(opendir, DIR *, (name), (const char *name))
MCVS_WRAP(closedir, int, (dir), (DIR *dir))
MCVS_WRAP(open, int, (pathname, flags), (const char *pathname, int flags))
MCVS_WRAP(close, int, (fd), (int fd))
MCVS_WRAP(chdir, int, (path), (const char *path))
MCVS_WRAP(fchdir, int, (fd), (int fd))
MCVS_WRAP(link, int, (oldpath, newpath), (const char *oldpath, const char *newpath))
MCVS_WRAP(symlink, int, (oldpath, newpath), (const char *oldpath, const char *newpath))
MCVS_WRAP(unlink, int, (pathname), (const char *pathname))
MCVS_WRAP(rmdir, int, (pathname), (const char *pathname))
MCVS_WRAP(chmod, int, (path, mode), (const char *path, mode_t mode))

/*
 * Null pointer test
 */

int mcvs_null_pointer_p(void *ptr)
{
  return ptr == 0;
}

/*
 * <errno.h> stuff
 */

int mcvs_get_errno(void)
{
  return errno;
}

int mcvs_set_errno(int value)
{
  return errno = value;
}

/*
 * <dirent.h> stuff
 */

mcvs_dirent *mcvs_readdir(DIR *dir)
{ 
  static mcvs_dirent dw;
  struct dirent *de = readdir(dir);
  if (de != 0) {
    strncpy(dw.d_name, de->d_name, sizeof dw.d_name - 1);
    dw.d_ino = de->d_ino;
    return &dw;
  }
  return 0;
}

/*
 * <unistd.h> stuff
 */

char *mcvs_readlink(const char *path)
{
  size_t size = 256;
  char *str = malloc(size);
  char *temp;

  if (str == 0)
    goto bail;

  for (;;) {
    int result = readlink(path, str, size);
    if (result == -1)
      goto bail;
    if (result < size) {
      str[result] = 0;
      break;
    }
    if (size * 2 < size)
      goto bail;
    size *= 2;
    if ((temp = realloc(str, size)) == 0)
      goto bail;
    str = temp;
  }

  /* No need to realloc to actual size, since CLISP will free this anyway */ 
  return str;

bail:
  free(str);
  return 0;
}

/*
 * <sys/stat.h> stuff
 */

static void stat_to_wrap(struct mcvs_stat *out, const struct stat *in)
{
  out->dev = in->st_dev;
  out->ino = in->st_ino;
  out->mode = in->st_mode;
  out->nlink = in->st_nlink;
  out->uid = in->st_uid;
  out->gid = in->st_gid;
  out->rdev = in->st_rdev;
  out->blksize = in->st_blksize;
  out->blocks = in->st_blocks;
  out->atime = in->st_atime;
  out->mtime = in->st_mtime;
  out->ctime = in->st_ctime;
}

#define IMPL_STAT(FUNC, ARGTYPE) \
int mcvs_ ## FUNC(ARGTYPE arg, struct mcvs_stat *buf) \
{ \
  struct stat sbuf; \
  int retval = FUNC(arg, &sbuf); \
  if (retval == 0) \
    stat_to_wrap(buf, &sbuf); \
  return retval; \
}

IMPL_STAT(stat, const char *)
IMPL_STAT(lstat, const char *)
IMPL_STAT(fstat, int)

/*
 * <unistd.h> -- getcwd
 */

const char *mcvs_getcwd(void)
{
  size_t size = 256;
  char *str = malloc(size);
  char *temp;

  if (str == 0)
    goto bail;

  while (getcwd(str, size) == 0) {
    if (errno != ERANGE)
      goto bail;
    if (size * 2 < size)
      goto bail;
    size *= 2;
    if ((temp = realloc(str, size)) == 0)
      goto bail;
    str = temp;
  }

  /* No need to realloc to actual size, since CLISP will free this anyway */ 
  return str;

bail:
  free(str);
  return 0;
}

/* 
 * We need this because CLISP sets it to SIG_IGN, which is
 * inherited by the exec'd image, and causes the wait()
 * functions to have behavior that programs don't expect.
 */

void mcvs_default_sigchld()
{
    signal(SIGCHLD, SIG_DFL);
}

/*
 * <unistd.h> -- fork, wait*, exec*
 */

#ifdef __CYGWIN__
/*
 * On Cygwin, we have a straightforward alternative to fork exec 
 * and wait, so let's use it.
 */

int mcvs_spawn(const char *name, const char *const *argv)
{
  return spawnvp(_P_WAIT, name, argv);
}
#else
int mcvs_spawn(const char *name, char *const *argv)
{
  int result = -1;
  int status = 0;
  pid_t child;

  mcvs_default_sigchld();

  child = fork();

  if (child == -1)
    goto out;

  if (child == 0) {
    execvp(name, argv);
    _exit(EXIT_FAILURE);
  }

  do {
    result = waitpid(child, &status, 0);
  } while (result == -1 && errno == EINTR);

  if (result == -1)
    goto out;

  if (WIFEXITED(status))
    result = WEXITSTATUS(status);

out:
  free((void *) argv);
  return result;
}
#endif

/*
 * Terminal handling
 */

char *mcvs_ctermid(void)
{
  char *buf = malloc(L_ctermid + 1);
  if (buf == 0)
    return 0;
  return ctermid(buf);
}