]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/subversion/subversion/libsvn_subr/user.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / subversion / subversion / libsvn_subr / user.c
1 /*
2  * user.c: APR wrapper functions for Subversion
3  *
4  * ====================================================================
5  *    Licensed to the Apache Software Foundation (ASF) under one
6  *    or more contributor license agreements.  See the NOTICE file
7  *    distributed with this work for additional information
8  *    regarding copyright ownership.  The ASF licenses this file
9  *    to you under the Apache License, Version 2.0 (the
10  *    "License"); you may not use this file except in compliance
11  *    with the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *    Unless required by applicable law or agreed to in writing,
16  *    software distributed under the License is distributed on an
17  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  *    KIND, either express or implied.  See the License for the
19  *    specific language governing permissions and limitations
20  *    under the License.
21  * ====================================================================
22  */
23
24
25 #include <apr_pools.h>
26 #include <apr_user.h>
27 #include <apr_env.h>
28
29 #include "svn_user.h"
30 #include "svn_utf.h"
31
32 /* Get the current user's name from the OS */
33 static const char *
34 get_os_username(apr_pool_t *pool)
35 {
36 #if APR_HAS_USER
37   char *username;
38   apr_uid_t uid;
39   apr_gid_t gid;
40
41   if (apr_uid_current(&uid, &gid, pool) == APR_SUCCESS &&
42       apr_uid_name_get(&username, uid, pool) == APR_SUCCESS)
43     return username;
44 #endif
45
46   return NULL;
47 }
48
49 /* Return a UTF8 version of STR, or NULL on error.
50    Use POOL for any necessary allocation. */
51 static const char *
52 utf8_or_nothing(const char *str, apr_pool_t *pool) {
53   if (str)
54     {
55       const char *utf8_str;
56       svn_error_t *err = svn_utf_cstring_to_utf8(&utf8_str, str, pool);
57       if (! err)
58         return utf8_str;
59       svn_error_clear(err);
60     }
61   return NULL;
62 }
63
64 const char *
65 svn_user_get_name(apr_pool_t *pool)
66 {
67   const char *username = get_os_username(pool);
68   return utf8_or_nothing(username, pool);
69 }
70
71 const char *
72 svn_user_get_homedir(apr_pool_t *pool)
73 {
74   const char *username;
75   char *homedir;
76
77   if (apr_env_get(&homedir, "HOME", pool) == APR_SUCCESS)
78     return utf8_or_nothing(homedir, pool);
79
80   username = get_os_username(pool);
81   if (username != NULL &&
82       apr_uid_homepath_get(&homedir, username, pool) == APR_SUCCESS)
83     return utf8_or_nothing(homedir, pool);
84
85   return NULL;
86 }