Debian Codegraph @

src/id.cc

raw symbols SCIP
1 #include <cstddef>
2 #include <string>
3 #include <vector>
4
5 #ifndef __WIN32
6 #include <grp.h>
7 #include <pwd.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 #endif
11
12 #include <R.h>
13 #include <Rinternals.h>
14
15 #include "utils.h"
16
17 // [[export]]
18 extern "C" SEXP fs_getpwnam_(SEXP name_sxp) {
19 SEXP out = PROTECT(Rf_allocVector(INTSXP, Rf_xlength(name_sxp)));
20
21 #ifndef __WIN32
22 int* out_p = INTEGER(out);
23 for (R_xlen_t i = 0; i < Rf_xlength(name_sxp); ++i) {
24 passwd* pwd;
25 pwd = getpwnam(CHAR(STRING_ELT(name_sxp, i)));
26 if (pwd != NULL) {
27 out_p[i] = pwd->pw_uid;
28 } else {
29 out_p[i] = NA_INTEGER;
30 }
31 }
32 #endif
33
34 UNPROTECT(1);
35 return out;
36 }
37
38 // [[export]]
39 extern "C" SEXP fs_getgrnam_(SEXP name_sxp) {
40 SEXP out = PROTECT(Rf_allocVector(INTSXP, Rf_xlength(name_sxp)));
41
42 #ifndef __WIN32
43 int* out_p = INTEGER(out);
44 for (R_xlen_t i = 0; i < Rf_xlength(name_sxp); ++i) {
45 group* grp;
46 grp = getgrnam(CHAR(STRING_ELT(name_sxp, i)));
47 if (grp != NULL) {
48 out_p[i] = grp->gr_gid;
49 } else {
50 out_p[i] = NA_INTEGER;
51 }
52 }
53 #endif
54
55 UNPROTECT(1);
56 return out;
57 }
58
59 // [[export]]
60 extern "C" SEXP fs_groups_() {
61
62 BEGIN_CPP
63 std::vector<std::string> names;
64 std::vector<int> ids;
65 #ifndef __WIN32
66 group* grp = getgrent();
67 while (grp != NULL) {
68 names.push_back(grp->gr_name);
69 ids.push_back(grp->gr_gid);
70 grp = getgrent();
71 }
72 endgrent();
73 #endif
74
75 SEXP out = PROTECT(Rf_allocVector(VECSXP, 2));
76 SEXP ids_sxp = PROTECT(Rf_allocVector(INTSXP, ids.size()));
77 SEXP group_names_sxp = PROTECT(Rf_allocVector(STRSXP, names.size()));
78
79 for (size_t i = 0; i < ids.size(); ++i) {
80 INTEGER(ids_sxp)[i] = ids[i];
81 SET_STRING_ELT(group_names_sxp, i, Rf_mkChar(names[i].c_str()));
82 }
83 SET_VECTOR_ELT(out, 0, ids_sxp);
84 SET_VECTOR_ELT(out, 1, group_names_sxp);
85
86 SEXP col_names_sxp = PROTECT(Rf_allocVector(STRSXP, 2));
87 SET_STRING_ELT(col_names_sxp, 0, Rf_mkChar("group_id"));
88 SET_STRING_ELT(col_names_sxp, 1, Rf_mkChar("group_name"));
89 Rf_setAttrib(out, R_NamesSymbol, col_names_sxp);
90 UNPROTECT(1);
91
92 Rf_setAttrib(out, R_ClassSymbol, Rf_mkString("data.frame"));
93
94 SEXP row_names = PROTECT(Rf_allocVector(INTSXP, 2));
95 INTEGER(row_names)[0] = NA_INTEGER;
96 INTEGER(row_names)[1] = -names.size();
97 Rf_setAttrib(out, R_RowNamesSymbol, row_names);
98 UNPROTECT(1);
99
100 UNPROTECT(3);
101 return out;
102
103 END_CPP
104 }
105
106 // [[export]]
107 extern "C" SEXP fs_users_() {
108
109 BEGIN_CPP
110 std::vector<std::string> names;
111 std::vector<int> ids;
112 #ifndef __WIN32
113 passwd* pwd = getpwent();
114 while (pwd != NULL) {
115 names.push_back(pwd->pw_name);
116 ids.push_back(pwd->pw_uid);
117 pwd = getpwent();
118 }
119 endpwent();
120 #endif
121
122 SEXP out = PROTECT(Rf_allocVector(VECSXP, 2));
123 SEXP ids_sxp = PROTECT(Rf_allocVector(INTSXP, ids.size()));
124 SEXP user_names_sxp = PROTECT(Rf_allocVector(STRSXP, names.size()));
125
126 for (size_t i = 0; i < ids.size(); ++i) {
127 INTEGER(ids_sxp)[i] = ids[i];
128 SET_STRING_ELT(user_names_sxp, i, Rf_mkChar(names[i].c_str()));
129 }
130 SET_VECTOR_ELT(out, 0, ids_sxp);
131 SET_VECTOR_ELT(out, 1, user_names_sxp);
132
133 SEXP col_names_sxp = PROTECT(Rf_allocVector(STRSXP, 2));
134 SET_STRING_ELT(col_names_sxp, 0, Rf_mkChar("user_id"));
135 SET_STRING_ELT(col_names_sxp, 1, Rf_mkChar("user_name"));
136 Rf_setAttrib(out, R_NamesSymbol, col_names_sxp);
137 UNPROTECT(1);
138
139 Rf_setAttrib(out, R_ClassSymbol, Rf_mkString("data.frame"));
140
141 SEXP row_names = PROTECT(Rf_allocVector(INTSXP, 2));
142 INTEGER(row_names)[0] = NA_INTEGER;
143 INTEGER(row_names)[1] = -names.size();
144 Rf_setAttrib(out, R_RowNamesSymbol, row_names);
145 UNPROTECT(1);
146
147 UNPROTECT(3);
148 return out;
149
150 END_CPP
151 }