tmkdir.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <stdio.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "log.h"
  25. #include "../config.h"
  26. int mkpath(char *path)
  27. {
  28. int pathlen;
  29. char *pathend;
  30. char *p;
  31. int ret = 255;
  32. pathlen=strlen(path);
  33. pathend = path + pathlen;
  34. p = path;
  35. // Find the first level at which we *can* make a directory
  36. // go down one level at a time until we make something that works
  37. DEBUG(("down: %s\n", path));
  38. while ( 0 != mkdir(path, 0777))
  39. {
  40. // ENOENT means that the parent directory doesn't exist so it's ok
  41. // any other error is not ok and means that we must give up
  42. if (errno != ENOENT)
  43. return 1;
  44. p = strrchr(path,'/');
  45. if (!p)
  46. break;
  47. *p = '\0';
  48. }
  49. // So we found the point at which a pre-existing tree starts
  50. do
  51. {
  52. p = index(path, '\0');
  53. if (p >= pathend)
  54. {
  55. ret = 0;
  56. break;
  57. }
  58. *p = '/';
  59. DEBUG(("up: %s\n", path));
  60. }
  61. while (0 == mkdir(path, 0777));
  62. return ret;
  63. }
  64. int main(int argc, char *argv[])
  65. {
  66. int i;
  67. //loglevel=LOGDEBUG;
  68. for (i=1; i < argc; i++)
  69. {
  70. if ( 0 != mkpath(argv[i]))
  71. return 255;
  72. }
  73. return 0;
  74. }