lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

m_util.h (2706B)


      1 /*********************************** LICENSE **********************************\
      2  *                            Copyright 2017 Morphux                            *
      3  *                                                                              *
      4  *        Licensed under the Apache License, Version 2.0 (the "License");       *
      5  *        you may not use this file except in compliance with the License.      *
      6  *                  You may obtain a copy of the License at                     *
      7  *                                                                              *
      8  *                 http://www.apache.org/licenses/LICENSE-2.0                   *
      9  *                                                                              *
     10  *      Unless required by applicable law or agreed to in writing, software     *
     11  *       distributed under the License is distributed on an "AS IS" BASIS,      *
     12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
     13  *        See the License for the specific language governing permissions and   *
     14  *                       limitations under the License.                         *
     15  \******************************************************************************/
     16 
     17 #ifndef M_UTIL
     18 # define M_UTIL
     19 
     20 # include <stdint.h>
     21 # include <sys/types.h>
     22 # include <sys/stat.h>
     23 # include <morphux.h>
     24 # include <sys/wait.h>
     25 # include <fail_test.h>
     26 
     27 # define COMP_FLAGS_NONE   1 << 0x0
     28 # define COMP_FLAGS_FILE   1 << 0x1
     29 # define COMP_FLAGS_STDOUT 1 << 0x2
     30 
     31 
     32 # define STR_OR_EMPTY(str) (str ? str : "")
     33 # define STR_NULL_OR_EMPTY(str) (str == NULL || (str != NULL && *str == '\0'))
     34 
     35 # define FREE(ptr) free(ptr); ptr = NULL;
     36 
     37 # define COUNTOF(ptr) (sizeof(ptr) / sizeof((ptr)[0]))
     38 
     39 /*!
     40  * \brief Set flags for compilation output
     41  *
     42  * \param[in] flags Flags to set
     43  *
     44  * \note If COMP_FLAGS_FILE is used, the compilations will call m_log.
     45  * Caller must init the logging first.
     46  */
     47 void set_execute_flags(u8_t flags);
     48 
     49 /*!
     50  * \brief Delete a directory recursively
     51  *
     52  * \param[in] dir Path of the directory to delete
     53  *
     54  * \return true on success, false on failure
     55  */
     56 bool recursive_delete(const char *dir);
     57 
     58 /*!
     59  * \brief Check if a file is there or not
     60  *
     61  * \param[in] str File path
     62  *
     63  * \return true if the file exist, false otherwise
     64  */
     65 static inline bool file_exist(const char *str) {
     66     int         fd = open(str, O_RDONLY);
     67     bool        result = (fd != -1);
     68 
     69     if (result == true)
     70         close(fd);
     71     return result;
     72 }
     73 
     74 /*!
     75  * \brief Execute a command from a string
     76  *
     77  * \param[in] str Command to execute
     78  *
     79  * \return 0 success, error on failure
     80  */
     81 int exec_line(const char *str);
     82 
     83 int exec_list(mlist_t *list);
     84 
     85 #endif /* M_UTIL */