/* ***************************************************************************** * utils/extension.c -- extension utilities. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of libg1m. * libg1m is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 3.0 of the License, * or (at your option) any later version. * * libg1m is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with libg1m; if not, see . * ************************************************************************** */ #include #include /** * g1m_getext: * Get extension from a path. * * @arg path the path. * @arg buf the extension buffer. * @arg n the extension buffer size. * @return the size of the extension. */ int g1m_getext(const char *path, char *buf, size_t n) { int i; /* no size? */ if (!n) return (0); /* get filename */ const char *filename = strrchr(path, '/'); filename = filename ? filename + 1 : path; /* get extension */ const char *ext = strrchr(filename, '.'); ext = ext ? ext + 1 : ""; /* copy it */ buf[n - 1] = 0; strncpy(buf, ext, n - 1); for (i = 0; buf[i]; i++) buf[i] = tolower(buf[i]); /* return the size */ return (strlen(buf)); }