Handle Enter/Escape keys in message dialog.
[helenos.git] / uspace / lib / posix / src / pwd.c
blobc6df4fc8c1f7ea130e677317d66a9d44abff242c
1 /*
2 * Copyright (c) 2011 Jiri Zarevucky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup libposix
30 * @{
32 /** @file Password handling.
35 #include <stdbool.h>
36 #include <pwd.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <assert.h>
41 static bool entry_read = false;
43 /* dummy user account */
44 static const struct passwd dummy_pwd = {
45 .pw_name = (char *) "user",
46 .pw_uid = 0,
47 .pw_gid = 0,
48 .pw_dir = (char *) "/",
49 .pw_shell = (char *) "/app/bdsh"
52 /**
53 * Retrieve next broken-down entry from the user database.
55 * Since HelenOS doesn't have user accounts, this always returns
56 * the same made-up entry.
58 * @return Next user database entry or NULL if not possible. Since HelenOS
59 * doesn't have user accounts, this always returns the same made-up entry.
61 struct passwd *getpwent(void)
63 if (entry_read) {
64 return NULL;
67 entry_read = true;
68 return (struct passwd *) &dummy_pwd;
71 /**
72 * Rewind the user list.
74 void setpwent(void)
76 entry_read = false;
79 /**
80 * Ends enumerating and releases all resources. (Noop)
82 void endpwent(void)
84 /* noop */
87 /**
88 * Find an entry by name.
90 * @param name Name of the entry.
91 * @return Either found entry or NULL if no such entry exists.
93 struct passwd *getpwnam(const char *name)
95 assert(name != NULL);
97 if (strcmp(name, "user") != 0) {
98 return NULL;
101 return (struct passwd *) &dummy_pwd;
105 * Find an entry by name, thread safely.
107 * @param name Name of the entry.
108 * @param pwd Original structure.
109 * @param buffer Buffer for the strings referenced from the result structure.
110 * @param bufsize Length of the buffer.
111 * @param result Where to store updated structure.
112 * @return Zero on success (either found or not found, but without an error),
113 * non-zero error number if error occured.
115 int getpwnam_r(const char *name, struct passwd *pwd,
116 char *buffer, size_t bufsize, struct passwd **result)
118 assert(name != NULL);
119 assert(pwd != NULL);
120 assert(buffer != NULL);
121 assert(result != NULL);
123 if (strcmp(name, "user") != 0) {
124 *result = NULL;
125 return 0;
128 return getpwuid_r(0, pwd, buffer, bufsize, result);
132 * Find an entry by UID.
134 * @param uid UID of the entry.
135 * @return Either found entry or NULL if no such entry exists.
137 struct passwd *getpwuid(uid_t uid)
139 if (uid != 0) {
140 return NULL;
143 return (struct passwd *) &dummy_pwd;
147 * Find an entry by UID, thread safely.
149 * @param uid UID of the entry.
150 * @param pwd Original structure.
151 * @param buffer Buffer for the strings referenced from the result structure.
152 * @param bufsize Length of the buffer.
153 * @param result Where to store updated structure.
154 * @return Zero on success (either found or not found, but without an error),
155 * non-zero error number if error occured.
157 int getpwuid_r(uid_t uid, struct passwd *pwd,
158 char *buffer, size_t bufsize, struct passwd **result)
160 assert(pwd != NULL);
161 assert(buffer != NULL);
162 assert(result != NULL);
164 static const char bf[] = { 'u', 's', 'e', 'r', '\0',
165 '/', '\0', 'b', 'd', 's', 'h', '\0' };
167 if (uid != 0) {
168 *result = NULL;
169 return 0;
171 if (bufsize < sizeof(bf)) {
172 *result = NULL;
173 return ERANGE;
176 memcpy(buffer, bf, sizeof(bf));
178 pwd->pw_name = (char *) bf;
179 pwd->pw_uid = 0;
180 pwd->pw_gid = 0;
181 pwd->pw_dir = (char *) bf + 5;
182 pwd->pw_shell = (char *) bf + 7;
183 *result = (struct passwd *) pwd;
185 return 0;
188 /** @}