Skip to content

Commit 1ab6d3d

Browse files
committed
Fix compilation on Linux
1 parent 9e73460 commit 1ab6d3d

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

Sources/SignalHandling/CStructsInSwift/Signal.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ public struct Signal : RawRepresentable, Hashable, Codable, CaseIterable, Custom
260260
}
261261

262262
/** Will return `usr1` or similar for `.userDefinedSignal1` for instance. */
263-
#if !os(Linux)
264263
public var signalName: String? {
264+
#if !os(Linux)
265265
guard rawValue >= 0 && rawValue < NSIG else {
266266
return nil
267267
}
@@ -271,12 +271,24 @@ public struct Signal : RawRepresentable, Hashable, Codable, CaseIterable, Custom
271271
return siglistPtrAsPointerToCStrings.advanced(by: Int(rawValue)).pointee.flatMap{ String(cString: $0) }
272272
})
273273
})
274-
}
274+
#else
275+
/* This is not available when `_GNU_SOURCE` is not defined. */
276+
// guard let cstr = strsignal(rawValue) else {
277+
// return nil
278+
// }
279+
// return String(cString: cstr)
280+
return nil
275281
#endif
282+
}
276283

277284
/**
278-
Return a user readable description of the signal (always in English I think). */
285+
Return a user readable description of the signal.
286+
Might be translated depending on locale (`LC_MESSAGES`) on Linux.
287+
288+
- Note: I’d prefer the description to always be in English but the `_np` variant of the strsignal method is not available by default.
289+
IIUC I’d have to define `_GNU_SOURCE` to be able to get it. */
279290
public var signalDescription: String? {
291+
#if !os(Linux)
280292
guard rawValue >= 0 && rawValue < NSIG else {
281293
return nil
282294
}
@@ -286,14 +298,16 @@ public struct Signal : RawRepresentable, Hashable, Codable, CaseIterable, Custom
286298
return siglistPtrAsPointerToCStrings.advanced(by: Int(rawValue)).pointee.flatMap{ String(cString: $0) }
287299
})
288300
})
301+
#else
302+
guard let cstr = strsignal(rawValue) else {
303+
return nil
304+
}
305+
return String(cString: cstr)
306+
#endif
289307
}
290308

291309
public var description: String {
292-
#if !os(Linux)
293-
return "SIG\((signalName ?? "\(rawValue)").uppercased())"
294-
#else
295-
return "\(signalDescription ?? "\(rawValue)")"
296-
#endif
310+
return "SIG\((signalName ?? "\(rawValue)").uppercased()) - \(signalDescription ?? "Unknown")"
297311
}
298312

299313
}

0 commit comments

Comments
 (0)