home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / MAKEDIR.CMM < prev    next >
Text File  |  1996-01-02  |  4KB  |  118 lines

  1. // MakeDir.cmm - CEnvi tool to make a full directory path, given any
  2. // ver.1         relative path.  This goes down the directory tree
  3. //               to create any path necessary.  Returns code 1 if path
  4. //               could not be created, or 0 if path was fully created
  5. //               or already exists.
  6.  
  7. usage()
  8. {
  9.    puts(`MakeDir.cmm - Make full directory path`)
  10.    puts("\a")
  11.    puts(`USAGE: CEnvi MakeDir <PathSpec>`)
  12.    puts(``)
  13.    puts(`EXAMPLES: CEnvi MakeDir C:\GOOBER\FOOBER\DOOBER`)
  14.    puts(`          CEnvi MakeDir ..\BOMP\SHOO\BOMP`)
  15.    puts(``);
  16.    exit(EXIT_FAILURE);
  17. }
  18.  
  19. main(argc,argv)
  20. {
  21.   if( argc!=2 ) usage();
  22.   if( !strcmp(argv[1],"/?") ) usage();
  23.  
  24.   if ( MakeFullDirectoryPath(argv[1]) )
  25.     return EXIT_SUCCESS;
  26.    
  27.    // if made it to here then it failed somewhere
  28.    puts("Path not made\a");
  29.    if ( defined(_WINDOWS_) || defined(_NTWIN_) || defined(_95WIN_)) &&
  30.         !defined(_SHELL_) {
  31.       printf("press any key to exit...");
  32.       getch();
  33.    }
  34.   if( defined(_NWNLM_) ) PressAnyKeyToContinue();
  35.  
  36.   return ( EXIT_FAILURE );
  37. }
  38.  
  39.  
  40. MakeFullDirectoryPath(pPathSpec)
  41.    // make full directories given partial specification.  Return True if
  42.    // directory already exists or can be made, else False if it cannot be
  43.    // made, is invalid, contains wildcards, or any other problem
  44. {
  45.    // failure if any wildcards in the name
  46.    if ( strchr(pPathSpec,'*')  ||  strchr(pPathSpec,'?') )
  47.       return False;
  48.  
  49.    // make full path specification
  50.    if ( !(lFullName = FullPath(pPathSpec)) )
  51.       return False;
  52.  
  53.    // find the first backslash (error if is not one)
  54.    if ( !(lDirDelimiter = strchr(lFullName,'\\')) )
  55.      if( defined(_NWNLM_) && !(lDirDelimiter = strchr(lFullName,'/')) )
  56.        return False;
  57.  
  58.    // if this is at the end of string then this
  59.    // is the root directory, and so make no sub-directories
  60.    if ( lDirDelimiter[1] ) {
  61.  
  62.       // if the final character is a backslash, and if that is
  63.       // not two backslashes in a row, then remove it.
  64.       // some people will add a final backslash on directories
  65.       lLast = strrchr(lDirDelimiter+1,'\\');
  66.       if ( lLast  &&  !lLast[1]  &&  '\\' != lLast[-1] )
  67.          lLast[0] = 0;
  68.  
  69.       if( defined(_NWNLM_) )
  70.         {
  71.           lLast = strrchr(lDirDelimiter+1,'/');
  72.           if ( lLast  &&  !lLast[1]  &&  '/' != lLast[-1] )
  73.             lLast[0] = 0;
  74.         }
  75.       // now make the directories one at a time, not bothering to check
  76.       // if they already exist, because there's no harm making one that
  77.       // already exists
  78.       old = lDirDelimiter;
  79.       while ( lDirDelimiter = strchr(lDirDelimiter+1,'\\') ) {
  80.          lDirDelimiter[0] = 0;
  81.          MakeDirectory(lFullName);
  82.          lDirDelimiter[0] = '\\';
  83.       }
  84.       lDirDelimiter = old;
  85.       while( defined(_NWNLM_) && (lDirDelimiter = strchr(lDirDelimiter+1,'/')) )
  86.         {
  87.           lDirDelimiter[0] = 0;
  88.           MakeDirectory(lFullName);
  89.           lDirDelimiter[0] = '/';
  90.         }
  91.       MakeDirectory(lFullName);
  92.    }
  93.  
  94.    // finally, to see if it all worked, return whether dir exists
  95.    return ( NULL != Directory(lFullName,False,FATTR_SUBDIR,FATTR_SUBDIR) );
  96. }
  97.  
  98. MakeDirectory(pDirName) // no return value
  99. {
  100.    if defined(_OS2_) {
  101.       #define ORD_DOS32CREATEDIR 270
  102.       DynamicLink("doscalls",ORD_DOS32CREATEDIR,BIT32,CDECL,pDirName,0)
  103.    } else if ( defined(_NTCON_) || defined(_NTWIN_) || defined(_95CON_) ||
  104.                defined(_95WIN_) ) {
  105.       DynamicLink("KERNEL32","CreateDirectoryA",STDCALL,pDirName,NULL);
  106.    } else if( defined(_NWNLM_) )
  107.    {
  108.      mkdir(pDirName);
  109.    } else { // dos or windows use the same code
  110.     lReg.ah = 0x39;
  111.     if !defined(_DOS32_)
  112.        lReg.ds = segment(pDirName), lREg.dx = offset(pDirName);
  113.     else
  114.        lReg.dx = pointer(pDirName);
  115.     interrupt(0x21,lReg);
  116.    }
  117. }
  118.