Skip to content

Instantly share code, notes, and snippets.

@auriza
Created March 3, 2017 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auriza/35eee010c999e93ff660043fa32d6cf8 to your computer and use it in GitHub Desktop.
Save auriza/35eee010c999e93ff660043fa32d6cf8 to your computer and use it in GitHub Desktop.
/*
Shell
=====
Shell adalah program yang membuat proses child untuk menjalankan string perintah yang diberikan.
Shell berupa infinite looping yang berisi siklus fork--exec--wait.
Lengkapilah kode berikut ini untuk membuat program shell sederhana!
Contoh masukan dan keluaran
---------------------------
$ date
Tue Dec 9 13:34:17 WIB 2014
$ whoami
auriza
$ exit
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
// pecah string per kata
void split_words(char *string, char **words)
{
int i;
words[0] = strtok(string, " \n");
for (i = 0; words[i]; i++)
words[i+1] = strtok(NULL, " \n");
}
int main()
{
char cmd[80]; // string perintah
char *args[20]; // argumen string perintah
while (1)
{
// cetak prompt "$ "
// baca string perintah
// pecah string perintah per argumen
// jika perintah = "exit"
// break
// jika perintah = "cd"
// ganti direktori
// continue
// buat proses child:
// exec string perintah
// cetak pesan error
// keluar dengan kode -1
// tunggu child selesai
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment