I'm trying to make a program that simulates the ls command and then sorts the files in case insensitive alphabetical order. So far, all of the file names go into the words array, but when I try to compile, there is a problem.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
// This program is pretty much a simulation of the ls command. Find out how to scan all of the files
// in the directory it's being run in and print out all the file names. Has to be in order.
int main(int argc, char* argv[])
{
char **words = calloc(1000, sizeof(*words));
char **words2 = calloc(1000, sizeof(*words2));
DIR *d;
struct dirent *dir; // Pointer for directory entry
d = opendir(".");
char* a = ".";
char* b = "..";
char* c = "ls";
int ret1, ret2, ret3, count = 0;
if (d) // opendir returns NULL if couldn't open directory
{
while ((dir = readdir(d)) != NULL)
{
ret1 = strcmp(dir->d_name, a); // Compare with the parent directory.
ret2 = strcmp(dir->d_name, b); // Compare with the parent directory.
ret3 = strcmp(dir->d_name, c); // Compare with the ls
if (ret1 == 0 || ret2 == 0 || ret3 == 0)
{
// Skip the ., .., and ls
}
else
{
words[count] = dir->d_name; // Put the file name in the array.
count++;
}
}
for (int i = 1; i < 10; i++) // Start readjusting the array in alphabetical order.
{
for (int j = 1; j < 10; j++)
{
if (strcmp(words[j - 1], words[j]) > 0)
{
strcpy(words2, words[j - 1]);
strcpy(words[j - 1], words[j]);
strcpy(words[j], words2);
}
}
}
// Print every word in the array.
while (count != 0)
{
printf("%s\n", words[count - 1]);
count--;
}
}
// Closing and freeing
closedir(d);
for (int a = 0; a < 1000; ++a)
{
free(words[a]);
}
for (int a = 0; a < 1000; ++a)
{
free(words2[a]);
}
free(words);
free(words2);
return 0;
}
When I compile, this is the error message that I get below. It happens during the sequence when I'm trying to sort the array. Is there something I can do to fix this issue?
ls.c: In function ‘main’:
ls.c:52:13: warning: passing argument 1 of ‘strcpy’ from incompatible pointer type [-Wincompatible-pointer-types]
52 | strcpy(words2, words[j - 1]);
| ^~~~~~
| |
| char **
In file included from ls.c:3:
/usr/include/string.h:122:14: note: expected ‘char * restrict’ but argument is of type ‘char **’
122 | extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
| ^~~~~~
ls.c:54:23: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [-Wincompatible-pointer-types]
54 | strcpy(words[j], words2);
| ^~~~~~
| |
| char **
In file included from ls.c:3:
/usr/include/string.h:122:14: note: expected ‘const char * restrict’ but argument is of type ‘char **’
122 | extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
| ^~~~~~