Previous: , Up: Process Library Samples   [Index]


7.17.3.3 Process Piping

List the (regular) files in the current directory, together with their (in bytes expressed) sizes:

file f;
list l;
xshell xs, xt;

# Setup find(1) to determine the files in the current directory

# set path
b_cast(xs_path(xs), "find");

# relink the _argv_ list for ease of use
l_set(l, xs_argv(xs));

# fill in command
lb_p_text(l, "find");
lb_p_text(l, ".");
lb_p_text(l, "-maxdepth");
lb_p_text(l, "1");
lb_p_text(l, "-type");
lb_p_text(l, "f");

# start process
xs_lead(xs);

# Retrieve the file sizes via wc(1), running through xargs(1) on the output of
# previously setup find(1)

# set path
b_cast(xs_path(xt), "xargs");

# relink the _argv_ list for ease of use
l_set(l, xs_argv(xt));

# filling in command
lb_p_text(l, "xargs");
lb_p_text(l, "wc");
lb_p_text(l, "-c");

# set the output of _find_ as input for _xargs_
xs_fold(xt, xs_output(xs));

# start process
xs_lead(xt);

# relink output file
f_set(f, xs_output(xt));

while (f_list(f, l, 2) ^ -1) {
    # _wc_ will print a `total' line.  Try to remove it.  _find_ will prefix
    # the files names with `./'.  Remove that too.
    if (match("./*", lb_q_text(l))) {
	# the `total' line?
    } else {
	integer x;

	# the _l_ list should describe two strings, the second the file name,
	# the first its size

	o_text(cut(lb_q_text(l), 2, 1048576));
	o_space(42 - length(lb_q_text(l)));
	x = atoi(lf_q_text(l));
	# print size with commas separated runs of three digits,
	# on 16 characters
	o_wbfxinteger(16, 3, 0, 10, x);
	o_byte('\n');
    }
}