]> SALOME platform Git repositories - modules/yacs.git/blob - src/Utils/Utils_Identity.cxx
Salome HOME
sources v1.2
[modules/yacs.git] / src / Utils / Utils_Identity.cxx
1 //  SALOME Utils : general SALOME's definitions and tools
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : Utils_Identity.cxx
25 //  Author : Pascale NOYRET, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 # include <iostream>
30 # include <arpa/inet.h>
31 # include <netinet/in.h>
32 # include <sys/types.h>
33 # include <netdb.h>
34 # include "utilities.h"
35 # include "Utils_Identity.hxx"
36 using namespace std;
37 extern "C"
38 {
39 # include <string.h>
40 # include <pwd.h>
41 }
42
43 const char* duplicate( const char *const str ) ;
44
45 const struct utsname get_uname( void )
46 {
47         struct utsname          hostid;
48         const int retour=uname(&hostid);
49         ASSERT(retour>=0);
50         return hostid ;
51 }
52
53 const char* get_adip( void )
54 {
55         struct utsname  hostid;
56         const int retour=uname(&hostid);
57         ASSERT(retour>=0);
58
59         const hostent* pour_adip=gethostbyname(hostid.nodename);
60         ASSERT(pour_adip!=NULL);
61         const in_addr ip_addr=*(struct in_addr*)(pour_adip->h_addr) ;
62         return duplicate(inet_ntoa(ip_addr));
63 }
64 const char* const get_pwname( void )
65 {
66         struct passwd *papa = getpwuid( getuid() ) ;
67         return papa->pw_name ;
68 }
69
70 Identity::Identity( const char *name ): _name(duplicate(name)),\
71                                                         _hostid(get_uname()),\
72                                                         _adip(get_adip()),\
73                                                         _uid(getuid()) ,\
74                                                         _pwname(get_pwname()) ,\
75                                                         _dir(getcwd(NULL,0)),\
76                                                         _pid(getpid()) ,\
77                                                         _start(time(NULL)),\
78                                                         _cstart(ctime(&_start))
79 {
80         ;
81 }
82
83
84 Identity::~Identity(void)
85 {
86         delete [] (char*)_name ;
87         (char*&)_name = NULL ;
88
89         delete [] (char*)_dir ;
90         (char*&)_dir = NULL ;
91
92         delete [] (char*)_adip ;
93         (char*&)_adip = NULL ;
94 }
95
96 /*------------*/
97 /* Accessors  */
98 /*------------*/
99
100 const char* const Identity::name (void) const
101 {
102         return  _name ;
103 }
104 const pid_t &Identity::pid(void) const
105 {
106         return _pid ;
107 }
108 const struct utsname &Identity::hostid(void) const
109 {
110         return _hostid ;
111 }
112 const uid_t &Identity::uid(void) const
113 {
114         return _uid ;
115 }
116 const time_t &Identity::start(void) const
117 {
118         return _start ;
119 }
120 const char* const Identity::rep (void) const
121 {
122         return  _dir ;
123 }
124 const char* const Identity::pwname (void) const
125 {
126         return  _pwname ;
127 }
128 const char* const Identity::adip (void) const
129 {
130         return _adip ;
131 }
132
133 /*------------------*/
134 /* Other methods    */
135 /*------------------*/
136
137 const char* Identity::host_char( void ) const
138 {
139         return _hostid.nodename;
140 }
141
142 const char* Identity::start_char(void) const
143 {
144         return ctime(&_start) ;
145 }
146
147 ostream & operator<< ( ostream& os , const Identity& monid )
148 {
149         ASSERT(monid._name!=NULL) ;
150         os << "Identity :" << endl ;
151         os << '\t' << "Component name : " << monid._name << endl ;
152         os << '\t' << "Numero de PID :  " << monid._pid << endl;
153         os << '\t' << "Uid utilisateur  : "   << monid._uid << endl;
154         os << '\t' << "nom utilisateur  : "   << monid._pwname << endl;
155         os << '\t' << "Nom de machine : " << (monid._hostid).nodename << endl;
156         os << '\t' << "Adresse IP : " << monid._adip << endl;
157         os << '\t' << "Heure de lancement : " << monid._cstart ; //ctime(&monid._start) ;
158         os << '\t' << "Dans le repertoire : " << monid._dir << endl;
159
160         return os ;
161 }