5.7 Exercise: News biff

Team-FLY

5.7 Exercise: News biff

The simplebiff program informs the user of incoming mail. A user might also want to be informed of changes in other files such as the Internet News files. If a system is a news server, it probably organizes articles as individual files whose pathname contains the newsgroup name .

Example 5.36

A system keeps its news files in the directory /var/spool/news . Article 1034 in newsgroup comp.os.unix is located in the following file.

 /var/spool/news/comp/os/unix/1034 

The following exercises develop a facility for biffing when any file in a list of files changes.

  1. Write a function called lastmod that returns the time at which a file was last modified. The prototype for lastmod is as follows .

     time_t lastmod(char *pathname); 

    Use stat to determine the last modification time. The time_t is time in seconds since 00:00:00 UTC, January 1, 1970. The lastmod function returns “1 if there is an error and sets errno to the error number set by stat .

  2. Write a main program that takes a pathname as a command-line argument and calls lastmod to determine the time of last modification of the corresponding file. Use ctime to print out the time_t value in a readable form. Compare the results with those obtained from ls -l .

  3. Write a function called convertnews that converts a newsgroup name to a fully qualified pathname. The prototype of convertnews is as follows.

     char *convertnews(char *newsgroup); 

    If the environment variable NEWSDIR is defined, use it to determinethe path . Otherwise, use /var/spool/news . (Call getenv to determine whether the environment variable is defined.) For example, if the newsgroup is comp.os.unix and NEWSDIR is not defined, the pathname is the following.

     /var/spool/news/comp/os/unix 

    The convertnews function allocates space to hold the converted string and returns a pointer to that space. (A common error is to return a pointer to an automatic variable defined within convertnews .) Do not modify newsgroup in convertnews . The convertnews returns a NULL pointer and sets errno if there was an error.

  4. Write a program that takes a newsgroup name and a sleeptime value as command-line arguments. Print the time of the last modification of the newsgroup and then loop as follows.

    1. Sleep for sleeptime .

    2. Test to see whether the newsgroup has been modified.

    3. If the newsgroup directory has been modified, print a message with the newsgroup name and the time of modification.

    Test the program on several newsgroups. Post news to a local newsgroup to verify that the program is working. The newsgroup directory can be modified both by news arrival and by expiration. Most systems expire news in the middle of the night.

  5. Generalize your newsbiff program so that it reads in a list of files to be tracked from a file. Your program should store the files and their last modification times in a list. (For example, you can modify the list object developed in Section 2.9 for this purpose.) Your program should sleep for a specified number of seconds and then update the modification times of the files in the list. If any have changed, print an informative message to standard output.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net