MED fichier: Fichier source de MEDmeshUniversalNameWr.c

MED fichier

MEDmeshUniversalNameWr.c
Aller à la documentation de ce fichier.
1 /* This file is part of MED.
2 *
3 * COPYRIGHT (C) 1999 - 2021 EDF R&D, CEA/DEN
4 * MED is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * MED is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with MED. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19 #include <med.h>
20 #include <med_config.h>
21 #include <med_outils.h>
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #ifdef PPRO_NT_CALL
28 // Windows Header Files:
29 # include <windows.h>
30 # include <Lmcons.h>
31 # include <sys/timeb.h>
32 # include <time.h>
33 #else
34
35 # if HAVE_SYS_TIME_H
36 # include <sys/time.h>
37 # endif
38
39 # include <time.h>
40
41 #ifndef HAVE_UNISTD_H
42 #error "unistd.h required."
43 #endif
44
45 #include <unistd.h>
46
47 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
48 # include <sys/types.h>
49 # include <pwd.h>
50 #endif
51
52 #endif
53
62 med_err
63 MEDmeshUniversalNameWr(const med_idt fid,
64 const char * const meshname)
65 {
66 med_access_mode _MED_ACCESS_MODE;
67 med_err _ret=-1;
68 med_idt _meshid=0;
69 char _path [MED_MESH_GRP_SIZE+MED_NAME_SIZE+1]=MED_MESH_GRP;
70 char _unvname [MED_LNAME_SIZE+1]="";
71 time_t _time;
72 #ifdef PPRO_NT_CALL
73 struct timeb _tp;
74 char _lpBuffer [UNLEN+1]="";
75 long _nSize = UNLEN+1;
76 #else
77 struct timeval _tp;
78 struct passwd* mypasswd;
79 #endif
80
81
82 /*
83 * On inhibe le gestionnaire d'erreur
84 */
85 _MEDmodeErreurVerrouiller();
86 if (_MEDcheckVersion30(fid) < 0) goto ERROR_;
87
88 if ( (_MED_ACCESS_MODE = _MEDmodeAcces(fid) ) == MED_ACC_UNDEF ) {
89 MED_ERR_(_ret,MED_ERR_UNRECOGNIZED,MED_ERR_ACCESSMODE,MED_ERR_FILE_MSG);
90 goto ERROR_;
91 }
92
93 if ( _MED_ACCESS_MODE == MED_ACC_RDONLY) {
94 MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE,MED_ERR_FILE_MSG);
95 ISCRUTE_int(_MED_ACCESS_MODE);
96 goto ERROR_;
97 }
98
99 /*
100 * Si le maillage n'existe pas => erreur
101 */
102 strcat(_path,meshname);
103 if ((_meshid = _MEDdatagroupOuvrir(fid,_path)) < 0) {
104 MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_DATAGROUP,_path);
105 ISCRUTE_id(_meshid);goto ERROR_;
106 }
107
108 /*
109 * Creation/Ecriture de l'attribut nom universel
110 */
111
112 #ifdef PPRO_NT_CALL
113
114 if ( GetUserName(_lpBuffer,&_nSize) == 0 ) goto ERROR_;
115 if ( _nSize > MED_NAME_SIZE ) _nSize = MED_NAME_SIZE;
116 strncpy(_unvname,_lpBuffer,_nSize);
117 strcat(_unvname," ");
118 _time=time(&_time);
119 strcat(_unvname,ctime(&_time));
120 ftime(&_tp);
121 _nSize = strlen(_unvname)-1;
122 if ( sprintf(&_unvname[_nSize]," %hu",_tp.millitm) < 0 ) goto ERROR_;
123
124 #else
125 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
126 mypasswd=getpwuid(geteuid());
127 if ( mypasswd == NULL ) {
128 MESSAGE("Impossible d'obtenir le nom de l'utilisateur effectif");
129 goto ERROR_;
130 }
131 strcat(_unvname,mypasswd->pw_name);
132 #elif defined(HAVE_CUSERID)
133 if ( !cuserid(_unvname) ) {
134 MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"cuserid");
135 SSCRUTE(_path); goto ERROR_;
136 }
137 #else
138 #error "There is no ( getpwuid && geteuid) nor cuserid"
139 #endif
140 strcat(_unvname," ");
141 _time=time(&_time);
142 strcat(_unvname,ctime(&_time));
143 if ( gettimeofday(&_tp,NULL) < 0 ) {
144 MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"gettimeofday");
145 SSCRUTE(_path); goto ERROR_;
146 }
147 if ( sprintf(&_unvname[strlen(_unvname)-1]," %li",_tp.tv_usec) < 0 ) {
148 MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"sprintf");
149 SSCRUTE(_path); goto ERROR_;
150 }
151 #endif
152
153 if ( _MEDattributeStringWr(_meshid,MED_NOM_UNV,MED_LNAME_SIZE,_unvname) < 0) {
154 MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE,MED_ERR_MESH_MSG);
155 SSCRUTE(meshname);SSCRUTE(MED_NOM_UNI);
156 SSCRUTE(_unvname);goto ERROR_;
157 }
158
159 _ret = 0;
160 ERROR_:
161
162 if (_meshid>0) if (_MEDdatagroupFermer(_meshid) < 0) {
163 MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,_path);
164 ISCRUTE_id(_meshid);
165 }
166
167 return _ret;
168 }
169
MED_ERR_API
#define MED_ERR_API
Definition: med_err.h:111
_MEDdatagroupFermer
MEDC_EXPORT med_err _MEDdatagroupFermer(med_idt id)
ISCRUTE_id
#define ISCRUTE_id(entier)
Definition: med_utils.h:319
MEDmeshUniversalNameWr
med_err MEDmeshUniversalNameWr(const med_idt fid, const char *const meshname)
Cette routine permet l'écriture du nom universel d'un maillage.
Definition: MEDmeshUniversalNameWr.c:63
_MEDmodeAcces
MEDC_EXPORT med_access_mode _MEDmodeAcces(med_idt oid)
ISCRUTE_int
#define ISCRUTE_int(entier)
Definition: med_utils.h:314
med_idt
hid_t med_idt
Definition: med.h:331
MED_ERR_
#define MED_ERR_(rt, r1, r2, r3)
Definition: med_utils.h:160
MED_ACC_RDONLY
Definition: med.h:120
MED_ERR_INVALID
#define MED_ERR_INVALID
Definition: med_err.h:40
MED_ERR_ATTRIBUTE
#define MED_ERR_ATTRIBUTE
Definition: med_err.h:100
med_err
herr_t med_err
Definition: med.h:332
MED_MESH_GRP
#define MED_MESH_GRP
Definition: med_outils.h:160
MED_ERR_CLOSE
#define MED_ERR_CLOSE
Definition: med_err.h:30
MED_ERR_OPEN
#define MED_ERR_OPEN
Definition: med_err.h:37
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:324
MED_NOM_UNV
#define MED_NOM_UNV
Definition: med_outils.h:42
MED_ERR_WRITE
#define MED_ERR_WRITE
Definition: med_err.h:29
MED_ERR_CALL
#define MED_ERR_CALL
Definition: med_err.h:48
med.h
MED_MESH_GRP_SIZE
#define MED_MESH_GRP_SIZE
Definition: med_outils.h:161
med_access_mode
med_access_mode
Definition: med.h:120
MED_ERR_ACCESSMODE
#define MED_ERR_ACCESSMODE
Definition: med_err.h:104
_MEDattributeStringWr
MEDC_EXPORT med_err _MEDattributeStringWr(med_idt pid, const char *const attname, const med_size attsize, const char *const val)
SSCRUTE
#define SSCRUTE(chaine)
Definition: med_utils.h:323
MED_ERR_DATAGROUP
#define MED_ERR_DATAGROUP
Definition: med_err.h:99
MED_LNAME_SIZE
#define MED_LNAME_SIZE
Definition: med.h:83
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:81
_MEDcheckVersion30
MEDC_EXPORT med_err _MEDcheckVersion30(med_idt fid)
med_outils.h
med_config.h
MED_NOM_UNI
#define MED_NOM_UNI
Definition: med_outils.h:62
MED_ERR_MESH_MSG
#define MED_ERR_MESH_MSG
Definition: med_err.h:133
MED_ERR_UNRECOGNIZED
#define MED_ERR_UNRECOGNIZED
Definition: med_err.h:39
MED_ERR_FILE_MSG
#define MED_ERR_FILE_MSG
Definition: med_err.h:132
_MEDdatagroupOuvrir
MEDC_EXPORT med_idt _MEDdatagroupOuvrir(med_idt pid, const char *const nom)
_MEDmodeErreurVerrouiller
MEDC_EXPORT void _MEDmodeErreurVerrouiller(void)
MED_ACC_UNDEF
Definition: med.h:124