Ejemplo n.º 1
0
int dir_setcwd(struct ByteArray *newpath) {
  word_t pathLen = byte_array_size(newpath);
  char *path = (char*)malloc(pathLen + 1);
#ifdef WIN32
  if (path == NULL)
	return GetLastError();
  pathLen = extractCString(newpath, path, sizeof(path));

  if (pathLen < 0)
    return SLATE_ERROR_RETURN;

  return SetCurrentDirectory (path);
#else
  if (path == NULL)
    return -errno;
  memcpy(path, newpath->elements, pathLen);
  path[pathLen] = '\0';
  if (chdir(path) == -1) {
    int savedErrno = errno;
    free(path);
    return -savedErrno;
  } else {
    free(path);
    return 0;
  }
#endif
}
Ejemplo n.º 2
0
word_t file_open(struct object_heap* oh, struct ByteArray * name, word_t flags) {
  byte_t nameString[SLATE_FILE_NAME_LENGTH];
  word_t nameLength;
#ifdef WIN32
  DWORD rwMode = 0;
  DWORD openMode = 0;
#else
  char mode[8];
  word_t modeIndex = 0;
#endif
  word_t file;

  nameLength = extractCString(name, nameString, sizeof(nameString));

  if (nameLength <= 0)
    return SLATE_ERROR_RETURN;

  file = file_allocate(oh);
  if (file < 0)
    return SLATE_ERROR_RETURN;

  /* (CLEAR \/ CREATE) /\ !WRITE */
  if ((flags & SF_CLEAR || flags & SF_CREATE) && (! (flags & SF_WRITE)))
    error("ANSI does not support clearing or creating files that are not opened for writing");

  /* WRITE /\ !READ */
  if (flags & SF_WRITE && (! (flags & SF_READ)))
    error("ANSI does not support opening files for writing only");

  if (flags & SF_WRITE) {
    if (flags & SF_CLEAR) {
#ifdef WIN32
      rwMode |= GENERIC_WRITE;
#else
      mode[modeIndex++] = 'w';
#endif
      if (flags & SF_READ)
#ifdef WIN32
        rwMode |= GENERIC_READ;
#else
        mode[modeIndex++] = '+';
#endif
    } else {
#ifdef WIN32
      rwMode |= GENERIC_READ;
#else
      mode[modeIndex++] = 'r';
      mode[modeIndex++] = '+';
#endif
    }
  } else if (flags & SF_READ)
#ifdef WIN32
    rwMode |= GENERIC_READ;
#else
    mode[modeIndex++] = 'r';
#endif
  else {