Skip to content

Instantly share code, notes, and snippets.

@pasali
Last active December 15, 2015 14:09
Show Gist options
  • Save pasali/5272260 to your computer and use it in GitHub Desktop.
Save pasali/5272260 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
#include<readline/readline.h>
#include<readline/history.h>
#include<sys/wait.h>
/* pasali */
static char * arguments[10];
static char * komut;
void argv_free(void);
int run(void);
int parse(char *);
int
main(void)
{
while(1) {
komut = readline("#> ");
if (komut == NULL) {
exit(EXIT_SUCCESS);
}else if (!strcmp(komut, "")) {
continue;
}
else {
parse(komut);
run();
argv_free();
}
free(komut);
}
atexit(argv_free);
return 0;
}
void
CD(void)
{
if (!strcmp(arguments[1], "~") || !strcmp(arguments[1], " ") || !strcmp(arguments[1], "")) {
chdir(getenv("HOME"));
}else if (!strcmp(arguments[1], ".")) {
/* none */
}else if (!strcmp(arguments[1], "..")) {
char * parent_directory = strrchr(getenv("PWD"), '/');
char * pwd = getenv("PWD");
int index = parent_directory - pwd;
pwd[index] = '\0';
chdir(pwd);
}else
chdir(arguments[1]);
main();
}
void
argv_free(void)
{
int i;
for (i = 0; arguments[i]; i++) {
free(arguments[i]);
}
}
int
run(void)
{
int status;
pid_t pid;
pid = fork();
if (!pid) {
int ret;
if ( **arguments == '/')
ret = execv(arguments[0], arguments);
else
ret = execvp(arguments[0], arguments);
if (ret == -1) {
perror("execv");
exit(EXIT_FAILURE);
}
} else if (pid == -1)
perror("fork");
if (waitpid(-1, &status, 0) == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
return 0;
}
int
parse(char *string)
{
char ** str_p = &string;
char * token;
int len;
add_history(string);
for (len = 0; (token = strsep(str_p, " \t")); len++) {
arguments[len] = strdup(token);
}
arguments[len] = NULL;
if (!strcmp(arguments[0], "exit") || !strcmp(arguments[0], "quit")) {
exit(EXIT_SUCCESS);
}
if (!strcmp(arguments[0], "cd")) {
CD();
}
if (komut[0] != '/' && strchr(komut, '/')) {
printf("full path of program or just name !!!\n");
main();
}
return 0;
}
@roktas
Copy link

roktas commented Mar 31, 2013

Yavrum 10 sn'lik bir işlemden bahsediyoruz:

$ git clone [email protected]:5272260.git # hala klonlamamışsan
$ cd 5272260
$ astyle --style=linux *.c
$ git commit -a -m "stil düzelt"
$ git push origin master

Bundan sonra stili bozuk bir koda review yapmayacağım, notu düşüreceğim. Duyan duymayana bildirsin. Başka türlü anlatamıyorum meseleyi size.

@roktas
Copy link

roktas commented Mar 31, 2013

Peki soru? Kullanıcı programdan çıkarken kullanılan belleğin boşaltılmasını nasıl garanti altına alırız (her ne kadar İşletim Sistemi bu işlemi her sonlanan proses için yapsa bile)... İpucu: atexit

@pasali
Copy link
Author

pasali commented Apr 1, 2013

@roktas hocam, ben stili elimin alışması bakımından söylemiştim tam ifade edememişim kendimi :) Bellekle ilgili olarak argv_free() zaten istenileni yapmıyor mu hocam ? Orayı anlayamadım.

@roktas
Copy link

roktas commented Apr 2, 2013

exit işlevi çağrıldığı noktada program sonlanır; argv_free'nin çağrıldığı noktaya dikkat et ;-)

@pasali
Copy link
Author

pasali commented Apr 2, 2013

Onuda ekledim hocam. Son bir cd işlevi kaldı onu bu akşamlık pes ettim yarın o da inşallah çalışacak :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment