ok, i have been working on this unix emulator for a few days. i have to hand it in tomorrow (gulp)
it takes in a unix command from the user such as 'cat' which displays the contents of a file or 'mv' which renames a file, and is supposed to process the instuctions.
i am new to this and have taken it as far as i can. i have had a lot of help from the people who contribute on this site and i'd like to say a big thank you to you all whoever you are! : )
alas, the actual program is diabolical! i had to laugh when i saw the amount of errors i got when i tried to run it! (it was either that or cry!)
anyhow below is the code, please feel free to howl with laughter at my ignorance of the c language. if you would like to point out my major mistakes id really appreciate it (if you find over 100 i'll get the noose ready!)
now, where's that application form for macdonalds........
jim.
i couldn't figure out how to view the contents of a folder...(ls) so i've omitted that.
#include <stdio.h>
#include <conio.h>
#include <dos>.h>
#include <stdlib.h>
#include <string.h>
void CopyFile();
void GetFileName(char []);
void main (void)
{
char command [7] [4];
strcpy(command[0],"date");
strcpy(command[1],"time");
strcpy(command[2],"ls");
strcpy(command[3],"cat");
strcpy(command[4],"rm");
strcpy(command[5],"cp");
strcpy(command[6],"mv");
char selection [255];
char file[80];
char oldname[80], newname[80];
clrscr();
printf("This is a Unix Operating System Emulator");
printf("$");
scanf("%s", &selection);
if strcmp(selection, command [0]) == true;
date();
else if strcmp(selection, command [1]) == true;
time();
//else if strcmp(selection, command [2]) == true;
ls();
else if strcmp(selection, command [3]) == true;
cat();
else if strcmp(selection, command [4]) == true;
rm();
else if strcmp(selection, command [5]) == true;
cp();
else if strcmp(selection, command [6]) == true;
mv();
else {
printf("Error!");
}
time(){
struct dostime_t t;
_dos_gettime(&t);
printf("The current time is: %2d:%02d:%02d.%02d\n", t.hour, t.minute,
t.second, t.hsecond);
}
date(){
struct date d;
getdate(&d);
printf("The current year is: %d\n", d.da_year);
printf("The current day is: %d\n", d.da_day);
printf("The current month is: %d\n", d.da_mon);
}
cat(){
FILE *fptr, *fopen();
ftpr = fopen("filename", "r");
}
rm(){
/* prompt for file name to delete */
printf("File to delete: ");
gets(file);
/* delete the file */
if (remove(file) == 0)
printf("Removed %s.\n",file);
else
perror("remove");
}
cp(){
{
CopyFile();
return 0;
}
void GetFileName(char filename[])
{
fgets(filename,256,stdin);
filename[ strlen(filename) -1 ] = '\0';
}
void CopyFile(){
char source[256];
char dest[256];
int character;
printf("Enter source filepath: ");
GetFileName(source);
FILE *fsource = fopen(source,"r");
if(fsource == NULL)
{
printf("Source File Does Not Exist!");
exit(1);
}
printf("Enter destination filepath: ");
GetFileName(dest);
FILE *fdest = fopen(dest,"w");
while( fscanf(fsource,"%c", &character) != EOF)
{
fprintf(fdest, "%c", character);
}
printf("File copied sucessfully");
}
mv(){
char oldname[80], newname[80];
/* prompt for file to rename and new name */
printf("File to rename: ");
gets(oldname);
printf("New name: ");
gets(newname);
/* Rename the file */
if (rename(oldname, newname) == 0)
printf("Renamed %s to %s.\n", oldname, newname);
else
perror("rename");
}
return 0;
}