File: various.c

package info (click to toggle)
casu 3.3.3-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 272 kB
  • ctags: 206
  • sloc: ansic: 2,446; makefile: 158
file content (165 lines) | stat: -rw-r--r-- 3,725 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 *  CaSU - communications & status utilities.
 *  Copyright (C) 1992, 1993, 1994 Luke Mewburn <lm@rmit.edu.au>
 *	incorporating:
 *	   flon - lists your friends who are logged on.
 *	   to - send a short message to a friend
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "casu.h"

/*
 * 	errmesg
 * print a message to stderr, in a consistant format.
 * output is of the form:
 *	progname: 'm2' - m1.\n		# m2 is ! NULL
 *	progname: m1.\n			# m2 is NULL
 */
void
errmesg(m1, m2)
    char *m1, *m2;
{
    fprintf(stderr, "%s: ", progname);
    if (m2)
	fprintf(stderr, "'%s' - ", m2);
    fprintf(stderr, "%s.\n", m1);
} /* errmesg */


/*
 * 	errexit
 *
 * use errmesg to print a message and exit the program.
 */
void
errexit(m1, m2)
    char *m1, *m2;
{
    errmesg(m1, m2);
    exit(1);
} /* errexit */


/*
 * 	strnc0py
 *
 * as strncpy, but always put in the \0 (assume array is siz+1 long)
 */
char *
strnc0py(s1, s2, n)
    char *s1, *s2;
    size_t n;
{
    s1[n] = '\0';
    return strncpy(s1, s2, n);
} /* strnc0py */


/*
 *	get_username
 *
 * find out the username of the user...
 */
char *
get_username()
{
    struct passwd	*pwbufr;
    char		*myname;
    int			my_uid;
    static char		namebuf[UT_NAMESIZE+1];

    myname=getlogin();
    if (myname == NULL)		/* attempt to get name another way */
    {
	my_uid = (int) getuid();
	if (pw_list != NULL)
	{
	    int lp;
	    for (lp = 0; lp < pw_count; lp++)
		if (pw_list[lp].uid == my_uid)
		    break;
	    if (lp != pw_count)
		myname = pw_list[lp].username;
	}
	else	/* don't use inbuilt passwd file - USE_GETPWENT does this too */
	{
	    if ((pwbufr = getpwuid(my_uid)) != NULL)
		myname = pwbufr->pw_name;
	}
    }
    if (myname == NULL)
	myname = "";
    strnc0py(namebuf, myname, UT_NAMESIZE);
    return (namebuf);
} /* get_username */


/*
 *	convert_realname
 *
 * converts the GCOS field at tfb to a full name, replacing
 * occurances of `&' with name. If uid matches the last request,
 * it will return the last converted entry (cache!). It returns
 * a pointer to an internal static buffer
 */
char *
convert_realname(tfb, name, uid)
    char *tfb, *name;
    int  uid;
{
    static char	realnam[BUFSIZ + 1];
    static int	last_uid = -1;

    char	*namep, *final;
    int		len, namelen;

    if (last_uid == uid)
	return realnam;
    final = strchr(tfb, ',');
    if (final != NULL)
	*final = '\0';
    if (strchr(tfb, '&') == NULL)
	return tfb;
    final = tfb;
    namelen = strlen(name) - 1;
    for (namep=tfb, len=1; *namep && len < BUFSIZ ; namep++, len++)
    {
	if (*namep=='&')
	{
	    if (len + namelen >= BUFSIZ)
		break;		/* safety net for copy below */
	    len += namelen;
	}
    }
    final = namep;
    for (namep=tfb, len=0; namep < final ; namep++, len++)
    {
	if (*namep=='&')
	{
	    strcpy(&realnam[len], name);
	    if (islower(realnam[len]))
		realnam[len] = toupper(realnam[len]);
	    len += namelen;
	}
	else
	    realnam[len] = *namep;
    }
    realnam[len]='\0';
    last_uid = uid;
    return realnam;
} /* convert_realname */