home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MKDIRS.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  74 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  MKDIRS.C - Function to build multi-level directories in a single call
  5. **
  6. **  Original Copyright 1993-95 by Bob Stout as part of
  7. **  the MicroFirm Function Library (MFL)
  8. **
  9. **  The user is granted a free limited license to use this source file
  10. **  to create royalty-free programs, subject to the terms of the
  11. **  license restrictions specified in the LICENSE.MFL file.
  12. **
  13. **  Also uses PUSHDIR.C from SNIPPETS.
  14. */
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <io.h>
  20. #include "dosfiles.h"
  21. #if defined(MSDOS) || defined(__MSDOS__)
  22.  #include "unistd.h"
  23. #else
  24.  #include <unistd.h>
  25. #endif
  26.  
  27. int mkdirs(char *pathname)
  28. {
  29.       int retval;
  30.       char path[FILENAME_MAX];
  31.  
  32.       strcpy (path, pathname);            /* isdir() may expand this    */
  33.  
  34.       while (strlen(path) && '\\' == LAST_CHAR(path))
  35.             LAST_CHAR(path) = NUL;
  36.  
  37.       while (0 != (retval = mkdir(path)))
  38.       {
  39.             char subpath[FILENAME_MAX] = "", *delim;
  40.  
  41.             if (EACCES == errno)
  42.             {
  43.                   if (isdir(path))
  44.                         return 0;
  45.                   else  return retval;
  46.             }
  47.             if (NULL == (delim = strrchr(path, '\\')))
  48.                   return retval;
  49.             strncat(subpath, path, delim - path);     /* Appends NUL    */
  50.             if (Success_ != mkdirs(subpath))
  51.                   break;
  52.       }
  53.       return retval;
  54. }
  55.  
  56. #ifdef TEST
  57.  
  58. main(int argc, char *argv[])
  59. {
  60.       if (2 > argc)
  61.       {
  62.             puts("Usage: MKDIRS pathname [...pathname]");
  63.             return -1;
  64.       }
  65.       while (--argc)
  66.       {
  67.             ++argv;
  68.             printf("mkdirs(%s) returned %d\n", *argv, mkdirs(*argv));
  69.       }
  70.       return 0;
  71. }
  72.  
  73. #endif /* TEST */
  74.