Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Making Linux system calls


Linux system calls are typically performed via a C interface. D can call these just like any other C library functions (see the Using external libraries recipe in Chapter 1, Core Tasks). Here, we'll write a "Hello World" program using the standard C function for the system call, and say "hello" using inline assembly to demonstrate how that can be done as well.

How to do it…

Making Linux system calls with inline assembly is quite different than making them with the C interface. First, we will look at how it is done with the function, then we'll translate it to the assembly.

With the C interface

Let's make Linux system calls by executing the following steps:

  1. Import the appropriate header from the core.sys.posix package, or if it is not present, write the prototype with extern(C).

  2. Call the function like you would call any other function.

The code is as follows:

import core.sys.posix.unistd; // analogous to #include <unistd.h>
string hello = "Hello, world!";
write(1 ...