Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Plain Text by lukaszmaly ( 14 years ago )
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
/*
TO_DO:
-poprawić env(ostatni składnik to null
*/
#define MAX_ARG 4
extern char **environ;
void print_env()
{
int i=0;
while(*environ[i++]!=0)
printf("%s \n",environ[i]);
}
void print_finger(char nazwa[25])
{
struct passwd *p;
p=getpwnam(nazwa);
printf("\n Nazwa: %s\n",p->pw_name);
printf("Info %s\n",p->pw_gecos);
printf("Shell: %s\n",p->pw_shell);
printf("Katalog domowy: %s\n",p->pw_dir);
printf("UID: %d\n",p->pw_uid);
printf("GID: %d\n",p->pw_gid);
}
int main(void)
{
int w;
char input[100];
char *p;
int done=0;
int ile_p;
// Do some error checking on the call to fgets
while(done==0)
{
printf("Komenda: ");
ile_p=0;
if (fgets(input, 100, stdin) == NULL)
{
perror("fgets");
exit(EXIT_FAILURE);
}
// Check if there is a newline in the buffer. If there is, get rid of it.
if ((p = strchr(input, '\n')) != NULL)
{
*p = '\0';
}
char args[MAX_ARG][15];
*args[1]=(char)0;
int a = 0;
int i = 0;
int j,k = 0;
int index = 0;
for(k=0; k<MAX_ARG; k++)
for( j=0; j<sizeof(args[index]); j++) args[k][j]='\0';
while (input[a] != '\0')
{
if (input[a] == ' ')
{
args[index][i] = '\0';
i = 0;
a++;
index++;
ile_p++;
} // SPACE detected
args[index][i++] = input[a++];
}
args[index][i] = '\0';
if(strcmp((args[0]),"quit")==0)
{
d
printf("Wychodze");
}
else if(strcmp((args[0]),"env")==0)
{
w=fork();
if(w==0)
print_env();
else
waitpid(w, NULL, 0);
}
else if(strcmp((args[0]),"finger")==0 && ile_p==1)
{
w=fork();
if(w==0)
print_finger(args[1]);
else
waitpid(w, NULL, 0);
}
else
{
w=fork();
if(w==0)
{
switch(ile_p)
{
case 0:
execlp(args[0],args[0],NULL);
break;
case 1:
execlp(args[0],args[0],args[1],NULL);
break;
case 2:
execlp(args[0],args[0],args[1],args[2],NULL);
break;
}
}
else
{
waitpid(w, NULL, 0);
}
}
}
return 0;
}
Revise this Paste
Parent: 55916