forked from dart-lang/native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost_name.dart
More file actions
34 lines (28 loc) · 1.21 KB
/
host_name.dart
File metadata and controls
34 lines (28 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart';
import 'third_party/unix.dart' as unix;
import 'third_party/windows.dart' as windows;
/// The machine's hostname.
///
/// Returns `null` if looking up the machines host name fails.
String? getHostName() => using((arena) {
const maxHostNameLength = 256;
final buffer = arena<Char>(maxHostNameLength);
final result = Platform.isWindows
? windows.gethostname(buffer, maxHostNameLength)
: unix.gethostname(buffer, maxHostNameLength);
if (result != 0) {
// The `errno` or `WSAGetLastError` are not preserved currently.
// https://github.com/dart-lang/sdk/issues/38832 So, simply return null
// instead of throwing an exception with the system error message. (A
// possible workaround is to do the `errno` and `WSAGetLastError` in C
// wrapper functions. However, this example project is meant to show an
// example without compiling C code.)
return null;
}
return buffer.cast<Utf8>().toDartString();
});