home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 21 / CD_ASCQ_21_040595.iso / dos / prg / c / freedos3 / source / jh_utils / del.c < prev    next >
C/C++ Source or Header  |  1995-01-07  |  5KB  |  229 lines

  1. /*
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.  
  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.    GNU General Public License for more details.
  11.  
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program; if not, write to the Free Software
  14.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.    */
  16.  
  17. /*************************************************************************
  18.  * This program will erase one or more files or directories.  You may
  19.  * specify that it be interactive.
  20.  *
  21.  * Libraries by John Hall, 1994.
  22.  * Main function by James Hall, 1994.
  23.  */
  24.  
  25. /* NOTE: Some of these #include's may not be needed anymore since the
  26.    last update. */
  27.  
  28. #include <stdio.h>
  29. #include <conio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <memory.h>
  33. #include <time.h>
  34. #include <dos.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <assert.h>
  38. #include "getopt.h"
  39. #include "freedos.h"
  40.  
  41.  
  42. /* Symbolic constants */
  43.  
  44. #define STREQ(a,b) (strcmp((a), (b)) == 0)
  45. #define _A_FILE (_A_ARCH | _A_HIDDEN | _A_NORMAL | _A_RDONLY | _A_SYSTEM)
  46.  
  47.  
  48. /* freedos.h defines TRUE and FALSE which present a problem to John's
  49. original code */
  50.  
  51. #undef TRUE
  52. #undef FALSE
  53.  
  54.  
  55. /* Boolean values */
  56.  
  57. enum bool
  58.   {
  59.     FALSE, TRUE
  60.   };
  61.  
  62. /* Error codes */
  63.  
  64. enum ecode
  65.   {
  66.     EOK, ESYNTAX, EBADTIME
  67.   };
  68.  
  69. /* Globals */
  70.  
  71. int Force = 0, Subdir = 0;
  72.  
  73. /* Functions */
  74.  
  75. void erase (char *pathname);
  76. void erase_dir (char *dir);
  77. void usage (void);
  78.  
  79.  
  80. main (int argc, char **argv)
  81. {
  82.   struct stat statbuf;
  83.   int i, c;
  84.  
  85.   /* Parse the command line */
  86.  
  87.   while ((c = getopt (argc, argv, "fFsS?")) != EOF)
  88.     {
  89.       switch (c)
  90.     {
  91.     case 'f':
  92.     case 'F':        /* Force */
  93.       Force = 1;
  94.       break;
  95.  
  96.     case 's':
  97.     case 'S':        /* Subdirectories */
  98.       Subdir = 1;
  99.       break;
  100.  
  101.     default:
  102.       usage ();
  103.       break;
  104.     }
  105.     }
  106.  
  107.   /* Delete all files */
  108.  
  109.   for (i = optind; i < argc; i++)
  110.     {
  111.       stat (argv[i], &statbuf);
  112.  
  113.       /* Delete file */
  114.  
  115.       if (statbuf.st_mode & S_IFDIR)
  116.     {
  117.       if (!Force)
  118.         {
  119.           fprintf (stderr, "Warning: You are about to delete a directory");
  120.  
  121.           if (Subdir)
  122.         fprintf (stderr, " and its subcontents");
  123.  
  124.           fprintf (stderr, "\n");
  125.  
  126.           fprintf (stderr, "Are you sure?\n");
  127.           c = getch ();
  128.  
  129.           if ((c == 'y') || (c == 'Y'))
  130.         erase_dir (argv[i]);
  131.  
  132.         }
  133.       else
  134.         erase_dir (argv[i]);
  135.  
  136.     }
  137.       else
  138.     erase (argv[i]);
  139.     }
  140.  
  141.   exit (0);
  142. }
  143.  
  144. /************************************************************************
  145.  * This function clears "pathname" of its attributes, then deletes the
  146.  * file.
  147.  * Returns: nothing
  148.  */
  149. void 
  150. erase (char *pathname)
  151. {
  152.   assert (pathname != NULL);
  153.  
  154.   /* Clear file attributes */
  155.   _dos_setfileattr (pathname, 0x00);
  156.  
  157.   /* Delete file */
  158.   if (remove (pathname) != EOK)
  159.     fprintf (stderr, "Warning: Unable to remove %s\n", pathname);
  160. }
  161.  
  162.  
  163. /************************************************************************
  164.  * This function deletes all files and subdirectories in a directory,
  165.  * then removes the directory.
  166.  * Returns: nothing
  167.  */
  168. void 
  169. erase_dir (char *dir)
  170. {
  171.   /* Some of these may not be needed anymore: */
  172.  
  173.   struct find_t finfo;
  174.   unsigned done;
  175.   char filename[_MAX_PATH];
  176.   int length;
  177.  
  178.   assert (dir != NULL);
  179.  
  180.   /* Remove any trailing backslash from dir */
  181.   length = strlen (dir);
  182.   assert (length > 0);
  183.   if (dir[length - 1] == '\\')
  184.     dir[length - 1] = '\0';
  185.  
  186.   /* Remove contents of directory */
  187.   sprintf (filename, "%s\\*.*", dir);
  188.   for (done = _dos_findfirst (filename, _A_FILE | _A_SUBDIR, &finfo);
  189.        !done;
  190.        done = _dos_findnext (&finfo))
  191.     {
  192.  
  193.       /* Specifically exclude . and .. from search */
  194.       if (!STREQ (finfo.name, ".") && !STREQ (finfo.name, ".."))
  195.     {
  196.  
  197.       /* Build full path name */
  198.       sprintf (filename, "%s\\%s", dir, finfo.name);
  199.  
  200.       /* Delete file or directory */
  201.       if (finfo.attrib == _A_SUBDIR)
  202.         erase_dir (filename);
  203.       else
  204.         erase (filename);
  205.     }
  206.     }
  207.  
  208.   /* Remove directory */
  209.   if ((Subdir) && (rmdir (dir) == EOK))
  210.     fprintf (stderr, "Removed %s\n", dir);
  211.   else
  212.     fprintf (stderr, "Warning: Unable to remove %s\n", dir);
  213. }
  214.  
  215.  
  216. /************************************************************************
  217.  * This function prints usage information for the program.
  218.  * Returns: nothing.
  219.  */
  220. void 
  221. usage (void)
  222. {
  223.   printp ("DEL", "Erases one or more files or directories.");
  224.   printu ("DEL", "[/F] [/S] file..");
  225.   printo ("/F", "Force the removal of subdirectories, even if not empty.");
  226.   printo ("/S", "Recursively erase subdirectories.  Will ask first.");
  227.   exit (1);
  228. }
  229.