my cp cudzyslow showtree

This commit is contained in:
Jakub Adamski 2019-01-17 13:21:50 +01:00
parent 4ee50dda46
commit af2bdfb47a
3 changed files with 65 additions and 7 deletions

View File

@ -1 +1,2 @@
hdhd To jest plik1.txt
jajajajaj

Binary file not shown.

View File

@ -9,6 +9,7 @@
#include <pwd.h> #include <pwd.h>
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
#include <fcntl.h>
const int max = 1000; const int max = 1000;
@ -24,6 +25,15 @@ void cut(char **argv, char *command){
argv[w]=NULL; argv[w]=NULL;
} }
void rmChar(char *str, char garbage) { /*przesledzic jak to dziala!!!!! w opisie sobie napisac*/
char *src, *dst;
for (src = dst = str; *src != '\0'; src++) {
*dst = *src;
if (*dst != garbage) dst++;
}
*dst = '\0';
}
void cd (char **argv){ void cd (char **argv){
if(argv[1] == NULL){ if(argv[1] == NULL){
printf("Add arguments\n"); printf("Add arguments\n");
@ -94,17 +104,20 @@ void tree (char **argv, char * directory){
printf("Too many arguments\nshowtree takes no arguments \nor exacly 1 argument for desired directory\n"); printf("Too many arguments\nshowtree takes no arguments \nor exacly 1 argument for desired directory\n");
return; return;
} }
else if (argv[1] != NULL && argv[2] == NULL){
printf("Dokladnie 1 arg\n");
return;
}
else{ else{
if (fork() == 0){ if (fork() == 0){
DIR *d; DIR *d;
struct dirent *ent; struct dirent *ent;
char ndr[max]; char ndr[max];
int tab = 0; int tab = 0;
strcpy(ndr, directory);
if (argv[1] != NULL && argv[2] == NULL){ /*jeden argument*/
cd(argv);
getcwd(ndr, sizeof(ndr));
}
else
strcpy(ndr, directory);
printf("Content of: %s\n", ndr);
if ((d = opendir(ndr)) != NULL){ /*wchodzimy do katalogu*/ if ((d = opendir(ndr)) != NULL){ /*wchodzimy do katalogu*/
while ((ent = readdir(d)) != NULL){ /*czytamy zawartosc*/ while ((ent = readdir(d)) != NULL){ /*czytamy zawartosc*/
@ -140,8 +153,43 @@ void tree (char **argv, char * directory){
} }
} }
void mycp (char **argv){
if (argv[1] == NULL || argv[2] == NULL){
printf("Add arguments\n");
return;
}
else if (argv[3] != NULL){
printf("Too many arguments\n");
return;
}
else{
int file1 = open(argv[1], O_RDONLY);
if (file1 == -1){
perror("open error");
return;
}
if (open(argv[2], O_RDONLY) != -1){
printf("%s already exists!\n", argv[2]);
return;
}
int newfile = creat(argv[2], 0666);
if (newfile == -1){
perror("create error");
return;
}
char bufor[1024];
int r;
while ((r = read(file1, bufor, sizeof(bufor))) > 0)
write(newfile, bufor, r);
close(file1);
close(newfile);
}
}
int main (){ int main (){
int spath = 1; int spath = 1, i=0;
using_history(); using_history();
struct passwd *p = getpwuid(getuid()); struct passwd *p = getpwuid(getuid());
@ -176,6 +224,11 @@ int main (){
add_history(komenda); add_history(komenda);
cut(argv, komenda); cut(argv, komenda);
while (argv[i] != NULL){
rmChar(argv[i], 34);
i+=1;
}
i=0;
if (argv[0]==NULL) if (argv[0]==NULL)
continue; continue;
@ -195,6 +248,10 @@ int main (){
tree(argv, directory); tree(argv, directory);
continue; continue;
} }
else if (!strcmp(argv[0], "mycp")){
mycp(argv);
continue;
}
else if (!strcmp(argv[0], "exit")) else if (!strcmp(argv[0], "exit"))
break; break;
else else