Skip to content

Commit 535ee79

Browse files
authored
[k2] fix fgets (#1522)
1 parent 0bb1456 commit 535ee79

4 files changed

Lines changed: 11 additions & 6 deletions

File tree

runtime-light/k2-platform/k2-api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ inline size_t pread(k2::descriptor descriptor, std::span<std::byte> buffer, uint
216216
return k2_pread(descriptor, buffer.size(), static_cast<void*>(buffer.data()), offset);
217217
}
218218

219-
inline size_t fgets(k2::descriptor descriptor, std::span<std::byte> buffer) noexcept {
220-
return k2_fgets(descriptor, buffer.size(), static_cast<void*>(buffer.data()));
219+
inline size_t readline(k2::descriptor descriptor, std::span<std::byte> buffer) noexcept {
220+
return k2_readline(descriptor, buffer.size(), static_cast<void*>(buffer.data()));
221221
}
222222

223223
inline void* mmap(k2::descriptor* md, void* addr, size_t length, int32_t prot, int32_t flags, k2::descriptor fd, uint64_t offset) noexcept {

runtime-light/k2-platform/k2-header.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,11 @@ size_t k2_read(uint64_t stream_d, size_t buf_len, void* buf);
357357
size_t k2_pread(uint64_t stream_d, size_t buf_len, void* buf, uint64_t offset);
358358

359359
/**
360-
* equivalent to libc's `fgets` function
360+
* behavior is similar to libc's `fgets` function, except that EOF and empty string return the same result.
361361
*
362362
* @return `0` if EOF is reached or an error occurs. total number of bytes read otherwise
363363
*/
364-
size_t k2_fgets(uint64_t stream_d, size_t buf_len, void* buf);
364+
size_t k2_readline(uint64_t stream_d, size_t buf_len, void* buf);
365365

366366
/**
367367
* Semantically equivalent to libc's `mmap` function.

runtime-light/stdlib/file/file-system-functions.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ Optional<string> f$fgets(const resource& stream, int64_t length) noexcept {
396396
return false;
397397
}
398398

399+
if (length == 1) {
400+
return string{};
401+
}
402+
399403
auto file_resource{from_mixed<class_instance<kphp::fs::file>>(stream, {})};
400404
if (file_resource.is_null()) {
401405
return false;
@@ -418,7 +422,7 @@ Optional<string> f$fgets(const resource& stream, int64_t length) noexcept {
418422
return false;
419423
}
420424
string res{static_cast<string::size_type>(length), false};
421-
auto read_res{k2::fgets(file.descriptor(), std::as_writable_bytes(std::span<char>{res.buffer(), res.size()}))};
425+
auto read_res{k2::readline(file.descriptor(), std::as_writable_bytes(std::span<char>{res.buffer(), res.size()}))};
422426

423427
if (read_res == 0) {
424428
return false;

tests/phpt/dl/473_fgets.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@ok
22
<?php
3+
34
function init() {
45
$stream = fopen(__DIR__.'/fgets.txt', 'w');
56
fwrite ($stream, "123\n");
@@ -20,7 +21,7 @@ function test_fgets() {
2021
var_dump (fgets ($stream)); // line = "\n"
2122

2223
var_dump (fgets ($stream, 4)); // `length` less than length of the line. line = "php < kphp\n"
23-
var_dump (fgets ($stream, 1)); // always false
24+
// var_dump (fgets ($stream, 1)); kphp expects "", php expects false
2425
var_dump (fgets ($stream)); // rest of the line
2526

2627
var_dump (fgets ($stream)); // last line. line = "bang"

0 commit comments

Comments
 (0)