Implementing a Text-based Graphical Shell using Basic C Commands
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* Remove spaces at start and end of string */
char *trim(char *string)
{
char *trimmed;
while (isspace(string[strlen(string) - 1]))
string[strlen(string) - 1] = 0;
trimmed = string;
while (isspace(*trimmed))
trimmed++;
return trimmed;
}
/* Print the contents of the current directory */
void print_directory()
{
DIR *dir;
struct dirent *dir_entry;
struct stat file_stat;
char buffer[PATH_MAX];
getcwd(buffer, PATH_MAX);
printf("Current directory: %s\n", buffer);
printf("***Directory listing follows***\n");
dir = opendir(".");
while((dir_entry = readdir(dir)) != NULL)
{
if (dir_entry->d_name[0]!='.')
{
if(stat(dir_entry->d_name, &file_stat) != -1)
{
if (file_stat.st_mode & S_IFDIR) /* directory */
printf("Directory: ");
else if (file_stat.st_mode & S_IXUSR) /* executable */
printf("Executable: ");
else
printf("File: ");
printf("%s\n", dir_entry->d_name);
}
}
}
}
/* Execute the file given as an argument */
void execute_file(char *filename)
{
struct stat file_stat;
char buffer[PATH_MAX];
if(stat(filename, &file_stat) != -1)
{
if ( !(file_stat.st_mode & S_IFDIR) && /* not directory */
file_stat.st_mode & S_IXUSR) /* executable */
{
if (strchr(filename, '/') != NULL)
sprintf(buffer, "xterm -hold -e '%s' &", filename);
else
sprintf(buffer, "xterm -hold -e './%s' &", filename);
system(buffer);
}
else
printf("Error: Unable to execute file.\n");
}
else
printf("Error: Unable to execute file.\n");
}
/* Show the contents of the file given as an argument */
void show_file(char *filename)
{
struct stat file_stat;
char buffer[PATH_MAX];
if(stat(filename, &file_stat) != -1)
{
if (!(file_stat.st_mode & S_IFDIR)) /* not directory */
{
if (file_stat.st_size < 500000)
{
sprintf(buffer, "xterm -hold -e 'less %s' &", filename);
system(buffer);
}
else
printf("Error: Could not open text file. File size too large.\n");
}
else
printf("Error: file is a directory.\n");
}
else
printf("Error: Unable to show file contents.\n");
}
int main()
{
int quit;
char buffer[PATH_MAX];
char *command;
print_directory();
printf("\n");
quit = 0;
while(!quit)
{
fgets(buffer, PATH_MAX, stdin);
command = trim(buffer);
if(!strncmp(command, "d:", 2))
{
command = trim(command + 2);
if (command[0] == 0)
strcpy(command, ".");
if( chdir(command) < 0 )
printf("Unable to change to directory: %s\n", command);
else
{
getcwd(buffer, PATH_MAX);
print_directory();
}
}
else if(!strncmp(command, "x:", 2))
{
command = trim(command + 2);
execute_file(command);
}
else if(!strncmp(command, "v:", 2))
{
command = trim(command + 2);
show_file(command);
}
else if(!strncmp(command, "q:", 2))
quit = 1;
else
printf("Invalid command\n");
printf("\n");
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
int error_visible = 0;
/* Show an error message on the error box */
void Error(char *msg)
{
printf("");
printf("",msg);
error_visible = 1;
}
void ClearError()
{
if(error_visible)
printf("");
error_visible = 0;
}
/* Remove spaces at start and end of string */
char *trim(char *string)
{
char *trimmed;
while (isspace(string[strlen(string) - 1]))
string[strlen(string) - 1] = 0;
trimmed = string;
while (isspace(*trimmed))
trimmed++;
return trimmed;
}
/* Print the contents of the current directory */
void print_directory()
{
DIR *dir;
struct dirent *dir_entry;
struct stat file_stat;
char buffer[PATH_MAX];
int n, open;
getcwd(buffer, PATH_MAX);
printf("", buffer);
printf("\n");
dir = opendir(".");
/*count number of entries */
n = 0;
while((dir_entry = readdir(dir)) != NULL)
if (dir_entry->d_name[0]!='.')
if(stat(dir_entry->d_name, &file_stat) != -1)
n++;
if(n>0) /* if there's at least one entry*/
{
printf("d_name[0]!='.')
{
if(stat(dir_entry->d_name, &file_stat) != -1)
{
if((n%2) == 0)
{
printf(" "
" onmousein="
" onmouseout= "
"{"
" }>"
" "
"}>}>", n, dir_entry->d_name, n, n, dir_entry->d_name);
else if (file_stat.st_mode & S_IXUSR) /* executable */
printf( " "
" onmousein= "
" onmouseout= "
"{"
" }>"
" "
"}>}>", n, dir_entry->d_name, n, n, dir_entry->d_name);
else
printf( " "
" onmousein= "
" onmouseout= "
"{"
" }>"
" "
"}>}>", n, dir_entry->d_name, n, n, dir_entry->d_name);
n++;
if((n%2) == 0)
{
printf(">\n");
open=0;
}
}
}
}
if(open)
printf(">");
printf(">}>\n");
}
else
printf("}");
ClearError();
}
/* Execute the file given as an argument */
void execute_file(char *filename)
{
struct stat file_stat;
char buffer[PATH_MAX];
if(stat(filename, &file_stat) != -1)
{
if ( !(file_stat.st_mode & S_IFDIR) && /* not directory */
file_stat.st_mode & S_IXUSR) /* executable */
{
if (strchr(filename, '/') != NULL)
sprintf(buffer, "xterm -e '%s' &", filename);
else
sprintf(buffer, "xterm -e './%s' &", filename);
system(buffer);
ClearError();
}
else
Error("Unable to execute file.");
}
else
Error("Unable to execute file.");
}
/* Show the contents of the file given as an argument */
void show_file(char *filename)
{
struct stat file_stat;
char buffer[PATH_MAX];
if(stat(filename, &file_stat) != -1)
{
if (!(file_stat.st_mode & S_IFDIR)) /* not directory */
{
if (file_stat.st_size < 500000)
{
sprintf(buffer, "xterm -e 'less %s' &", filename);
system(buffer);
ClearError();
}
else
Error("Could not open text file. File size too large.");
}
else
Error("File is a directory.");
}
else
Error("Unable to show file contents.");
}
int main()
{
int quit;
char buffer[PATH_MAX];
char *command;
printf("");
printf("");
printf("");
printf(">");
printf(" {Go to parent directory} >");
printf(" ");
printf("}>");
printf("}> ");
printf("");
printf("}>");
printf("");
printf("}>");
print_directory();
quit = 0;
while(!quit)
{
fgets(buffer, PATH_MAX, stdin);
command = trim(buffer);
if(!strncmp(command, "d:", 2))
{
command = trim(command + 2);
if (command[0] == 0)
strcpy(command, ".");
if( chdir(command) < 0 )
Error("Unable to change to directory");
else
{
getcwd(buffer, PATH_MAX);
print_directory();
}
}
else if(!strncmp(command, "x:", 2))
{
command = trim(command + 2);
execute_file(command);
}
else if(!strncmp(command, "v:", 2))
{
command = trim(command + 2);
show_file(command);
}
else if(!strncmp(command, "q:", 2))
quit = 1;
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
enum Operations {COPY, CUT};
/* Show an error message on the error box */
void Error(char *msg)
{
printf("");
printf("",msg);
printf( ""
""
"");
}
/* Remove spaces at start and end of string */
char *trim(char *string)
{
char *trimmed;
while (isspace(string[strlen(string) - 1]))
string[strlen(string) - 1] = 0;
trimmed = string;
while (isspace(*trimmed))
trimmed++;
return trimmed;
}
/* Print the contents of the current directory */
void print_directory()
{
DIR *dir;
struct dirent *dir_entry;
struct stat file_stat;
char buffer[PATH_MAX];
int n, open;
getcwd(buffer, PATH_MAX);
printf("", buffer);
printf("\n");
printf( ""
"");
dir = opendir(".");
/*count number of entries */
n = 0;
while((dir_entry = readdir(dir)) != NULL)
if (dir_entry->d_name[0]!='.')
if(stat(dir_entry->d_name, &file_stat) != -1)
n++;
if(n>0) /* if there's at least one entry*/
{
printf("d_name[0]!='.')
{
if(stat(dir_entry->d_name, &file_stat) != -1)
{
if((n%2) == 0)
{
printf(" "
" onmousein="
" onmouseout= "
"{"
" }>"
" "
"}>}>", n, dir_entry->d_name, n, n, dir_entry->d_name);
else if (file_stat.st_mode & S_IXUSR) /* executable */
printf( " "
" onmousein= "
" oncontextclick="
" ,, "
" ,, "
" , "
" "
" onmouseout= "
"{"
" }>"
" "
"}>}>", n, dir_entry->d_name, n, dir_entry->d_name, dir_entry->d_name, n, dir_entry->d_name);
else
printf( " "
" onmousein= "
" oncontextclick="
" ,, "
" ,, "
" , "
" "
" onmouseout= "
"{"
" }>"
" "
"}>}>", n, dir_entry->d_name, n, dir_entry->d_name, dir_entry->d_name, n, dir_entry->d_name);
n++;
if((n%2) == 0)
{
printf(">\n");
open=0;
}
}
}
}
if(open)
printf(">");
printf(">}>\n");
}
else
printf("}");
}
/* Execute the file given as an argument */
void execute_file(char *filename)
{
struct stat file_stat;
char buffer[PATH_MAX];
if(stat(filename, &file_stat) != -1)
{
if ( !(file_stat.st_mode & S_IFDIR) && /* not directory */
file_stat.st_mode & S_IXUSR) /* executable */
{
if (strchr(filename, '/') != NULL)
sprintf(buffer, "xterm -e '%s' &", filename);
else
sprintf(buffer, "xterm -e './%s' &", filename);
system(buffer);
}
else
Error("Unable to execute file.");
}
else
Error("Unable to execute file.");
}
/* Show the contents of the file given as an argument */
void show_file(char *filename)
{
struct stat file_stat;
char buffer[PATH_MAX];
if(stat(filename, &file_stat) != -1)
{
if (!(file_stat.st_mode & S_IFDIR)) /* not directory */
{
if (file_stat.st_size < 500000)
{
sprintf(buffer, "xterm -e 'less %s' &", filename);
system(buffer);
}
else
Error("Could not open text file. File size too large.");
}
else
Error("File is a directory.");
}
else
Error("Unable to show file contents.");
}
void paste_file(char *dirname, char *filename, int operation)
{
struct stat file_stat;
char buffer[PATH_MAX];
char inpath[PATH_MAX];
char outpath[PATH_MAX];
getcwd(buffer, PATH_MAX);
if(!strcmp(dirname, buffer)) /* if we are in the same directory */
{
if(operation != CUT) /* moving to the same place */
{
sprintf(inpath, "\"%s/%s\"", buffer, filename);
sprintf(outpath, "\"%s/Copy of %s\"", buffer, filename);
sprintf(buffer, "cp %s %s", inpath, outpath);
system(buffer);
}
}
else
{
sprintf(inpath, "\"%s/%s\"", dirname, filename);
if(stat(filename, &file_stat) == -1) /* file doesn't exist here */
{
if (operation == COPY)
sprintf(buffer, "cp %s .", inpath);
else
sprintf(buffer, "mv %s .", inpath);
}
else
{
if (operation == COPY)
sprintf(buffer, "cp %s \"./Copy of %s\"", inpath, filename);
else
sprintf(buffer, "mv %s \"./Copy of %s\"", inpath, filename);
}
system(buffer);
}
if(operation == CUT)
printf("");
}
int main()
{
int quit;
char buffer[PATH_MAX];
char *command;
char sel_dir[PATH_MAX];
char sel_file[PATH_MAX];
int operation; /**/
printf("");
printf("");
printf("");
printf(">");
printf(" {Go to parent directory} >");
printf(" ");
printf(" ");
printf(" , ");
printf(" {Cut} ");
printf(" >");
printf(" , ");
printf(" {Copy} ");
printf(" >");
printf(" ");
printf(" {Paste} ");
printf(" >");
printf("}>");
printf("}> ");
printf("");
printf("}>");
printf("");
printf("}>");
print_directory();
quit = 0;
while(!quit)
{
fgets(buffer, PATH_MAX, stdin);
command = trim(buffer);
if(!strncmp(command, "d:", 2))
{
command = trim(command + 2);
if (command[0] == 0)
strcpy(command, ".");
if( chdir(command) < 0 )
Error("Unable to change to directory");
else
print_directory();
}
else if(!strncmp(command, "x:", 2))
{
command = trim(command + 2);
execute_file(command);
}
else if(!strncmp(command, "v:", 2))
{
command = trim(command + 2);
show_file(command);
}
else if(!strncmp(command, "k:", 2)) /* cut */
{
operation = CUT;
}
else if(!strncmp(command, "c:", 2)) /* copy */
{
operation = COPY;
}
else if(!strncmp(command, "p:", 2)) /* paste */
{
paste_file(sel_dir, sel_file, operation);
print_directory();
}
else if(!strncmp(command, "f:", 2)) /* file selected */
{
command = trim(command + 2);
getcwd(sel_dir, PATH_MAX);
strcpy(sel_file, command);
}
else if(!strncmp(command, "q:", 2))
quit = 1;
}
return 0;
}