RCS_ID_C="$Id: whoami.c,v 1.3 1994/02/12 04:39:41 ppessi Exp $";
/*
 * whoami.c ---
 *
 * Author: ppessi <Pekka.Pessi@hut.fi>
 *
 * Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>,
 *		    Helsinki University of Technology, Finland.
 *		    All rights reserved.
 *
 * Created      : Wed Jan 12 01:40:38 1994 ppessi
 * Last modified: Sat Feb 12 03:39:20 1994 ppessi
 */

#include "whoami_rev.h"

static const char version[] = VERSTAG " "
  "Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>\n"
  "Helsinki University of Technology, Finland.\n";

/****** utilities/whoami ***************************************************

   NAME
        whoami - prints effective current user id

   VERSION
        $Id: whoami.c,v 1.3 1994/02/12 04:39:41 ppessi Exp $

   TEMPLATE
        whoami

   FUNCTION
        Whoami prints your effective user id.  It works even if you are
        su'd.

   RETURN VALUE
        Whoami return WARN, if the user id has got no user name associated.

   SEE ALSO
        id

*****************************************************************************
*
*/


#include <exec/types.h>
#include <exec/ports.h>
#include <exec/semaphores.h>
#include <exec/memory.h>
#include <exec/execbase.h>

#include <dos/dos.h>
#include <dos/dosextens.h>

#ifdef __SASC
#include <clib/exec_protos.h>
extern struct ExecBase* SysBase;
#include <pragmas/exec_sysbase_pragmas.h>
#include <proto/dos.h>
#include <proto/usergroup.h>
#else
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/usergroup_protos.h>
#include <libraries/usergroup.h>
#endif

#include <devices/sana2.h>
#include <net/sana2errno.h>
#include <string.h>

#define USERGROUPVERSION 1	/* minimum version to use */

#ifndef MAXLINELENGTH
#define MAXLINELENGTH 1024
#endif

LONG whoami(void)
{
  LONG retval = 128;
  struct DosLibrary *DOSBase;
  struct ExecBase *SysBase;

  SysBase = *(struct ExecBase**)4;
  DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L);

  if (DOSBase) {
    struct Library *UserGroupBase =
      OpenLibrary(USERGROUPNAME, USERGROUPVERSION);

    if (UserGroupBase != NULL) {
      uid_t uid = geteuid();
      struct passwd *pw = getpwuid(uid);

      if (pw != NULL) {
	Printf("%s\n", pw->pw_name);
	retval = RETURN_OK;
      } else {
	Printf("whoami: no login associated with uid %lu.\n", uid);
	retval = RETURN_WARN;
      }

      CloseLibrary(UserGroupBase);
    } else {
      Printf("whoami: cannot open %s\n", USERGROUPNAME);
    }

    CloseLibrary((struct Library *)DOSBase);
  }

  return retval;
}
