How to get arguments of NSRunningApplication? - swift

How do I get the list of arguments using during launch for a NSRunningApplication, similar to the ones I see when I run ps aux:
let workspace = NSWorkspace.shared
let applications = workspace.runningApplications
for application in applications {
// how do I get arguments that were used during application launch?
}

The "ps" tool uses sysctl() with KERN_PROCARGS2 to get the arguments of a running process. The following is an attempt to translate the code from adv_cmds-153/ps/print.c to Swift. That file also contains a documentation of the memory layout of the raw argument space and explains how to locate the string arguments in that memory.
func processArguments(pid: pid_t) -> [String]? {
// Determine space for arguments:
var name : [CInt] = [ CTL_KERN, KERN_PROCARGS2, pid ]
var length: size_t = 0
if sysctl(&name, CUnsignedInt(name.count), nil, &length, nil, 0) == -1 {
return nil
}
// Get raw arguments:
var buffer = [CChar](repeating: 0, count: length)
if sysctl(&name, CUnsignedInt(name.count), &buffer, &length, nil, 0) == -1 {
return nil
}
// There should be at least the space for the argument count:
var argc : CInt = 0
if length < MemoryLayout.size(ofValue: argc) {
return nil
}
var argv: [String] = []
buffer.withUnsafeBufferPointer { bp in
// Get argc:
memcpy(&argc, bp.baseAddress, MemoryLayout.size(ofValue: argc))
var pos = MemoryLayout.size(ofValue: argc)
// Skip the saved exec_path.
while pos < bp.count && bp[pos] != 0 {
pos += 1
}
if pos == bp.count {
return
}
// Skip trailing '\0' characters.
while pos < bp.count && bp[pos] == 0 {
pos += 1
}
if pos == bp.count {
return
}
// Iterate through the '\0'-terminated strings.
for _ in 0..<argc {
let start = bp.baseAddress! + pos
while pos < bp.count && bp[pos] != 0 {
pos += 1
}
if pos == bp.count {
return
}
argv.append(String(cString: start))
pos += 1
}
}
return argv.count == argc ? argv : nil
}
There is only a simple error handling: if anything goes wrong, the function returns nil.
For an instance of NSRunningApplication you can then call
processArguments(pid: application.processIdentifier)

Related

List all processes with arguments in Swift

I'm trying to convert some piece of code to Swift that will list all running processes. But since it requires calling some C specific APIs I'm a bit struggling.
Can someone tell me here what am I doing here incorrectly? print statement at the end is outputting incorrect values. I assume it should be process name. Also line info = malloc(length) gives me the creeps. How should I properly allocate it?
var maxArgumentsSize: Int = 0
if maxArgumentsSize == 0 {
var size: size_t = MemoryLayout<Int>.size
var mib: [Int32] = [CTL_KERN, KERN_ARGMAX]
let a = sysctl(&mib, 2, &maxArgumentsSize, &size, nil, 0)
if a == -1 {
maxArgumentsSize = 4096;
}
}
var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_ALL]
var info: UnsafeMutableRawPointer? = nil
var length: size_t = 0
var count: Int = 0
if sysctl(&mib, 3, nil, &length, nil, 0) < 0 {
exit(EXIT_FAILURE)
}
info = malloc(length)
if sysctl(&mib, 3, info, &length, nil, 0) < 0 {
free(info)
exit(EXIT_FAILURE)
}
count = length / MemoryLayout<kinfo_proc>.size
for index in 0...count {
let info = info?.assumingMemoryBound(to: kinfo_proc.self)
let pid: pid_t = info![index].kp_proc.p_pid
let buffer = UnsafeMutablePointer<CChar>.allocate(capacity: maxArgumentsSize)
var mib: [Int32] = [CTL_KERN, KERN_PROCARGS2, pid]
if sysctl(&mib, 3, buffer, &maxArgumentsSize, nil, 0) == 0 {
let str = String(cString: buffer, encoding: .utf8)
print(str)
}
free(buffer);
}
Basically I've changed my initial code to this and calling #MartinR solution (https://stackoverflow.com/a/72445381/1187415) at the end. Of course it's not complete and pasted from my code directly but it's working.
// Get all processess information:
var name: [CInt] = [CTL_KERN, KERN_PROC, KERN_PROC_ALL]
var length: size_t = 0
if sysctl(&name, CUnsignedInt(name.count), nil, &length, nil, 0) < 0 {
return
}
var infoPtr = UnsafeMutableRawPointer.allocate(
byteCount: length,
alignment: MemoryLayout<kinfo_proc>.alignment
)
if sysctl(&name, CUnsignedInt(name.count), infoPtr, &length, nil, 0) < 0 {
infoPtr.deallocate()
return
}
let count = length / MemoryLayout<kinfo_proc>.size
for index in 0...count {
let info = infoPtr.assumingMemoryBound(to: kinfo_proc.self)
let pid: pid_t = info[index].kp_proc.p_pid
let arguments = self.processArguments(pid: pid)
}
infoPtr.deallocate()

