home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / WIPER.ZIP / WIPER.C < prev    next >
Text File  |  1991-10-11  |  5KB  |  162 lines

  1. /************************************************************************
  2.    WIPER 1.0 (C)1991 Turgut Kalfaoglu <TURGUT@TREARN.BITNET>
  3.                      1378 Sok, 8/10, Alsancak, Izmir,Turkey
  4.  
  5.    This program wipes away a directory and its subdirectories by
  6.    removing its files, and the directories themselves.
  7.  
  8.    This file can be compiled either for OS/2 or DOS. Use /D option of MSC
  9.    to define the OS2 variable, like:  /DSO2 to compile it for OS/2
  10.  
  11.    High Performance File System - friendly version.
  12.  
  13.    It returns an error code (>0) if it fails, or 0 if everything goes
  14.    well.
  15.  
  16.    Compiled with maximum optimization and minimized codesize under MSC 6
  17.  
  18.    USER-SUPPORTED Software - if you find it of use, send $10 to the above
  19.    address. Thank you for your support!
  20.  
  21. ************************************************************************/
  22.  
  23. #ifdef OS2
  24. #define INCL_DOS
  25. #include <os2.h>
  26. #else
  27. #include <dos.h>
  28. #endif
  29. #include <errno.h>
  30. #include <direct.h>
  31. #include <stdio.h>
  32. #include <conio.h>
  33. #include <process.h>
  34. #include <string.h>
  35. #include <malloc.h>
  36.  
  37. /* Let's please _fastcall: */
  38. void showhelp();
  39. int yallah(char *); /* in case you wonder, 'yallah' means 'go for it' in Turkish :) */
  40. void fatal(char *,char *);
  41.  
  42. main(argc,argv)
  43. int argc;
  44. char *argv[];
  45. {       char a;
  46.         if (argc<2) showhelp();
  47.         if (strcmp(argv[1],"\\")==0) {
  48.            printf("Warning! This will remove all files from drive!\n");
  49.            printf("Hit any key to perform this function!\n");
  50.            a = getch(); }
  51.         exit(yallah(argv[1]));
  52. }
  53.  
  54. void showhelp() {
  55.     printf("WIPER 1.0 (C)1991 Turgut Kalfaoglu,1378 Sok 8/10,Alsancak,Izmir,Turkey\n\n");
  56.     printf("This program enables you to remove a whole branch of your diskette or hard disk\n");
  57.     printf("Simply give the subdirectory you want removed, and WIPER will remove\n");
  58.     printf("all subdirectories of that directory, along with all files contained\n");
  59.     printf("within those directories.\n");
  60.     printf("Example:\n");
  61.     printf("To remove C:\\WINDOWS along with its subdirectories from your hard disk:\n");
  62.     printf("1) Make the C: drive default by typing:\n");
  63.     printf("    C:      <ENTER>\n");
  64.     printf("2) Change to a directory 'above' the want you removed:\n");
  65.     printf("    CD \\    <ENTER>\n");
  66.     printf("3) Enter the command to remove the directory:\n");
  67.     printf("    WIPER WINDOWS\n\n\n");
  68.     printf("This program is user-supported. If you find it useful, please send $10\n");
  69.     fatal("to the above address. %s!","Thank you");
  70. }
  71.  
  72. int yallah(char *path)
  73. {
  74. #ifdef OS2
  75.        FILEFINDBUF find;
  76.        unsigned srch_cnt=1;
  77. #define name achName
  78. #define attrib attrFile
  79. #undef  _A_SUBDIR
  80. #define _A_SUBDIR 16
  81. #else
  82.        struct find_t find;
  83. #endif
  84.  
  85.     char *dirn[100];
  86.     char dira[100];
  87.     unsigned handle=0xffff,d;
  88.     int i=0;
  89.  
  90.     /* Let's see if they are desired entries: */
  91.     if (strcmp(path,".")==0) return 0; /* naw.. */
  92.     if (strcmp(path,"..")==0) return 0;
  93.  
  94.     /* Try changing to that directory, fail if we can't */
  95.     if (chdir(path) == -1) fatal("Error changing path to %s",path);
  96.  
  97.     /* Load up all the files, and directory names */
  98. #ifdef OS2
  99.  
  100.    /* 'handle' is like a reference mark for this query, 55 means 'match
  101.    all files, regardless of attributes', srch_cnt tells how many items it
  102.    can return - for compatibility with the rest of the code, I set it to 1.
  103.    Making it return everything would have been a bit more efficient.
  104.    Finally, 0L is a reserved field, it needs to be a long integer.. */
  105.  
  106.    if (!DosFindFirst("*.*",&handle,55,&find,sizeof(find),&srch_cnt,0L)) {
  107.  
  108. #else
  109.     if (!_dos_findfirst("*.*",0xffff,&find)) {
  110. #endif
  111.     /* Filename can be as long as 260 characters for HPFS (defined in BSEDOS.H)
  112.        We don't care - it's all virtual to us :)  */
  113.  
  114.        if ((dirn[i] = malloc(strlen(find.name)+1))==NULL)
  115.           fatal("Error allocating memory for %s",find.name);
  116.  
  117.        strcpy(dirn[i],find.name); /* Store name and attributes of 'zis' file */
  118.        dira[i++] = find.attrib; }
  119.  
  120.     /* Keep at it until we read the whole directory */
  121.     while (1) {
  122. #ifdef OS2
  123.       /* Find the next occurrance, using the reference 'handle' we are given: */
  124.  
  125.       if (DosFindNext(handle,&find,sizeof(find),&srch_cnt)) break;
  126. #else
  127.       if (_dos_findnext(&find)) break;
  128. #endif
  129.       if ((dirn[i] = malloc(strlen(find.name)+1))==NULL)
  130.          fatal("Error allocating memory for %s",find.name);
  131.       strcpy(dirn[i],find.name);
  132.       dira[i] = find.attrib;
  133.       i++;
  134.     }
  135.     i--;
  136.  
  137.     while (i > -1) {
  138.         if (dira[i] & _A_SUBDIR)
  139.            d=yallah(dirn[i]);
  140.         else
  141.            if (remove(dirn[i])) fatal("Cannot remove file %s. Is it read-only?",dirn[i]);
  142.         i--;
  143.     }
  144.  
  145.     /* We are done here, go up a level */
  146.     if (chdir("..") == -1) fatal("cannot change path to '%s'","..");
  147.  
  148.     /* remove the directory that we just got out of */
  149.     if (rmdir(path) != 0)  fatal("Just cannot remove %s directory",path);
  150.  
  151.     return 0; /* normal out */
  152. }
  153.  
  154. /* Something went wrong, display and quit */
  155. void fatal(char *blurb,char *string)
  156. {
  157.         printf(blurb,string);
  158.         printf("\n");
  159.         exit(1);
  160. }
  161.  
  162.