digitalmars.D.learn - How to list all process directories under /proc/
- Ky-Anh Huynh (27/27) Sep 17 2017 Hi,
- Ky-Anh Huynh (9/36) Sep 17 2017 My bad. Range doesn't support. The correct pattern is
- Ky-Anh Huynh (6/14) Sep 17 2017 The official documentation here
- Matt Jones (26/31) Sep 18 2017 The problem with matching "[0123456789]*" is that it will match
- Ky-Anh Huynh (4/12) Sep 19 2017 I understand. Thanks a lot.
- Matt Jones (3/5) Sep 19 2017 That is strange. But then again, every glob library I've seen
- Ky-Anh Huynh (7/14) Sep 19 2017 I see. Maybe I'm using Linux and Ruby too much. Missing character
- angel (4/31) Sep 22 2017 Are you familiar with libprocps ?
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
Sep 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 lotMy 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?
Sep 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.
Sep 17 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; }
Sep 18 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: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?[...]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: [...]
Sep 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.
Sep 19 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: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.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.
Sep 19 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 lotAre you familiar with libprocps ? Maybe you had better make use of this library, or, at least, peek into its code, for reference.
Sep 22 2017