Coverage Report

Created: 2026-06-30 08:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/netcdf-c-4.7.4/libdispatch/ddispatch.c
Line
Count
Source
1
/*
2
Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
3
See LICENSE.txt for license information.
4
*/
5
6
#include "config.h"
7
#include "ncdispatch.h"
8
#include "ncuri.h"
9
#include "nclog.h"
10
#include "ncbytes.h"
11
#include "ncrc.h"
12
#include "ncoffsets.h"
13
14
/* Required for getcwd, other functions. */
15
#ifdef HAVE_UNISTD_H
16
#include <unistd.h>
17
#endif
18
19
/* Required for getcwd, other functions. */
20
#ifdef _MSC_VER
21
#include <direct.h>
22
#define getcwd _getcwd
23
#endif
24
25
#if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4)
26
#include <curl/curl.h>
27
#endif
28
29
/* Define vectors of zeros and ones for use with various nc_get_varX functions */
30
size_t NC_coord_zero[NC_MAX_VAR_DIMS] = {0};
31
size_t NC_coord_one[NC_MAX_VAR_DIMS] = {1};
32
ptrdiff_t NC_stride_one[NC_MAX_VAR_DIMS] = {1};
33
34
NCRCglobalstate ncrc_globalstate;
35
36
/*
37
static nc_type longtype = (sizeof(long) == sizeof(int)?NC_INT:NC_INT64);
38
static nc_type ulongtype = (sizeof(unsigned long) == sizeof(unsigned int)?NC_UINT:NC_UINT64);
39
*/
40
41
/* Allow dispatch to do general initialization and finalization */
42
int
43
NCDISPATCH_initialize(void)
44
9
{
45
9
    int status = NC_NOERR;
46
9
    int i;
47
9
    NCRCglobalstate* globalstate = NULL;
48
49
9.22k
    for(i=0;i<NC_MAX_VAR_DIMS;i++) {
50
9.21k
        NC_coord_zero[i] = 0;
51
9.21k
        NC_coord_one[i]  = 1;
52
9.21k
        NC_stride_one[i] = 1;
53
9.21k
    }
54
55
9
    globalstate = ncrc_getglobalstate(); /* will allocate and clear */
56
57
    /* Capture temp dir*/
58
9
    {
59
9
  char* tempdir;
60
9
  char* p;
61
9
  char* q;
62
9
  char cwd[4096];
63
#ifdef _MSC_VER
64
        tempdir = getenv("TEMP");
65
#else
66
9
  tempdir = "/tmp";
67
9
#endif
68
9
        if(tempdir == NULL) {
69
0
      fprintf(stderr,"Cannot find a temp dir; using ./\n");
70
0
      tempdir = getcwd(cwd,sizeof(cwd));
71
0
      if(tempdir == NULL || *tempdir == '\0') tempdir = ".";
72
0
  }
73
9
        globalstate->tempdir= (char*)malloc(strlen(tempdir) + 1);
74
45
  for(p=tempdir,q=globalstate->tempdir;*p;p++,q++) {
75
36
      if((*p == '/' && *(p+1) == '/')
76
36
         || (*p == '\\' && *(p+1) == '\\')) {p++;}
77
36
      *q = *p;
78
36
  }
79
9
  *q = '\0';
80
#ifdef _MSC_VER
81
#else
82
        /* Canonicalize */
83
45
  for(p=globalstate->tempdir;*p;p++) {
84
36
      if(*p == '\\') {*p = '/'; };
85
36
  }
86
9
  *q = '\0';
87
9
#endif
88
9
    }
89
90
    /* Capture $HOME */
91
9
    {
92
9
  char* p;
93
9
  char* q;
94
9
        char* home = getenv("HOME");
95
96
9
        if(home == NULL) {
97
      /* use tempdir */
98
0
      home = globalstate->tempdir;
99
0
  }
100
9
        globalstate->home = (char*)malloc(strlen(home) + 1);
101
54
  for(p=home,q=globalstate->home;*p;p++,q++) {
102
45
      if((*p == '/' && *(p+1) == '/')
103
45
         || (*p == '\\' && *(p+1) == '\\')) {p++;}
104
45
      *q = *p;
105
45
  }
106
9
  *q = '\0';
107
#ifdef _MSC_VER
108
#else
109
        /* Canonicalize */
110
54
  for(p=home;*p;p++) {
111
45
      if(*p == '\\') {*p = '/'; };
112
45
  }
113
9
#endif
114
9
    }
115
116
    /* Now load RC File */
117
9
    status = NC_rcload();
118
9
    ncloginit();
119
120
    /* Compute type alignments */
121
9
    NC_compute_alignments();
122
123
    /* Initialize curl if it is being used */
124
#if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4)
125
    {
126
        CURLcode cstat = curl_global_init(CURL_GLOBAL_ALL);
127
  if(cstat != CURLE_OK)
128
      status = NC_ECURL;
129
    }
130
#endif
131
9
    return status;
132
9
}
133
134
int
135
NCDISPATCH_finalize(void)
136
0
{
137
0
    int status = NC_NOERR;
138
0
    ncrc_freeglobalstate();
139
#if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4)
140
    curl_global_cleanup();
141
#endif
142
0
    return status;
143
0
}