[comp.lang.c] /tmp file duration program questions.

cosc6bp@elroy.uh.edu (A JETSON News User) (02/06/90)

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.
  3) compare it to an array or linked list of filenames already 
  existing in /tmp.
  4) Based on 3) add new file names to the list.
  5) Also based on 3) write to a file the duration a file lasted that no 
  longer exists in the /tmp directory and delete the name from the array
  or linked list. 

It would do the above in a forever loop until we decide to kill it.

My questions are as follows:

  . What command would I use to execute 'ls /tmp' from C?
  . 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.

Thanks for your interest,

Ignacio Valdes - U of H - Home for the moment, of Andre Ware  

bph@buengc.BU.EDU (Blair P. Houghton) (02/07/90)

In article <5474.25cddf6b@elroy.uh.edu> cosc6bp@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...

ted@welch.jhu.edu (Ted Ying) (02/07/90)

In article <5368@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes:
>In article <5474.25cddf6b@elroy.uh.edu> cosc6bp@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.
>
	Well, I would suggest using vfork(2) and execle(3) for the "ls"
	command.  It is a better alternative than using the system(3)
	command as it uses less overhead.  Especially since you intend
	to run this endlessly in the background.  You really don't want
	the overhead of a system(3) process going continuously.

	Ted Ying			ted@welch.jhu.edu

#include <std.disclaimer.h>
Velilind's Laws of Experimentation:
	1. If reproducibility may be a problem, conduct the test only once.
	2. If a straight line fit is required, obtain only two data points.

bill@twwells.com (T. William Wells) (02/08/90)

In article <5474.25cddf6b@elroy.uh.edu> cosc6bp@elroy.uh.edu (A JETSON News User) writes:
:   . What command would I use to execute 'ls /tmp' from C?
:   . 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.

Use popen(3).

---
Bill                    { uunet | novavax | ankh } !twwells!bill
bill@twwells.com