gethostname

gethostname(): string|false

获取本机主机名:CI 与本地结果必然不同,确定性策略是只输出「获取成功 + 长度 > 0」的派生事实,不输出名字本身。

C C23

C23
#include <stdio.h>
#include <unistd.h>

int main(void) {
    // POSIX gethostname(2)——PHP 函数背后的 C 原版
    char buf[256];
    int rc = gethostname(buf, sizeof buf);
    printf("ok: %s\n", rc == 0 ? "yes" : "no");
    printf("length > 0: %s\n", rc == 0 && buf[0] != '\0' ? "yes" : "no");
    return 0;
}
输出
ok: yes
length > 0: yes

C++ C++26

C++26
#include <cstdio>
#include <unistd.h>

int main() {
    // C++ 标准库无此 API——直接用 POSIX gethostname
    char buf[256];
    int rc = gethostname(buf, sizeof buf);
    std::printf("ok: %s\n", rc == 0 ? "yes" : "no");
    std::printf("length > 0: %s\n", rc == 0 && buf[0] != '\0' ? "yes" : "no");
}
输出
ok: yes
length > 0: yes

C# v11.0.100

v11.0.100
// Environment.MachineName 是托管等价物
string name = Environment.MachineName;
Console.WriteLine("ok: yes");
Console.WriteLine("length > 0: " + (name.Length > 0 ? "yes" : "no"));
输出
ok: yes
length > 0: yes

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

func main() {
	// os.Hostname() 封装了 POSIX 调用
	name, err := os.Hostname()
	fmt.Println("ok:", err == nil)
	fmt.Println("length > 0:", len(name) > 0)
}
输出
ok: true
length > 0: true

Java v27.0.1

v27.0.1
import java.net.InetAddress;

public class Main {
    public static void main(String[] args) throws Exception {
        // InetAddress.getLocalHost().getHostName() 是经典写法;
        // 值因机器而异,所以只输出派生事实
        String name = InetAddress.getLocalHost().getHostName();
        System.out.println("ok: yes");
        System.out.println("length > 0: " + (name.length() > 0 ? "yes" : "no"));
    }
}
输出
ok: yes
length > 0: yes

JavaScript v26.13.1

v26.13.1
// os.hostname() 是原生等价物
const os = require("node:os");
const name = os.hostname();
console.log("ok: yes");
console.log("length > 0:", name.length > 0 ? "yes" : "no");
输出
ok: yes
length > 0: yes

PHP v8.6.0

v8.6.0
// gethostname() 在 CLI 下同样可用;主机名本身因机器而异,
// 所以只输出派生事实
$h = gethostname();
echo "ok: ", is_string($h) ? "yes" : "no", "\n";
echo "length > 0: ", is_string($h) && strlen($h) > 0 ? "yes" : "no", "\n";
输出
ok: yes
length > 0: yes

Python v3.15.2

v3.15.2
# socket.gethostname() 封装了 POSIX 调用
import socket

name = socket.gethostname()
print("ok: yes")
print("length > 0:", "yes" if len(name) > 0 else "no")
输出
ok: yes
length > 0: yes

Rust v1.99.1

v1.99.1
fn main() {
    // Rust 标准库无主机名 API;改调 hostname(1) 命令
    let out = std::process::Command::new("hostname").output().unwrap();
    let name = String::from_utf8_lossy(&out.stdout).trim().to_string();
    println!("ok: {}", if out.status.success() { "yes" } else { "no" });
    println!("length > 0: {}", if !name.is_empty() { "yes" } else { "no" });
}
输出
ok: yes
length > 0: yes

TypeScript v26.13.1

v26.13.1
// os.hostname() 是原生等价物
import * as os from "node:os";
const name: string = os.hostname();
console.log("ok: yes");
console.log("length > 0:", name.length > 0 ? "yes" : "no");
输出
ok: yes
length > 0: yes

语言差异说明

C:gethostname(2) 是 POSIX 原版,PHP 即其封装。

C++:标准库无此 API,直接用 POSIX。

C#:Environment.MachineName 托管等价。

Go:os.Hostname() 返回 (name, err)。

Java:InetAddress.getLocalHost().getHostName() 经典但底层可能涉及名字解析。

JavaScript:os.hostname();浏览器 JS 无此能力。

PHP:gethostname() CLI/Web 均可用;php_uname("n") 是变体。

Python:socket.gethostname();platform.node() 等价。

Rust:标准库空白,第三方 hostname crate 或命令行兜底。

TypeScript:同 JavaScript。

确定性提示:主机名必然因机器而异,示例只断言「获取成功 + 非空」这类环境无关事实。