/src/netcdf-c/libdispatch/ddispatch.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "ncpathmgr.h" |
14 | | #include "ncxml.h" |
15 | | #include "nc4internal.h" |
16 | | |
17 | | /* Required for getcwd, other functions. */ |
18 | | #ifdef HAVE_UNISTD_H |
19 | | #include <unistd.h> |
20 | | #endif |
21 | | |
22 | | /* Required for getcwd, other functions. */ |
23 | | #ifdef _WIN32 |
24 | | #include <direct.h> |
25 | | #endif |
26 | | |
27 | | #if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4) |
28 | | #include <curl/curl.h> |
29 | | #endif |
30 | | |
31 | | #ifdef ENABLE_S3 |
32 | | #include "ncs3sdk.h" |
33 | | #endif |
34 | | |
35 | 0 | #define MAXPATH 1024 |
36 | | |
37 | | |
38 | | |
39 | | /* Define vectors of zeros and ones for use with various nc_get_varX functions */ |
40 | | /* Note, this form of initialization fails under Cygwin */ |
41 | | size_t NC_coord_zero[NC_MAX_VAR_DIMS] = {0}; |
42 | | size_t NC_coord_one[NC_MAX_VAR_DIMS] = {1}; |
43 | | ptrdiff_t NC_stride_one[NC_MAX_VAR_DIMS] = {1}; |
44 | | |
45 | | /* |
46 | | static nc_type longtype = (sizeof(long) == sizeof(int)?NC_INT:NC_INT64); |
47 | | static nc_type ulongtype = (sizeof(unsigned long) == sizeof(unsigned int)?NC_UINT:NC_UINT64); |
48 | | */ |
49 | | |
50 | | /* Allow dispatch to do general initialization and finalization */ |
51 | | int |
52 | | NCDISPATCH_initialize(void) |
53 | 1 | { |
54 | 1 | int status = NC_NOERR; |
55 | 1 | int i; |
56 | 1 | NCglobalstate* globalstate = NULL; |
57 | | |
58 | 1.02k | for(i=0;i<NC_MAX_VAR_DIMS;i++) { |
59 | 1.02k | NC_coord_zero[i] = 0; |
60 | 1.02k | NC_coord_one[i] = 1; |
61 | 1.02k | NC_stride_one[i] = 1; |
62 | 1.02k | } |
63 | | |
64 | 1 | globalstate = NC_getglobalstate(); /* will allocate and clear */ |
65 | | |
66 | | /* Capture temp dir*/ |
67 | 1 | { |
68 | 1 | char* tempdir = NULL; |
69 | | #if defined _WIN32 || defined __MSYS__ || defined __CYGWIN__ |
70 | | tempdir = getenv("TEMP"); |
71 | | #else |
72 | 1 | tempdir = "/tmp"; |
73 | 1 | #endif |
74 | 1 | if(tempdir == NULL) { |
75 | 0 | fprintf(stderr,"Cannot find a temp dir; using ./\n"); |
76 | 0 | tempdir = "."; |
77 | 0 | } |
78 | 1 | globalstate->tempdir= strdup(tempdir); |
79 | 1 | } |
80 | | |
81 | | /* Capture $HOME */ |
82 | 1 | { |
83 | | #if defined(_WIN32) && !defined(__MINGW32__) |
84 | | char* home = getenv("USERPROFILE"); |
85 | | #else |
86 | 1 | char* home = getenv("HOME"); |
87 | 1 | #endif |
88 | 1 | if(home == NULL) { |
89 | | /* use cwd */ |
90 | 0 | home = malloc(MAXPATH+1); |
91 | 0 | NCgetcwd(home,MAXPATH); |
92 | 0 | } else |
93 | 1 | home = strdup(home); /* make it always free'able */ |
94 | 1 | assert(home != NULL); |
95 | 1 | NCpathcanonical(home,&globalstate->home); |
96 | 1 | nullfree(home); |
97 | 1 | } |
98 | | |
99 | | /* Capture $CWD */ |
100 | 0 | { |
101 | 1 | char cwdbuf[4096]; |
102 | | |
103 | 1 | cwdbuf[0] = '\0'; |
104 | 1 | (void)NCgetcwd(cwdbuf,sizeof(cwdbuf)); |
105 | | |
106 | 1 | if(strlen(cwdbuf) == 0) { |
107 | | /* use tempdir */ |
108 | 0 | strcpy(cwdbuf, globalstate->tempdir); |
109 | 0 | } |
110 | 1 | globalstate->cwd = strdup(cwdbuf); |
111 | 1 | } |
112 | | |
113 | 1 | ncloginit(); |
114 | | |
115 | | /* Now load RC Files */ |
116 | 1 | ncrc_initialize(); |
117 | | |
118 | | /* Compute type alignments */ |
119 | 1 | NC_compute_alignments(); |
120 | | |
121 | 1 | #if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4) |
122 | | /* Initialize curl if it is being used */ |
123 | 1 | { |
124 | 1 | CURLcode cstat = curl_global_init(CURL_GLOBAL_ALL); |
125 | 1 | if(cstat != CURLE_OK) |
126 | 0 | status = NC_ECURL; |
127 | 1 | } |
128 | 1 | #endif |
129 | | |
130 | 1 | return status; |
131 | 1 | } |
132 | | |
133 | | int |
134 | | NCDISPATCH_finalize(void) |
135 | 1 | { |
136 | 1 | int status = NC_NOERR; |
137 | 1 | NC_freeglobalstate(); |
138 | 1 | #if defined(ENABLE_BYTERANGE) || defined(ENABLE_DAP) || defined(ENABLE_DAP4) |
139 | 1 | curl_global_cleanup(); |
140 | 1 | #endif |
141 | | #if defined(ENABLE_DAP4) |
142 | | ncxml_finalize(); |
143 | | #endif |
144 | 1 | return status; |
145 | 1 | } |