home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / DIR / NDELV00.ZIP / NDEL.C next >
Encoding:
C/C++ Source or Header  |  1991-02-25  |  6.3 KB  |  194 lines

  1. /*******************************************************************************
  2.  
  3.                                ndel(tm) v0.0
  4.  
  5.     COPYRIGHT:  This program is the sole property of Thomas D. Haynes.
  6.                             Copyright 1991.
  7.  
  8.     DISCLAIMER: While it was compilied with Turbo C++, it will work
  9.                 in Turbo C, and if you change the findfirst and
  10.                 findnext functions to the corresponding MSC functions,
  11.                 it ought to work with MSC.
  12.  
  13.                 Use of this product waives the author of any responsibilty
  14.                 of damage.  Face it, I've tested this software on my
  15.                 system, and if it wiped out my hard disk, I'd be sure
  16.                 to rewrite the code so that I could use it safely.
  17.                 (Okay, so it wiped out my c source directory when I
  18.                 tested it.  But, it _won't_ do that again.)
  19.  
  20.     AUTHOR:     Thomas D. Haynes
  21.  
  22.     FILE:   ndel        TYPE:   void              PROTOTYPE FILE:   N/A
  23.  
  24.     PURPOSE:    To delete all files from a directory except those specified.
  25.  
  26.     DATE:       02/25/91
  27.  
  28.     LANGUAGE:   Turbo C++ v1.00
  29.  
  30.     FUNCTIONS CALLED:
  31.                 input_error
  32.  
  33.     LIBRARY FUNCTIONS CALLED:
  34.                 chmod               <io.h>
  35.                 findfirst           <dir.h>
  36.                 findnext            <dir.h>
  37.                 memset              <string.h>
  38.                 perror              <stdio.h>
  39.                 remove              <stdio.h>
  40.                 strcat              <string.h>
  41.                 strcmp              <string.h>
  42.                 strcpy              <string.h>
  43.  
  44.     RETURN CODES:
  45.                 -1 if bad command line format
  46.  
  47.     OTHER:
  48.                 ndel -x ext1 ext2 ... ext(n-1) extn -f file1 file2 ...
  49.                     file(m-1) filem
  50.  
  51.                 Where ext is the file extension of those files you wish
  52.                 to save, and file is the filename minus the extension of
  53.                 those files you wish to save.
  54.  
  55.            Turbo C++ is a Copyright of Borland International
  56.  
  57. *******************************************************************************/
  58.  
  59. #define NDEL_ROOT_LEN           3
  60. #define NDEL_EXT                1
  61. #define NDEL_FILE               2
  62.  
  63. #define NDEL_FALSE              0
  64. #define NDEL_TRUE               1
  65.  
  66. #define NDEL_FILE_LEN          13
  67. #define NDEL_MAX_ENT          512
  68.  
  69. void input_error(void);
  70.  
  71. #include <dir.h>
  72. #include <sys\stat.h>
  73. #include <string.h>
  74. #include <stdlib.h>
  75. #include <stdio.h>
  76.  
  77. static char *mine[] =
  78.     {
  79.     "COPYRIGHT:  This program is the sole property of Thomas D. Haynes.",
  80.     "                     ALL RIGHTS RESERVED                          ",
  81.     "                        Copyright 1991.                           "
  82.     };
  83.  
  84. void input_error(void)
  85. {
  86.     printf("\nndel -x <extension(s)> -f <filename(s)>\n");
  87.     exit (-1);
  88. }
  89.  
  90. void main(int argc, char *argv[])
  91. {
  92.     char sav_file[NDEL_FILE_LEN];   /* string containing arg for finds      */
  93.     struct ffblk ffblk_st;          /* structure containg found info        */
  94.     int i;                          /* loop variable                        */
  95.     int done;                       /* flag if all files found              */
  96.     int type;                       /* flag for either -x or -f             */
  97.     int retcde;                     /* return of calls                      */
  98.     int index = 0;                  /* index on save array                  */
  99.     int okay_to_del;                /* flag for okay to delete              */
  100.     char sav_ar[NDEL_MAX_ENT][NDEL_FILE_LEN];
  101.                                     /* array of files to save               */
  102.     extern int errno;               /* external error code                  */
  103.  
  104.     if (argc <= 2)
  105.         {
  106.         perror("\nargc <= 2");
  107.         input_error();
  108.         }
  109.  
  110.     if (!strcmp(argv[1], "-x"))
  111.         type = NDEL_EXT;
  112.     else if (!strcmp(argv[1], "-f"))
  113.         type = NDEL_FILE;
  114.     else
  115.         input_error();
  116.  
  117.     /*  Parse the command line, getting patterns to block out
  118.         of the file deletion process                                        */
  119.  
  120.     for ( i = 1 ; i <= argc ; i++)
  121.         {
  122.          if (((!strcmp(argv[i], "-x")) || (!strcmp(argv[i], "-f"))) &&
  123.                 ((!strcmp(argv[i + 1], "-x")) || (!strcmp(argv[i + 1], "-f"))))
  124.             {
  125.             printf("\nYou have %s %s\nYou need %s <expression(s)> %s\n",
  126.                     argv[i], argv[i + 1], argv[i], argv[i + 1]);
  127.             input_error();
  128.             }
  129.  
  130.         if (type == NDEL_EXT)
  131.             {
  132.             strcpy(sav_file,"*.");
  133.             strcat(sav_file, argv[i]);
  134.  
  135.             if (!strcmp(argv[i + 1], "-f"))
  136.                 {
  137.                 type = NDEL_FILE;
  138.                 i++;
  139.                 }
  140.             }
  141.         else
  142.             {
  143.             strcpy(sav_file, argv[i]);
  144.             strcat(sav_file, ".*");
  145.  
  146.             if (!strcmp(argv[i + 1], "-x"))
  147.                 {
  148.                 type = NDEL_EXT;
  149.                 i++;
  150.                 }
  151.             }
  152.  
  153.         /*  Scan through the directory structure, finding all matches,
  154.             which are put in the save array                                 */
  155.  
  156.         done = findfirst(sav_file, &ffblk_st, 0);
  157.         while (!done)
  158.             {
  159.             memset(sav_ar[index], '\0', NDEL_FILE_LEN);
  160.             strcpy(sav_ar[index++], ffblk_st.ff_name);
  161.             done = findnext(&ffblk_st);
  162.             }
  163.         }
  164.  
  165.     /* everything should be backed up, now let's delete all unwanted files  */
  166.  
  167.     strcpy(sav_file, "*.*");
  168.     done = findfirst(sav_file, &ffblk_st,0);
  169.  
  170.     while (!done)
  171.         {
  172.         okay_to_del = NDEL_TRUE;
  173.         for (i = 0; (i < index) && (i < NDEL_MAX_ENT); i++)
  174.             {
  175.             if (!strcmp(sav_ar[i], ffblk_st.ff_name))
  176.                 {
  177.                 okay_to_del = NDEL_FALSE;
  178.                 break;
  179.                 }
  180.             }
  181.  
  182.         if (okay_to_del == NDEL_TRUE)
  183.             {
  184.             if (chmod(ffblk_st.ff_name, S_IREAD|S_IWRITE))
  185.                 perror("del: chmod");
  186.  
  187.             if (remove(ffblk_st.ff_name))
  188.                 perror("del: remove");
  189.             }
  190.  
  191.         done = findnext(&ffblk_st);
  192.         }
  193. }
  194.