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