#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#define MAX_DEPTH 100
#define MAX_PATH 1024
int max_depth = MAX_DEPTH;
int show_hidden = 0;
int show_size = 0;
int show_perms = 0;
int color_enabled = 0;
void print_usage() {
printf("Usage: tree [-d <depth>] [-a] [-s] [-p] [-c] [directory]\n");
printf("Options:\n");
printf(" -d <depth>: Display directory tree up to specified depth (default: 100)\n");
printf(" -a: Show hidden files and directories\n");
printf(" -s: Show file sizes in kilobytes\n");
printf(" -p: Show file permissions\n");
printf(" -c: Enable color coding\n");
printf(" directory: Directory to display (default: current directory)\n");
}
void print_color(char* text, char* color) {
if (color_enabled) {
printf("%s%s\033[0m", color, text);
} else {
printf("%s", text);
}
}
void print_file(char* path, char* name, int depth) {
struct stat statbuf;
char* color = "\033[0m";
if (lstat(path, &statbuf) == -1) {
perror("lstat");
return;
}
if (!show_hidden && name[0] == '.') {
return;
}
if (S_ISDIR(statbuf.st_mode)) {
color = "\033[34m"; // blue
} else if (access(path, X_OK) == 0) {
color = "\033[32m"; // green
} else if (!(statbuf.st_mode & S_IWUSR)) {
color = "\033[31m"; // red
}
//printf("%*s", depth * 4, "├──");
print_color(name, color);
if (show_perms) {
printf(" (%o)", statbuf.st_mode & 0777);
}
if (show_size && !S_ISDIR(statbuf.st_mode)) {
printf(" %ldK", (long) statbuf.st_size / 1024);
}
printf("\n");
}
void print_tree(char* path, int depth) {
if (depth > max_depth) {
return;
}
DIR* dir = opendir(path);
if (dir == NULL) {
perror("opendir");
return;
}
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
char* name = entry->d_name;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
continue;
}
char child_path[MAX_PATH];
snprintf(child_path, sizeof(child_path), "%s/%s", path, name);
print_file(child_path, name, depth);
printf("%*s", depth * 4, "├──");
if (entry->d_type == DT_DIR) {
print_tree(child_path, depth + 1);
printf("%*s", depth * 4, "├──");
}
}
closedir(dir);
}
void compare_files(char* path1, char* path2) {
printf("Comparing %s and %s:\n", path1, path2);
// Implement file comparison logic here
}
int main(int argc, char* argv[]) {
int opt;
char* dir;
// Process command line arguments
while ((opt = getopt(argc, argv, "d:aspch")) != -1) {
switch (opt) {
case 'd':
max_depth = atoi(optarg);
break;
case 'a':
show_hidden = 1;
break;
case 's':
show_size = 1;
break;
case 'p':
show_perms = 1;
break;
case 'c':
color_enabled = 1;
break;
case 'h':
default:
print_usage();
return 0;
}
}
// Get directory path
char* path;
if (optind < argc) {
path = argv[optind];
} else {
path = ".";
}
// Print directory tree
printf("%s\n", path);
print_tree(path, 0);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#define MAX_DEPTH 100
#define MAX_PATH 1024
int max_depth = MAX_DEPTH;
int show_hidden = 0;
int show_size = 0;
int show_perms = 0;
int color_enabled = 0;
void print_usage() {
printf("Usage: tree [-d <depth>] [-a] [-s] [-p] [-c] [directory]\n");
printf("Options:\n");
printf(" -d <depth>: Display directory tree up to specified depth (default: 100)\n");
printf(" -a: Show hidden files and directories\n");
printf(" -s: Show file sizes in kilobytes\n");
printf(" -p: Show file permissions\n");
printf(" -c: Enable color coding\n");
printf(" directory: Directory to display (default: current directory)\n");
}
void print_color(char* text, char* color) {
if (color_enabled) {
printf("%s%s\033[0m", color, text);
} else {
printf("%s", text);
}
}
void print_file(char* path, char* name, int depth) {
struct stat statbuf;
char* color = "\033[0m";
if (lstat(path, &statbuf) == -1) {
perror("lstat");
return;
}
if (!show_hidden && name[0] == '.') {
return;
}
if (S_ISDIR(statbuf.st_mode)) {
color = "\033[34m"; // blue
} else if (access(path, X_OK) == 0) {
color = "\033[32m"; // green
} else if (!(statbuf.st_mode & S_IWUSR)) {
color = "\033[31m"; // red
}
printf("%*s", depth * 4, "");
print_color(name, color);
if (show_perms) {
printf(" (%o)", statbuf.st_mode & 0777);
}
if (show_size && !S_ISDIR(statbuf.st_mode)) {
printf(" %ldK", (long) statbuf.st_size / 1024);
}
printf("\n");
}
void print_tree(char* path, int depth) {
if (depth > max_depth) {
return;
}
DIR* dir = opendir(path);
if (dir == NULL) {
perror("opendir");
return;
}
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
char* name = entry->d_name;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
continue;
}
char child_path[MAX_PATH];
snprintf(child_path, sizeof(child_path), "%s/%s", path, name);
print_file(child_path, name, depth);
if (entry->d_type == DT_DIR) {
print_tree(child_path, depth + 1);
}
}
closedir(dir);
}
int main(int argc, char* argv[]) {
int opt;
char* dir;
// Process command line arguments
while ((opt = getopt(argc, argv, "d:aspch")) != -1) {
switch (opt) {
case 'd':
max_depth = atoi(optarg);
break;
case 'a':
show_hidden = 1;
break;
case 's':
show_size = 1;
break;
case 'p':
show_perms = 1;
break;
case 'c':
color_enabled = 1;
break;
case 'h':
default:
print_usage();
return 0;
}
}
// Get directory path
char* path;
if (optind < argc) {
path = argv[optind];
} else {
path = ".";
}
// Print directory tree
printf("%s\n", path);
print_tree(path, 0);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#define MAX_DEPTH 100
#define MAX_PATH 1024
int max_depth = MAX_DEPTH;
int show_hidden = 0;
int show_size = 0;
int show_perms = 0;
int color_enabled = 0;
void print_usage() {
printf("Usage: tree [-d <depth>] [-a] [-s] [-p] [-c] [directory]\n");
printf("Options:\n");
printf(" -d <depth>: Display directory tree up to specified depth (default: 100)\n");
printf(" -a: Show hidden files and directories\n");
printf(" -s: Show file sizes in kilobytes\n");
printf(" -p: Show file permissions\n");
printf(" -c: Enable color coding\n");
printf(" directory: Directory to display (default: current directory)\n");
}
void print_color(char* text, char* color) {
if (color_enabled) {
printf("%s%s\033[0m", color, text);
} else {
printf("%s", text);
}
}
void print_file(char* path, char* name, int depth) {
struct stat statbuf;
char* color = "\033[0m";
if (lstat(path, &statbuf) == -1) {
perror("lstat");
return;
}
if (!show_hidden && name[0] == '.') {
return;
}
if (S_ISDIR(statbuf.st_mode)) {
color = "\033[34m"; // blue
} else if (access(path, X_OK) == 0) {
color = "\033[32m"; // green
} else if (!(statbuf.st_mode & S_IWUSR)) {
color = "\033[31m"; // red
}
printf("%*s", depth * 4, "");
print_color(name, color);
if (show_perms) {
printf(" (%o)", statbuf.st_mode & 0777);
}
if (show_size && !S_ISDIR(statbuf.st_mode)) {
printf(" %ldK", (long) statbuf.st_size / 1024);
}
printf("\n");
}
void print_tree(char* path, int depth) {
if (depth > max_depth) {
return;
}
DIR* dir = opendir(path);
if (dir == NULL) {
perror("opendir");
return;
}
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
char* name = entry->d_name;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
continue;
}
char child_path[MAX_PATH];
snprintf(child_path, sizeof(child_path), "%s/%s", path, name);
print_file(child_path, name, depth);
if (entry->d_type == DT_DIR) {
print_tree(child_path, depth + 1);
}
}
closedir(dir);
}
int main(int argc, char* argv[]) {
int opt;
char* dir;
// Process command line arguments
while ((opt = getopt(argc, argv, "d:aspch")) != -1) {
switch (opt) {
case 'd':
max_depth = atoi(optarg);
break;
case 'a':
show_hidden = 1;
break;
case 's':
show_size = 1;
break;
case 'p':
show_perms = 1;
break;
case 'c':
color_enabled = 1;
break;
case 'h':
default:
print_usage();
return 0;
}
}
// Get directory path
char* path;
if (optind < argc) {
path = argv[optind];
} else {
path = ".";
}
// Print directory tree
printf("%s\n", path);
print_tree(path, 0);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <dirent.h>
void display_diff(char* dir1, char* dir2) {
DIR *d1, *d2;
struct dirent *dir;
char path1[100], path2[100];
d1 = opendir(dir1);
d2 = opendir(dir2);
if (d1 && d2) {
printf("Directory: %s\t\tDirectory: %s\n", dir1, dir2);
printf("---------------------------------------------------------\n");
while ((dir = readdir(d1)) != NULL) {
if (dir->d_type == DT_REG) { // check if file
sprintf(path1, "%s/%s", dir1, dir->d_name);
sprintf(path2, "%s/%s", dir2, dir->d_name);
FILE* fp1 = fopen(path1, "rb");
FILE* fp2 = fopen(path2, "rb");
if (fp1 && fp2) { // check if both files exist
char buf1[1024], buf2[1024];
size_t n1, n2;
while ((n1 = fread(buf1, 1, sizeof(buf1), fp1)) > 0 && (n2 = fread(buf2, 1, sizeof(buf2), fp2)) > 0) {
//if (n1 != n2 || memcmp(buf1, buf2, n1)) {
printf("%-35s| %-35s\n", path1, path2);
break;
//}
}
fclose(fp1);
fclose(fp2);
} else {
if (fp1) fclose(fp1);
if (fp2) fclose(fp2);
if (fp1 && !fp2) {
printf("%-35s| %-35s\n", path1, "");
} else if (!fp1 && fp2) {
printf("%-35s| %-35s\n", "", path2);
}
}
}
}
}
closedir(d1);
closedir(d2);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s directory1 directory2\n", argv[0]);
return 1;
}
display_diff(argv[1], argv[2]);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <dirent.h>
void display_diff(char* dir1, char* dir2) {
DIR *d1, *d2;
struct dirent *dir;
char path1[100], path2[100];
d1 = opendir(dir1);
d2 = opendir(dir2);
if (d1 && d2) {
printf("Directory: %s\t\tDirectory: %s\n", dir1, dir2);
printf("---------------------------------------------------------\n");
while ((dir = readdir(d1)) != NULL) {
if (dir->d_type == DT_REG) { // check if file
sprintf(path1, "%s/%s", dir1, dir->d_name);
sprintf(path2, "%s/%s", dir2, dir->d_name);
FILE* fp1 = fopen(path1, "rb");
FILE* fp2 = fopen(path2, "rb");
if (fp1 && fp2) { // check if both files exist
char buf1[1024], buf2[1024];
size_t n1, n2;
while ((n1 = fread(buf1, 1, sizeof(buf1), fp1)) > 0 && (n2 = fread(buf2, 1, sizeof(buf2), fp2)) > 0) {
if (n1 != n2 || memcmp(buf1, buf2, n1)) {
printf("%-35s| %-35s\n", path1, path2);
break;
}
}
fclose(fp1);
fclose(fp2);
} else {
if (fp1) fclose(fp1);
if (fp2) fclose(fp2);
if (fp1 && !fp2) {
printf("%-35s| %-35s\n", path1, "");
}
if (!fp1 && fp2) {
printf("%-35s| %-35s\n", "", path2);
}
}
}
}
}
closedir(d1);
closedir(d2);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s directory1 directory2\n", argv[0]);
return 1;
}
display_diff(argv[1], argv[2]);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <dirent.h>
void display_diff(char* dir1, char* dir2) {
DIR *d1, *d2;
struct dirent *dir;
char path1[100], path2[100];
d1 = opendir(dir1);
d2 = opendir(dir2);
if (d1 && d2) {
printf("Directory: %s\t\t\tDirectory: %s\n", dir1, dir2);
printf("---------------------------------------------------------\n");
while (((dir = readdir(d1)) != NULL) && ((dir = readdir(d2)) != NULL)) {
if (dir->d_type == DT_REG) { // check if file
sprintf(path1, "%s/%s", dir1, dir->d_name);
sprintf(path2, "%s/%s", dir2, dir->d_name);
FILE* fp1 = fopen(path1, "rb");
FILE* fp2 = fopen(path2, "rb");
if (fp1 && fp2)
{ // check if both files exist
char buf1[1024], buf2[1024];
size_t n1, n2;
while ((n1 = fread(buf1, 1, sizeof(buf1), fp1)) > 0 && (n2 = fread(buf2, 1, sizeof(buf2), fp2)) > 0) {
printf("%-35s| %-35s\n", path1, path2);
break;
}
fclose(fp1);
fclose(fp2);
} else {
if (fp1) fclose(fp1);
if (fp2) fclose(fp2);
// if (fp1 && !fp2) {
// printf("%-35s| %-35s\n", path1, path2);
// }
// if (!fp1 && fp2) {
// printf("%-35s| %-35s\n", path1, path2);
// }
} // else
} // if in while
} // while
} // if
closedir(d1);
closedir(d2);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s directory1 directory2\n", argv[0]);
return 1;
}
display_diff(argv[1], argv[2]);
return 0;
}