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
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
_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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started