lib

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

commit 54138805f599e5eefb200be461889e7cd1e78052
parent 018f1559db0eb0aa3203c5c2ce8de14123bde0f2
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 10 May 2017 13:54:07 +0200

Add(recursive_delete): Add a function to recursiely delete a directory:

Not tested, not documented (yet)

Diffstat:
Ainc/m_util.h | 27+++++++++++++++++++++++++++
Asrc/m_util.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/inc/m_util.h b/inc/m_util.h @@ -0,0 +1,27 @@ +/*********************************** LICENSE **********************************\ + * Copyright 2017 Morphux * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + \******************************************************************************/ + +#ifndef M_UTIL +# define M_UTIL + +# include <sys/types.h> +# include <sys/stat.h> +# include <fts.h> +# include <morphux.h> + +bool recursive_delete(const char *dir); + +#endif /* M_UTIL */ diff --git a/src/m_util.c b/src/m_util.c @@ -0,0 +1,61 @@ +/*********************************** LICENSE **********************************\ + * Copyright 2017 Morphux * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + \******************************************************************************/ + +#include <m_util.h> + +bool recursive_delete(const char *dir) { + FTS *ftsp = NULL; + FTSENT *curr = NULL; + const char *files[] = { (char *)dir, NULL }; + + ftsp = fts_open((char * const *)files, FTS_NOCHDIR | FTS_PHYSICAL | FTS_XDEV, NULL); + if (ftsp == NULL) + return false; + + while ((curr = fts_read(ftsp))) { + switch (curr->fts_info) { + case FTS_NS: + /* FALLTROUGH */ + case FTS_DNR: + /* FALLTROUGH */ + case FTS_ERR: + return false; + + case FTS_DC: + /* FALLTROUGH */ + case FTS_DOT: + /* FALLTROUGH */ + case FTS_NSOK: + break ; + + case FTS_DP: + /* FALLTROUGH */ + case FTS_F: + /* FALLTROUGH */ + case FTS_SL: + /* FALLTROUGH */ + case FTS_SLNONE: + /* FALLTROUGH */ + case FTS_DEFAULT: + if (remove(curr->fts_accpath) < 0) + return false; + break; + } + } + + fts_close(ftsp); + return true; +}