SendCountedFile 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/sh
  2. # send a 4 byte (32 bit LE) count then a file to standard output
  3. FileList='
  4. kernel.elf
  5. kernel/kernel.elf
  6. mahatma.elf
  7. kernel/mahatma.elf
  8. KERNEL
  9. forth.elf
  10. forth/forth.elf
  11. '
  12. usage()
  13. {
  14. [ -z "$1" ] || echo error: $* >&2
  15. echo usage: $(basename "$0") '<filename>' >&2
  16. exit 1
  17. }
  18. FileName="$1"
  19. if [ -z "${FileName}" ]
  20. then
  21. for f in ${FileList}
  22. do
  23. echo trying: ${f} >&2
  24. if [ -e "${f}" ]
  25. then
  26. FileName="${f}"
  27. break
  28. fi
  29. done
  30. [ -z "${FileName}" ] && usage missing argument
  31. else
  32. [ -e "${FileName}" ] || usage file, "'"${FileName}'"' does not exist >&2
  33. fi
  34. if [ -L "${FileName}" ]
  35. then
  36. FileName=$(readlink -m "${FileName}")
  37. fi
  38. size=$(ls -l "${FileName}" | awk '{print $5}')
  39. printf 'File: "%s", size = 0x%x [%d]\n' ${FileName} ${size} ${size} >&2
  40. b1=$(printf '%02x' $((${size} & 0xff)))
  41. b2=$(printf '%02x' $((( ${size} >> 8 ) & 0xff)))
  42. b3=$(printf '%02x' $((( ${size} >> 16 ) & 0xff)))
  43. b4=$(printf '%02x' $((( ${size} >> 24 ) & 0xff)))
  44. printf "\x${b1}\x${b2}\x${b3}\x${b4}"
  45. dd if="${FileName}" bs=1