Sync usage with man page.
[netbsd-mini2440.git] / libexec / cron / user.c
blob0779884d39a3e864577d3442d1b27e423757f405
1 /* Copyright 1988,1990,1993 by Paul Vixie
2 * All rights reserved
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
11 * user.
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
18 #if !defined(lint) && !defined(LINT)
19 static char rcsid[] = "$Id: user.c,v 1.1 1994/01/05 20:40:15 jtc Exp $";
20 #endif
22 /* vix 26jan87 [log is in RCS file]
26 #include "cron.h"
27 #include "externs.h"
30 void
31 free_user(u)
32 user *u;
34 entry *e, *ne;
35 char **env;
37 for (e = u->crontab; e != NULL; e = ne) {
38 ne = e->next;
39 free_entry(e);
41 for (env = u->envp; *env; env++)
42 (void) free(*env);
43 (void) free(u->envp);
44 (void) free(u);
48 user *
49 load_user(crontab_fd, name, uid, gid, dir, syscron)
50 int crontab_fd;
51 char *name;
52 int uid;
53 int gid;
54 char *dir;
55 int syscron; /* system crontab file? */
57 char envstr[MAX_ENVSTR];
58 FILE *file;
59 user *u;
60 entry *e;
61 int status;
63 if (!(file = fdopen(crontab_fd, "r"))) {
64 perror("fdopen on crontab_fd in load_user");
65 return NULL;
68 Debug(DPARS, ("load_user()\n"))
70 /* file is open. build user entry, then read the crontab file.
72 u = (user *) malloc(sizeof(user));
73 u->uid = uid;
74 u->gid = gid;
75 u->envp = env_init();
76 u->crontab = NULL;
79 * do auto env settings that the user could reset in the cron tab
81 sprintf(envstr, "SHELL=%s", _PATH_BSHELL);
82 u->envp = env_set(u->envp, envstr);
84 sprintf(envstr, "HOME=%s", dir);
85 u->envp = env_set(u->envp, envstr);
87 /* load the crontab
89 while ((status = load_env(envstr, file)) >= OK) {
90 if (status == TRUE) {
91 u->envp = env_set(u->envp, envstr);
92 } else {
93 if (NULL != (e = load_entry(file, NULL, syscron))) {
94 e->next = u->crontab;
95 u->crontab = e;
101 * do automatic env settings that should have precedence over any
102 * set in the cron tab.
104 sprintf(envstr, "%s=%s", "LOGNAME", name);
105 u->envp = env_set(u->envp, envstr);
106 #if defined(BSD)
107 sprintf(envstr, "%s=%s", "USER", name);
108 u->envp = env_set(u->envp, envstr);
109 #endif
112 * done. close file, return pointer to 'user' structure
114 fclose(file);
116 Debug(DPARS, ("...load_user() done\n"))
118 return u;