File: main.c | Size: 2,515 bytes | Download file | Back to directory listing | BWPOW's homepage
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include "ext2sim.h"
 
FS_INFO info;
extern char fs_state;
extern S_SUPERBLOCK superblock;
 
extern FS_DIR fs_dir;
 
typedef struct{
  char str[1024];
}PARAM;
 
PARAM param[128];
int num_param;
 
int tokenize(char *buf)
{
   char *tok;
   int num=0;
 
   int l;
   l=strlen(buf);
   if(l<=0) return 0;
   if(buf[l-1]=='\n') buf[l-1]=0;
 
   for(tok=strtok(buf," ");tok;tok=strtok(0," ")){
     strcpy(param[num].str,tok);
     num++;
   }
   num_param=num;
   return num;
}
 
int main(int argc, char *argv[])
{
  char buf[1024];
  int r;
  int a,b,ok,i;
 
  fs_state=FS_STATE_NOT_MOUNTED;
 
  if(argc<2){
    printf("Ako parameter treba zadat nazov suboru!\n");
    return 1;
  }
  r=mount_fs(argv[1],FS_STATE_MOUNTED_RW);
  if(r<0){
    printf("Neviem otvorit subor '%s'!\n",argv[1]);
    return 2;
  }
 
  if(dir_read(2,"/")!=0){
    printf("Neviem nacitat root adresar zo suboru '%s'!\n",argv[1]);
  }
 
  while(1){
    printf("root@%s>",fs_dir.name);
    fgets(buf,1024,stdin);
    if(tokenize(buf)>0){
      ok=0;
      if(!stricmp(param[0].str,"exit")) break;
      if(!stricmp(param[0].str,"check")){
        check();
        ok=1;
      }
      if(!stricmp(param[0].str,"format")){
        if(num_param<3){
          printf("Usage: format group_count block_size\n");
        }
        else{
          if(sscanf(param[1].str,"%d",&a)!=1||sscanf(param[2].str,"%d",&b)!=1){
            printf("bad number format!\n");
          }
          else{
            r=format(a,b);
            if(r<0) printf("Error formating disk!\n");
            if(r==0) printf("Disk formatted successfully.\n");
          }
        }
        ok=1;
      }
      if(!stricmp(param[0].str,"ls")){
        function_ls();
        ok=1;
      }
      if(!stricmp(param[0].str,"cd")){
        if(num_param<2){
          printf("Usage: cd directory_name\n");
        }
        else{
          if(function_cd(param[1].str)!=0) printf("Bad directory name!\n");
        }
        ok=1;
      }
      if(!stricmp(param[0].str,"cat")){
        if(num_param<2){
          printf("Usage: cat file_name\n");
        }
        else{
          if(function_cat(param[1].str)!=0) printf("Bad file name!\n");
        }
        ok=1;
      }
 
      if(ok==0){
        printf("Bad command or operator!\n");
      }
    }
 
  }
  printf("BYE!\n");
 
  umount_fs();
  return 0;
}