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