From b0c8e55a4c0d43a5f8efcb85936519d7836ff645 Mon Sep 17 00:00:00 2001 From: akruphi <92621645+akruphi@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:37:42 +0300 Subject: [PATCH] Attributes dialog: Owner and Group lists obtaining via API Update after #1717 Rewritten Owner and Group lists for obtaining via API getpwent() ... endpwent() & getgrent() ... endgrent(); --- far2l/src/setattr.cpp | 53 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/far2l/src/setattr.cpp b/far2l/src/setattr.cpp index 17331297..23a98d58 100644 --- a/far2l/src/setattr.cpp +++ b/far2l/src/setattr.cpp @@ -33,8 +33,9 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "headers.hpp" -#include -#include +#include // for std::sort() +#include // for getpwent() +#include // for getgrent() #include "lang.hpp" #include "dialog.hpp" @@ -645,7 +646,7 @@ static void ApplyFSFileFlags(DialogItemEx *AttrDlg, const FARString &strSelName) FFFlags.Apply(strSelName.GetMB()); } -class ListFromFile { +class ListPwGrEnt { std::vector Items; FarList List; void Append(const wchar_t *s) { @@ -654,8 +655,8 @@ class ListFromFile { Items.back().Text = wcsdup(s); } public: - ListFromFile(const char *filename, int SelCount); - ~ListFromFile() { + ListPwGrEnt(bool bGroups,int SelCount); + ~ListPwGrEnt() { for (auto& item : Items) free((void*)item.Text); } @@ -667,32 +668,30 @@ public: } }; -ListFromFile::ListFromFile(const char *filename, int SelCount) +ListPwGrEnt::ListPwGrEnt(bool bGroups,int SelCount) { - Items.reserve(64); + Items.reserve(128); if (SelCount >= 2) Append(Msg::SetAttrOwnerMultiple); - // may be rewrite to obtain by getpwent() & getgrent() ??? - // now direct reading from /etc/passwd or /etc/group - std::ifstream is; - is.open(filename); - if (is.is_open()) { - std::string str; - while (getline(is,str), !str.empty()) { - // remove comment (starting with '#') from string - auto pos = str.find('#'); - if (pos != std::string::npos) - str.resize(pos); - // ':' need be in string - pos = str.find(':'); - if (pos != std::string::npos) { - str.resize(pos); - Append(FARString(str).CPtr()); // name always before first ':' - } + if (!bGroups) { // usernames + struct passwd *pw; + while ((pw = getpwent()) != NULL) { + Append(FARString(pw->pw_name).CPtr()); } - std::sort(Items.begin()+(SelCount<2 ? 0:1), Items.end(), Cmp); + endpwent(); + } + else { // groups + struct group *gr; + while ((gr = getgrent()) != NULL) { + Append(FARString(gr->gr_name).CPtr()); + } + endgrent(); } + + if( Items.size() > 1 ) + std::sort(Items.begin()+(SelCount<2 ? 0:1), Items.end(), Cmp); + List.ItemsNumber = Items.size(); List.Items = Items.data(); } @@ -773,8 +772,8 @@ bool ShellSetFileAttributes(Panel *SrcPanel, LPCWSTR Object) MakeDialogItemsEx(AttrDlgData, AttrDlg); SetAttrDlgParam DlgParam{}; - ListFromFile Owners("/etc/passwd", SelCount); - ListFromFile Groups("/etc/group", SelCount); + ListPwGrEnt Owners(FALSE, SelCount); + ListPwGrEnt Groups(TRUE, SelCount); AttrDlg[SA_COMBO_OWNER].ListItems = Owners.GetFarList(); AttrDlg[SA_COMBO_GROUP].ListItems = Groups.GetFarList(); -- 2.11.4.GIT