home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / DOpus4-GPL / Program / main12.c < prev    next >
C/C++ Source or Header  |  2000-01-27  |  11KB  |  388 lines

  1. /*
  2.  
  3. Directory Opus 4
  4. Original GPL release version 4.12
  5. Copyright 1993-2000 Jonathan Potter
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. All users of Directory Opus 4 (including versions distributed
  22. under the GPL) are entitled to upgrade to the latest version of
  23. Directory Opus version 5 at a reduced price. Please see
  24. http://www.gpsoft.com.au for more information.
  25.  
  26. The release of Directory Opus 4 under the GPL in NO WAY affects
  27. the existing commercial status of Directory Opus 5.
  28.  
  29. */
  30.  
  31. #include "DOpus.h"
  32.  
  33. void setupchangestate()
  34. {
  35.     int unit;
  36.     struct IOStdReq *diskreq;
  37.  
  38.     disk_change_state=0;
  39.     for (unit=0;unit<4;unit++) {
  40.         if (diskreq=(struct IOStdReq *)LCreateExtIO(general_port,sizeof(struct IOStdReq))) {
  41.             if (!(OpenDevice("trackdisk.device",unit,(struct IORequest *)diskreq,0))) {
  42.                 diskreq->io_Command=TD_CHANGESTATE;
  43.                 DoIO((struct IORequest *)diskreq);
  44.                 if (!diskreq->io_Actual) disk_change_state|=1<<unit;
  45.                 CloseDevice((struct IORequest *)diskreq);
  46.             }
  47.             LDeleteExtIO((struct IORequest *)diskreq);
  48.         }
  49.     }
  50. }
  51.  
  52. #define ITEM_NUM 11
  53.  
  54. static short
  55.     diskinfo_heads[]={
  56.         STR_DISKINFO_DEVICE,
  57.         STR_DISKINFO_NAME,
  58.         STR_DISKINFO_SIZE,
  59.         STR_DISKINFO_USED,
  60.         STR_DISKINFO_FREE,
  61.         STR_DISKINFO_PERCENT,
  62.         STR_DISKINFO_DENSITY,
  63.         STR_DISKINFO_ERRORS,
  64.         STR_DISKINFO_STATUS,
  65.         STR_DISKINFO_DATE,
  66.         STR_DISKINFO_FILESYSTEM};
  67.  
  68. void dodiskinfo(path)
  69. char *path;
  70. {
  71.     struct RastPort *dirp;
  72.     int a,b,c,isd=-1,cx,cy,fail=0,xoffset,yoffset,titwidth;
  73.     static char disktxt[11][60],buf[256],formstr[80],diskname[40],dname[10],buf1[40];
  74.     float f;
  75.     struct DateStamp ds;
  76.     struct MsgPort *prt;
  77.     struct InfoData __aligned infodata;
  78.     struct Gadget contgad;
  79.     char *gad_gads[2];
  80.     BPTR lock;
  81.     UBYTE old_change_state;
  82.     ULONG class;
  83.     USHORT code;
  84.     struct DOpusRemember *key=NULL;
  85.  
  86.     if (!path || !path[0]) return;
  87.  
  88.     cx=scr_font[FONT_REQUEST]->tf_XSize;
  89.     cy=scr_font[FONT_REQUEST]->tf_YSize;
  90.  
  91.     for (a=0;a<ITEM_NUM;a++) {
  92.         disktxt[a][0]='-';
  93.         disktxt[a][1]=0;
  94.     }
  95.     main_proc->pr_WindowPtr=(APTR)-1;
  96. getnewdisk:
  97.     strcpy(diskname,path);
  98.     if (getroot(diskname,&ds)) {
  99.         strcat(diskname,":");
  100.         if ((prt=(struct MsgPort *)DeviceProc(diskname))) {
  101.             if (lock=Lock(diskname,ACCESS_READ)) {
  102.                 Forbid();
  103.                 get_device_task(lock,disktxt[0],prt);
  104.  
  105.                 for (a=0;a<4;a++) {
  106.                     lsprintf(dname,"DF%ld:",a);
  107.                     if (LStrCmpI(disktxt[0],dname)==0) {
  108.                         isd=a;
  109.                         break;
  110.                     }
  111.                 }
  112.                 Permit();
  113.                 Info(lock,&infodata);
  114.                 UnLock(lock);
  115.                 switch (infodata.id_DiskType) {
  116.                     case ID_UNREADABLE_DISK:
  117.                         strcpy(disktxt[1],globstring[STR_UNREADABLE_DISK]);
  118.                         break;
  119.                     case ID_NOT_REALLY_DOS:
  120.                         strcpy(disktxt[1],globstring[STR_NOT_A_DOS_DISK]);    
  121.                         break;
  122.                     case ID_KICKSTART_DISK:
  123.                         strcpy(disktxt[1],globstring[STR_KICKSTART_DISK]);
  124.                         break;
  125.                     default:
  126.                         strcpy(buf,path);
  127.                         strcpy(disktxt[1],diskname);
  128.                         disktxt[1][(strlen(disktxt[1])-1)]=0;
  129.                         a=infodata.id_NumBlocks*infodata.id_BytesPerBlock;
  130.                         lsprintf(buf,"%ld",a); b=strlen(buf);
  131.                         lsprintf(buf,"%ld",infodata.id_NumBlocks);
  132.                         lsprintf(formstr,"%%%ldld %s; %%%ldld %s; %%s",
  133.                             b,globstring[STR_DISKINFO_BYTES],
  134.                             strlen(buf),globstring[STR_DISKINFO_BLOCKS]);
  135.                         getsizestring(buf,a);
  136.                         lsprintf(disktxt[2],formstr,a,infodata.id_NumBlocks,buf);
  137.                         a=infodata.id_NumBlocksUsed*infodata.id_BytesPerBlock;
  138.                         getsizestring(buf,a);
  139.                         lsprintf(disktxt[3],formstr,a,infodata.id_NumBlocksUsed,buf);
  140.                         a=(infodata.id_NumBlocks-infodata.id_NumBlocksUsed)*infodata.id_BytesPerBlock;
  141.                         getsizestring(buf,a);
  142.                         lsprintf(disktxt[4],formstr,a,(infodata.id_NumBlocks-infodata.id_NumBlocksUsed),buf);
  143.                         if (infodata.id_NumBlocks==0) f=100;
  144.                         else f=((float)(infodata.id_NumBlocksUsed*100)/(float)infodata.id_NumBlocks);
  145.                         getfloatstr(f,buf); getfloatstr((float)(100-f),buf1);
  146.                         lsprintf(disktxt[5],"%s%% %s; %s%% %s",
  147.                             buf,globstring[STR_DISKINFO_FULL],
  148.                             buf1,globstring[STR_DISKINFO_SFREE]);
  149.                         lsprintf(disktxt[6],"%ld %s",
  150.                             infodata.id_BytesPerBlock,
  151.                             globstring[STR_DISKINFO_BYTESPERBLOCK]);
  152.                         lsprintf(disktxt[7],"%ld",infodata.id_NumSoftErrors);
  153.                         switch (infodata.id_DiskState) {
  154.                             case ID_VALIDATING:
  155.                                 strcpy(disktxt[8],globstring[STR_VALIDATING]);
  156.                                 break;
  157.                             case ID_WRITE_PROTECTED:
  158.                                 strcpy(disktxt[8],globstring[STR_WRITE_PROTECTED]);
  159.                                 break;
  160.                             default:
  161.                                 strcpy(disktxt[8],globstring[STR_READ_WRITE]);
  162.                                 break;
  163.                         }
  164.                         seedate(&ds,disktxt[9],0);
  165.                         disktxt[10][0]=(char)((infodata.id_DiskType>>24)&0xff);
  166.                         disktxt[10][1]=(char)((infodata.id_DiskType>>16)&0xff);
  167.                         disktxt[10][2]=(char)((infodata.id_DiskType>>8)&0xff);
  168.                         disktxt[10][3]=(char)(infodata.id_DiskType&0xff)+'0';
  169.                         disktxt[10][4]=0;
  170.                         switch (infodata.id_DiskType) {
  171.                             case ID_DOS_DISK:
  172.                                 strcat(disktxt[10]," (OFS)");
  173.                                 break;
  174.                             case ID_FFS_DISK:
  175.                                 strcat(disktxt[10]," (FFS)");
  176.                                 break;
  177.                             case ID_INTER_DOS_DISK:
  178.                                 strcat(disktxt[10]," (I-OFS)");
  179.                                 break;
  180.                             case ID_INTER_FFS_DISK:
  181.                                 strcat(disktxt[10]," (I-FFS)");
  182.                                 break;
  183.                             case ID_FASTDIR_DOS_DISK:
  184.                                 strcat(disktxt[10]," (DC-OFS)");
  185.                                 break;
  186.                             case ID_FASTDIR_FFS_DISK:
  187.                                 strcat(disktxt[10]," (DC-FFS)");
  188.                                 break;
  189.                             case ID_MSDOS_DISK:
  190.                                 strcat(disktxt[10]," (MS-DOS)");
  191.                                 break;
  192.                         }
  193.                         break;
  194.                 }
  195.             }
  196.             else {
  197.                 a=IoErr();
  198.                 doerror(a);
  199.                 switch (a) {
  200.                     case ERROR_DEVICE_NOT_MOUNTED:
  201.                         strcpy(disktxt[1],globstring[STR_DEVICE_NOT_MOUNTED]);
  202.                         break;
  203.                     case ERROR_NOT_A_DOS_DISK:
  204.                         strcpy(disktxt[1],globstring[STR_NOT_A_DOS_DISK]);
  205.                         break;
  206.                     case ERROR_NO_DISK:
  207.                         strcpy(disktxt[1],globstring[STR_NO_DISK_IN_DRIVE]);
  208.                         break;
  209.                     default:
  210.                         lsprintf(disktxt[1],globstring[STR_DOS_ERROR_CODE],a);
  211.                         break;
  212.                 }
  213.             }
  214.         }
  215.         else fail=1;
  216.     }
  217.     else fail=1;
  218.  
  219.     if (config->errorflags&ERROR_ENABLE_DOS) main_proc->pr_WindowPtr=(APTR)Window;
  220.     if (fail) {
  221.         doerror(IoErr());
  222.         return;
  223.     }
  224.  
  225.     b=0; titwidth=0;
  226.     for (a=0;a<ITEM_NUM;a++) {
  227.         if ((c=strlen(disktxt[a]))>b) b=c;
  228.         if ((c=strlen(globstring[diskinfo_heads[a]]))>titwidth) titwidth=c;
  229.     }
  230.  
  231.     disk_win.Width=((b+titwidth+3)*cx)+16;
  232.     disk_win.Height=(ITEM_NUM*(cy+1))+19;
  233.     if (config->generalscreenflags&SCR_GENERAL_REQDRAG) {
  234.         disk_win.Flags=WFLG_RMBTRAP|WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_DEPTHGADGET;
  235.         xoffset=Window->WScreen->WBorLeft;
  236.         yoffset=Window->WScreen->WBorTop+Window->WScreen->Font->ta_YSize+1;
  237.         disk_win.Width+=xoffset+Window->WScreen->WBorRight;
  238.         disk_win.Height+=yoffset+Window->WScreen->WBorBottom;
  239.         disk_win.Title="DiskInfo";
  240.     }
  241.     else {
  242.         disk_win.Flags=WFLG_BORDERLESS|WFLG_RMBTRAP|WFLG_ACTIVATE;
  243.         xoffset=2;
  244.         yoffset=1;
  245.         disk_win.Width+=4;
  246.         disk_win.Height+=2;
  247.         disk_win.Title=NULL;
  248.     }
  249.     centerwindow(&disk_win);
  250.  
  251.     contgad.NextGadget=NULL;
  252.     contgad.Width=(strlen(globstring[STR_CONTINUE])+4)*cx;
  253.     contgad.Height=cy+5;
  254.     contgad.LeftEdge=(disk_win.Width-contgad.Width)/2;
  255.     contgad.TopEdge=disk_win.Height-cy-10;
  256.     contgad.Flags=GFLG_GADGHCOMP;
  257.     contgad.Activation=GACT_RELVERIFY;
  258.     contgad.GadgetType=GTYP_BOOLGADGET;
  259.     contgad.GadgetRender=NULL;
  260.     contgad.SelectRender=NULL;
  261.     contgad.GadgetText=NULL;
  262.     contgad.MutualExclude=0;
  263.     contgad.SpecialInfo=NULL;
  264.     contgad.GadgetID=0;
  265.  
  266.     StrCombine(buf,"_",globstring[STR_CONTINUE],256);
  267.     gad_gads[0]=buf;
  268.     gad_gads[1]=NULL;
  269.  
  270.     if (!(fontwindow=OpenWindow(&disk_win))) return;
  271.  
  272.     dirp=fontwindow->RPort;
  273.     setupwindreq(fontwindow);
  274.     SetFont(dirp,scr_font[FONT_REQUEST]);
  275.     for (a=0;a<ITEM_NUM;a++) {
  276.         Move(dirp,xoffset+8,yoffset+5+(a*cy)+scr_font[FONT_REQUEST]->tf_Baseline);
  277.         Text(dirp,globstring[diskinfo_heads[a]],strlen(globstring[diskinfo_heads[a]]));
  278.         Move(dirp,xoffset+8+(titwidth*cx),yoffset+5+(a*cy)+scr_font[FONT_REQUEST]->tf_Baseline);
  279.         Text(dirp," : ",3);
  280.         Text(dirp,disktxt[a],strlen(disktxt[a]));
  281.     }
  282.     AddGadgetBorders(&key,
  283.         &contgad,1,
  284.         screen_pens[config->gadgettopcol].pen,screen_pens[config->gadgetbotcol].pen);
  285.     AddGadgets(fontwindow,
  286.         &contgad,
  287.         gad_gads,
  288.         1,
  289.         screen_pens[config->gadgettopcol].pen,
  290.         screen_pens[config->gadgetbotcol].pen,1);
  291.     a=0;
  292.  
  293.     FOREVER {
  294.         while (IMsg=(struct IntuiMessage *)GetMsg(fontwindow->UserPort)) {
  295.             class=IMsg->Class; code=IMsg->Code;
  296.             ReplyMsg((struct Message *)IMsg);
  297.             switch (class) {
  298.                 case IDCMP_DISKREMOVED:
  299.                     setupchangestate();
  300.                     break;
  301.                 case IDCMP_DISKINSERTED:
  302.                     if (isd!=-1) {
  303.                         old_change_state=disk_change_state;
  304.                         setupchangestate();
  305.                         if ((old_change_state&(1<<isd))!=(disk_change_state&(1<<isd))) {
  306.                             a=2;
  307.                             break;
  308.                         }
  309.                     }
  310.                     break;
  311.                 case IDCMP_VANILLAKEY:
  312.                     if (code!='\r' && LToLower(code)!=LToLower(globstring[STR_CONTINUE][0]))
  313.                         break;
  314.                     SelectGadget(fontwindow,&contgad);
  315.                 case IDCMP_GADGETUP:
  316.                     a=1;
  317.                     break;
  318.             }
  319.         }
  320.         if (a) break;
  321.         Wait(1<<fontwindow->UserPort->mp_SigBit);
  322.     }
  323.     CloseWindow(fontwindow);
  324.     fontwindow=NULL;
  325.     LFreeRemember(&key);
  326.     if (a==2) goto getnewdisk;
  327. }
  328.  
  329. void get_device_task(lock,buffer,port)
  330. BPTR lock;
  331. char *buffer;
  332. struct MsgPort *port;
  333. {
  334.     struct DeviceList *devlist,*dl;
  335.     struct RootNode *rootnode;
  336.     struct DosInfo *dosinfo;
  337.     struct FileLock *lock2;
  338.  
  339.     rootnode=(struct RootNode *) DOSBase->dl_Root;
  340.     dosinfo=(struct DosInfo *) BADDR(rootnode->rn_Info);
  341.     devlist=(struct DeviceList *) BADDR(dosinfo->di_DevInfo);
  342.     lock2=(struct FileLock *)BADDR(lock);
  343.     dl=(struct DeviceList *)BADDR(lock2->fl_Volume);
  344.  
  345.     while (devlist) {
  346.         if (devlist->dl_Type==DLT_DEVICE && devlist->dl_Task==dl->dl_Task) break;
  347.         devlist=(struct DeviceList *) BADDR(devlist->dl_Next);
  348.     }
  349.     if (devlist) BtoCStr((BPTR)devlist->dl_Name,buffer,31);
  350.     else strcpy(buffer,((struct Task *)port->mp_SigTask)->tc_Node.ln_Name);
  351.     strcat(buffer,":");
  352. }
  353.  
  354. void getsizestring(buf,a)
  355. char *buf;
  356. ULONG a;
  357. {
  358.     a/=1024;
  359.     if (a>1073741824) lsprintf(buf,"HUGE");
  360.     else if (a>1048576) {
  361.         getfloatstr((double)((double)a/1048576),buf);
  362.         strcat(buf,"G");
  363.     }
  364.     else if (a>1024) {
  365.         getfloatstr((double)((double)a/1024),buf);
  366.         strcat(buf,"M");
  367.     }
  368.     else lsprintf(buf,"%ldK",a);
  369. }
  370.  
  371. void getfloatstr(f,buf)
  372. double f;
  373. char *buf;
  374. {
  375.     int a,b,c,d;
  376.     char buf1[20];
  377.  
  378.     a=(int)f; f-=a;
  379.     b=(int)(f*100);
  380.     c=(b/10)*10; d=b-c;
  381.     if (d>4) c+=10;
  382.     if (c==100) {
  383.         c=0; ++a;
  384.     }
  385.     lsprintf(buf1,"%ld",c); buf1[1]=0;
  386.     lsprintf(buf,"%ld.%s",a,buf1);
  387. }
  388.