Building a simple Shell for linux

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 8K views
FrostScYthe
Messages
80
Reaction score
0
Hi,

I'm trying to build a shell for Linux, programming it in c currently. I need to be able to read a command like 'copy2flash' so that it'll copy itself to another flash memory, but right now I need the case instruction to be able to recognize the command and print something like "copying to device" I'm not sure how to do this with an array of chars?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>

#define MAX 80 /* 80 chars per line, per command, should be enough. */

int main(int argc, char *argv[], char *envp[])
{
/* Inicializa a c y tmp. */
char string [MAX];
char c = '\0';
char tmp = '\0';
printf("Welcome to MySHELL, use 'copy2flash' to copy flash memory\n");
printf("to another flash memory to another system\n");
printf("[MySHELL] ");
while(c != EOF) {
c = getchar();
switch(c) {
case '\n': /* parse y ejecute. */
printf("[MySHELL] ");
break;
/**** case 'copy2flash': parse y ejecute.
printf("Copying to another flash...");
bzero(&tmp, 1);
break; */
default: strncat(&tmp, &c, 1);
break;
}
}
/* some processing before terminating. */
return 0;
}
 
Physics news on Phys.org