diff options
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 87 |
1 files changed, 74 insertions, 13 deletions
diff --git a/src/utils.c b/src/utils.c index 2d2f615..2d30bfc 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,7 +1,7 @@ /* * Calcurse - text-based organizer * - * Copyright (c) 2004-2020 calcurse Development Team <misc@calcurse.org> + * Copyright (c) 2004-2023 calcurse Development Team <misc@calcurse.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -626,6 +626,42 @@ long min2sec(unsigned minutes) return minutes * MININSEC; } +int modify_wday(int wday, int shift) +{ + return (WEEKINDAYS + wday + shift) % WEEKINDAYS; +} + +/* returns char* representing a wday, used for internal functions */ +char *get_wday_default_string(int wday) +{ + switch(wday) { + case MONDAY: + return "Monday"; + break; + case TUESDAY: + return "Tuesday"; + break; + case WEDNESDAY: + return "Wednesday"; + break; + case THURSDAY: + return "Thursday"; + break; + case FRIDAY: + return "Friday"; + break; + case SATURDAY: + return "Saturday"; + break; + case SUNDAY: + return "Sunday"; + break; + default: + return "Sunday"; + break; + } +} + /* * Display a scroll bar when there are so many items that they * can not be displayed inside the corresponding panel. @@ -1317,21 +1353,32 @@ void psleep(unsigned secs) /* * Fork and execute an external process. * - * If pfdin and/or pfdout point to a valid address, a pipe is created and the - * appropriate file descriptors are written to pfdin/pfdout. + * If pfdin/pfdout/pfderr point to a valid address, a pipe is created and the + * appropriate file descriptors are written to pfdin/pfdout/pfderr. + * + * If new_session is non-zero, setsid() is called after forking. */ -int fork_exec(int *pfdin, int *pfdout, const char *path, - const char *const *arg) +int fork_exec(int *pfdin, int *pfdout, int *pfderr, int new_session, + const char *path, const char *const *arg) { - int pin[2], pout[2]; + int pin[2], pout[2], perr[2]; int pid; if (pfdin && (pipe(pin) == -1)) return 0; if (pfdout && (pipe(pout) == -1)) return 0; + if (pfderr && (pipe(perr) == -1)) + return 0; if ((pid = fork()) == 0) { + if (pfderr) { + if (dup2(perr[0], STDERR_FILENO) < 0) + _exit(127); + close(perr[0]); + close(perr[1]); + } + if (pfdout) { if (dup2(pout[0], STDIN_FILENO) < 0) _exit(127); @@ -1346,6 +1393,11 @@ int fork_exec(int *pfdin, int *pfdout, const char *path, close(pin[1]); } + if (new_session) { + if ((setsid() < 0)) + _exit(127); + } + execvp(path, (char *const *)arg); _exit(127); } else { @@ -1353,6 +1405,8 @@ int fork_exec(int *pfdin, int *pfdout, const char *path, close(pin[1]); if (pfdout) close(pout[0]); + if (pfderr) + close(perr[0]); if (pid > 0) { if (pfdin) { @@ -1363,11 +1417,17 @@ int fork_exec(int *pfdin, int *pfdout, const char *path, fcntl(pout[1], F_SETFD, FD_CLOEXEC); *pfdout = pout[1]; } + if (pfderr) { + fcntl(perr[1], F_SETFD, FD_CLOEXEC); + *pfderr = perr[1]; + } } else { if (pfdin) close(pin[0]); if (pfdout) close(pout[1]); + if (pfderr) + close(perr[1]); return 0; } } @@ -1376,8 +1436,8 @@ int fork_exec(int *pfdin, int *pfdout, const char *path, /* Execute an external program in a shell. */ int -shell_exec(int *pfdin, int *pfdout, const char *path, - const char *const *arg) +shell_exec(int *pfdin, int *pfdout, int *pfderr, int new_session, + const char *path, const char *const *arg) { int argc, i; const char **narg; @@ -1406,7 +1466,7 @@ shell_exec(int *pfdin, int *pfdout, const char *path, narg[3] = NULL; } - ret = fork_exec(pfdin, pfdout, *narg, narg); + ret = fork_exec(pfdin, pfdout, pfderr, new_session, *narg, narg); if (arg0) mem_free(arg0); @@ -1416,7 +1476,7 @@ shell_exec(int *pfdin, int *pfdout, const char *path, } /* Wait for a child process to terminate. */ -int child_wait(int *pfdin, int *pfdout, int pid) +int child_wait(int *pfdin, int *pfdout, int *pfderr, int pid) { int stat; @@ -1424,6 +1484,8 @@ int child_wait(int *pfdin, int *pfdout, int pid) close(*pfdin); if (pfdout) close(*pfdout); + if (pfderr) + close(*pfderr); if (waitpid(pid, &stat, 0) == pid) return stat; @@ -1643,7 +1705,7 @@ static void print_date(time_t date, time_t day, const char *extformat) if (!strcmp(extformat, "epoch")) { printf("%ld", (long)date); } else { - time_t day_start = update_time_in_date(day, 0, 0); + time_t day_start = DAY(day); time_t day_end = date_sec_change(day_start, 0, 1); struct tm lt; @@ -2079,8 +2141,7 @@ char *day_ins(char **template, time_t t) { char *day, *msg; - day = date_sec2date_str(update_time_in_date(t, 0 , 0), - DATEFMT(conf.input_datefmt)); + day = date_sec2date_str(DAY(t), DATEFMT(conf.input_datefmt)); asprintf(&msg, *template, day); mem_free(day); return msg; |