├── .gitignore ├── getlogin ├── pom.xml └── src │ └── main │ └── java │ └── getlogin │ └── Getlogin.java ├── getpid ├── pom.xml └── src │ └── main │ └── java │ └── getpid │ └── Getpid.java ├── gettimeofday ├── pom.xml └── src │ └── main │ └── java │ └── gettimeofday │ └── Gettimeofday.java ├── pom.xml ├── qsort ├── pom.xml └── src │ └── main │ └── java │ └── qsort │ └── Qsort.java └── uname ├── pom.xml └── src └── main └── java └── uname └── GetUname.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.orig$ 2 | *.rej$ 3 | *.class$ 4 | *~ 5 | .idea 6 | *.iml 7 | target 8 | -------------------------------------------------------------------------------- /getlogin/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | jnr-ffi-example 5 | jnr.ffi.example 6 | 1.0 7 | 8 | 4.0.0 9 | 10 | getlogin 11 | getlogin 12 | jar 13 | 14 | getlogin 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | maven-assembly-plugin 24 | 25 | 26 | jar-with-dependencies 27 | 28 | 29 | 30 | getlogin.Getlogin 31 | 32 | 33 | 34 | 35 | 36 | simple-command 37 | package 38 | 39 | attached 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /getlogin/src/main/java/getlogin/Getlogin.java: -------------------------------------------------------------------------------- 1 | package getlogin; 2 | 3 | import jnr.ffi.LibraryLoader; 4 | 5 | /** 6 | * Gets the login name of the current user 7 | */ 8 | public class Getlogin { 9 | public interface LibC { 10 | public String getlogin(); 11 | } 12 | 13 | public static void main( String[] args ) { 14 | LibC libc = LibraryLoader.create(LibC.class).load("c"); 15 | 16 | System.out.println("login=" + libc.getlogin()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /getpid/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | jnr-ffi-example 5 | jnr.ffi.example 6 | 1.0 7 | 8 | 4.0.0 9 | 10 | getpid 11 | jar 12 | 13 | getpid 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | 23 | org.codehaus.mojo 24 | appassembler-maven-plugin 25 | 1.1.1 26 | 27 | 28 | ${project.build.directory} 29 | 30 | 31 | getpid.Getpid 32 | getpid 33 | 34 | 35 | 36 | 37 | 38 | 39 | package 40 | 41 | assemble 42 | 43 | 44 | 45 | 46 | 47 | maven-assembly-plugin 48 | 49 | 50 | jar-with-dependencies 51 | 52 | 53 | 54 | getpid.Getpid 55 | 56 | 57 | 58 | 59 | 60 | simple-command 61 | package 62 | 63 | attached 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /getpid/src/main/java/getpid/Getpid.java: -------------------------------------------------------------------------------- 1 | package getpid; 2 | 3 | import jnr.ffi.*; 4 | import jnr.ffi.types.pid_t; 5 | 6 | /** 7 | * Gets the process ID of the current process, and that of its parent. 8 | */ 9 | public class Getpid { 10 | public interface LibC { 11 | public @pid_t long getpid(); 12 | public @pid_t long getppid(); 13 | } 14 | 15 | public static void main(String[] args) { 16 | LibC libc = LibraryLoader.create(LibC.class).load("c"); 17 | 18 | System.out.println("pid=" + libc.getpid() + " parent pid=" + libc.getppid()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /gettimeofday/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | jnr-ffi-example 5 | jnr.ffi.example 6 | 1.0 7 | 8 | 4.0.0 9 | 10 | gettimeofday 11 | jar 12 | 13 | gettimeofday 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | 23 | maven-assembly-plugin 24 | 25 | 26 | jar-with-dependencies 27 | 28 | 29 | 30 | gettimeofday.Gettimeofday 31 | 32 | 33 | 34 | 35 | 36 | simple-command 37 | package 38 | 39 | attached 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /gettimeofday/src/main/java/gettimeofday/Gettimeofday.java: -------------------------------------------------------------------------------- 1 | package gettimeofday; 2 | 3 | import jnr.ffi.*; 4 | import jnr.ffi.Runtime; 5 | import jnr.ffi.annotations.Out; 6 | import jnr.ffi.annotations.Transient; 7 | 8 | /** 9 | * Retrieves the current system time using gettimeofday(3) 10 | */ 11 | public class Gettimeofday { 12 | public static final class Timeval extends Struct { 13 | public final time_t tv_sec = new time_t(); 14 | public final Unsigned32 tv_usec = new Unsigned32(); 15 | 16 | public Timeval(Runtime runtime) { 17 | super(runtime); 18 | } 19 | } 20 | 21 | public interface LibC { 22 | public int gettimeofday(@Out @Transient Timeval tv, Pointer unused); 23 | } 24 | 25 | public static void main( String[] args ) { 26 | LibC libc = LibraryLoader.create(LibC.class).load("c"); 27 | Runtime runtime = Runtime.getRuntime(libc); 28 | 29 | Timeval tv = new Timeval(runtime); 30 | libc.gettimeofday(tv, null); 31 | System.out.println("gettimeofday tv.tv_sec=" + tv.tv_sec.get() + " tv.tv_usec=" + tv.tv_usec.get()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | jnr.ffi.example 8 | jnr-ffi-example 9 | pom 10 | 1.0 11 | 12 | qsort 13 | gettimeofday 14 | getpid 15 | getlogin 16 | uname 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.8.2 23 | 24 | 25 | com.github.jnr 26 | jnr-ffi 27 | 2.0.9 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /qsort/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | jnr-ffi-example 5 | jnr.ffi.example 6 | 1.0 7 | 8 | 4.0.0 9 | 10 | qsort 11 | jar 12 | 13 | qsort 14 | http://maven.apache.org 15 | 16 | 17 | 18 | maven-assembly-plugin 19 | 20 | 21 | jar-with-dependencies 22 | 23 | 24 | 25 | qsort.Qsort 26 | 27 | 28 | 29 | 30 | 31 | simple-command 32 | package 33 | 34 | attached 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | UTF-8 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /qsort/src/main/java/qsort/Qsort.java: -------------------------------------------------------------------------------- 1 | package qsort; 2 | 3 | 4 | import jnr.ffi.LibraryLoader; 5 | import jnr.ffi.Memory; 6 | import jnr.ffi.Pointer; 7 | import jnr.ffi.Runtime; 8 | import jnr.ffi.annotations.Delegate; 9 | import jnr.ffi.types.size_t; 10 | 11 | import java.nio.Buffer; 12 | import java.nio.ByteBuffer; 13 | import java.nio.ByteOrder; 14 | import java.nio.IntBuffer; 15 | 16 | /** 17 | * Hello world! 18 | * 19 | */ 20 | public class Qsort { 21 | 22 | public static interface Compare { 23 | @Delegate public int compare(Pointer p1, Pointer p2); 24 | } 25 | 26 | public static final class IntCompare implements Compare { 27 | public int compare(Pointer p1, Pointer p2) { 28 | int i1 = p1.getInt(0); 29 | int i2 = p2.getInt(0); 30 | 31 | return i1 < i2 ? -1 : i1 > i2 ? 1 : 0; 32 | } 33 | } 34 | 35 | public interface LibC { 36 | public int qsort(int[] data, @size_t int count, @size_t int width, Compare compare); 37 | public int qsort(Pointer data, @size_t long count, @size_t int width, Compare compare); 38 | public int qsort(Buffer data, @size_t long count, @size_t int width, Compare compare); 39 | } 40 | 41 | public static void main(String[] args) { 42 | 43 | LibC libc = LibraryLoader.create(LibC.class).load("c"); 44 | 45 | int[] numbers = { 2, 1 }; 46 | System.out.println("qsort using java int[] array"); 47 | System.out.println("before, numbers[0]=" + numbers[0] + " numbers[1]=" + numbers[1]); 48 | libc.qsort(numbers, 2, 4, new IntCompare()); 49 | System.out.println("after, numbers[0]=" + numbers[0] + " numbers[1]=" + numbers[1]); 50 | System.out.append('\n'); 51 | 52 | System.out.println("sort using native memory"); 53 | Pointer memory = Memory.allocate(Runtime.getRuntime(libc), 8); 54 | memory.putInt(0, 4); 55 | memory.putInt(4, 3); // offset is in bytes 56 | System.out.println("before, memory[0]=" + memory.getInt(0) + " memory[1]=" + memory.getInt(4)); 57 | libc.qsort(memory, 2, 4, new IntCompare()); 58 | System.out.println("after, memory[0]=" + memory.getInt(0) + " memory[1]=" + memory.getInt(4)); 59 | 60 | System.out.append('\n'); 61 | System.out.println("qsort using NIO buffer"); 62 | IntBuffer intBuffer = ByteBuffer.allocateDirect(8).order(ByteOrder.nativeOrder()).asIntBuffer(); 63 | intBuffer.put(0, 6); 64 | intBuffer.put(1, 5); // offset is in units of int elements 65 | 66 | System.out.println("before, buffer[0]=" + intBuffer.get(0) + " buffer[1]=" + intBuffer.get(1)); 67 | libc.qsort(intBuffer, 2, 4, new IntCompare()); 68 | System.out.println("after, buffer[0]=" + intBuffer.get(0) + " buffer[1]=" + intBuffer.get(1)); 69 | 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /uname/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | jnr-ffi-example 5 | jnr.ffi.example 6 | 1.0 7 | 8 | 4.0.0 9 | 10 | uname 11 | uname 12 | jar 13 | 14 | uname 15 | http://maven.apache.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | maven-assembly-plugin 24 | 25 | 26 | jar-with-dependencies 27 | 28 | 29 | 30 | uname.GetUname 31 | 32 | 33 | 34 | 35 | 36 | simple-command 37 | package 38 | 39 | attached 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /uname/src/main/java/uname/GetUname.java: -------------------------------------------------------------------------------- 1 | package uname; 2 | 3 | import jnr.ffi.LibraryLoader; 4 | import jnr.ffi.Runtime; 5 | import jnr.ffi.Struct; 6 | import jnr.ffi.annotations.Out; 7 | import jnr.ffi.annotations.Transient; 8 | 9 | public class GetUname { 10 | private static final int _UTS_NAME_LENGTH_ = 65; 11 | 12 | /* 13 | * 14 | * struct utsname { 15 | * char sysname[]; // Operating system name (e.g., "Linux") 16 | * char nodename[]; // Name within "some implementation-defined network" 17 | * char release[]; // Operating system release (e.g., "2.6.28") 18 | * char version[]; // Operating system version 19 | * char machine[]; // Hardware identifier 20 | * #ifdef _GNU_SOURCE 21 | * char domainname[]; // NIS or YP domain name 22 | * #endif 23 | * }; 24 | * 25 | * */ 26 | static final class UtsNameStruct extends Struct { 27 | public final String sysname = new AsciiString(_UTS_NAME_LENGTH_); 28 | public final String nodename = new AsciiString(_UTS_NAME_LENGTH_); 29 | public final String release = new AsciiString(_UTS_NAME_LENGTH_); 30 | public final String version = new AsciiString(_UTS_NAME_LENGTH_); 31 | public final String machine = new AsciiString(_UTS_NAME_LENGTH_); 32 | 33 | public UtsNameStruct(Runtime runtime) { 34 | super(runtime); 35 | } 36 | } 37 | 38 | public interface LibC { 39 | public int uname(@Out @Transient UtsNameStruct nameStruct); 40 | } 41 | 42 | public static void main(String[] args) { 43 | LibC libc = LibraryLoader.create(LibC.class).load("libc.so.6"); 44 | Runtime runtime = Runtime.getRuntime(libc); 45 | UtsNameStruct nameStruct = new UtsNameStruct(runtime); 46 | System.out.println(libc.uname(nameStruct)); 47 | System.out.println(nameStruct.nodename); 48 | System.out.println(nameStruct.sysname); 49 | System.out.println(nameStruct.release); 50 | System.out.println(nameStruct.version); 51 | System.out.println(nameStruct.machine); 52 | } 53 | } 54 | --------------------------------------------------------------------------------