-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
69 lines (57 loc) · 1.53 KB
/
util.cpp
File metadata and controls
69 lines (57 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// $Id: util.cpp,v 1.11 2016-01-13 16:21:53-08 - - $
#include <cstdlib>
#include <unistd.h>
using namespace std;
#include "debug.h"
#include "util.h"
int exit_status::status = EXIT_SUCCESS;
static string execname_string;
void exit_status::set(int new_status)
{
status = new_status;
}
int exit_status::get()
{
return status;
}
void execname(const string& name)
{
execname_string = name.substr(name.rfind('/') + 1);
DEBUGF('u', execname_string);
}
string& execname()
{
return execname_string;
}
bool want_echo()
{
constexpr int CIN_FD{ 0 };
constexpr int COUT_FD{ 1 };
bool cin_is_not_a_tty = not isatty(CIN_FD);
bool cout_is_not_a_tty = not isatty(COUT_FD);
DEBUGF('u', "cin_is_not_a_tty = " << cin_is_not_a_tty <<
", cout_is_not_a_tty = " << cout_is_not_a_tty);
return cin_is_not_a_tty or cout_is_not_a_tty;
}
wordvec split(const string& line, const string& delimiters)
{
wordvec words;
size_t end = 0;
// Loop over the string, splitting out words, and for each word
// thus found, append it to the output wordvec.
for (;;) {
size_t start = line.find_first_not_of(delimiters, end);
if (start == string::npos)
break;
end = line.find_first_of(delimiters, start);
words.push_back(line.substr(start, end - start));
}
DEBUGF('u', words);
return words;
}
ostream& complain()
{
exit_status::set(EXIT_FAILURE);
cerr << execname() << ": ";
return cerr;
}