нужно написать свой cd все работает прога гулят по папочкам тока вот почему-то stdout кудато делся...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
void detect_curpath(char* curpath);
int isdir(char* pathtogo,char* curpath);
void add(char* s1, char* s2);
int main()
{
int out=dup(1);
char* curpath;
curpath=(char*)malloc(sizeof(char)*1024);
detect_curpath(curpath);
dup2(out,1);
printf("%s :",curpath);
char pathtogo[256];
gets(pathtogo);
while (strcmp(pathtogo,"exit"))
{
if (strlen(pathtogo)!=0)
{
int out=dup(STDOUT_FILENO);
int i=strlen(curpath)-1;
if (!strcmp(pathtogo,".."))
{
while (curpath[i]!='/')
{
curpath[i]='\0';
i--;
}
curpath[i]='\0';
}
else
if (pathtogo[0]=='/') {if (isdir(pathtogo,curpath)==1) strcpy(curpath,pathtogo);else printf("Error : Not a Directory\n");}
else {if (isdir(pathtogo,curpath)==1) add(curpath,pathtogo); else printf("Error : Not a Directory\n");}
dup2(out,STDOUT_FILENO);
}
fprintf(stdout,"%s :",curpath);
gets(pathtogo);
}
return 0;
}
void add(char* s1, char* s2)
{
int i,len1=strlen(s1);
s1[len1]='/';
for (i=0;i<strlen(s2);i++)
{
s1[len1+i+1]=s2[i];
}
}
int isdir(char* pathtogo,char* curpath)
{
int out=dup(STDOUT_FILENO);
char tmp[1024]="";
strcpy(tmp,curpath);
add(tmp,pathtogo);
freopen("tmp","w",stdout);
if (fork()==0)
{
char *cmd[]={"stat","--printf=%F",tmp,(char*)0};
execvp(cmd[0],cmd);
}
wait(getppid());
dup2(out,STDOUT_FILENO);
int stdIn=dup(STDIN_FILENO);
freopen("tmp","r",stdin);
char* type;
type=(char*)malloc(sizeof(char)*1024);
gets(type);
dup2(stdIn,STDIN_FILENO);
if (fork()==0)
{
char *ncmd[]={"rm","tmp",(char*)0};
execvp(ncmd[0],ncmd);
}
wait(getppid());
if (!strcmp(type,"Каталог")) return 1; else return 0;
}
void detect_curpath(char* curpath)
{
int out=dup(STDOUT_FILENO);
freopen("tmp","w",stdout);
if (fork()==0)
{
char *cmd[]={"pwd",(char*)0};
execvp(cmd[0],cmd);
}
wait(getppid());
dup2(out,STDOUT_FILENO);
int stdIn=dup(STDIN_FILENO);
int fd=open("tmp",O_RDONLY);
dup2(fd,STDIN_FILENO);
gets(curpath);
dup2(stdIn,STDIN_FILENO);
if (fork()==0)
{
char *ncmd[]={"rm","tmp",(char*)0};
execvp(ncmd[0],ncmd);
}
wait(getppid());
}