bug-glibc
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: There are still bugs in dirname(3) (glibc 2.2.3.pre1)


From: Michael Kerrisk
Subject: Re: There are still bugs in dirname(3) (glibc 2.2.3.pre1)
Date: Thu, 5 Apr 2001 20:45:50 +0200

Sigh...

Hello Ulrich,

The "thoroughly tested" dirname I sent you is broken, as you may already 
have already observed.  I injected a bug in the fixing of the multiple-
slashes-at-start-of-pathname problem.  A revised version is appended to 
this message.

I saw your note go through the Austin mailing list, and Donn Terry's 
response.  Any conclusions yet?

Cheers

Michael

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

char *
dirname(char *path)
{
  static const char dot[] = ".";
  char *last_slash;

  if (path == NULL || *path == '\0') {
    /* NULL path or empty string: return "." */
    path = (char *) dot;
  } else {
    last_slash = path + strlen(path) - 1;
  
    /* Ignore multiple slashes at end of path, step back until we find
       a non-slash, or we reach start of string */
    
    while (*last_slash == '/' && last_slash > path) 
      --last_slash;
    
    if (*last_slash == '/')     /* We got to BOS and found no non-slash */
      /* Pathname consisted only of slashes, return "/" */
  
      path[1] = '\0';
    else {
      /* Walk backwards to find slash */
      while (*last_slash != '/' && last_slash > path)
        last_slash --;
      if (*last_slash == '/') {         /* We found a slash */
        if (last_slash == path)         /* But it was the first char */
          path[1] = '\0';               /* So return "/" */
        else {                          /* Slash is not first char, so... */

          /* may have multiple slashes at end of directory spec,
             so walk back till we reach a non-slash */

          while (*(last_slash-1) == '/' && last_slash > path)
            last_slash --;
          if (last_slash == path)       /* We hit BOS */
            *(last_slash+1) = '\0';     /* So make next char terminator */
          else
            *last_slash = '\0';         /* dirname ends here */
        }
      } else
        path = (char *) dot;            /* No slash found, return "." */
    }
  }
    
  return path;
}    





reply via email to

[Prev in Thread] Current Thread [Next in Thread]