Skip to content

Commit

Permalink
Add nohup implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Edd12321 committed May 23, 2023
1 parent be14311 commit 8b9276c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions corebuf/nohup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#define or ||
#define GetFile(X) open(X, O_APPEND|O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR)

static inline bool
die(const char *errstr, int errcode)
{
fprintf(stderr, "%s\n", errstr);
exit(errcode);
return 0;//dummy return
}

int
main(int argc, char *argv[])
{
const char *fname = "nohup.out";
const char *usage = "usage: nohup <cmd [<args...>]";
char dest[PATH_MAX];
char *home;
int fd;

argv++;
argc > 1
or die(usage, EXIT_FAILURE);
signal(SIGHUP, SIG_IGN) != SIG_ERR
or die("signal(2)", 126);
if (isatty(STDOUT_FILENO)) {
if ((fd = GetFile(fname)) == -1) {
home = getenv("HOME");
if (home)
snprintf(dest, sizeof(dest), "%s/%s", home, fname);
else
strcpy(dest, fname);
if ((fd = GetFile(fname)) == -1)
return 126;
dup2(fd, STDOUT_FILENO);
}
}
if (isatty(STDERR_FILENO))
dup2(STDOUT_FILENO, STDERR_FILENO);
execvp(*argv, argv);
exit(126+(errno == ENOENT));
}
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ typedef std::string CodeBlock;
typedef std::string AliasName;
typedef std::string Path;
typedef int Jid;
#define DispatchTable std::unordered_map
#define DispatchTable std::map
/***** GLOBAL VARIABLES BEGIN *****/
extern char **environ;

Expand Down

0 comments on commit 8b9276c

Please sign in to comment.