Write a C program that command ls must print the names of files in the current d
ID: 3595104 • Letter: W
Question
Write a C program that command ls must print the names of files in the current directory sorted alphabetically and in a sequence so that column 1 is followed by column 2, and so on. I have this ls, but i dont know how to modify it to add qsort()
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
void do_ls(char[], int, int, int);
main(int ac, char *av[])
{
int parA=0, parS=0, parR=0, paramCounter=1;
if(ac == 1)
do_ls(".", parA, parS, parR);
else if((ac == 2) && (av[1][0] == '-'))
{
while(paramCounter < sizeof(av[1])){
if(av[1][paramCounter] == 'a') parA = 1;
if(av[1][paramCounter] == 's') parS = 1;
if(av[1][paramCounter] == 'r') parR = 1;
paramCounter++;
}
do_ls(".", parA, parS, parR);
}
else{
if(av[1][0] == '-'){
while(paramCounter < sizeof(av[1])){
if(av[1][paramCounter] == 'a') parA = 1;
if(av[1][paramCounter] == 's') parS = 1;
if(av[1][paramCounter] == 'r') parR = 1;
paramCounter++;
}
++av;
--ac;
}
while (--ac){
printf("%s: ", *++av);
do_ls(*av, parA, parS, parR);
}
}
}
void do_ls(char dirname[], int parA, int parS, int parR)
{
DIR *dir_ptr;
struct dirent *direntp;
int size = 0;
{
if((dir_ptr = opendir(dirname)) == NULL);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
if(parA)
size++;
else{
if(direntp->d_name[0] != '.')
size++;
}
}
closedir(dir_ptr);
}
}
char names[size][150], tempn[150];
int i=0, j;
if(parA)
{
if(parS || parR)
{
if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr, "ls: cannot open %s ", dirname);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
strcpy(names[i], direntp->d_name);
i++;
}
closedir(dir_ptr);
for(i=1; i<size; i++){
for(j=1; j<size; j++){
if((names[j-1], names[j]) > 0){
strcpy(tempn, names[j-1]);
strcpy(names[j-1], names[j]);
strcpy(names[j], tempn);
}
}
}
if(parS){
for(i=0; i<size; i++)
printf("%s ", names[i]);
}
if(parR){
for(i= size-1; i>=0; i--)
printf("%s ", names[i]);
}
}
}
else{
if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr, "ls: cannot open %s ", dirname);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
printf("%s ", direntp->d_name);
}
closedir(dir_ptr);
}
}
}
else
{
if(parS || parR)
{
if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr, "ls: cannot open %s ", dirname);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
if(direntp->d_name[0] != '.'){
strcpy(names[i], direntp->d_name);
i++;
}
}
closedir(dir_ptr);
for(i=1; i<size; i++){
for(j=1; j<size; j++){
if(strcasecmp(names[j-1], names[j]) > 0){
strcpy(tempn, names[j-1]);
strcpy(names[j-1], names[j]);
strcpy(names[j], tempn);
}
}
}
if(parS){
for(i=0; i<size; i++)
printf("%s ", names[i]);
}
if(parR){
for(i= size-1; i>=0; i--)
printf("%s ", names[i]);
}
}
}
else{
if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr, "ls: cannot open %s ", dirname);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
if(direntp->d_name[0] != '.')
printf("%s ", direntp->d_name);
}
closedir(dir_ptr);
}
}
}
}
Explanation / Answer
qsort function to sort pointers of char array has four arguments as shown :
qsort( list, n, sizeof(*list), compariion function);
and the comparisioin function is like ( for pointers of char array ):
int comparision_function ( const void *a, const void *b){
return (strcmp( (char*) a), (char*) b ) );
}
now whereever you have used bubble sort to sort the array of char pointers you can just use this:
And the code looks like:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
/* added dependency for qsort() libbrary function - stdlib.h*/
#include <stdlib.h>
void do_ls(char[], int, int, int);
/* adding a function comparision_function to pass to qsort() function call */
int comparision_function( const void* a, const void* b);
main(int ac, char *av[])
{
int parA=0, parS=0, parR=0, paramCounter=1;
if(ac == 1)
do_ls(".", parA, parS, parR);
else if((ac == 2) && (av[1][0] == '-'))
{/* else if start */
while(paramCounter < sizeof(av[1])){
if(av[1][paramCounter] == 'a') parA = 1;
if(av[1][paramCounter] == 's') parS = 1;
if(av[1][paramCounter] == 'r') parR = 1;
paramCounter++;
}/* while close */
do_ls(".", parA, parS, parR);
}/* else if close */
else{/* else starts */
if(av[1][0] == '-'){ /* if starts */
while(paramCounter < sizeof(av[1])){ /* while starts */
if(av[1][paramCounter] == 'a') parA = 1;
if(av[1][paramCounter] == 's') parS = 1;
if(av[1][paramCounter] == 'r') parR = 1;
paramCounter++;
}
++av;
--ac;
}
while (--ac){
printf("%s: ", *++av);
do_ls(*av, parA, parS, parR);
}
}
}
void do_ls(char dirname[], int parA, int parS, int parR)
{
DIR *dir_ptr;
struct dirent *direntp;
int size = 0;
{
if((dir_ptr = opendir(dirname)) == NULL);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
if(parA)
size++;
else{
if(direntp->d_name[0] != '.')
size++;
}
}
closedir(dir_ptr);
}
}
char names[size][150], tempn[150];
int i=0, j;
if(parA)
{
if(parS || parR)
{
if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr, "ls: cannot open %s ", dirname);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
strcpy(names[i], direntp->d_name);
i++;
}
closedir(dir_ptr);
/* changed to use qsort() */
qsort( names, size, sizeof(*names), comparision_function);
if(parS){
for(i=0; i<size; i++)
printf("%s ", names[i]);
}
if(parR){
for(i= size-1; i>=0; i--)
printf("%s ", names[i]);
}
}
}
else{
if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr, "ls: cannot open %s ", dirname);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
printf("%s ", direntp->d_name);
}
closedir(dir_ptr);
}
}
}
else
{
if(parS || parR)
{
if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr, "ls: cannot open %s ", dirname);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
if(direntp->d_name[0] != '.'){
strcpy(names[i], direntp->d_name);
i++;
}
}
closedir(dir_ptr);
/* changed to use qsort() */
qsort( names, size, sizeof(*names), comparision_function);
if(parS){
for(i=0; i<size; i++)
printf("%s ", names[i]);
}
if(parR){
for(i= size-1; i>=0; i--)
printf("%s ", names[i]);
}
}
}
else{
if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr, "ls: cannot open %s ", dirname);
else
{
while ((direntp = readdir(dir_ptr)) != NULL){
if(direntp->d_name[0] != '.')
printf("%s ", direntp->d_name);
}
closedir(dir_ptr);
}
}
}
}
int comparision_function (const void* a, const void *b )
{
return strcmp( (char *)a, (char *)b );
}
/* hope this works fine and helps you */
/* if you have any further doubts, please comment */
/* thank you */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.