1 //===--------------------- TildeExpressionResolver.cpp ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Utility/TildeExpressionResolver.h"
11 
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 
16 using namespace lldb_private;
17 using namespace llvm;
18 
19 namespace fs = llvm::sys::fs;
20 namespace path = llvm::sys::path;
21 
22 TildeExpressionResolver::~TildeExpressionResolver() {}
23 
24 bool StandardTildeExpressionResolver::ResolveExact(
25     StringRef Expr, SmallVectorImpl<char> &Output) {
26   // We expect the tilde expression to be ONLY the expression itself, and
27   // contain
28   // no separators.
29   assert(!llvm::any_of(Expr, path::is_separator));
30   assert(Expr.empty() || Expr[0] == '~');
31 
32   return !fs::real_path(Expr, Output, true);
33 }
34 
35 bool StandardTildeExpressionResolver::ResolvePartial(StringRef Expr,
36                                                      StringSet<> &Output) {
37   // We expect the tilde expression to be ONLY the expression itself, and
38   // contain no separators.
39   assert(!llvm::any_of(Expr, path::is_separator));
40   assert(Expr.empty() || Expr[0] == '~');
41 
42   Output.clear();
43 #if defined(LLVM_ON_WIN32)
44   return false;
45 #else
46   if (Expr.empty())
47     return false;
48 
49   SmallString<32> Buffer = "~";
50   setpwent();
51   struct passwd *user_entry;
52   Expr = Expr.drop_front();
53 
54   while ((user_entry = getpwent()) != NULL) {
55     StringRef ThisName(user_entry->pw_name);
56     if (!ThisName.startswith(Expr))
57       continue;
58 
59     Buffer.resize(1);
60     Buffer.append(ThisName);
61     Buffer.append(path::get_separator()) Output.insert(Buffer);
62   }
63 
64   return true;
65 #endif
66 }
67