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