Przeglądaj źródła

Return stderr if stdout is empty

This also includes a note if both are empty.  Both having content is not
yet handled.
Luna Stadler 4 lat temu
rodzic
commit
9b1c050796
1 zmienionych plików z 11 dodań i 4 usunięć
  1. 11 4
      zig/sdl/hello_sdl.zig

+ 11 - 4
zig/sdl/hello_sdl.zig

@ -244,13 +244,20 @@ fn runCommand(raw_cmd: []const u8, allocator: *std.mem.Allocator) ![]const u8 {
244 244
        std.debug.print("'{s}' ", .{arg});
245 245
    }
246 246
    const result = try std.ChildProcess.exec(.{ .allocator = allocator, .argv = argv, .max_output_bytes = 1024 * 1024 });
247
    std.debug.print("stdout: '{s}'\n", .{result.stdout[0..std.math.min(100, result.stdout.len)]});
247 248
    std.debug.print("stderr: '{s}'\n", .{result.stderr});
248
    defer {
249
        // FIXME: allocator.free(result.stdout);
249
250
    if (result.stdout.len > 0) {
250 251
        allocator.free(result.stderr);
252
        return result.stdout;
253
    } else if (result.stderr.len > 0) {
254
        allocator.free(result.stdout);
255
        return result.stderr;
256
    } else {
257
        allocator.free(result.stdout);
258
        allocator.free(result.stderr);
259
        return std.fmt.allocPrint(allocator, "<no output>", .{});
251 260
    }
252
253
    return result.stdout;
254 261
}
255 262
256 263
// tests