1 //
2 // aegis - project change supervisor
3 // Copyright (C) 2008 Peter Miller
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or (at
8 // your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 
19 #include <common/ac/unistd.h>
20 #include <sys/utsname.h>
21 
22 #include <common/wstring/list.h>
23 #include <libaegis/sub.h>
24 #include <libaegis/sub/functor/hostname.h>
25 
26 
~sub_functor_hostname()27 sub_functor_hostname::~sub_functor_hostname()
28 {
29 }
30 
31 
sub_functor_hostname(const nstring & aname)32 sub_functor_hostname::sub_functor_hostname(const nstring &aname) :
33     sub_functor(aname)
34 {
35 }
36 
37 
38 sub_functor::pointer
create(const nstring & aname)39 sub_functor_hostname::create(const nstring &aname)
40 {
41     return pointer(new sub_functor_hostname(aname));
42 }
43 
44 
45 wstring
evaluate(sub_context_ty * scp,const wstring_list & arg)46 sub_functor_hostname::evaluate(sub_context_ty *scp, const wstring_list &arg)
47 {
48     if (arg.size() != 1)
49     {
50         scp->error_set(i18n("requires zero arguments"));
51     }
52     else
53     {
54         char buf1[300];
55         if (gethostname(buf1, sizeof(buf1)) >= 0)
56             return wstring(buf1);
57         struct utsname buf2;
58         if (uname(&buf2) >= 0)
59             return wstring(buf2.nodename);
60     }
61     return wstring();
62 }
63