-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Open
@divinity76
Description
Description
The following invocation:
php -n -r 'fseek(fopen("/dev/zero", "rb"), 1*1024*1024, SEEK_SET);'
Resulted in this output:
Warning: fseek(): Stream does not support seeking in Command line code on line 1
But I expected this output instead:
- there's nothing illegal/wrong about seeking in a /dev/zero (or /dev/null for that matter) handle.
If you try doing it in C, you'll see there is no complaint from the OS whatsoever, for example if you run
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp = fopen("/dev/zero", "rb"); if (fp == NULL) { perror("Failed to open /dev/zero"); return EXIT_FAILURE; } if (fseek(fp, 1024 * 1024, SEEK_CUR) != 0) { perror("Failed to fseek to 1MB"); fclose(fp); return EXIT_FAILURE; } int c = fgetc(fp); if (c == EOF) { if (ferror(fp)) { perror("Error reading from /dev/zero"); } else { fprintf(stderr, "Unexpected EOF on /dev/zero\n"); } fclose(fp); return EXIT_FAILURE; } if (putchar(c) == EOF) { perror("Failed to write to stdout"); fclose(fp); return EXIT_FAILURE; } if (fclose(fp) != 0) { perror("Failed to close /dev/zero"); return EXIT_FAILURE; } return EXIT_SUCCESS; }
or
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> int main(void) { int fd = open("/dev/zero", O_RDONLY); if (fd == -1) { perror("Failed to open /dev/zero"); return EXIT_FAILURE; } // Seek forward 1MB if (lseek(fd, 1024 * 1024, SEEK_CUR) == (off_t)-1) { perror("Failed to lseek to 1MB"); close(fd); return EXIT_FAILURE; } unsigned char c; ssize_t n = read(fd, &c, 1); if (n == -1) { perror("Error reading from /dev/zero"); close(fd); return EXIT_FAILURE; } else if (n == 0) { fprintf(stderr, "Unexpected EOF on /dev/zero\n"); close(fd); return EXIT_FAILURE; } if (write(STDOUT_FILENO, &c, 1) == -1) { perror("Failed to write to stdout"); close(fd); return EXIT_FAILURE; } if (close(fd) == -1) { perror("Failed to close /dev/zero"); return EXIT_FAILURE; } return EXIT_SUCCESS; }
you will get no complains.
PHP Version
PHP 8.3.6 (cli) (built: Jul 14 2025 18:30:55) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
Operating System
Ubuntu 24.04 on WSL2 on Windows 10