plan9port

[fork] Plan 9 from user space
git clone git://src.adamsgaard.dk/plan9port # fast
git clone https://src.adamsgaard.dk/plan9port.git # slow
Log | Files | Refs | README | LICENSE Back to index

ps (2022B)


      1 #!/bin/sh
      2 
      3 I_WANT_A_BROKEN_PS=yes
      4 export I_WANT_A_BROKEN_PS
      5 all=no
      6 if [ "x$1" = "x-a" ]
      7 then
      8 	all=yes
      9 fi
     10 export all
     11 
     12 cat >/tmp/awk.xxx$$ <<'!'
     13 BEGIN{
     14 	state["D"] = "Spinwait";
     15 	state["I"] = "Idle";
     16 	state["J"] = "Jail";
     17 	state["R"] = "Ready";
     18 	state["O"] = "Running";
     19 	state["S"] = "Sleep";
     20 	state["T"] = "Stopped";
     21 	state["Z"] = "Zombie";
     22 	state["W"] = "Fault";
     23 	state["X"] = "Moribund";
     24 }
     25 
     26 function statestr(s, wchan)
     27 {
     28 	if(wchan != s && wchan != "?" && wchan != "-")
     29 		return wchan;
     30 	t = state[substr(s, 1, 1)];
     31 	if(t == "")
     32 		return s;
     33 	return t;
     34 }
     35 
     36 # rsc  36706   starttime 0:00.17  1076 Is+  -bash (bash)
     37 {
     38 	i=1
     39 	user=$i; i++
     40 	pid=$i; i++
     41 	start=$i; i++
     42 	if(start ~ /^[A-Z][a-z][a-z]$/){
     43 		start = start "-" $i; i++
     44 	}
     45 	cputime=$i; i++
     46 	mem=$i; i++
     47 	stat=$i; i++
     48 	wchan=$i; i++
     49 	cmd=$i; i++	
     50 	if(ENVIRON["all"] == "yes"){
     51 		for(; i<=NF; i++)
     52 			cmd = cmd " " $i;
     53 	}else{
     54 		sub(/.*\//, "", cmd);
     55 		sub(/:$/, "", cmd);
     56 		sub(/^-/, "", cmd);
     57 		s = " " cmd;
     58 	}
     59 	sub(/\.[0-9][0-9]$/, "", cputime);	# drop .hundredths of second
     60 	if(cputime ~ /..:..:../){	# convert hh:mm:ss into mm:ss
     61 		split(cputime, a, ":");
     62 		cputime = sprintf("%d:%02d", a[1]*60+a[2], a[3]);
     63 	}
     64 	if(start ~ /..:..:../){	# drop :ss
     65 		sub(/:..$/, "", start);
     66 	}
     67 	sub(/[ 	]+$/, "", cmd);
     68 	line[0+nline++] = sprintf("%s\001%d\001%s\001%s\001%dK\001%s\001%s",
     69 		user, pid, start, cputime, mem, statestr(stat, wchan), cmd);
     70 }
     71 
     72 END{
     73 	for(i=0; i<nline; i++){
     74 		split(line[i], a, "\001");
     75 		for(j=1; j<=7; j++)
     76 			if(length(a[j]) > max[j])
     77 				max[j] = length(a[j]);
     78 	}
     79 	for(i=0; i<nline; i++){
     80 		split(line[i], a, "\001");
     81 		printf("%-*s %*s %*s %*s %*s %-*s %s\n",
     82 			max[1], a[1], max[2], a[2], max[3], a[3], max[4], a[4],
     83 			max[5], a[5], max[6], a[6], a[7]);
     84 	}
     85 }
     86 !
     87 
     88 case "${SYSNAME:-`uname`}" in 
     89 SunOS)
     90 	/bin/ps -aA -o 'user,pid,stime,time,rss,s,s,args' | sed 1d |
     91 		nawk -f /tmp/awk.xxx$$ | sort -n +1
     92 	;;
     93 *)
     94 	/bin/ps -axww -o 'user,pid,start,time,rss,stat,wchan,command' | sed 1d |
     95 		awk -f /tmp/awk.xxx$$ | sort -n +1
     96 	;;
     97 esac
     98 
     99 rm -f /tmp/awk.xxx$$
    100