
/*
** File: getsh.c
** Author: Chris McCarthy (cmccarth@dullahan.rtc-cork.ie)
** Dist: Freeware, free to modify this program for your own needs.
**
** Program which prints the shell of the owner of the current task.
** Usefull for scripts which need to check which shell the user uses.
** e.g., in my S:Shell-Startup, I have the following lines
**
** IF `getsh` EQ "unix:usr/local/bin/csh"
**    unix:usr/local/bin/csh
**    endcli
** ENDIF
*/

#include <stdio.h>
#include <stdlib.h>

#include <proto/exec.h>
#include <proto/multiuser.h>
#include <libraries/multiuser.h>

main()
{
	struct muBase *muBase;
	struct muUserInfo *info;

	if (!(muBase = (struct muBase *)OpenLibrary(MULTIUSERNAME, MULTIUSERVERSION)))
	{
			/* Multiuser not installed ?? */
		printf("Cannot open %s v%d\n", MULTIUSERNAME, MULTIUSERVERSION);
		exit(ERROR_INVALID_RESIDENT_LIBRARY);
	}

	info = muAllocUserInfo();		/* Allocate memory for userinfo struct */
	if (!info)
	{
		printf("Out of memory\n");
		exit(EXIT_FAILURE);
	}
		/* Only want uid (not gid) */
	info->uid = (muGetTaskOwner(NULL) & muMASK_UID) >> 16;

		/* Get userinfo for the uid of the current task */
	info = muGetUserInfo(info, muKeyType_uid);
	if (!info)
	{
		puts("Cannot get user info.");
		exit(EXIT_FAILURE);
	}

	printf("%s\n", info->Shell);
	CloseLibrary((struct Library *)muBase);
	return 0;
}

