From:	MERC::"uunet!CRVAX.SRI.COM!RELAY-INFO-VAX" 27-JUN-1992 09:18:32.00
To:	info-vax@kl.sri.com
CC:	
Subj:	Re: Looking for a program (binary) to delete directory trees

In article <1992Jun24.184844.3846@unvax.union.edu>, boehme@unvax.union.edu (Eric M. Boehm) writes:
>I am looking for a program to delete directory trees. I have command
>procedures that will recursively delete a directory tree but I need
>greater efficiency.
>
>I am looking for the functionality of the unix "rm -r" command.

/*  DELTREE - deletes a directory tree.  */
/*                                       */
/*  usage:  deltree root                 */

#include    <stdio.h>
#include    <stdlib.h>
#include    <unixlib.h>

main(int argc, char *argv[])
{
    void    rm_tree();
    char    *original, *top;

    if (argc < 2) {                         /*  root supplied?              */
        printf("usage: deltree root");
        exit(1);
    }

    original = getcwd('\0',BUFSIZ);         /*  save the current default    */

    if (chdir(argv[1]) != EOF) {            /*  if a valid root             */
        top = getcwd('\0',BUFSIZ,0);        /*  save the root spec          */
        strncat(top,".dir.",BUFSIZ);

        chdir(original);                    /*  go back home                */
        free(original);

        rm_tree(argv[1]);                   /*  and let the fun begin       */

        chmod(top,0777);                    /*  finally, remove the root    */
        delete(top);
        free(top);
    }
    else printf("%s: not a valid directory\n",argv[1]);

    exit(1);
}

void rm_tree(char *s)   /*  remove tree - call shell$to_vms to remove files  */
{
    unsigned int    action();
    int             shell$to_vms();

    chdir(s);                               /*  move to target dir    */
    shell$from_vms("*.*;*",action,1);       /*  blow everything away  */
    chdir("[-]");                           /*  move back one         */

    return;
}

unsigned int action(char *name, int type)   /*  this routine deletes files  */
{
    char    *strstr(), *ps;

    if ((ps=strstr(name,".DIR.")) != '\0')  /*  is this a directory?  */
    {
        *ps = '\0';                         /*  yup, so delete all    */
        rm_tree(name);                      /*  files in it too!      */

        *ps = '.';                          /*  then delete it        */
        chmod(name,0777);
        delete(name);
    }
    else                                    /*  it's a file, so        */
    {  
        chmod(name,0777);                   /*  just delete it         */
        delete(name);
    }
    return 1;
}

