Wait for Signal

When compiled, the following program will wait on Ctrl+D or Ctrl+C after which the program will print out which key-combination has been pressed and the program will terminate.

#include <exec/types.h>
#include <dos/dos.h>
 
#include <clib/exec_protos.h>
 
#include <stdio.h>
 
int main(void) {
 
    /* Wait for CTRL+C or CTRL+D */
    switch(Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D)) {
        case SIGBREAKF_CTRL_C:
            printf("Received CTRL+C\n");
            break;
        case SIGBREAKF_CTRL_D:
            printf("Received CTRL+D\n");
            break;
    }
 
    return 0;    
}

Get Base Path

AmigaOS benefits from the FilePart and PathPart functions that are able to retrieve the name of the file, respectively the name of the file with a / pre-prended. Unfortunately, that is not the behaviour one would expect from PathPart since more than likely you are looking for something that would grab the directory or the drive of the path. For example, you want a function that will take a path such as:

SYS:S/Startup-Sequence

and return SYS:S - however, that is not something that PathPart can do.

The wasBasePart function below will perform that operation:

/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
/* Takes as input a path and returns the directory containing that path. */
UBYTE *wasBasePart(UBYTE *path) {
    UBYTE *buffer;
    UBYTE *filePart;
    UBYTE *baseName;
    ULONG pathSize;
    ULONG partSize;
    ULONG position = 0;
 
    pathSize = strlen(path) * sizeof(UBYTE *) + 1;
 
    /* Get the file part which we need to ignore. */
    filePart = FilePart(path);
    partSize = strlen(filePart) * sizeof(UBYTE *) + 1;
 
    /* Allocate memory for the base name. */
    if((baseName = 
        AllocMem(
            strlen(path) * sizeof(UBYTE *) - strlen(filePart) + 1, 
            MEMF_ANY|MEMF_CLEAR
        )
    ) == NULL) return NULL;
 
    /* Allocate memory for path part. */
    if((buffer = AllocMem(pathSize, MEMF_ANY|MEMF_CLEAR)) == NULL) 
        return NULL;
 
    /* Strip pieces of the path and compose them. */
    do {
        position = SplitName(path, '/', buffer, position, pathSize);
 
        switch(position == -1 ||
            strlen(buffer) == 0 || 
            strncmp(buffer, filePart, partSize) == 0) {
 
            case TRUE: break; /* We have found the file part. */
            default: /* Otherwise, copy the buffer into the base path. */
                sprintf(baseName + strlen(baseName), "%s/", buffer);
                break;
        }
 
    } while(position != -1);
 
    switch(strlen(baseName)) {
        case 0: /* Attempt to build a drive path. */
            if(SplitName(path, ':', buffer, 0, pathSize) != -1)
                sprintf(baseName + strlen(baseName), "%s:", buffer);
            break;
        default: /* Otherwise, strip trailing slash. */
            baseName[strlen(baseName)-1] = '\0';
            break;
    }
 
    return baseName;
}

The wasBasePart function returns the empty-string in case the supplied path has no ancestor.