Example #1
0
static FS_i32 _FS_fat_find_file(int Idx, FS_u32 Unit, const char *pFileName,
                                    FS__fat_dentry_type *pDirEntry,
                                    FS_u32 DirStart, FS_u32 DirSize) {
  FS__fat_dentry_type *s;
  FS_u32 i;
  FS_u32 dsec;
  int len;
  int err; 
  int c;
  char *buffer;

  buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
  if (!buffer) {
    return -1;
  }
  len = FS__CLIB_strlen(pFileName);
  if (len > 11) {
    len = 11;
  }
  /* Read directory */
  for (i = 0; i < DirSize; i++) {
    dsec = FS__fat_dir_realsec(Idx, Unit, DirStart, i);
    if (dsec == 0) {
      FS__fat_free(buffer);
      return -1;
    }
    err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
    if (err < 0) {
      FS__fat_free(buffer);
      return -1;
    }
    s = (FS__fat_dentry_type*)buffer;
    while (1) {
      if (s >= (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
        break;  /* End of sector reached */
      }
      c = FS__CLIB_strncmp((char*)s->data, pFileName, len);
      if (c == 0) {  /* Name does match */
        if (s->data[11] & FS_FAT_ATTR_ARCHIVE) {
          break;  /* Entry found */
        }
      }
      s++;
    }
    if (s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
      /* Entry found. Return number of 1st block of the file */
      if (pDirEntry) {
        FS__CLIB_memcpy(pDirEntry, s, sizeof(FS__fat_dentry_type));
      }
      FS__fat_free(buffer);
      dsec  = (FS_u32)s->data[26];
      dsec += (FS_u32)s->data[27] * 0x100UL;
      dsec += (FS_u32)s->data[20] * 0x10000UL;
      dsec += (FS_u32)s->data[21] * 0x1000000UL;
      return ((FS_i32)dsec);
    }
  }
  FS__fat_free(buffer);
  return -1;
}
Example #2
0
FS_DIR *FS__fat_opendir(const char *pDirName, FS_DIR *pDir) {
  FS_size_t len;
  FS_u32 unit;
  FS_u32 dstart;
  FS_u32 dsize;
  FS_i32 i;
  char realname[12];
  char *filename;

  if (!pDir) {
    return 0;  /* No valid pointer to a FS_DIR structure */
  }
  /* Find path on the media and return file name part of the complete path */
  dsize = FS__fat_findpath(pDir->dev_index, pDirName, &filename, &unit, &dstart); 
  if (dsize == 0) {
    return 0;  /* Directory not found */
  }
  FS__lb_ioctl(FS__pDevInfo[pDir->dev_index].devdriver, unit, FS_CMD_INC_BUSYCNT, 0, (void*)0); /* Turn on busy signal */
  len = FS__CLIB_strlen(filename);
  if (len != 0) {
    /* There is a name in the complete path (it does not end with a '\') */
    FS__fat_make_realname(realname, filename);  /* Convert name to FAT real name */
    i =  FS__fat_find_dir(pDir->dev_index, unit, realname, dstart, dsize);  /* Search name in the directory */
    if (i == 0) {
      /* Directory not found */
      FS__lb_ioctl(FS__pDevInfo[pDir->dev_index].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn off busy signal */
      return 0;
    }
  }
  else {
    /* 
       There is no name in the complete path (it does end with a '\'). In that
       case, FS__fat_findpath returns already start of the directory.
    */
    i = dstart;  /* Use 'current' path */
  }
  if (i) {
    dsize  =  FS__fat_dir_size(pDir->dev_index, unit, i);  /* Get size of the directory */
  }
  if (dsize == 0) {
    /* Directory not found */
    FS__lb_ioctl(FS__pDevInfo[pDir->dev_index].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn off busy signal */
    return 0;
  }
  pDir->dirid_lo  = unit;
  pDir->dirid_hi  = i;
  pDir->dirid_ex  = dstart;
  pDir->error     = 0;
  pDir->size      = dsize;
  pDir->dirpos    = 0;
  pDir->inuse     = 1;
  return pDir;
}
Example #3
0
int FS__find_fsl(const char *pFullName, FS_FARCHARPTR *pFileName) {
  int idx;
  int i;
  int j;
  int m;
  FS_FARCHARPTR s;

  /* Find correct FSL (device:unit:name) */
  s = (FS_FARCHARPTR)FS__CLIB_strchr(pFullName, ':');
  if (s) {
    /* Scan for device name */
    idx = 0;
    m = (int)((FS_u32)(s) - (FS_u32)(pFullName));
    while (1) {
      j = FS__CLIB_strlen(FS__pDevInfo[idx].devname);
      if (m > j) {
        j = m;
      }
      i = FS__CLIB_strncmp(FS__pDevInfo[idx].devname, pFullName, j);
      idx++;
      if (idx >= (int)FS__maxdev) {
        break;  /* End of device information table reached */
      }
      if (i == 0) {
        break;  /* Device found */
      }
    }
    if (i == 0) {
      idx--;  /* Correct index */
    }
    else {
      return -1;  /* Device not found */
    }
    s++;
  }
  else {
    /* use 1st FSL as default */
    idx = 0;
    s = (FS_FARCHARPTR) pFullName;
  }
  *pFileName = s;
  return idx;
}
Example #4
0
FS_i32 FS__fat_DeleteFileOrDir(int Idx, FS_u32 Unit,  const char *pName,
                                    FS_u32 DirStart, FS_u32 DirSize, char RmFile) {
  FS__fat_dentry_type *s;
  FS_u32 dsec;
  FS_u32 i;
  FS_u32 value;
  FS_u32 fatsize;
  FS_u32 filesize;
  FS_i32 len;
  FS_i32 bytespersec;
  FS_i32 fatindex;
  FS_i32 fatsec;
  FS_i32 fatoffs;
  FS_i32 lastsec;
  FS_i32 curclst;
  FS_i32 todo;
  char *buffer;
  int fattype;
  int err;
  int err2;
  int lexp;
  int x;
  unsigned char a;
  unsigned char b;
#if (FS_FAT_NOFAT32==0)
  unsigned char c;
  unsigned char d;
#endif /* FS_FAT_NOFAT32==0 */

  buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
  if (!buffer) {
    return 0;
  }
  fattype = FS__fat_which_type(Idx, Unit);
#if (FS_FAT_NOFAT32!=0)
  if (fattype == 2) {
    FS__fat_free(buffer);
    return -1;
  }
#endif  /* FS_FAT_NOFAT32!=0 */
  fatsize = FS__FAT_aBPBUnit[Idx][Unit].FATSz16;
  if (fatsize == 0) {
    fatsize = FS__FAT_aBPBUnit[Idx][Unit].FATSz32;
  }
  bytespersec = (FS_i32)FS__FAT_aBPBUnit[Idx][Unit].BytesPerSec;
  len = FS__CLIB_strlen(pName);
  if (len > 11) {
    len = 11;
  }
  /* Read directory */
  for (i = 0; i < DirSize; i++) {
    curclst = -1;
    dsec = FS__fat_dir_realsec(Idx, Unit, DirStart, i);
    if (dsec == 0) {
      FS__fat_free(buffer);
      return -1;
    }
    err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
    if (err < 0) {
      FS__fat_free(buffer);
      return -1;
    }
    /* Scan for pName in the directory sector */
    s = (FS__fat_dentry_type*) buffer;
    while (1) {
      if (s >= (FS__fat_dentry_type*)(buffer + bytespersec)) {
        break;  /* End of sector reached */
      }
      x = FS__CLIB_strncmp((char*)s->data, pName, len);
      if (x == 0) { /* Name does match */
        if (s->data[11] != 0) {
          break;  /* Entry found */
        }
      }
      s++;
    }
    if (s < (FS__fat_dentry_type*)(buffer + bytespersec)) {
      /* Entry has been found, delete directory entry */
      s->data[0]  = 0xe5;
      s->data[11] = 0;
      err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
      if (err < 0) {
        FS__fat_free(buffer);
        return -1;
      }
      /* Free blocks in FAT */
      /*
         For normal files, there are no more clusters freed than the entrie's filesize
         does indicate. That avoids corruption of the complete media in case there is
         no EOF mark found for the file (FAT is corrupt!!!). 
         If the function should remove a directory, filesize if always 0 and cannot
         be used for that purpose. To avoid running into endless loop, todo is set
         to 0x0ffffff8L, which is the maximum number of clusters for FAT32.
      */
      if (RmFile) {
        filesize  = s->data[28] + 0x100UL * s->data[29] + 0x10000UL * s->data[30] + 0x1000000UL * s->data[31];
        todo      = filesize / (FS__FAT_aBPBUnit[Idx][Unit].SecPerClus * bytespersec);
        value     = filesize % (FS__FAT_aBPBUnit[Idx][Unit].SecPerClus * bytespersec);
        if (value != 0) {
          todo++;
        }
      } 
      else {
        todo = (FS_i32)0x0ffffff8L;
      }
      curclst = s->data[26] + 0x100L * s->data[27] + 0x10000L * s->data[20] + 0x1000000L * s->data[21];
      lastsec = -1;
      /* Free cluster loop */
      while (todo) {
        if (fattype == 1) {
          fatindex = curclst + (curclst / 2);    /* FAT12 */
        }
#if (FS_FAT_NOFAT32==0)
        else if (fattype == 2) {
          fatindex = curclst * 4;               /* FAT32 */
        }
#endif  /* FS_FAT_NOFAT32==0 */
        else {
          fatindex = curclst * 2;               /* FAT16 */
        }
        fatsec = FS__FAT_aBPBUnit[Idx][Unit].RsvdSecCnt + (fatindex / bytespersec);
        fatoffs = fatindex % bytespersec;
        if (fatsec != lastsec) {
          err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, fatsec, (void*)buffer);
          if (err < 0) {
            err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, fatsize + fatsec, (void*)buffer);
            if (err < 0) {
              FS__fat_free(buffer);
              return -1;
            }
            /* Try to repair original FAT sector with contents of copy */
            FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, fatsec, (void*)buffer);
          }
          lastsec = fatsec;
        }
        if (fattype == 1) {
          if (fatoffs == (bytespersec - 1)) {
            a = buffer[fatoffs];
            if (curclst & 1) {
              buffer[fatoffs] &= 0x0f;
            }
            else {
              buffer[fatoffs]  = 0x00;
            }
            err  = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, fatsec, (void*)buffer);
            err2 = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, fatsize + fatsec, (void*)buffer);
            lexp = (err < 0);
            lexp = lexp || (err2 < 0);
            if (lexp) {
              FS__fat_free(buffer);
              return -1;
            }
            err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, fatsec + 1, (void*)buffer);
            if (err < 0) {
              err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, fatsize + fatsec + 1, (void*)buffer);
              if (err < 0) {
                FS__fat_free(buffer);
                return -1;
              }
              /* Try to repair original FAT sector with contents of copy */
              FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, fatsec + 1, (void*)buffer);
            }
            lastsec = fatsec + 1;
            b = buffer[0];
            if (curclst & 1) {
              buffer[0]  = 0x00;;
            }
            else {
              buffer[0] &= 0xf0;
            }
            err  = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, fatsec + 1, (void*)buffer);
            err2 = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, fatsize + fatsec + 1, (void*)buffer);
            lexp = (err < 0);
            lexp = lexp || (err2 < 0);
            if (lexp) {
              FS__fat_free(buffer);
              return -1;
            }
          }
          else {
            a = buffer[fatoffs];
            b = buffer[fatoffs + 1];
            if (curclst & 1) {
              buffer[fatoffs]     &= 0x0f;
              buffer[fatoffs + 1]  = 0x00;
            }
            else {
              buffer[fatoffs]      = 0x00;
              buffer[fatoffs + 1] &= 0xf0;
            }
            err  = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, fatsec, (void*)buffer);
            err2 = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, fatsize + fatsec, (void*)buffer);
            lexp = (err < 0);
            lexp = lexp || (err2 < 0);
            if (lexp) {
              FS__fat_free(buffer);
              return -1;
            }
          }
          if (curclst & 1) {
            curclst = ((a & 0xf0) >> 4) + 16 * b;
          }
          else {
            curclst = a + 256 * (b & 0x0f);
          }
          curclst &= 0x0fff;
          if (curclst >= 0x0ff8) {
            FS__fat_free(buffer);
            return 0;
          }
        }
