File: cpmls.c

package info (click to toggle)
cpmtools 2.5-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 904 kB
  • ctags: 383
  • sloc: ansic: 5,005; sh: 2,735; makefile: 138
file content (368 lines) | stat: -rw-r--r-- 11,796 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* #includes */ /*{{{C}}}*//*{{{*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "getopt.h"
#include "cpmfs.h"
/*}}}*/

/* variables */ /*{{{*/
static const char * const month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
/*}}}*/

/* namecmp -- compare two entries */ /*{{{*/
static int namecmp(const void *a, const void *b)
{
  if (**((const char * const *)a)=='[') return -1;
  return strcmp(*((const char * const *)a),*((const char * const *)b));
}
/*}}}*/
/* olddir  -- old style output */ /*{{{*/
static void olddir(char **dirent, int entries)
{
  int i,j,k,l,user,announce;

  announce=0;
  for (user=0; user<32; ++user)
  {
    for (i=l=0; i<entries; ++i)
    {
      if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%33)
      {
        if (announce==1)
        {
          printf("User %d\n",user);
        }
        announce=2;
        if (l%4) printf(" : ");
        for (j=2; dirent[i][j] && dirent[i][j]!='.'; ++j) putchar(toupper(dirent[i][j]));
        k=j; while (k<11) { putchar(' '); ++k; }
        if (dirent[i][j]=='.') ++j;
        for (k=0; dirent[i][j]; ++j,++k) putchar(toupper(dirent[i][j]));
        for (; k<3; ++k) putchar(' ');
        ++l;
      }
      if (l && (l%4)==0) putchar('\n');
    }
    if (l%4) putchar('\n');
    if (announce==2) announce=1;
  }
  if (entries==0) printf("No files\n");
}
/*}}}*/
/* oldddir -- old style long output */ /*{{{*/
static void oldddir(char **dirent, int entries, struct cpmInode *ino)
{
  struct cpmStatFS buf;
  struct cpmStat statbuf;
  struct cpmInode file;

  if (entries)
  {
    int i,j,k,l,announce,user;

    qsort(dirent,entries,sizeof(char*),namecmp);
    cpmStatFS(ino,&buf);
    printf("     Name    Bytes   Recs  Attr     update             create\n");
    printf("------------ ------ ------ ---- -----------------  -----------------\n");
    announce=0;
    for (l=user=0; user<32; ++user)
    {
      for (i=0; i<entries; ++i)
      {
        struct tm *tmp;

        if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%33)
        {
          if (announce==1)
          {
            printf("\nUser %d:\n\n",user);
            printf("     Name    Bytes   Recs  Attr     update             create\n");
            printf("------------ ------ ------ ---- -----------------  -----------------\n");
          }
          announce=2;
          for (j=2; dirent[i][j] && dirent[i][j]!='.'; ++j) putchar(toupper(dirent[i][j]));
          k=j; while (k<10) { putchar(' '); ++k; }
          putchar('.');
          if (dirent[i][j]=='.') ++j;
          for (k=0; dirent[i][j]; ++j,++k) putchar(toupper(dirent[i][j]));
          for (; k<3; ++k) putchar(' ');

          cpmNamei(ino,dirent[i],&file);
          cpmStat(&file,&statbuf);
          printf(" %5.1ldK",(statbuf.size+buf.f_bsize-1)/buf.f_bsize*(buf.f_bsize/1024));
          printf(" %6.1ld ",(long)(statbuf.size/128));
          putchar(statbuf.mode&0200 ? ' ' : 'R');
          putchar(statbuf.mode&01000 ? 'S' : ' ');
          putchar(' ');
          if (statbuf.mtime)
          {
            tmp=gmtime(&statbuf.mtime);
            printf("  %02d-%s-%04d %02d:%02d",tmp->tm_mday,month[tmp->tm_mon],tmp->tm_year+1900,tmp->tm_hour,tmp->tm_min);
            tmp=gmtime(&statbuf.ctime);
            printf("  %02d-%s-%04d %02d:%02d",tmp->tm_mday,month[tmp->tm_mon],tmp->tm_year+1900,tmp->tm_hour,tmp->tm_min);
          }
          putchar('\n');
          ++l;
        }
      }
      if (announce==2) announce=1;
    }
    printf("%5.1d Files occupying %6.1ldK",l,(buf.f_bused*buf.f_bsize)/1024);
    printf(", %7.1ldK Free.\n",(buf.f_bfree*buf.f_bsize)/1024);
  }
  else printf("No files found\n");
}
/*}}}*/
/* old3dir -- old CP/M Plus style long output */ /*{{{*/
static void old3dir(char **dirent, int entries, struct cpmInode *ino)
{
  struct cpmStatFS buf;
  struct cpmStat statbuf;
  struct cpmInode file;

  if (entries)
  {
    int i,j,k,l,announce,user, attrib;
    int totalBytes=0,totalRecs=0;

    qsort(dirent,entries,sizeof(char*),namecmp);
    cpmStatFS(ino,&buf);
    announce=1;
    for (l=0,user=0; user<32; ++user)
    {
      for (i=0; i<entries; ++i)
      {
        struct tm *tmp;

        if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%33)
        {
          cpmNamei(ino,dirent[i],&file);
          cpmStat(&file,&statbuf);
	  cpmAttrGet(&file, &attrib);
          if (announce==1)
          {
            if (user) putchar('\n');
            printf("Directory For Drive A:  User %2.1d\n\n",user);
            printf("    Name     Bytes   Recs   Attributes   Prot      Update          Create\n");
            printf("------------ ------ ------ ------------ ------ --------------  --------------\n\n");
          }
          announce=2;
          for (j=2; dirent[i][j] && dirent[i][j]!='.'; ++j) putchar(toupper(dirent[i][j]));
          k=j; while (k<10) { putchar(' '); ++k; }
          putchar(' ');
          if (dirent[i][j]=='.') ++j;
          for (k=0; dirent[i][j]; ++j,++k) putchar(toupper(dirent[i][j]));
          for (; k<3; ++k) putchar(' ');

          totalBytes+=statbuf.size;
          totalRecs+=(statbuf.size+127)/128;
          printf(" %5.1ldk",(statbuf.size+buf.f_bsize-1)/buf.f_bsize*(buf.f_bsize/1024));
          printf(" %6.1ld ",(long)(statbuf.size/128));
          putchar((attrib & CPM_ATTR_F1)   ? '1' : ' ');
          putchar((attrib & CPM_ATTR_F2)   ? '2' : ' ');
          putchar((attrib & CPM_ATTR_F3)   ? '3' : ' ');          
          putchar((attrib & CPM_ATTR_F4)   ? '4' : ' ');
          putchar((statbuf.mode&(S_IWUSR|S_IWGRP|S_IWOTH)) ? ' ' : 'R');
          putchar((attrib & CPM_ATTR_SYS)  ? 'S' : ' ');
          putchar((attrib & CPM_ATTR_ARCV) ? 'A' : ' ');
          printf("      ");
          if      (attrib & CPM_ATTR_PWREAD)  printf("Read   ");
          else if (attrib & CPM_ATTR_PWWRITE) printf("Write  ");
          else if (attrib & CPM_ATTR_PWDEL)   printf("Delete "); 
          else printf("None   ");
          tmp=gmtime(&statbuf.mtime);
          printf("%02d/%02d/%02d %02d:%02d  ",tmp->tm_mon+1,tmp->tm_mday,tmp->tm_year%100,tmp->tm_hour,tmp->tm_min);
          tmp=gmtime(&statbuf.ctime);
          printf("%02d/%02d/%02d %02d:%02d",tmp->tm_mon+1,tmp->tm_mday,tmp->tm_year%100,tmp->tm_hour,tmp->tm_min);
          putchar('\n');
          ++l;
        }
      }
      if (announce==2) announce=1;
    }
    printf("\nTotal Bytes     = %6.1dk  ",(totalBytes+1023)/1024);
    printf("Total Records = %7.1d  ",totalRecs);
    printf("Files Found = %4.1d\n",l);
    printf("Total 1k Blocks = %6.1ld   ",(buf.f_bused*buf.f_bsize)/1024);
    printf("Used/Max Dir Entries For Drive A: %4.1ld/%4.1ld\n",buf.f_files-buf.f_ffree,buf.f_files);
  }
  else printf("No files found\n");
}
/*}}}*/
/* ls      -- UNIX style output */ /*{{{*/
static void ls(char **dirent, int entries, struct cpmInode *ino, int l, int c, int iflag)
{
  int i,user,announce,any;
  time_t now;
  struct cpmStat statbuf;
  struct cpmInode file;

  time(&now);
  qsort(dirent,entries,sizeof(char*),namecmp);
  announce=0;
  any=0;
  for (user=0; user<32; ++user)
  {
    announce=0;
    for (i=0; i<entries; ++i) if (dirent[i][0]!='.')
    {
      if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%33)
      {
        if (announce==0)
        {
          if (any) putchar('\n');
          printf("%d:\n",user);
          announce=1;
        }
        any=1;
        if (iflag || l)
        {
          cpmNamei(ino,dirent[i],&file);
          cpmStat(&file,&statbuf);
        }
        if (iflag) printf("%4ld ",statbuf.ino);
        if (l)
        {
          struct tm *tmp;

          putchar(S_ISDIR(statbuf.mode) ? 'd' : '-');
          putchar(statbuf.mode&0400 ? 'r' : '-');
          putchar(statbuf.mode&0200 ? 'w' : '-');
          putchar(statbuf.mode&0100 ? 'x' : '-');
          putchar(statbuf.mode&0040 ? 'r' : '-');
          putchar(statbuf.mode&0020 ? 'w' : '-');
          putchar(statbuf.mode&0010 ? 'x' : '-');
          putchar(statbuf.mode&0004 ? 'r' : '-');
          putchar(statbuf.mode&0002 ? 'w' : '-');
          putchar(statbuf.mode&0001 ? 'x' : '-');
#if 0
          putchar(statbuf.flags&FLAG_PUBLIC ? 'p' : '-');
          putchar(dir[i].flags&FLAG_SYSTEM ? 's' : '-');
          printf(" %-2d ",dir[i].user);
#endif
          printf("%8.1ld ",(long)statbuf.size);
          tmp=gmtime(c ? &statbuf.ctime : &statbuf.mtime);
          printf("%s %02d ",month[tmp->tm_mon],tmp->tm_mday);
          if ((c ? statbuf.ctime : statbuf.mtime)<(now-182*24*3600)) printf("%04d  ",tmp->tm_year+1900);
          else printf("%02d:%02d ",tmp->tm_hour,tmp->tm_min);
        }
        printf("%s\n",dirent[i]+2);
      }
    }
  }
}
/*}}}*/
/* lsattr  -- output something like e2fs lsattr */ /*{{{*/
static void lsattr(char **dirent, int entries, struct cpmInode *ino)
{
  int i,user,announce,any;
  struct cpmStat statbuf;
  struct cpmInode file;
  cpm_attr_t attrib;

  qsort(dirent,entries,sizeof(char*),namecmp);
  announce=0;
  any=0;
  for (user=0; user<32; ++user)
  {
    announce=0;
    for (i=0; i<entries; ++i) if (dirent[i][0]!='.')
    {
      if (dirent[i][0]=='0'+user/10 && dirent[i][1]=='0'+user%33)
      {
        if (announce==0)
        {
          if (any) putchar('\n');
          printf("%d:\n",user);
          announce=1;
        }
        any=1;

        cpmNamei(ino,dirent[i],&file);
        cpmStat(&file,&statbuf);
        cpmAttrGet(&file, &attrib); 

        putchar ((attrib & CPM_ATTR_F1)      ? '1' : '-');
        putchar ((attrib & CPM_ATTR_F2)      ? '2' : '-');
        putchar ((attrib & CPM_ATTR_F3)      ? '3' : '-');
        putchar ((attrib & CPM_ATTR_F4)      ? '4' : '-');
        putchar ((attrib & CPM_ATTR_SYS)     ? 's' : '-');
        putchar ((attrib & CPM_ATTR_ARCV)    ? 'a' : '-');
        putchar ((attrib & CPM_ATTR_PWREAD)  ? 'r' : '-');
        putchar ((attrib & CPM_ATTR_PWWRITE) ? 'w' : '-');
        putchar ((attrib & CPM_ATTR_PWDEL)   ? 'e' : '-');

        printf(" %s\n",dirent[i]+2);
      }
    }
  }
}
/*}}}*/

