Parse a /etc/passwd line: name:x:uid:gid:gecos:home:shell
| 41 | |
| 42 | // Parse a /etc/passwd line: name:x:uid:gid:gecos:home:shell |
| 43 | bool parse_line(const std::string& line, PasswdEntry& e) { |
| 44 | if (line.empty() || line[0] == '#') return false; |
| 45 | std::vector<std::string> f; |
| 46 | std::string acc; |
| 47 | for (char c : line) { |
| 48 | if (c == ':') { f.push_back(std::move(acc)); acc.clear(); } |
| 49 | else if (c != '\r' && c != '\n') acc.push_back(c); |
| 50 | } |
| 51 | f.push_back(std::move(acc)); |
| 52 | if (f.size() < 7) return false; |
| 53 | e.name = f[0]; |
| 54 | try { |
| 55 | e.uid = static_cast<u32>(std::stoul(f[2])); |
| 56 | e.gid = static_cast<u32>(std::stoul(f[3])); |
| 57 | } catch (...) { return false; } |
| 58 | e.gecos = f[4]; |
| 59 | e.home = f[5]; |
| 60 | e.shell = f[6]; |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | // Parse a whole /etc/passwd blob into a uid → entry map. |
| 65 | std::map<u32, PasswdEntry> parse_passwd(const std::string& passwd) { |