OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
tmpenv_c.c
Go to the documentation of this file.
1//Copyright> OpenRadioss
2//Copyright> Copyright (C) 1986-2025 Altair Engineering Inc.
3//Copyright>
4//Copyright> This program is free software: you can redistribute it and/or modify
5//Copyright> it under the terms of the GNU Affero General Public License as published by
6//Copyright> the Free Software Foundation, either version 3 of the License, or
7//Copyright> (at your option) any later version.
8//Copyright>
9//Copyright> This program is distributed in the hope that it will be useful,
10//Copyright> but WITHOUT ANY WARRANTY; without even the implied warranty of
11//Copyright> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12//Copyright> GNU Affero General Public License for more details.
13//Copyright>
14//Copyright> You should have received a copy of the GNU Affero General Public License
15//Copyright> along with this program. If not, see <https://www.gnu.org/licenses/>.
16//Copyright>
17//Copyright>
18//Copyright> Commercial Alternative: Altair Radioss Software
19//Copyright>
20//Copyright> As an alternative to this open-source version, Altair also offers Altair Radioss
21//Copyright> software under a commercial license. Contact Altair to discuss further if the
22//Copyright> commercial version may interest you: https://www.altair.com/radioss/.
23#include "hardware.inc"
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/types.h>
29
30#define _FCALL
31
32
33#ifdef _WIN64
34
35#include <Windows.h>
36#include <direct.h>
37
38#define my_getpid (int)GetCurrentProcessId()
39
40#else
41
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <unistd.h>
45#include <dirent.h>
46
47#define my_getpid getpid()
48
49#endif
50
51
52#ifdef _WIN64
53
54
55char* tmpenv_c(){
56// ---------------------------------------------------------------------------------------
57// tmpenv_c : give TMPDIR environment variable. If not set,give current working directory
58// WINDOWS CODE
59// ---------------------------------------------------------------------------------------
60// INPUT
61// OUTPUT
62// char * : returns tmpdir
63// ----------------------------------------------------------------------------
64 char * tmpdir;
65 int sz_tmpdir;
66 DWORD fileattr;
67
68 tmpdir=(char *)malloc(sizeof(char)*2048);
69 sz_tmpdir = GetEnvironmentVariable("TMPDIR",tmpdir,2048);
70
71 /* Check if TMPDIR is a directory */
72 if (sz_tmpdir > 0){
73 fileattr = GetFileAttributesA(tmpdir);
74
75 // Local directory : if directory does not exist or it is not a directory
76 if ( fileattr == INVALID_FILE_ATTRIBUTES || !(fileattr & FILE_ATTRIBUTE_DIRECTORY) ){
77 sz_tmpdir = 0;
78 }
79 }
80
81 /* second trial get current working directory */
82 if (sz_tmpdir == 0){
83
84 sz_tmpdir = GetCurrentDirectory( 2048,tmpdir);
85 }
86 fflush(stdout);
87 return tmpdir;
88
89}
90
91#elif 1
92// ---------------------------------------------------------------------------------------
93// tmpenv_c : give TMPDIR environment variable. If not set,give current working directory
94// LINUX CODE
95// ---------------------------------------------------------------------------------------
96// INPUT
97// OUTPUT
98// char * : returns tmpdir
99// ----------------------------------------------------------------------------
100char* tmpenv_c(){
101
102 char * tmpdir;
103 DIR* directory;
104
105 tmpdir = getenv("TMPDIR");
106
107 if (tmpdir != NULL){
108 directory = opendir(tmpdir); // check if directory exists
109
110 if (directory == NULL){
111 tmpdir=NULL;
112 }else{
113 closedir(directory);
114 }
115 }
116
117 /* second trial get current working directory */
118 if (tmpdir==NULL){
119 tmpdir = (char *)calloc(200,sizeof(char));
120 getcwd(tmpdir,200);
121 }
122
123 return tmpdir;
124}
125
126#endif
127
128
129
130// --------------------------------------------------------------------------------------------------
131// tmpenv_c : give TMPDIR environment variable to Fortran. If not set,give current working directory
132// LINUX CODE
133// --------------------------------------------------------------------------------------------------
134// INPUT
135// OUTPUT
136// char * : returns tmpdir
137// ----------------------------------------------------------------------------
138void tmpenvf_(char* tmpdir,int *tmplen){
139
140 char * tdir= tmpenv_c();
141 int slen=strlen(tdir);
142 int i;
143
144 for (i=0;i<slen;i++) tmpdir[i]=tdir[i];
145 tmpdir[slen]='\0';
146 *tmplen = slen;
147}
148
149void tmpenvf(char* tmpdir,int *tmplen){
150 tmpenvf_(tmpdir,tmplen);
151}
152
153void tmpenvf__(char* tmpdir,int *tmplen){
154 tmpenvf_(tmpdir,tmplen);
155}
156
157void _FCALL TMPENVF (char* tmpdir,int* tmplen){
158 tmpenvf_(tmpdir,tmplen);
159}
160
161
162// ---------------------------------------------------------------------------------
163// fgetpid : send processID back to Fortran
164// WINDOWS CODE
165// ---------------------------------------------------------------------------------
166// INPUT
167// OUTPUT
168// int *pid : returns process id of Radioss
169// ----------------------------------------------------------------------------
170void fgetpid_(int * pid){
171 *pid=my_getpid;
172}
173
174void fgetpid__(int * pid){
175 *pid=my_getpid;
176}
177
178void fgetpid(int * pid){
179 *pid=my_getpid;
180}
181
182void _FCALL FGETPID(int * pid){
183 *pid=my_getpid;
184}
185
186
187#ifdef MAIN
188int main()
189{
190 char * tmpenv = tmpenv_c();
191 printf("output: %s\n",tmpenv);
192
193 return 1;
194}
195
196#endif
int main()
#define _FCALL
subroutine slen(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4, j, area, aream)
Definition slen.F:31
void tmpenvf__(char *tmpdir, int *tmplen)
Definition tmpenv_c.c:153
void tmpenvf_(char *tmpdir, int *tmplen)
Definition tmpenv_c.c:138
void fgetpid__(int *pid)
Definition tmpenv_c.c:174
#define my_getpid
Definition tmpenv_c.c:47
void tmpenvf(char *tmpdir, int *tmplen)
Definition tmpenv_c.c:149
char * tmpenv_c()
Definition tmpenv_c.c:100
void fgetpid(int *pid)
Definition tmpenv_c.c:178
void fgetpid_(int *pid)
Definition tmpenv_c.c:170
void _FCALL FGETPID(int *pid)
Definition tmpenv_c.c:182
void _FCALL TMPENVF(char *tmpdir, int *tmplen)
Definition tmpenv_c.c:157