Sync with 5.1.0
[deliverable/titan.core.git] / common / userinfo.c
CommitLineData
970ed795
EL
1///////////////////////////////////////////////////////////////////////////////
2// Copyright (c) 2000-2014 Ericsson Telecom AB
3// All rights reserved. This program and the accompanying materials
4// are made available under the terms of the Eclipse Public License v1.0
5// which accompanies this distribution, and is available at
6// http://www.eclipse.org/legal/epl-v10.html
7///////////////////////////////////////////////////////////////////////////////
8#include <time.h>
9#include <unistd.h>
10#ifdef MINGW
11# include <windows.h>
12# include <lmcons.h>
13#else
14# include <pwd.h>
15#endif
16
17#include "memory.h"
18#include "userinfo.h"
19
20char *get_user_info(void)
21{
22 char *ret_val;
23 time_t t;
24 const char *current_time;
25#ifdef MINGW
26 TCHAR user_name[UNLEN + 1], computer_name[MAX_COMPUTERNAME_LENGTH + 1];
27 DWORD buffer_size = sizeof(user_name);
28 if (GetUserName(user_name, &buffer_size)) ret_val = mcopystr(user_name);
29 else ret_val = mcopystr("unknown");
30 ret_val = mputc(ret_val, '@');
31 buffer_size = sizeof(computer_name);
32 if (GetComputerName(computer_name, &buffer_size))
33 ret_val = mputstr(ret_val, computer_name);
34 else ret_val = mputstr(ret_val, "unknown");
35#else
36 struct passwd *p;
37 char host_name[256];
38 setpwent();
39 p = getpwuid(getuid());
40 if (p != NULL) {
41 size_t i;
42 for (i = 0; p->pw_gecos[i] != '\0'; i++) {
43 if (p->pw_gecos[i] == ',') {
44 /* Truncating additional info (e.g. phone number) after the full name */
45 p->pw_gecos[i] = '\0';
46 break;
47 }
48 }
49 ret_val = mprintf("%s (%s", p->pw_gecos, p->pw_name);
50 } else ret_val = mcopystr("Unknown User (unknown");
51 endpwent();
52 if (gethostname(host_name, sizeof(host_name)))
53 ret_val = mputstr(ret_val, "@unknown)");
54 else {
55 host_name[sizeof(host_name) - 1] = '\0';
56 ret_val = mputprintf(ret_val, "@%s)", host_name);
57 }
58#endif
59 t = time(NULL);
60 current_time = ctime(&t);
61 ret_val = mputprintf(ret_val, " on %s", current_time);
62 return ret_val;
63}
This page took 0.039881 seconds and 5 git commands to generate.