ReactOS 0.4.15-dev-7788-g1ad9096
saveargs.c
Go to the documentation of this file.
1/* @(#)saveargs.c 1.16 10/11/18 Copyright 1995-2010 J. Schilling */
2/*
3 * save argc, argv for command error printing routines
4 *
5 * Copyright (c) 1995-2010 J. Schilling
6 */
7/*
8 * The contents of this file are subject to the terms of the
9 * Common Development and Distribution License, Version 1.0 only
10 * (the "License"). You may not use this file except in compliance
11 * with the License.
12 *
13 * See the file CDDL.Schily.txt in this distribution for details.
14 *
15 * When distributing Covered Code, include this CDDL HEADER in each
16 * file and include the License file CDDL.Schily.txt from this distribution.
17 */
18
19#include <schily/mconfig.h>
20#include <schily/standard.h>
21#include <schily/string.h>
22#include <schily/stdlib.h>
23#include <schily/avoffset.h>
24#include <schily/schily.h>
25#include <schily/dlfcn.h>
26
27#if !defined(AV_OFFSET) || !defined(FP_INDIR)
28# ifdef HAVE_SCANSTACK
29# undef HAVE_SCANSTACK
30# endif
31#endif
32
33#ifdef HAVE_VAR___PROGNAME
34extern char *__progname;
35#ifdef HAVE_VAR___PROGNAME_FULL
36extern char *__progname_full;
37#else
38#define __progname_full __progname
39#endif
40#endif
41
42static int ac_saved;
43static char **av_saved;
44static char *av0_saved;
45static char *av0_name_saved;
46static char *progpath_saved;
47static char *progname_saved;
48
49static char av0_sp[32]; /* av0 space, avoid malloc() in most cases */
50static char prn_sp[32]; /* name space, avoid malloc() in most cases */
51static char dfl_str[] = "?";
52
53LOCAL void save_av0 __PR((char *av0));
55LOCAL void init_arginfo __PR((void));
56
57EXPORT void
58save_args(ac, av)
59 int ac;
60 char *av[];
61{
62 ac_saved = ac;
63 av_saved = av;
64 save_av0(av[0]);
65}
66
67LOCAL void
69 char *av0;
70{
71 int slen;
72 char *p;
73
74 if (av0_saved && av0_saved != av0_sp)
76
77 slen = strlen(av0) + 1;
78
79 if (slen <= (int)sizeof (av0_sp))
81 else
82 av0_saved = malloc(slen);
83
84 if (av0_saved) {
85 strcpy(av0_saved, av0);
86 av0 = av0_saved;
87 }
88
89 if ((p = strrchr(av0, '/')) == NULL)
90 av0_name_saved = av0;
91 else
92 av0_name_saved = ++p;
93}
94
95EXPORT int
97{
98 if (av_saved == NULL)
100
101 return (ac_saved);
102}
103
104EXPORT char **
106{
107 if (av_saved == NULL)
108 init_arginfo();
109
110 return (av_saved);
111}
112
113EXPORT char *
115{
116 if (av0_saved == NULL)
117 init_arginfo();
118
119 return (av0_saved);
120}
121
122EXPORT void
124 const char *name;
125{
126 int slen;
127 char *p;
128
131
132 slen = strlen(name) + 1;
133
134 if (slen <= sizeof (prn_sp))
136 else
137 progpath_saved = malloc(slen);
138
139 if (progpath_saved) {
142 }
143
144 if ((p = strrchr(name, '/')) == NULL)
145 progname_saved = (char *)name;
146 else
147 progname_saved = ++p;
148}
149
150EXPORT char *
152{
153 if (progname_saved)
154 return (progname_saved);
155 if (av0_name_saved == NULL)
157 if (av0_name_saved)
158 return (av0_name_saved);
159 return (dfl_str);
160}
161
162EXPORT char *
164{
165 if (progpath_saved)
166 return (progpath_saved);
167 if (av0_saved == NULL)
169 if (av0_saved)
170 return (av0_saved);
171 return (dfl_str);
172}
173
174LOCAL void
176{
177#if defined(HAVE_SCANSTACK) || defined(HAVE_GETPROGNAME)
178 char *progname;
179#endif
180
181 if (av0_name_saved == NULL)
182 init_arginfo();
183 if (av0_name_saved)
184 return;
185#ifdef HAVE_GETPROGNAME
186 progname = (char *)getprogname();
187 if (progname) {
189 return;
190 }
191#endif
192#ifdef HAVE_VAR___PROGNAME
193 if (__progname_full) {
194 save_av0(__progname_full);
195 return;
196 }
197#endif
198#ifdef HAVE_SCANSTACK
199 progname = getav0(); /* scan stack to get argv[0] */
200 if (progname) {
202 return;
203 }
204#endif
205}
206
207LOCAL void
209{
210#if defined(HAVE_DLINFO) && defined(HAVE_DLOPEN_IN_LIBC) && defined(RTLD_DI_ARGSINFO)
211 Dl_argsinfo args;
212
213 if (dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &args) < 0 ||
214 args.dla_argc <= 0 ||
215 args.dla_argv[0] == NULL)
216 return;
217
218 if (av_saved == NULL)
219 save_args(args.dla_argc, args.dla_argv);
220#endif
221}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
static const char * progname
Definition: cjpeg.c:137
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
GLfloat GLfloat p
Definition: glext.h:8902
#define LOCAL(type)
Definition: jmorecfg.h:289
#define __PR(a)
Definition: prototyp.h:106
LOCAL void save_av0(char *av0)
Definition: saveargs.c:68
EXPORT int saved_ac()
Definition: saveargs.c:96
EXPORT void set_progname(char *name) const
Definition: saveargs.c:123
EXPORT void save_args(int ac, av)
Definition: saveargs.c:58
static char ** av_saved
Definition: saveargs.c:43
EXPORT char * get_progname()
Definition: saveargs.c:151
static char dfl_str[]
Definition: saveargs.c:51
LOCAL void init_progname()
Definition: saveargs.c:175
static char * progpath_saved
Definition: saveargs.c:46
EXPORT char ** saved_av()
Definition: saveargs.c:105
EXPORT char * saved_av0()
Definition: saveargs.c:114
LOCAL void init_arginfo()
Definition: saveargs.c:208
static char * av0_saved
Definition: saveargs.c:44
EXPORT char * get_progpath()
Definition: saveargs.c:163
static char av0_sp[32]
Definition: saveargs.c:49
static int ac_saved
Definition: saveargs.c:42
static char prn_sp[32]
Definition: saveargs.c:50
static char * progname_saved
Definition: saveargs.c:47
static char * av0_name_saved
Definition: saveargs.c:45
_Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
#define __progname
Definition: getopt.c:29
#define args
Definition: format.c:66
Definition: match.c:390
Definition: name.c:39