mirror of
https://github.com/tuxbox-fork-migrations/recycled-ni-neutrino.git
synced 2025-08-28 07:51:11 +02:00
Origin commit data
------------------
Commit: ae73a950eb
Author: vanhofen <vanhofen@gmx.de>
Date: 2012-08-30 (Thu, 30 Aug 2012)
Origin message was:
------------------
- system/helpers.h|cpp: change header
105 lines
2.4 KiB
C++
105 lines
2.4 KiB
C++
/*
|
|
Neutrino-HD
|
|
|
|
License: GPL
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include <config.h>
|
|
#endif
|
|
|
|
#include <iostream>
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/wait.h>
|
|
|
|
#include <system/helpers.h>
|
|
|
|
int my_system(const char * cmd, const char * arg1, const char * arg2)
|
|
{
|
|
int i;
|
|
pid_t pid;
|
|
int maxfd = getdtablesize();// sysconf(_SC_OPEN_MAX);
|
|
switch (pid = vfork())
|
|
{
|
|
case -1: /* can't vfork */
|
|
perror("vfork");
|
|
return -1;
|
|
case 0: /* child process */
|
|
for(i = 3; i < maxfd; i++)
|
|
close(i);
|
|
if(execlp(cmd, cmd, arg1, arg2, NULL))
|
|
{
|
|
perror("exec");
|
|
}
|
|
exit(0);
|
|
default: /* parent returns to calling process */
|
|
break;
|
|
}
|
|
waitpid(pid, 0, 0);
|
|
return 0;
|
|
}
|
|
|
|
FILE* my_popen( pid_t& pid, const char *cmdstring, const char *type)
|
|
{
|
|
int pfd[2] ={-1,-1};
|
|
FILE *fp = NULL;
|
|
|
|
/* only allow "r" or "w" */
|
|
if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) {
|
|
errno = EINVAL; /* required by POSIX */
|
|
return(NULL);
|
|
}
|
|
|
|
if (pipe(pfd) < 0)
|
|
return(NULL); /* errno set by pipe() */
|
|
|
|
if ((pid = vfork()) < 0) {
|
|
return(NULL); /* errno set by vfork() */
|
|
} else if (pid == 0) { /* child */
|
|
if (*type == 'r') {
|
|
close(pfd[0]);
|
|
if (pfd[1] != STDOUT_FILENO) {
|
|
dup2(pfd[1], STDOUT_FILENO);
|
|
close(pfd[1]);
|
|
}
|
|
} else {
|
|
close(pfd[1]);
|
|
if (pfd[0] != STDIN_FILENO) {
|
|
dup2(pfd[0], STDIN_FILENO);
|
|
close(pfd[0]);
|
|
}
|
|
}
|
|
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
|
|
exit(0);
|
|
}
|
|
|
|
/* parent continues... */
|
|
if (*type == 'r') {
|
|
close(pfd[1]);
|
|
if ((fp = fdopen(pfd[0], type)) == NULL)
|
|
return(NULL);
|
|
} else {
|
|
close(pfd[0]);
|
|
if ((fp = fdopen(pfd[1], type)) == NULL)
|
|
return(NULL);
|
|
}
|
|
return(fp);
|
|
}
|