const char cmd[]="cpmls";

int main(int argc, char *argv[])
{
  /* variables */ /*{{{*/
  const char *err;
  const char *image;
  const char *format=FORMAT;
  const char *devopts=NULL;
  int c,usage=0;
  struct cpmSuperBlock drive;
  struct cpmInode root;
  int style=0;
  int changetime=0;
  int inode=0;
  char **gargv;
  int gargc;
  static char starlit[2]="*";
  static char * const star[]={starlit};
  /*}}}*/

  /* parse options */ /*{{{*/
  while ((c=getopt(argc,argv,"cT:f:ih?dDFlA"))!=EOF) switch(c)
  {
    case 'f': format=optarg; break;
    case 'T': devopts=optarg; break;
    case 'h':
    case '?': usage=1; break;
    case 'd': style=1; break;
    case 'D': style=2; break;
    case 'F': style=3; break;
    case 'l': style=4; break;
    case 'A': style=5; break;
    case 'c': changetime=1; break;
    case 'i': inode=1; break;
  }

  if (optind==argc) usage=1;
  else image=argv[optind++];

  if (usage)
  {
    fprintf(stderr,"Usage: %s [-f format] [-T libdsk-type] [-d|-D|-F|-A|[-l][-c][-i]] image [file ...]\n",cmd);
    exit(1);
  }
  /*}}}*/
  /* open image */ /*{{{*/
  if ((err=Device_open(&drive.dev,image,O_RDONLY,devopts))) 
  {
    fprintf(stderr,"%s: can not open %s (%s)\n",cmd,image,err);
    exit(1);
  }
  cpmReadSuper(&drive,&root,format);
  /*}}}*/
  if (optind<argc) cpmglob(optind,argc,argv,&root,&gargc,&gargv);
  else cpmglob(0,1,star,&root,&gargc,&gargv);
  if (style==1) olddir(gargv,gargc);
  else if (style==2) oldddir(gargv,gargc,&root);
  else if (style==3) old3dir(gargv,gargc,&root);
  else if (style==5) lsattr(gargv, gargc, &root); 
  else ls(gargv,gargc,&root,style==4,changetime,inode);
  exit(0);
}