home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 3 / FREEWARE.BIN / ms_dos / attr / attr.c next >
Text File  |  1980-01-02  |  6KB  |  173 lines

  1. /*************************************************************************
  2.               ATTR.C     Ver.1.02
  3.  
  4.        ATTR [{+/-}{r/h/s/a}] [YY:MM:DD] [HH:MM:SS] <file name>
  5.  
  6.     このプログラムを C言語の勉強に使用しないでください。
  7.    但し、悪い見本で使用する場合は、この限りではありません。
  8.    若し、使用した場合は、すぐに忘れることをお勧めします。(^^;)
  9.  
  10. *************************************************************************/
  11.  
  12. #include <dos.h>
  13. #include <fcntl.h>
  14.  
  15. /* File attribute constants */
  16.  
  17. #define _A_NORMAL       0x00    /* Normal file - No read/write restrictions */
  18. #define _A_RDONLY       0x01    /* Read only file */
  19. #define _A_HIDDEN       0x02    /* Hidden file */
  20. #define _A_SYSTEM       0x04    /* System file */
  21. #define _A_VOLID        0x08    /* Volume ID file */
  22. #define _A_SUBDIR       0x10    /* Subdirectory */
  23. #define _A_ARCH         0x20    /* Archive file */
  24. /*
  25. struct find_t 
  26.     {
  27.     char reserved[21];
  28.     char attrib;
  29.     unsigned wr_time;
  30.     unsigned wr_date;
  31.     long size;
  32.     char name[13];
  33.     };*/
  34. struct find_t f;
  35.  
  36. void set_attr(char*);
  37. int val(char*);
  38. void guide(char*);
  39. /************************** main routine *********************************/
  40. main(int argc,char *argv[])
  41.  
  42. {
  43. char buf[50];
  44. int i=0,j=0,k=0,err=0;
  45. unsigned attrib,date,time;
  46.  
  47. if (argc==1)
  48.     {
  49.     guide(argv[0]);
  50.     return(0);
  51.     }
  52.  
  53. while (buf[k]=*(argv[argc-1]+k)) k++;
  54. while (k>=0 && buf[--k]!=0x5c && buf[k]!=0x3a);
  55. buf[++k]=0;
  56. printf("---------------+-----------------------------------------------------------\n");
  57. printf("     path      | %s\n",argv[argc-1]);
  58. printf("---------------+---------------------------+----------+----------+---------\n");
  59. printf("   file name   |         attribute         |   date   |   time   |   size\n");
  60. printf("---------------+---------------+-----------+----------+----------+---------\n");
  61.  
  62. while (1)
  63.     {
  64.     if (!j)  i=_dos_findfirst(argv[argc-1],0x3f,&f);
  65.     if (j++) i=_dos_findnext(&f);
  66.  
  67.     if (i) break;
  68.     for(i=0;buf[i+k]=*(f.name+i);i++);
  69.     buf[i+k]=0;
  70.     if (argc>2)
  71.         {
  72.         attrib=f.attrib;
  73.         date  =f.wr_date;
  74.         time  =f.wr_time;
  75.         for(err=0,i=2;i<argc;i++) set_attr(argv[i-1]);
  76.  
  77.         if (attrib!=f.attrib) if (err =_dos_setfileattr(buf,f.attrib))
  78.             {
  79.             f.attrib=attrib;
  80.             }
  81.         if (date!=f.wr_date || time!=f.wr_time)
  82.             {
  83.             if (_dos_open(buf,O_WRONLY,&i))
  84.                 {
  85.                 f.wr_date=date;
  86.                 f.wr_time=time;
  87.                 err++;
  88.                 }
  89.                 else
  90.                 err +=_dos_setftime(i,f.wr_date,f.wr_time);
  91.             _dos_close(i);
  92.             }
  93.         }
  94.  
  95.     printf("%14s",f.name);
  96.     printf("%c",err?'*':' ');
  97.     printf("|%s", f.attrib & _A_RDONLY ? "R/O" : "   " );
  98.     printf(" %s", f.attrib & _A_HIDDEN ? "Hid" : "   " );
  99.     printf(" %s", f.attrib & _A_SYSTEM ? "Sys" : "   " );
  100.     printf(" %s", f.attrib & _A_ARCH   ? "Arc" : "   " );
  101.     printf("|%s", f.attrib & _A_VOLID  ? "Vol" : "   " );
  102.     printf(" %s", f.attrib & _A_SUBDIR ? "Dir    " :"    Fil" );
  103.     printf("| %02d-%02d-%02d ",((f.wr_date>>9 & 127)+80)%100
  104.                   , f.wr_date>> 5 & 15
  105.                   , f.wr_date     & 31);
  106.     printf("| %02d:%02d:%02d ", f.wr_time>>11 & 31
  107.                   , f.wr_time>> 5 & 63
  108.                   ,(f.wr_time     & 31)* 2 );
  109.     (f.attrib & 0x18) ? printf("|********\n") : printf("|%8ld\n",f.size);
  110.     }
  111. printf("---------------+---------------+-----------+----------+----------+---------\n");
  112. printf("                                                                %3d file(s).\n",j-1);
  113. return (j-1);
  114. }
  115.  
  116. /******************* sub routine *******************/
  117.  
  118. void set_attr(char set[])
  119.  
  120. {
  121. int i;
  122.  
  123. if (set[0]=='+' || set[0]=='-') for (i=1;set[i]!=0;i++)
  124.     {
  125.     if (set[i] < 97) set[i] += 32;
  126.     if (set[0]=='-') f.attrib = ~f.attrib;
  127.     if (set[i]=='s') f.attrib |= _A_SYSTEM;
  128.     if (set[i]=='r') f.attrib |= _A_RDONLY;
  129.     if (set[i]=='h') f.attrib |= _A_HIDDEN;
  130.     if (set[i]=='a') f.attrib |= _A_ARCH;
  131.     if (set[0]=='-') f.attrib = ~f.attrib;
  132.     }
  133. if (set[2]=='-' && set[5]=='-')
  134.     f.wr_date=(((val(&set[0])+20)%100)<<9)+(val(&set[3])<<5)+val(&set[6]);
  135. if (set[2]==':' && set[5]==':')
  136.     f.wr_time=(val(&set[0])<<11)+(val(&set[3])<<5)+val(&set[6])/2;
  137. }
  138.  
  139. int val(char *a)
  140.  
  141. {
  142. int b=0;
  143.  
  144. while (*a>='0' && *a<='9') b=b*10+(int)(*a++)-48;
  145. return (b);
  146. }
  147.  
  148. void guide(char *name)
  149.  
  150. {
  151. printf("+----------------------------------------------------------------------------+\n");
  152. printf("|                  ATTRIBUTE  EXCHANGER        ver 1.02    |\n");
  153. printf("+----------------------------------------------------------------------------+\n");
  154. printf("|                                                                            |\n");
  155. printf("|  path :  %-66s|\n",name);
  156. printf("| option:          [{+|-}{r|h|s|a}・・・] [YY-MM-DD] [HH:MM:SS] <ファイル名>  |\n");
  157. printf("|                                                                            |\n");
  158. printf("|      ファイル名(含ディレクトリ)で指定されたファイルの                    |\n");
  159. printf("|                       属性、日付、時刻を変更、また、表示 を行います。      |\n");
  160. printf("+----------------------------------------------------------------------------+\n");
  161. printf(" | +   以降に指定したファイルの属性を設定します。  |\n");
  162. printf(" | -   以降に指定したファイルの属性を削除します。  |\n");
  163. printf(" |                                                 |\n");
  164. printf(" | r   読出のみ可能              [ R/O ] Read only |\n");
  165. printf(" | h   隠しファイル              [ Hid ] Hidden    |\n");
  166. printf(" | s   システムファイル          [ Sys ] System    |\n");
  167. printf(" | a   書き込みされたファイル    [ Arc ] Archives  |\n");
  168. printf(" |     ボリューム・ラベル        [ Vol ] Volume    |\n");
  169. printf(" |     サブ・ディレクトリ        [ Dir ] Directly  |\n");
  170. printf(" |     ファイル                  [ Fil ] File      |\n");
  171. printf(" +-------------------------------------------------+\n");
  172. }
  173.