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

  1. /*
  2.  * deltree.cmm
  3.  *
  4.  * CEnvi shell command 'deltree'. Kills an entire directory.
  5.  */
  6.  
  7. #include "netware.lib"
  8. #include "filename.lib"
  9.  
  10. usage()
  11. {
  12.   printf("Use the DELTREE command to delete a directory and all of its contents.\n");
  13.   printf("Syntax:\n");
  14.   printf("  DELTREE [drive:][path]filename [/Y]\n");
  15.   printf("where:\n");
  16.   printf("  drive:/path/filename   Specifies the directory you want to delete.\n");
  17.   printf("  /Y                     Suppresses prompting to confirm you want to\n");
  18.   printf("                         delete the subdirectory.\n");
  19.   printf("\nNote: Use DELTREE cautiously. Every file and subdirectory within the\n");
  20.   printf("specified directory will be deleted.\n");
  21.   exit(EXIT_FAILURE);
  22. }
  23.  
  24. DeleteDirectory(pDirSpec)
  25. {
  26.    lReg.ah = 0x3A;
  27.    if !defined(_DOS32_)
  28.       lReg.ds = segment(pDirSpec), lReg.dx = offset(pDirSpec);
  29.    else
  30.       lReg.dx = pointer(pDirSpec);
  31.    return interrupt(0x21,lReg);
  32. }
  33.  
  34. myrmdir(dir)
  35. {
  36.   if( defined(_NWNLM_) ) return rmdir(dir);
  37.   if( defined(_OS2_) )
  38.     {
  39. #define ORD_DOS32DELETEDIR 226
  40.       return DynamicLink("DOSCALLS",ORD_DOS32DELETEDIR,BIT32,CDECL,dir);
  41.     }
  42.   if( defined(_NTCON_) || defined(_NTWIN_) || defined(_95CON_) ||
  43.       defined(_95WIN_) )
  44.     {
  45.       return !DynamicLink("KERNEL32","RemoveDirectoryA",STDCALL,dir);
  46.     }
  47.   if( defined(_DOS_) || defined(_WINDOWS_) || defined(_DOS32_) )
  48.     {
  49.       return !DeleteDirectory(dir);
  50.     }
  51.  
  52.   printf("Don't know how to remove a dir in this version of CEnvi.\n");
  53.   return 1;
  54. }
  55.  
  56. unprotect(file)
  57. {
  58.   if( defined(_NWNLM_) )
  59.     {
  60.       temp[0] = DOSTimeFromCalendar(time());
  61.       
  62.       code = NLMLink("SetFileInfo",
  63.                      file.name,
  64.                      0x06,
  65.                      file.attrib & ~(_A_RDONLY|_A_SYSTEM|_A_HIDDEN),
  66.                      tempcreate,
  67.                      tempaccess,
  68.                      tempdate,
  69.                      tempbackup,
  70.                      file.uid);
  71.       if( code )
  72.         printf("\nError setting attributes for file %s\n",file.name);
  73.       return;
  74.     }
  75.   if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
  76.     {
  77.       SetFileAttributes(file.name,file.attrib & ~_A_RDONLY);
  78.       return;
  79.     }
  80.   if( defined(_NTCON_) || defined(_NTWIN_) || defined(_95CON_) ||
  81.       defined(_95WIN_) )
  82.     {
  83.       DynamicLink("KERNEL32","SetFileAttributesA",STDCALL,file.name,
  84.                   file.attrib & ~_A_RDONLY);
  85.       return;
  86.     }
  87.   if( defined(_OS2_) )
  88.     {
  89.     }
  90. }
  91.  
  92. /* ---------------------------------------------------------------------- */
  93.  
  94. dir = "";
  95. are_you_sure = TRUE;
  96.  
  97. parse_command_line(argc,argv)
  98. {
  99.   for( i=1;i<argc;i++ )
  100.     {
  101.       if( argv[i][0]=='-' || argv[i][0]=='/' )
  102.         {
  103.           switch( toupper(argv[i][1]) )
  104.             {
  105.             default: usage();
  106.             case 'Y': are_you_sure = FALSE; break;
  107.             }
  108.         } else {
  109.           if( dir[0]=='\0' )
  110.             dir = argv[i];
  111.           else
  112.             {
  113.               printf("You may not specify multiple directories.\n");
  114.               exit(EXIT_FAILURE);
  115.             }
  116.         }
  117.     }
  118. }
  119.  
  120. /* ---------------------------------------------------------------------- */
  121.  
  122. /*
  123.  * Remove the contents of a directory, then remove it
  124.  */
  125. kill_directory(dirname)
  126. {
  127.   strcpy(killdir,dirname);
  128.  
  129.   if( defined(_NWNLM_) )
  130.     strcat(killdir,"/*.*");
  131.   else
  132.     strcat(killdir,"\\*.*");
  133.  
  134.   to_kill = Directory(killdir,FALSE,0xff);
  135.   for( i=0;to_kill && i<=GetArraySpan(to_kill);i++ )
  136.     {
  137.       if( to_kill[i].attrib & _A_SUBDIR )
  138.         {
  139.           kill_directory(to_kill[i].name);
  140.         } else {
  141.           if( remove(to_kill[i].name) )
  142.             {
  143.               unprotect(to_kill[i]);
  144.               if( remove(FullPath(to_kill[i].name)) )
  145.                 printf("Unable to delete file \"%s\".\n",FullPath(to_kill[i].name));
  146.             }
  147.         }
  148.     }
  149.   if( myrmdir(dirname) )
  150.     {
  151.       printf("Unable to remove directory \"%s\".\n",dirname);
  152.     }
  153. }
  154.  
  155. /* --------------------------------------------------------------------------- */
  156.  
  157. main(argc,argv)
  158. {
  159. // First thing to do is parse the command line.
  160.  
  161.   parse_command_line(argc,argv);
  162.  
  163.   s = strlen(dir);
  164.   if( !strcmp(dir+s-1,filename_separator) ) dir[s-1] = '\0';
  165.  
  166.   to_kill = Directory(dir);
  167.   if( to_kill==NULL )
  168.     {
  169.       printf("No such file or directory \"%s\".\n",dir);
  170.       exit(EXIT_FAILURE);
  171.     }
  172.   if( GetArraySpan(to_kill)>0 )
  173.     {
  174.       printf("You must specify a single directory.\n");
  175.       exit(EXIT_FAILURE);
  176.     }
  177.   if( (to_kill[0].attrib & _A_SUBDIR)==0 )
  178.     {
  179.       printf("You may only DELTREE a directory.\n");
  180.       exit(EXIT_FAILURE);
  181.     }
  182.   if( are_you_sure )
  183.     {
  184.       printf("Delete \"%s\" and all its contents; Are you sure? (Y/N) ",dir);
  185.       ch = gets();
  186.       if( toupper(ch[0])!='Y' ) exit(EXIT_FAILURE);
  187.     }
  188.  
  189.   kill_directory(dir);
  190. }
  191.