* K&R function cleanup
[dragonfly.git] / usr.sbin / cron / cron / user.c
1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
2  * All rights reserved
3  *
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.
12  *
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
16  *
17  * $FreeBSD: src/usr.sbin/cron/cron/user.c,v 1.8 1999/08/28 01:15:50 peter Exp $
18  * $DragonFly: src/usr.sbin/cron/cron/user.c,v 1.3 2003/11/16 11:51:14 eirikn Exp $
19  */
20
21 /* vix 26jan87 [log is in RCS file]
22  */
23
24
25 #include "cron.h"
26
27 static char *User_name;
28
29 void
30 free_user(user *u)
31 {
32         entry   *e, *ne;
33
34         free(u->name);
35         for (e = u->crontab;  e != NULL;  e = ne) {
36                 ne = e->next;
37                 free_entry(e);
38         }
39         free(u);
40 }
41
42 static void
43 log_error(char *msg)
44 {
45         log_it(User_name, getpid(), "PARSE", msg);
46 }
47
48 user *
49 load_user(int crontab_fd, struct passwd *pw, char *name)
50 /* pw == NULL implies syscrontab */
51 {
52         char    envstr[MAX_ENVSTR];
53         FILE    *file;
54         user    *u;
55         entry   *e;
56         int     status;
57         char    **envp, **tenvp;
58
59         if (!(file = fdopen(crontab_fd, "r"))) {
60                 warn("fdopen on crontab_fd in load_user");
61                 return NULL;
62         }
63
64         Debug(DPARS, ("load_user()\n"))
65
66         /* file is open.  build user entry, then read the crontab file.
67          */
68         if ((u = (user *) malloc(sizeof(user))) == NULL) {
69                 errno = ENOMEM;
70                 return NULL;
71         }
72         if ((u->name = strdup(name)) == NULL) {
73                 free(u);
74                 errno = ENOMEM;
75                 return NULL;
76         }
77         u->crontab = NULL;
78
79         /* 
80          * init environment.  this will be copied/augmented for each entry.
81          */
82         if ((envp = env_init()) == NULL) {
83                 free(u->name);
84                 free(u);
85                 return NULL;
86         }
87
88         /*
89          * load the crontab
90          */
91         while ((status = load_env(envstr, file)) >= OK) {
92                 switch (status) {
93                 case ERR:
94                         free_user(u);
95                         u = NULL;
96                         goto done;
97                 case FALSE:
98                         User_name = u->name;    /* for log_error */
99                         e = load_entry(file, log_error, pw, envp);
100                         if (e) {
101                                 e->next = u->crontab;
102                                 u->crontab = e;
103                         }
104                         break;
105                 case TRUE:
106                         if ((tenvp = env_set(envp, envstr))) {
107                                 envp = tenvp;
108                         } else {
109                                 free_user(u);
110                                 u = NULL;
111                                 goto done;
112                         }
113                         break;
114                 }
115         }
116
117  done:
118         env_free(envp);
119         fclose(file);
120         Debug(DPARS, ("...load_user() done\n"))
121         return u;
122 }