/tmp file duration program questions.

Blair P. Houghton bph at buengc.BU.EDU
Wed Feb 7 05:46:05 AEST 1990


In article <5474.25cddf6b at elroy.uh.edu> cosc6bp at elroy.uh.edu (A JETSON News User) writes:
>
>I am writing a program that will collect data on the number of files in
>the /tmp directory, and their lifetimes.  I am going to do it by the 
>following 'snapshot' method:
>
>  1) execute 'ls /tmp' 
>  2) collect the output from ls either one at a time, or all at once.
...
>My questions are as follows:
>  . What command would I use to execute 'ls /tmp' from C?

You would use system(3); but there's a better way.

Learn how to use opendir(3), readdir(3), closedir(3) and all the
rest of the *dir(3) functions; also learn how to use the info
in the directory entries as arguments to the stat(2) system call.

ls(1) won't give you nearly as much information, and using system(3)
to run ls(1) will slow the program and load the machine,
aggravating the unavoidable skew in the life-expectancy statistics
you're trying to collect.  With *dir(3) and stat(2) you'll compute
like the wind.

>  . What is the most convenient way of routing the output from
>  'ls /tmp' back into the program so that it could be compared to the
>  array or linked list?  I think that a one line at a time feed would 
>  be best.

Using stat(2) (or lstat(2) or fstat(2)) places the info directly
into a struct you provide.

>Thanks for your interest,

Don't thank me, thank Chris Torek for not whining about the
fact that this is actually a Unix question... (though I should
be fair and wait about 24 hours to give him a chance. :-)

				--Blair
				  "Blair(8)."

P.S.  At least under Ultrix, there's a function called getdirentries(3)
that also slogs about in directories, but even the man page says to
use the *dir(3) functions instead...gotta love that DEC...



More information about the Comp.lang.c mailing list