rendered paste body#include "api.h"#include "lib.h"#include "errno.h"#define WIDTH 80#define DATA_LEN ((WIDTH-4)/3)// dump the hex of a file.static const char* USAGE = "Usage: xd [-c count] filename\n";static void out(char c) { char val = c&0xf; if(val<10) putch('0'+val); else putch('A'+val-10);}int main(int argc,char** argv) { int fd=0; char *filename=0; int count=0; char** arg=argv; while(*++arg) { if(strncmp("-c",*arg,3)==0) { ++arg; if(*arg) { count=atoi(*arg); if(count==0) { print(USAGE); return 1; } } else { print(USAGE); return 1; } continue; } filename=*arg; } if(filename) { fd=sys_open(filename,0); if(fd==-1) { printf("Error opening '%s': %s\n",argv[1],strerror(errno)); return 1; } } u8 data[DATA_LEN]; int eof=0; int total_read=0; while(!eof) { if(count && total_read>=count) break; int to_read = total_read+DATA_LEN> count ? count-total_read :DATA_LEN; memset(data,0,sizeof(data)); int br=sys_read(fd,data,to_read); if(br==-1) { printf("Error reading from %d: %s\n",fd,strerror(errno)); return 1; } if(br==0) { return 0; } while(br<DATA_LEN) { int br2=sys_read(fd,data+br,to_read-br); if(br2==0) { eof=1; break; } if(br==-1) { printf("Error reading from %d: %s\n",fd,strerror(errno)); return 1; } br+=br2; } total_read+=br; // put hex data. for(int i=0;i<br;++i) { out(data[i]>>4); out(data[i]); } // make it all line up for(int i=br;i<DATA_LEN;++i) { putch(' '); putch(' '); } print(" "); for(int i=0;i<br;++i) { if(data[i]>0x20 && data[i]<0x7f) putch(data[i]); else putch('.'); } putch('\n');} return 0;}