Building a simple Shell for linux

AI Thread Summary
The discussion focuses on building a Linux shell in C that can recognize and execute a command called 'copy2flash'. The user seeks guidance on how to implement a case instruction to identify this command and print a message indicating the copying process. The provided code snippet initializes a shell environment, reads user input, and suggests using string comparison functions like strcmp() to facilitate command recognition. The user is specifically looking for advice on handling character arrays to capture and compare the input command effectively.
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;
}
 
Technology news on Phys.org
Use the string comparison methods like strcmp().

- Warren
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top