Email List: Xaustin-review-lX
[All Lists]

Defect in XSH EXAMPLES

To: yyyyyyyyyyyyyyy@xxxxxxxxxxxxx
Subject: Defect in XSH EXAMPLES
From: yyyyy@xxxxxxxxxxx
Date: Fri, 7 Jun 2002 18:54:58 +0100 (BST)
        Defect report from : Doug Stevenson , Sun Microsystems/Technical 
Publications

(Please direct followup comments direct to yyyyyyyyyyyyyy@xxxxxxxxxxxxx)

@ page 1175 line 36568 section EXAMPLES comment {dougs1}

Problem:

Defect code :  3. Clarification required


The readdir(3c) manpage contains the following text:

     The following sample code will search the current  directory
     for the entry name:

     dirp = opendir(".");

     while (dirp) {
         errno = 0;
         if ((dp = readdir(dirp)) != NULL) {
             if (strcmp(dp->d_name, name) == 0) {
                 closedir(dirp);
                 return FOUND;
             }
         } else {
             if (errno == 0) {
                 closedir(dirp);
                 return NOT_FOUND;
             }
             closedir(dirp);
             return READ_ERROR;
         }
     }

     return OPEN_ERROR;


There are several problems with this code:

- it is needlessly incomplete
- it doesn't show the inclusion of header files
- it invents a bunch of confusing constants like FOUND and READ_ERROR
- it uses 'dirp' and 'dp' and 'name' which are never declared; it is
  not immediately obvious what type dirp and dp are supposed to be.
- The while loop is overly subtle; and if() test against dirp after the
  opendir is much more appropriate.


Action:

I suggest the following replacement example; it is somewhat longer but much
more complete; a developer can much more easily adapt this to her program
and it will compile cleanly and run as a standalone example program.

-------------------------------------------------------------------------------

        The following sample program will search the current directory for
        each of the arguments supplied on the command line:

        #include <sys/types.h>
        #include <dirent.h>
        #include <errno.h>
        #include <stdio.h>
        #include <strings.h>
        
        static void lookup(const char *arg)
        {
                DIR *dirp;
                struct dirent *dp;
        
                if ((dirp = opendir(".")) == NULL) {
                        perror("couldn't open '.'");
                        return;
                }               

                do {
                        errno = 0;
                        if ((dp = readdir(dirp)) != NULL) {
                                if (strcmp(dp->d_name, arg) != 0)
                                        continue;

                                (void) printf("found %s\n", arg);
                                (void) closedir(dirp);
                                return;
                        }
                } while (dp != NULL);

                if (errno != 0)
                        perror("error reading directory");
                else 
                        (void) printf("failed to find %s\n", arg);
                (void) closedir(dirp);
                return;
        }

        int main(int argc, char *argv[])
        {
                int i;
                for (i = 1; i < argc; i++)
                        lookup(argv[i]);
                return (0);
        }



<Prev in Thread] Current Thread [Next in Thread>