home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / BOUNDUTI.ZIP / MRM.C < prev    next >
C/C++ Source or Header  |  1990-08-16  |  2KB  |  101 lines

  1. /*
  2.     ---------------------------------
  3.       mrm.c - file deletion utility
  4.     ---------------------------------
  5.  
  6.     written 8/15/90 by M. Mackey
  7. */
  8. #define INCL_DOS
  9.  
  10. #include <os2.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <conio.h>
  15.  
  16. #define TRUE 1
  17. #define FALSE 0
  18. #define ATTR 0x0021
  19. #define READ_ONLY 0x0001
  20. #define LENGTH sizeof(buff)
  21.  
  22. static int    yes(void);
  23. static void report(char *format,char *arg);
  24. static void error(char *fmt,char *arg1);
  25. static void ChangeAttr(struct _FILEFINDBUF *buff);
  26.  
  27. int interactive = FALSE;
  28.  
  29. void main(int argc,char *argv[])
  30. {
  31.     char pathname[_MAX_PATH],buf[10];
  32.     struct _FILEFINDBUF buff;
  33.     unsigned handle,count,rc;
  34.  
  35.     for(argc--,argv++;argc && argv[0][0] == '-'; argc--,argv++)
  36.         switch(argv[0][1]) {
  37.             case 'i':
  38.                 interactive = TRUE;
  39.                 break;
  40.             default:
  41.                 error("mrm: unknown option %s",argv[0]);
  42.         }
  43.     handle=0xffff;
  44.     for(;argc;argc--,argv++) {
  45.         count=1;
  46.         strcpy(pathname,*argv);
  47.         if(rc=DosFindFirst(pathname,&handle,ATTR,&buff,LENGTH,&count,0L))
  48.             if(rc==18)
  49.                 error("mrm: file %s not found",pathname);
  50.             else
  51.                 error("mrm: internal error %s",itoa((unsigned)rc,buf,10));
  52.         report("\nmrm '%s' [y/n]? ",buff.achName);
  53.         if(yes()) {
  54.             ChangeAttr(&buff);
  55.             DosDelete(buff.achName,0L);
  56.         }
  57.         while(TRUE) {
  58.             DosFindNext(handle,&buff,LENGTH,&count);
  59.             if(!count)
  60.                 break;
  61.             report("\nmrm '%s' [y/n]? ",buff.achName);
  62.             if(yes()) {
  63.                 ChangeAttr(&buff);
  64.                 DosDelete(buff.achName,0L);
  65.             }
  66.         }
  67.     }
  68.     DosFindClose(handle);
  69.     exit(0);
  70. }
  71. void ChangeAttr(struct _FILEFINDBUF *buff)
  72. {
  73.     unsigned attr;
  74.  
  75.     if(buff->attrFile & READ_ONLY) {
  76.         report("  override read only [y/n]? ",NULL);
  77.         if(yes()) {
  78.             DosQFileMode(buff->achName,&attr,0L);
  79.             attr ^= READ_ONLY;
  80.             DosSetFileMode(buff->achName,attr,0L);
  81.         }
  82.     }
  83. }
  84. int yes(void)
  85. {
  86.     if (interactive)
  87.         return (getche() == 'y');
  88.     else
  89.         return TRUE;
  90. }
  91. void report(char *format,char *arg)
  92. {
  93.     if (interactive)
  94.         printf(format, arg);
  95. }
  96. void error(char *fmt,char *arg1)
  97. {
  98.     printf(fmt, arg1);
  99.     exit(1);
  100. }
  101.