1 /* crontab 1.2 - user crontab manipulation Author: Kees J. Bot
19 #if __minix && !__minix_vmd
20 #define seteuid(uid) ((uid),0) /* Minix can't fiddle with uids. */
23 static int opentab(int uid
, char *file
, int how
)
24 /* Open a crontab file under the given uid. How is 'r' or 'w'. Return
25 * the result of open(2).
32 case 'r': flags
= O_RDONLY
; break;
33 case 'w': flags
= O_WRONLY
| O_CREAT
| O_TRUNC
; break;
34 default: errno
= EINVAL
; return -1;
37 #if __minix && !__minix_vmd
38 /* Standard Minix has no saved uid, so use the lousy old access(). */
40 if (access(file
, how
== 'r' ? R_OK
: W_OK
) < 0) return -1;
46 r
= open(file
, flags
, 0666);
53 static void copytab(int fd_in
, char *file_in
, int fd_out
, char *file_out
)
54 /* Copy one open file to another. Complain and exit on errors. */
59 while ((r
= read(fd_in
, buf
, sizeof(buf
))) > 0) {
62 if ((r
= write(fd_out
, buf
+w
, r
-w
)) <= 0) {
64 "%s: Write error on %s: %s\n",
67 r
== 0 ? "End of file"
75 fprintf(stderr
, "%s: Read error on %s: %s\n",
76 prog_name
, file_in
, strerror(errno
));
81 static void usage(void)
84 "Usage: %s -c [user] file # Change crontab\n"
85 " %s -l [user] # List crontab\n"
86 " %s -r [user] # Remove crontab\n"
87 " %s -p # Tell cron to reload\n",
88 prog_name
, prog_name
, prog_name
, prog_name
);
92 int main(int argc
, char **argv
)
95 int cflag
, lflag
, rflag
, pflag
;
99 static char SPOOLDIR
[]= "/usr/spool/crontabs";
100 char tabfile
[sizeof(SPOOLDIR
) + NAME_MAX
];
102 prog_name
= strrchr(argv
[0], '/');
103 if (prog_name
== nil
) prog_name
= argv
[0]; else prog_name
++;
105 cflag
= lflag
= rflag
= pflag
= 0;
107 while (i
< argc
&& argv
[i
][0] == '-') {
108 char *opt
= argv
[i
++] + 1;
110 if (opt
[0] == '-' && opt
[1] == 0) break; /* -- */
112 while (*opt
!= 0) switch (*opt
++) {
113 case 'c': cflag
= 1; break;
114 case 'l': lflag
= 1; break;
115 case 'r': rflag
= 1; break;
116 case 'p': pflag
= 1; break;
120 if (cflag
+ lflag
+ rflag
+ pflag
!= 1) usage();
123 if (!pflag
&& i
< argc
) user
= argv
[i
++];
125 if (user
== nil
) usage();
133 if (i
!= argc
) usage();
135 if (geteuid() != 0) {
136 fprintf(stderr
, "%s: No root privileges?\n", prog_name
);
140 if ((pw
= getpwuid(uid
)) == nil
) {
142 "%s: Don't know who you (uid %lu) are!\n",
143 prog_name
, (unsigned long) uid
);
147 if ((pw
= getpwnam(user
)) == nil
) {
149 "%s: Don't know who you (%s) are!\n",
154 if (uid
!= 0 && pw
->pw_uid
!= uid
) {
156 "%s: Only root can change the crontabs of others!\n",
166 sprintf(tabfile
, "%s/%s", SPOOLDIR
, user
);
171 if ((fd
= opentab(0, tabfile
, 'r')) < 0) {
172 fprintf(stderr
, "%s: Can't open %s: %s\n",
173 prog_name
, tabfile
, strerror(errno
));
176 copytab(fd
, tabfile
, 1, "stdout");
182 if (unlink(tabfile
) < 0) {
183 fprintf(stderr
, "%s: Can't remove %s: %s\n",
184 prog_name
, tabfile
, strerror(errno
));
188 printf("Crontab of %s removed\n", user
);
192 /* Initialize current Time */
198 if ((fd1
= opentab(uid
, file
, 'r')) < 0) {
199 fprintf(stderr
, "%s: Can't open %s: %s\n",
200 prog_name
, file
, strerror(errno
));
204 /* Try to parse the new crontab file. If the parsing
205 * succeeds then 'crontabs' will be non-null.
207 tab_parse(file
, user
);
209 if (crontabs
== nil
) exit(1);
211 if ((fd2
= opentab(0, tabfile
, 'w')) < 0) {
212 fprintf(stderr
, "%s: Can't open %s: %s\n",
213 prog_name
, tabfile
, strerror(errno
));
216 copytab(fd1
, file
, fd2
, tabfile
);
219 printf("New crontab for %s installed\n", user
);
224 /* Alert cron to the new situation. */
228 if ((fp
= fopen(PIDFILE
, "r")) != NULL
) {
233 while ((c
= fgetc(fp
)) != EOF
&& c
!= '\n') {
234 if ((unsigned) (c
- '0') >= 10) {
237 pid
= 10*pid
+ (c
- '0');
238 if (pid
>= 30000) { pid
= 0; break; }
240 if (pid
> 1 && kill((pid_t
) pid
, SIGHUP
) == 0) {
247 "%s: Alerting cron has failed; cron still running?\n",
251 printf("Cron signalled to reload tables\n");
257 * $PchId: crontab.c,v 1.4 2000/07/17 18:54:50 philip Exp $