1// Copyright 2023 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5//go:build wasip1
6
7package os
8
9import (
10	"internal/poll"
11	"syscall"
12)
13
14func open(filePath string, flag int, perm uint32) (int, poll.SysFile, error) {
15	if filePath == "" {
16		return -1, poll.SysFile{}, syscall.EINVAL
17	}
18	absPath := filePath
19	// os.(*File).Chdir is emulated by setting the working directory to the
20	// absolute path that this file was opened at, which is why we have to
21	// resolve and capture it here.
22	if filePath[0] != '/' {
23		wd, err := syscall.Getwd()
24		if err != nil {
25			return -1, poll.SysFile{}, err
26		}
27		absPath = joinPath(wd, filePath)
28	}
29	fd, err := syscall.Open(absPath, flag, perm)
30	return fd, poll.SysFile{Path: absPath}, err
31}
32