home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_02 / undo.c < prev    next >
Text File  |  1990-02-21  |  4KB  |  164 lines

  1. /*
  2. HEADER:        ;
  3. TITLE:        Prompted delete with wildcards;
  4. VERSION:    1.0;
  5.  
  6. DESCRIPTION:    Undo allows for multiple file names to be specified on the
  7.         same command line along with wildcard specifiers if desired.
  8.         Undo will display the directory and the file name and wait
  9.         for the user to respond before deleting the file.  Undo will
  10.         continue until all matching files have been prompted for.
  11.         Also, the file(s) entered on the command line do not have to
  12.         be in the same directory or even on the same drive. Undo will
  13.         find any matches as long as the complete path is specified.
  14.  
  15. KEYWORDS:    Dos utilities;
  16. SYSTEM:        MSDOS;
  17. FILENAME:    UNDO;
  18. WARNINGS:    None;
  19.  
  20. SEE-ALSO:    FBYTE;
  21. AUTHORS:    Dr. Ronald J. Terry;
  22. COMPILERS:    Turbo C;
  23. */
  24. /***************************************************************************/
  25. /*  Most of the code is self explanatory.  This function will examine      */
  26. /*  multiple inputs from the command line including wildcards and prompt   */
  27. /*  the user for a yes (y) or no (n) response before deleting the file(s). */
  28. /***************************************************************************/
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <dir.h>
  33. #define BKSLASH 92
  34. int getdrv_num(char dltr);
  35.  
  36. void main(int argc, char *argv[])  /* Get command line arguments */
  37. {
  38.    struct ffblk  ffblk;
  39.    char buf[80],cmd_arg[80],driveno[80];
  40.    char response,drvltr,errmsg[80];
  41.    int i,len,len1,buflen,done,drive,cdir;
  42.    buflen = 80;       /* Maximum number of characters on command line */
  43.    getcwd(buf,buflen);  /* save current directory */
  44.    i = 1;
  45.    while(i<argc)
  46.    {
  47.    cdir = 0;
  48.    strncpy(driveno,buf,2);
  49.    driveno[2] = NULL;
  50.    strcpy(cmd_arg,argv[i]);
  51.    len = strlen(argv[i]);
  52.    cmd_arg[len] = NULL;
  53.      if(cmd_arg[1]==':')
  54.      {
  55.      cmd_arg[0] = toupper((int)(cmd_arg[0]));   /* drive specification */
  56.      strncpy(driveno,cmd_arg,2);
  57.      driveno[2] = NULL;
  58.      }
  59.      else
  60.      {
  61.      cdir = 1;  /* true if file(s) in current directory otherwise false */
  62.      }
  63.      if(cmd_arg[2]!=BKSLASH)
  64.      {
  65.        if(cmd_arg[0]==BKSLASH)        /* get path */
  66.        {
  67.        strcpy(cmd_arg,cmd_arg+1);
  68.        }
  69.      driveno[2] = NULL;
  70.      strcat(driveno,"\\");
  71.      driveno[3] = NULL;
  72.       if(!cdir)
  73.       {
  74.       strcpy(cmd_arg,cmd_arg+2);
  75.       }
  76.      strcpy(cmd_arg,(strcat(driveno,cmd_arg)));
  77.      }
  78.    strcpy(driveno,strrchr(cmd_arg+3,BKSLASH));
  79.    len1=strlen(driveno);
  80.    if(cdir&&!len1)
  81.    {
  82.    strcpy(cmd_arg,buf);
  83.    }
  84.    else
  85.    {
  86.      if(!len1)
  87.      {
  88.        if(cmd_arg[0]==buf[0])
  89.        {
  90.        strcpy(cmd_arg,buf);
  91.        }
  92.      else
  93.      {
  94.      cmd_arg[3] = NULL;
  95.      }}
  96.    len = strlen(cmd_arg);
  97.    len = len - len1;
  98.    cmd_arg[len] = NULL;
  99.    }
  100.    done = findfirst(argv[i],&ffblk,0);
  101.    putchar(10);                  /* find first file and any other matches */
  102.    drvltr = cmd_arg[0];
  103.    drive = getdrv_num(drvltr);
  104.    setdisk(drive);
  105.    chdir(cmd_arg);
  106.    printf("  directory of %s\n",cmd_arg);
  107.      if(done)
  108.      {
  109.      strcpy(errmsg,argv[i]);
  110.      putchar(7);
  111.      printf("  ");
  112.      perror(errmsg);        /* indicate if no matches were found */
  113.      puts("  By courtesy of RT: <Press any key>");
  114.        for(;;)
  115.        {
  116.      if(getch())
  117.      {
  118.      break;
  119.      }
  120.        }
  121.      }
  122.    while(!done)
  123.    {
  124.    printf("  %s",ffblk.ff_name);
  125.      for(;;)
  126.      {
  127.      response = tolower(getch());
  128.        if(response=='y' || response=='n')
  129.        {
  130.      if(response=='y')
  131.      {
  132.      remove(ffblk.ff_name);   /* delete file */
  133.      printf("  deleted");
  134.      }
  135.        putchar(10);
  136.        break;
  137.        }
  138.      }
  139.    done = findnext(&ffblk);
  140.    }
  141.    i++;
  142.    drvltr = buf[0];
  143.    drive  = getdrv_num(drvltr);
  144.    setdisk(drive);
  145.    chdir(buf);
  146.    }
  147. }
  148. /********* function GETDRV_NUM is used to convert the drive letter into an
  149.                integer number for use in SETDISK. ****************/
  150.  
  151. int getdrv_num(char drvltr)
  152. {
  153.     int drive;
  154.     switch(drvltr)
  155.     {
  156.     case 'A': drive = 0;
  157.     break;
  158.     case 'B': drive = 1;
  159.     break;
  160.     default : drive = 2;
  161.     }
  162.     return(drive);
  163. }
  164.