Exemple #1
0
int main(int argc, char* argv[])
{
	char* nom;
	int npartition = 0;
	int i;
	if(argc < 3)
	{
			fprintf(stderr, "Erreur: le nombre d'argument est invalide \n");
			exit(-1);
	}
	for(i=1;i<argc-1;i++)
	{
		if(!strcmp(argv[i], "-p"))
		{
			if(atoi(argv[i+1]) ==0)
			{
				fprintf(stderr, "Erreur: L'argument -p doit être suivi par un nombre\n");
				exit(-1);
			}
			npartition++;
		}
	}
	
	int partition[npartition];
	//printf("npar = %d \n", npartition);
	int k=0;
	for(i=1;i<argc-1;i++)
	{
		if(!strcmp(argv[i], "-p"))
		{
			if(!atoi(argv[i+1]) || atoi(argv[i+1]) < 0)
			{
				fprintf(stderr, "Erreur: L'argument -p doit être suivi par un nombre positif \n");
				exit(-1);
			}
			partition[k] = atoi(argv[i+1]);
			k++;
		}
	}
	
	for(i = 0; i<npartition;i++)
	{
		if(partition[i] < 0)
		{
			fprintf(stderr, "Erreur: La taille des partitions ne peut pas être négative\n");
				exit(-1);
		}
	}
	if(atoi(argv[argc-1]) == 0) // Si le dernier argument n'est pas un nombre alors on a donné le nom du disque
	{						// en parametre
		nom = argv[argc-1];
	}
	else
	{
		nom = "disk.tfs";
	}

	/*
	d = malloc(sizeof(disk_id_s));
	b = malloc(BLOCK_SIZE);
	f = malloc(BLOCK_SIZE);
	printf("%d", size);
	strncpy(f,toLittleEndian(size),4);
	if(argc == 4)
		nom = argv[3];
	else
		nom = "disk.tfs";
	start_disk(nom, &d);
	write_block(d, b, size-1);
	write_block(d, f, 0);
	printf("Creation du disque %s de taille %d blocs (%d octets) \n", nom, size, size*BLOCK_SIZE);*/

	int offset = 4;
	disk_id d;
	d=malloc(sizeof(disk_id_s));
	
	block first_block = malloc(1024);

	char* n_little = toLittleEndian(npartition); 

	//printf("%s", nom);
	start_disk(nom, &d);
	read_block(d, first_block, 0);


	int tmp = 0;
	for(i=0;i<npartition;i++)
	{
		tmp+= partition[i];
	}
	char* sub = blocksub(first_block,0);
	int taille_disk = data_to_int(sub, 4);
	//printf("-> %d\n", taille_disk);
	if(tmp > taille_disk-1)
	{
		fprintf(stderr, "Erreur: La taille totale des partitions dépasse la taille du disque \n");
		exit(-1);
	}
	ajouter_infos(first_block,offset,n_little, sizeof(n_little));
	offset += 4;

	write_block(d, first_block,0);

	for(i = 0; i<npartition;i++)
	{
		printf("Creation de la partition : %d de taille %d bloc(s)\n", i, partition[i]);
		char* p = toLittleEndian(partition[i]);
		ajouter_infos(first_block,offset,p, sizeof(p));
		offset +=4;
	}
	stop_disk(d);
	return 0;
}
Exemple #2
0
// ----------------------------------------- 
// main function
// -----------------------------------------
//
// Input:
//   none
//
// Returns:
//   0/1 - success/failure
//
// Notes:
//   - character input is not handled yet.
// 
int main(void)
{
  int  idx;                      // general purpose index
  int  inputIdx;                 // index specific to stepping through user input 
  int  littleEndian = 0;         // flag for little endian: 0 = false, 1 = true
  int  jump;                     // amount to jump when parsing user input string
  int  decimalNumber;            // final integer to convert
  long long inputDecimalNumber;  // temp placeholder for value to convert 

  char ch, scrap;                // holds prompt for little endian conversion + stdin flush

  char decIntBits[INTBITS + 1];  // holds bits that describe decimal integer
  char hexBits[HEXINTBITS + 1];  // holds hex value for decIntBits[]

  char promptString[] = "Enter a number (0 to cancel): "; 

  int  maxchars = 100;           // maximum number of characters in user input
  char inputNumbers[maxchars];   // holds user input


  // Prompt for big endian or little endian output
  //
  printf("Byte ordering is set to big endian. Switch to little endian? (Y = yes): ");
  scanf(" %c", &ch);
  while ((scrap = getchar()) != '\n' && scrap != EOF);  // flush stdin
  if(tolower(ch) == 'y')
  {
      littleEndian = 1;
      printf("Byte order: little-endian\n");
  }
  else
  {
      printf("Byte order: big-endian\n");
  }


  // Prompt for a number. Loop until we get a number we can use, then
  // display the binary number and break the loop.
  //
  while(1 && (inputDecimalNumber != 0))
  {
      printf("\n%s", promptString);

      if(fgets(inputNumbers, sizeof(inputNumbers), stdin) != NULL)  // no valid characters input?
      {
          for(inputIdx = 0; (sscanf(&inputNumbers[inputIdx], "%25lld%n", &inputDecimalNumber, &jump) != EOF) && (jump <= maxchars); inputIdx += jump)
          {
              // If the number entered was outside the min/max range of regular integers...
              //
              if( inputDecimalNumber < INT_MIN || inputDecimalNumber > INT_MAX )
              {
                  printf("Number is outside of integer range (%d to %d).\n", INT_MIN, INT_MAX);
              }
              // If we get this far, we have a valid number. Convert it, then
              // display it and break from the while loop.
              //
              else if( inputDecimalNumber != 0 )
              {
                  decimalNumber = (int)inputDecimalNumber;   // convert input number to normal int
                  intToBin(decimalNumber, decIntBits);       // convert int to binary


                  // print the integer being converted
                  //
                  printf("%10d ", decimalNumber);

                  // convert to little endian byte ordering if requested
                  //
                  if(littleEndian == 1)
                  {
                      toLittleEndian(decIntBits);
                  }

          
                  // convert final bit array to hex
                  //
                  idx = 0;
                  while(idx < HEXINTBITS)
                  {
                      hexBits[idx++] = '0';                      
                  }

                  binToHex(decIntBits, hexBits);             // convert to hex
                  printf("0x");                              // start the hex string


                  // display the hex string
                  //
                  idx = 0;
                  while(idx < strlen(hexBits))
                  {
                      printf("%c", hexBits[idx]);
                      idx++;
                  }
                  printf("\n");
              }
          }
      }
      else
      {
          break;
      }
  }

  return 0;

} // end of main()