/* convert "ls -lR" to crc format */ /* * The author of this software is Eric Grosse. Copyright (c) 1995 by AT&T. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */ /* This uses local libraries, but can be made portable later if demand warrants. To tell the truth, I hope this hack doesn't catch on. Among the unfixable bugs: the file date will change as the calendar advances so that hh:mm turns into yyyy in the ls -lR listing. I've borrowed from Presotto (Plan9) /sys/src/cmd/ftpfs/proto.c. */ #include "num.h" char *MonthName[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; #define OK 0 time_t clck; struct tm *now; time_t parsedate(int datef) { int mon; struct tm *t; char *f2, *p; t = localtime(&clck); for( mon = 1; mon<13; mon++) if(strcmp(field[datef],MonthName[mon])==0) break; mon<=12 || Error("bad date"); t->tm_mon = mon; t->tm_mday = atoi(field[datef+1]); f2 = field[datef+2]; if(p = strchr(f2,':')){ /* hh:mm */ t->tm_hour = atoi(f2); t->tm_min = strtol(p+1,&p,0); if(*p==':') t->tm_sec = atoi(p+1); if(t->tm_mon > now->tm_mon || (t->tm_mon==now->tm_mon && t->tm_mday>now->tm_mday+1)) t->tm_year--; }else{ /* yyyy */ t->tm_year = atoi(f2)-1900; } return(mktime(t)); } void main(int argc, char**argv) { char *dir = 0, *cd; int n; unsigned long siz; time_t date; char *siz_end; ehgwmc(argc,argv); clck = time(0); now = localtime(&clck); while(awk()!=EOF){ if(NF==0) /* "" */ continue; if(isdigit(field[1][0]))/* 200 PORT command successful. */ continue; if(NF>2 && strcmp(field[NF-1],"->")==0) NF -= 2; switch(NF){ case 1: /* "mac:" */ cd = field[1]; n = strlen(cd); if(cd[n-1]!=':') continue; if(cd[0]=='.' && cd[1]=='/'){ cd += 2; /* chop ./ */ n -= 2; } dir = (char*)Realloc(dir,n+1); strcpy(dir,cd); dir[n-1] = '\0'; /* chop : */ break; case 2: /* "total 1447" */ break; case 7: /* "-r--r--r-- gs 972 Feb 2 09:42 gs.tar.gz" */ if(field[1][0]=='d') continue; siz = strtoul(field[3],&siz_end,10); if(*siz_end!='\0'){ fprintf(stderr,"bad size l.%d\n",NR); continue; } date = parsedate(4); if(dir) printf("%s/%s %u %u 0\n",dir,field[7],date,siz); else printf("%s %u %u 0\n",field[7],date,siz); break; case 8: /* "-r--r--r-- 1 gs 972 Feb 2 09:42 gs.tar.gz" */ if(field[1][0]=='d') continue; siz = strtoul(field[4],&siz_end,10); if(*siz_end!='\0'){ fprintf(stderr,"bad size l.%d\n",NR); continue; } date = parsedate(5); if(dir) printf("%s/%s %u %u 0\n",dir,field[8],date,siz); else printf("%s %u %u 0\n",field[8],date,siz); break; case 9: /* "-r--r--r-- 1 gs num 972 Feb 2 09:42 gs.tar.gz" */ if(field[1][0]=='d') continue; siz = strtoul(field[5],&siz_end,10); if(*siz_end!='\0'){ fprintf(stderr,"bad size l.%d\n",NR); continue; } date = parsedate(6); if(dir) printf("%s/%s %u %u 0\n",dir,field[9],date,siz); else printf("%s %u %u 0\n",field[9],date,siz); break; default: fprintf(stderr,"[%d] %s\n",NF,field[0]); break; } } exit(0); }