1 /* #includes */ /*{{{C}}}*//*{{{*/
19 /* variables */ /*{{{*/
20 static const char * const month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
23 /* namecmp -- compare two entries */ /*{{{*/
24 static int namecmp(const void *a, const void *b)
26 if (**((const char * const *)a)=='[') return -1;
27 return strcmp(*((const char * const *)a),*((const char * const *)b));
30 /* olddir -- old style output */ /*{{{*/
31 static void olddir(char **dirent, int entries)
33 int i,j,k,l,user,announce;
36 for (user=0; user<32; ++user)
38 for (i=l=0; i<entries; ++i)
40 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
44 printf("User %d\n",user);
47 if (l%4) printf(" : ");
48 for (j=2; dirent[i][j] && dirent[i][j]!='.'; ++j) putchar(toupper(dirent[i][j]));
49 k=j; while (k<11) { putchar(' '); ++k; }
50 if (dirent[i][j]=='.') ++j;
51 for (k=0; dirent[i][j]; ++j,++k) putchar(toupper(dirent[i][j]));
52 for (; k<3; ++k) putchar(' ');
64 if (announce==2) announce=1;
66 if (entries==0) printf("No files\n");
69 /* oldddir -- old style long output */ /*{{{*/
70 static void oldddir(char **dirent, int entries, struct cpmInode *ino)
73 struct cpmStat statbuf;
78 int i,j,k,l,announce,user;
80 qsort(dirent,entries,sizeof(char*),namecmp);
82 printf(" Name Bytes Recs Attr update create\n");
83 printf("------------ ------ ------ ---- ----------------- -----------------\n");
85 for (l=user=0; user<32; ++user)
87 for (i=0; i<entries; ++i)
91 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
95 printf("\nUser %d:\n\n",user);
96 printf(" Name Bytes Recs Attr update create\n");
97 printf("------------ ------ ------ ---- ----------------- -----------------\n");
100 for (j=2; dirent[i][j] && dirent[i][j]!='.'; ++j) putchar(toupper(dirent[i][j]));
101 k=j; while (k<10) { putchar(' '); ++k; }
103 if (dirent[i][j]=='.') ++j;
104 for (k=0; dirent[i][j]; ++j,++k) putchar(toupper(dirent[i][j]));
105 for (; k<3; ++k) putchar(' ');
107 cpmNamei(ino,dirent[i],&file);
108 cpmStat(&file,&statbuf);
109 printf(" %5.1ldK",(long) (statbuf.size+buf.f_bsize-1) /
110 buf.f_bsize*(buf.f_bsize/1024));
112 printf(" %6.1ld ",(long)(statbuf.size/128));
113 putchar(statbuf.mode&0200 ? ' ' : 'R');
114 putchar(statbuf.mode&01000 ? 'S' : ' ');
118 tmp=localtime(&statbuf.mtime);
119 printf(" %02d-%s-%04d %02d:%02d",tmp->tm_mday,month[tmp->tm_mon],tmp->tm_year+1900,tmp->tm_hour,tmp->tm_min);
121 else if (statbuf.ctime) printf(" ");
124 tmp=localtime(&statbuf.ctime);
125 printf(" %02d-%s-%04d %02d:%02d",tmp->tm_mday,month[tmp->tm_mon],tmp->tm_year+1900,tmp->tm_hour,tmp->tm_min);
131 if (announce==2) announce=1;
133 printf("%5.1d Files occupying %6.1ldK",l,(buf.f_bused*buf.f_bsize)/1024);
134 printf(", %7.1ldK Free.\n",(buf.f_bfree*buf.f_bsize)/1024);
136 else printf("No files found\n");
139 /* old3dir -- old CP/M Plus style long output */ /*{{{*/
140 static void old3dir(char **dirent, int entries, struct cpmInode *ino)
142 struct cpmStatFS buf;
143 struct cpmStat statbuf;
144 struct cpmInode file;
148 int i,j,k,l,announce,user, attrib;
149 int totalBytes=0,totalRecs=0;
151 qsort(dirent,entries,sizeof(char*),namecmp);
154 for (l=0,user=0; user<32; ++user)
156 for (i=0; i<entries; ++i)
160 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
162 cpmNamei(ino,dirent[i],&file);
163 cpmStat(&file,&statbuf);
164 cpmAttrGet(&file, &attrib);
167 if (user) putchar('\n');
168 printf("Directory For Drive A: User %2.1d\n\n",user);
169 printf(" Name Bytes Recs Attributes Prot Update Create\n");
170 printf("------------ ------ ------ ------------ ------ -------------- --------------\n\n");
173 for (j=2; dirent[i][j] && dirent[i][j]!='.'; ++j) putchar(toupper(dirent[i][j]));
174 k=j; while (k<10) { putchar(' '); ++k; }
176 if (dirent[i][j]=='.') ++j;
177 for (k=0; dirent[i][j]; ++j,++k) putchar(toupper(dirent[i][j]));
178 for (; k<3; ++k) putchar(' ');
180 totalBytes+=statbuf.size;
181 totalRecs+=(statbuf.size+127)/128;
182 printf(" %5.1ldk",(long) (statbuf.size+buf.f_bsize-1) /
183 buf.f_bsize*(buf.f_bsize/1024));
184 printf(" %6.1ld ",(long)(statbuf.size/128));
185 putchar((attrib & CPM_ATTR_F1) ? '1' : ' ');
186 putchar((attrib & CPM_ATTR_F2) ? '2' : ' ');
187 putchar((attrib & CPM_ATTR_F3) ? '3' : ' ');
188 putchar((attrib & CPM_ATTR_F4) ? '4' : ' ');
189 putchar((statbuf.mode&(S_IWUSR|S_IWGRP|S_IWOTH)) ? ' ' : 'R');
190 putchar((attrib & CPM_ATTR_SYS) ? 'S' : ' ');
191 putchar((attrib & CPM_ATTR_ARCV) ? 'A' : ' ');
193 if (attrib & CPM_ATTR_PWREAD) printf("Read ");
194 else if (attrib & CPM_ATTR_PWWRITE) printf("Write ");
195 else if (attrib & CPM_ATTR_PWDEL) printf("Delete ");
196 else printf("None ");
199 tmp=localtime(&statbuf.mtime);
200 printf("%02d/%02d/%02d %02d:%02d ",tmp->tm_mon+1,tmp->tm_mday,tmp->tm_year%100,tmp->tm_hour,tmp->tm_min);
202 else if (statbuf.ctime) printf(" ");
205 tmp=localtime(&statbuf.ctime);
206 printf("%02d/%02d/%02d %02d:%02d",tmp->tm_mon+1,tmp->tm_mday,tmp->tm_year%100,tmp->tm_hour,tmp->tm_min);
212 if (announce==2) announce=1;
214 printf("\nTotal Bytes = %6.1dk ",(totalBytes+1023)/1024);
215 printf("Total Records = %7.1d ",totalRecs);
216 printf("Files Found = %4.1d\n",l);
217 printf("Total 1k Blocks = %6.1ld ",(buf.f_bused*buf.f_bsize)/1024);
218 printf("Used/Max Dir Entries For Drive A: %4.1ld/%4.1ld\n",buf.f_files-buf.f_ffree,buf.f_files);
220 else printf("No files found\n");
223 /* ls -- UNIX style output */ /*{{{*/
224 static void ls(char **dirent, int entries, struct cpmInode *ino, int l, int c, int iflag)
226 int i,user,announce,any;
228 struct cpmStat statbuf;
229 struct cpmInode file;
232 qsort(dirent,entries,sizeof(char*),namecmp);
235 for (user=0; user<32; ++user)
238 for (i=0; i<entries; ++i) if (dirent[i][0]!='.')
240 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
244 if (any) putchar('\n');
245 printf("%d:\n",user);
251 cpmNamei(ino,dirent[i],&file);
252 cpmStat(&file,&statbuf);
254 if (iflag) printf("%4ld ",(long) statbuf.ino);
259 putchar(S_ISDIR(statbuf.mode) ? 'd' : '-');
260 putchar(statbuf.mode&0400 ? 'r' : '-');
261 putchar(statbuf.mode&0200 ? 'w' : '-');
262 putchar(statbuf.mode&0100 ? 'x' : '-');
263 putchar(statbuf.mode&0040 ? 'r' : '-');
264 putchar(statbuf.mode&0020 ? 'w' : '-');
265 putchar(statbuf.mode&0010 ? 'x' : '-');
266 putchar(statbuf.mode&0004 ? 'r' : '-');
267 putchar(statbuf.mode&0002 ? 'w' : '-');
268 putchar(statbuf.mode&0001 ? 'x' : '-');
270 putchar(statbuf.flags&FLAG_PUBLIC ? 'p' : '-');
271 putchar(dir[i].flags&FLAG_SYSTEM ? 's' : '-');
272 printf(" %-2d ",dir[i].user);
274 printf("%8.1ld ",(long)statbuf.size);
275 tmp=localtime(c ? &statbuf.ctime : &statbuf.mtime);
276 printf("%s %02d ",month[tmp->tm_mon],tmp->tm_mday);
277 if ((c ? statbuf.ctime : statbuf.mtime)<(now-182*24*3600)) printf("%04d ",tmp->tm_year+1900);
278 else printf("%02d:%02d ",tmp->tm_hour,tmp->tm_min);
280 printf("%s\n",dirent[i]+2);
286 /* lsattr -- output something like e2fs lsattr */ /*{{{*/
287 static void lsattr(char **dirent, int entries, struct cpmInode *ino)
289 int i,user,announce,any;
290 struct cpmStat statbuf;
291 struct cpmInode file;
294 qsort(dirent,entries,sizeof(char*),namecmp);
297 for (user=0; user<32; ++user)
300 for (i=0; i<entries; ++i) if (dirent[i][0]!='.')
302 if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%10)
306 if (any) putchar('\n');
307 printf("%d:\n",user);
312 cpmNamei(ino,dirent[i],&file);
313 cpmStat(&file,&statbuf);
314 cpmAttrGet(&file, &attrib);
316 putchar ((attrib & CPM_ATTR_F1) ? '1' : '-');
317 putchar ((attrib & CPM_ATTR_F2) ? '2' : '-');
318 putchar ((attrib & CPM_ATTR_F3) ? '3' : '-');
319 putchar ((attrib & CPM_ATTR_F4) ? '4' : '-');
320 putchar ((attrib & CPM_ATTR_SYS) ? 's' : '-');
321 putchar ((attrib & CPM_ATTR_ARCV) ? 'a' : '-');
322 putchar ((attrib & CPM_ATTR_PWREAD) ? 'r' : '-');
323 putchar ((attrib & CPM_ATTR_PWWRITE) ? 'w' : '-');
324 putchar ((attrib & CPM_ATTR_PWDEL) ? 'e' : '-');
326 printf(" %s\n",dirent[i]+2);
333 const char cmd[]="cpmls";
335 int main(int argc, char *argv[])
337 /* variables */ /*{{{*/
341 const char *devopts=NULL;
343 struct cpmSuperBlock drive;
344 struct cpmInode root;
350 static char starlit[2]="*";
351 static char * const star[]={starlit};
354 /* parse options */ /*{{{*/
355 if (!(format=getenv("CPMTOOLSFMT"))) format=FORMAT;
356 while ((c=getopt(argc,argv,"cT:f:ih?dDFlA"))!=EOF) switch(c)
358 case 'f': format=optarg; break;
359 case 'T': devopts=optarg; break;
361 case '?': usage=1; break;
362 case 'd': style=1; break;
363 case 'D': style=2; break;
364 case 'F': style=3; break;
365 case 'l': style=4; break;
366 case 'A': style=5; break;
367 case 'c': changetime=1; break;
368 case 'i': inode=1; break;
371 if (optind==argc) usage=1;
372 else image=argv[optind++];
376 fprintf(stderr,"Usage: %s [-f format] [-T libdsk-type] [-d|-D|-F|-A|[-l][-c][-i]] image [file ...]\n",cmd);
380 /* open image */ /*{{{*/
381 if ((err=Device_open(&drive.dev,image,O_RDONLY,devopts)))
383 fprintf(stderr,"%s: cannot open %s (%s)\n",cmd,image,err);
386 if (cpmReadSuper(&drive,&root,format)==-1)
388 fprintf(stderr,"%s: cannot read superblock (%s)\n",cmd,boo);
392 if (optind<argc) cpmglob(optind,argc,argv,&root,&gargc,&gargv);
393 else cpmglob(0,1,star,&root,&gargc,&gargv);
394 if (style==1) olddir(gargv,gargc);
395 else if (style==2) oldddir(gargv,gargc,&root);
396 else if (style==3) old3dir(gargv,gargc,&root);
397 else if (style==5) lsattr(gargv, gargc, &root);
398 else ls(gargv,gargc,&root,style==4,changetime,inode);