#include <exec/types.h>
#include <libraries/dos.h>
#include <proto/dos.h>
#include <string.h>
#include <stdlib.h>

char *getenv(char *envvar)
{
    char *envfile, *name;
    BPTR fh;
    int end;

    envfile = malloc(strlen(envvar) + 5);
    if (envfile == NULL) return NULL;

    strcpy(envfile, "env:");
    strcat(envfile, envvar);

    fh = Open(envfile, MODE_OLDFILE);
    free(envfile);
    if (fh == NULL) return NULL;

    Seek(fh, 0, OFFSET_END);
    end = Seek(fh, 0, OFFSET_BEGINNING);

    name = malloc(end + 1);
    if (name == NULL) {
	Close(fh);
	return NULL;
    }

    Read(fh, name, end);
    name[end] = '\0';
    Close(fh);
    return name;
}