#if (FS_FAT_NOFAT32==0)
        else if (fattype == 2) { /* FAT32 */
Example #5
0
static FS_i32 _FS_fat_create_file(int Idx, FS_u32 Unit,  const char *pFileName,
                                    FS_u32 DirStart, FS_u32 DirSize) {
  FS__fat_dentry_type *s;
  FS_u32 i;
  FS_u32 dsec;
  FS_i32 cluster;
  int len;
  int err;
  FS_u16 val;
  char *buffer;

  buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
  if (!buffer) {
    return -1;
  }
  len = FS__CLIB_strlen(pFileName);
  if (len > 11) {
    len = 11;
  }
  /* Read directory */
  for (i = 0; i < DirSize; i++) {
    dsec = FS__fat_dir_realsec(Idx, Unit, DirStart, i);
    if (dsec == 0) {
      FS__fat_free(buffer);
      return -1;
    }
    err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
    if (err < 0) {
      FS__fat_free(buffer);
      return -1;
    }
    s = (FS__fat_dentry_type*)buffer;
    while (1) {
      if (s >= (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
        break;  /* End of sector reached */
      }
      if (s->data[0] == 0x00) {
        break;  /* Empty entry found */
      }
      if (s->data[0] == (unsigned char)0xe5) {
        break;  /* Deleted entry found */
      }
      s++;
    }
    if (s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
      /* Free entry found. Make entry and return 1st block of the file */
      FS__CLIB_strncpy((char*)s->data, pFileName, len);
      s->data[11] = FS_FAT_ATTR_ARCHIVE;
      /* Alloc block in FAT */
      cluster = FS__fat_FAT_alloc(Idx, Unit, -1);
      if (cluster >= 0) {
        s->data[12]     = 0x00;                           /* Res */
        s->data[13]     = 0x00;                           /* CrtTimeTenth (optional, not supported) */
        s->data[14]     = 0x00;                           /* CrtTime (optional, not supported) */
        s->data[15]     = 0x00;
        s->data[16]     = 0x00;                           /* CrtDate (optional, not supported) */
        s->data[17]     = 0x00;
        s->data[18]     = 0x00;                           /* LstAccDate (optional, not supported) */
        s->data[19]     = 0x00;
        val             = FS_X_OS_GetTime();
        s->data[22]     = (unsigned char)(val & 0xff);   /* WrtTime */
        s->data[23]     = (unsigned char)(val / 256);
        val             = FS_X_OS_GetDate();
        s->data[24]     = (unsigned char)(val & 0xff);   /* WrtDate */
        s->data[25]     = (unsigned char)(val / 256);
        s->data[26]     = (unsigned char)(cluster & 0xff);    /* FstClusLo / FstClusHi */ 
        s->data[27]     = (unsigned char)((cluster / 256) & 0xff);
        s->data[20]     = (unsigned char)((cluster / 0x10000L) & 0xff);
        s->data[21]     = (unsigned char)((cluster / 0x1000000L) & 0xff);
        s->data[28]     = 0x00;                           /* FileSize */
        s->data[29]     = 0x00;
        s->data[30]     = 0x00;
        s->data[31]     = 0x00;
        err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
        if (err < 0) {
          FS__fat_free(buffer);
          return -1;
        }
      }
      FS__fat_free(buffer);
      return cluster;
    }
  }
  FS__fat_free(buffer);
  return -2;      /* Directory is full */
}
Example #6
0
static int _FS_fat_create_directory(int Idx, FS_u32 Unit, const char *pDirName,
                                    FS_u32 DirStart, FS_u32 DirSize) {
  char *buffer;
  FS__fat_dentry_type *s;
  FS_u32 dirindex;
  FS_u32 dsec;
  FS_i32 cluster;
  FS_u16 val_time;
  FS_u16 val_date;
  int err;
  int len;
  int j;

  buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
  if (!buffer) {
    return -1;
  }
  len = FS__CLIB_strlen(pDirName);
  if (len > 11) {
    len = 11;
  }
  /* Read directory */
  for (dirindex = 0; dirindex < DirSize; dirindex++) {
    dsec = FS__fat_dir_realsec(Idx, Unit, DirStart, dirindex);
    if (dsec == 0) {
      /* Translation of relativ directory sector to an absolute sector failed */
      FS__fat_free(buffer);
      return -1;
    }
    err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer); /* Read directory sector */
    if (err < 0) {
      /* Read error */
      FS__fat_free(buffer);
      return -1;
    }
    /* Scan the directory sector for a free or deleted entry */
    s = (FS__fat_dentry_type*)buffer;
    while (1) {
      if (s >= (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
        break;  /* End of sector reached */
      }
      if (s->data[0] == 0x00) {
        break;  /* Found a free entry */
      }
      if (s->data[0] == (unsigned char)0xe5) {
        break;  /* Found a deleted entry */
      }
      s++;
    }
    if (s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
      /* Free entry found. Make entry and return 1st block of the file. */
      FS__CLIB_strncpy((char*)s->data, pDirName, len);
       s->data[11] = FS_FAT_ATTR_DIRECTORY;
      cluster = FS__fat_FAT_alloc(Idx, Unit, -1);              /* Alloc block in FAT */
      if (cluster >= 0) {
        s->data[12]     = 0x00;                                /* Res */
        s->data[13]     = 0x00;                                /* CrtTimeTenth (optional, not supported) */
        s->data[14]     = 0x00;                                /* CrtTime (optional, not supported) */
        s->data[15]     = 0x00;
        s->data[16]     = 0x00;                                /* CrtDate (optional, not supported) */
        s->data[17]     = 0x00;
        s->data[18]     = 0x00;                                /* LstAccDate (optional, not supported) */
        s->data[19]     = 0x00;
        val_time        = FS_X_OS_GetTime();
        s->data[22]     = (unsigned char)(val_time & 0xff);   /* WrtTime */
        s->data[23]     = (unsigned char)(val_time / 256);
        val_date        = FS_X_OS_GetDate();
        s->data[24]     = (unsigned char)(val_date & 0xff);   /* WrtDate */
        s->data[25]     = (unsigned char)(val_date / 256);
        s->data[26]     = (unsigned char)(cluster & 0xff);    /* FstClusLo / FstClusHi */ 
        s->data[27]     = (unsigned char)((cluster / 256) & 0xff);
        s->data[20]     = (unsigned char)((cluster / 0x10000L) & 0xff);
        s->data[21]     = (unsigned char)((cluster / 0x1000000L) & 0xff);
        s->data[28]     = 0x00;                                /* FileSize */
        s->data[29]     = 0x00;
        s->data[30]     = 0x00;
        s->data[31]     = 0x00;
        err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer); /* Write the modified directory sector */
        if (err < 0) {
          FS__fat_free(buffer);
          return -1;
        }
        /* Clear new directory and make '.' and '..' entries */
        /* Make "." entry */
        FS__CLIB_memset(buffer, 0x00, (FS_size_t)FS_FAT_SEC_SIZE);
        s = (FS__fat_dentry_type*)buffer;
        FS__CLIB_strncpy((char*)s->data, ".          ", 11);
        s->data[11]     = FS_FAT_ATTR_DIRECTORY;
        s->data[22]     = (unsigned char)(val_time & 0xff);   /* WrtTime */
        s->data[23]     = (unsigned char)(val_time / 256);
        s->data[24]     = (unsigned char)(val_date & 0xff);   /* WrtDate */
        s->data[25]     = (unsigned char)(val_date / 256);
        s->data[26]     = (unsigned char)(cluster & 0xff);    /* FstClusLo / FstClusHi */ 
        s->data[27]     = (unsigned char)((cluster / 256) & 0xff);
        s->data[20]     = (unsigned char)((cluster / 0x10000L) & 0xff);
        s->data[21]     = (unsigned char)((cluster / 0x1000000L) & 0xff);
        /* Make entry ".." */
        s++;
        FS__CLIB_strncpy((char*)s->data, "..         ", 11);
        s->data[11]     = FS_FAT_ATTR_DIRECTORY;
        s->data[22]     = (unsigned char)(val_time & 0xff);   /* WrtTime */
        s->data[23]     = (unsigned char)(val_time / 256);
        s->data[24]     = (unsigned char)(val_date & 0xff);   /* WrtDate */
        s->data[25]     = (unsigned char)(val_date / 256);
        s->data[26]     = (unsigned char)(DirStart & 0xff);    /* FstClusLo / FstClusHi */ 
        s->data[27]     = (unsigned char)((DirStart / 256) & 0xff);
        s->data[20]     = (unsigned char)((DirStart / 0x10000L) & 0xff);
        s->data[21]     = (unsigned char)((DirStart / 0x1000000L) & 0xff);
        dsec = FS__fat_dir_realsec(Idx, Unit, cluster, 0); /* Find 1st absolute sector of the new directory */
        if (dsec == 0) {
          FS__fat_free(buffer);
          return -1;
        }
        /* Write "." & ".." entries into the new directory */
        err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
        if (err < 0) {
          FS__fat_free(buffer);
          return -1;
        }
        /* Clear rest of the directory cluster */
        FS__CLIB_memset(buffer, 0x00, (FS_size_t)FS_FAT_SEC_SIZE);
        for (j = 1; j < FS__FAT_aBPBUnit[Idx][Unit].SecPerClus; j++) {
          dsec = FS__fat_dir_realsec(Idx, Unit, cluster, j);
          err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
          if (err < 0) {
            FS__fat_free(buffer);
            return -1;
          }
        }
        FS__fat_free(buffer);
        return 1;

      }
      FS__fat_free(buffer);
      return -1;
    }
  }
  FS__fat_free(buffer);
  return -2;  /* Directory is full */
}
Example #7
0
int  FS__fat_MkRmDir(const char *pDirName, int Idx, char MkDir) {
  FS_size_t len;
  FS_u32 dstart;
  FS_u32 dsize;
  FS_u32 unit;
  FS_i32 i;
  int lexp_a;
  int lexp_b;
  char realname[12];
  char *filename;

  if (Idx < 0) {
    return -1; /* Not a valid index */
  }
  dsize = FS__fat_findpath(Idx, pDirName, &filename, &unit, &dstart);
  if (dsize == 0) {
    return -1;  /* Path not found */
  }
  FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_INC_BUSYCNT, 0, (void*)0); /* Turn on busy signal */
  len = FS__CLIB_strlen(filename);
  if (len != 0) {
    FS__fat_make_realname(realname, filename);  /* Convert name to FAT real name */
    i =  FS__fat_find_dir(Idx, unit, realname, dstart, dsize);
    lexp_a = (i!=0) && (MkDir);  /* We want to create a direcory , but it does already exist */
    lexp_b = (i==0) && (!MkDir); /* We want to remove a direcory , but it does not exist */
    lexp_a = lexp_a || lexp_b;
    if (lexp_a) {
      /* We want to create, but dir does already exist or we want to remove, but dir is not there */
      /* turn off busy signal */
      FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);
      return -1;
    }
  }
  else {
    FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn off busy signal */
    return -1;
  }
  /* 
      When you get here, variables have following values:
       dstart="current"  
       dsize="size of current"  
       realname="real dir name to create" 
  */
  if (MkDir) {
    i = _FS_fat_create_directory(Idx, unit,realname, dstart, dsize);  /* Create the directory */
  }
  else {
    i = FS__fat_DeleteFileOrDir(Idx, unit, realname, dstart, dsize, 0);  /* Remove the directory */
  }
  if (i >= 0) {
    /* If the operation has been successfull, flush the cache.*/
    i = FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_FLUSH_CACHE, 2, (void*)0);
    FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn of busy signal */
    if (i < 0) {
      return -1;
    }
    return 0;
  }
  FS__lb_ioctl(FS__pDevInfo[Idx].devdriver, unit, FS_CMD_DEC_BUSYCNT, 0, (void*)0);  /* Turn of busy signal */
  return -1;
}
Example #8
0
int FS__CLIB_atoi(const char *s) {
  int value;
  int len;
  int i;
  unsigned int base;
  const char *t;
  char c;
  signed char sign;

  value = 0;
  /* Check for +/- */
  sign = 1;
  len = FS__CLIB_strlen(s);
  if (len <= 0) {
    return 0;
  }
  t = s;
  if (*t == '-') {
    t++;
    sign = -1;
  }
  else if (*t == '+') {
    t++;
  }
  /* Skip leading 0 */
  len = FS__CLIB_strlen(t);
  if (len <= 0) {
    return 0;
  }
  while (1) {
    if (*t != '0') {
      break;
    }
    t++;
    len--;
    if (len <= 0) {
      break;
    }
  }
  /* Find end of number */
  for (i = 0; i < len; i++) {
    if (t[i] > '9') {
      break;
    }
    if (t[i] < '0') {
      break;
    }
  }
  len = i;
  if (len <= 0) {
      return 0;
  }
  /* Calculate base */
  base = 1;
  for (i = 1; i < len; i++) {
    base *= 10;
  }
  /* Get value */
  for (i = 0; i < len; i++) {
    c = t[i];
    if (c > '9') {
      break;
    }
    if (c < '0') {
      break;
    }
    c -= '0';
    value += c*base;
    base /= 10;
  }
  return sign * value;
}