sbsv2/raptor/util/tmkdir/tmkdir.c
author Mike Kinghan <mikek@symbian.org>
Wed, 06 Oct 2010 15:13:17 +0100
changeset 28 b8fa7dfeeaa1
permissions -rw-r--r--
Update Raptor to v2.15.2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
     1
/*
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
     3
* All rights reserved.
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
     4
* This component and the accompanying materials are made available
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
     6
* which accompanies this distribution, and is available
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
     8
*
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
     9
* Initial Contributors:
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    11
*
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    12
* Contributors:
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    13
*
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    14
* Description: 
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    15
*
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    16
* Program for making directory hierarchies
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    17
*
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    18
*/
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    19
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    20
#include <stdio.h>
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    21
#include <sys/stat.h>
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    22
#include <sys/types.h>
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    23
#include <string.h>
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    24
#include <errno.h>
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    25
#include "log.h"
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    26
#include "../config.h"
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    27
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    28
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    29
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    30
int mkpath(char *path)
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    31
{
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    32
	int pathlen;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    33
	char *pathend;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    34
	char *p;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    35
	int ret = 255;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    36
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    37
	
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    38
	pathlen=strlen(path);
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    39
	pathend = path + pathlen;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    40
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    41
	p = path;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    42
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    43
	// Find the first level at which we *can* make a directory
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    44
	// go down one level at a time until we make something that works
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    45
	DEBUG(("down: %s\n", path));
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    46
	while ( 0 != mkdir(path, 0777))
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    47
	{
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    48
		//  ENOENT means that the parent directory doesn't exist so it's ok
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    49
		//  any other error is not ok and means that we must give up
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    50
		if (errno != ENOENT)
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    51
			return 1;	
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    52
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    53
		p = strrchr(path,'/');
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    54
		if (!p)
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    55
			break;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    56
		*p = '\0';
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    57
	}
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    58
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    59
	// So we found the point at which a pre-existing tree starts
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    60
	do
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    61
	{
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    62
		p = index(path, '\0');
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    63
		if (p >= pathend)
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    64
		{
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    65
			ret = 0;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    66
			break;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    67
		}
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    68
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    69
		*p = '/';
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    70
		DEBUG(("up: %s\n", path));
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    71
	}
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    72
	while  (0 == mkdir(path, 0777));
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    73
	
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    74
	return ret;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    75
}
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    76
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    77
int main(int argc, char *argv[])
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    78
{
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    79
	int i;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    80
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    81
	//loglevel=LOGDEBUG;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    82
	for (i=1; i < argc; i++)
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    83
	{
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    84
		if ( 0 != mkpath(argv[i]))
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    85
			return 255;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    86
	}
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    87
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    88
	return 0;
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    89
}
b8fa7dfeeaa1 Update Raptor to v2.15.2
Mike Kinghan <mikek@symbian.org>
parents:
diff changeset
    90