dot

packages and services management
Log | Files | Refs | README

commit f725f36fbc294a4fb587cb6fdd13ec9600988ab0
parent 91980316089486af0dde2e44e683c7c9b9f0197b
Author: Josuah Demangeon <mail@josuah.net>
Date:   Wed, 14 Mar 2018 22:55:26 +0100

on a good way for pstree

Diffstat:
Abin/.pstree.swp | 0
Mbin/lstree | 22+++++++++++-----------
Abin/pstree | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 72 insertions(+), 11 deletions(-)

diff --git a/bin/.pstree.swp b/bin/.pstree.swp Binary files differ. diff --git a/bin/lstree b/bin/lstree @@ -17,6 +17,11 @@ # -rwxr-xr-x 1 josuah josuah 468 Feb 17 19:55 ... BEGIN { + LINE = "| "; + NODE = "|- "; + TAIL = "`- "; + VOID = " "; + num = list(entries, ARGC == 1 ? "." : ARGV[1]); tree(entries, num); @@ -44,17 +49,12 @@ BEGIN { function list(entries, path) { - LINE = "| "; - NODE = "|- "; - TAIL = "`- "; - VOID = " "; - - find = "LC_COLLATE='C' cd '" path "' && find . " \ - "-name '*.git' -prune -o " \ - "-name 'CVS' -prune -o " \ - "-exec ls -ld {} +"; + cmd = "cd '" path "' && find ." \ + " -name '*.git' -prune -o" \ + " -name 'CVS' -prune -o" \ + " -exec ls -ld {} +"; - for (num = 0; find | getline; num++) { + for (num = 0; cmd | getline; num++) { sub(" \\.$", "", $0); sub(" -> .*", "", $0); infos = $0; @@ -69,7 +69,7 @@ function list(entries, path) entries[num":"1] = infos " "; entries[num"name"] = count == 1 ? "." : path_v[count]; } - close(find); + close(cmd); return num - 1; } diff --git a/bin/pstree b/bin/pstree @@ -0,0 +1,61 @@ +#!/usr/bin/awk -f + +# pstree implementation in awk + +# find(1) walk the entire tree and then ({} + means call at the end) call +# ls -ld with all the result, which are then sorted. The result will be +# a list with all ls -l information and the the path name from the current +# directory (./). This pattern is used to delimit the find-style output, +# which is transformed into tree(1)-style output: +# +# drwxr-xr-x 2 josuah josuah 512 Feb 16 13:19 |- .ssh +# -rw-r--r-- 1 josuah josuah 870 Feb 9 02:24 | `- config +# drwxr-xr-x 2 josuah josuah 1536 Feb 18 21:24 |- bin +# -rwxr-xr-x 1 josuah josuah 1351 Feb 18 22:30 | |- lt +# -rwxr-xr-x 1 josuah josuah 565 Feb 17 19:53 | |- mfilter +# -rwxr-xr-x 1 josuah josuah 5430 Feb 17 19:51 | `- xdg-open +# -rwxr-xr-x 1 josuah josuah 468 Feb 17 19:55 ... + +BEGIN { + LINE = "| "; + NODE = "|- "; + TAIL = "`- "; + VOID = " "; + + list(entries); + fill(entries, 1, 0); +} + +# Build a relational database in entries from the output of ps. + +function list(entries) +{ + cmd = "ps -axo pid,ppid,comm"; + + cmd | getline; + for (num = 0; cmd | getline; num++) { + pid = $1; + ppid = $2 + sub(" *[0-9]* *[0-9]* ", "", $0); + entries[num] = pid; + entries[pid":args"] = $0; + entries[ppid":cpid"] = entries[ppid":cpid"] "," pid; + } + close(cmd); +} + +# Using the informations from the child pid in entries, build the absolute +# path from PID 1 to every pid. + +function fill(entries, pid, lvl) +{ + for (i = 0; i <= lvl; i++) + entries[pid":"i] = LINE; + while (sub("[^,]*,", "", entries[pid":cpid"])) { + cpid = entries[pid":cpid"]; + sub(",.*", "", cpid); + fill(entries, cpid, lvl + 1); + } +} + +