Skip to content

Instantly share code, notes, and snippets.

@je4npw
Last active May 7, 2023 06:16
Show Gist options
  • Save je4npw/a5ada37a909bb35922a2dfe953eeb7ed to your computer and use it in GitHub Desktop.
Save je4npw/a5ada37a909bb35922a2dfe953eeb7ed to your computer and use it in GitHub Desktop.
trein-307.cpp
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string line_passwd, line_group;
struct
{
fstream readFile(string file)
{
fstream arq(file, ios::in);
if (!arq.is_open()) {
cout << "Não foi possível abrir o arquivo " << file;
exit(0);
}
return arq;
};
vector<string> explode(string separador, string frase)
{
int prox = separador.size();
vector<string> ret;
string::size_type ini, fim;
ini = fim = 0;
while (true) {
fim = frase.find(separador, ini);
if (fim == string::npos) {
ret.push_back(frase.substr(ini, frase.size() - ini));
break;
}
ret.push_back(frase.substr(ini, fim - ini));
ini = fim + prox;
}
return ret;
}
} wl;
vector<pair<string, string>> users;
fstream file_group = wl.readFile("/etc/group");
fstream file_passwd = wl.readFile("/etc/passwd");
fstream arq("teste.txt", ios::out);
while (getline(file_passwd, line_passwd)) {
vector<string> passwd_fields = wl.explode(":", line_passwd);
users.push_back(make_pair(passwd_fields[0], passwd_fields[3]));
}
while (getline(file_group, line_group)) {
vector<string> group_fields = wl.explode(":", line_group);
for (pair<string, string> arr : users) {
if (group_fields[2] == arr.second) {
arq << arr.first << " -> " << arr.second << " -> " << group_fields[0]
<< endl;
}
}
}
file_passwd.close();
file_group.close();
arq.close();
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment