SURFEX v8.1
General documentation of Surfex
endian.c
Go to the documentation of this file.
1 /* endian.c */
2 
3 /*
4  Please note: the following 2 routines
5  cannot be named as "is_little_endian()"
6  and "is_big_endian()", since there is a clash
7  with the new Magics++ library (by SS, 21-Mar-2006)
8 
9  --> consequently "ec_" prefix was added
10 */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <signal.h>
15 #include <stdarg.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 
20 {
21  /* Little/big-endian runtime auto-detection */
22  const unsigned int ulbtest = 0x12345678;
23  const unsigned char *clbtest = (const unsigned char *)&ulbtest;
24 
25  if (*clbtest == 0x78) {
26  /* We are on a little-endian machine */
27  return 1;
28  }
29  else {
30  /* We are on a big-endian machine */
31  return 0;
32  }
33 }
34 
36 {
37  return !ec_is_little_endian();
38 }
39 
40 /* Fortran interface */
41 
44 
45 /* A routine to be called at the very end in case MPI wasn't finalized */
46 /* Registered *only* by MPL_INIT */
47 /* Disable this feature via : export EC_MPI_ATEXIT=0 */
48 
49 void ec_mpi_atexit_(void)
50 {
51  char *env = getenv("EC_MPI_ATEXIT");
52  int do_it = env ? atoi(env) : 1;
53  static int callnum = 0;
54  ++callnum;
55  if (do_it) {
56  if (callnum == 1) {
57  /* register */
58  atexit(ec_mpi_atexit_);
59  }
60  else if (callnum == 2) {
61  /* action : finish MPI via F90 cmpl_end (in cmpl_binding.F90) */
62 #ifdef SFX_MPI
63  extern void cmpl_end_(int *);
64  int ierr = 0;
65  cmpl_end_(&ierr);
66 #endif
67  }
68  }
69 }
70 
71 void ec_mpi_atexit(void)
72 {
74 }
75 
76 void ec_set_umask_(void)
77 {
78  char *env = getenv("EC_SET_UMASK");
79  if (env) {
80  int newmask;
81  int n = sscanf(env,"%o",&newmask);
82  if (n == 1) {
83  int oldmask = umask(newmask);
84  fprintf(stderr,
85  "*** EC_SET_UMASK : new/old = %o/%o (oct), %d/%d (dec), %x/%x (hex)\n",
86  newmask,oldmask,
87  newmask,oldmask,
88  newmask,oldmask);
89  } /* if (n == 1) */
90  } /* if (env) */
91 }
92 
93 
94 /* CALL ec_raise(6) == CALL abort() */
95 
96 void ec_raise_(const int *sig) { raise(*sig); }
97 void ec_raise(const int *sig) { ec_raise_(sig); }
98 
99 /* CALL ec_exit(iexit_code) */
100 
101 void ec_exit_(const int *exit_code) { exit(exit_code ? *exit_code : 0); }
102 void ec_exit(const int *exit_code) { ec_exit_(exit_code); }
103 
104 /* snprintf replacement for VPP/VPP5000's */
105 
106 #if defined(VPP5000) || defined(VPP)
107 
108 int snprintf(char *str, size_t size, const char *format, ...)
109 {
110  int rc;
111  va_list ap;
112  va_start(ap, format);
113  rc = vsprintf(str, format, ap);
114  va_end(ap);
115  return rc;
116 }
117 
118 #endif
static long size
Definition: bytes_io.c:262
int ec_is_big_endian()
Definition: endian.c:35
void ec_mpi_atexit_(void)
Definition: endian.c:49
int ec_is_little_endian_()
Definition: endian.c:43
void ec_exit(const int *exit_code)
Definition: endian.c:102
void ec_set_umask_(void)
Definition: endian.c:76
void ec_exit_(const int *exit_code)
Definition: endian.c:101
int ec_is_little_endian()
Definition: endian.c:19
int snprintf(char *str, size_t size, const char *format,...)
Definition: endian.c:108
ERROR in n
Definition: ecsort_shared.h:90
void ec_mpi_atexit(void)
Definition: endian.c:71
int ec_is_big_endian_()
Definition: endian.c:42
void ec_raise(const int *sig)
Definition: endian.c:97
void ec_raise_(const int *sig)
Definition: endian.c:96