home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / NDEL.ZIP / NDEL.C next >
Encoding:
C/C++ Source or Header  |  1990-09-15  |  6.1 KB  |  185 lines

  1. /***************************************************************************
  2.  * NAME HEADER:      FILE  : NDEL.C  (not delete)                             *
  3.  *                AUTHOR: RAJESH NIHAL, compuserve [73427,1377]            *
  4.  *                DATE  : 9/11/90                                          *
  5.  *                SYS   : DOS 3.0 and up                                   *
  6.  *                PROG  : NDEL version 0.3 Copyright (c) 1990 RAJESH NIHAL *
  7.  *                REMARK: This program will delete all the files, Except   *
  8.  *                        those which are specified on the command line.   *
  9.  *                        Use this program as you wish (except abusing it).*
  10.  *                        If distributting, leave the name of the header   *
  11.  *              (this whole commented block) intact and          *
  12.  *                        do not change the run time help screen. I am NOT *
  13.  *                        responsible for any damages(so far works great). *
  14.  *                        Comments, Advises, Corrections are welcome. Next *
  15.  *                        version will have wildcard support.              *
  16.  *                PS    : THIS PROGRAM IS NOT A SHAREWEAR. DO NOT USE      *
  17.  *              WILDCARDS, IF USED WILL WIPE OUT EVERYTHING.     *
  18.  *              THE FILE[S] WHICH YOU SPECIFY ON THE COMMAND     *
  19.  *              LINE SHOULD BE PRESENT IN THE CURRENT DIRECTORY. *
  20.  ***************************************************************************/
  21.  
  22. /******************************* HEADER FILES ******************************/
  23. #include <stdio.h>
  24. #include <dir.h>
  25. #include <dos.h>
  26. #include <string.h>
  27.  
  28. /******************************* DEFINES AND MACROS ************************/
  29. #define read_only 0x01
  30. #define archive 0x20
  31. #define getdir getcwd(buffer,MAXPATH) 
  32. #define readonly_prog(attr,name) regs.h.ah = 0x43;\
  33.                  regs.h.al = 0x01;\
  34.                  regs.x.cx = attr;\
  35.                  regs.x.dx = (unsigned) name;\
  36.                  intdos(®s, ®s);
  37.  
  38. #define archive_prog(attr,name) regs.h.ah = 0x43;\
  39.                 regs.h.al = 0x01;\
  40.                 regs.x.cx = attr;\
  41.                 regs.x.dx = (unsigned) name;\
  42.                 intdos(®s, ®s);
  43. /******************************* GLOBAL VARS *******************************/
  44. int get_file(void);
  45. int delete_file(char near *);
  46. int tempargc;
  47. char near buffer[MAXPATH];
  48. int err,i;
  49. char temp[100][12];
  50. union REGS regs;
  51. struct ffblk ffblk;
  52. char probuf[MAXPATH];
  53.  
  54. /******************************** MAIN MODULE ******************************/
  55. int main(register int argc,char  *argv[])
  56. {
  57.    int f;
  58.    register int targc;
  59.    int pro;
  60.    if (argc == 1) 
  61.    {
  62.     printf("\a\n\n");
  63.     puts("Required parameter missing!");
  64.     argv[0] [strlen(argv[0]) - 4] = NULL;
  65.     printf("Synopsis: %s file[s].\n",argv[0]);
  66.     puts("Use NDEL [/? /h[H] -? -h[H] ? ] for help screen.");
  67.     exit (-1);
  68.    }
  69.    if(argc > 1)                             
  70.    {
  71.     if((strcmp(argv[1],"/?")==0) || (strcmp(argv[1],"/h")==0) || 
  72.        (strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"-?")==0) ||
  73.        (strcmp(argv[1],"-H")==0) || (strcmp(argv[1],"/H")==0) ||
  74.        (strcmp(argv[1],"?")==0))          
  75.     {
  76.         printf("NDEL  Version 0.3 Copyright (c) 1990 RAJESH (RISC) NIHAL\n"); 
  77.         printf("\nThis Utility Deletes all the files, except those specified");
  78.         printf(" on the command line\n"); 
  79.         printf("Syntax is : NDEL FILE(S)   leave a space between files.\n");
  80.         printf("Example : NDEL tc.exe  all the files except tc.exe will be deleted.\n");
  81.         printf("Example : NDEL tcc.exe tc.exe prjcnvt.txt comp.txt\n");
  82.         exit (0);
  83.     }
  84. /*************************** EXTRACT THE NAME OF THE PROGRAM ****************/
  85.     pro = 0;
  86.     for(i=(strlen(argv[0])-1);i>=0;i--)
  87.     {
  88.         if(argv[0][i] == '\\')
  89.             break;
  90.         else
  91.             probuf[pro] = argv[0][i];
  92.         pro++;
  93.     
  94.     }
  95.  
  96.     strrev(probuf);
  97.     
  98.  
  99. /*************************** SAVE THE GIVEN FILE NAMES **********************/
  100.     tempargc = argc;    
  101.                   /* this is because C compiler will not store the 
  102.                  value of tempargc after this loop, why I don't know */
  103.     targc = tempargc;
  104.     i = 0;
  105.     while (argc != 1)
  106.     {
  107.           getdir;
  108.           strcpy(temp[i],argv[argc-1]);
  109.           strcat(buffer,"\\");
  110.           strcat(buffer,temp[i]);
  111.           if(!findfirst(buffer,&ffblk,0))
  112.          for(f=0;f <= 81;f++) 
  113.              {
  114.                buffer[f] = 0;
  115.              } 
  116.           else 
  117.          {
  118.              printf("%s not found  ...program aborted.\n",temp[i]);
  119.              exit(-1);
  120.          }
  121.           argc--;
  122.           i++;
  123.     }
  124.        argc = targc;
  125.  
  126. /************************* MAKE THE GIVEN FILES READ ONLY *******************/
  127.     i = 0;
  128.         readonly_prog(read_only,probuf);
  129.     while(argc != 1) 
  130.     {
  131.         readonly_prog(read_only,temp[argc-2]);
  132.         i++;
  133.         argc--;
  134.     }
  135.     argc = targc;
  136.  
  137. /******************** FIND THE REMAINING FILES AND DELETE THEM **************/
  138.  
  139.     getfiles();    
  140.  
  141. /************************* MAKE THE GIVEN FILES ARCHIVE ********************/
  142.     while(argc != 1) 
  143.     {
  144.         archive_prog(archive,temp[argc-2]);
  145.         argc--;
  146.     } 
  147. /***************************************************************************/
  148.  
  149.     archive_prog(archive,probuf);  /* change the attrib of program to archive */
  150.    }   /* if argc > 1 */
  151.  
  152. return 0;
  153. }      /* end main module */
  154.  
  155. /***************************************************************************/
  156. int delete_file(char near *filename)            /* delete all the files */
  157. {
  158.                         /* function 41h */
  159.    regs.h.ah = 0x41;                            /* delete file */
  160.    regs.x.dx = (unsigned) filename;
  161.    intdos(®s, ®s);                        /* intr 21h */
  162.  
  163.    return 0;
  164. }                                     
  165.  
  166.  
  167. /***************************************************************************/
  168. int getfiles(void)                             /* get all the files */
  169. {
  170.    
  171.    int done;
  172.    getdir;
  173.    strcat(buffer,"\\*.*");                    /* current dir and all the files */
  174.    done = findfirst(buffer,&ffblk,0);         /* get first file */
  175.    delete_file(ffblk.ff_name);                /* delete that */
  176.          
  177.    while (!done)
  178.    {
  179.       done = findnext(&ffblk);                /* find next file */
  180.       err = delete_file(ffblk.ff_name);       /* delete that file */
  181.    }
  182.  
  183.    return 0;
  184. }
  185.