C program Very important: Your source program must consist of multiple source fi
ID: 3839253 • Letter: C
Question
C program
Very important: Your source program must consist of multiple source files and you must also have a makefile. Specific structural requirements are stated elsewhere in this handout. All of these files must be submitted together using the turnin-csi402 command. Instructions for using turnin-csi402 and additional specifications for the makefile will be discussed later in this hand out. Programs that produce compiler/linker errors will receive a grade of zero. It is also worth reminding everyone that collaboration with other students is not allowed. Obtaining help from anyone other than the instructor or TAs is also prohibited. Please review the section in the syllabus regarding academic integrity. Violation of this policy will result in a failing grade for the course. Introduction In this assignment, you will develop your own sinmplified command-line shell, similar to bash (the shell used on most modern Unix systerns) In general, a shell is a user interface built on top of an operating system. Most modern sys tems such as Windows, MacOS, and Ubuntu Linux include a graphical user interface (GUI) shell called a windowing system. However, many of these systems also include a terminal emulator which grants access to a classic command-line shell. As we've seen on ITSUnix, a command-line shell is a text based user interface in which the user types commands in order to interact with the system. Each shell has its own command language. The bash (Bourne Again Shell) has a particular set of commands that it accepts (most modern shells follow a similar convention to bash). Instead of accepting one command at a time, many shells also accept scripts as input A script is simply a sequence of commands stored in a file, to be executed one after another. Our shell will function in this way (it will interpret script files, in addition to accepting one command at a time from the keyboard)Explanation / Answer
shell402.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
char* getLine(FILE* stream);
void main(int argc, char** argv)
{
int exit1 =0;
//CHANGE AFTER TESTING; TOO LARGE
char * buffer = malloc(sizeof(char) * 1000);
if(argc > 2)
{
fprintf(stderr, "Wrong number of commands. ");
exit(1);
}
while(exit1 == 0)
{
if(argc==1)
{
printf("shell402>>");
fgets(buffer, 100, stdin);
}
else
{
FILE * tempfile;
char * filename = malloc(sizeof(char) * strlen(argv[1]));
strcpy(filename, argv[1]);
if(tempfile = fopen(filename, "r") == NULL)
{
fprintf(stderr, "File could not be opened. ");
exit(1);
}
while(!feof(tempfile))
{
buffer = getLine(tempfile);
}
}
}
}
shellFunctions.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <limits.h>
void wd()
{
fprintf(stdout, "%s", getcwd(".", PATH_MAX +1));
}
void chwd(char* path)
{
int success =0;
if((success = chdir(path)) == -1)
{
fprintf(stderr, "The directory couldn't be opened");
exit(1);
}
}
int quit()
{
fprintf(stdout, "Goodbye ");
return 1;
}
create.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
int main(int argc, char ** argv)
{
if(argc == 3 || argc == 4)
{
if(argc == 3)
{
if(argv[1][1] == 'f')
{
char * filepath = malloc(sizeof(char) * strlen(argv[2]));
strcpy(filepath, argv[2]);
int newFile;
if(newFile = open(filepath, O_RDWR | O_CREAT, 0644) == -1)
{
fprintf(stderr, "Open failed. ");
exit(1);
}
}
else if(argv[1][1] == 'd')
{
char * dirpath = malloc(sizeof(char) * strlen(argv[2]));
strcpy(dirpath, argv[2]);
int newDir;
if((newDir = mkdir(dirpath, 0750)) == -1)
{
fprintf(stderr, "Directory open fail");
exit(1);
}
}
else
{
fprintf(stderr, "Incorrect option selected");
exit(1);
}
}
else
{
char * oldname = malloc(sizeof(char) * strlen(argv[2]));
char * newname = malloc(sizeof(char) * strlen(argv[3]));
strcpy(oldname, argv[2]);
strcpy(newname, argv[3]);
int linky;
if(argv[1][1] == 'h')
{
if((linky = link(oldname, newname)) == -1)
{
fprintf(stderr, "The paths couldn't be linked. ");
exit(1);
}
}
else if(argv[1][1] == 's')
{
if((linky = symlink(oldname, newname))== -1)
{
fprintf(stderr, "Couldn't make the symbolic link ");
exit(1);
}
}
else
{
fprintf(stderr, "option is incorrect.");
exit(1);
}
}
}
else
{
fprintf(stderr, "The number of args is incorrect. ");
exit(1);
}
}
input.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
char* getLine(FILE* stream)
{
char * buffer = malloc(sizeof(char) * 256);
char curr;
int loopy =0;
fscanf(stream, "%c", &curr);
while(curr = ' ')
{
buffer[loopy++] = curr;
fscanf(stream, "%c", &curr);
}
buffer[loopy] = curr;
return buffer;
}
list.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
void main(int argc, char** argv)
{
char * pathname = malloc(sizeof(char) *1000);
char mode;
DIR * currDIR;
struct dirent * dir_dirent;
struct stat info;
//make sure correct number of command line arguments
if(argc >0 && argc <=3)
{
if(argc == 2)
{
mode = argv[1][1];
if(mode == 'i')
{
//TODO: information of current path
pathname = ".";
currDIR = opendir(pathname);
while((dir_dirent = readdir(currDIR)) != NULL)
{
char * filepath = malloc(sizeof(char)* strlen(dir_dirent->d_name) + (sizeof(char) * strlen(pathname) + 2));
strcat(filepath, pathname);
strcat(filepath,"/" );
strcat(filepath, dir_dirent->d_name);
if(stat(filepath, &info) == -1)
{
fprintf(stderr,"Couldn't get stat on file");
exit(1);
}
fprintf(stdout, "%s Size:%d Inode:%d Permissions:%d ", dir_dirent->d_name, info.st_size, info.st_ino, info.st_mode);
}
}
else if(mode =='h')
{
//TODO: show hidden files in current path
pathname = ".";
currDIR = opendir(pathname);
while((dir_dirent =readdir(currDIR))!=NULL)
{
if(dir_dirent->d_name[0] == '.')
{
fprintf(stdout, "%s ", dir_dirent->d_name);
}
}
}
else
{
//TODO: print all non-hidden files in specified filename
strcpy(pathname, argv[1]);
currDIR = opendir(pathname);
while((dir_dirent = readdir(currDIR))!=NULL){
if(dir_dirent->d_name[0] != '.')
{
fprintf(stdout, "%s ", dir_dirent->d_name);
}
}
}
}
else if(argc == 3)
{
mode = argv[1][1];
if(mode == 'i')
{
//TODO: show information at specified pathname
strcpy(pathname, argv[2]);
if(stat(pathname, &info) == -1)
{
fprintf(stderr,"Couldn't get stat on file");
exit(1);
}
currDIR = opendir(pathname);
while((dir_dirent = readdir(currDIR)) != NULL)
{
char * filepath = malloc(sizeof(char)* strlen(dir_dirent->d_name) + (sizeof(char) * strlen(pathname) + 2));
strcat(filepath, pathname);
strcat(filepath,"/" );
strcat(filepath, dir_dirent->d_name);
if(stat(filepath, &info) == -1)
{
fprintf(stderr,"Couldn't get stat on file");
exit(1);
}
fprintf(stdout, "%s Size:%d Inode:%d Permissions:%d ", dir_dirent->d_name, info.st_size, info.st_ino, info.st_mode);
}
}
else if(mode == 'h')
{
//TODO: show hidden files in specified path
strcpy(pathname, argv[2]);
currDIR = opendir(pathname);
while((dir_dirent =readdir(currDIR))!=NULL)
{
if(dir_dirent->d_name[0] == '.')
{
fprintf(stdout, "%s ", dir_dirent->d_name);
}
}
}
else
{
//Shouldn't come here. Chose a wrong mode
fprintf(stderr, "The list mode must be <-i> or <-h>");
exit(1);
}
}
else
{
//only one option with 1 argc. Print non-hidden files in current directory
pathname=".";
currDIR = opendir(pathname);
while((dir_dirent = readdir(currDIR))!=NULL)
{
if(dir_dirent->d_name[0] != '.')
{
fprintf(stdout, "%s ", dir_dirent->d_name);
}
}
}
}
else
{
fprintf(stderr, "The wrong number of arguments are inc");
}
}
token.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* getTokened(char* original, char token)
{
char* returnString = malloc(strlen(original) * sizeof(char));
int i=0;
while(original[i] != token)
{
returnString[i] = original[i];
i++;
}
returnString[i] = '';
return returnString;
}
char* getRemaining(char* original, char* split)
{
char* string = malloc(strlen(original) * sizeof(char));
int length = strlen(split) +1;
int i=0;
for(;length < strlen(original); length++)
{
string[i] = original[length];
i++;
}
return string;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.