Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language C: Below is an implementation of scandir, given the implelmentation, please answer the following questions: *Note, function sel behaves as the function, filter (see

Language C:

Below is an implementation of scandir, given the implelmentation, please answer the following questions:

*Note, function sel behaves as the function, filter (see man scandir, and look for filter usage), analogously cmp is a comparator function used in this implementation, we are not provided these functions' implementations (which would be useful, I know), the questions asked pertain only to the code provided below.

image text in transcribed

image text in transcribed

1) Line 12 makes use of continue, when does the condition if(sel && !sel(de)) will evaluate as true and cause continue to be called?

2) The function calls realloc on line 16 without having made a previous call to malloc. Why is this valid in this case? Is this just a style choice?

3) On line 16, it assigns the return value from realloc to tmp and two lines later copies from the pointer from tmp to names. Why does it not just assign directly to names, and then check if names is NULL? What is the advantage to tmp?

4) Line 27 refers to a mysterious errno which arises out of nowhere. Read man 3 errno to learn more about its purpose and function. If an allocation failure occurred, what will be the value of errno? (Hint: read NOTES section of the malloc man page)

5) Line 32 is a little hard to parse, but it is applying a typecast to the function pointer being passed to qsort. Try compiling the code both with and without the cast. What warning/error do you get without the cast? Casting a function pointer is a sketchy thing to do, why did they choose to do so here? Do you agree with this choice?

6) The scandir filter function receives its argument as a const struct dirent *; the comparison function receives its arguments as const struct dirent **. Why the inconsistency? Is it so we can use void *?

**Any additional thoughts on this is appreciated, this is a little cryptic to me.

1 int my scandir(const char *path, struct dirent ***res, int (sel) (const struct dirent *) int (*cmp) (const struct dirent **, const struct dirent 4 DIR *dopendir(path); struct dirent *de, *"names =NULL, **tmp; size_t cnt =0, len 0; 7 8 9 10 if (!d) return -1; while readdir(d))) { ((de if 12 13 (sel && !sel(de)) continue; if (cnt >= len) { 15 16 17 18 19 20 len 2*len+ 1 ; if (len SIZE_MAX/sizeof("names)) break; tmp realloc (names, len * sizeof(*names)); if (!tmp) break; names = tmp; names [cnt]-malloc(de->d_reclen); if (names [cnt]) break; memcpy (names [cnt++], de, de->d_reclen) 23 24 25 26 27 28 29 30 closedir(d); errno if (names) while (cnt-- >0) free(names [cnt]); free(names) return -1; 32 32 if (cmp) qsort(names, cnt, sizeof names, (int ()(const void *, const void ))cmp)

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

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions