diff --git a/chp05/proc/d/build b/chp05/proc/d/build new file mode 100755 index 0000000..a5e534b --- /dev/null +++ b/chp05/proc/d/build @@ -0,0 +1,4 @@ +#!/bin/bash +echo "Building the D example scripts readuptime and whologgedin" +gdc readUptime.d -o readUptime +gdc whologgedin.d -o whologgedin diff --git a/chp05/proc/d/readUptime b/chp05/proc/d/readUptime new file mode 100755 index 0000000..44d4629 Binary files /dev/null and b/chp05/proc/d/readUptime differ diff --git a/chp05/proc/d/readUptime.d b/chp05/proc/d/readUptime.d new file mode 100644 index 0000000..b24cdee --- /dev/null +++ b/chp05/proc/d/readUptime.d @@ -0,0 +1,23 @@ +import std.file; +import std.stdio; +import std.conv; +import std.string; + +int main(string[] args) +{ + enum int SECONDS_PER_MINUTE = 60; + enum int PERCENT_BASE = 100; + writeln("Starting the read uptime program"); + enum string PROC_UPTIME_PATH = "/proc/uptime"; + auto procUptime = File(PROC_UPTIME_PATH, "r"); + string upAndIdleTime = chop(procUptime.readln()); + auto upAndIdleTimeInArray = upAndIdleTime.split(" "); + string uptimeString = upAndIdleTimeInArray[0]; + string idletimeString = upAndIdleTimeInArray[1]; + float uptime = to!float(uptimeString); + float idletime = to!float(idletimeString); + writefln("The system up time is %f minutes.", uptime/SECONDS_PER_MINUTE, " minutes."); + writefln("It was idle for %f minutes, or %f percent.", idletime/SECONDS_PER_MINUTE, PERCENT_BASE*(idletime/uptime)); + procUptime.close(); + return 0; +} diff --git a/chp05/proc/d/whologgedin b/chp05/proc/d/whologgedin new file mode 100755 index 0000000..b7e6e35 Binary files /dev/null and b/chp05/proc/d/whologgedin differ diff --git a/chp05/proc/d/whologgedin.d b/chp05/proc/d/whologgedin.d new file mode 100644 index 0000000..5628247 --- /dev/null +++ b/chp05/proc/d/whologgedin.d @@ -0,0 +1,8 @@ +import std.stdio; +import core.sys.posix.unistd; + +int main(string[] args) +{ + writefln("The user logged in is %s.", getlogin()); + return 0; +}