Mercurial > octave
annotate liboctave/system/oct-env.cc @ 21729:815b2f500fab
use namespace for system password wrapper class
* oct-passwd.h, oct-passwd.cc: Put password class in octave::sys
namespace. Change all uses.
(octave_passwd): Now a deprecated typedef for octave::sys::password.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 18 May 2016 13:11:15 -0400 |
parents | e0da98857c19 |
children | 6a1eded90355 |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19597
diff
changeset
|
3 Copyright (C) 1996-2015 John W. Eaton |
2926 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2926 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2926 | 20 |
21 */ | |
22 | |
23 /* | |
24 | |
25 The functions listed below were adapted from a similar functions | |
26 from GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 | |
27 Free Software Foundation, Inc. | |
28 | |
29 octave_env::do_absolute_pathname | |
30 octave_env::do_base_pathname | |
31 octave_env::do_chdir | |
32 octave_env::do_getcwd | |
33 octave_env::do_make_absolute | |
34 octave_env::do_polite_directory_format | |
35 octave_env::pathname_backup | |
36 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
37 */ |
2926 | 38 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21614
diff
changeset
|
39 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21202
diff
changeset
|
40 # include "config.h" |
2926 | 41 #endif |
42 | |
4093 | 43 #include <cctype> |
2926 | 44 #include <cstdlib> |
10463
bbe99b2a5ba7
undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents:
10447
diff
changeset
|
45 #include <cstring> |
4093 | 46 |
3504 | 47 #include <string> |
2926 | 48 |
49 #include <sys/types.h> | |
50 #include <unistd.h> | |
51 | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
52 #include "progname.h" |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
53 |
4097 | 54 #include "file-ops.h" |
2926 | 55 #include "lo-error.h" |
56 #include "lo-sysdep.h" | |
57 #include "lo-utils.h" | |
58 #include "oct-env.h" | |
2934 | 59 #include "oct-passwd.h" |
2947 | 60 #include "oct-syscalls.h" |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
61 #include "singleton-cleanup.h" |
2926 | 62 |
63 octave_env::octave_env (void) | |
64 : follow_symbolic_links (true), verbatim_pwd (true), | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
65 current_directory (), prog_name (), prog_invocation_name (), |
2926 | 66 user_name (), host_name () |
67 { | |
68 // Get a real value for the current directory. | |
69 do_getcwd (); | |
70 | |
71 // Etc. | |
72 do_get_user_name (); | |
73 | |
74 do_get_host_name (); | |
75 } | |
76 | |
77 octave_env *octave_env::instance = 0; | |
78 | |
79 bool | |
80 octave_env::instance_ok (void) | |
81 { | |
82 bool retval = true; | |
83 | |
84 if (! instance) | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
85 { |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
86 instance = new octave_env (); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
87 |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
88 if (instance) |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
89 singleton_cleanup_list::add (cleanup_instance); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
90 } |
2926 | 91 |
92 if (! instance) | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21017
diff
changeset
|
93 (*current_liboctave_error_handler) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21017
diff
changeset
|
94 ("unable to create current working directory object!"); |
2926 | 95 |
96 return retval; | |
97 } | |
98 | |
3504 | 99 std::string |
100 octave_env::polite_directory_format (const std::string& name) | |
2926 | 101 { |
102 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
103 ? instance->do_polite_directory_format (name) : ""; |
2926 | 104 } |
105 | |
106 bool | |
3504 | 107 octave_env::absolute_pathname (const std::string& s) |
2926 | 108 { |
109 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 ? instance->do_absolute_pathname (s) : false; |
2926 | 111 } |
112 | |
6838 | 113 bool |
114 octave_env::rooted_relative_pathname (const std::string& s) | |
115 { | |
116 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
117 ? instance->do_rooted_relative_pathname (s) : false; |
6838 | 118 } |
119 | |
3504 | 120 std::string |
121 octave_env::base_pathname (const std::string& s) | |
2926 | 122 { |
123 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
124 ? instance->do_base_pathname (s) : ""; |
2926 | 125 } |
126 | |
3504 | 127 std::string |
128 octave_env::make_absolute (const std::string& s, const std::string& dot_path) | |
2926 | 129 { |
130 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
131 ? instance->do_make_absolute (s, dot_path) : ""; |
2926 | 132 } |
133 | |
3504 | 134 std::string |
10250 | 135 octave_env::get_current_directory () |
2926 | 136 { |
137 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
138 ? instance->do_getcwd () : ""; |
2926 | 139 } |
140 | |
3504 | 141 std::string |
2926 | 142 octave_env::get_home_directory () |
143 { | |
144 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
145 ? instance->do_get_home_directory () : ""; |
2926 | 146 } |
147 | |
3504 | 148 std::string |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
149 octave_env::get_temp_directory () |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
150 { |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
151 return (instance_ok ()) |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
152 ? instance->do_get_temp_directory () : ""; |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
153 } |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
154 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
155 std::string |
2926 | 156 octave_env::get_program_name (void) |
157 { | |
158 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
159 ? instance->prog_name : ""; |
2926 | 160 } |
161 | |
3504 | 162 std::string |
2926 | 163 octave_env::get_program_invocation_name (void) |
164 { | |
165 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
166 ? instance->prog_invocation_name : ""; |
2926 | 167 } |
168 | |
169 void | |
3504 | 170 octave_env::set_program_name (const std::string& s) |
2926 | 171 { |
172 if (instance_ok ()) | |
173 instance->do_set_program_name (s); | |
174 } | |
175 | |
3504 | 176 std::string |
2926 | 177 octave_env::get_user_name (void) |
178 { | |
179 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
180 ? instance->do_get_user_name () : ""; |
2926 | 181 } |
182 | |
3504 | 183 std::string |
2926 | 184 octave_env::get_host_name (void) |
185 { | |
186 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
187 ? instance->do_get_host_name () : ""; |
2926 | 188 } |
189 | |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
190 std::string |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
191 octave_env::do_get_temp_directory (void) const |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
192 { |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
193 std::string tempd; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
194 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
195 #if defined (__MINGW32__) || defined (_MSC_VER) |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19459
diff
changeset
|
196 |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
197 tempd = do_getenv ("TEMP"); |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
198 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
199 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
200 tempd = do_getenv ("TMP"); |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
201 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
202 #if defined (P_tmpdir) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
203 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
204 tempd = P_tmpdir; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
205 #endif |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
206 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
207 // Some versions of MinGW and MSVC either don't define P_tmpdir, or |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
208 // define it to a single backslash. In such cases just use C:\temp. |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
209 if (tempd.empty () || tempd == "\\") |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
210 tempd = "c:\\temp"; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
211 |
20945
9d9270e2f98f
eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
212 #else |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
213 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
214 tempd = do_getenv ("TMP"); |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
215 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
216 #if defined (P_tmpdir) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
217 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
218 tempd = P_tmpdir; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
219 #else |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
220 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
221 tempd = "/tmp"; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
222 #endif |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
223 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
224 #endif |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
225 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
226 return tempd; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
227 } |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
228 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
229 // FIXME: this leaves no way to distinguish between a |
2926 | 230 // variable that is not set and one that is set to the empty string. |
231 // Is this a problem? | |
232 | |
3504 | 233 std::string |
234 octave_env::getenv (const std::string& name) | |
2926 | 235 { |
236 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
237 ? instance->do_getenv (name) : ""; |
2926 | 238 } |
239 | |
240 void | |
3504 | 241 octave_env::putenv (const std::string& name, const std::string& value) |
2926 | 242 { |
243 octave_putenv (name, value); | |
244 } | |
245 | |
246 bool | |
5489 | 247 octave_env::have_x11_display (void) |
248 { | |
249 std::string display = getenv ("DISPLAY"); | |
250 | |
251 return ! display.empty (); | |
252 } | |
253 | |
254 bool | |
3504 | 255 octave_env::chdir (const std::string& newdir) |
2926 | 256 { |
257 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
258 ? instance->do_chdir (newdir) : false; |
2926 | 259 } |
260 | |
261 void | |
3504 | 262 octave_env::do_set_program_name (const std::string& s) const |
2926 | 263 { |
21614
9bb39b754ab1
finish previous change to octave_env::set_program_name
John W. Eaton <jwe@octave.org>
parents:
21610
diff
changeset
|
264 static bool initialized = false; |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
265 |
21610
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
266 if (! initialized) |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
267 { |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
268 // The string passed to gnulib's ::set_program_name function must |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
269 // exist for the duration of the program so allocate a copy here |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
270 // instead of passing S.c_str () which only exists as long as the |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
271 // string object S. |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
272 |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
273 // For gnulib. |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
274 ::set_program_name (strsave (s.c_str ())); |
2926 | 275 |
21610
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
276 // Let gnulib strip off things like the "lt-" prefix from libtool. |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
277 prog_invocation_name = program_name; |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
278 |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
279 size_t pos |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
280 = prog_invocation_name.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 281 |
21610
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
282 // Also keep a shortened version of the program name. |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
283 prog_name = (pos == std::string::npos |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
284 ? prog_invocation_name |
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
285 : prog_invocation_name.substr (pos+1)); |
21614
9bb39b754ab1
finish previous change to octave_env::set_program_name
John W. Eaton <jwe@octave.org>
parents:
21610
diff
changeset
|
286 |
9bb39b754ab1
finish previous change to octave_env::set_program_name
John W. Eaton <jwe@octave.org>
parents:
21610
diff
changeset
|
287 initialized = true; |
21610
eff26982a19b
avoid passing temp string to ::set_program_name (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21609
diff
changeset
|
288 } |
2926 | 289 } |
290 | |
291 // Return a pretty pathname. If the first part of the pathname is the | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
292 // same as $HOME, then replace that with '~'. |
2926 | 293 |
3504 | 294 std::string |
295 octave_env::do_polite_directory_format (const std::string& name) const | |
2926 | 296 { |
3504 | 297 std::string retval; |
2926 | 298 |
3504 | 299 std::string home_dir = do_get_home_directory (); |
2926 | 300 |
301 size_t len = home_dir.length (); | |
302 | |
3516 | 303 if (len > 1 && home_dir == name.substr (0, len) |
4097 | 304 && (name.length () == len || file_ops::is_dir_sep (name[len]))) |
2926 | 305 { |
306 retval = "~"; | |
307 retval.append (name.substr (len)); | |
308 } | |
309 else | |
310 retval = name; | |
311 | |
312 return retval; | |
313 } | |
314 | |
315 bool | |
3504 | 316 octave_env::do_absolute_pathname (const std::string& s) const |
2926 | 317 { |
4087 | 318 size_t len = s.length (); |
319 | |
320 if (len == 0) | |
321 return false; | |
2926 | 322 |
4097 | 323 if (file_ops::is_dir_sep (s[0])) |
2926 | 324 return true; |
325 | |
4101 | 326 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) |
4088 | 327 if ((len == 2 && isalpha (s[0]) && s[1] == ':') |
4097 | 328 || (len > 2 && isalpha (s[0]) && s[1] == ':' |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
329 && file_ops::is_dir_sep (s[2]))) |
4087 | 330 return true; |
331 #endif | |
332 | |
2926 | 333 return false; |
334 } | |
335 | |
6838 | 336 bool |
337 octave_env::do_rooted_relative_pathname (const std::string& s) const | |
338 { | |
339 size_t len = s.length (); | |
340 | |
341 if (len == 0) | |
342 return false; | |
343 | |
344 if (len == 1 && s[0] == '.') | |
345 return true; | |
346 | |
347 if (len > 1 && s[0] == '.' && file_ops::is_dir_sep (s[1])) | |
348 return true; | |
349 | |
350 if (len == 2 && s[0] == '.' && s[1] == '.') | |
351 return true; | |
352 | |
353 if (len > 2 && s[0] == '.' && s[1] == '.' && file_ops::is_dir_sep (s[2])) | |
354 return true; | |
355 | |
356 return false; | |
357 } | |
358 | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
359 // Return the 'basename' of the pathname in STRING (the stuff after |
4097 | 360 // the last directory separator). If STRING is not a full pathname, |
361 // simply return it. | |
2926 | 362 |
3504 | 363 std::string |
364 octave_env::do_base_pathname (const std::string& s) const | |
2926 | 365 { |
7609
7e6002d15d4d
octave_env::do_base_pathname: handle rooted relativel names
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
366 if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s))) |
2926 | 367 return s; |
368 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
369 size_t pos = s.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 370 |
8021 | 371 if (pos == std::string::npos) |
2926 | 372 return s; |
373 else | |
374 return s.substr (pos+1); | |
375 } | |
376 | |
377 // Turn STRING (a pathname) into an absolute pathname, assuming that | |
4097 | 378 // DOT_PATH contains the symbolic location of the current directory. |
2926 | 379 |
3504 | 380 std::string |
381 octave_env::do_make_absolute (const std::string& s, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
382 const std::string& dot_path) const |
2926 | 383 { |
4087 | 384 if (dot_path.empty () || s.empty () || do_absolute_pathname (s)) |
2926 | 385 return s; |
386 | |
4097 | 387 std::string current_dir = dot_path; |
2926 | 388 |
4097 | 389 if (current_dir.empty ()) |
390 current_dir = do_getcwd (); | |
2926 | 391 |
4097 | 392 size_t pos = current_dir.length () - 1; |
2926 | 393 |
4097 | 394 if (! file_ops::is_dir_sep (current_dir[pos])) |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
395 current_dir.append (file_ops::dir_sep_str ()); |
4097 | 396 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
397 // FIXME: this is probably not correct for all systems. |
2926 | 398 |
399 size_t i = 0; | |
400 size_t slen = s.length (); | |
401 | |
402 while (i < slen) | |
403 { | |
404 if (s[i] == '.') | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
405 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
406 if (i + 1 == slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
407 return current_dir; |
2926 | 408 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
409 if (file_ops::is_dir_sep (s[i+1])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
410 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
411 i += 2; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
412 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
413 } |
2926 | 414 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
415 if (s[i+1] == '.' |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
416 && (i + 2 == slen || file_ops::is_dir_sep (s[i+2]))) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
417 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
418 i += 2; |
2926 | 419 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
420 if (i != slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
421 i++; |
2926 | 422 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
423 pathname_backup (current_dir, 1); |
2926 | 424 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
425 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
426 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
427 } |
2926 | 428 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
429 size_t tmp = s.find_first_of (file_ops::dir_sep_chars (), i); |
2926 | 430 |
8021 | 431 if (tmp == std::string::npos) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
432 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
433 current_dir.append (s, i, tmp-i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
434 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
435 } |
2926 | 436 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
437 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
438 current_dir.append (s, i, tmp-i+1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
439 i = tmp + 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
440 } |
2926 | 441 } |
442 | |
4097 | 443 return current_dir; |
2926 | 444 } |
445 | |
4097 | 446 // Return a string which is the current working directory. |
2926 | 447 |
3504 | 448 std::string |
4097 | 449 octave_env::do_getcwd () const |
2926 | 450 { |
451 if (! follow_symbolic_links) | |
452 current_directory = ""; | |
453 | |
454 if (verbatim_pwd || current_directory.empty ()) | |
455 current_directory = ::octave_getcwd (); | |
456 | |
457 return current_directory; | |
458 } | |
459 | |
460 // This value is not cached because it can change while Octave is | |
461 // running. | |
462 | |
3504 | 463 std::string |
2926 | 464 octave_env::do_get_home_directory (void) const |
465 { | |
3504 | 466 std::string hd = do_getenv ("HOME"); |
2926 | 467 |
6096 | 468 #if defined (__MINGW32__) || defined (_MSC_VER) |
469 // Maybe we are started directly from cmd.exe. | |
5451 | 470 if (hd.empty ()) |
5454 | 471 { |
472 std::string drv = do_getenv ("HOMEDRIVE"); | |
473 if (drv.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
474 hd = do_getenv ("HOMEPATH"); |
5454 | 475 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
476 hd = drv + do_getenv ("HOMEPATH"); |
5454 | 477 } |
5451 | 478 #endif |
479 | |
2947 | 480 if (hd.empty ()) |
481 { | |
21729
815b2f500fab
use namespace for system password wrapper class
John W. Eaton <jwe@octave.org>
parents:
21727
diff
changeset
|
482 octave::sys::password pw = octave::sys::password::getpwuid (octave::sys::getuid ()); |
2947 | 483 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
484 hd = pw ? pw.dir () : std::string (file_ops::dir_sep_str ()); |
2947 | 485 } |
486 | |
487 return hd; | |
2926 | 488 } |
489 | |
3504 | 490 std::string |
2926 | 491 octave_env::do_get_user_name (void) const |
492 { | |
493 if (user_name.empty ()) | |
494 { | |
21729
815b2f500fab
use namespace for system password wrapper class
John W. Eaton <jwe@octave.org>
parents:
21727
diff
changeset
|
495 octave::sys::password pw = octave::sys::password::getpwuid (octave::sys::getuid ()); |
2926 | 496 |
3504 | 497 user_name = pw ? pw.name () : std::string ("unknown"); |
2926 | 498 } |
499 | |
500 return user_name; | |
501 } | |
502 | |
3504 | 503 std::string |
2926 | 504 octave_env::do_get_host_name (void) const |
505 { | |
506 if (host_name.empty ()) | |
507 { | |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
508 char hostname[1024]; |
2926 | 509 |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
510 int status = gnulib::gethostname (hostname, 1023); |
2926 | 511 |
3185 | 512 host_name = (status < 0) ? "unknown" : hostname; |
2926 | 513 } |
514 | |
515 return host_name; | |
516 } | |
517 | |
3504 | 518 std::string |
519 octave_env::do_getenv (const std::string& name) const | |
2926 | 520 { |
521 char *value = ::getenv (name.c_str ()); | |
522 | |
523 return value ? value : ""; | |
524 } | |
525 | |
526 // Do the work of changing to the directory NEWDIR. Handle symbolic | |
527 // link following, etc. | |
528 | |
529 bool | |
3504 | 530 octave_env::do_chdir (const std::string& newdir) |
2926 | 531 { |
532 bool retval = false; | |
533 | |
3504 | 534 std::string tmp; |
2926 | 535 |
536 if (follow_symbolic_links) | |
537 { | |
538 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
539 do_getcwd (); |
2926 | 540 |
541 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
542 tmp = newdir; |
2926 | 543 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
544 tmp = do_make_absolute (newdir, current_directory); |
2926 | 545 |
4097 | 546 // Get rid of trailing directory separator. |
2926 | 547 |
548 size_t len = tmp.length (); | |
549 | |
550 if (len > 1) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
551 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
552 if (file_ops::is_dir_sep (tmp[--len])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
553 tmp.resize (len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
554 } |
2926 | 555 |
556 if (! ::octave_chdir (tmp)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
557 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
558 current_directory = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
559 retval = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
560 } |
2926 | 561 } |
562 else | |
563 retval = (! ::octave_chdir (newdir)); | |
564 | |
565 return retval; | |
566 } | |
567 | |
568 // Remove the last N directories from PATH. | |
569 | |
570 void | |
3504 | 571 octave_env::pathname_backup (std::string& path, int n) const |
2926 | 572 { |
573 if (path.empty ()) | |
574 return; | |
575 | |
576 size_t i = path.length () - 1; | |
577 | |
578 while (n--) | |
579 { | |
4097 | 580 while (file_ops::is_dir_sep (path[i]) && i > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
581 i--; |
2926 | 582 |
4097 | 583 while (! file_ops::is_dir_sep (path[i]) && i > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
584 i--; |
2926 | 585 |
586 i++; | |
587 } | |
588 | |
589 path.resize (i); | |
590 } | |
591 | |
592 void | |
593 octave_env::error (int err_num) const | |
594 { | |
10411 | 595 (*current_liboctave_error_handler) ("%s", gnulib::strerror (err_num)); |
2926 | 596 } |
597 | |
598 void | |
3504 | 599 octave_env::error (const std::string& s) const |
2926 | 600 { |
601 (*current_liboctave_error_handler) ("%s", s.c_str ()); | |
602 } |