1 /* #includes */ /*{{{C}}}*//*{{{*/
18 /* variables */ /*{{{*/
19 static const char * const month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
22 /* namecmp -- compare two entries */ /*{{{*/
23 static int namecmp(const void *a, const void *b)
25 if (**((const char * const *)a)=='[') return -1;
26 return strcmp(*((const char * const *)a),*((const char * const *)b));
29 /* olddir -- old style output */ /*{{{*/
30 static void olddir(char **dirent, int entries)
32 int i,j,k,l,user,announce;
35 for (user=0; user<32; ++user)
37 for (i=l=0; i<entries; ++i)
39 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
43 printf("User %d\n",user);
46 if (l%4) printf(" : ");
47 for (j=2; dirent[i][j] && dirent[i][j]!='.'; ++j) putchar(toupper(dirent[i][j]));
48 k=j; while (k<11) { putchar(' '); ++k; }
49 if (dirent[i][j]=='.') ++j;
50 for (k=0; dirent[i][j]; ++j,++k) putchar(toupper(dirent[i][j]));
51 for (; k<3; ++k) putchar(' ');
63 if (announce==2) announce=1;
65 if (entries==0) printf("No files\n");
68 /* oldddir -- old style long output */ /*{{{*/
69 static void oldddir(char **dirent, int entries, struct cpmInode *ino)
72 struct cpmStat statbuf;
77 int i,j,k,l,announce,user;
79 qsort(dirent,entries,sizeof(char*),namecmp);
81 printf(" Name Bytes Recs Attr update create\n");
82 printf("------------ ------ ------ ---- ----------------- -----------------\n");
84 for (l=user=0; user<32; ++user)
86 for (i=0; i<entries; ++i)
90 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
94 printf("\nUser %d:\n\n",user);
95 printf(" Name Bytes Recs Attr update create\n");
96 printf("------------ ------ ------ ---- ----------------- -----------------\n");
99 for (j=2; dirent[i][j] && dirent[i][j]!='.'; ++j) putchar(toupper(dirent[i][j]));
100 k=j; while (k<10) { putchar(' '); ++k; }
102 if (dirent[i][j]=='.') ++j;
103 for (k=0; dirent[i][j]; ++j,++k) putchar(toupper(dirent[i][j]));
104 for (; k<3; ++k) putchar(' ');
106 cpmNamei(ino,dirent[i],&file);
107 cpmStat(&file,&statbuf);
108 printf(" %5.1ldK",(long) (statbuf.size+buf.f_bsize-1) /
109 buf.f_bsize*(buf.f_bsize/1024));
111 printf(" %6.1ld ",(long)(statbuf.size/128));
112 putchar(statbuf.mode&0200 ? ' ' : 'R');
113 putchar(statbuf.mode&01000 ? 'S' : ' ');
117 tmp=localtime(&statbuf.mtime);
118 printf(" %02d-%s-%04d %02d:%02d",tmp->tm_mday,month[tmp->tm_mon],tmp->tm_year+1900,tmp->tm_hour,tmp->tm_min);
120 else if (statbuf.ctime) printf(" ");
123 tmp=localtime(&statbuf.ctime);
124 printf(" %02d-%s-%04d %02d:%02d",tmp->tm_mday,month[tmp->tm_mon],tmp->tm_year+1900,tmp->tm_hour,tmp->tm_min);
130 if (announce==2) announce=1;
132 printf("%5.1d Files occupying %6.1ldK",l,(buf.f_bused*buf.f_bsize)/1024);
133 printf(", %7.1ldK Free.\n",(buf.f_bfree*buf.f_bsize)/1024);
135 else printf("No files found\n");
138 /* old3dir -- old CP/M Plus style long output */ /*{{{*/
139 static void old3dir(char **dirent, int entries, struct cpmInode *ino)
141 struct cpmStatFS buf;
142 struct cpmStat statbuf;
143 struct cpmInode file;
147 int i,j,k,l,announce,user, attrib;
148 int totalBytes=0,totalRecs=0;
150 qsort(dirent,entries,sizeof(char*),namecmp);
153 for (l=0,user=0; user<32; ++user)
155 for (i=0; i<entries; ++i)
159 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
161 cpmNamei(ino,dirent[i],&file);
162 cpmStat(&file,&statbuf);
163 cpmAttrGet(&file, &attrib);
166 if (user) putchar('\n');
167 printf("Directory For Drive A: User %2.1d\n\n",user);
168 printf(" Name Bytes Recs Attributes Prot Update Create\n");
169 printf("------------ ------ ------ ------------ ------ -------------- --------------\n\n");
172 for (j=2; dirent[i][j] && dirent[i][j]!='.'; ++j) putchar(toupper(dirent[i][j]));
173 k=j; while (k<10) { putchar(' '); ++k; }
175 if (dirent[i][j]=='.') ++j;
176 for (k=0; dirent[i][j]; ++j,++k) putchar(toupper(dirent[i][j]));
177 for (; k<3; ++k) putchar(' ');
179 totalBytes+=statbuf.size;
180 totalRecs+=(statbuf.size+127)/128;
181 printf(" %5.1ldk",(long) (statbuf.size+buf.f_bsize-1) /
182 buf.f_bsize*(buf.f_bsize/1024));
183 printf(" %6.1ld ",(long)(statbuf.size/128));
184 putchar((attrib & CPM_ATTR_F1) ? '1' : ' ');
185 putchar((attrib & CPM_ATTR_F2) ? '2' : ' ');
186 putchar((attrib & CPM_ATTR_F3) ? '3' : ' ');
187 putchar((attrib & CPM_ATTR_F4) ? '4' : ' ');
188 putchar((statbuf.mode&(S_IWUSR|S_IWGRP|S_IWOTH)) ? ' ' : 'R');
189 putchar((attrib & CPM_ATTR_SYS) ? 'S' : ' ');
190 putchar((attrib & CPM_ATTR_ARCV) ? 'A' : ' ');
192 if (attrib & CPM_ATTR_PWREAD) printf("Read ");
193 else if (attrib & CPM_ATTR_PWWRITE) printf("Write ");
194 else if (attrib & CPM_ATTR_PWDEL) printf("Delete ");
195 else printf("None ");
198 tmp=localtime(&statbuf.mtime);
199 printf("%02d/%02d/%02d %02d:%02d ",tmp->tm_mon+1,tmp->tm_mday,tmp->tm_year%100,tmp->tm_hour,tmp->tm_min);
201 else if (statbuf.ctime) printf(" ");
204 tmp=localtime(&statbuf.ctime);
205 printf("%02d/%02d/%02d %02d:%02d",tmp->tm_mon+1,tmp->tm_mday,tmp->tm_year%100,tmp->tm_hour,tmp->tm_min);
211 if (announce==2) announce=1;
213 printf("\nTotal Bytes = %6.1dk ",(totalBytes+1023)/1024);
214 printf("Total Records = %7.1d ",totalRecs);
215 printf("Files Found = %4.1d\n",l);
216 printf("Total 1k Blocks = %6.1ld ",(buf.f_bused*buf.f_bsize)/1024);
217 printf("Used/Max Dir Entries For Drive A: %4.1ld/%4.1ld\n",buf.f_files-buf.f_ffree,buf.f_files);
219 else printf("No files found\n");
222 /* ls -- UNIX style output */ /*{{{*/
223 static void ls(char **dirent, int entries, struct cpmInode *ino, int l, int c, int iflag)
225 int i,user,announce,any;
227 struct cpmStat statbuf;
228 struct cpmInode file;
231 qsort(dirent,entries,sizeof(char*),namecmp);
234 for (user=0; user<32; ++user)
237 for (i=0; i<entries; ++i) if (dirent[i][0]!='.')
239 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
243 if (any) putchar('\n');
244 printf("%d:\n",user);
250 cpmNamei(ino,dirent[i],&file);
251 cpmStat(&file,&statbuf);
253 if (iflag) printf("%4ld ",(long) statbuf.ino);
258 putchar(S_ISDIR(statbuf.mode) ? 'd' : '-');
259 putchar(statbuf.mode&0400 ? 'r' : '-');
260 putchar(statbuf.mode&0200 ? 'w' : '-');
261 putchar(statbuf.mode&0100 ? 'x' : '-');
262 putchar(statbuf.mode&0040 ? 'r' : '-');
263 putchar(statbuf.mode&0020 ? 'w' : '-');
264 putchar(statbuf.mode&0010 ? 'x' : '-');
265 putchar(statbuf.mode&0004 ? 'r' : '-');
266 putchar(statbuf.mode&0002 ? 'w' : '-');
267 putchar(statbuf.mode&0001 ? 'x' : '-');
269 putchar(statbuf.flags&FLAG_PUBLIC ? 'p' : '-');
270 putchar(dir[i].flags&FLAG_SYSTEM ? 's' : '-');
271 printf(" %-2d ",dir[i].user);
273 printf("%8.1ld ",(long)statbuf.size);
274 tmp=localtime(c ? &statbuf.ctime : &statbuf.mtime);
275 printf("%s %02d ",month[tmp->tm_mon],tmp->tm_mday);
276 if ((c ? statbuf.ctime : statbuf.mtime)<(now-182*24*3600)) printf("%04d ",tmp->tm_year+1900);
277 else printf("%02d:%02d ",tmp->tm_hour,tmp->tm_min);
279 printf("%s\n",dirent[i]+2);
285 /* lsattr -- output something like e2fs lsattr */ /*{{{*/
286 static void lsattr(char **dirent, int entries, struct cpmInode *ino)
288 int i,user,announce,any;
289 struct cpmStat statbuf;
290 struct cpmInode file;
293 qsort(dirent,entries,sizeof(char*),namecmp);
296 for (user=0; user<32; ++user)
299 for (i=0; i<entries; ++i) if (dirent[i][0]!='.')
301 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
305 if (any) putchar('\n');
306 printf("%d:\n",user);
311 cpmNamei(ino,dirent[i],&file);
312 cpmStat(&file,&statbuf);
313 cpmAttrGet(&file, &attrib);
315 putchar ((attrib & CPM_ATTR_F1) ? '1' : '-');
316 putchar ((attrib & CPM_ATTR_F2) ? '2' : '-');
317 putchar ((attrib & CPM_ATTR_F3) ? '3' : '-');
318 putchar ((attrib & CPM_ATTR_F4) ? '4' : '-');
319 putchar ((attrib & CPM_ATTR_SYS) ? 's' : '-');
320 putchar ((attrib & CPM_ATTR_ARCV) ? 'a' : '-');
321 putchar ((attrib & CPM_ATTR_PWREAD) ? 'r' : '-');
322 putchar ((attrib & CPM_ATTR_PWWRITE) ? 'w' : '-');
323 putchar ((attrib & CPM_ATTR_PWDEL) ? 'e' : '-');
325 printf(" %s\n",dirent[i]+2);
332 const char cmd[]="cpmls";
334 int main(int argc, char *argv[])
336 /* variables */ /*{{{*/
340 const char *devopts=NULL;
342 struct cpmSuperBlock drive;
343 struct cpmInode root;
349 static char starlit[2]="*";
350 static char * const star[]={starlit};
353 /* parse options */ /*{{{*/
354 if (!(format=getenv("CPMTOOLSFMT"))) format=FORMAT;
355 while ((c=getopt(argc,argv,"cT:f:ih?dDFlA"))!=EOF) switch(c)
357 case 'f': format=optarg; break;
358 case 'T': devopts=optarg; break;
360 case '?': usage=1; break;
361 case 'd': style=1; break;
362 case 'D': style=2; break;
363 case 'F': style=3; break;
364 case 'l': style=4; break;
365 case 'A': style=5; break;
366 case 'c': changetime=1; break;
367 case 'i': inode=1; break;
370 if (optind==argc) usage=1;
371 else image=argv[optind++];
375 fprintf(stderr,"Usage: %s [-f format] [-T libdsk-type] [-d|-D|-F|-A|[-l][-c][-i]] image [file ...]\n",cmd);
379 /* open image */ /*{{{*/
380 if ((err=Device_open(&drive.dev,image,O_RDONLY,devopts)))
382 fprintf(stderr,"%s: can not open %s (%s)\n",cmd,image,err);
385 cpmReadSuper(&drive,&root,format);
387 if (optind<argc) cpmglob(optind,argc,argv,&root,&gargc,&gargv);
388 else cpmglob(0,1,star,&root,&gargc,&gargv);
389 if (style==1) olddir(gargv,gargc);
390 else if (style==2) oldddir(gargv,gargc,&root);
391 else if (style==3) old3dir(gargv,gargc,&root);
392 else if (style==5) lsattr(gargv, gargc, &root);
393 else ls(gargv,gargc,&root,style==4,changetime,inode);