home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 294.lha / SuperInfo / SuperInfo.c < prev    next >
C/C++ Source or Header  |  1989-10-08  |  14KB  |  465 lines

  1. #include <Functions.h>
  2. #include <Stdio.h>
  3. #include <CType.h>
  4. #include <Exec/Exec.h>
  5. #include <Intuition/Intuition.h>
  6. #include <Libraries/Dos.h>
  7. #include <Libraries/DosExtens.h>
  8. #include <Libraries/FileHandler.h>
  9.  
  10. #define PHYSICAL_DEVICE      0L
  11.  
  12. #define SHUT_IT_ALL_DOWN     0L
  13. #define INFO_ALLOC_ERROR     1L
  14. #define BLOCK_ALLOC_ERROR    2L
  15. #define INTUITION_OPEN_ERROR 3L
  16. #define GRAPHICS_OPEN_ERROR  4L
  17. #define WINDOW_OPEN_ERROR    5L
  18.  
  19. #define WINDOW_WIDTH      640L
  20. #define WINDOW_HEIGHT     100L
  21.  
  22. #define PEN_0             0L
  23. #define PEN_1             1L
  24. #define PEN_2             2L
  25. #define PEN_3             3L
  26.  
  27. extern struct DosLibrary *DOSBase;
  28.  
  29. struct Library      *IntuitionBase;
  30. struct Library      *GfxBase;
  31. struct IntuiMessage *Message;
  32. struct RastPort     *raster;
  33. struct Window       *Info_Window;
  34. struct NewWindow     Info_Window_Def =
  35.    {
  36.       (SHORT)0,             (SHORT)0,
  37.       (SHORT)WINDOW_WIDTH,  (SHORT)WINDOW_HEIGHT,
  38.       (UBYTE)PEN_0,         (UBYTE)PEN_1,
  39.       (ULONG)(CLOSEWINDOW),
  40.       (ULONG)(WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE | RMBTRAP),
  41.       NULL,                 NULL,
  42.       (UBYTE *)"Super Info",
  43.       NULL,                 NULL,
  44.       (SHORT)WINDOW_WIDTH,  (SHORT)WINDOW_HEIGHT,
  45.       (SHORT)WINDOW_WIDTH,  (SHORT)WINDOW_HEIGHT,
  46.       (USHORT)WBENCHSCREEN
  47.    } ;
  48.  
  49. struct DevList
  50.    {
  51.       unsigned char  *Name;
  52.       struct DevList *Next;
  53.    } ;
  54.  
  55. struct Lock            *DeviceLock;
  56. struct InfoData        *DeviceInfo;
  57. struct FileInfoBlock   *DeviceBlock;
  58.  
  59. main(arc, arv)
  60. int  arc;
  61. char *arv[];
  62.    {
  63.       void FreeDeviceList(),
  64.            DrawHeading(),
  65.            ProcessDevice(),
  66.            DetailPrint(),
  67.            ShutDown();
  68.  
  69.       struct DevList *FindAllDeviceNames();
  70.       struct DevList *BuildFromArgs();
  71.       struct DevList *FindLastDevice();
  72.  
  73.       struct Process    *ThisTask;
  74.       struct Window     *OldWindowPtr;
  75.       struct DevList    *FirstDevice;
  76.       struct DevList    *Temp;
  77.       unsigned char      DeviceData[72];
  78.       unsigned long int  class,
  79.                          success,
  80.                          StopProgram = FALSE;
  81.       unsigned short int run_program = TRUE,
  82.                          info_available,
  83.                          loop,
  84.                          code,
  85.                          TotalDevices = 0,
  86.                          counter;
  87.  
  88.       DeviceInfo  = (struct InfoData *)AllocMem( (sizeof(struct InfoData) ),
  89.                                                 MEMF_CLEAR);
  90.       if (DeviceInfo == NULL)
  91.          ShutDown(INFO_ALLOC_ERROR);
  92.  
  93.       DeviceBlock = (struct FileInfoBlock *)AllocMem( (sizeof(struct FileInfoBlock) ),
  94.                                                 MEMF_CLEAR);
  95.       if (DeviceBlock == NULL)
  96.          ShutDown(BLOCK_ALLOC_ERROR);
  97.  
  98.       IntuitionBase = (struct Library *)OpenLibrary("intuition.library", LIBRARY_VERSION);
  99.       if (IntuitionBase == NULL)
  100.          ShutDown(INTUITION_OPEN_ERROR);
  101.  
  102.       GfxBase = (struct Library *)OpenLibrary("graphics.library", LIBRARY_VERSION);
  103.       if (GfxBase == NULL)
  104.          ShutDown(GRAPHICS_OPEN_ERROR);
  105.  
  106.       if(arc < 2)
  107.          FirstDevice = FindAllDeviceNames();
  108.       else
  109.          FirstDevice = BuildFromArgs(arv, arc);
  110.  
  111.       Temp = FirstDevice;
  112.       do
  113.          {
  114.             TotalDevices++;
  115.             Temp = Temp->Next;
  116.          }
  117.       while (Temp != NULL);
  118.  
  119.       Info_Window_Def.Height = ( (TotalDevices + 2L) * 8L) + 4L;
  120.  
  121.       Info_Window = (struct Window *)OpenWindow(&Info_Window_Def);
  122.       if(Info_Window == NULL)
  123.          ShutDown(WINDOW_OPEN_ERROR);
  124.  
  125.       ThisTask               = (struct Process *)FindTask(0L);
  126.       OldWindowPtr           = (struct Window *)ThisTask->pr_WindowPtr;
  127.       ThisTask->pr_WindowPtr = (APTR)-1L;
  128.  
  129.       raster = (struct RastPort *)Info_Window->RPort;
  130.       DrawHeading(FirstDevice);
  131.  
  132.       while (StopProgram == FALSE)
  133.          {
  134.             Message = (struct IntuiMessage *)GetMsg(Info_Window->UserPort);
  135.             class      = Message->Class;
  136.             code       = Message->Code;
  137.  
  138.             if(Message != 0)
  139.                ReplyMsg(Message);
  140.  
  141.             switch (class)
  142.                {
  143.                   case CLOSEWINDOW: StopProgram = TRUE;
  144.                                     break;
  145.                }
  146.  
  147.             counter = 1;
  148.             Temp = FirstDevice;
  149.             do
  150.                {
  151.                   Forbid();
  152.                   ProcessDevice(Temp->Name, DeviceData);
  153.                   Permit();
  154.                   DetailPrint(DeviceData, counter);
  155.                   counter++;
  156.                   Temp = Temp->Next;
  157.                }
  158.             while (Temp != NULL);
  159.             Delay(10L);
  160.          }
  161.  
  162.       ShutDown(SHUT_IT_ALL_DOWN);
  163.       FreeDeviceList(FirstDevice);
  164.       ThisTask->pr_WindowPtr = (APTR)OldWindowPtr;
  165.  
  166.    }
  167.  
  168. void ProcessDevice(DeviceName, DataLine)
  169. unsigned char DeviceName[];
  170. unsigned char DataLine[];
  171.    {
  172.       void ProcessIOErr();
  173.  
  174.       long int Error;
  175.       long int StorageSize;
  176.       long int Percent;
  177.  
  178.       float PercentFloat;
  179.  
  180.       unsigned char StorageSizeString[5];
  181.       unsigned char PercentString[5];
  182.       unsigned char StateString[11];
  183.       unsigned char VolumeString[29];
  184.  
  185.       DeviceLock = (struct Lock*)Lock( (char *)DeviceName, (long)ACCESS_READ);
  186.       if (DeviceLock == NULL)
  187.          {
  188.             Error = IoErr();
  189.             ProcessIOErr(Error, DataLine);
  190.             return;
  191.          }
  192.  
  193.       Info(DeviceLock, DeviceInfo);
  194.       Examine(DeviceLock, DeviceBlock);
  195.  
  196.       PercentFloat = (float)DeviceInfo->id_NumBlocksUsed / (float)DeviceInfo->id_NumBlocks
  197.                  * (float)100;
  198.       Percent = PercentFloat;
  199.       sprintf(PercentString, "%d%c", Percent, '%');
  200.  
  201.       strncpy(VolumeString, &DeviceBlock->fib_FileName[0], 28);
  202.  
  203.       if ( (strlen(&DeviceBlock->fib_FileName[0]) ) > 28);
  204.          {
  205.             VolumeString[27] = '~';
  206.             VolumeString[28] = NULL;
  207.          }
  208.  
  209.       StorageSize = DeviceInfo->id_NumBlocks / 2;
  210.       if (StorageSize >= 1024)
  211.          {
  212.             StorageSize = StorageSize / 1024;
  213.             sprintf(StorageSizeString, "%3dM", StorageSize);
  214.          }
  215.       else
  216.          sprintf(StorageSizeString, "%3dK", StorageSize);
  217.  
  218.       switch (DeviceInfo->id_DiskState)
  219.          {
  220.             case ID_WRITE_PROTECTED: sprintf(StateString, "%s", "Read Only");
  221.                                      break;
  222.             case ID_VALIDATED:       sprintf(StateString, "%s", "Read/Write");
  223.                                      break;
  224.             case ID_VALIDATING:      sprintf(StateString, "%s", "Validating");
  225.                                      break;
  226.          }
  227.  
  228.       sprintf(DataLine, "%4s  %5d  %5d  %4s %5d %-11s %-28s",
  229.                          StorageSizeString, DeviceInfo->id_NumBlocksUsed,
  230.                          (DeviceInfo->id_NumBlocks - DeviceInfo->id_NumBlocksUsed),
  231.                          PercentString, DeviceInfo->id_NumSoftErrors,
  232.                          StateString, VolumeString);
  233.  
  234.       UnLock(DeviceLock);
  235.    }
  236.  
  237. void ProcessIOErr(ErrNumber, ErrorReport)
  238. long int ErrNumber;
  239. unsigned char ErrorReport[];
  240.    {
  241.       switch (ErrNumber)
  242.          {
  243.             case ERROR_OBJECT_IN_USE:
  244.                sprintf(ErrorReport, "%-71s", "Object in use");
  245.                break;
  246.             case ERROR_DEVICE_NOT_MOUNTED:
  247.                sprintf(ErrorReport, "%-71s", "Device not mounted");
  248.                break;
  249.             case ERROR_NOT_A_DOS_DISK:
  250.                sprintf(ErrorReport, "%-71s", "Not a DOS disk");
  251.                break;
  252.             case ERROR_NO_DISK:
  253.                sprintf(ErrorReport, "%-71s", "No disk present");
  254.                break;
  255.             default:
  256.                sprintf(ErrorReport, "%s #%ld", "Undocumented DOS error", ErrNumber);
  257.                break;
  258.          }
  259.    }
  260.  
  261. struct DevList *FindAllDeviceNames()
  262.    {
  263.  
  264.       void *malloc();
  265.       unsigned char *calloc();
  266.  
  267.       struct RootNode   *DOSRoot;
  268.       struct DosInfo    *InfoRoot;
  269.       struct DeviceNode *DeviceRoot;
  270.       struct DevList    *Temp  = NULL;
  271.       struct DevList    *First = NULL;
  272.       struct DevList    *Last  = NULL;
  273.       struct DevList    DevListSize;
  274.  
  275.       unsigned char      *TempDeviceName;
  276.       unsigned char       DeviceName[256];
  277.       unsigned long int   DeviceNameLength;
  278.       unsigned short int  counter;
  279.  
  280.       Forbid(); /* Turn off task switching */
  281.  
  282.       DOSRoot    = (struct RootNode *)DOSBase->dl_Root;
  283.       InfoRoot   = (struct DosInfo *)BADDR(DOSRoot->rn_Info);
  284.       DeviceRoot = (struct DeviceNode *)BADDR(InfoRoot->di_DevInfo);
  285.  
  286.       while (DeviceRoot != NULL)
  287.          {
  288.             TempDeviceName   = (unsigned char *)BADDR(DeviceRoot->dn_Name);
  289.             DeviceNameLength = (unsigned long int)DeviceName[0];
  290.  
  291.             if ( (DeviceRoot->dn_Type == PHYSICAL_DEVICE) &&
  292.                  (DeviceRoot->dn_Task != NULL)            &&
  293.                  (DeviceRoot->dn_Name != 0) )
  294.                {
  295.                   counter = 0;
  296.                   DeviceName[counter] = TempDeviceName[counter + 1L];
  297.                   while(DeviceName[counter] != NULL)
  298.                      {
  299.                         counter++;
  300.                         DeviceName[counter] = TempDeviceName[counter + 1];
  301.                      }
  302.  
  303.                   DeviceName[counter++] = ':';
  304.                   DeviceName[counter]   = NULL;
  305.  
  306.                   Temp = (struct DevList *)malloc( (sizeof(DevListSize) ) );
  307.                   Temp->Next = NULL;
  308.                   Temp->Name = (unsigned char *)calloc(strlen(DeviceName) + 1, sizeof(char) );
  309.                   sprintf(Temp->Name, "%s", DeviceName);
  310.  
  311.                   if (First == NULL)
  312.                      {
  313.                         Temp->Next = NULL;
  314.                         First      = Temp;
  315.                         Last       = First;
  316.                      }
  317.                   else
  318.                      {
  319.                         Temp->Next = NULL;
  320.                         Last->Next = Temp;
  321.                         Last       = Temp;
  322.                      }
  323.                }
  324.             DeviceRoot = (struct DeviceNode *)BADDR(DeviceRoot->dn_Next);
  325.          }
  326.  
  327.       Permit();
  328.       return(First);
  329.  
  330.    }
  331.  
  332. struct DevList *BuildFromArgs(Arg, ArgNum)
  333. char *Arg[];
  334. int   ArgNum;
  335.    {
  336.       void *malloc();
  337.       unsigned char *calloc();
  338.  
  339.       struct DevList *First = NULL;
  340.       struct DevList *Last  = NULL;
  341.       struct DevList *Temp  = NULL;
  342.       struct DevList DevListSize;
  343.  
  344.       unsigned short int counter = 1;
  345.       unsigned short int letter;
  346.  
  347.       while (counter < ArgNum)
  348.          {
  349.             Temp       = (struct DevList *)malloc( (sizeof(DevListSize) ) );
  350.             Temp->Next = NULL;
  351.  
  352.             letter = 0;
  353.             while(Arg[counter][letter] != NULL)
  354.                {
  355.                   Arg[counter][letter] = toupper(Arg[counter][letter]);
  356.                   letter++;
  357.                }
  358.  
  359.             if (Arg[counter][ (strlen(Arg[counter] ) - 1) ] != ':')
  360.                {
  361.                   Temp->Name = (unsigned char *)calloc(strlen(Arg[counter] ) + 2, sizeof(char) );
  362.                   sprintf(Temp->Name, "%s:", Arg[counter]);
  363.                }
  364.             else
  365.                {
  366.                   Temp->Name = (unsigned char *)calloc(strlen(Arg[counter] ) + 1, sizeof(char) );
  367.                   sprintf(Temp->Name, "%s", Arg[counter]);
  368.                }
  369.  
  370.             if (First == NULL)
  371.                {
  372.                   Temp->Next = NULL;
  373.                   First      = Temp;
  374.                   Last       = First;
  375.                }
  376.             else
  377.                {
  378.                   Temp->Next = NULL;
  379.                   Last->Next = Temp;
  380.                   Last       = Temp;
  381.                }
  382.  
  383.             counter++;
  384.          }
  385.  
  386.       return(First);
  387.  
  388.    }
  389.  
  390. void FreeDeviceList(Items)
  391. struct DevList *Items;
  392.    {
  393.       struct DevList *Temp;
  394.       struct DevList *Temp2;
  395.  
  396.       Temp = Items;
  397.       while (Temp != NULL)
  398.          {
  399.             free(Temp->Name);
  400.             Temp2 = Temp->Next;
  401.             free(Temp);
  402.             Temp = Temp2;
  403.          }
  404.    }
  405.  
  406. void ShutDown(Error_Level)
  407. long int Error_Level;
  408.    {
  409.       switch (Error_Level)
  410.          {
  411.             case SHUT_IT_ALL_DOWN:     CloseWindow(Info_Window);
  412.             case WINDOW_OPEN_ERROR:    CloseLibrary(GfxBase);
  413.             case GRAPHICS_OPEN_ERROR:  CloseLibrary(IntuitionBase);
  414.             case INTUITION_OPEN_ERROR: FreeMem(DeviceBlock, sizeof(struct FileInfoBlock) );
  415.             case BLOCK_ALLOC_ERROR:    FreeMem(DeviceInfo, sizeof(struct InfoData) );
  416.                                        break;
  417.          }
  418.  
  419.       switch (Error_Level)
  420.          {
  421.             case WINDOW_OPEN_ERROR:    printf("Window open error.\n");
  422.                                        exit(WINDOW_OPEN_ERROR);
  423.             case GRAPHICS_OPEN_ERROR:  printf("Graphics open error.\n");
  424.                                        exit(GRAPHICS_OPEN_ERROR);
  425.             case INTUITION_OPEN_ERROR: printf("Intuition open error.\n");
  426.                                        exit(INTUITION_OPEN_ERROR);
  427.             case BLOCK_ALLOC_ERROR:    printf("Block allocation error.\n");
  428.                                        exit(BLOCK_ALLOC_ERROR);
  429.             case INFO_ALLOC_ERROR:     printf("Info allocation error.\n");
  430.                                        exit(INFO_ALLOC_ERROR);
  431.          }
  432.    }
  433.  
  434. void DrawHeading(Dev)
  435. struct DevList *Dev;
  436.    {
  437.       struct DevList *Temp;
  438.       unsigned short int counter = 1;
  439.  
  440.       SetAPen(raster, PEN_2);
  441.       Move(raster, 4L, 17L);
  442.       Text(raster, "Unit  Size   Used   Free  Full  Errs   Status    Name",
  443.           (long)(strlen("Unit  Size   Used   Free  Full  Errs   Status    Name") ) );
  444.  
  445.       SetAPen(raster, PEN_1);
  446.       Temp = Dev;
  447.  
  448.       do
  449.          {
  450.             Move(raster, 4L, (long)(17 + (counter * 8) ) );
  451.             Text(raster, Temp->Name, (long)(strlen(Temp->Name) ) );
  452.             counter++;
  453.             Temp = Temp->Next;
  454.          }
  455.       while (Temp != NULL);
  456.  
  457.    }
  458.  
  459. void DetailPrint(Information, Line)
  460. unsigned char Information[];
  461. unsigned short int Line;
  462.    {
  463.       Move (raster, 52L, (long)(17 + (Line * 8) ) );
  464.       Text(raster, Information, (long)(strlen(Information) ) );
  465.    }