## root/source/rmtree.c

/* [<][>][^][v][top][bottom][index][help] */

## DEFINITIONS

This source file includes following definitions.

1. main
2. parse_comline
3. usage

    /* rmtree - A program to delete an entire directory tree.
       Rick Smereka, Copyright (C) 2005.

       This program is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published by
       the Free Software Foundation; either version 2 of the License, or
       (at your option) any later version.

       This program is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       GNU General Public License for more details.

       You should have received a copy of the GNU General Public License
       along with this program; if not, get a copy via the Internet at
       http://gnu.org/copyleft/gpl.html or write to the Free Software
       Foundation, Inc., 59 Temple Place, Suite 330, Boston,
       MA 02111-1307 USA

       You can contact the author via email at rsmereka@future-lab.com

       This program will remove an entire directory tree from a hard
       drive. BE VERY CAREFULL. It is very easy to accidently delete
       the entire contents of the hard drive.

       Program syntax is:

          rmtree [-l log_file] dirpath

       Where 'log_file' is the name of an optional log_file. The log at
       this point is very verbose (program uses the 'logman' API for logging),
       and 'dirpath' is the absolute path to the directory you want to
       delete. Program returns zero upon success, one otherwise. Program
       uses the 'armtree' API to perform the deletion of the directory tree.

       Original Linux version. Jan/2005, Rick Smereka

       Ported to QNX, 32bit Windows and DOS. Jun/2005, Rick Smereka */

    #include "stdhead.h"
    #include "armtree.h"

    #define APNAME "rmtree"
    #define VERSION "1.0.03-2005.01.22"

    // global data

    char rmtree_log[128];
    char dir_path[128];

    int main(int, char **);
    int parse_comline(int, char **);
    void usage(void);

    int main(int argc, char **argv)
    {
       int ret;
       dir_path[0] = rmtree_log[0] = EOS;

       // check command line parameters

       if (argc == 1)
          {
          usage();
          return(1);
          }

       /* parse command line */

       if (!parse_comline(argc, argv))
          return(1);

         /* build logo string based on platform */

    #ifndef OS_UNIX
       /* non-Unix */

       printf("%s for %s Version %s\n", APNAME, PLATFORM_STRING, VERSION);
    #else
       /* Unix */

       printf("%s for %s Version %s\n", APNAME, SUB_PLATFORM_STRING, VERSION);
    #endif

       printf("By Rick Smereka, Copyright (c) 2005\n");
       printf("%s comes with ABSOLUTELY NO WARRANTY\n", APNAME);
       printf("This is free software, and you are welcome to "
                           "redistribute it\n");
       printf("under certain conditions; see 'gpl.txt' for "
                           "information.\n");

       if (strlen(rmtree_log))
          {
          if (logman_start(rmtree_log, APNAME))
             {
             printf("%s:unable to start log\n", APNAME);
             return(0);
             }

          logman_console(TRUE);
          }

       // check to make sure 'dir_path' is a valid directory

       if (!isdirectory(dir_path))
          {
          if (!logman_is_active())
             printf("%s:'%s' is not a directory\n", APNAME, dir_path);

          return(0);
          }

       if (!rmtree(dir_path))
          if (!logman_is_active())
             printf("%s:removal of directory tree '%s' failed\n", APNAME, dir_path);

       return(0);
    }

    int parse_comline(int c_count, char **c_parm)
    {
       /* Parse the command line for parameters. Function
          returns 'TRUE' if no error was detected, 'FALSE'
          otherwise. */

       int parms = 1, done = FALSE;

       while(!done)
          {
          if (c_parm[parms][0] == SWITCH_CHAR)
             {
             switch(toupper(c_parm[parms][1]))
                {
                case 'L':
                   if (strlen(c_parm[parms]) > 2)
                      printf("%s:extraneous input with log switch, ignoring\n",
                             APNAME);

                   parms++;

                   if (parms >= c_count)
                      {
                      printf("%s:log switch with no file name, "
                             "program abort\n", APNAME);
                      return(FALSE);
                      }

                   strcpy(rmtree_log, c_parm[parms]);
                   parms++;
                   break;

                default:
                   printf("%s:unknown switch[%s], program abort\n",
                          APNAME, c_parm[parms]);
                   return(FALSE);
                };
             }
          else
             {
             strcpy(dir_path, c_parm[parms]);
             parms++;
             }

          if (parms >= c_count)
             done = TRUE;
          }

       if (!strlen(dir_path))
          {
          printf("%s:directory path missing, program abort\n", APNAME);
          return(FALSE);
          }

       return(TRUE);
    }

    void usage(void)
    {
       printf("usage:%s [%cl log_file] dir_path\n", APNAME, SWITCH_CHAR);
    }

/* [<][>][^][v][top][bottom][index][help] */