source/AccessorySDK/Support/MiscUtils.c
int	mkpath( const char *path, mode_t mode, mode_t dir_mode )
{
	char buf[PATH_MAX];
	size_t len;
	struct stat sb;
	char *slash;
	int done, err;

	len = strlen(path);
	if(len > (sizeof(buf) - 1)) len = sizeof(buf) - 1;
	memcpy(buf, path, len);
	buf[len] = '\0';
	slash = buf;

	for (;;) {
		slash += strspn(slash, "/");
		slash += strcspn(slash, "/");
		done = (*slash == '\0');
		*slash = '\0';

		err = mkdir(buf, done ? mode : dir_mode);
		if ((err < 0) && (errno != EEXIST)) {
			/*
			 * Can't create; path exists or no perms.
			 * stat() path to determine what's there now.
			 */
			err = errno;
			if (stat(buf, &sb) < 0) {
				/* Not there; use mkdir()'s error */
				return err ? err : -1;
			}
			if (!S_ISDIR(sb.st_mode)) {
				/* Is there, but isn't a directory */
				return ENOTDIR;
			}
		} else if (done) {
			/*
			 * Created ok, and this is the last element
			 */
			/*
			 * The mkdir() and umask() calls both honor only the
			 * file permission bits, so if you try to set a mode
			 * including the sticky, setuid, setgid bits you lose
			 * them. So chmod().
			 */
			if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) != 0 &&
				chmod(buf, mode) == -1) {
				err = errno;
				return err ? err : -1;
			}
		}
		if (done) {
			break;
		}
		*slash = '/';
	}
	return 0;
}
//===========================================================================================================================
//	mkpath
//
//	Copied from the mkdir tool and tweaked to work with const paths and without console output.
//===========================================================================================================================
mkpath
Copied from the mkdir tool and tweaked to work with const paths and without console output.
mkpath Copied from the mkdir tool and tweaked to work with const paths and without console output .
int	mkpath( const char *path, mode_t mode, mode_t dir_mode )
{
	char buf[PATH_MAX];
	size_t len;
	struct stat sb;
	char *slash;
	int done, err;
	len = strlen(path);
	if(len > (sizeof(buf) - 1)) len = sizeof(buf) - 1;
	memcpy(buf, path, len);
	buf[len] = '\0';
	slash = buf;
	for (;;) {
		slash += strspn(slash, "/");
		slash += strcspn(slash, "/");
		done = (*slash == '\0');
		*slash = '\0';
		err = mkdir(buf, done ? mode : dir_mode);
		if ((err < 0) && (errno != EEXIST)) {
			err = errno;
			if (stat(buf, &sb) < 0) {
				return err ? err : -1;
			}
			if (!S_ISDIR(sb.st_mode)) {
				return ENOTDIR;
			}
		} else if (done) {
			if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) != 0 &&
				chmod(buf, mode) == -1) {
				err = errno;
				return err ? err : -1;
			}
		}
		if (done) {
			break;
		}
		*slash = '/';
	}
	return 0;
}
int mkpath ( const char * path , mode_t mode , mode_t dir_mode ) { char buf [ PATH_MAX ] ; size_t len ; struct stat sb ; char * slash ; int done , err ; len = strlen ( path ) ; if ( len > ( sizeof ( buf ) - 1 ) ) len = sizeof ( buf ) - 1 ; memcpy ( buf , path , len ) ; buf [ len ] = ' \0 ' ; slash = buf ; for ( ; ; ) { slash += strspn ( slash , " " ) ; slash += strcspn ( slash , " " ) ; done = ( * slash == ' \0 ' ) ; * slash = ' \0 ' ; err = mkdir ( buf , done ? mode : dir_mode ) ; if ( ( err < 0 ) && ( errno != EEXIST ) ) { err = errno ; if ( stat ( buf , & sb ) < 0 ) { return err ? err : -1 ; } if ( ! S_ISDIR ( sb . st_mode ) ) { return ENOTDIR ; } } else if ( done ) { if ( ( mode & ~ ( S_IRWXU | S_IRWXG | S_IRWXO ) ) != 0 && chmod ( buf , mode ) == -1 ) { err = errno ; return err ? err : -1 ; } } if ( done ) { break ; } * slash = ' ' ; } return 0 ; }
mkpath
Copied from the mkdir tool and tweaked to work with const paths and without console output.
mkpath Copied from the mkdir tool and tweaked to work with const paths and without console output .
/*
			 * Can't create; path exists or no perms.
			 * stat() path to determine what's there now.
			 */ /* Not there; use mkdir()'s error */ /* Is there, but isn't a directory */ /*
			 * Created ok, and this is the last element
			 */ /*
			 * The mkdir() and umask() calls both honor only the
			 * file permission bits, so if you try to set a mode
			 * including the sticky, setuid, setgid bits you lose
			 * them. So chmod().
			 */