Bump version to 24.04.3.4
[LibreOffice.git] / sal / rtl / cmdargs.cxx
blobbad8330770123b329e1a38ac2d1a82929df38062
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/mutex.hxx>
21 #include <rtl/alloc.h>
22 #include <rtl/process.h>
23 #include <rtl/ustring.hxx>
25 namespace {
27 rtl_uString ** g_ppCommandArgs = nullptr;
28 sal_uInt32 g_nCommandArgCount = 0;
30 struct ArgHolder
32 ~ArgHolder();
35 ArgHolder::~ArgHolder()
37 while (g_nCommandArgCount > 0)
38 rtl_uString_release (g_ppCommandArgs[--g_nCommandArgCount]);
40 free (g_ppCommandArgs);
41 g_ppCommandArgs = nullptr;
44 // The destructor of this static ArgHolder is "activated" by the assignments to
45 // g_ppCommandArgs and g_nCommandArgCount in init():
46 ArgHolder argHolder;
48 void init()
50 osl::MutexGuard guard( osl::Mutex::getGlobalMutex() );
51 if (g_ppCommandArgs)
52 return;
54 sal_Int32 i, n = osl_getCommandArgCount();
56 g_ppCommandArgs =
57 static_cast<rtl_uString**>(rtl_allocateZeroMemory (n * sizeof(rtl_uString*)));
58 for (i = 0; i < n; i++)
60 rtl_uString * pArg = nullptr;
61 osl_getCommandArg (i, &pArg);
62 bool env;
64 auto const & arg = OUString::unacquired(&pArg);
65 env = (arg.startsWith("-") || arg.startsWith("/")) &&
66 arg.match("env:", 1) &&
67 arg.indexOf ('=') >= 0;
69 if (env )
71 // ignore.
72 rtl_uString_release (pArg);
74 else
76 // assign.
77 g_ppCommandArgs[g_nCommandArgCount++] = pArg;
84 oslProcessError SAL_CALL rtl_getAppCommandArg (
85 sal_uInt32 nArg, rtl_uString **ppCommandArg)
87 init();
88 oslProcessError result = osl_Process_E_NotFound;
89 if( nArg < g_nCommandArgCount )
91 rtl_uString_assign( ppCommandArg, g_ppCommandArgs[nArg] );
92 result = osl_Process_E_None;
94 return result;
97 sal_uInt32 SAL_CALL rtl_getAppCommandArgCount()
99 init();
100 return g_nCommandArgCount;
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */