home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / deltree.cmm < prev    next >
Encoding:
Text File  |  1995-10-06  |  4.7 KB  |  186 lines

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