Question 0Write a procedure atos() which takes a NULL-terminated array of strings as its parameter and returns a string. What it should do is allocate, construct and return a single string composed of each string in the array separated by a space. The procedure should run in O(n) time, where n is the total number of characters in the string that you return. |
Question 1In your jtar program, you called lstat(), and it filled in a data structure of type struct stat. List for me all of the ways in which that data structure was used by your jtar program. There may be parts of the data structure that were used for multiple purposes -- list each of these separately. |
rv = read(fd, buf, sz); |
Below are 25 potential outcomes of the read() call. For each outcome, label it either "P" for "Possible" or "I" for "Impossible." In other words, if it is possible for the outcome to occur, label it "P". If there is no way for the outcome to occur, label it "I". I don't want explanation. I just want P's and I's.
A | Fewer than sz bytes are read from a file to buf, and rv is set to the number of bytes that were read. |
B | sbrk(0)-buf is less than sz, and as a result, the read generates a segmentation violation |
C | fd is not an open file, and the read call generates a segmentation violation. |
D | fd is a file opened for writing only, and the read call returns -1 as a result. |
E | buf is pointing to a chunk of memory that is fewer than sz bytes, and the read call generates a segmentation violation. |
F | buf is pointing to the stack segment and sz bytes are read successfully. |
G | Fewer than sz bytes are read from a file to buf, and rv is set to -1. |
H | buf is pointing into the void and the read call returns -1. |
I | buf is pointing to a chunk of memory that is fewer than sz bytes, and the read call corrupts memory in the process. |
J | buf is pointing to a region of sz bytes in the globals segment,and the read call returns -1 because of where buf is pointing |
K | A bus error occurs because buf is not a multiple of four. |
L | buf is pointing to sz bytes in the code segment, and the read call generates a segmentation violation because of where buf is pointing. |
M | Zero bytes are read from any file, and rv is set to 0. |
N | buf is pointing to the code segment and sz bytes are read successfully. |
O | sbrk(0)-buf is less than sz, and as a result, the read call returns -1 |
P | sz bytes are read from a file to buf, and rv is set to sz. |
Q | fd is a file opened for writing only, and the read call generates a segmentation violation. |
R | buf is pointing to the stack segment and a segmentation violation occurs because of where buf is pointing |
S | A buffer overflow attack occurs as a result of the read statement. |
T | buf is pointing to a region of more than sz bytes in the globals segment and sz bytes are read successfully. |
U | buf is pointing to a region of more than sz bytes in the globals segment and a segmentation violation occurs because of where buf is pointing. |
V | fd is not an open file, and the read call returns -1 as a result. |
W | buf is pointing to the stack segment and the read call returns -1 because of where buf is pointing |
X | buf is pointing to sz bytes in the code segment, and the read call returns -1 because of where buf is pointing. |
Y | buf is pointing into the void and the read call generates a segmentation violation. |
For example, the four bytes starting at address 0xbfffdb30 are equal to -1073751220 when represented as an integer. They are equal to 0xbfffdb4c when represented as hexadecimal. The byte at 0xbfffdb30 is equal to the 'L' character. The bytes at 0xbfffdb31, 0xbfffdb32 and 0xbfffdb33 are all non-printable characters.
Here is messy_proc():
|
|
The first three lines printed by messy_proc() are "a: 0xbfffdb30", "b: 0xbfffdb48" and "c: 0xbfffdb3c". Tell me what the rest of the output is. There are no segmentation violations or bus errors in this program (I have compiled and run it).
Question 4Suppose your heap is composed of 384 bytes starting at address 0x1c230, pictured on the right. You are given the following assumptions:
Part B: Tell me all of the allocated chunks of memory. For each chunk, tell me the value that was returned from malloc(), and the total size of the chunk. Part C: What would sbrk(0) return? Part D: Suppose I have an integer pointer j whose value is 0x1c3c4. If I execute "*j = 55", will the operation complete successfully, cause a segmentation violation or cause a bus error? Explain why. |
|
int strlen(char *s); - Returns the length of a string char *strcpy(char *dest, char *src); - Copies the string in src to memory pointed to by dest. - Returns its first argument. char *strdup(char *s); - Allocates room for a copy of s, copies it and returns it. char *strcat(char *dest, char *src); - Assumes that dest is a string, and appends src to it. char *strchr(char *s, char c) - Returns a pointer to the first occurrence of c in s, or NULL. char *strrchr(char *s, char c) - Returns a pointer to the last occurrence of c in s, or NULL. char *strstr(char *s, char *st) - Returns a pointer to the first occurrence of st in s, or NULL. int read(int fd, char *buf, int size);