Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.25 KB | None | 0 0
  1. int writeFile(char* fname, char* buffer, int sectors)
  2. {
  3.   //Xwrite sectors*512 bytes of data from buffer into a file
  4.   // indicated by fname
  5.  
  6.  
  7.   //X if the file indicated by fname exists, overwrite it.
  8.  
  9.  
  10.   //X max sectors is 26, only write 26 sectors if it is more
  11.  
  12.  
  13.   //if file is successful, return number of sectors
  14.  
  15.   //X if there is no entry available return -1, don't write
  16.  
  17.   //if diskmap contains fewer than sectors free sectors, write as
  18.   //many sectors as possible and return -2.
  19.  
  20.   //CHECK
  21.  
  22.   //ITERATE THROUGH DISKMAP
  23.   //FIND A ZERO, CHANGE TO FF, TAKE INDEX
  24.   //WRITE THAT AS FILELOCATION
  25.  
  26.   char sectorBuf[512];
  27.   char mapBuf[512];
  28.   char dirBuf[512];
  29.   char currentName[7]; //Name is 6 bytes, ends with \0
  30.   char fileLocation[27]; //Location is 26 bytes after name, ends with \0
  31.   int returnVal;
  32.   int looking = 1;
  33.     int i; //Index
  34.     int j = 0; //Index
  35.     int k = 0; //Index
  36.     int l = 0;
  37.     int freeEntry = 0;
  38.     int searchBreak = 0;
  39.  
  40.     int mapBufIndex = 0;
  41.     int entryNumber= 0; //Number of the sector we're working with
  42.     int found = 0; //Checks if file is found
  43.     char entryNum[2];
  44.  
  45.     readSector(mapBuf, 1);
  46.     readSector(dirBuf, 2);
  47.  
  48.     entryNum[1]=entryNumber;
  49.     entryNum[2]='\0';
  50.   //POPULATES fileName and fileLocation buffers from directory
  51.   while(!found && searchBreak == 0)
  52.   { //While we haven't found the file
  53.       for(i = 0; i<32; i++)
  54.       { //Search the entry (each entry is 32 bytes)
  55.         if(i < 6) //While still checking the file name
  56.         {
  57.           currentName[i] = dirBuf[i+32*entryNumber]; //Apply ith letter of our entry's file name to index of currentName
  58.         }
  59.         else //If we've gone through all 6 bytes of the name
  60.         {
  61.           fileLocation[i-6] = dirBuf[i+32*entryNumber]; //Apply ith byte of our entry's location to index of fileLocation
  62.         }
  63.       }
  64.  
  65.  
  66.       //COMPARES STRING OF FILENAME in entry TO CURRENTNAME
  67.       for(i = 0; i < 6; i++) //For length of name
  68.       {
  69.         if(fname[i] == currentName[i]) //If i of fileName equal to i of our desired name
  70.         {
  71.           if(i == 5) //If the names match
  72.           {  
  73.             found = 1; //It's found
  74.           }
  75.         }
  76.         else //If it's not found
  77.         {
  78.           entryNum[0]=entryNumber;
  79.           //printString("[wrong entry..] ");
  80.           i = 7; //End comparison
  81.           entryNumber++; //Go to next entry
  82.           if(entryNumber == 16) //If we've searched all possible entries
  83.           {
  84.             printString(" [File not found!] "); //Then tell us we searched all possible entires
  85.             searchBreak = 1;
  86.           }
  87.         }
  88.       }
  89.   }
  90.  
  91.  
  92.  
  93.  
  94.   //FILENAME IS FOUND ENTRY MUST BE OVERWRITTEN
  95.   if(found)
  96.   {
  97.     printString("[File Found]");
  98.     deleteFile(fname);
  99.     //ENTRY NUMBER
  100.     //FIND AN EMPTY ENTRYNUMBER
  101.   }
  102.   else
  103.   {
  104.     entryNumber = 0;
  105.     //BREAK OUT WHEN IT FINDS AN ENTRY
  106.     for(i = 0; i<16; i++)
  107.     {
  108.       if(dirBuf[32*entryNumber] != 0x00)
  109.       {
  110.         entryNumber++;
  111.       }
  112.       else
  113.       {
  114.         //freeEntry = 1
  115.         freeEntry = 1;
  116.         i = 16;
  117.       }
  118.  
  119.       if(i==15 && (freeEntry == 0))
  120.       {
  121.         //DIRECTORY IS FULL
  122.         return -1;
  123.       }
  124.  
  125.     }
  126.  
  127. }
  128.  
  129.  
  130.   k = 0; // # OF SECTORS SAVED
  131.  
  132.   //FOR EACH ENTRY IN THE DIRECTORY
  133.   for(i = 0;i<32; i++)
  134.   {
  135.     if(i<6)
  136.     {
  137.       //COPY FILENAME
  138.       dirBuf[i+32*entryNumber] = fname[i];
  139.     }
  140.     //FIND SPACE IN THE MAP
  141.     //FOR GIVEN AMOUNT OF SECTORS
  142.     else
  143.     {
  144.       if(k<sectors)
  145.       {
  146.         if(k>25)
  147.         {
  148.           //MORE SECTORS TO WRITE THAN ARE AVAILABLE
  149.           return -2;
  150.         }
  151.         //FOR THE SIZE OF THE DISK MAP
  152.         looking = 1;
  153.         printString(" [LOOKING FOR FREE SECTOR] ");
  154.         while(looking){
  155.          
  156.        // }
  157.         //for(mapBufIndex = 0; mapBufIndex <512; mapBufIndex++)
  158.         //{
  159.           if(mapBufIndex == 512){
  160.             printString("BREAK");
  161.             looking = 0;
  162.           }
  163.           if(mapBuf[mapBufIndex] == 0x00)
  164.           {
  165.             //SPACE IS FREE!
  166.             for(l = 0; l<mapBufIndex; l++){
  167.               printString("*");
  168.  
  169.             }
  170.  
  171.  
  172.             mapBuf[mapBufIndex] = 0xff; // FLAG THE DISK MAP
  173.            
  174.  
  175.             printString("Flagged!");
  176.             dirBuf[i+32*entryNumber] = mapBufIndex; //COPY SECTOR # TO DIRECTORY
  177.            
  178.             for(j = 0; j<512; j++)//COPY DATA FROM THE BUFFER PARAMETER
  179.             {
  180.               sectorBuf[j] = buffer[k*512+j];
  181.             }
  182.             printString("Sector Written!");
  183.             writeSector(sectorBuf, mapBufIndex); //WRITE THE DATA BUFFER TO THE FOUND SECTOR
  184.             k++; //SECTOR SAVED INCREMENT SECTOR COPIED VARIABLE
  185.             mapBufIndex++; // INCREMENT THE INDEX
  186.             looking = 0;
  187.         }
  188.  
  189.         else
  190.         {
  191.           mapBufIndex++;
  192.           //USED SPACE IN THE DISK MAP / INDEX INCREMENTS
  193.         }
  194.  
  195.  
  196.       }
  197.       //CHECK THIS
  198.       /*
  199.       if(k < sectors)
  200.       {
  201.         return -2;
  202.       }
  203.       */
  204.     }
  205.     else
  206.     {
  207.       //ALL SECTORS WRITTEN
  208.       dirBuf[32*entryNumber+i] = 0x00;
  209.     }
  210.   }
  211.  
  212.  
  213. }
  214. writeSector(dirBuf, 2);
  215. writeSector(mapBuf, 1);
  216.  
  217. //SUCCESSFUL return k
  218. return k;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement