From 604fd85578b9b7ee9f0d994be4d0e14e03b9ef26 Mon Sep 17 00:00:00 2001 From: crc Date: Tue, 14 May 2019 11:11:15 +0000 Subject: [PATCH] new example: unix-does-user-exist.forth FossilOrigin-Name: ae18d3be928709883496d7a686d2717b0d9d42625fadc543d43509d787883568 --- RELEASE_NOTES.md | 2 ++ example/unix-does-user-exist.forth | 38 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 example/unix-does-user-exist.forth diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 0a2e559..9e64c4c 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -39,6 +39,8 @@ July - September window for this release. - add mandelbrot.forth - add shell.forth - add sqlite3 wrapper +- add unix-does-user-exist.forth +- improved 99bottles.forth - corrected an issue in mail.forth - cleanup publish-examples.forth - publish-examples.forth now uses `retro-document` to generate glossaries diff --git a/example/unix-does-user-exist.forth b/example/unix-does-user-exist.forth new file mode 100644 index 0000000..a64509a --- /dev/null +++ b/example/unix-does-user-exist.forth @@ -0,0 +1,38 @@ +This implements a word to determine if a user exists. This is done by +parsing the results of `finger`. + +~~~ +:pipe> (s-s) file:R unix:popen [ file:read-line ] [ unix:pclose ] bi ; + +{{ + :command 'finger_%s_2>&1 s:format ; + :parse ASCII:SPACE s:tokenize ; + :login? #0 a:th fetch 'Login: s:eq? ; +---reveal--- + :user:exists? (s-f) + &Heap [ command pipe> parse login? ] v:preserve ; +}} + +'crc user:exists? +'fakeuser user:exists? +'root user:exists? +~~~ + +A second way is to parse the `/etc/passwd` file. + +~~~ +{{ + :setup (s-fs) FALSE swap ': s:append s:keep ; + :match? (fss-fsf) over s:begins-with? ; + :found (fs-fs) drop-pair TRUE s:empty s:keep ; + :check (fss-fs) match? [ found ] if ; + :seek (fs-fs) '/etc/passwd [ check ] file:for-each-line ; +---reveal--- + :user:exists? (s-f) + &Heap [ setup seek drop ] v:preserve ; +}} + +'crc user:exists? +'fakeuser user:exists? +'root user:exists? +~~~