+ case CB_SYS_lseek :
+ {
+ int fd = sc->arg1;
+ unsigned long offset = sc->arg2;
+ int whence = sc->arg3;
+
+ result = (*cb->lseek) (cb, fd, offset, whence);
+ if (result < 0)
+ goto ErrorFinish;
+ }
+ break;
+
+ case CB_SYS_unlink :
+ {
+ char *path;
+
+ errcode = get_path (cb, sc, sc->arg1, &path);
+ if (errcode != 0)
+ {
+ result = -1;
+ goto FinishSyscall;
+ }
+ result = (*cb->unlink) (cb, path);
+ free (path);
+ if (result < 0)
+ goto ErrorFinish;
+ }
+ break;
+
+ case CB_SYS_truncate :
+ {
+ char *path;
+ long len = sc->arg2;
+
+ errcode = get_path (cb, sc, sc->arg1, &path);
+ if (errcode != 0)
+ {
+ result = -1;
+ errcode = EFAULT;
+ goto FinishSyscall;
+ }
+ result = (*cb->truncate) (cb, path, len);
+ free (path);
+ if (result < 0)
+ goto ErrorFinish;
+ }
+ break;
+
+ case CB_SYS_ftruncate :
+ {
+ int fd = sc->arg1;
+ long len = sc->arg2;
+
+ result = (*cb->ftruncate) (cb, fd, len);
+ if (result < 0)
+ goto ErrorFinish;
+ }
+ break;
+
+ case CB_SYS_rename :
+ {
+ char *path1, *path2;
+
+ errcode = get_path (cb, sc, sc->arg1, &path1);
+ if (errcode != 0)
+ {
+ result = -1;
+ errcode = EFAULT;
+ goto FinishSyscall;
+ }
+ errcode = get_path (cb, sc, sc->arg2, &path2);
+ if (errcode != 0)
+ {
+ result = -1;
+ errcode = EFAULT;
+ free (path1);
+ goto FinishSyscall;
+ }
+ result = (*cb->rename) (cb, path1, path2);
+ free (path1);
+ free (path2);
+ if (result < 0)
+ goto ErrorFinish;
+ }
+ break;
+
+ case CB_SYS_stat :
+ {
+ char *path,*buf;
+ int buflen;
+ struct stat statbuf;
+ TADDR addr = sc->arg2;
+
+ errcode = get_path (cb, sc, sc->arg1, &path);
+ if (errcode != 0)
+ {
+ result = -1;
+ goto FinishSyscall;
+ }
+ result = (*cb->to_stat) (cb, path, &statbuf);
+ free (path);
+ if (result < 0)
+ goto ErrorFinish;
+ buflen = cb_host_to_target_stat (cb, NULL, NULL);
+ buf = xmalloc (buflen);
+ if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
+ {
+ /* The translation failed. This is due to an internal
+ host program error, not the target's fault. */
+ free (buf);
+ errcode = ENOSYS;
+ result = -1;
+ goto FinishSyscall;
+ }
+ if ((*sc->write_mem) (cb, sc, addr, buf, buflen) != buflen)
+ {
+ free (buf);
+ errcode = EINVAL;
+ result = -1;
+ goto FinishSyscall;
+ }
+ free (buf);
+ }
+ break;
+
+ case CB_SYS_fstat :
+ {
+ char *buf;
+ int buflen;
+ struct stat statbuf;
+ TADDR addr = sc->arg2;
+
+ result = (*cb->to_fstat) (cb, sc->arg1, &statbuf);
+ if (result < 0)
+ goto ErrorFinish;
+ buflen = cb_host_to_target_stat (cb, NULL, NULL);
+ buf = xmalloc (buflen);
+ if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
+ {
+ /* The translation failed. This is due to an internal
+ host program error, not the target's fault. */
+ free (buf);
+ errcode = ENOSYS;
+ result = -1;
+ goto FinishSyscall;
+ }
+ if ((*sc->write_mem) (cb, sc, addr, buf, buflen) != buflen)
+ {
+ free (buf);
+ errcode = EINVAL;
+ result = -1;
+ goto FinishSyscall;
+ }
+ free (buf);
+ }
+ break;
+
+ case CB_SYS_lstat :
+ {
+ char *path, *buf;
+ int buflen;
+ struct stat statbuf;
+ TADDR addr = sc->arg2;
+
+ errcode = get_path (cb, sc, sc->arg1, &path);
+ if (errcode != 0)
+ {
+ result = -1;
+ goto FinishSyscall;
+ }
+ result = (*cb->to_lstat) (cb, path, &statbuf);
+ free (path);
+ if (result < 0)
+ goto ErrorFinish;
+
+ buflen = cb_host_to_target_stat (cb, NULL, NULL);
+ buf = xmalloc (buflen);
+ if (cb_host_to_target_stat (cb, &statbuf, buf) != buflen)
+ {
+ /* The translation failed. This is due to an internal
+ host program error, not the target's fault.
+ Unfortunately, it's hard to test this case, so there's no
+ test-case for this execution path. */
+ free (buf);
+ errcode = ENOSYS;
+ result = -1;
+ goto FinishSyscall;
+ }
+
+ if ((*sc->write_mem) (cb, sc, addr, buf, buflen) != buflen)
+ {
+ free (buf);
+ errcode = EINVAL;
+ result = -1;
+ goto FinishSyscall;
+ }
+
+ free (buf);
+ }
+ break;
+
+ case CB_SYS_pipe :
+ {
+ int p[2];
+ char *target_p = xcalloc (1, cb->target_sizeof_int * 2);
+
+ result = (*cb->pipe) (cb, p);
+ if (result != 0)
+ goto ErrorFinish;
+
+ cb_store_target_endian (cb, target_p, cb->target_sizeof_int, p[0]);
+ cb_store_target_endian (cb, target_p + cb->target_sizeof_int,
+ cb->target_sizeof_int, p[1]);
+ if ((*sc->write_mem) (cb, sc, sc->arg1, target_p,
+ cb->target_sizeof_int * 2)
+ != cb->target_sizeof_int * 2)
+ {
+ /* Close the pipe fd:s. */
+ (*cb->close) (cb, p[0]);
+ (*cb->close) (cb, p[1]);
+ errcode = EFAULT;
+ result = -1;
+ }
+
+ free (target_p);
+ }
+ break;
+
+ case CB_SYS_time :
+ {
+ /* FIXME: May wish to change CB_SYS_time to something else.
+ We might also want gettimeofday or times, but if system calls
+ can be built on others, we can keep the number we have to support
+ here down. */
+ time_t t = (*cb->time) (cb, (time_t *) 0);
+ result = t;
+ /* It is up to target code to process the argument to time(). */
+ }
+ break;
+
+ case CB_SYS_chdir :
+ case CB_SYS_chmod :
+ case CB_SYS_utime :
+ /* fall through for now */
+