Commits

Evan Gates  committed 2ddfa08

update rev.c (more obfuscated, more correct)

  • Participants
  • Parent commits dbccb82

Comments (0)

Files changed (1)

 #include <string.h>
 #include <sys/stat.h>
 #include <unistd.h>
-int main(int c,char**v){int r,f=0,i=1;char*b,*e,*p;b=e=p=0;do{if(c>1&&(f=open(v
-[i],O_RDONLY))<0){fprintf(stderr,"%s: cannot open %s: %s\n",*v,v[i],strerror(
-errno));continue;}do{do{if(p==e){char*q=p=b;b=realloc(b,(e-p+1)*2);if(!b){
-fprintf(stderr,"%s: realloc failed\n",*v);free(q);return 1;}p=b+(e-p);e=b+(p-b+
-1)*2;}}while((r=read(f,p,1))&&*p++!='\n');if(p-b){while(p>b)if(*--p!='\n')write
-(1,p,1);write(1,"\n",1);}}while(r);}while(++i<c);free(b);return 0;}
+#define x(s,d,...)if(s){o=errno;fputs(*v,stderr);fprintf(stderr,": "__VA_ARGS__\
+);errno=o;perror(0);d;}
+#define s(r,s,d,...)while((r=s)<0&&errno==EINTR);x(r<0,d,__VA_ARGS__)
+#define z " %s: ",(c>1?v[i]:"stdin")
+#define l s(w,close(f),,"close"z)
+#define w(c)s(w,write(1,c,1),,"write: ")
+int main(int c,char**v){int o,r,f,i=1,w,n;f=n=0;char*b,*e,*p,*q;b=e=p=0;do{if(c>
+1){s(f,open(v[i],O_RDONLY),n=1;continue,"cannot open %s: ",v[i])}do{do{if(p==e){
+q=p=b;b=realloc(b,(e-p)*2+1);x(!b,{free(q);l return 1;},"realloc: ")p=e-p+b;e=b+
+(p-b)*2+1;}s(r,read(f,p,1),n=1,"read"z)}while(r>0&&*p++-10);if(r+1&&p-b){while(p
+-b)if(*--p-10){w(p)}w("\n")}}while(r>0);l}while(++i<c);free(b);return n;}