Common AmigaOS Code Patterns

Getting a disk's free space

https://aminet.net/package/misc/fish/fish-0066 – free.c

snippet.c
#include <protos/dos.h>
 
BPTR lock;
struct InfoData info;
long bytes; // max 4 GB, might need more space, a 48 bit number might be best
 
disk = Lock(device, ACCESS_READ);
 
Info(disk, &info);
 
bytes = (info.id_NumBlocks - info.id_NumBlocksUsed) * info->id_BytesPerBlock;
 
UnLock(disk);

Getting the current system time

I've used this for seeding a random number generator.

snippet.c
#include <devices/timer.h>
#include <clib/timer_protos.h>
 
struct Library *TimerBase;
struct MsgPort *TimerMP;
struct timerequest *TimerIO;
 
int main(void) { 
  long randomSeed = 0;
  struct timeval currentSystemTime;
 
  if (TimerMP = CreatePort(NULL, NULL)) {
    if (TimerIO = (struct timerequest *)CreateExtIO(TimerMP, sizeof(struct timerequest))) {
       if (!(OpenDevice(
         TIMERNAME,
         UNIT_MICROHZ,
         (struct IORequest *)TimerIO,
         0
       ))) {
         TimerBase = (struct Library *)TimerIO->tr_node.io_Device;
 
         GetSysTime(&currentSystemTime);
 
         randomSeed = currentSystemTime.tv_micro;
 
         CloseDevice(TimerIO);
       }
 
       DeleteExtIO(TimerIO);
    }
 
    DeletePort(TimerMP);
  }
}

Generating a random number

snippet.c
#include <clib/alib_protos.h>
 
long semiRandom = FastRand(someSeed); // i actually needed to change the seed in my program a bunch of times...