xref: /onnv-gate/usr/src/lib/libcmd/common/logname.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1992-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                                                                      *
204887Schin ***********************************************************************/
214887Schin #pragma prototyped
224887Schin /*
234887Schin  * David Korn
244887Schin  * AT&T Research
254887Schin  *
264887Schin  * logname
274887Schin  */
284887Schin 
294887Schin static const char usage[] =
304887Schin "[-?\n@(#)$Id: logname (AT&T Research) 1999-04-30 $\n]"
314887Schin USAGE_LICENSE
324887Schin "[+NAME?logname - return the user's login name]"
334887Schin "[+DESCRIPTION?\blogname\b writes the users's login name to standard "
344887Schin 	"output.  The login name is the string that is returned by the "
354887Schin 	"\bgetlogin\b(2) function.  If \bgetlogin\b(2) does not return "
364887Schin 	"successfully, the corresponding to the real user id of the calling "
374887Schin 	"process is used instead.]"
384887Schin 
394887Schin "\n"
404887Schin "\n\n"
414887Schin "\n"
424887Schin "[+EXIT STATUS?]{"
434887Schin         "[+0?Successful Completion.]"
444887Schin         "[+>0?An error occurred.]"
454887Schin "}"
464887Schin "[+SEE ALSO?\bgetlogin\b(2)]"
474887Schin ;
484887Schin 
494887Schin 
504887Schin #include <cmd.h>
514887Schin 
524887Schin int
b_logname(int argc,char ** argv,void * context)534887Schin b_logname(int argc, char** argv, void* context)
544887Schin {
554887Schin 	register char*	logname;
564887Schin 
574887Schin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
584887Schin 	for (;;)
594887Schin 	{
604887Schin 		switch (optget(argv, usage))
614887Schin 		{
624887Schin 		case ':':
634887Schin 			error(2, "%s", opt_info.arg);
644887Schin 			continue;
654887Schin 		case '?':
664887Schin 			error(ERROR_usage(2), "%s", opt_info.arg);
674887Schin 			continue;
684887Schin 		}
694887Schin 		break;
704887Schin 	}
714887Schin 	if (error_info.errors)
724887Schin 		error(ERROR_usage(2), "%s", optusage(NiL));
734887Schin 	if (!(logname = getlogin()))
744887Schin 		logname = fmtuid(getuid());
754887Schin 	sfputr(sfstdout, logname, '\n');
764887Schin 	return 0;
774887Schin }
784887Schin 
79