Question: Your program must use only the POSIX system call write() (you may also use _exit() if you want). You cannot use any C library functions,
Your program must use only the POSIX system call write() (you may also use _exit() if you want). You cannot use any C library functions, such as printf(), putchar(), getchar(), exit(), strncmp(), strlen(), isalpha(). The only #include of standard library that you can do is #include
Details: You will write three C program files:
1. The file functions.c should contain four functions. The first two functions are "single digit conversion" functions (between int and char) that are then used by the next two "multi digit conversion" functions:
/************************* * * * FILE functions.c * * * *************************/ /* This file should only contain four standalone functions and must not include any headers! Your code here should be pure C code that does not use any library function at all! */ /* Function 1: char idtoc(int d) { } - Takes an integer input - Requires the input to be between 0 and 15 (inclusive) - Returns the character for that int digit, assuming the maximum possible base of 16 (that is, hex) */ char idtoc(int d) { /* Write your code here */ } /* Function 2: int ctoid(int base, char c) { } - This is a kind of an inverse function of idtoc() - The first input parameter is an int specifying the conversion base which is required to be between 2 and 16 (inclusive) - The second parameter is any character - Return value: If the character c represents a valid digit in the given base, returns the int value of that digit, else returns -1. For bases between 11 and 16 (inclusive), allow both upper case and lowe case characters as digits. */ int ctoid(int base, char c) { /* Write your code here */ } /* Function 3 - Convert a char digit-string to the int it represents int strtonum(int base, char *bufsrc, unsigned int *ndst) { } - The first input parameter is an int specifying the conversion base which is required to be between 2 and 16 (inclusive) - The second input parameter is a pointer to a character string, which is required to be null-terminated string - The third input parameter is a pointer to an unsigned int (for storing the converted integer) - Return value: If all inputs are valid and the conversion can be done successfully, then return 0. Otherwise return a negative integer whose value is informative. Example: If the character string pointed to by bufsrc contains an invalid char (i.e. any char other than the digit characters in the given base) then return -1, else if the string is a valid digit string in the given base but the integer it represents is too large to be stored as an unsigned int then return -2, etc. - As usual, the caller is responsible for valid storage at the locations pointed to bt all the pointer parameters that are passed */ int strtonum(int base, char *bufsrc, unsigned int *ndst) { /* Write your code here */ } /* Function 4 - Convert an int to its char digit string in a given base: int numtostr(int base, unsigned int *nsrc, char *bufdst) { } - The first input parameter is an int specifying the conversion base which is required to be between 2 and 16 (inclusive) - The second input parameter is a pointer to the unsigned int (required to be non-negative) that needs to be converted to its representation as a string of digit characters in the given base - The third parameter is a pointer to a buffer of chars; the caller is required to have allocated sufficient storage to this buffer for holding the converted character digit string, including the terminating null character - Return value: On success, return the length of the computed digit string (without leading zero), and return -1 on any error */ int numtostr(int base, unsigned int *nsrc, char *bufdst) { /* Write your code here */ } /******************************* * END OF FILE functions.c * *******************************/ You can compile the source code in functions.c into object code using the -c flag of gcc as follows:
gcc -Wall -m32 -c functions.c ls
If you do not have compilation errors, this will produce the object file functions.o, which is the machine code version of your source program in C. Object files are not programs to be directly executed. Rather, they are meant to be linked at a later stage for use by the main program.
2. Build a test executable (file test.c)
Write a file test.c for testing your functions in functions.c. For creating an executable program from it, you will need to have a main() function in the file test.c. The purpose of this program is to run a thorough suite of test cases for your functions in functions.c. For example, your test program here could print (using the function numtostr()) the first twenty positive integers in decimal, binary, octal, and hex. The test program can also test your function strtonum() by converting strings of character digits (in various bases) into the corresponding numbers. The test.c will need to include declarations of the functions from functions.c as follows:
/************************* * * * FILE test.c * * * *************************/ #include#include /* More as needed */ /* Function declarations for functions in functions.c */ int ctoid(int base, char c); char idtoc(int d); int strtonum(int base, char *bufsrc, unsigned int *ndst); int numtostr(int base, unsigned int *nsrc, char *bufdst); int main(int argc, char *argv[]) { /* Write your code here */ }
You may use C library functions in your test program.
To build the test program, first compile each source file into object code:
gcc -Wall -m32 -c functions.c gcc -Wall -m32 -c test.c ls
Make sure you have both object files functions.o and test.o built correctly. Then link all the object files together in the following way:
gcc -Wall -m32 -o test functions.o test.o ls
Run the executable file test to test your code.
3. The final base conversion program baseconv.c
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
/********************** * FILE baseconv.c * **********************/ #include/* No other #include allowed! */ int strtonum(int base, char *bufsrc, unsigned int *ndst); int numtostr(int base, unsigned int *nsrc, char *bufdst); /* Any globals or statics should go here, but be careful */ int main(int argc, char *argv[]) { /* Write your code here */ }
The program baseconv can be built by first compiling the source code into object code as:
gcc -Wall -m32 -c functions.c gcc -Wall -m32 -c baseconv.c ls
and then linking the resulting object files into the final exectuable as:
gcc -Wall -m32 -o baseconv functions.o baseconv.o
Example outputs from the program are shown below to illustrate how it should work
$ ./baseconv Error: Usage: baseconv from_base to_base num1 [num2 ...] $ ./baseconv b d 1101 13 $ ./baseconv d o 50 62 $ ./baseconv b d 10 11 1011 2 3 11 $ ./baseconv b d 123 Error: Invalid digit character encountered $ ./baseconv d h 9 10 11 12 15 16 100 255 256 512 65535 65536 9 a b c f 10 64 ff 100 200 ffff 10000 $ ./baseconv x d 10 20 100 55 aa ff 100 ffff fffffffe 16 32 256 85 170 255 256 65535 4294967294 $ ./baseconv d d 10 20 99 5555 10 20 99 5555 $ ./baseconv p q 1010 Error: Invalid base specification $ ./baseconv d x 77777777777 Error: Number too large to be converted
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
