File size: 5,406 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

U0 HomeSet(U8 *dirname)
{//Change home directory.
  dirname=DirNameAbs(dirname);
  Free(blkdev.home_dir);
  blkdev.home_dir=AStrNew(dirname);
  Free(dirname);
}

Bool Cd(U8 *dirname=NULL,Bool make_dirs=FALSE)
{//Change directory. Optionally, make directories, too.
  I64 maxlen,cur_dir_clus=0;
  U8 *chg_to_buf,*new_cur_dir,*buf;
  CDrv *dv;
  Bool res=TRUE;
  if (!dirname)
    dirname="~";
  else if (!*dirname)
    return TRUE;
  if (dirname[1]==':') {
    if (*dirname==':') {
      if (Fs->cur_dv!=Let2Drv(':') && !Drv(*dirname))
        return FALSE;
    } else if (Fs->cur_dv!=Let2Drv(*dirname) && !Drv(*dirname))
      return FALSE;
    dirname+=2;
  }
  if (*dirname=='/' || !*dirname || !Fs->cur_dir) {
    Free(Fs->cur_dir);
    Fs->cur_dir=StrNew("/");
    if (*dirname=='/')
      dirname++;
  }
  chg_to_buf=MStrUtil(dirname,
        SUF_REM_LEADING|SUF_REM_TRAILING|SUF_REM_CTRL_CHARS);
  maxlen=StrLen(Fs->cur_dir)+1+StrLen(chg_to_buf)+1;
  new_cur_dir=MAlloc(maxlen);
  buf=MAlloc(maxlen);
  StrCpy(new_cur_dir,Fs->cur_dir);
  while (*chg_to_buf && res) {
    StrFirstRem(chg_to_buf,"/",buf);
    if (!*buf)
      StrCpy(new_cur_dir,"/");
    else if (!StrCmp(buf,"..")) {
      StrLastRem(new_cur_dir,"/");
      if (!*new_cur_dir)
        StrCpy(new_cur_dir,"/");
    } else if (!StrCmp(buf,"~")) {
      Free(new_cur_dir);
      new_cur_dir=MAlloc(StrLen(blkdev.home_dir+2)+1+StrLen(chg_to_buf)+1);
      StrCpy(new_cur_dir,blkdev.home_dir+2);
      if (Fs->cur_dv!=Let2Drv('~') && !Drv('~'))
        return FALSE;
    } else if (StrCmp(buf,".") && *buf) {
      dv=Fs->cur_dv;
      cur_dir_clus=Name2DirClus(dv,new_cur_dir);
      switch (dv->fs_type) {
        case FSt_REDSEA:
          res=RedSeaCd(buf,cur_dir_clus);
          break;
        case FSt_FAT32:
          res=FAT32Cd(buf,cur_dir_clus);
          break;
        case FSt_ISO9660:
          res=ISO1Cd(buf,cur_dir_clus);
          break;
        default:
          PrintErr("File System Not Supported\n");
          res=FALSE;
      }
      if (!res && make_dirs) {
        Free(Fs->cur_dir);
        Fs->cur_dir=StrNew(new_cur_dir);
        res=DirMk(buf);
      }
      if (res) {
        if (StrCmp(new_cur_dir,"/"))
          CatPrint(new_cur_dir,"/");
        CatPrint(new_cur_dir,buf);
      }
    }
  }
  Free(Fs->cur_dir);
  Fs->cur_dir=StrNew(new_cur_dir);
  Free(buf);
  Free(chg_to_buf);
  Free(new_cur_dir);
  return res;
}

Bool IsDir(U8 *dir_name)
{//Is a str a valid, existing Dir?
  U8 *mask=MStrPrint("%s/*",dir_name);
  Bool res,old_silent=Silent;
  CDirContext *dirc;
  if (dirc=DirContextNew(mask)) {
    DirContextDel(dirc);
    res=TRUE;
  } else
    res=FALSE;
  Free(mask);
  Silent(old_silent);
  return res;
}

I64 Dir(U8 *files_find_mask,Bool full)
{//List directory.
  CDirEntry *tmpde1=NULL,*tmpde2;
  U8 *st;
  CDateStruct ds;
  I64 csize=0xFFFF,c=0xFFFF,res=0;
  tmpde1=FilesFind(files_find_mask);
  if (!(st=DirCur))
    PrintErr("Invalid Drive\n");
  else {
    if (tmpde1) {
      Free(st);
      st=MAllocIdent(tmpde1->full_name);
      StrLastRem(st,"/");
      if (!st[2])
        StrCpy(st+2,"/");
//Find max columns
      tmpde2=tmpde1;
      while (tmpde2) {
        if (tmpde2->size>csize)
          csize=tmpde2->size;
        if (tmpde2->clus>c)
          c=tmpde2->clus;
        tmpde2=tmpde2->next;
      }
      csize=Bsr(csize)/4+1;
      c=Bsr(c)/4+1;

      "$MA,T=\"Directory\",LM=\"PopUpCd;Dir;\n\"$ of %s\n",st;
      if (full)
        "__DATE__ __TIME__ %*ts %*ts\n",
              csize,"SIZE",c,"CLUS";
      else
        "DATE_ TIME_ %*ts\n",csize,"SIZE";
      while (tmpde1) {
        tmpde2=tmpde1->next;
        res++;
        if (full)
          "%D %T %0*tX %0*tX ",tmpde1->datetime,tmpde1->datetime,
                csize,tmpde1->size,c,tmpde1->clus;
        else {
          Date2Struct(&ds,tmpde1->datetime+local_time_offset);
          "%02d/%02d %02d:%02d %0*tX ",ds.mon,ds.day_of_mon,ds.hour,ds.min,
                csize,tmpde1->size;
        }
        if (tmpde1->attr & RS_ATTR_DIR)
          PutDirLink(tmpde1->name,tmpde1->full_name);
        else
          PutFileLink(tmpde1->name,tmpde1->full_name);
        '\n';
        DirEntryDel(tmpde1);
        tmpde1=tmpde2;
      }
    } else
      "No matching entries\n";
    Free(st);
  }
  return res;
}

Bool DirMk(U8 *filename,I64 entry_cnt=0)
{//Make directory. Cd() can also make directories.
//entry_cnt is for preallocating dir blks, leave it zero if you like.
  U8 *name;
  CDirContext *dirc;
  Bool res=FALSE;
  if (FileFind(filename,,FUF_JUST_DIRS))
    return FALSE;
  if (dirc=DirContextNew(filename)) {
    if (*dirc->mask) {
      if (!FileNameChk(dirc->mask))
        PrintErr("Invalid FileName: \"%s\".\n",dirc->mask);
      else {
        "Make Directory:%s\n",filename;
        name=MStrUtil(dirc->mask,
              SUF_REM_LEADING|SUF_REM_TRAILING|SUF_REM_CTRL_CHARS);
        switch (dirc->dv->fs_type) {
          case FSt_REDSEA:
            res=RedSeaMkDir(dirc->dv,Fs->cur_dir,name,entry_cnt);
            break;
          case FSt_FAT32:
            res=FAT32MkDir(dirc->dv,Fs->cur_dir,name,entry_cnt);
            break;
          case FSt_ISO9660:
            PrintErr("Not Writable\n");
            break;
          default:
            PrintErr("File System Not Supported\n");
        }
        Free(name);
      }
    }
    DirContextDel(dirc);
  }
  return res;
}