Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!rutgers!apple!agate!linus!nixbur!nixpbe!peun11!josef From: josef@nixpbe.UUCP (Moellers) Newsgroups: comp.unix.internals Subject: Re: How to know your current shell from C code Message-ID: Date: 12 Sep 90 10:17:36 GMT References: <1990Sep11.164436.9592@cadence.com> Sender: news@nixpbe.nixdorf.de Distribution: usa Lines: 39 In <1990Sep11.164436.9592@cadence.com> mikel@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 ... 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@nixbur.uucp | Abt. PXD-S14 | | !USA: mollers.pad@nixpbe.uucp | Heinz-Nixdorf-Ring | | Phone: (+49) 5251 104662 | D-4790 Paderborn |