1 /*
2  * userdata.c -- Get data on users (from /etc/passwd).
3  * Copyright (c) 1999 Akim Demaille, Miguel Santana
4  */
5 
6 /*
7  * This file is part of a2ps.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; see the file COPYING.  If not, write to
21  * the Free Software Foundation, 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24 
25 #include "system.h"
26 #include "xalloc.h"
27 #include "userdata.h"
28 
29 char *stpcpy PARAMS ((char *dest, const char *src));
30 
31 #if !HAVE_PWD_H
32 struct passwd
33 {
34   const char *pw_name;
35   const char *pw_gecos;
36   const char *pw_dir;
37 };
38 #endif
39 
40 /* Free the memory hold by UDATA, but not UDATA itself. */
41 void
userdata_free(struct userdata * udata)42 userdata_free (struct userdata *udata)
43 {
44   XFREE (udata->login);
45   XFREE (udata->name);
46   XFREE (udata->comments);
47   XFREE (udata->home);
48 }
49 
50 void
userdata_get(struct userdata * udata)51 userdata_get (struct userdata *udata)
52 {
53   struct passwd *passwd = NULL;
54   const char *home, *login;
55   char *comments = NULL, *name = NULL, *cp;
56 
57 #if HAVE_GETPWUID
58   passwd = getpwuid (getuid ());
59 #endif
60 
61   /* Home dir. */
62   if ((cp = getenv ("HOME")))
63     home = cp;
64   else if (passwd && passwd->pw_dir)
65     home = passwd->pw_dir;
66   else
67     home = "/home";
68 
69   /* Login. */
70   if ((cp = getenv ("LOGNAME")))
71     login = cp;
72   else if ((cp = getenv ("USERNAME")))
73     login = cp;
74   else if (passwd && passwd->pw_name)
75     login = passwd->pw_name;
76   else
77     login = NULL;
78 
79   /* The field `pw_gecos' contains the full name and comments, such as
80      phone number etc. */
81 #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
82   if (passwd && passwd->pw_gecos)
83     {
84       char *gecos = NULL;
85       if (login)
86 	{
87 	  /* Occurences of `&' in the gecos should be substituted with
88 	     the login (first letter in uppercase). */
89 	  size_t amps = 0;	/* Number of ampersands. */
90 	  char *cp2;
91 
92 	  for (cp = passwd->pw_gecos; *cp; cp++)
93 	    if (*cp == '&')
94 	      amps++;
95 	  gecos = ALLOCA (char,
96 			  (strlen (passwd->pw_gecos)
97 			   + amps * (strlen (login) - 1) + 1));
98 
99 	  cp2 = gecos;
100 
101 	  for (cp = passwd->pw_gecos; *cp; cp++)
102 	    if (*cp == '&')
103 	      {
104 		*cp2++ = toupper (*login);
105 		cp2 = stpcpy (cp2, login + 1);
106 	      }
107 	    else
108 	      *cp2++ = *cp;
109 	  *cp2 = '\0';
110 	}
111       else
112 	{
113 	  gecos = ALLOCA (char, strlen (passwd->pw_gecos) + 1);
114 	  stpcpy (gecos, passwd->pw_gecos);
115 	}
116 
117       /* The gecos may have subfields seperated by `,'.  Consider the
118 	 first field to be the user name, and the rest to be
119 	 comments. */
120       name = gecos;
121       if ((comments = strchr (gecos, ',')))
122 	{
123 	  *comments = '\0';
124 	  comments++;
125 	}
126     }
127 #endif
128 
129   udata->login = xstrdup (login ? login : _("user"));
130   udata->name = xstrdup (name ? name : _("Unknown User"));
131   udata->comments = comments ? xstrdup (comments) : NULL;
132   udata->home = xstrdup (home ? home : "/home");
133 }
134