File indexing completed on 2024-04-28 05:46:51

0001 /*****************************************************************************
0002  *   Copyright 2012 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0003  *                                                                           *
0004  *   This program is free software; you can redistribute it and/or modify    *
0005  *   it under the terms of the GNU Lesser General Public License as          *
0006  *   published by the Free Software Foundation; either version 2.1 of the    *
0007  *   License, or (at your option) version 3, or any later version accepted   *
0008  *   by the membership of KDE e.V. (or its successor approved by the         *
0009  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0010  *   Section 6 of version 3 of the license.                                  *
0011  *                                                                           *
0012  *   This program is distributed in the hope that it will be useful,         *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0015  *   Lesser General Public License for more details.                         *
0016  *                                                                           *
0017  *   You should have received a copy of the GNU Lesser General Public        *
0018  *   License along with this library. If not,                                *
0019  *   see <http://www.gnu.org/licenses/>.                                     *
0020  *****************************************************************************/
0021 
0022 #include "utils.h"
0023 #include "strs.h"
0024 
0025 #include <config.h>
0026 
0027 #include <dlfcn.h>
0028 
0029 namespace QtCurve {
0030 
0031 QTC_EXPORT const char*
0032 getProgName()
0033 {
0034     static uniqueStr name = [] {
0035         void *hdl = dlopen(nullptr, RTLD_NOW);
0036         // I do not use the procfs here since any system that has procfs
0037         // (Linux) should support one of these variables/functions.
0038         if (!hdl) {
0039             return strdup("");
0040         }
0041         void *progname_p = nullptr;
0042         const char *name = nullptr;
0043         if ((progname_p = dlsym(hdl, "program_invocation_short_name")) ||
0044             (progname_p = dlsym(hdl, "program_invocation_name")) ||
0045             (progname_p = dlsym(hdl, "__progname"))) {
0046             name = *(char *const*)progname_p;
0047         } else if ((progname_p = dlsym(hdl, "getprogname"))) {
0048             name = ((const char *(*)())progname_p)();
0049         }
0050         if (!name) {
0051             return strdup("");
0052         }
0053         const char *sub_name;
0054         if ((sub_name = strrchr(name, '/')) && sub_name[1]) {
0055             return strdup(sub_name + 1);
0056         }
0057         return strdup(name);
0058     };
0059     return name.get();
0060 }
0061 
0062 }