|
|
Message-ID: <00e801ce2418$e35274d0$a9f75e70$@net>
Date: Mon, 18 Mar 2013 15:40:54 -0500
From: "jfoug" <jfoug@....net>
To: <john-dev@...ts.openwall.com>
Subject: basename portability 'fix'.
Here is a proper running basename. I build both a 'normal' basename, and a
thread safe one. The 'normal' one, simply has a static buffer that it
passes into the thread safe one. the reason for the buffer, is I chose to
have the name param be a const char *. Some basename's have only a char *,
and document that the function can mess with the param. BBAAADDDD, but they
do that. Looking at many implementations of basename, it looks like about
50% of them have very bad side effects, or simply do not work properly.
Here is the function proto's (I put these into misc.c/h
char *jtr_basename(const char *name)
char *jtr_basename_r(const char *name, char *buf) (buf must be
strlen(name)+1, but PATH_BUFFER_SIZE+1 can also be used.
Here are the 'rules'
a null name, or name pointing to a length 0 string returns "."
Both Win32 ('\\') and unix ('/') separator chars are handled. In unix
builds, only the / is handled. In win32, 'both' are handled, since they
actually mean the same thing.
if string is only / or \ chars, then a single / or \ will be returned.
Win32/DOS drive letters are handled. NOTE, a C: by itself, is treated like
a string of length 0, and returns "."
Trailing / or \ are removed from final basename, IIF there is a basename.
This is made against bleeding (since a code freeze is on, I think on the new
jumbo). It likely will patch to the jumbo easily, IF someone wants to do
that also.
To use this, simply do this:
#include "misc.h"
char *p = jtr_basename(fullname);
To use this IF there were in code that had to be thread safe, the
jtr_basename_r() function could be used. I do not foresee that need
currently, but it is there, since this function needs either a static
buffer, OR needs the input parameter to be able to be destroyed.
Jim.
Content of type "text/html" skipped
Download attachment "bleeding_basename_portability_fix.patch" of type "application/octet-stream" (5763 bytes)
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.