Advertisement
Guest User

cdlspwd

a guest
Apr 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.66 KB | None | 0 0
  1. #include "type.h"
  2.  
  3. void cd(char *pn)
  4. {
  5.     int ino, nchars,inocd;
  6.     MINODE *mip;
  7.     char *path;
  8.    
  9.     if(!strcmp(pn, "") || !strcmp(pn, "/"))
  10.     {
  11.         ino = root->ino;
  12.     }
  13.     else
  14.     {
  15.         //Get cwd ino
  16.         ino = getino(dev, pn);
  17.     }
  18.     mip = iget(dev, ino);
  19.     printf("\nIno: %d\nIMODE for target:%x\n",ino,mip->INODE.i_mode);
  20.  
  21.     //iput(running->cwd);
  22.     //running->cwd = iget(dev, ino);
  23.     printf("Set running->cwd to: %d\n", running->cwd->ino);
  24.    
  25.     if((mip->INODE.i_mode & 0040000) == 0040000)
  26.     {
  27.         iput(running->cwd);
  28.         running->cwd = iget(dev, ino);
  29.         printf("Set running->cwd to: %d\n", running->cwd->ino);
  30.     }
  31.     else
  32.     {
  33.         printf("\'%s\' is not a directory. i_mode: %x\n", pn, mip->INODE.i_mode);
  34.     }
  35.    
  36.     iput(mip);
  37. }
  38.  
  39.  
  40. void printdir(INODE *inodePtr)
  41. {
  42.     int i=0;
  43.     int j=0;
  44.     while ((i <12)&&(inodePtr->i_block[i]!=0))
  45.     {
  46.         int data_block=inodePtr->i_block[i];
  47.         DIR *dp;
  48.         char fbuff[1024];
  49.         memset(fbuff, 0, 1024);
  50.         lseek(fd,BLOCK_SIZE*data_block,SEEK_SET);
  51.         read(fd,fbuff,BLOCK_SIZE);
  52.         dp=(DIR *)fbuff;
  53.         char *cp=fbuff;
  54.         MINODE *mip;
  55.         int ino = dp->inode;
  56.         char name[256];
  57.         while(cp<fbuff+1024)
  58.         {
  59.             mip = iget(fd, ino);
  60.             if(S_ISREG( mip->INODE.i_mode ) ) printf("r");
  61.             if(S_ISDIR( mip->INODE.i_mode ) ) printf("d");
  62.             if(S_ISLNK( mip->INODE.i_mode ) ) printf("l");
  63.  
  64.             for(j=8; j>=0; j--) //Check bits and print permisions for
  65.             {
  66.                 if(mip->INODE.i_mode & 1 << j)
  67.                 {
  68.                     if(j == 8 || j == 5 || j == 2) printf("r");
  69.                     else if(j == 7 || j == 4|| j == 1) printf("w");
  70.                     else printf("x");
  71.                 }
  72.                 else printf("-");
  73.             }
  74.  
  75.             char time_s[64];
  76.             ctime_r((time_t *)&mip->INODE.i_mtime, time_s);
  77.             time_s[strlen(time_s)-1]=0;
  78.             printf(" %3d%3d %3d%6d %20s ", dp->inode, mip->INODE.i_uid,
  79.                     mip->INODE.i_gid, mip->INODE.i_size, time_s);
  80.             memmove(name, dp->name, dp->name_len);
  81.             name[dp->name_len]='\0';
  82.             if(S_ISLNK( mip->INODE.i_mode)){
  83.                 printf("%16s->%s\n",name,(char *)mip->INODE.i_block);
  84.             }else{
  85.                 printf("%16s\n",name);
  86.             }
  87.             iput(mip);
  88.             cp+=dp->rec_len;
  89.             dp=(DIR *)cp;
  90.             ino = dp->inode;
  91.         }
  92.         i++;
  93.     }
  94.     return;
  95. }
  96.  
  97. void ls(char *pathname, PROC *parent)
  98. {
  99.     INODE *cwd = calloc(sizeof(INODE), 1);
  100.     char path[128] = "\0";
  101.     strncpy(path, pathname, 128);
  102.     int inodeIndex, seek;
  103.     if(pathname[0] == '/') {  
  104.         strncpy(path, path+1, 127);
  105.         char *token = strtok(path, "/");
  106.         memcpy(cwd, &root->INODE,sizeof(INODE));
  107.  
  108.         while(token !=NULL) {
  109.             inodeIndex =search(dev,cwd, token);
  110.             if (inodeIndex==0) {
  111.                 printf("cannot find that directory\n");
  112.                 return;
  113.             }
  114.             seek =((inodeIndex-1)/8 + gp->bg_inode_table)*1024+(inodeIndex-1)%8
  115.                 *128;
  116.  
  117.             lseek(fd,seek,SEEK_SET);
  118.             read(fd,cwd,sizeof(INODE));
  119.             token=strtok(NULL,"/");
  120.         }
  121.         if((cwd->i_mode & 0040000) != 0040000){
  122.             printf("cannot ls non dir");
  123.             return;
  124.         }
  125.  
  126.         printdir(cwd);
  127.         return;
  128.     }
  129.  
  130.     else if(pathname[0] <=0){
  131.         printf("current dir: Ino %lu, Iblock[0]= %lu\n",(long unsigned
  132.                     int)parent->cwd->ino, (long unsigned int)parent->cwd->INODE.i_block[0]);
  133.         printdir(&parent->cwd->INODE);
  134.         return;
  135.     }
  136.     else{
  137.         char *token=strtok(path,"/");
  138.         memcpy(cwd, &parent->cwd->INODE,sizeof(INODE));
  139.  
  140.         while(token!=NULL){
  141.             inodeIndex=search(dev,cwd,token);
  142.             if (inodeIndex==0) {
  143.                 printf("cannot find that directory\n");
  144.                 return;
  145.             }
  146.             seek=((inodeIndex-1)/8 + gp->bg_inode_table)*1024+
  147.                 (inodeIndex-1)%8* 128;
  148.             lseek(fd,seek,SEEK_SET);
  149.             read(fd,cwd,sizeof(INODE));
  150.             printdir(cwd);
  151.             token=strtok(NULL,"/");
  152.         }
  153.     }
  154. }
  155.  
  156.  
  157.  
  158.  
  159. int do_pwd()
  160. {
  161.     pwd(running->cwd,0);
  162.     printf("\n");
  163.     return 0;
  164. }
  165.  
  166. char *pwd(MINODE *wd, int childIno)
  167. {
  168.     char wdtemp[1024];
  169.     wdtemp[0]='/';
  170.     wdtemp[1]='\0';
  171.     if(wd->ino == root->ino){
  172.         printf("/");
  173.     }
  174.     char buf[1024], *cp,name[64];
  175.     DIR *dp;
  176.     MINODE *parentmip;
  177.    
  178.     get_block(fd, wd->INODE.i_block[0], (char *)&buf);
  179.     dp = (DIR *)buf;
  180.     cp = buf + dp->rec_len;
  181.     dp = (DIR *)cp;
  182.     if(wd->ino != root->ino){
  183.         int ino = dp->inode;
  184.         parentmip = iget(fd, ino);
  185.         pwd(parentmip,wd->ino);
  186.     }
  187.     if (childIno!=0){
  188.         while (dp->inode != childIno)
  189.         {
  190.             cp += dp->rec_len;
  191.             dp = (DIR *)cp;
  192.         }
  193.         strncpy(name,dp->name,dp->name_len);
  194.         name[dp->name_len] = '\0';
  195.         //strcat(&wdtemp,&name);
  196.         //printf("\n%s\n",wdtemp);
  197.         printf("%s/",name);
  198.     }
  199.  
  200.     return &wdtemp;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement