gustavonihei commented on a change in pull request #4595: URL: https://github.com/apache/incubator-nuttx/pull/4595#discussion_r714733523
########## File path: libs/libc/misc/lib_glob.c ########## @@ -0,0 +1,525 @@ +/**************************************************************************** + * libs/libc/misc/lib_glob.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <dirent.h> +#include <errno.h> +#include <fnmatch.h> +#include <glob.h> +#include <limits.h> +#include <pwd.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "libc.h" + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +struct match_s +{ + FAR struct match_s *next; + char name[]; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int append(FAR struct match_s **tail, FAR const char *name, + size_t len, int mark); +static int do_glob(FAR char *buf, size_t pos, int type, FAR char *pat, + int flags, + CODE int (*errfunc)(FAR const char *path, int err), + FAR struct match_s **tail); +static int ignore_err(FAR const char *path, int err); +static void freelist(FAR struct match_s *head); +static int sort(FAR const void *a, FAR const void *b); + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: append + ****************************************************************************/ + +static int append(FAR struct match_s **tail, FAR const char *name, + size_t len, int mark) +{ + FAR struct match_s *new = lib_malloc(sizeof(struct match_s) + len + 2); + if (!new) + { + return -1; + } + + (*tail)->next = new; + new->next = NULL; + memcpy(new->name, name, len + 1); + if (mark && len && name[len - 1] != '/') + { + new->name[len] = '/'; + new->name[len + 1] = 0; + } + + *tail = new; + return 0; +} + +/**************************************************************************** + * Name: do_glob + ****************************************************************************/ + +static int do_glob(FAR char *buf, size_t pos, int type, FAR char *pat, + int flags, + CODE int (*errfunc)(FAR const char *path, int err), + FAR struct match_s **tail) +{ + /* If GLOB_MARK is unused, we don't care about type. */ + + if (!type && !(flags & GLOB_MARK)) + { + type = DT_REG; + } + + /* Special-case the remaining pattern being all slashes, in + * which case we can use caller-passed type if it's a dir. + */ + + if (*pat && type != DT_DIR) + { + type = 0; + } + + while (pos + 1 < PATH_MAX && *pat == '/') + { + buf[pos++] = *pat++; + } + + /* Consume maximal [escaped-]literal prefix of pattern, copying + * and un-escaping it to the running buffer as we go. + */ + + ptrdiff_t i = 0; + ptrdiff_t j = 0; Review comment: Another NuttX restriction for generic code is the compliance to C89 standard, so variable declarations must be done in the beginning of the scope. https://cwiki.apache.org/confluence/display/NUTTX/Coding+Standard#funcbody (topic *Local variables first*) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org