beep: the -d option takes milliseconds not microseconds
[oweals/busybox.git] / miscutils / crontab.c
index 673b558c334a274a2026cfd84903a62671fb600d..5557bc491e0f000013207253d093f73b1d95fd0c 100644 (file)
 
 #include "libbb.h"
 
-#ifndef CRONTABS
-#define CRONTABS        "/var/spool/cron/crontabs"
-#endif
+#define CRONTABS        CONFIG_FEATURE_CROND_DIR "/crontabs"
 #ifndef CRONUPDATE
 #define CRONUPDATE      "cron.update"
 #endif
 
-static void change_user(const struct passwd *pas)
-{
-       xsetenv("USER", pas->pw_name);
-       xsetenv("HOME", pas->pw_dir);
-       xsetenv("SHELL", DEFAULT_SHELL);
-
-       /* initgroups, setgid, setuid */
-       change_identity(pas);
-
-       if (chdir(pas->pw_dir) < 0) {
-               bb_perror_msg("chdir(%s) by %s failed",
-                               pas->pw_dir, pas->pw_name);
-               xchdir("/tmp");
-       }
-}
-
 static void edit_file(const struct passwd *pas, const char *file)
 {
        const char *ptr;
@@ -48,7 +30,11 @@ static void edit_file(const struct passwd *pas, const char *file)
        }
 
        /* CHILD - change user and run editor */
-       change_user(pas);
+       /* initgroups, setgid, setuid */
+       change_identity(pas);
+       setup_environment(DEFAULT_SHELL,
+                       SETUP_ENV_CHANGEENV | SETUP_ENV_TO_TMP,
+                       pas);
        ptr = getenv("VISUAL");
        if (!ptr) {
                ptr = getenv("EDITOR");
@@ -120,21 +106,15 @@ int crontab_main(int argc UNUSED_PARAM, char **argv)
        argv += optind;
 
        if (sanitize_env_if_suid()) { /* Clears dangerous stuff, sets PATH */
-               /* run by non-root? */
+               /* Run by non-root */
                if (opt_ler & (OPT_u|OPT_c))
-                       bb_error_msg_and_die("only root can use -c or -u");
+                       bb_error_msg_and_die(bb_msg_you_must_be_root);
        }
 
        if (opt_ler & OPT_u) {
-               pas = getpwnam(user_name);
-               if (!pas)
-                       bb_error_msg_and_die("user %s is not known", user_name);
+               pas = xgetpwnam(user_name);
        } else {
-/* XXX: xgetpwuid */
-               uid_t my_uid = getuid();
-               pas = getpwuid(my_uid);
-               if (!pas)
-                       bb_perror_msg_and_die("unknown uid %d", (int)my_uid);
+               pas = xgetpwuid(getuid());
        }
 
 #define user_name DONT_USE_ME_BEYOND_THIS_POINT
@@ -201,7 +181,7 @@ int crontab_main(int argc UNUSED_PARAM, char **argv)
                        close(fd);
                        xrename(new_fname, pas->pw_name);
                } else {
-                       bb_error_msg("cannot create %s/%s",
+                       bb_error_msg("can't create %s/%s",
                                        crontab_dir, new_fname);
                }
                if (tmp_fname)
@@ -228,7 +208,7 @@ int crontab_main(int argc UNUSED_PARAM, char **argv)
                /* loop */
        }
        if (fd < 0) {
-               bb_error_msg("cannot append to %s/%s",
+               bb_error_msg("can't append to %s/%s",
                                crontab_dir, CRONUPDATE);
        }
        return 0;