-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimezone.cpp
More file actions
46 lines (41 loc) · 1.4 KB
/
timezone.cpp
File metadata and controls
46 lines (41 loc) · 1.4 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
#include <iostream>
#include <regex>
#include <boost/program_options.hpp>
int main (int argc, char **argv) {
std::string time_zone;
int current_uid = getuid();
std::regex tzRegex (R"(^UTC$|^[A-Za-z]+\/[A-Za-z_\-]+$)");
try {
boost::program_options::options_description desc{"Options"};
desc.add_options()
("help,h", "Help screen")
("time-zone", boost::program_options::value<std::string>()->required(), "New Time Zone");
boost::program_options::variables_map vm;
boost::program_options::command_line_style::allow_long_disguise;
store(parse_command_line(argc, argv, desc), vm);
if (vm.count("help")) {
std::cout << desc << '\n';
return 0;
}
notify(vm);
if (vm.count("time-zone")) {
time_zone = vm["time-zone"].as<std::string>();
}
}
catch (const boost::program_options::error &ex) {
std::cerr << ex.what() << '\n';
return 1;
}
if (!std::regex_match(time_zone, tzRegex)) {
std::cerr<<std::endl<<"Error! Invalid time zone! "<<time_zone<<std::endl<<std::endl;
return 1;
}
std::string tz_cmd = "/usr/bin/timedatectl set-timezone "+time_zone;
if (setuid(0)) { //I am now root!
perror("setuid");
return 1;
}
std::system(tz_cmd.c_str());
setuid(current_uid); //return to previous user
return 0;
}