home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / prog2 / at.lzh / AT.C next >
Encoding:
C/C++ Source or Header  |  1991-03-03  |  7.6 KB  |  231 lines

  1. /* AT.C
  2. **  Changes the attributes of one or more files as specified from the
  3. **  command line.
  4. **  AT filename1 +R would add the readonly attribute to filename1.
  5. **  AT filename1 +RH would add both readonly and hidden attributes to filename1.
  6. **  AT filename1 -R would remove only the readonly attribute.
  7. **  AT filename1 -RH would remove both readonly and hidden attributes.
  8. **  AT filename1 +r filename2 -h does the following:
  9. **   adds readonly to filename1,
  10. **   removes hidden from filename2
  11. **  By Kirby Devore 2/91
  12. **  MSC V5.1
  13. **   cl /AL /W3 at.c   
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <dos.h>      /* for _dos_() */
  18. #include <process.h>  /* for exit() */
  19. #include <string.h>
  20. #include <malloc.h>   /* for malloc() */
  21.  
  22. #define READO 0x01 /* obtained from dos.h and shortened */
  23. #define HIDE  0x02
  24. #define SYS   0x04
  25. #define ARCH  0x20
  26.  
  27. struct llist /* structure for linked list */
  28. {
  29.    char fname[13];
  30.    struct llist *link;
  31. };
  32.  
  33. /* function prototypes */
  34. char **dirlist(char *);              
  35. void get_file_attrs(char *);
  36. void set_file_attrs(char *,unsigned);
  37. void reset_file_attrs(char *,unsigned);
  38. void main(int,char **); /* yes, even main to maintain ANSI standards */
  39.  
  40. void get_file_attrs(filename)
  41. char *filename;
  42. {
  43.    unsigned attr;
  44.  
  45.    if(_dos_getfileattr(filename,&attr)) /* non-portable - insert TC equivalent fileattr call here */
  46.       printf("READ failed\n");          
  47.    {
  48.       printf("%s %s %s %s %s\n",filename,
  49.             (attr&SYS)?"SYSTEM":"",
  50.             (attr&HIDE)?"HIDDEN":"",
  51.             (attr&READO)?"READON":"",
  52.             (attr&ARCH)?"ARCHIV":"");
  53.    }
  54. }
  55.  
  56. void set_file_attrs(filename,attr)
  57. char *filename;
  58. unsigned attr;
  59. {
  60.    unsigned attrib;
  61.  
  62.    if(!_dos_getfileattr(filename,&attrib)) /* non-portable insert TC equivalent fileattr call here */
  63.    {
  64.       attrib|=attr; /* attrib is bit-wise ORed by attr */
  65.       if(_dos_setfileattr(filename,attrib)) /* non-portable insert TC equivalent fileattr call here */
  66.           printf("SET failed.\n");
  67.    }
  68. }
  69.  
  70. void reset_file_attrs(filename,attr)
  71. char *filename;
  72. unsigned attr;
  73. {
  74.    unsigned attrib;
  75.  
  76.    if(!_dos_getfileattr(filename,&attrib)) /* non-portable insert TC equivalent fileattr call here */
  77.    {
  78.       attrib&=~attr; /* attrib is bit-wise ANDed with the complement of attr */
  79.       if(_dos_setfileattr(filename,attrib)) /* non-portable insert TC equivalent fileattr call here */
  80.           printf("RESET failed.\n");
  81.    }
  82. }
  83.  
  84. char **dirlist(arglist)
  85. char *arglist;
  86. {
  87.    char **list;
  88.    struct find_t name;
  89.    struct llist *link_list;
  90.    struct llist *base;
  91.    int nfiles,i;
  92.  
  93.    nfiles=0;
  94.    if(!_dos_findfirst(arglist,READO|HIDE|SYS|ARCH,&name)) /* non-portable insert TC equivalent fileattr call here */
  95.    {
  96.       link_list=(struct llist *)malloc(sizeof(struct llist));
  97.       if(link_list!=NULL)
  98.       {
  99.          base=link_list;
  100.          strcpy(link_list->fname,name.name);
  101.          nfiles++;
  102.          link_list->link=(struct llist *)malloc(sizeof(struct llist));
  103.          link_list=link_list->link;
  104.          while(!_dos_findnext(&name)) /* non-portable insert TC equivalent fileattr call here */
  105.          {
  106.             strcpy(link_list->fname,name.name);
  107.             nfiles++;
  108.             link_list->link=(struct llist *)malloc(sizeof(struct llist));
  109.             link_list=link_list->link;
  110.          }
  111.          link_list->link=NULL;
  112.          link_list=base;
  113.          list=(char **)malloc((nfiles+1)*sizeof(char *));
  114.          for(i=0;i<nfiles;i++)
  115.          {   
  116.             list[i]=link_list->fname;
  117.             link_list=link_list->link;
  118.          }
  119.          list[i]=NULL;
  120.          return(list);
  121.       }
  122.       else
  123.          return(NULL);
  124.    }
  125.    else
  126.       return(NULL);
  127. }
  128.  
  129. void main(argc,argv)
  130. int argc;
  131. char **argv;
  132. {
  133.    char *filename;
  134.    char **wildnames;
  135.    int names,ops,wild;
  136.  
  137.    names=ops=wild=0;
  138.    if(argc>1) /* must be more than one argument to run program */
  139.    {
  140.       if(strcmp(argv[1],"")) /* at least one filename exists */
  141.       {
  142.          if(strcmp(argv[2],"")) /* at least one operation exists */
  143.          {
  144.             for(names=1;names<argc;names+=2)
  145.             {
  146.                wildnames=dirlist(argv[names]);
  147.                if(wildnames!=NULL)
  148.                {
  149.                   wild=0; /* make sure index gets reset */
  150.                   while(wildnames[wild]!=NULL)
  151.                   {
  152.                      filename=wildnames[wild];
  153.                      switch(argv[names+1][0]) /* operation must follow filename */
  154.                      {
  155.                         case '+':
  156.                            for(ops=1;argv[names+1][ops]!=' ' && argv[names+1][ops]!='\0';ops++)
  157.                            {
  158.                               switch(argv[names+1][ops]) /* keeps looping until space or end of line */
  159.                               {
  160.                                  case 'r':
  161.                                  case 'R':
  162.                                     set_file_attrs(filename,READO);
  163.                                     break;
  164.                                  case 'a':
  165.                                  case 'A':
  166.                                     set_file_attrs(filename,ARCH);
  167.                                     break;
  168.                                  case 'h':
  169.                                  case 'H':
  170.                                     set_file_attrs(filename,HIDE);
  171.                                     break;
  172.                                  case 's':
  173.                                  case 'S':
  174.                                     set_file_attrs(filename,SYS);
  175.                                     break;
  176.                               }
  177.                            }
  178.                            break;
  179.                         case '-':
  180.                            for(ops=1;argv[names+1][ops]!=' ' && argv[names+1][ops]!='\0';ops++)
  181.                            {
  182.                               switch(argv[names+1][ops])
  183.                               {
  184.                                  case 'r':
  185.                                  case 'R':
  186.                                     reset_file_attrs(filename,READO);
  187.                                     break;
  188.                                  case 'a':
  189.                                  case 'A':
  190.                                     reset_file_attrs(filename,ARCH);
  191.                                     break;
  192.                                  case 'h':
  193.                                  case 'H':
  194.                                     reset_file_attrs(filename,HIDE);
  195.                                     break;
  196.                                  case 's':
  197.                                  case 'S':
  198.                                     reset_file_attrs(filename,SYS);
  199.                                     break;
  200.                               }
  201.                            }
  202.                            break;
  203.                      }
  204.                      get_file_attrs(filename); /* echo atrs back */
  205.                      wild++;
  206.                   }
  207.                }
  208.                else
  209.                {
  210.                   printf("Insufficient memory to create filelist.\n");
  211.                   exit(0);
  212.                }
  213.             }
  214.          }
  215.          else
  216.          {
  217.             filename=argv[1];
  218.             get_file_attrs(filename);
  219.          }
  220.       }
  221.    }
  222.    else
  223.    {
  224.       printf("Usage:AT filename1 (+ or -)RSAH filename2 (+ or -)RSAH etc..\n");
  225.       printf("where filenamex is any valid DOS filename.ext.\n");
  226.       printf("Wildcards are fully supported.\n");
  227.    }
  228. /* No real need to free up memory here, DOS will do that for us. */
  229. /* but if you don't believe me, run chkdsk before and after to satisfy. */
  230. }
  231.