Various cosmetic changes.
[wine] / dlls / advapi32 / advapi.c
1 /*
2  * Win32 advapi functions
3  *
4  * Copyright 1995 Sven Verdoolaege
5  */
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <pwd.h>
12
13 #include "winbase.h"
14 #include "windef.h"
15 #include "winnls.h"
16 #include "winerror.h"
17
18 #include "debugtools.h"
19
20 DEFAULT_DEBUG_CHANNEL(advapi);
21
22 /******************************************************************************
23  * GetUserNameA [ADVAPI32.@]
24  *
25  * NOTE: lpSize returns the total length of the username, including the
26  * terminating null character.
27  */
28 BOOL WINAPI
29 GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
30 {
31   size_t len;
32   char *name;
33
34   struct passwd *pwd = getpwuid( getuid() );
35   if (!pwd)
36   {
37     ERR("Username lookup failed: %s\n", strerror(errno));
38     return 0;
39   }
40
41   name = pwd->pw_name;
42
43   /* We need to include the null character when determining the size of the buffer. */
44   len = strlen(name) + 1;
45   if (len > *lpSize)
46   {
47     SetLastError(ERROR_MORE_DATA);
48     *lpSize = len;
49     return 0;
50   }
51
52   *lpSize = len;
53   strcpy(lpszName, name);
54   return 1;
55 }
56
57 /******************************************************************************
58  * GetUserNameW [ADVAPI32.@]
59  *
60  * PARAMS
61  *   lpszName []
62  *   lpSize   []
63  */
64 BOOL WINAPI
65 GetUserNameW( LPWSTR lpszName, LPDWORD lpSize )
66 {
67         LPSTR name = (LPSTR)HeapAlloc( GetProcessHeap(), 0, *lpSize );
68         DWORD   size = *lpSize;
69         BOOL res = GetUserNameA(name,lpSize);
70
71         /* FIXME: should set lpSize in WCHARs */
72         if (size && !MultiByteToWideChar( CP_ACP, 0, name, -1, lpszName, size ))
73             lpszName[size-1] = 0;
74         HeapFree( GetProcessHeap(), 0, name );
75         return res;
76 }