hd from stdin

This commit is contained in:
Christoph J. Scherr 2023-09-14 17:56:26 +02:00
parent f12c1141eb
commit 3465c56c2e
2 changed files with 15 additions and 7 deletions

View File

@ -12,6 +12,7 @@ This is a list of tasks that would be cool to add.
- (x) Wait for new input when the file is lazy - (x) Wait for new input when the file is lazy
- (x) Install script to system - (x) Install script to system
Using update-alternatives is the most handy Using update-alternatives is the most handy
- (x) Accept input from stdin when no file is given
- ( ) Write a complete task for this stuff - ( ) Write a complete task for this stuff
*** Networking *** Networking
- ( ) Mini curl - ( ) Mini curl

View File

@ -2,6 +2,7 @@
import argparse import argparse
import os import os
import sys import sys
import io
import fcntl # only for non blocking IO, if you dont know what it is, you can ignore it or study up import fcntl # only for non blocking IO, if you dont know what it is, you can ignore it or study up
# 2**30 bytes aka 1 Mebibyte # 2**30 bytes aka 1 Mebibyte
@ -35,7 +36,7 @@ def main():
prog="hd", prog="hd",
description="Dumps data as hex" description="Dumps data as hex"
) )
parser.add_argument("file") parser.add_argument("file", nargs='?', type=argparse.FileType('rb'), default=sys.stdin)
parser.add_argument("-c", "--chars", action="store_true") parser.add_argument("-c", "--chars", action="store_true")
parser.add_argument("-f", "--force", action="store_true") # ignore set limits parser.add_argument("-f", "--force", action="store_true") # ignore set limits
args = parser.parse_args() args = parser.parse_args()
@ -62,7 +63,7 @@ def main():
# make a syscall that will open our file in readonly mode, nonblocking. # make a syscall that will open our file in readonly mode, nonblocking.
# if it did not exist and we write to it, it would be created with the # if it did not exist and we write to it, it would be created with the
# perms: 644 (like `chmod 644 myfile`) # perms: 644 (like `chmod 644 myfile`)
fd = os.open(args.file, os.O_RDONLY | os.O_NONBLOCK, mode=0o644) fd = args.file.fileno()
# now we make the fd into the high level python class `file` # now we make the fd into the high level python class `file`
file = os.fdopen(fd, "rb") file = os.fdopen(fd, "rb")
# i read somewhere that file.readline() does not work like this, but as # i read somewhere that file.readline() does not work like this, but as
@ -76,12 +77,18 @@ def main():
sys.exit(1) sys.exit(1)
# check if the file has a reasonable size to dump # check if the file has a reasonable size to dump
if 0 != os.path.getsize(args.file) > MAX_SIZE: try:
file.seek(0, os.SEEK_END)
flen = file.tell()
except io.UnsupportedOperation:
# some kinds of "files" like stdin are not seekable
flen = 0
if 0 != flen > MAX_SIZE:
print(f"""The file you are trying to dump is larger than 1M.\n\ print(f"""The file you are trying to dump is larger than 1M.\n\
Actual size: {humanbytes(os.path.getsize(args.file))}\nrefusing to dump""") Actual size: {humanbytes(flen)}\nrefusing to dump""")
if (0 != os.path.getsize(args.file) > MAX_SIZE) and not args.force: if (0 != flen > MAX_SIZE) and not args.force:
print(f"The file you are trying to dump is larger than {humanbytes(MAX_SIZE)}.\n\n\ print(f"The file you are trying to dump is larger than {humanbytes(MAX_SIZE)}.\n\n\
Actual size: {humanbytes(os.path.getsize(args.file))}\n\nrefusing to dump. You can force dump the file with --force.") Actual size: {humanbytes(flen)}\n\nrefusing to dump. You can force dump the file with --force.")
sys.exit(2) sys.exit(2)
# print header # print header
if args.chars: if args.chars: