[3083] | 1 | /* |
---|
[3090] | 2 | * Copyright (c) (2013-2015,2017,2022) Jeremie Burgalat (jeremie.burgalat@univ-reims.fr). |
---|
[3083] | 3 | * |
---|
| 4 | * This file is part of SWIFT |
---|
| 5 | * |
---|
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
---|
| 7 | * this software and associated documentation files (the "Software"), to deal in |
---|
| 8 | * the Software without restriction, including without limitation the rights to |
---|
| 9 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
---|
| 10 | * the Software, and to permit persons to whom the Software is furnished to do so, |
---|
| 11 | * subject to the following conditions: |
---|
| 12 | * |
---|
| 13 | * The above copyright notice and this permission notice shall be included in all |
---|
| 14 | * copies or substantial portions of the Software. |
---|
| 15 | * |
---|
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
---|
| 18 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
---|
| 19 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
---|
| 20 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
| 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
[1897] | 22 | */ |
---|
[1793] | 23 | |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | /* ------------------ WRAPPERS METHODS FOR FORTRAN BINDINGS ------------------ */ |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Gets the current umask (in decimal system) |
---|
| 30 | * @return An int with the current umask set for the current session |
---|
| 31 | */ |
---|
| 32 | int c_umask(); |
---|
| 33 | |
---|
[3083] | 34 | /** |
---|
| 35 | * Get directory name of input path |
---|
[1793] | 36 | * @param[in] in A C string with the input path |
---|
| 37 | * @return A pointer to a char array with the directory name of @bti{input} path. |
---|
| 38 | * @note On error, a NULL pointer is returned. |
---|
[3083] | 39 | * @warning In any case, the returned pointer must be freed in the Fortran counterpart |
---|
[1793] | 40 | * (using fsystem::free_c). |
---|
| 41 | */ |
---|
| 42 | char * c_dirname(const char *in); |
---|
| 43 | |
---|
[3083] | 44 | /** |
---|
| 45 | * Get base name of input path |
---|
[1793] | 46 | * @param[in] in A C string with the input path |
---|
| 47 | * @return A pointer to a char array with the base name of @bti{input} path. |
---|
| 48 | * @note On error, a NULL pointer is returned. |
---|
[3083] | 49 | * @warning In any case, the returned pointer must be freed in the Fortran counterpart |
---|
[1793] | 50 | * (using fsystem::free_c). |
---|
| 51 | */ |
---|
| 52 | char* c_basename(const char *in); |
---|
| 53 | |
---|
[3083] | 54 | /** |
---|
[1793] | 55 | * Get the current working directory. |
---|
| 56 | * @return A pointer to a char array with the current workind directory. |
---|
| 57 | * @note On error, a NULL pointer is returned. |
---|
[3083] | 58 | * @warning In any case, the returned pointer must be freed in the Fortran counterpart |
---|
[1793] | 59 | * (using fsystem::free_c). |
---|
| 60 | */ |
---|
| 61 | char* c_getcwd(); |
---|
| 62 | |
---|
| 63 | |
---|
[3083] | 64 | /** |
---|
[1793] | 65 | * Get the realpath of input path. |
---|
| 66 | * @param[in] input A C string with the input path |
---|
| 67 | * @return A pointer to a char array with the realpath of @bti{input} path. |
---|
| 68 | * @note On error, a NULL pointer is returned. |
---|
[3083] | 69 | * @warning In any case, the returned pointer must be freed in the Fortran counterpart |
---|
[1793] | 70 | * (using fsystem::free_c). |
---|
| 71 | */ |
---|
| 72 | char* c_realpath(const char *input); |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Get the relative path of two file paths |
---|
| 76 | * @details The method computes the relative path of can_fname to can_reldir if |
---|
| 77 | * possible. |
---|
| 78 | * @param path string with the path to compute in relative representation |
---|
| 79 | * @param reldir a directory path from which output should be relative to |
---|
| 80 | * @return A pointer to a char array with the relative path. |
---|
| 81 | * @note On error, a NULL pointer is returned. |
---|
[3083] | 82 | * @warning In any case, the returned pointer must be freed in the Fortran counterpart |
---|
[1793] | 83 | * (using fsystem::free_c). |
---|
| 84 | */ |
---|
| 85 | char* c_relpath(const char *path, const char *reldir) ; |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Get the corresponding name of the given user id |
---|
| 89 | * @param[in] uid An integer with a user id |
---|
| 90 | * @return A pointer to a char array with the user name. |
---|
| 91 | * @note On error, a NULL pointer is returned. |
---|
[3083] | 92 | * @warning In any case, the returned pointer must be freed in the Fortran counterpart |
---|
[1793] | 93 | * (using fsystem::free_c). |
---|
| 94 | */ |
---|
| 95 | char* c_uname(int uid); |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Get the corresponding name of the given group id |
---|
| 99 | * @param[in] gid An integer with a group id |
---|
| 100 | * @return A pointer to a char array with the group name. |
---|
| 101 | * @note On error, a NULL pointer is returned. |
---|
[3083] | 102 | * @warning In any case, the returned pointer must be freed in the Fortran counterpart |
---|
[1793] | 103 | * (using fsystem::free_c). |
---|
| 104 | */ |
---|
| 105 | char* c_gname(int gid); |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * Get last errno from C library |
---|
| 109 | * @return An int with last errno saved in C library. |
---|
| 110 | */ |
---|
| 111 | int c_get_errno(); |
---|
| 112 | |
---|
[3083] | 113 | /** |
---|
[1793] | 114 | * Get the error message of the given error id |
---|
| 115 | * @param err An integer with the error id |
---|
| 116 | * @return A pointer to a char array with the group name. |
---|
| 117 | * @note On error, the hard-coded message "Unknown error" is returned. |
---|
[3083] | 118 | * @warning In any case, the returned pointer must be freed in the Fortran counterpart |
---|
[1793] | 119 | * (using fsystem::free_c). |
---|
| 120 | */ |
---|
| 121 | char* c_strerror(int err); |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * Creates directories recursively |
---|
| 125 | * @note The method is simple stripped copy of mkdir tool source code. |
---|
| 126 | * @param[in] path A C string with the path of the directory to create |
---|
| 127 | * @param[in] mode A mode_t structure with the permission of the directory |
---|
| 128 | * @return An integer with 0 on success, last errno on failure |
---|
| 129 | */ |
---|
[3083] | 130 | int c_mkdirp(const char *path, mode_t mode); |
---|
[1793] | 131 | |
---|
| 132 | /** |
---|
| 133 | * Rename a path |
---|
| 134 | * @param old A string with the (valid) path to rename |
---|
| 135 | * @param new A string with the new name of the path |
---|
| 136 | * @return An integer with 0 on success, last errno on failure |
---|
| 137 | */ |
---|
| 138 | int c_rename(const char *old, const char *new); |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * Change path permissions |
---|
[3083] | 142 | * @param path A string with the path |
---|
[1793] | 143 | * @param mode A integer with the new permissions to set |
---|
| 144 | * @return An integer with 0 on success, last errno on failure |
---|
| 145 | */ |
---|
| 146 | int c_chmod(const char *path, mode_t mode); |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Change current working directory |
---|
| 150 | * @param path A C string with the new path |
---|
| 151 | * @return An integer with 0 on success, last errno on failure |
---|
| 152 | */ |
---|
| 153 | int c_chdir(const char *path); |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * Create directory |
---|
| 157 | * @param[in] path A C string with the path of the directory to create |
---|
| 158 | * @param[in] mode A mode_t structure with the permission of the directory |
---|
| 159 | * (as well as all the parent directorie created, if any). |
---|
| 160 | * @return An integer with 0 on success, last errno on failure |
---|
[3083] | 161 | */ |
---|
[1793] | 162 | int c_mkdir(const char *path, mode_t mode); |
---|
| 163 | |
---|
[1897] | 164 | |
---|
[1793] | 165 | /** |
---|
[1897] | 166 | * Copy file to another. |
---|
| 167 | * @param to A C string with the new filepath |
---|
| 168 | * @param from A C string with the filepath to copy |
---|
| 169 | * @return An integer with 0 on success, 1 on failure. |
---|
| 170 | */ |
---|
| 171 | int c_copy(const char *to, const char *from); |
---|
| 172 | |
---|
| 173 | /** |
---|
[1793] | 174 | * Remove file from filesytem |
---|
| 175 | * @note Also works for directory (if empty) |
---|
| 176 | * @param path A C string with the filepath to remove |
---|
| 177 | * @return An integer with 0 on success, last errno on failure |
---|
| 178 | */ |
---|
| 179 | int c_remove(const char *path); |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * Remove a directory |
---|
| 183 | * @param path A C string with the path of the directory to remove. |
---|
| 184 | * @return An integer with 0 on success, last errno on failure |
---|
| 185 | */ |
---|
| 186 | int c_rmdir(const char *path); |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | * Remove a directory and its contents recursively |
---|
| 190 | * |
---|
[3083] | 191 | * This method mimics 'rm -rf' command. |
---|
[1793] | 192 | * @param path A C string with the path of the directory to remove. |
---|
| 193 | * @return An integer with 0 on success, last errno on failure |
---|
| 194 | */ |
---|
| 195 | int c_rmdir_f(const char *path) ; |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * Get some file informations |
---|
[3083] | 199 | * @note If the path cannot be "stat", most of the output parameters are set |
---|
[1793] | 200 | * to -1. |
---|
| 201 | * @param[in] p A C string with the path of a file (or directory) |
---|
[3083] | 202 | * @param[out] pe An int with the permissions of the path |
---|
[1793] | 203 | * @param[out] nl An int with the inumber of links |
---|
| 204 | * @param[out] ty An int with the type of the file : |
---|
| 205 | * - 0 -> file |
---|
| 206 | * - 1 -> link to a file |
---|
| 207 | * - 2 -> directory |
---|
| 208 | * - 3 -> link to a directory |
---|
| 209 | * - 4 -> Other (fifo, socket, block special, char special ...) |
---|
[3083] | 210 | * @param[out] ui An int with the user id of the path |
---|
| 211 | * @param[out] gi An int with the group id of the path |
---|
| 212 | * @param[out] si An int with the size of the path |
---|
[1793] | 213 | * @param[out] a A C string (20 chars wide, including NULL character) with the |
---|
| 214 | * last access date |
---|
| 215 | * @param[out] m A C string (20 chars wide, including NULL character) with the |
---|
| 216 | * last modification date |
---|
[3083] | 217 | * @param[out] c A C string (20 chars wide, including NULL character) with the |
---|
[1793] | 218 | * creation date |
---|
[3083] | 219 | * @return An integer with 0 on success, last errno on failure |
---|
[1793] | 220 | */ |
---|
[3083] | 221 | int c_fstat(const char *p, int *pe, int *nl, int *ty, int *ui, int *gi, |
---|
[1793] | 222 | long *si, char a[20], char m[20], char c[20]); |
---|
| 223 | |
---|
| 224 | /** |
---|
| 225 | * Check if path is accessible |
---|
| 226 | * @param[in] path A C string with the path to check |
---|
| 227 | * @param[in] perm An integer with the user's permission to check : |
---|
| 228 | * - 0 do not check for permissions |
---|
[3083] | 229 | * - 1 check for execute permission |
---|
[1793] | 230 | * - 2 check for write permission |
---|
| 231 | * - 4 check for read permission |
---|
| 232 | * @return An int with 0 on success, errno on failure |
---|
| 233 | */ |
---|
| 234 | int c_access(const char *path, int perm) ; |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * Create a directory or a file in given path |
---|
| 238 | * @param[in] path Path to be created |
---|
| 239 | * @param[in] mode Permission of the path |
---|
| 240 | * @param[in] astype A boolean with False to create a directory, True to create a file. |
---|
| 241 | * @param[in] forced A boolean with True to force creation of intermediate directory |
---|
| 242 | * @return 0 on success, -9 on allocation failure, last errno otherwise |
---|
| 243 | */ |
---|
| 244 | int c_create(const char* path, mode_t mode, int astype, int forced); |
---|
| 245 | |
---|
| 246 | /** |
---|
| 247 | * Create a directory or a file in given path |
---|
| 248 | * @param[out] rows Number of rows of the current terminal window |
---|
| 249 | * @param[out] cols Number of columns of the current terminal window |
---|
[3083] | 250 | * @return An int with 0 on success, errno on failure. On failure, rows is set |
---|
[1793] | 251 | * to 80 and cols to 20. |
---|
| 252 | */ |
---|
| 253 | int c_termsize(int *rows,int *cols); |
---|
| 254 | |
---|
[1897] | 255 | /** |
---|
| 256 | * Get the current resident set size memory used by the program. |
---|
| 257 | */ |
---|
| 258 | size_t c_getCurrentRSS(); |
---|
| 259 | |
---|
| 260 | /** |
---|
| 261 | * Get the peak resident set size memory used by the program. |
---|
| 262 | */ |
---|
| 263 | size_t c_getPeakRSS(); |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * Get global memory usage informations. |
---|
| 267 | * |
---|
| 268 | * Note: The method attempts to read /proc/meminfo. If the file does not exists, all the given output arguments are |
---|
| 269 | * set to zero. |
---|
| 270 | */ |
---|
| 271 | int c_getSystemMemory(long long int *m_total,long long int *m_available,long long int *m_free); |
---|