Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions

Question

Where in the Constitution can the due process clause be found?

Answered: 1 week ago

Question

Are your goals SMART?

Answered: 1 week ago

Question

=+5 Does this case provide an example of the future for IHRM?

Answered: 1 week ago

Question

=+4 How did it affect HR?

Answered: 1 week ago