Explanation of this prefix Sum Coding Challenge from Codility GenomicRangeQuery in Swift 4.2

Could someone explain how this prefix sum calculation work as I am confused I mean I understand that it creates an array of Ints with the prefix-sums of the letters but I do not understand how? Could someone perhaps post a more naive logic or some explanation? Or perhaps a shorter version of that MAP function as it is all confusingly complicated.
Link to the challenge just in case someone would like to have a look at it
public func solution(_ S : inout String, _ P : inout [Int], _ Q : inout [Int]) -> [Int] {
//The mapping below needs the explanation
var prefixSumA = S.map({ () -> (Character) -> Int in
var s = 0; return {
if $0 == "A" {
return (s += 1, s).1
}
return s
}
}())//until here
prefixSumA.insert(0, at: 0)
var prefixSumC = S.map({ () -> (Character) -> Int in
var s = 0; return {
if $0 == "C" {
return (s += 1, s).1
}
return s
}
}())
prefixSumC.insert(0, at: 0)
var prefixSumG = S.map({ () -> (Character) -> Int in
var s = 0; return {
if $0 == "G" {
return (s += 1, s).1
}
return s
}
}())
prefixSumG.insert(0, at: 0)
let iterations = min(P.count, Q.count)
var result = [Int]()
for i in 0...iterations - 1 {
let p = P[i]
let q = Q[i] + 1
if prefixSumA[q] - prefixSumA[p] > 0 {
result.append(1)
} else if prefixSumC[q] - prefixSumC[p] > 0 {
result.append(2)
} else if prefixSumG[q] - prefixSumG[p] > 0 {
result.append(3)
} else {
result.append(4)
}
}
return result
}
prefixSumA calculates the number of As from the start to every given index. The same can be said about prefixSumC, and prefixSumG.
For example, if the string S is "CAGCCTA", we'll have:
prefixSumA = [0, 0, 1, 1, 1, 1, 1, 2]
prefixSumC = [0, 1, 1, 1, 2, 3, 3, 3]
prefixSumG = [0, 0, 0, 1, 1, 1, 1, 1]
(Notice the zero inserted at the beginning.)
This block of code:
var prefixSumA = S.map({ () -> (Character) -> Int in
var s = 0; return {
if $0 == "A" {
return (s += 1, s).1
}
return s
}
}())
prefixSumA.insert(0, at: 0)
returns a closure that, if the character is A, would capture the current value of s (which is the last value in the array), increment it, end return it (s).
return (s += 1, s).1 is a fancy way, that could be written as:
s += 1
return s
The whole block can be written more simply:
var prefixSumA = [0]
var countOfA = 0
prefixSumA += S.map { char in
if char == "A" { countOfA += 1 }
return countOfA
}
The same can be done for prefixSumC and prefixSumG.
I tried this one and got 62%. Performance Tests Fails.
public func solution(_ S : inout String, _ P : inout [Int], _ Q : inout [Int]) -> [Int] {
// write your code in Swift 4.2.1 (Linux)
var retArr = [Int]()
var chrArr = [Character]()
for chr in S {
chrArr.append(chr)
}
for i in 0..<P.count {
var minFactor = 4
if P[i] - Q[i] == 0 {
if chrArr[P[i]] == "A"{
minFactor = 1
}else if chrArr[P[i]] == "C"{
minFactor = 2
}else if chrArr[P[i]] == "G"{
minFactor = 3
}
}else {
for j in P[i]...Q[i] {
if chrArr[j] == "A"{
minFactor = 1
break
}else if chrArr[j] == "C"{
minFactor = 2
}else if chrArr[j] == "G"{
if minFactor > 2 {
minFactor = 3
}
}
}
}
retArr.append(minFactor)
}
return retArr
}

Return Repeating character in swift

I am trying to find the first repeating character in swift, and returning the character found. I am getting a bunch of errors with this code, I am not sure why this is not working.
var myArry = "Hello, World"
var counts = [Character]()
func findRepeating(myArry: String) -> Character
{
counts = []
for char in myArry.characters
{
if char in counts
{
print("Character found")
return char
}
else
{
counts.append(char)
}
}
return "A"
}
Most simple answer:
let str = "abcdefghijkhlmnop"
var count = 0
for char in str.characters {
for charNext in str.characters {
if (char == charNext) {
count += 1
if (count > 1) {
return char // h
}
}
}
count = 0
}

Practical number algorithm too slow

