diff --git a/sys/posix/include/unistd.h b/sys/posix/include/unistd.h new file mode 100644 index 0000000000..a051e8a144 --- /dev/null +++ b/sys/posix/include/unistd.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2013 Freie Universität Berlin + * + * This file subject to the terms and conditions of the GNU Lesser General + * Public License. See the file LICENSE in the top level directory for more + * details. + */ + +/** + * @addtogroup posix + * @{ + */ + +/** + * @file unistd.h + * @brief standard symbolic constants and types + * @see + * The Open Group Base Specifications Issue 7, + * + * + * @author Freie Universität Berlin + * @author Martin Lenders + */ +#ifndef _UNISTD_H +#define _UNISTD_H + +/** + * @brief Close a file descriptor. + * @details shall deallocate the file descriptor indicated by *fildes*. To + * deallocate means to make the file descriptor available for return + * by subsequent calls to open() or other functions that allocate file + * descriptors. All outstanding record locks owned by the process on + * the file associated with the file descriptor shall be removed (that + * is, unlocked). + * + * @see + * The Open Group Base Specification Issue 7, close + * + * + * @param[in] fildes The file descriptor to the file which is to close. + * @return Upon successful completion, 0 shall be returned; otherwise, -1 + * shall be returned and errno set to indicate the error. + */ +int close(int fildes); + +/** + * @} + */ +#endif /* _UNISTD_H */ diff --git a/sys/posix/unistd.c b/sys/posix/unistd.c new file mode 100644 index 0000000000..0030a0d446 --- /dev/null +++ b/sys/posix/unistd.c @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2013 Freie Universität Berlin + * + * This file subject to the terms and conditions of the GNU Lesser General + * Public License. See the file LICENSE in the top level directory for more + * details. + */ +#include + +#include "fd.h" +#include "unistd.h" + +int close(int fildes) +{ + fd_t *fd_obj = fd_get(fildes); + + if (!fd_obj) { + errno = EBADF; + return -1; + } + + if (fd_obj->close(fd_obj->fd) < 0) { + errno = EIO; // EINTR may not occur since RIOT has no signals yet. + return -1; + } + + fd_destroy(fd_obj->fd); + + return 0; +}