Question: The final base conversion program must only use the POSIX system call write() (and possibly also _exit()). You cannot use any C library functions. The

The final base conversion program must only use the POSIX system call write() (and possibly also _exit()). You cannot use any C library functions. The only #include of standard library that you can do is #include , ans so your file should be like this:

I can not use atoi for this function because it requires stdlib and I can not use anymore headers only unistd.h. Could you fix the atoi.

#include

int strtonum(int base, char *bufsrc, unsigned int *ndst);

int numtostr(int base, unsigned int *nsrc, char *bufdst);

int main(int argc, char *argv[]) {

if (argc != 4) {

write(STDERR_FILENO, "Usage: ./base_conversion ", 50);

_exit(1);

}

int base_from = atoi(argv[1]);

int base_to = atoi(argv[2]);

char *number = argv[3];

unsigned int decimal_number;

int strtonum_result = strtonum(base_from, number, &decimal_number);

if (strtonum_result == -1) {

write(STDERR_FILENO, "Error: Invalid character in input ", 31);

_exit(1);

} else if (strtonum_result == -2) {

write(STDERR_FILENO, "Error: Overflow ", 16);

_exit(1);

}

char output_number[33];

int numtostr_result = numtostr(base_to, &decimal_number, output_number);

if (numtostr_result == -1) {

write(STDERR_FILENO, "Error: Invalid base ", 20);

_exit(1);

}

write(STDOUT_FILENO, output_number, numtostr_result);

write(STDOUT_FILENO, " ", 1);

return 0;

}

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 Databases Questions!