-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrazzshell.c
More file actions
2696 lines (2398 loc) · 82.6 KB
/
Copy pathrazzshell.c
File metadata and controls
2696 lines (2398 loc) · 82.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <pwd.h>
#include <time.h>
#include <sys/utsname.h>
#include <grp.h>
#include <termios.h>
#include <dlfcn.h>
#include <linux/limits.h> // For PATH_MAX
#include <sys/select.h>
#include <sys/time.h>
#include <strings.h>
#include <ctype.h>
// RazzShell modernization includes
#include "src/shell_config.h"
#include "src/posix_compat.h"
#define MAX_ARGS 128
#define MAX_JOBS 100
#define MAX_HISTORY 1000
#define MAX_BOOKMARKS 100
#define MAX_ALIASES 100
// Color codes
#define RESET_COLOR "\x1b[0m"
#define GREEN_COLOR "\x1b[38;5;46m" // Bright neon green
#define BLUE_COLOR "\x1b[38;5;33m" // Bright cyan-blue
#define CYAN_COLOR "\x1b[38;5;51m" // Electric cyan
#define RED_COLOR "\x1b[38;5;196m" // Bright red
#define YELLOW_COLOR "\x1b[38;5;226m" // Bright yellow
#define MAGENTA_COLOR "\x1b[38;5;201m" // Hot pink
#define PURPLE_COLOR "\x1b[38;5;93m" // Deep purple
#define ORANGE_COLOR "\x1b[38;5;208m" // Bright orange
// Style codes
#define BOLD_TEXT "\x1b[1m"
#define UNDERLINE_TEXT "\x1b[4m"
#define BLINK_TEXT "\x1b[5m"
#define DIM_TEXT "\x1b[2m"
#define ITALIC_TEXT "\x1b[3m"
// Background colors
#define BG_BLACK "\x1b[40m"
#define BG_BLUE "\x1b[44m"
#define BG_CYBER "\x1b[48;5;17m" // Dark blue background
// Shell styling
#define PROMPT_STYLE BOLD_TEXT CYAN_COLOR
#define ERROR_STYLE BOLD_TEXT RED_COLOR
#define SUCCESS_STYLE BOLD_TEXT GREEN_COLOR
#define WARNING_STYLE BOLD_TEXT YELLOW_COLOR
#define INFO_STYLE BOLD_TEXT BLUE_COLOR
#define CYBER_STYLE BOLD_TEXT PURPLE_COLOR BG_CYBER
// ASCII art frames
#define TOP_BORDER "╔════════════════════════════════════╗"
#define BOTTOM_BORDER "╚════════════════════════════════════╝"
#define SIDE_BORDER "║"
// Enhanced color gradients for cyber theme
#define NEON_CYAN "\x1b[38;5;51m"
#define NEON_BLUE "\x1b[38;5;33m"
#define NEON_GREEN "\x1b[38;5;46m"
#define NEON_PINK "\x1b[38;5;198m"
#define NEON_PURPLE "\x1b[38;5;141m"
#define NEON_YELLOW "\x1b[38;5;226m"
#define NEON_ORANGE "\x1b[38;5;214m"
#define NEON_RED "\x1b[38;5;196m"
// Gradient backgrounds
#define BG_DARK_BLUE "\x1b[48;5;17m"
#define BG_CYBER_ALT "\x1b[48;5;23m"
#define BG_CYBER_DIM "\x1b[48;5;16m"
// Job structure to manage background jobs
typedef struct {
int id;
pid_t pid;
char command[256];
int is_background;
} Job;
// Plugin structure
typedef struct {
char *name;
void *handle;
int (*command_func)(char **args);
} Plugin;
Plugin plugins[MAX_ALIASES]; // Reuse the MAX_ALIASES constant for simplicity
int plugin_count = 0;
static char *ai_api_key = NULL;
static char *ai_model = NULL;
// Alias structure
typedef struct {
char *alias_name;
char *command;
} Alias;
Job jobs[MAX_JOBS];
int job_count = 0;
char *history[MAX_HISTORY];
int history_count = 0;
char *bookmarks[MAX_BOOKMARKS];
int bookmark_count = 0;
Alias aliases[MAX_ALIASES];
int alias_count = 0;
static char *safe_strdup(const char *value) {
if (!value) {
return NULL;
}
char *copy = strdup(value);
if (!copy) {
fprintf(stderr, ERROR_STYLE "Memory allocation failed\n" RESET_COLOR);
}
return copy;
}
static char *escape_json_string(const char *input) {
if (!input) {
return safe_strdup("");
}
size_t length = 0;
for (const unsigned char *ptr = (const unsigned char *)input; *ptr; ptr++) {
switch (*ptr) {
case '\\':
case '"':
length += 2;
break;
case '\n':
case '\r':
case '\t':
length += 2;
break;
default:
if (iscntrl(*ptr)) {
length += 6;
} else {
length += 1;
}
}
}
char *escaped = malloc(length + 1);
if (!escaped) {
fprintf(stderr, ERROR_STYLE "Memory allocation failed\n" RESET_COLOR);
return NULL;
}
char *out = escaped;
for (const unsigned char *ptr = (const unsigned char *)input; *ptr; ptr++) {
switch (*ptr) {
case '\\':
*out++ = '\\';
*out++ = '\\';
break;
case '"':
*out++ = '\\';
*out++ = '"';
break;
case '\n':
*out++ = '\\';
*out++ = 'n';
break;
case '\r':
*out++ = '\\';
*out++ = 'r';
break;
case '\t':
*out++ = '\\';
*out++ = 't';
break;
default:
if (iscntrl(*ptr)) {
snprintf(out, 7, "\\u%04x", *ptr);
out += 6;
} else {
*out++ = (char)*ptr;
}
}
}
*out = '\0';
return escaped;
}
static char *join_args(char **args, int start_index) {
size_t total = 0;
int count = 0;
for (int i = start_index; args[i] != NULL; i++) {
total += strlen(args[i]) + 1;
count++;
}
if (count == 0) {
return safe_strdup("");
}
char *result = malloc(total);
if (!result) {
fprintf(stderr, ERROR_STYLE "Memory allocation failed\n" RESET_COLOR);
return NULL;
}
result[0] = '\0';
for (int i = start_index; args[i] != NULL; i++) {
strcat(result, args[i]);
if (args[i + 1] != NULL) {
strcat(result, " ");
}
}
return result;
}
// Shell environment setup
void setup_shell_env() {
setenv("SHELL", "/usr/local/bin/razzshell", 1);
setenv("RAZZSHELL_VERSION", "2.0.0", 1);
setenv("RAZZSHELL_MODE", shell_mode_name(shell_get_mode()), 1);
}
// Forward declarations
char **razzshell_completion(const char *text, int start, int end);
char *command_generator(const char *text, int state);
char *get_command_name(int index);
void initialize_readline();
char *read_input_line();
char *get_prompt();
// Signal handling variables
struct termios shell_tmodes;
pid_t shell_pgid;
// Function prototypes for commands
int razz_change(char **args); // cd
int razz_quit(char **args); // exit
int razz_say(char **args); // echo
int razz_where(char **args); // pwd
int razz_viewjobs(char **args); // jobs
int razz_bringtofront(char **args); // fg
int razz_sendtoback(char **args); // bg
int razz_terminate(char **args); // kill
int razz_list(char **args); // ls
int razz_copy(char **args); // cp
int razz_move(char **args); // mv
int razz_delete(char **args); // rm
int razz_searchfile(char **args); // find
int razz_readfile(char **args); // cat
int razz_searchtext(char **args); // grep
int razz_commands(char **args); // history
int razz_create(char **args); // touch
int razz_makedir(char **args); // mkdir
int razz_removedir(char **args); // rmdir
int razz_setperm(char **args); // chmod
int razz_setowner(char **args); // chown
int razz_showprocesses(char **args); // ps
int razz_whome(char **args); // whoami
int razz_pinghost(char **args); // ping
int razz_fetchurl(char **args); // curl
int razz_sudo(char **args); // sudo
int razz_sudo_su(char **args); // sudo su
int razz_save(char **args); // save session
int razz_load(char **args); // load session
int razz_bookmark(char **args); // bookmark
int razz_listbookmarks(char **args);// list bookmarks
int razz_visualize(char **args); // visualize command flow
int razz_sysinfo(char **args); // system information
int razz_diskusage(char **args); // disk usage
int razz_cpuusage(char **args); // cpu usage
int razz_memusage(char **args); // memory usage
int razz_howto(char **args); // help
int razz_makealias(char **args); // alias
int razz_removealias(char **args); // unalias
int razz_setenv(char **args); // set environment variable
int razz_printenv(char **args); // print environment variables
int razz_clear(char **args); // clear screen
int razz_today(char **args); // date
int razz_calendar(char **args); // cal
int razz_diskfree(char **args); // df
int razz_diskuse(char **args); // du
int razz_systemname(char **args); // uname
int razz_headfile(char **args); // head
int razz_tailfile(char **args); // tail
int razz_wordcount(char **args); // wc
int razz_aliases(char **args); // list aliases
int razz_unsetenv(char **args); // unset environment variable
int razz_repeat(char **args); // repeat command
int razz_history_clear(char **args);// clear history
int razz_mkcd(char **args); // mkdir + cd
int razz_which(char **args); // which
int razz_setapi_ai(char **args); // set AI API key and model
int razz_ai(char **args); // AI command
int razz_loadplugin(char **args); // Load a plugin
int razz_unloadplugin(char **args); // Unload a plugin
int razz_monitor(char **args); // System resource monitor
int razz_matrix(char **args); // Matrix-style text effect
int razz_sysart(char **args); // System information with ASCII art
int razz_clock(char **args); // Digital clock
int razz_fetch(char **args); // RazzFetch (custom neofetch-style system info)
int razz_history_search(char **args); // Enhanced history search
int razz_mode(char **args); // Switch shell mode
int razz_set(char **args); // Set shell options (set -e, etc.)
// Command-to-function mapping
typedef struct {
char *command_name;
int (*command_func)(char **args);
char *description;
} CommandMap;
CommandMap command_list[] = {
{"change", razz_change, "Change directory"}, // cd
{"loadplugin", razz_loadplugin, "Load a plugin"}, // loadplugin
{"unloadplugin", razz_unloadplugin, "Unload a plugin"}, // unloadplugin
{"quit", razz_quit, "Exit the shell"}, // exit
{"say", razz_say, "Display a line of text"}, // echo
{"where", razz_where, "Print working directory"}, // pwd
{"viewjobs", razz_viewjobs, "List active background jobs"}, // jobs
{"bringtofront", razz_bringtofront, "Bring job to foreground"}, // fg
{"sendtoback", razz_sendtoback, "Send job to background"}, // bg
{"terminate", razz_terminate, "Terminate a process"}, // kill
{"list", razz_list, "List directory contents"}, // ls
{"copy", razz_copy, "Copy files"}, // cp
{"move", razz_move, "Move/rename files"}, // mv
{"delete", razz_delete, "Delete files"}, // rm
{"searchfile", razz_searchfile, "Search for files"}, // find
{"readfile", razz_readfile, "Display file contents"}, // cat
{"searchtext", razz_searchtext, "Search text in files"}, // grep
{"commands", razz_commands, "Show command history"}, // history
{"create", razz_create, "Create a file"}, // touch
{"makedir", razz_makedir, "Create directory"}, // mkdir
{"mkcd", razz_mkcd, "Create a directory and switch into it"},
{"removedir", razz_removedir, "Remove directory"}, // rmdir
{"setperm", razz_setperm, "Change file permissions"}, // chmod
{"setowner", razz_setowner, "Change file owner and group"}, // chown
{"showprocesses", razz_showprocesses, "Show running processes"}, // ps
{"whome", razz_whome, "Show current user"}, // whoami
{"pinghost", razz_pinghost, "Ping a host"}, // ping
{"fetchurl", razz_fetchurl, "Fetch URL"}, // curl
{"sudo", razz_sudo, "Run command as root"}, // sudo
{"sudo_su", razz_sudo_su, "Switch to root shell within razzshell"}, // sudo su
{"save", razz_save, "Save current session"}, // save session
{"load", razz_load, "Load saved session"}, // load session
{"bookmark", razz_bookmark, "Bookmark a command"}, // bookmark
{"listbookmarks", razz_listbookmarks, "List all bookmarks"}, // list bookmarks
{"visualize", razz_visualize, "Visualize command flow"}, // visualize command flow
{"sysinfo", razz_sysinfo, "Display system information"}, // system information
{"diskusage", razz_diskusage, "Display disk usage"}, // disk usage
{"cpuusage", razz_cpuusage, "Display CPU usage"}, // cpu usage
{"memusage", razz_memusage, "Display memory usage"}, // memory usage
{"howto", razz_howto, "Show help for commands"}, // help
{"makealias", razz_makealias, "Create a command alias"}, // alias
{"removealias", razz_removealias, "Remove a command alias"}, // unalias
{"setenv", razz_setenv, "Set an environment variable"}, // set environment variable
{"printenv", razz_printenv, "Print environment variables"}, // print environment variables
{"clear", razz_clear, "Clear the terminal screen"},
{"today", razz_today, "Display current date and time"}, // date
{"calendar", razz_calendar, "Display calendar"}, // cal
{"diskfree", razz_diskfree, "Display free disk space"}, // df
{"diskuse", razz_diskuse, "Estimate file space usage"}, // du
{"systemname", razz_systemname, "Print system information"}, // uname
{"headfile", razz_headfile, "Display first lines of a file"}, // head
{"tailfile", razz_tailfile, "Display last lines of a file"}, // tail
{"wordcount", razz_wordcount, "Count words in a file"}, // wc
{"aliases", razz_aliases, "List all aliases"}, // list aliases
{"unsetenv", razz_unsetenv, "Unset an environment variable"}, // unset environment variable
{"which", razz_which, "Locate a command in PATH or built-ins"},
{"setapi-ai", razz_setapi_ai, "Set AI API key and model"},
{"ai", razz_ai, "Query the configured AI model"},
{"repeat", razz_repeat, "Repeat a command multiple times"}, // repeat command
{"history_clear", razz_history_clear, "Clear command history"}, // clear history
{"monitor", razz_monitor, "Show system resource monitor"},
{"matrix", razz_matrix, "Display Matrix-style animation"},
{"sysart", razz_sysart, "Show system information with ASCII art"},
{"clock", razz_clock, "Show digital clock"},
{"razzfetch", razz_fetch, "Display system information in RazzShell style"},
{"hsearch", razz_history_search, "Search command history with highlighting"},
{"mode", razz_mode, "Switch shell execution mode"},
{"set", razz_set, "Set shell options (set -e, set -o pipefail, etc.)"},
// Add additional commands here as per your list
};
// File type labels for list command
#define ICON_DIRECTORY "[DIR]"
#define ICON_FILE "[FILE]"
#define ICON_EXECUTABLE "[EXE]"
#define ICON_IMAGE "[IMG]"
#define ICON_VIDEO "[VID]"
#define ICON_AUDIO "[AUD]"
#define ICON_ARCHIVE "[ARC]"
#define ICON_TEXT "[TXT]"
#define ICON_PDF "[PDF]"
#define ICON_CONFIG "[CFG]"
#define ICON_LINK "[LNK]"
// Function to get file icon based on extension and permissions
const char* get_file_icon(const char* name, mode_t mode) {
if (S_ISDIR(mode)) return ICON_DIRECTORY;
if (S_ISLNK(mode)) return ICON_LINK;
if (mode & S_IXUSR) return ICON_EXECUTABLE;
// Get file extension
const char* ext = strrchr(name, '.');
if (!ext) return ICON_FILE;
ext++; // Skip the dot
// Common file extensions
if (strcasecmp(ext, "jpg") == 0 || strcasecmp(ext, "png") == 0 ||
strcasecmp(ext, "gif") == 0 || strcasecmp(ext, "bmp") == 0)
return ICON_IMAGE;
if (strcasecmp(ext, "mp4") == 0 || strcasecmp(ext, "avi") == 0 ||
strcasecmp(ext, "mkv") == 0)
return ICON_VIDEO;
if (strcasecmp(ext, "mp3") == 0 || strcasecmp(ext, "wav") == 0 ||
strcasecmp(ext, "flac") == 0)
return ICON_AUDIO;
if (strcasecmp(ext, "zip") == 0 || strcasecmp(ext, "tar") == 0 ||
strcasecmp(ext, "gz") == 0)
return ICON_ARCHIVE;
if (strcasecmp(ext, "txt") == 0 || strcasecmp(ext, "md") == 0 ||
strcasecmp(ext, "c") == 0 || strcasecmp(ext, "cpp") == 0 ||
strcasecmp(ext, "py") == 0 || strcasecmp(ext, "js") == 0)
return ICON_TEXT;
if (strcasecmp(ext, "pdf") == 0)
return ICON_PDF;
if (strcasecmp(ext, "conf") == 0 || strcasecmp(ext, "config") == 0 ||
strcasecmp(ext, "ini") == 0)
return ICON_CONFIG;
return ICON_FILE;
}
// Format size in human-readable format
char* format_size(off_t size) {
static char buf[32];
const char* units[] = {"B", "K", "M", "G", "T"};
int unit = 0;
double size_d = size;
while (size_d >= 1024 && unit < 4) {
size_d /= 1024;
unit++;
}
if (unit == 0)
sprintf(buf, "%ld%s", (long)size_d, units[unit]);
else
sprintf(buf, "%.1f%s", size_d, units[unit]);
return buf;
}
// Format permissions in a readable format
char* format_permissions(mode_t mode) {
static char perms[11];
strcpy(perms, "----------");
// File type
if (S_ISDIR(mode)) perms[0] = 'd';
else if (S_ISLNK(mode)) perms[0] = 'l';
// User permissions
if (mode & S_IRUSR) perms[1] = 'r';
if (mode & S_IWUSR) perms[2] = 'w';
if (mode & S_IXUSR) perms[3] = 'x';
// Group permissions
if (mode & S_IRGRP) perms[4] = 'r';
if (mode & S_IWGRP) perms[5] = 'w';
if (mode & S_IXGRP) perms[6] = 'x';
// Others permissions
if (mode & S_IROTH) perms[7] = 'r';
if (mode & S_IWOTH) perms[8] = 'w';
if (mode & S_IXOTH) perms[9] = 'x';
return perms;
}
// Improved list command with better organization and visuals
int razz_list(char **args) {
DIR *dir;
struct dirent *entry;
struct stat file_stat;
char path[PATH_MAX];
char datestr[256];
struct passwd *pw;
struct group *gr;
// Get target directory
const char *target_dir = args[1] ? args[1] : ".";
dir = opendir(target_dir);
if (!dir) {
printf(ERROR_STYLE "Error: Could not open directory %s\n" RESET_COLOR, target_dir);
return 1;
}
// Print header with fancy border
printf(CYBER_STYLE "\n╭──────────────────────────────────────────────────────────────╮\n");
printf("│ " BOLD_TEXT "Directory Listing: %-43s" RESET_COLOR CYBER_STYLE "│\n", target_dir);
printf("├──────────────────────────────────────────────────────────────┤\n" RESET_COLOR);
printf(BOLD_TEXT "%-2s %-10s %-8s %-8s %-6s %-19s %s\n" RESET_COLOR,
"", "Perms", "Owner", "Group", "Size", "Modified", "Name");
printf(DIM_TEXT "%-2s %-10s %-8s %-8s %-6s %-19s %s\n" RESET_COLOR,
"", "----------", "--------", "--------", "------", "-------------------", "--------------------");
// Store entries for sorting
typedef struct file_entry {
char name[256];
char perms[11];
char owner[32];
char group[32];
char size[32];
char date[64];
char icon[8];
int is_dir;
} FileEntry;
FileEntry entries[1024];
int entry_count = 0;
// Read directory entries
while ((entry = readdir(dir)) != NULL && entry_count < 1024) {
snprintf(path, sizeof(path), "%s/%s", target_dir, entry->d_name);
if (lstat(path, &file_stat) < 0)
continue;
// Skip hidden files unless -a flag is present
if (entry->d_name[0] == '.' && (!args[1] || strcmp(args[1], "-a") != 0))
continue;
// Get owner and group names
pw = getpwuid(file_stat.st_uid);
gr = getgrgid(file_stat.st_gid);
// Format modification time
strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
// Store entry information
FileEntry *current = &entries[entry_count];
strncpy(current->name, entry->d_name, sizeof(current->name) - 1);
strncpy(current->perms, format_permissions(file_stat.st_mode), sizeof(current->perms) - 1);
strncpy(current->owner, pw ? pw->pw_name : "unknown", sizeof(current->owner) - 1);
strncpy(current->group, gr ? gr->gr_name : "unknown", sizeof(current->group) - 1);
strncpy(current->size, format_size(file_stat.st_size), sizeof(current->size) - 1);
strncpy(current->date, datestr, sizeof(current->date) - 1);
strncpy(current->icon, get_file_icon(entry->d_name, file_stat.st_mode), sizeof(current->icon) - 1);
current->is_dir = S_ISDIR(file_stat.st_mode);
entry_count++;
}
// Sort entries (directories first, then files alphabetically)
for (int i = 0; i < entry_count - 1; i++) {
for (int j = 0; j < entry_count - i - 1; j++) {
if ((entries[j].is_dir < entries[j + 1].is_dir) ||
(entries[j].is_dir == entries[j + 1].is_dir &&
strcasecmp(entries[j].name, entries[j + 1].name) > 0)) {
FileEntry temp = entries[j];
entries[j] = entries[j + 1];
entries[j + 1] = temp;
}
}
}
// Print entries with alternating background colors for better readability
for (int i = 0; i < entry_count; i++) {
const char *bg_color = (i % 2 == 0) ? "" : BG_CYBER;
if (entries[i].is_dir) {
printf("%s%-2s " BLUE_COLOR "%-10s %-8s %-8s %-6s %-19s %s%s" RESET_COLOR "\n",
bg_color, entries[i].icon, entries[i].perms, entries[i].owner,
entries[i].group, entries[i].size, entries[i].date,
entries[i].name, S_ISLNK(file_stat.st_mode) ? " -> " : "");
} else {
printf("%s%-2s %-10s %-8s %-8s %-6s %-19s %s%s" RESET_COLOR "\n",
bg_color, entries[i].icon, entries[i].perms, entries[i].owner,
entries[i].group, entries[i].size, entries[i].date,
entries[i].name, S_ISLNK(file_stat.st_mode) ? " -> " : "");
}
}
// Print footer with summary
printf(CYBER_STYLE "├──────────────────────────────────────────────────────────────┤\n");
printf("│ " BOLD_TEXT "Total: %d items" RESET_COLOR CYBER_STYLE "%-43s│\n", entry_count, "");
printf("╰──────────────────────────────────────────────────────────────╯\n" RESET_COLOR);
closedir(dir);
return 1;
}
// Enhanced RazzFetch with better visuals
int razz_fetch(char **args) {
struct utsname sys_info;
if (uname(&sys_info) == -1) {
printf(ERROR_STYLE "Error getting system information\n" RESET_COLOR);
return 1;
}
// Get username and hostname
char hostname[1024];
struct passwd *pw = getpwuid(getuid());
gethostname(hostname, sizeof(hostname));
// Get memory information
long total_mem = 0, available_mem = 0;
FILE *meminfo = fopen("/proc/meminfo", "r");
if (meminfo) {
char line[256];
while (fgets(line, sizeof(line), meminfo)) {
if (strncmp(line, "MemTotal:", 9) == 0)
sscanf(line, "MemTotal: %ld", &total_mem);
else if (strncmp(line, "MemAvailable:", 12) == 0)
sscanf(line, "MemAvailable: %ld", &available_mem);
}
fclose(meminfo);
}
// Get CPU info
char cpu_model[256] = "Unknown";
FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
if (cpuinfo) {
char line[256];
while (fgets(line, sizeof(line), cpuinfo)) {
if (strncmp(line, "model name", 10) == 0) {
char *colon = strchr(line, ':');
if (colon) {
strncpy(cpu_model, colon + 2, sizeof(cpu_model) - 1);
char *newline = strchr(cpu_model, '\n');
if (newline) *newline = '\0';
}
break;
}
}
fclose(cpuinfo);
}
// Get package count (pacman)
int pacman_count = 0;
FILE *pacman = popen("pacman -Q | wc -l", "r");
if (pacman) {
fscanf(pacman, "%d", &pacman_count);
pclose(pacman);
}
// Get desktop environment
char *desktop_env = getenv("XDG_CURRENT_DESKTOP");
if (!desktop_env) desktop_env = "Unknown";
// Get GPU info
char gpu_info[256] = "Unknown";
FILE *lspci = popen("lspci | grep -i vga | head -n1 | cut -d ':' -f3", "r");
if (lspci) {
fgets(gpu_info, sizeof(gpu_info), lspci);
char *newline = strchr(gpu_info, '\n');
if (newline) *newline = '\0';
pclose(lspci);
}
// Get uptime
long uptime = 0;
FILE *uptime_file = fopen("/proc/uptime", "r");
if (uptime_file) {
fscanf(uptime_file, "%ld", &uptime);
fclose(uptime_file);
}
int days = uptime / 86400;
int hours = (uptime % 86400) / 3600;
int minutes = (uptime % 3600) / 60;
// Clear screen and move cursor to top
printf("\033[2J\033[H");
// Print RazzShell logo with gradient effect
printf(NEON_CYAN " ╭─────────────╮\n");
printf(" │ " NEON_PINK "R" NEON_PURPLE "A" NEON_BLUE "Z" NEON_CYAN "Z"
NEON_GREEN "S" NEON_YELLOW "H" NEON_ORANGE "E" NEON_PINK "L"
NEON_PURPLE "L" NEON_CYAN " │\n");
printf(" ╰─────────────╯\n\n");
// Print system information in a fancy box
printf(NEON_CYAN "╭────────────────────── " NEON_PINK "System Information"
NEON_CYAN " ──────────────────────╮\n");
// User@Host with custom art
printf("│ " NEON_BLUE "%s" NEON_PINK "@" NEON_BLUE "%s" NEON_CYAN "%*s│\n",
pw->pw_name, hostname,
(int)(50 - strlen(pw->pw_name) - strlen(hostname) - 1), "");
// Separator
printf("├──────────────────────────────────────────────────────────────┤\n");
// System information with custom styling and progress bars
printf("│ " NEON_GREEN "OS " RESET_COLOR "%-48s" NEON_CYAN "│\n", sys_info.sysname);
printf("│ " NEON_BLUE "Shell " RESET_COLOR "RazzShell v1.0.2%35s" NEON_CYAN "│\n", "");
printf("│ " NEON_YELLOW "Kernel " RESET_COLOR "%-48s" NEON_CYAN "│\n", sys_info.release);
printf("│ " NEON_PINK "Packages " RESET_COLOR "%d (pacman)%39s" NEON_CYAN "│\n",
pacman_count, "");
printf("│ " NEON_PURPLE "DE " RESET_COLOR "%-48s" NEON_CYAN "│\n", desktop_env);
// Hardware information
printf("├──────────────────────────────────────────────────────────────┤\n");
printf("│ " NEON_ORANGE "CPU " RESET_COLOR "%-48s" NEON_CYAN "│\n", cpu_model);
printf("│ " NEON_GREEN "GPU " RESET_COLOR "%-48s" NEON_CYAN "│\n", gpu_info);
// Memory usage with progress bar
int mem_percent = ((total_mem - available_mem) * 100) / total_mem;
printf("│ " NEON_BLUE "Memory " RESET_COLOR "[");
for (int i = 0; i < 20; i++) {
if (i < mem_percent / 5)
printf(NEON_GREEN "█");
else
printf(DIM_TEXT "░" RESET_COLOR);
}
printf("] %.1f/%.1fG%*s" NEON_CYAN "│\n",
(total_mem - available_mem) / 1048576.0,
total_mem / 1048576.0,
(int)(13 - (mem_percent >= 100 ? 3 : mem_percent >= 10 ? 2 : 1)), "");
// Uptime
printf("│ " NEON_PINK "Uptime " RESET_COLOR "%d days, %d hours, %d mins%*s" NEON_CYAN "│\n",
days, hours, minutes,
(int)(27 - (days >= 100 ? 3 : days >= 10 ? 2 : 1) -
(hours >= 10 ? 2 : 1) - (minutes >= 10 ? 2 : 1)), "");
// Color blocks
printf("├──────────────────────────────────────────────────────────────┤\n");
printf("│ ");
for (int i = 0; i < 8; i++) printf(NEON_CYAN "█" NEON_BLUE "█" NEON_GREEN "█ ");
printf(NEON_CYAN "│\n│ ");
for (int i = 0; i < 8; i++) printf(NEON_PINK "█" NEON_PURPLE "█" NEON_YELLOW "█ ");
printf(NEON_CYAN "│\n");
printf("╰──────────────────────────────────────────────────────────────╯\n");
printf(RESET_COLOR);
return 1;
}
// Function to check for aliases
char* check_alias(char *cmd) {
for (int i = 0; i < alias_count; i++) {
if (strcmp(cmd, aliases[i].alias_name) == 0) {
return aliases[i].command;
}
}
return cmd;
}
// Load a plugin
int razz_loadplugin(char **args) {
if (args[1] == NULL) {
fprintf(stderr, "Usage: loadplugin [plugin_path]\n");
return 1;
}
if (plugin_count >= MAX_ALIASES) {
fprintf(stderr, "Plugin limit reached.\n");
return 1;
}
void *handle = dlopen(args[1], RTLD_LAZY);
if (!handle) {
fprintf(stderr, "Error loading plugin: %s\n", dlerror());
return 1;
}
int (*command_func)(char **args) = dlsym(handle, "plugin_command");
char *error = dlerror();
if (error != NULL) {
fprintf(stderr, "Error finding symbol: %s\n", error);
dlclose(handle);
return 1;
}
plugins[plugin_count].name = safe_strdup(args[1]);
if (!plugins[plugin_count].name) {
dlclose(handle);
return 1;
}
plugins[plugin_count].handle = handle;
plugins[plugin_count].command_func = command_func;
plugin_count++;
printf("Plugin '%s' loaded.\n", args[1]);
return 1;
}
// Unload a plugin
int razz_unloadplugin(char **args) {
if (args[1] == NULL) {
fprintf(stderr, "Usage: unloadplugin [plugin_name]\n");
return 1;
}
for (int i = 0; i < plugin_count; i++) {
if (strcmp(args[1], plugins[i].name) == 0) {
dlclose(plugins[i].handle);
free(plugins[i].name);
for (int j = i; j < plugin_count - 1; j++) {
plugins[j] = plugins[j + 1];
}
plugin_count--;
printf("Plugin '%s' unloaded.\n", args[1]);
return 1;
}
}
fprintf(stderr, "Plugin '%s' not found.\n", args[1]);
return 1;
}
// Generate the shell prompt
char* get_prompt() {
char cwd[PATH_MAX];
const char *fallback_dir = "?";
if (!getcwd(cwd, sizeof(cwd))) {
strncpy(cwd, fallback_dir, sizeof(cwd) - 1);
cwd[sizeof(cwd) - 1] = '\0';
}
char *dir_name = strrchr(cwd, '/');
if (dir_name != NULL) {
dir_name++; // Move past '/'
} else {
dir_name = cwd; // Root directory
}
char *prompt = malloc(256);
if (!prompt) {
return NULL;
}
if (geteuid() == 0) {
snprintf(prompt, 256, CYBER_STYLE TOP_BORDER "\n" SIDE_BORDER " razzshell-# [%s]> " RESET_COLOR, dir_name);
} else {
snprintf(prompt, 256, CYBER_STYLE TOP_BORDER "\n" SIDE_BORDER " razzshell-$ [%s]> " RESET_COLOR, dir_name);
}
return prompt;
}
// Helper function to remove a job from the jobs list
void remove_job(pid_t pid) {
for (int i = 0; i < job_count; i++) {
if (jobs[i].pid == pid) {
for (int j = i; j < job_count - 1; j++) {
jobs[j] = jobs[j + 1];
}
job_count--;
break;
}
}
}
// Signal handlers
void sigint_handler(int signo) {
// Reset Readline state
rl_replace_line("", 0);
rl_on_new_line();
printf("\n");
rl_redisplay();
}
void sigtstp_handler(int sig) {
// Ignore SIGTSTP
}
// Initialize Readline
void initialize_readline() {
rl_readline_name = "razzshell";
rl_attempted_completion_function = razzshell_completion;
}
// Command generator for completion
char *get_command_name(int index) {
int built_in_count = sizeof(command_list) / sizeof(CommandMap);
if (index < built_in_count) {
return command_list[index].command_name;
}
index -= built_in_count;
if (index < alias_count) {
return aliases[index].alias_name;
}
index -= alias_count;
if (index < plugin_count) {
return plugins[index].name;
}
return NULL;
}
char *command_generator(const char *text, int state) {
static int list_index, len;
char *command_name;
if (state == 0) {
list_index = 0;
len = strlen(text);
}
while ((command_name = get_command_name(list_index)) != NULL) {
list_index++;
if (strncmp(command_name, text, len) == 0) {
return safe_strdup(command_name);
}
}
return NULL;
}
char *read_input_line() {
char *prompt = get_prompt();
char *line = readline(prompt);
free(prompt);
if (line && *line) {
add_history(line);
}
return line;
}
char *history_generator(const char *text, int state) {
static int history_index;
HIST_ENTRY **hist_list = history_list();
if (!hist_list) {
return NULL;
}
if (state == 0) {
history_index = history_length;
}
while (--history_index >= 0) {
char *cmd = hist_list[history_index]->line;
if (strncmp(cmd, text, strlen(text)) == 0) {
return safe_strdup(cmd);
}
}
return NULL;
}
// Completion function
char **razzshell_completion(const char *text, int start, int end) {
char **matches = NULL;
// If this is the first word, offer command completions
if (start == 0) {
matches = rl_completion_matches(text, command_generator);
} else {
// For other words, perform default file completion
rl_attempted_completion_over = 0;
matches = NULL;
}
return matches;
}
// Function prototype for highlighting commands
void highlight_command(const char *cmd, const char *highlight);
// Command implementations
int razz_change(char **args) {
if (args[1] == NULL) {
chdir(getenv("HOME"));
} else {
if (chdir(args[1]) != 0) {
perror("change");
}
}
return 1;
}
int razz_quit(char **args) {
return 0; // Exit the shell
}
int razz_say(char **args) {
for (int i = 1; args[i] != NULL; i++) {
if (args[i][0] == '$') {
char *env_var = getenv(args[i] + 1);
if (env_var) {
printf("%s ", env_var);
}
} else {
printf("%s ", args[i]);
}
}
printf("\n");
return 1;
}