Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rlib
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Recolic
rlib
Commits
8910127e
There was an error fetching the commit references. Please try again later.
Commit
8910127e
authored
1 month ago
by
Recolic
Browse files
Options
Downloads
Patches
Plain Diff
shell_run
parent
fcd760c4
No related branches found
No related tags found
1 merge request
!2
shell_run
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
functional.hpp
+13
-1
13 additions, 1 deletion
functional.hpp
sys/unix_handy.hpp
+42
-6
42 additions, 6 deletions
sys/unix_handy.hpp
with
55 additions
and
7 deletions
functional.hpp
+
13
−
1
View file @
8910127e
...
...
@@ -9,6 +9,7 @@
#include
<list>
#include
<functional>
#include
<chrono>
#include
<future>
#include
<stdexcept>
namespace
rlib
{
...
...
@@ -44,7 +45,18 @@ namespace rlib {
auto
begin
=
std
::
chrono
::
high_resolution_clock
::
now
();
f
(
std
::
forward
<
Args
>
(
args
)
...);
auto
end
=
std
::
chrono
::
high_resolution_clock
::
now
();
return
::
std
::
chrono
::
duration
<
double
>
(
end
-
begin
).
count
();
return
std
::
chrono
::
duration
<
double
>
(
end
-
begin
).
count
();
}
template
<
typename
Func
,
typename
...
Args
>
static
inline
auto
timeout
(
int
timeout_seconds
,
Func
&&
func
,
Args
&&
...
args
)
{
using
ReturnType
=
decltype
(
func
(
args
...));
auto
future
=
std
::
async
(
std
::
launch
::
async
,
std
::
forward
<
Func
>
(
func
),
std
::
forward
<
Args
>
(
args
)...);
if
(
future
.
wait_for
(
std
::
chrono
::
seconds
(
timeout_seconds
))
==
std
::
future_status
::
timeout
)
{
return
ReturnType
{};
}
return
future
.
get
();
}
template
<
class
Func
,
typename
...
Args
>
...
...
This diff is collapsed.
Click to expand it.
sys/unix_handy.hpp
+
42
−
6
View file @
8910127e
...
...
@@ -2,18 +2,18 @@
#define RLIB_UNIX_HANDY_HPP_
#include
<unistd.h>
#include
<sys/socket.h>
#include
<sys/types.h>
#include
<netdb.h>
#include
<rlib/scope_guard.hpp>
#include
<rlib/string.hpp>
#include
<string>
#include
<stdexcept>
#include
<vector>
#include
<rlib/sys/os.hpp>
#if RLIB_OS_ID == OS_WINDOWS
#error rlib/sys/unix_handy.hpp is not for Windows.
#endif
// For shell_run
#include
<sstream>
namespace
rlib
{
// args DOES NOT contain the "$0".
inline
void
execs
(
std
::
string
path
,
std
::
vector
<
std
::
string
>
args
)
{
...
...
@@ -28,10 +28,46 @@ namespace rlib {
::
execv
(
path
.
c_str
(),
arr
);
}
struct
shell_result
{
int
status
;
std
::
string
stdout_
;
};
// Execute command with shell and capture stdout.
// Note: stderr would be discarded. Use `2>&1` if needed.
shell_result
shell_run
(
const
std
::
string
&
command
)
{
char
buffer
[
128
];
FILE
*
pipe
=
popen
(
command
.
c_str
(),
"r"
);
if
(
!
pipe
)
{
return
{
-
errno
,
""
};
}
shell_result
res
;
while
(
fgets
(
buffer
,
sizeof
(
buffer
),
pipe
)
!=
nullptr
)
{
res
.
stdout_
+=
buffer
;
}
res
.
status
=
pclose
(
pipe
);
res
.
status
=
WIFEXITED
(
res
.
status
)
?
WEXITSTATUS
(
res
.
status
)
:
-
errno
;
return
res
;
}
auto
get_shell_name
()
{
return
shell_run
(
"echo -n $0"
).
stdout_
;
}
}
// Deprecated. Use sys/sio.hpp
#if 1+1 == 4
#include
<sys/socket.h>
#include
<sys/types.h>
#include
<netdb.h>
#include
<rlib/scope_guard.hpp>
#include
<rlib/string.hpp>
namespace
rlib
{
namespace
impl
{
using
rlib
::
literals
::
operator
""
_format
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment