🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.micro.pc
Article #7280

Re: Simple Environment Question

#7280
From: Bennett E. Todd III
Date: Tue, 15 Jul 1986 21:45
75 lines
3081 bytes
Here's a getenv() in C source that I wrote for DeSmet. It differs from
the UNIX function in that it malloc()s space for the returned string,
rather than just returning a pointer into a copy of the environment (if
I were to write it again I'd write it differently). For any other
compiler, all you should need is to find a way to get a pointer to the
PSP (Program Segment Prefix) to replace my call to _showcs() (which
returns the segment pointer to the .EXE load point, 0x100 bytes or 0x10
paragraphs past the PSP), and to give it an _peek() function that takes
a pointer and a segment and returns the byte found there.

--- cut here -- *not* a shell archive; save as getenv.c ---
#include <stdio.h>
/*
 * PD rewrite of UNIX subroutine of the same name
 * for DeSmet C
 * by Bennett Todd
 * differs from the UNIX utility in that it malloc()s space for
 * the string to return
 */

char *getenv(name)
char *name;
{
	char *found, *malloc();
	char *offset, *tmp, _peek(), toupper();
	int i;
	unsigned segment, _showcs();

	/* PSP is at CS-10H */
	segment = _showcs() - 0x10;

	/*
	** The environment is stored starting at a segment whose segment address
	** can be found 2Ch into the PSP -- a word address...
	*/
	segment = _peek(0x2c, segment) + 256*_peek(0x2d, segment);

	/* Look for the string NAME= in the environment */
	for (offset=NULL; _peek(offset, segment) != NULL; offset++) {
		i = 0; /* i scans through name */
		while (name[i] != NULL &&
			   toupper(_peek(offset, segment)) == toupper(name[i])) {
			i++;
			offset++;
		}
		if (name[i] == NULL && _peek(offset, segment) == '=') {
			/* then we found it -- get it and return it */
			tmp = ++offset;
			while (_peek(tmp, segment) != NULL)
				tmp++;
			found = malloc(1 + tmp-offset);
			if (found == NULL) {
				fprintf(stderr, "getenv: malloc failed. Bye....\n");
				exit(1);
			}
			tmp = found;
			while ((*tmp++ = _peek(offset++, segment)) !=NULL)
				/* NULL BODY */;
			return(found);
		}
		/* We didn't found it -- clean up and try next entry */
		while (_peek(offset, segment) != NULL)
			offset++;
	}
	/* Ain't no sech beastie. Sorry, boss... */
	return((char *) NULL);
}
---------------------- end of getenv.c -------------------------------
Enjoy!
-Bennett
-- 

Bennett Todd -- Duke Computation Center, Durham, NC 27706-7756; (919) 684-3695
UUCP: ...{decvax,seismo,philabs,ihnp4,akgua}!mcnc!ecsvax!duccpc!bet
Technical Details
Message-ID: <1820@ecsvax.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!linus!philabs!mcnc!ecsvax!bet
References: <10800008@mcomp>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!linus!philabs!mcnc!ecsvax!bet
From: bet@ecsvax.UUCP (Bennett E. Todd III)
Newsgroups: net.micro.pc
Subject: Re: Simple Environment Question
Message-ID: <1820@ecsvax.UUCP>
Date: Tue, 15-Jul-86 17:45:11 EDT
Article-I.D.: ecsvax.1820
Posted: Tue Jul 15 17:45:11 1986
Date-Received: Wed, 16-Jul-86 07:51:07 EDT
References: <10800008@mcomp>
Reply-To: duccpc!bet@ecsvax.UUCP (Bennett E. Todd III)
Distribution: net
Organization: Duke University Computation Center
Lines: 75
Keywords: getenv(), DeSmet C
Summary: getenv() source for DeSmet C