#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>

int filter(const struct dirent* ent) {
	return ent->d_name[0] != '.';
}

int main(int argc, char* argv[]) {
	struct dirent** array;
	int n = scandir(argv[1], &array, filter, alphasort);

	if (n == -1) {
		perror("scandir");
		exit(EXIT_FAILURE);
	}
	
	for (int i = 0; i < n; i++) {
		size_t size = strlen(argv[1]) + 1 + strlen(array[i]->d_name) + 1;
		char* file = malloc(size);
		if (snprintf(file, size, "%s/%s", argv[1], array[i]->d_name) < 0) {
			perror("snprintf");
			exit(EXIT_FAILURE);
		}

		printf("%s\n", file);
	
		struct stat stat;
		if (lstat(file, &stat) < 0) {
			perror("lstat");
			exit(EXIT_FAILURE);
		}

		if (S_ISLNK(stat.st_mode)) {
			printf("found %s\n", array[i]->d_name);
			free(array[i]);
		}
	}
	free(array);

	exit(EXIT_SUCCESS);
}

