Thread overview
How to list all process directories under /proc/
Sep 17, 2017
Ky-Anh Huynh
Sep 17, 2017
Ky-Anh Huynh
Sep 17, 2017
Ky-Anh Huynh
Sep 19, 2017
Matt Jones
Sep 19, 2017
Ky-Anh Huynh
Sep 19, 2017
Matt Jones
Sep 20, 2017
Ky-Anh Huynh
Sep 22, 2017
angel
September 17, 2017
Hi,

I want to list all processes by scanning /proc/. The following code doesn't work

[code]
  foreach (string fstatm; dirEntries("/proc/", "[0-9]*", SpanMode.shallow)) {
    writefln("pid %s", fstatm);
  }
[/code]

as it only list a few entries before exiting

[code]
pid /proc/9
pid /proc/935
pid /proc/9146
pid /proc/9149
pid /proc/9150
pid /proc/9151
pid /proc/9756
pid /proc/9759
pid /proc/9760
pid /proc/9761
[/code]

I don't want to use `SpanMode.depth` or `SpanMode.breadth` because it will scan so deeply and there would be a permission problem.

Any ideas?

Thanks a lot

September 17, 2017
On Sunday, 17 September 2017 at 08:15:58 UTC, Ky-Anh Huynh wrote:
> Hi,
>
> I want to list all processes by scanning /proc/. The following code doesn't work
>
> [code]
>   foreach (string fstatm; dirEntries("/proc/", "[0-9]*", SpanMode.shallow)) {
>     writefln("pid %s", fstatm);
>   }
> [/code]
>
> as it only list a few entries before exiting
>
> [code]
> pid /proc/9
> pid /proc/935
> pid /proc/9146
> pid /proc/9149
> pid /proc/9150
> pid /proc/9151
> pid /proc/9756
> pid /proc/9759
> pid /proc/9760
> pid /proc/9761
> [/code]
>
> I don't want to use `SpanMode.depth` or `SpanMode.breadth` because it will scan so deeply and there would be a permission problem.
>
> Any ideas?
>
> Thanks a lot

My bad. Range doesn't support. The correct pattern is

[code]
  foreach (string fstatm; dirEntries("/proc/", "[0123456789]*", SpanMode.shallow)) {
    writefln("pid %s", fstatm);
  }
[/code]

Is there a way to make this simpler?



September 17, 2017
On Sunday, 17 September 2017 at 08:32:24 UTC, Ky-Anh Huynh wrote:
>
> My bad. Range doesn't support. The correct pattern is
>
> [code]
>   foreach (string fstatm; dirEntries("/proc/", "[0123456789]*", SpanMode.shallow)) {
>     writefln("pid %s", fstatm);
>   }
> [/code]
>
> Is there a way to make this simpler?

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.
September 19, 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;
}
September 19, 2017
On Tuesday, 19 September 2017 at 06:35:18 UTC, Matt Jones wrote:
> On Sunday, 17 September 2017 at 08:37:33 UTC, Ky-Anh Huynh wrote:
>
>> [...]
>
> 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:
>
> [...]


I understand. Thanks a lot.

Btw, is that a bit weird that range is not supported in glob pattern :) Is there a design reason for this?
September 19, 2017
On Tuesday, 19 September 2017 at 13:32:29 UTC, Ky-Anh Huynh wrote:

>
> Btw, is that a bit weird that range is not supported in glob pattern :) Is there a design reason for this?

That is strange. But then again, every glob library I've seen works a little bit differently.
September 20, 2017
On Tuesday, 19 September 2017 at 18:32:06 UTC, Matt Jones wrote:
> On Tuesday, 19 September 2017 at 13:32:29 UTC, Ky-Anh Huynh wrote:
>
>>
>> Btw, is that a bit weird that range is not supported in glob pattern :) Is there a design reason for this?
>
> That is strange. But then again, every glob library I've seen works a little bit differently.

I see. Maybe I'm using Linux and Ruby too much. Missing character range doesn't really hurt, but the feature should be documented. Should we improve the documentation? To be fair, the syntax specification is clear after I read it twice, but the link to Wiki page is confusing, because Wiki page mentions the popular (FIXME) implementation with character range.
September 22, 2017
On Sunday, 17 September 2017 at 08:15:58 UTC, Ky-Anh Huynh wrote:
> Hi,
>
> I want to list all processes by scanning /proc/. The following code doesn't work
>
> [code]
>   foreach (string fstatm; dirEntries("/proc/", "[0-9]*", SpanMode.shallow)) {
>     writefln("pid %s", fstatm);
>   }
> [/code]
>
> as it only list a few entries before exiting
>
> [code]
> pid /proc/9
> pid /proc/935
> pid /proc/9146
> pid /proc/9149
> pid /proc/9150
> pid /proc/9151
> pid /proc/9756
> pid /proc/9759
> pid /proc/9760
> pid /proc/9761
> [/code]
>
> I don't want to use `SpanMode.depth` or `SpanMode.breadth` because it will scan so deeply and there would be a permission problem.
>
> Any ideas?
>
> Thanks a lot


Are you familiar with libprocps ?
Maybe you had better make use of this library, or, at least, peek into its code, for reference.