Skip to content

Commit 31a4de6

Browse files
tilde_expand: don't use regexes, error out for ~user expansion.
Expanding ~user/path to /home/user/path doesn't work on many UNIX systems, including OS X. The correct way to do this is to use the getpwnam function to look up a user's home directory from their user name. I'll implement that — it will look a lot like the code for Stat and will be handy for querying things about users. For now I'm just making ~user expansion an error.
1 parent b9441d1 commit 31a4de6

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

base/file.jl

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ const last_separator = Regex(strcat(os_separator_match, "(?!.*", os_separator_ma
2020
# an os_separator (handles cases like .bashrc or /home/fred/.juliarc)
2121
const extension_separator_match = Regex(strcat("(?<!^)(?<!",
2222
os_separator_match, ")\\.(?!.*[", os_separator_match_chars, "\.])"))
23-
# Match ~/filename
24-
const plain_tilde = Regex(strcat("^~", os_separator_match))
25-
# Match ~user/filename
26-
const user_tilde = r"^~\w"
2723

2824
filesep() = os_separator
2925

@@ -100,23 +96,15 @@ function isrooted(path::String)
10096
false
10197
end
10298

103-
global tilde_expand
104-
function tilde_expand(path::String)
105-
@windows_only return path # on windows, ~ means "temporary file"
106-
@unix_only begin
107-
m = match(user_tilde, path)
108-
if m != nothing
109-
return "/home/"*path[2:end]
110-
end
111-
end
112-
m = match(plain_tilde, path)
113-
if m != nothing
114-
return ENV["HOME"]*path[2:end]
115-
end
116-
if path == "~"
117-
return ENV["HOME"]
118-
end
119-
path
99+
@windows_only tilde_expand(path::String) = path # on windows, ~ means "temporary file"
100+
@unix_only function tilde_expand(path::String)
101+
i = start(path)
102+
c, i = next(path,i)
103+
if c != '~' return path end
104+
if done(path,i) return ENV["HOME"] end
105+
c, j = next(path,i)
106+
if c == '/' return ENV["HOME"]*path[i:end] end
107+
error("~user tilde expansion not yet implemented")
120108
end
121109

122110
# Get the absolute path to a file. Uses file system for cwd() when
@@ -161,8 +149,6 @@ function abs_path(fname::String)
161149
end
162150

163151

164-
# The remaining commands use the file system in some way
165-
166152
# Get the full, real path to a file, including dereferencing
167153
# symlinks.
168154
function real_path(fname::String)
@@ -174,7 +160,6 @@ function real_path(fname::String)
174160
return s
175161
end
176162

177-
178163
# get and set current directory
179164

180165
function cwd()
@@ -188,7 +173,6 @@ cd(dir::String) = system_error("chdir", ccall(:chdir,Int32,(Ptr{Uint8},),tilde_e
188173
cd() = cd(ENV["HOME"])
189174

190175
# do stuff in a directory, then return to current directory
191-
192176
function cd(f::Function, dir::String)
193177
fd = ccall(:open,Int32,(Ptr{Uint8},Int32),".",0)
194178
system_error("open", fd == -1)

0 commit comments

Comments
 (0)