I am trying to write a program to find the practical numbers, from an input from 1 to n.
Practical numbers : https://en.wikipedia.org/wiki/Practical_number
My code is running correctly but it is extremely slow - takes over 20 minutes when it should take 10 seconds. This happens when calculating numbers around 50 - it gets stuck at 44.
It is written in Swift
import Foundation
func getInteger() -> Int {
var firstNum:Int = 0
while true {
// get value from user. Using optional input since readLine returns an optional string.
let input = readLine()
// ensure string is not nil
if let unwrappedInput = input {
if let unwrappedInt = Int(unwrappedInput) {
firstNum = unwrappedInt
break
}
else { // the input doesn't convert into an int
print("`\(unwrappedInput)` is not an integer. Please enter an integer")
}
}
else { // did not enter anything
print("Please enter an integer")
}
}
return firstNum
}
func addOne(signArray: [Int]) -> [Int] { // finds the combinations
var signArray2 = [Int]()
for i in 0...signArray.count-1 {
signArray2.append (signArray[i])
}
for i in 0...signArray2.count-1 {
if signArray2[i] == 1 {
signArray2[i] = 0
}
else {
signArray2[i] = 1
break
}
}
return signArray2
}
func signEval (signArray: [Int], divArray: [Int], inNum: Int) -> Bool {// changes 2nd
var counts = 0
for i in 0...divArray.count-1 {
if signArray[i] == 0 {
counts = divArray[i] + counts }
if counts == inNum {
return true
}
}
return false
}
print("Please enter a number to find the summable numbers up to that number:")
var input2 = getInteger()// if num = 1 print 1 if num = 2 print 1 and 2 else print >2 1, 2
var inNum = 0
var numHalf = 0.0
var numRound = 0.0
var numCheck = false
var numCheck2 = false
var numQuarter = 0.0
var numSixth = 0.0
var divArray:[Int] = []
var theirArray = [Int]()
var signArray = [Int]()// array of 0s and 1s
var summableArray:[Int] = [1,2] // need to check if num is bigger than 2!
for input in 1...input2 {
numHalf = Double (input) / 2.0
numRound = round(numHalf)
if numRound == numHalf {
numCheck = true }
if input > 2 && numCheck == false { // odd numbers greater than one are not summable
}
else { // these are possible summable nums
numQuarter = Double (input) / 4.0
numRound = round(numQuarter)
if numRound == numQuarter {
numCheck = true
}
else {
numCheck = false
}
numSixth = Double(input) / 6.0
numRound = round(numSixth)
if numRound == numSixth {
numCheck2 = true }
else { numCheck2 = false}
if numCheck == true || numCheck2 == true {
theirArray = []
divArray = []
signArray = []
summableArray = []
for i in 1...input {
theirArray.append (i)
}
for i in 1...input { // creates an array of all the diviors of inputted number
if input%i == 0 {
divArray.append (i)
}
}
for j in 1...divArray.count {//
signArray.append(0)
}
for i in 1...input{
let x: Int = Int(pow(Double(2),Double(input-1)))// int 2 to the power of input -1
var Boolcheck = false
for q in 1...x-1 { // i to 2^n -1 (sequence to check)
Boolcheck = (signEval(signArray: signArray, divArray: divArray, inNum: i))// checks
signArray = addOne(signArray: signArray)// adding the ones to the array
if Boolcheck == true {
summableArray.append(i)// creates array of mini summable numbers
break
}
}
if summableArray.count == input {
print ("\(input)")
}
}
}
}
}

NilLiteralConvertible in Property setter

In swift I have this:
///3.5.1.8 Range is ± 32,576 FPM, FPM of 32640 means max. Also can be invalid (nil)
var vVelcotiy: Int? {
get {
let ret : Int16 = decode12Bit2sCompliment(bytes[15], bytes[16], useEntireFirstByte: false)
return Int(ret * 64);
}
set {
if (bytes[15] == 8 && bytes[16] == 0) {
return nil
}
if let n = newValue {
let nv = n / 64
bytes[15] = (bytes[15] & 0xF0) | (UInt8(nv) >> 8)
bytes[16] = UInt8(nv)
} else {
bytes[15] = (bytes[15] & 0xF0) | 0xF8
bytes[16] = 0x00
}
}
}
I'm getting an error of type '()' does not conform to protocol 'NilLiteralConvertible' but I've declared my property as optional so I'm confused.
I'm hoping to be able to do:
var a : vVelocity = nil
Reading rintaro's answer and taking my comment into consideration, I think you've misplaced the first check in the setter, it looks like it belongs in the getter instead:
var vVelcotiy: Int? {
get {
if (bytes[15] == 8 && bytes[16] == 0) {
return nil
}
let ret : Int16 = decode12Bit2sCompliment(bytes[15], bytes[16], useEntireFirstByte: false)
return Int(ret * 64);
}
set {
if let n = newValue {
let nv = n / 64
bytes[15] = (bytes[15] & 0xF0) | (UInt8(nv) >> 8)
bytes[16] = UInt8(nv)
} else {
bytes[15] = (bytes[15] & 0xF0) | 0xF8
bytes[16] = 0x00
}
}
}
Now your getter has a possibility of returning nil, and your setter doesn't depend on the existing value.
The error is here:
set {
if (bytes[15] == 8 && bytes[16] == 0) {
return nil // <--- HERE
}
You cannot return anything from set { }. If you want to break, just return instead.
set {
if (bytes[15] == 8 && bytes[16] == 0) {
return
}