add UNLEASHED_OBJ to unleashed.mk
[unleashed/tickless.git] / usr / src / cmd / cron / funcs.c
blobf907d439cb239ec046c93fc0088b97c4ea191dda
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/param.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <dirent.h>
38 #include <libintl.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <tzfile.h>
43 #include "cron.h"
45 #define CANTCD "can't change directory to the at directory"
46 #define NOREADDIR "can't read the at directory"
47 #define YEAR 1900
48 extern int audit_cron_is_anc_name(char *);
50 time_t
51 num(char **ptr)
53 time_t n = 0;
54 while (isdigit(**ptr)) {
55 n = n*10 + (**ptr - '0');
56 *ptr += 1; }
57 return (n);
61 static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
63 int
64 days_btwn(int m1, int d1, int y1, int m2, int d2, int y2)
67 * calculate the number of "full" days in between
68 * m1/d1/y1 and m2/d2/y2.
69 * NOTE: there should not be more than a year separation in the
70 * dates. also, m should be in 0 to 11, and d should be in 1 to 31.
73 int days;
74 int m;
76 if ((m1 == m2) && (d1 == d2) && (y1 == y2))
77 return (0);
78 if ((m1 == m2) && (d1 < d2)) {
80 * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not
81 * a leap year, this function should return the days till the
82 * the next Feb 29.See Bug 4257355.
84 if (d2 > days_in_mon(m2, y2)) {
85 int p;
86 for (p = 1; ! isleap(y2+YEAR+p); p++)
88 return (p*365 + d2-d1-1);
90 return (d2-d1-1);
92 /* the remaining dates are on different months */
93 days = (days_in_mon(m1, y1)-d1) + (d2-1);
94 m = (m1 + 1) % 12;
95 while (m != m2) {
96 if (m == 0)
97 y1++;
98 days += days_in_mon(m, y1);
99 m = (m + 1) % 12;
101 return (days);
105 days_in_mon(int m, int y)
108 * returns the number of days in month m of year y
109 * NOTE: m should be in the range 0 to 11
111 return (dom[m] + (((m == 1) && isleap(y + YEAR)) ? 1 : 0));
114 void *
115 xmalloc(size_t size)
117 char *p;
119 if ((p = malloc(size)) == NULL) {
120 perror("malloc");
121 exit(55);
123 return (p);
126 void *
127 xcalloc(size_t nElements, size_t size)
129 void *p;
131 if ((p = calloc(nElements, size)) == NULL) {
132 perror("calloc");
133 exit(55);
135 return (p);
138 char *
139 xstrdup(const char *str)
141 int len;
142 char *p;
144 len = strlen(str);
145 p = xmalloc(len + 1);
146 (void) memcpy(p, str, len);
147 p[len] = '\0';
149 return (p);
152 void
153 cron_sendmsg(char action, char *login, char *fname, char etype)
155 static int msgfd = -2;
156 struct message *pmsg, msgbuf;
157 int i;
159 (void) memset(&msgbuf, 0, sizeof (msgbuf));
160 pmsg = &msgbuf;
161 if (msgfd == -2) {
162 if ((msgfd = open(FIFO, O_WRONLY|O_NDELAY)) < 0) {
163 if (errno == ENXIO || errno == ENOENT)
164 (void) fprintf(stderr, gettext("cron may not"
165 " be running - call your system"
166 " administrator\n"));
167 else
168 (void) fprintf(stderr, gettext(
169 "error in message queue open\n"));
170 return;
173 pmsg->etype = etype;
174 pmsg->action = action;
175 (void) strlcpy(pmsg->fname, fname, FLEN);
176 (void) strlcpy(pmsg->logname, login, LLEN);
177 if ((i = write(msgfd, pmsg, sizeof (struct message))) < 0)
178 (void) fprintf(stderr, gettext("error in message send\n"));
179 else if (i != sizeof (struct message))
180 (void) fprintf(stderr, gettext(
181 "error in message send: Premature EOF\n"));
184 char
185 *errmsg(int errnum)
187 char *msg;
188 static char msg_buf[32];
190 msg = strerror(errnum);
192 if (msg == NULL) {
193 (void) snprintf(msg_buf, sizeof (msg_buf),
194 gettext("Error %d"), errnum);
195 return (msg_buf);
196 } else
197 return (msg);
201 filewanted(struct dirent *direntry)
203 char *p;
204 char c;
206 p = direntry->d_name;
207 (void) num(&p);
208 if (p == direntry->d_name)
209 return (0); /* didn't start with a number */
210 if (*p++ != '.')
211 return (0); /* followed by a period */
212 c = *p++;
213 if (c < 'a' || c > 'z')
214 return (0); /* followed by a queue name */
215 if (audit_cron_is_anc_name(direntry->d_name))
216 return (0);
217 return (1);
221 isvalid_shell(const char *shell)
223 char *t;
224 int ret = 0;
226 while ((t = getusershell()) != NULL) {
227 if (strcmp(t, shell) == 0) {
228 ret = 1;
229 break;
232 endusershell();
233 return (ret);
237 isvalid_dir(const char *dir)
239 char *cwd = getcwd(NULL, 0);
241 if (dir[0] != '/' || chdir(dir) == -1) {
242 return (0);
244 if (cwd != NULL) {
245 (void) chdir(cwd);
247 return (1);