xref: /titanic_51/usr/src/lib/libcmd/common/logname.c (revision e5dcf7beb7c949f9234713d5818b581ec3825443)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                                                                      *
20 ***********************************************************************/
21 #pragma prototyped
22 /*
23  * David Korn
24  * AT&T Research
25  *
26  * logname
27  */
28 
29 static const char usage[] =
30 "[-?\n@(#)$Id: logname (AT&T Research) 1999-04-30 $\n]"
31 USAGE_LICENSE
32 "[+NAME?logname - return the user's login name]"
33 "[+DESCRIPTION?\blogname\b writes the users's login name to standard "
34 	"output.  The login name is the string that is returned by the "
35 	"\bgetlogin\b(2) function.  If \bgetlogin\b(2) does not return "
36 	"successfully, the corresponding to the real user id of the calling "
37 	"process is used instead.]"
38 
39 "\n"
40 "\n\n"
41 "\n"
42 "[+EXIT STATUS?]{"
43         "[+0?Successful Completion.]"
44         "[+>0?An error occurred.]"
45 "}"
46 "[+SEE ALSO?\bgetlogin\b(2)]"
47 ;
48 
49 
50 #include <cmd.h>
51 
52 int
53 b_logname(int argc, char** argv, void* context)
54 {
55 	register char*	logname;
56 
57 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
58 	for (;;)
59 	{
60 		switch (optget(argv, usage))
61 		{
62 		case ':':
63 			error(2, "%s", opt_info.arg);
64 			continue;
65 		case '?':
66 			error(ERROR_usage(2), "%s", opt_info.arg);
67 			continue;
68 		}
69 		break;
70 	}
71 	if (error_info.errors)
72 		error(ERROR_usage(2), "%s", optusage(NiL));
73 	if (!(logname = getlogin()))
74 		logname = fmtuid(getuid());
75 	sfputr(sfstdout, logname, '\n');
76 	return 0;
77 }
78 
79