# MEDmeshUniversalNameWr.c

Aller à la documentation de ce fichier.

    00001 /*  This file is part of MED.
    00002  *
    00003  *  COPYRIGHT (C) 1999 - 2012  EDF R&D, CEA/DEN
    00004  *  MED is free software: you can redistribute it and/or modify
    00005  *  it under the terms of the GNU Lesser General Public License as published by
    00006  *  the Free Software Foundation, either version 3 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  MED is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Lesser General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU Lesser General Public License
    00015  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
    00016  */
    00017
    00018
    00019 #include <med.h>
    00020 #include <med_config.h>
    00021 #include <med_outils.h>
    00022
    00023 #include <stdlib.h>
    00024 #include <stdio.h>
    00025 #include <string.h>
    00026
    00027 #ifdef PPRO_NT
    00028 // Windows Header Files:
    00029 #include <windows.h>
    00030 #include <Lmcons.h>
    00031 #include <sys/timeb.h>
    00032 #else
    00033
    00034 #if TIME_WITH_SYS_TIME
    00035 # include <sys/time.h>
    00036 # include <time.h>
    00037 #else
    00038 # if HAVE_SYS_TIME_H
    00039 #  include <sys/time.h>
    00040 # else
    00041 #  include <time.h>
    00042 # endif
    00043 #endif
    00044
    00045 #ifndef HAVE_UNISTD_H
    00046 #error "unistd.h required."
    00047 #endif
    00048
    00049 # include <unistd.h>
    00050
    00051 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
    00052 # include <sys/types.h>
    00053 # include <pwd.h>
    00054 #endif
    00055
    00056 #endif
    00057
    00066 med_err
    00067 MEDmeshUniversalNameWr(const med_idt fid,
    00068                        const char * const meshname)
    00069 {
    00070   med_access_mode _MED_ACCESS_MODE;
    00071   med_err _ret=-1;
    00072   med_idt _meshid=0;
    00073   char    _path [MED_TAILLE_MAA+MED_NAME_SIZE+1]=MED_MAA;
    00074   char    _unvname [MED_LNAME_SIZE+1]="";
    00075   time_t  _time;
    00076 #ifdef PPRO_NT
    00077   struct timeb   _tp;
    00078   char   _lpBuffer [UNLEN+1]="";
    00079   long   _nSize   = UNLEN+1;
    00080 #else
    00081   struct timeval _tp;
    00082   struct passwd* mypasswd;
    00083 #endif
    00084
    00085
    00086   /*
    00087    * On inhibe le gestionnaire d'erreur
    00088    */
    00089   _MEDmodeErreurVerrouiller();
    00090  if (_MEDcheckVersion30(fid) < 0) goto ERROR_;
    00091
    00092   if ( (_MED_ACCESS_MODE = _MEDmodeAcces(fid) ) == MED_ACC_UNDEF ) {
    00093     MED_ERR_(_ret,MED_ERR_UNRECOGNIZED,MED_ERR_ACCESSMODE,MED_ERR_FILE_MSG);
    00094     goto ERROR_;
    00095   }
    00096
    00097   if ( _MED_ACCESS_MODE == MED_ACC_RDONLY) {
    00098     MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE,MED_ERR_FILE_MSG);
    00099     ISCRUTE_int(_MED_ACCESS_MODE);
    00100     goto ERROR_;
    00101   }
    00102
    00103   /*
    00104    * Si le maillage n'existe pas => erreur
    00105    */
    00106   strcat(_path,meshname);
    00107   if ((_meshid = _MEDdatagroupOuvrir(fid,_path)) < 0) {
    00108     MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_DATAGROUP,_path);
    00109     ISCRUTE_id(_meshid);goto ERROR_;
    00110   }
    00111
    00112   /*
    00113    * Creation/Ecriture de l'attribut nom universel
    00114    */
    00115
    00116 #ifdef PPRO_NT
    00117
    00118   if ( GetUserName(_lpBuffer,&_nSize) == 0 ) goto ERROR_;
    00119   if ( _nSize > MED_NAME_SIZE ) _nSize = MED_NAME_SIZE;
    00120   strncpy(_unvname,_lpBuffer,_nSize);
    00121   strcat(_unvname," ");
    00122   _time=time(&_time);
    00123   strcat(_unvname,ctime(&_time));
    00124   ftime(&_tp);
    00125   _nSize = strlen(_unvname)-1;
    00126   if ( sprintf(&_unvname[_nSize]," %hu",_tp.millitm) < 0 ) goto ERROR_;
    00127
    00128 #else
    00129 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
    00130   mypasswd=getpwuid(geteuid());
    00131   if ( mypasswd == NULL ) {
    00132     MESSAGE("Impossible d'obtenir le nom de l'utilisateur effectif");
    00133     goto ERROR_;
    00134   }
    00135   strcat(_unvname,mypasswd->pw_name);
    00136 #elif defined(HAVE_CUSERID)
    00137   if ( !cuserid(_unvname) ) {
    00138     MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"cuserid");
    00139     SSCRUTE(_path); goto ERROR_;
    00140   }
    00141 #else
    00142 #error "There is no ( getpwuid && geteuid) nor cuserid"
    00143 #endif
    00144   strcat(_unvname," ");
    00145   _time=time(&_time);
    00146   strcat(_unvname,ctime(&_time));
    00147   if ( gettimeofday(&_tp,NULL) < 0 ) {
    00148     MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"gettimeofday");
    00149     SSCRUTE(_path); goto ERROR_;
    00150   }
    00151   if ( sprintf(&_unvname[strlen(_unvname)-1]," %li",_tp.tv_usec) < 0 ) {
    00152     MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"sprintf");
    00153     SSCRUTE(_path); goto ERROR_;
    00154   }
    00155 #endif
    00156
    00157   if ( _MEDattributeStringWr(_meshid,MED_NOM_UNV,MED_LNAME_SIZE,_unvname) < 0) {
    00158     MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE,MED_ERR_MESH_MSG);
    00159     SSCRUTE(meshname);SSCRUTE(MED_NOM_UNI);
    00160     SSCRUTE(_unvname);goto ERROR_;
    00161   }
    00162
    00163   _ret = 0;
    00164  ERROR_:
    00165
    00166   if (_meshid>0)            if (_MEDdatagroupFermer(_meshid) < 0) {
    00167     MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,_path);
    00168     ISCRUTE_id(_meshid);
    00169   }
    00170
    00171   return _ret;
    00172 }
    00173