How to know your current shell from C code

Moellers josef at nixpbe.UUCP
Wed Sep 12 20:17:36 AEST 1990


In <1990Sep11.164436.9592 at cadence.com> mikel at cadence.cadence.com writes:


>        Recently I had a UNIX question.  I have a c program and
>I want to know which shell(sh,csh,ksh) is my parent.  Is there
>any way to do this?  Environment variable is not acceptable.

Quick'n'dirty:

	# include	<stdio.h>
	...
	FILE *ps;
	char line[80], *shell;
	int ppid, pspid;
	...
	ppid = getppid()
	ps = popen("ps", "r");
	fgets(line, 80, ps);	/* skip "PID TTY TIME COMMAND" */
	shell = (char *) 0;
	while (fgets(line, 80, ps) != NULL)
	{
		pspid = atoi(line);
		if (pspid == ppid)
		{
			shell = line+22;
			break;
		}
	}
	shell[strlen(shell)-1] = '\0';
	if (shell == (char *) 0)
		printf("Huh??");
	else
		printf("Your friendly neighbourhood shell is \"%s\"\n", shell);

--
| Josef Moellers		|	c/o Nixdorf Computer AG	|
|  USA: mollers.pad at nixbur.uucp	|	Abt. PXD-S14		|
| !USA: mollers.pad at nixpbe.uucp	|	Heinz-Nixdorf-Ring	|
| Phone: (+49) 5251 104662	|	D-4790 Paderborn	|



More information about the Comp.unix.internals mailing list