* subversion/libsvn_subr/validate.c
[svn.git] / subversion / libsvn_subr / user.c
blob4f83762ce537a4f1d8ec1694facb405381b66984
1 /*
2 * user.c: APR wrapper functions for Subversion
4 * ====================================================================
5 * Copyright (c) 2000-2004 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
20 #include <apr_pools.h>
21 #include <apr_user.h>
22 #include <apr_env.h>
24 #include "svn_user.h"
25 #include "svn_utf.h"
27 /* Get the current user's name from the OS */
28 static const char *
29 get_os_username(apr_pool_t *pool)
31 #if APR_HAS_USER
32 char *username;
33 apr_uid_t uid;
34 apr_gid_t gid;
36 if (apr_uid_current(&uid, &gid, pool) == APR_SUCCESS &&
37 apr_uid_name_get(&username, uid, pool) == APR_SUCCESS)
38 return username;
39 #endif
41 return NULL;
44 /* Return a UTF8 version of STR, or NULL on error.
45 Use POOL for any necessary allocation. */
46 static const char *
47 utf8_or_nothing(const char *str, apr_pool_t *pool) {
48 if (str)
50 const char *utf8_str;
51 svn_error_t *err = svn_utf_cstring_to_utf8(&utf8_str, str, pool);
52 if (! err)
53 return utf8_str;
54 svn_error_clear(err);
56 return NULL;
59 const char *
60 svn_user_get_name(apr_pool_t *pool)
62 const char *username = get_os_username(pool);
63 return utf8_or_nothing(username, pool);
66 const char *
67 svn_user_get_homedir(apr_pool_t *pool)
69 const char *username;
70 char *homedir;
72 if (apr_env_get(&homedir, "HOME", pool) == APR_SUCCESS)
73 return utf8_or_nothing(homedir, pool);
75 username = get_os_username(pool);
76 if (username != NULL &&
77 apr_uid_homepath_get(&homedir, username, pool) == APR_SUCCESS)
78 return utf8_or_nothing(homedir, pool);
80 return NULL;