summaryrefslogtreecommitdiff
path: root/dviware/quicspool/src/misc.c
blob: 1a6e60ed51245b2e4743d061a4a3054787efdd71 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef lint
static char *rcs = "$Header: misc.c,v 1.1 88/01/15 13:04:30 simpson Rel $";
#endif
/*
$Log:	misc.c,v $
 * Revision 1.1  88/01/15  13:04:30  simpson
 * initial release
 * 
 * Revision 0.1  87/12/11  18:31:03  simpson
 * beta test
 * 
*/

/* Miscellaneous routines */

#include <stdio.h>
#include <pwd.h>
#include <local/standard.h>
#include <ctype.h>
#include "constants.h"

/* Same as the tgetent of termcap(3x) but it works for the printcap
 * database.  Use the rest of the termcap(3x) routines after you use
 * this one.
 */
int pgetent(bp, name)
char	*bp;
char	*name;
{
    void    setenv();
    
    setenv("TERMCAP", "/etc/printcap");
    return tgetent(bp, name);
}

/* Changes the uid of the running process to that of `user'. If the `host' is
 * not equal to the current hostname, then a docile uid is switched to.  TRUE
 * is returned if the person selected in the password file exists and the uid
 * is switched, FALSE otherwise.
 */
Boolean setnewid(host, user)
char	*host, *user;
{
    char	    username[51];
    char	    hostname[51];
    char	    *strcpy();
    struct passwd   *passwd;

    (void)strcpy(username, user);
    (void)gethostname(hostname, 51);
    if (!EQ(hostname, host))
	(void)strcpy(username, DOCILEUSER);
    if (!(passwd = getpwnam(username)))
    	return FALSE;
    seteuid(passwd->pw_uid);
    setegid(passwd->pw_gid);
    return TRUE;
}

/* Gets the value of an environment variable just like getenv(3) but
 * instead it reads the value of the environment variable from the user's
 * .profile, .login or .cshrc file depending on what shell they are using.
 * This is useful for processes that are not forked by the user but still
 * wish to know the value of a user's environment variable.  The .cshrc
 * file is searched before the .login file.  The returned value is static
 * and the result is written over after each call as in getenv(3).  NULL is
 * returned if the user does not exist, the file is not readable or the
 * environment variable does not exist.  The string searched for should
 * begin with "<variable name>=" in .profile or "setenv <variable name> "
 * in the .cshrc or .login file.  Continuation lines are handled too.  If
 * the user name that is passed is NULL then the current effective user id
 * will used.
 */
char	*fgetenv(variable, user)
char	*variable;
char	*user;
{
    struct passwd   *passwd;
    static char	    envvariable[201];
    char	    s[201], line[201], *strcpy(), *strcat();
    FILE	    *f;
    Boolean	    continueline;
    Boolean	    bourne;
    Boolean	    dotcshrc = TRUE;

    if (!user) {
	if (!(passwd = getpwuid(geteuid())))
	    return NULL;
    } else
	if (!(passwd = getpwnam(user)))
	    return NULL;
    if (!passwd->pw_shell || strlen(passwd->pw_shell) == 0 || 
    passwd->pw_shell && (EQ(passwd->pw_shell, "/bin/sh") || 
    EQ(passwd->pw_shell, "/bin/ksh"))) {
	(void)sprintf(s, "%s/.profile", passwd->pw_dir);
	bourne = TRUE;
    } else {
	(void)sprintf(s, "%s/.cshrc", passwd->pw_dir);
	bourne = FALSE;
    }
onceagain:
    continueline = FALSE;
    if (!(f = fopen(s, "r")))
	if (bourne)
	    return NULL;
	else
	    goto trynextfile;
    while (fgets(line, 201, f)) {
	if (line[strlen(line) - 1] == '\n')
	    line[strlen(line) - 1] = '\0';
	if (bourne)
	    (void)sprintf(s, "%s=", variable);
	else
	    (void)sprintf(s, "setenv %s ", variable);
	/* Delete trailing blanks */
	while (strlen(line) > 0 && isspace(line[strlen(line) - 1]))
	    line[strlen(line) - 1] = '\0';
	if (continueline)
	    /* Check for \ on end of line */
	    if (line[strlen(line) - 1] == '\\') {
		line[strlen(line) - 1] = '\0';
		continueline = TRUE;
		(void)strcat(envvariable, line);
	    } else {
		(void)strcat(envvariable, line);
		(void)fclose(f);
		return envvariable;
	    }
	else if (EQN(line, s, strlen(s))) {
	    if (line[strlen(line) - 1] == '\\') {
		line[strlen(line) - 1] = '\0';
		(void)strcpy(envvariable, &line[strlen(s)]);
		continueline = TRUE;
	    } else {
		continueline = FALSE;
		(void)strcpy(envvariable, &line[strlen(s)]);
		(void)fclose(f);
		return envvariable;
	    }
	} else 
	    continue;
    }
    if (continueline) {
	(void)fclose(f);
	return envvariable;
    }
    (void)fclose(f);
trynextfile:
    if (!bourne && dotcshrc) {
	(void)sprintf(s, "%s/.login", passwd->pw_dir);
	dotcshrc = FALSE;
	goto onceagain;
    }
    return NULL;
}

void printbitmap(drawer, acter_set, egral, erceptor)
FILE *drawer;
char *acter_set;
int egral;
int erceptor;
{
    unsigned char red_human_bodies;	/* Heeeeere's Johnny...       */
    int erpret;				/*          -- Jack Nicholson */
    short cakes = (egral + 7) / 8;
    long john, silver;

    for (erpret = 0; erpret < erceptor; erpret++) {
	for (john = 0; john < cakes; john++)
	    if (john < cakes - 1)
		for (red_human_bodies = 0x80, silver = 0; silver < 8; 
		silver++, red_human_bodies >>= 1)
		    fprintf(drawer, "%c", *(acter_set + cakes * erpret + 
		    john) & red_human_bodies ? '*' : '.');
	    else
		for (red_human_bodies = 0x80, silver = 0; silver < (egral + 7)
		% 8 + 1; silver++, red_human_bodies >>= 1)
		    fprintf(drawer, "%c", *(acter_set + cakes * erpret + 
		    john) & red_human_bodies ? '*' : '.');
	fprintf(drawer, "\n");
    }
}