enum { iname, ipasswd, iuid, igid, icmt, idir, ishell };
static const char pwfile[] = "/etc/passwd";
-static struct buffer buffer;
-static size_t pos, lengths[7];
-static const char *starts[7];
-#define is(s, i) (!strncmp(s, starts[i], lengths[i]) && !s[lengths[i]])
+#define is(s, i) (!strncmp(s, context->pw.starts[i], context->pw.lengths[i]) && !s[context->pw.lengths[i]])
/* open the passwd file */
-static int oppw()
+static int oppw(struct tzplatform_context *context)
{
- pos = 0;
- return buffer_create(&buffer, pwfile);
+ context->pw.pos = 0;
+ return buffer_create(&context->pw.buffer, pwfile);
}
/* close the passwd file */
-static void clpw()
+static void clpw(struct tzplatform_context *context)
{
- buffer_destroy(&buffer);
+ buffer_destroy(&context->pw.buffer);
}
/* read the passwd file */
-static int rdpw()
+static int rdpw(struct tzplatform_context *context)
{
int col = 0;
- const char *head = buffer.buffer + pos;
- const char *end = buffer.buffer + buffer.length;
+ struct pw *pw = &context->pw;
+ const char *head = pw->buffer.buffer + pw->pos;
+ const char *end = pw->buffer.buffer + pw->buffer.length;
while (head != end) {
- starts[col] = head;
+ pw->starts[col] = head;
while (head != end && *head != ':' && *head != '\n') head++;
- lengths[col] = head - starts[col];
+ pw->lengths[col] = head - pw->starts[col];
col++;
if (col == 7) {
if (head == end) {
- pos = head - buffer.buffer;
+ pw->pos = head - pw->buffer.buffer;
return 1;
}
if (*head == '\n') {
head++;
- pos = head - buffer.buffer;
+ pw->pos = head - pw->buffer.buffer;
return 1;
}
while (head != end && *head != '\n') head++;
}
}
}
- pos = head - buffer.buffer;
+ pw->pos = head - pw->buffer.buffer;
return 0;
}
-int pw_get(struct heap *heap, struct pwget **items)
+int pw_get(struct tzplatform_context *context, struct heap *heap, struct pwget **items)
{
size_t user, home;
int result;
int i, n, s;
+ struct pw *pw = &context->pw;
for (n = 0 ; items[n] != NULL ; n++)
items[n]->set = 0;
- result = oppw();
+ result = oppw(context);
if (result == 0) {
s = n;
- while (s && rdpw()) {
+ while (s && rdpw(context)) {
user = home = HNULL;
for (i = 0 ; i < n ; i++) {
if (!items[i]->set && is(items[i]->id, iuid)) {
if (user == HNULL) {
user = heap_strndup(heap,
- starts[iname], lengths[iname]);
+ pw->starts[iname], pw->lengths[iname]);
home = heap_strndup(heap,
- starts[idir], lengths[idir]);
+ pw->starts[idir], pw->lengths[idir]);
}
items[i]->set = 1;
items[i]->user = user;
}
}
}
- clpw();
+ clpw(context);
}
return result;
}
-int pw_has_uid(uid_t uid)
+int pw_has_uid(struct tzplatform_context *context, uid_t uid)
{
- if (oppw() == 0) {
- while (rdpw()) {
- if (lengths[iuid] && (int)uid == atoi(starts[iuid])) {
- clpw();
+ struct pw *pw = &context->pw;
+
+ if (oppw(context) == 0) {
+ while (rdpw(context)) {
+ if (pw->lengths[iuid] && (int)uid == atoi(pw->starts[iuid])) {
+ clpw(context);
return 1;
}
}
- clpw();
+ clpw(context);
}
return 0;
}
-int pw_get_uid(const char *name, uid_t *uid)
+int pw_get_uid(struct tzplatform_context *context, const char *name, uid_t *uid)
{
- int result = oppw();
+ int result = oppw(context);
if (result == 0) {
- while (rdpw()) {
+ while (rdpw(context)) {
if (is(name, iname)) {
- *uid = (uid_t)atoi(starts[iuid]);
- clpw();
+ *uid = (uid_t)atoi(context->pw.starts[iuid]);
+ clpw(context);
return 0;
}
}
- clpw();
+ clpw(context);
result = -1;
errno = EEXIST;
}
return result;
}
-int pw_get_gid(const char *name, gid_t *gid)
+int pw_get_gid(struct tzplatform_context *context, const char *name, gid_t *gid)
{
- int result = oppw();
+ int result = oppw(context);
if (result == 0) {
- while (rdpw()) {
+ while (rdpw(context)) {
if (is(name, iname)) {
- *gid = (gid_t)atoi(starts[igid]);
- clpw();
+ *gid = (gid_t)atoi(context->pw.starts[igid]);
+ clpw(context);
return 0;
}
}
- clpw();
+ clpw(context);
result = -1;
errno = EEXIST;
}
#ifndef TIZEN_PLATFORM_WRAPPER_PASSWD_H
#define TIZEN_PLATFORM_WRAPPER_PASSWD_H
+#include "context.h"
+
struct pwget {
int set;
const char *id;
size_t home;
};
-int pw_get(struct heap *heap, struct pwget **items);
-int pw_get_uid(const char *name, uid_t *uid);
-int pw_get_gid(const char *name, gid_t *gid);
-int pw_has_uid(uid_t uid);
+int pw_get(struct tzplatform_context *context, struct heap *heap, struct pwget **items);
+int pw_get_uid(struct tzplatform_context *context, const char *name, uid_t *uid);
+int pw_get_gid(struct tzplatform_context *context, const char *name, gid_t *gid);
+int pw_has_uid(struct tzplatform_context *context, uid_t uid);
#endif