Kotlin-native posix directory iteration


#1

Hi i’m a novice with Kotlin, especially native,

I’m trying to build a serial tty package for native to allow my kotlin apps to talk to arduino boards. Im using Linux64.

I cant get this to compile, it fails on both the readdir line complaining about missing types, and on the println line where it cannot find the .d_name member

My expectation is that it would print out all the filenames of the files in the /dev directory, anybody got any ideas, im a bit stuck on this.

import kotlinx.cinterop.CPointer
import platform.posix.*

fun main(args: Array) {

val devdir = opendir("/dev")

if(devdir != null) {
    do {
        val result? = readdir(devdir)
        if (result != null) {
            println(result.d_name )
        }
    } while( result != null)
}

}


#2

I found my own answer, after hundereds of searchs more i found…


#3

Final working example

import kotlinx.cinterop.*
import platform.posix.*

fun main(args: Array) {

val devdir = opendir("/dev")

if(devdir != null) {

        var result = readdir(devdir)
        while ( result != null) {
            println(result.pointed.d_name.toKString())
            result = readdir(devdir)
        }
 }

}