How to list all process directories under /proc/

Matt Jones matthew.brennan.jones at gmail.com
Tue Sep 19 06:35:18 UTC 2017


On Sunday, 17 September 2017 at 08:37:33 UTC, Ky-Anh Huynh wrote:

> The official documentation here 
> https://dlang.org/phobos/std_path.html#.globMatch refers to the 
> wiki page https://en.wikipedia.org/wiki/Glob_%28programming%29 
> . However I think the popular glob rules (man 7 glob) are not 
> supported.

The problem with matching "[0123456789]*" is that it will match 
files like "1blah" and "8stuff". It looks like glob patterns are 
not robust enough to handle match patterns you want. A regex 
would probably be enough. Something like this works:

string[] getProcNumbers() {
	import std.file : dirEntries, SpanMode;
	import std.path : baseName;
	import std.regex : regex, match;
	import std.algorithm : map, filter;
	import std.array : array;

	auto r = regex(`^/proc/[0-9]*$`);

	string[] entries =
		dirEntries("/proc/", SpanMode.shallow)
		.map!(n => n.name)
		.filter!(n => match(n, r))
		.array();

	return entries;
}

int main() {
	import std.stdio : stdout;

	foreach (entry ; getProcNumbers()) {
		stdout.writefln("%s", entry);
	}

	return 0;
}


More information about the Digitalmars-d-learn mailing list