Skip to content

Implementation of fcntl() to both Darwin and Glibc overlays. Ported … #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/Glibc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(sources
Glibc.swift
Misc.c
)

set(output_dir "${SWIFTLIB_DIR}/glibc")
Expand Down
137 changes: 137 additions & 0 deletions stdlib/public/Glibc/Glibc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,100 @@ public var errno: Int32 {
// fcntl.h
//===----------------------------------------------------------------------===//

@warn_unused_result
@_silgen_name("_swift_Glibc_open")
func _swift_Glibc_open(path: UnsafePointer<CChar>,
_ oflag: CInt,
_ mode: mode_t
) -> CInt

@warn_unused_result
@_silgen_name("_swift_Glibc_openat")
func _swift_Glibc_openat(
fd: CInt,
_ path: UnsafePointer<CChar>,
_ oflag: CInt,
_ mode: mode_t
) -> CInt

@warn_unused_result
public func open(
path: UnsafePointer<CChar>,
_ oflag: CInt
) -> CInt {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering: why not use a default value of 0 for the mode: mode_t parameter instead of duplicating those functions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that's how the man page describes the function: there are three variants, one accepts void, another accepts a pointer, and another accepts int. It does not describe the void one as a variant of the int one.

return _swift_Glibc_open(path, oflag, 0)
}

@warn_unused_result
public func open(
path: UnsafePointer<CChar>,
_ oflag: CInt,
_ mode: mode_t
) -> CInt {
return _swift_Glibc_open(path, oflag, mode)
}

@warn_unused_result
public func openat(
fd: CInt,
_ path: UnsafePointer<CChar>,
_ oflag: CInt
) -> CInt {
return _swift_Glibc_openat(fd, path, oflag, 0)
}

@warn_unused_result
public func openat(
fd: CInt,
_ path: UnsafePointer<CChar>,
_ oflag: CInt,
_ mode: mode_t
) -> CInt {
return _swift_Glibc_openat(fd, path, oflag, mode)
}

@warn_unused_result
@_silgen_name("_swift_Glibc_fcntl")
internal func _swift_Glibc_fcntl(
fd: CInt,
_ cmd: CInt,
_ value: CInt
) -> CInt

@warn_unused_result
@_silgen_name("_swift_Glibc_fcntlPtr")
internal func _swift_Glibc_fcntlPtr(
fd: CInt,
_ cmd: CInt,
_ ptr: UnsafeMutablePointer<Void>
) -> CInt

@warn_unused_result
public func fcntl(
fd: CInt,
_ cmd: CInt
) -> CInt {
return _swift_Glibc_fcntl(fd, cmd, 0)
}

@warn_unused_result
public func fcntl(
fd: CInt,
_ cmd: CInt,
_ value: CInt
) -> CInt {
return _swift_Glibc_fcntl(fd, cmd, value)
}

@warn_unused_result
public func fcntl(
fd: CInt,
_ cmd: CInt,
_ ptr: UnsafeMutablePointer<Void>
) -> CInt {
return _swift_Glibc_fcntlPtr(fd, cmd, ptr)
}

public var S_IFMT: mode_t { return mode_t(0o170000) }
public var S_IFIFO: mode_t { return mode_t(0o010000) }
public var S_IFCHR: mode_t { return mode_t(0o020000) }
Expand Down Expand Up @@ -74,3 +168,46 @@ public var SIG_HOLD: __sighandler_t {
}
#endif

//===----------------------------------------------------------------------===//
// semaphore.h
//===----------------------------------------------------------------------===//

/// The value returned by `sem_open()` in the case of failure.
public var SEM_FAILED: UnsafeMutablePointer<sem_t> {
// The value is ABI. Value verified to be correct on Glibc.
return UnsafeMutablePointer<sem_t>(bitPattern: 0)
}

@warn_unused_result
@_silgen_name("_swift_Glibc_sem_open2")
internal func _swift_Glibc_sem_open2(
name: UnsafePointer<CChar>,
_ oflag: CInt
) -> UnsafeMutablePointer<sem_t>

@warn_unused_result
@_silgen_name("_swift_Glibc_sem_open4")
internal func _swift_Glibc_sem_open4(
name: UnsafePointer<CChar>,
_ oflag: CInt,
_ mode: mode_t,
_ value: CUnsignedInt
) -> UnsafeMutablePointer<sem_t>

@warn_unused_result
public func sem_open(
name: UnsafePointer<CChar>,
_ oflag: CInt
) -> UnsafeMutablePointer<sem_t> {
return _swift_Glibc_sem_open2(name, oflag)
}

@warn_unused_result
public func sem_open(
name: UnsafePointer<CChar>,
_ oflag: CInt,
_ mode: mode_t,
_ value: CUnsignedInt
) -> UnsafeMutablePointer<sem_t> {
return _swift_Glibc_sem_open4(name, oflag, mode, value)
}
46 changes: 46 additions & 0 deletions stdlib/public/Glibc/Misc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===--- Misc.c - Glibc overlay helpers -----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include <fcntl.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The man page says we also need

       #include <sys/types.h>
       #include <sys/stat.h>

for portability.

#include <sys/types.h>
#include <sys/stat.h>
#include <semaphore.h>

extern int
_swift_Glibc_open(const char *path, int oflag, mode_t mode) {
return open(path, oflag, mode);
}

extern int
_swift_Glibc_openat(int fd, const char *path, int oflag, mode_t mode) {
return openat(fd, path, oflag, mode);
}

extern sem_t *_swift_Glibc_sem_open2(const char *name, int oflag) {
return sem_open(name, oflag);
}

extern sem_t *_swift_Glibc_sem_open4(const char *name, int oflag,
mode_t mode, unsigned int value) {
return sem_open(name, oflag, mode, value);
}

extern int
_swift_Glibc_fcntl(int fd, int cmd, int value) {
return fcntl(fd, cmd, value);
}

extern int
_swift_Glibc_fcntlPtr(int fd, int cmd, void* ptr) {
return fcntl(fd, cmd, ptr);
}

86 changes: 74 additions & 12 deletions stdlib/public/SDK/Darwin/Darwin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ func _convertDarwinBooleanToBool(x: DarwinBoolean) -> Bool {
@_transparent
@warn_unused_result
public func && <T : BooleanType>(
lhs: T, @autoclosure rhs: () -> DarwinBoolean
lhs: T,
@autoclosure rhs: () -> DarwinBoolean
) -> Bool {
return lhs.boolValue ? rhs().boolValue : false
}

@_transparent
@warn_unused_result
public func || <T : BooleanType>(
lhs: T, @autoclosure rhs: () -> DarwinBoolean
lhs: T,
@autoclosure rhs: () -> DarwinBoolean
) -> Bool {
return lhs.boolValue ? true : rhs().boolValue
}
Expand Down Expand Up @@ -145,38 +147,98 @@ public var stderr : UnsafeMutablePointer<FILE> {

@warn_unused_result
@_silgen_name("_swift_Darwin_open")
func _swift_Darwin_open(path: UnsafePointer<CChar>,
_ oflag: CInt, _ mode: mode_t) -> CInt
func _swift_Darwin_open(
path: UnsafePointer<CChar>,
_ oflag: CInt,
_ mode: mode_t
) -> CInt

@warn_unused_result
@_silgen_name("_swift_Darwin_openat")
func _swift_Darwin_openat(fd: CInt,
_ path: UnsafePointer<CChar>,
_ oflag: CInt, _ mode: mode_t) -> CInt
_ oflag: CInt,
_ mode: mode_t
) -> CInt

@warn_unused_result
public func open(path: UnsafePointer<CChar>, _ oflag: CInt) -> CInt {
public func open(
path: UnsafePointer<CChar>,
_ oflag: CInt
) -> CInt {
return _swift_Darwin_open(path, oflag, 0)
}

@warn_unused_result
public func open(path: UnsafePointer<CChar>, _ oflag: CInt,
_ mode: mode_t) -> CInt {
public func open(
path: UnsafePointer<CChar>,
_ oflag: CInt,
_ mode: mode_t
) -> CInt {
return _swift_Darwin_open(path, oflag, mode)
}

@warn_unused_result
public func openat(fd: CInt, _ path: UnsafePointer<CChar>,
_ oflag: CInt) -> CInt {
public func openat(
fd: CInt,
_ path: UnsafePointer<CChar>,
_ oflag: CInt
) -> CInt {
return _swift_Darwin_openat(fd, path, oflag, 0)
}

@warn_unused_result
public func openat(fd: CInt, _ path: UnsafePointer<CChar>,
_ oflag: CInt, _ mode: mode_t) -> CInt {
public func openat(
fd: CInt,
_ path: UnsafePointer<CChar>,
_ oflag: CInt,
_ mode: mode_t
) -> CInt {
return _swift_Darwin_openat(fd, path, oflag, mode)
}

@warn_unused_result
@_silgen_name("_swift_Darwin_fcntl")
internal func _swift_Darwin_fcntl(
fd: CInt,
_ cmd: CInt,
_ value: CInt
) -> CInt

@warn_unused_result
@_silgen_name("_swift_Darwin_fcntlPtr")
internal func _swift_Darwin_fcntlPtr(
fd: CInt,
_ cmd: CInt,
_ ptr: UnsafeMutablePointer<Void>
) -> CInt

@warn_unused_result
public func fcntl(
fd: CInt,
_ cmd: CInt
) -> CInt {
return _swift_Darwin_fcntl(fd, cmd, 0)
}

@warn_unused_result
public func fcntl(
fd: CInt,
_ cmd: CInt,
_ value: CInt
) -> CInt {
return _swift_Darwin_fcntl(fd, cmd, value)
}

@warn_unused_result
public func fcntl(
fd: CInt,
_ cmd: CInt,
_ ptr: UnsafeMutablePointer<Void>
) -> CInt {
return _swift_Darwin_fcntlPtr(fd, cmd, ptr)
}

public var S_IFMT: mode_t { return mode_t(0o170000) }
public var S_IFIFO: mode_t { return mode_t(0o010000) }
public var S_IFCHR: mode_t { return mode_t(0o020000) }
Expand Down
10 changes: 10 additions & 0 deletions stdlib/public/SDK/Darwin/Misc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@
return sem_open(name, oflag, mode, value);
}

extern "C" int
_swift_Darwin_fcntl(int fd, int cmd, int value) {
return fcntl(fd, cmd, value);
}

extern "C" int
_swift_Darwin_fcntlPtr(int fd, int cmd, void* ptr) {
return fcntl(fd, cmd, ptr);
}

Loading