linux.rs 801 B

123456789101112131415161718192021222324252627282930
  1. use alloc::string::String;
  2. use c_str::CString;
  3. use header::fcntl;
  4. use fs::File;
  5. use io::{BufRead, BufReader};
  6. pub fn get_dns_server() -> String {
  7. let file = match File::open(
  8. &CString::new("/etc/resolv.conf").unwrap(),
  9. fcntl::O_RDONLY
  10. ) {
  11. Ok(file) => file,
  12. Err(_) => return String::new(), // TODO: better error handling
  13. };
  14. let file = BufReader::new(file);
  15. for line in file.split(b'\n') {
  16. let mut line = match line {
  17. Ok(line) => line,
  18. Err(_) => return String::new() // TODO: pls handle errors
  19. };
  20. if line.starts_with(b"nameserver ") {
  21. line.drain(..11);
  22. return String::from_utf8(line).unwrap_or_default();
  23. }
  24. }
  25. // TODO: better error handling
  26. String::new()
  27. }