FreeBSD/cddl/compat/opensolaris/misc/mkdirp.c
/*
 *	simplify - given a pathname, simplify that path by removing
 *		   duplicate contiguous slashes.
 *
 *		   A simplified copy of the argument is returned to the
 *		   caller, or NULL is returned on error.
 *
 *		   The caller should handle error reporting based upon the
 *		   returned vlaue, and should free the returned value,
 *		   when appropriate.
 */
given a pathname, simplify that path by removing
duplicate contiguous slashes.

A simplified copy of the argument is returned to the
caller, or NULL is returned on error.

The caller should handle error reporting based upon the
returned vlaue, and should free the returned value,
when appropriate.
given a pathname simplify that path by removing duplicate contiguous slashes . A simplified copy of the argument is returned to the caller or NULL is returned on error . The caller should handle error reporting based upon the returned vlaue and should free the returned value when appropriate .
static char *
simplify(const char *str)
{
	int i;
	size_t mbPathlen;	
	size_t wcPathlen;	
	wchar_t *wptr;		
	wchar_t *wcPath;	
	char *mbPath;		
	if (!str)
	    return (NULL);
	if ((mbPath = strdup(str)) == NULL) {
		return (NULL);
	}
	mbPathlen = strlen(mbPath);
	if ((wcPath = calloc(sizeof (wchar_t), mbPathlen+1)) == NULL) {
		free(mbPath);
		return (NULL);
	}
	if ((wcPathlen = mbstowcs(wcPath, mbPath, mbPathlen)) == (size_t)-1) {
		free(mbPath);
		free(wcPath);
		return (NULL);
	}
	for (wptr = wcPath, i = 0; i < wcPathlen; i++) {
		*wptr++ = wcPath[i];
		if (wcPath[i] == '/') {
			i++;
			while (wcPath[i] == '/') {
				i++;
			}
			i--;
		}
	}
	*wptr = '\0';
	if (wcstombs(mbPath, wcPath, mbPathlen) == (size_t)-1) {
		free(mbPath);
		free(wcPath);
		return (NULL);
	}
	free(wcPath);
	return (mbPath);
}
static char * simplify ( const char * str ) { int i ; size_t mbPathlen ; size_t wcPathlen ; wchar_t * wptr ; wchar_t * wcPath ; char * mbPath ; if ( ! str ) return ( NULL ) ; if ( ( mbPath = strdup ( str ) ) == NULL ) { return ( NULL ) ; } mbPathlen = strlen ( mbPath ) ; if ( ( wcPath = calloc ( sizeof ( wchar_t ) , mbPathlen + 1 ) ) == NULL ) { free ( mbPath ) ; return ( NULL ) ; } if ( ( wcPathlen = mbstowcs ( wcPath , mbPath , mbPathlen ) ) == ( size_t ) -1 ) { free ( mbPath ) ; free ( wcPath ) ; return ( NULL ) ; } for ( wptr = wcPath , i = 0 ; i < wcPathlen ; i ++ ) { * wptr ++ = wcPath [ i ] ; if ( wcPath [ i ] == ' ' ) { i ++ ; while ( wcPath [ i ] == ' ' ) { i ++ ; } i -- ; } } * wptr = ' \0 ' ; if ( wcstombs ( mbPath , wcPath , mbPathlen ) == ( size_t ) -1 ) { free ( mbPath ) ; free ( wcPath ) ; return ( NULL ) ; } free ( wcPath ) ; return ( mbPath ) ; }
simplify - given a pathname, simplify that path by removing
                 duplicate contiguous slashes.
simplify - given a pathname simplify that path by removing duplicate contiguous slashes .
/* length of multi-byte path */ /* length of wide-character path */ /* scratch pointer */ /* wide-character version of the path */ /* The copy fo the path to be returned */ /*
	 *  bail out if there is nothing there.
	 */ /*
	 *  Get a copy of the argument.
	 */ /*
	 *  convert the multi-byte version of the path to a
	 *  wide-character rendering, for doing our figuring.
	 */ /*
	 *  remove duplicate slashes first ("//../" -> "/")
	 */ /*
	 *  now convert back to the multi-byte format.
	 */