Question: Why is there no output from my function? void check _ galaxy ( char * galaxy _ pathname ) { FILE * fp = fopen

Why is there no output from my function?
void check_galaxy(char *galaxy_pathname){
FILE *fp = fopen(galaxy_pathname, "r");
if (fp == NULL){
perror("Failed to open the file");
exit(1);
}
while (1){
int c = fgetc(fp);
if (c == EOF){
break;
}
// Read magic number and star format
uint8_t magic_number =(uint8_t)c;
if (magic_number != INITIAL_MAGIC){
fprintf(stderr, "error: incorrect first star byte: 0x%02x should be 0x%02x
", magic_number, INITIAL_MAGIC);
break;
}
c = fgetc(fp);
if (c == EOF){
break;
}
uint8_t star_format =(uint8_t)c;
if (star_format != STAR_FORMAT1 && star_format != STAR_FORMAT2 && star_format != STAR_FORMAT3){
fprintf(stderr, "error: incorrect star format: 0x%02x
", star_format);
break;
}
// read pathname length
unsigned char pathname_length_bytes[2]; //2B
for (int i =0; i <2; i++){
c = fgetc(fp);
if (c == EOF){
fclose(fp);
return;
}
pathname_length_bytes[i]=(unsigned char)c;
}
uint16_t pathname_length = pathname_length_bytes[0]|(pathname_length_bytes[1]<<8);
// read pathname
char *pathname =(char *)malloc(pathname_length +1);
if (pathname == NULL){
perror("Failed to allocate memory for pathname");
fclose(fp);
exit(1);
}
for (int i =0; i < pathname_length; i++){
c = fgetc(fp);
if (c == EOF){
free(pathname);
fclose(fp);
return;
}
pathname[i]=(char)c;
}
pathname[pathname_length]='\0';
// Read content length
unsigned char content_length_bytes[6]; //6B
int valid_bytes =1;
for (int i =0; i <6; i++){
c = fgetc(fp);
if (c == EOF){
free(pathname);
valid_bytes =0;
break;
}
content_length_bytes[i]=(unsigned char)c;
}
if (!valid_bytes){
break;
}
uint64_t content_length = content_length_bytes[0]|
(content_length_bytes[1]<<8)|
(content_length_bytes[2]<<16)|
(content_length_bytes[3]<<24)|
((uint64_t)content_length_bytes[4]<<32)|
((uint64_t)content_length_bytes[5]<<40);
// Read and calculate the hash
uint8_t calculated_hash =0;
for (uint64_t i =0; i < content_length; i++){
c = fgetc(fp);
if (c == EOF){
break;
}
calculated_hash = galaxy_hash(calculated_hash, (uint8_t)c);
}
// Read the initial hash
c = fgetc(fp);
if (c == EOF){
break;
}
uint8_t initial_hash =(uint8_t)c;
// Compare the calculated hash with the initial hash
if (initial_hash == calculated_hash){
printf("%s - correct hash
", pathname);
} else {
printf("%s - incorrect hash 0x%x should be 0x%x
", pathname, initial_hash, calculated_hash);
}
free(pathname);
}
fclose(fp);
} the right situation./space -C examples/4_files.galaxy
256.bin - correct hash
hello.txt - correct hash
last_goodbye.txt - correct hash
these_days.txt - correct hash
Check the galaxy called examples/hello_world.bad_hash.galaxy, which is in the examples directory
./space -C examples/hello_world.bad_hash.galaxy
hello.c - correct hash
hello.cpp - correct hash
hello.d - correct hash
hello.go - correct hash
hello.hs - correct hash
hello.java - correct hash
hello.js - correct hash
hello.pl - correct hash
hello.py - correct hash
hello.rs - correct hash
hello.s - correct hash
hello.sh - correct hash
hello.sql - incorrect hash 0x19 should be 0x43 but mine~/space$ ./space -C examples/4_files.gala~/space$ ./space -C examples/hello_world.bad_hash.gala~/space$no output product

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!