Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add d scripts to read uptime and logged in user #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions chp05/proc/d/build
Original file line number Diff line number Diff line change
@@ -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
Binary file added chp05/proc/d/readUptime
Binary file not shown.
23 changes: 23 additions & 0 deletions chp05/proc/d/readUptime.d
Original file line number Diff line number Diff line change
@@ -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;
}
Binary file added chp05/proc/d/whologgedin
Binary file not shown.
8 changes: 8 additions & 0 deletions chp05/proc/d/whologgedin.d
Original file line number Diff line number Diff line change
@@ -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;
}