gbenv.c 6.39 KB
Newer Older
1
/*
2 3
  gbenv  (ver. 5.6) -- Floating point locale, and gbutils settings
  Copyright (C) 2006-2015 Giulio Bottazzi
4 5 6 7 8 9 10 11 12 13 14 15

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  (version 2) as published by the Free Software Foundation;
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
16
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 18 19 20 21 22 23 24 25 26 27 28 29 30
*/

#include "tools.h"

#ifdef linux
#include <fpu_control.h>
#include <fenv.h>
#endif

#include <langinfo.h>
#include <locale.h>

int main(int argc,char* argv[]){

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  /* variables for reading command line options */
  /* ------------------------------------------ */
  char opt;
  /* ------------------------------------------ */

  /* COMMAND LINE PROCESSING */
      
  while((opt=getopt_long(argc,argv,"h",gb_long_options, &gb_option_index))!=EOF){
    if(opt==0){
      gbutils_header(argv[0],stdout);
      exit(0);
    }
    else if(opt=='?'){
      fprintf(stderr,"option %c not recognized\n",optopt);
      return(-1);
    }
    else if(opt=='h'){
      /*print help*/
49 50
      fprintf(stdout,"Show floating point environment (for x86 family processors), locale and\n");
      fprintf(stdout,"gbutils settings.\n");
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
      fprintf(stdout,"   \n");
      fprintf(stdout,"Information about the floating point environment includes:\n");
      fprintf(stdout,"  if the environment is the default environment of the machine;\n");
      fprintf(stdout,"  if the precision is single, double or extended;\n");
      fprintf(stdout,"  if the rounding is nearest, upward, downward or toward zero;\n");
      fprintf(stdout,"  the list of exceptions whose interrupts are ignored (masked).\n");
      fprintf(stdout,"Information about the locale includes:    \n");
      fprintf(stdout,"  decimal point;\n");
      fprintf(stdout,"  thousand separator;\n");
      fprintf(stdout,"  the relative position of the thousand separator (grouping).\n");
      fprintf(stdout,"Information about gbutils environment include:\n");
      fprintf(stdout,"  if the error handler is activated;\n");
      fprintf(stdout,"  the value of the environment variables dictating the output format;\n");
      fprintf(stdout,"  the output format of integers and floating point numbers.\n");
      fprintf(stdout,"Options:                                                               \n");
      fprintf(stdout," -h  print this help message                                           \n");
      return(0);
    }
  }
  /* END OF COMMAND LINE PROCESSING */

72

73 74
/* ---- linux implementation and x86 processor ---- */
#if defined(linux) && ( defined(__x86_64__) || defined(__i386) )
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

  unsigned short mode = 0 ;

  int rounding =  fegetround ();

  printf(" floating point environment ------------------------\n");

#ifdef _FPU_GETCW
  _FPU_GETCW(mode);

#define PREC_MASK  _FPU_EXTENDED

  printf(" The environment is ");
  if( mode==_FPU_DEFAULT ) 
    printf("the default\n");
  else
    printf("not the default\n");

  /* precision */
  printf(" Precision is ");
  if ( (mode & PREC_MASK) == _FPU_EXTENDED)
    printf("extended");
  else if ( (mode & PREC_MASK) == _FPU_DOUBLE)
    printf("double");
  else if ( (mode & PREC_MASK) == _FPU_SINGLE)
    printf("single");
  printf("\n");
#endif
  
  /* rounding */
  printf(" Rounding is ");
  switch(rounding) {
  case FE_TONEAREST :
    printf("nearest\n");
    break;
  case FE_UPWARD :
    printf("upward\n");
    break;
  case FE_DOWNWARD :
    printf("downward\n");
    break;
  case FE_TOWARDZERO :
    printf("toward zero\n");
    break;
  }


  /* exception */
  printf(" Masked exceptions (no interrupt):\n");
#ifdef FE_INVALID
  if(!fetestexcept(FE_INVALID)) printf("    Invalid operation\n");
#endif
#ifdef FE_DIVBYZERO
  if(!fetestexcept(FE_DIVBYZERO)) printf("    Division by zero\n");
#endif
#ifdef FE_UNDERFLOW
  if(!fetestexcept(FE_UNDERFLOW)) printf("    Underflow\n");
#endif
#ifdef FE_OVERFLOW
  if(!fetestexcept(FE_OVERFLOW)) printf("    Overflow\n");
#endif
#ifdef FE_INEXACT
  if(!fetestexcept(FE_INEXACT)) printf("    Precision\n");
#endif
  printf("\n");

#endif
/* ------------------------------ */


145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  printf(" locale definitions --------------------------------\n");
  {

    if (setlocale(LC_NUMERIC,"") == NULL)
      fprintf(stderr,"WARNING:Cannot set LC_NUMERIC to default locale\n");

    printf(" decimal point        \"%s\"\n",nl_langinfo(RADIXCHAR));
    printf(" thousands separator  \"%s\"\n",nl_langinfo(THOUSEP));
    printf(" grouping             ");

    {/* code snippet stolen from locale.c in glibc */

      struct lconv *locale = localeconv();

      const char *val = locale->grouping;
      int cnt = val ? strlen (val) : 0;

      while (cnt > 1){
        printf ("%d;", *val == '\177' ? -1 : *val);
        --cnt;
        ++val;
      }
      
      printf ("%d\n", cnt == 0 || *val == '\177' ? -1 : *val);
    }

  }
  printf("\n");

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

#if defined HAVE_LIBGSL
  printf(" gsl environment -----------------------------------\n");
  printf(" GSL error handler is ");
  if(getenv("GB_ERROR_HANDLER_OFF"))
    printf(" off\n");
  else
    printf(" on\n");
  printf("\n");
#endif


  printf(" gbutils environment -------------------------------\n");

  printf(" GB_OUT_FLOAT_FORMAT is ");
  if(FLOAT==NULL && getenv("GB_OUT_FLOAT_FORMAT"))
    printf("%s\n",getenv("GB_OUT_FLOAT_FORMAT"));
  else
    printf("not set\n");

  printf(" GB_OUT_EMPTY_FORMAT is ");
  if(FLOAT==NULL && getenv("GB_OUT_EMPTY_FORMAT"))
    printf("%s\n",getenv("GB_OUT_EMPTY_FORMAT"));
  else
    printf("not set\n");

  printf(" GB_OUT_SEP is ");
  if(FLOAT==NULL && getenv("GB_OUT_SEP"))
    printf("%s\n",getenv("GB_OUT_SEP"));
  else
    printf("not set\n");

  printf(" GB_OUT_NL is ");
  if(FLOAT==NULL && getenv("GB_OUT_NL"))
    printf("%s\n",getenv("GB_OUT_NL"));
  else
    printf("not set\n");

  printf("\n");

  printf(" gbutils settings ----------------------------------\n");
  
  initialize_program(argv[0]);

  printf(" floating point output   \"%s\"\n",FLOAT);
  printf(" integer output          \"%s\"\n",INT);
  printf(" empty field             \"%s\"\n",EMPTY);
  printf(" separation string       \"%s\"\n",SEP);

  return 0;
}