1// Copyright 2016 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7#include <errno.h>
8#include <lib/fdio/io.h>
9#include <limits.h>
10#include <stdbool.h>
11#include <sys/types.h>
12#include <threads.h>
13
14#include "private.h"
15
16fdio_t* __fdio_fd_to_io(int fd);
17
18#define fd_to_io(n) __fdio_fd_to_io(n)
19
20zx_status_t __fdio_open_at(fdio_t** io, int dirfd, const char* path, int flags, uint32_t mode);
21zx_status_t __fdio_open(fdio_t** io, const char* path, int flags, uint32_t mode);
22
23int fdio_status_to_errno(zx_status_t status);
24
25// set errno to the closest match for error and return -1
26static inline int ERROR(zx_status_t error) {
27    errno = fdio_status_to_errno(error);
28    return -1;
29}
30
31// if status is negative, set errno as appropriate and return -1
32// otherwise return status
33static inline int STATUS(zx_status_t status) {
34    if (status < 0) {
35        errno = fdio_status_to_errno(status);
36        return -1;
37    } else {
38        return status;
39    }
40}
41
42// set errno to e, return -1
43static inline int ERRNO(int e) {
44    errno = e;
45    return -1;
46}
47