Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, 2014 Jonas 'Sortie' Termansen.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* stdio/popen.c
|
|
|
|
* Pipe stream to or from a process.
|
|
|
|
*/
|
2013-01-14 16:58:07 -05:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2016-02-28 06:11:02 -05:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2013-01-14 16:58:07 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
typedef struct popen_struct
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
int fd;
|
|
|
|
pid_t pid;
|
|
|
|
} popen_t;
|
|
|
|
|
2014-02-27 14:57:14 -05:00
|
|
|
static ssize_t popen_read(void* user, void* ptr, size_t size)
|
2013-01-14 16:58:07 -05:00
|
|
|
{
|
|
|
|
popen_t* info = (popen_t*) user;
|
2014-02-27 14:57:14 -05:00
|
|
|
return read(info->fd, ptr, size);
|
2013-01-14 16:58:07 -05:00
|
|
|
}
|
|
|
|
|
2014-02-27 14:57:14 -05:00
|
|
|
static ssize_t popen_write(void* user, const void* ptr, size_t size)
|
2013-01-14 16:58:07 -05:00
|
|
|
{
|
|
|
|
popen_t* info = (popen_t*) user;
|
2014-02-27 14:57:14 -05:00
|
|
|
return write(info->fd, ptr, size);
|
2013-01-14 16:58:07 -05:00
|
|
|
}
|
|
|
|
|
2016-02-28 06:11:02 -05:00
|
|
|
static off_t popen_seek(void* user, off_t offset, int whence)
|
2013-01-14 16:58:07 -05:00
|
|
|
{
|
2016-02-28 06:11:02 -05:00
|
|
|
(void) user;
|
|
|
|
(void) offset;
|
|
|
|
(void) whence;
|
2013-01-14 16:58:07 -05:00
|
|
|
return errno = ESPIPE, -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int popen_close(void* user)
|
|
|
|
{
|
|
|
|
popen_t* info = (popen_t*) user;
|
2014-02-27 14:57:14 -05:00
|
|
|
if ( close(info->fd) < 0 )
|
2013-01-14 16:58:07 -05:00
|
|
|
{
|
2014-02-27 14:57:14 -05:00
|
|
|
// TODO: Should this return value be discarded?
|
2013-01-14 16:58:07 -05:00
|
|
|
}
|
|
|
|
int status;
|
|
|
|
if ( waitpid(info->pid, &status, 0) < 0 )
|
|
|
|
status = -1;
|
|
|
|
free(info);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-11-20 17:54:30 -05:00
|
|
|
static void popen_install(FILE* fp, popen_t* info)
|
2013-01-14 16:58:07 -05:00
|
|
|
{
|
|
|
|
fp->user = info;
|
|
|
|
fp->read_func = popen_read;
|
|
|
|
fp->write_func = popen_write;
|
|
|
|
fp->seek_func = popen_seek;
|
|
|
|
fp->close_func = popen_close;
|
|
|
|
}
|
|
|
|
|
2016-02-28 06:11:02 -05:00
|
|
|
FILE* popen(const char* command, const char* type)
|
2013-01-14 16:58:07 -05:00
|
|
|
{
|
2014-02-27 14:57:14 -05:00
|
|
|
int mode_flags = fparsemode(type);
|
|
|
|
if ( mode_flags < 0 )
|
|
|
|
return (FILE*) NULL;
|
|
|
|
|
|
|
|
if ( mode_flags & ~(FILE_MODE_READ | FILE_MODE_WRITE | FILE_MODE_CLOEXEC |
|
|
|
|
FILE_MODE_CREATE | FILE_MODE_BINARY | FILE_MODE_TRUNCATE) )
|
|
|
|
return errno = EINVAL, (FILE*) NULL;
|
|
|
|
|
|
|
|
bool reading = mode_flags & FILE_MODE_READ;
|
|
|
|
bool writing = mode_flags & FILE_MODE_WRITE;
|
|
|
|
if ( reading == writing )
|
|
|
|
return errno = EINVAL, (FILE*) NULL;
|
|
|
|
|
|
|
|
int used_end, unused_end, redirect_what;
|
2013-01-14 16:58:07 -05:00
|
|
|
int pipefds[2];
|
|
|
|
popen_t* info;
|
|
|
|
FILE* fp;
|
|
|
|
pid_t childpid;
|
|
|
|
|
|
|
|
if ( !(info = (popen_t*) calloc(1, sizeof(popen_t))) )
|
|
|
|
goto cleanup_out;
|
|
|
|
if ( !(fp = fnewfile()) )
|
|
|
|
goto cleanup_info;
|
|
|
|
if ( pipe(pipefds) )
|
|
|
|
goto cleanup_fp;
|
|
|
|
if ( (childpid = fork()) < 0 )
|
|
|
|
goto cleanup_pipes;
|
|
|
|
|
|
|
|
if ( childpid )
|
|
|
|
{
|
2014-02-27 14:57:14 -05:00
|
|
|
used_end = writing ? 1 /*writing*/ : 0 /*reading*/;
|
|
|
|
unused_end = writing ? 0 /*reading*/ : 1 /*writing*/;
|
2013-01-14 16:58:07 -05:00
|
|
|
close(pipefds[unused_end]);
|
2014-02-27 14:57:14 -05:00
|
|
|
if ( mode_flags & FILE_MODE_CLOEXEC )
|
2013-01-14 16:58:07 -05:00
|
|
|
fcntl(pipefds[used_end], F_SETFL, FD_CLOEXEC);
|
|
|
|
info->fd = pipefds[used_end];
|
|
|
|
info->pid = childpid;
|
|
|
|
popen_install(fp, info);
|
2014-02-27 14:57:14 -05:00
|
|
|
fp->flags |= writing ? _FILE_WRITABLE : _FILE_READABLE;
|
2015-11-20 17:54:30 -05:00
|
|
|
fp->fd = info->fd;
|
2013-01-14 16:58:07 -05:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:57:14 -05:00
|
|
|
redirect_what = writing ? 0 /*stdin*/ : 1 /*stdout*/;
|
|
|
|
used_end = writing ? 0 /*reading*/ : 1 /*writing*/;
|
|
|
|
unused_end = writing ? 1 /*writing*/ : 0 /*reading*/;
|
2013-01-14 16:58:07 -05:00
|
|
|
if ( dup2(pipefds[used_end], redirect_what) < 0 ||
|
|
|
|
close(pipefds[used_end]) ||
|
|
|
|
close(pipefds[unused_end]) )
|
|
|
|
_exit(127);
|
|
|
|
|
2016-09-30 11:40:17 -04:00
|
|
|
execlp("sh", "sh", "-c", command, (const char*) NULL);
|
2013-01-14 16:58:07 -05:00
|
|
|
_exit(127);
|
|
|
|
|
|
|
|
cleanup_pipes:
|
|
|
|
close(pipefds[0]);
|
|
|
|
close(pipefds[1]);
|
|
|
|
cleanup_fp:
|
|
|
|
fclose(fp);
|
|
|
|
cleanup_info:
|
|
|
|
free(info);
|
|
|
|
cleanup_out:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-28 06:11:02 -05:00
|
|
|
int pclose(FILE* fp)
|
2013-01-14 16:58:07 -05:00
|
|
|
{
|
|
|
|
return fclose(fp);
|
|
|
|
}
|