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 <readline/readline.h>
#include <readline/history.h>
#include <fcntl.h>
const int max = 1000;
@ -24,6 +25,15 @@ void cut(char **argv, char *command){
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){
if(argv[1] == NULL){
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");
return;
}
else if (argv[1] != NULL && argv[2] == NULL){
printf("Dokladnie 1 arg\n");
return;
}
else{
if (fork() == 0){
DIR *d;
struct dirent *ent;
char ndr[max];
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*/
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 spath = 1;
int spath = 1, i=0;
using_history();
struct passwd *p = getpwuid(getuid());
@ -176,6 +224,11 @@ int main (){
add_history(komenda);
cut(argv, komenda);
while (argv[i] != NULL){
rmChar(argv[i], 34);
i+=1;
}
i=0;
if (argv[0]==NULL)
continue;
@ -195,6 +248,10 @@ int main (){
tree(argv, directory);
continue;
}
else if (!strcmp(argv[0], "mycp")){
mycp(argv);
continue;
}
else if (!strcmp(argv[0], "exit"))
break;
else