StandardPaths.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibCore/StandardPaths.h>
  10. #include <pwd.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. namespace Core {
  14. String StandardPaths::home_directory()
  15. {
  16. if (auto* home_env = getenv("HOME"))
  17. return LexicalPath::canonicalized_path(home_env);
  18. auto* pwd = getpwuid(getuid());
  19. String path = pwd ? pwd->pw_dir : "/";
  20. endpwent();
  21. return LexicalPath::canonicalized_path(path);
  22. }
  23. String StandardPaths::desktop_directory()
  24. {
  25. StringBuilder builder;
  26. builder.append(home_directory());
  27. builder.append("/Desktop");
  28. return LexicalPath::canonicalized_path(builder.to_string());
  29. }
  30. String StandardPaths::downloads_directory()
  31. {
  32. StringBuilder builder;
  33. builder.append(home_directory());
  34. builder.append("/Downloads");
  35. return LexicalPath::canonicalized_path(builder.to_string());
  36. }
  37. String StandardPaths::config_directory()
  38. {
  39. StringBuilder builder;
  40. builder.append(home_directory());
  41. builder.append("/.config");
  42. return LexicalPath::canonicalized_path(builder.to_string());
  43. }
  44. String StandardPaths::tempfile_directory()
  45. {
  46. return "/tmp";
  47. }
  48. }