Hello i wrote my c program which will be run on linux.
I am trying to make my own shell for linux.
I have the following code below...
#include <limits.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
int main(void){
int i = 0;
int k = 0;
int argsCount = 0;
char inputBuffer[MAX_LINE]; /*buffer to hold command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2 + 1]; /*command line arguments */
pid_t tpid ;
pid_t child_pid;
int child_status;
char path[PATH_MAX+1];
char *progpath = strdup(args[0]);
char *prog = basename(progpath);
char temp[MAX_LINE];
}
It'is compiling well but when i try to run the code it gives me segmentation fault error
How can i fix it and why i take this error?
Your main has a wrong signature. You want
int main(int argsCount, char**args) {
and of course you should remove the internal declaration of argCount & args inside your main.
Perhaps you want instead your args & argCount to contain the parsed arguments of your own shell (but you still have to give a good signature to your main, conventionally and very often int main(int argc, char**argv).... you probably want your shell to accept the -c argument as most shells do, this would ease debugging with simplistic test cases). Then you should initialize them, and you should read some line (probably with getline) in a loop.
As I commented, you should compile with all warnings & debug info:
gcc -Wall -Wextra -g yoursource.c -o yourprog
Then use gdb ./yourprog to debug your program (see GDB documentation). valgrind should also be helpful. Of course, be sure to develop on a Linux system!
BTW, your program is not a convincing start for a shell. Use strace on some existing shell to understand what a shell needs to do. Study the source code of some existing free software shell (e.g. sash, fish, GNU bash ...). Read Advanced Linux Programming
Related
For a reason I've made an static binary and rewrote it on /bin/bash. The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <fcntl.h>
int main(int argc, char *argv[]){
char *realbin, *curbin;
FILE *f;
printf("Hello there!\n");
curbin = basename(argv[0]);
realbin = malloc(strlen(curbin) + 9);
sprintf(realbin, "/bin/%s.cp", curbin); // Here , base name is "bash" so the string would be "/bin/bash"
execvp(realbin, argv); // Run /bin/bash with current process env and args
The binary loads completely okay when the system is booted up. I don't understand why display manager crashes even though I've managed to use exec. How is display manager's(In my case, gdm) procedure affected ? Do you have any suggestion how I can somehow stop it work this way?
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define read_buffsize 1024
#define split_buffsize 64
#define delimiters "\t\r\n\a"
int main();
void loop(void);
char *readLine(void);
char **splitLine(char *command);
int start(char** args);
int execute(char **args);
I am trying to make a shell program in c. The program runs but when I type in a command and execute it says 'lsh:no such file or directory.' I put my function declarations int his question only because my code is about 150 lines of code. Example out put is shown below:
> ls -l
lsh: No such file or directory
> pwd
lsh: No such file or directory
>
Perhaps you do not have the program lsh installed. But lsh is an implementation of the SSH protocol. Here is a Wikipedia desciption. I think that lsh is not even an alternative to bash or sh. It just for logging into remote computers. Thus lsh does not know how to execute it (if you have it installed). Try to make the executable string (or #define) be bash not lsh.
I tried use tkill ,to kill some thread with c code .
What are the library I need to include to use this function?
Running on linux ubuntu
int main(int argc , char* argv[])
{
tkill(123,9);
}
You neeed syscall(SYS_tkill, ThePid, TheSignal).
Example:
#include <unistd.h>
#include <sys/syscall.h>
#include <signal.h>
int main()
{
//kills self:
pid_t tid = syscall(SYS_gettid);
syscall(SYS_tkill,tid,SIGTERM);
return 0;
}
Glibc has had a policy of not including wrappers for non-POSIX syscalls.
They may be changing it in current/future versions but using the generic syscall syscall-making function should always work on Linux.
I want to implement RSA-Signature where I can calculate the signature of a file and verify a signature of a file. Therefore I use the libary gmp.
But when I want to print the data of the file, it always prints 0 even though the file is not empty. Here is my code:
//compiled with gcc rsa-sig.c -O3 -Wall -l gmp -o rsa-sig
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include <gmp.h> /* GNU Multi Precision library */
int main(int argc, char *argv[])
{
int r;
mpz_t modulus;
mpz_init(modulus);
r=mpz_set_str(modulus, "FFFF",16); //just an example modulus
mpz_t data;
mpz_init(data);
FILE * stream;
stream = fopen("file.bin", "rb");
r= mpz_inp_str(data, stream,16);
mpz_mod(data,data,modulus);
gmp_printf("%Zd\n",data);
}
I can't figure out why the output of that is 0. Maybe one of you guys have an idea.
Thanks!!
i want to write a program which executes the command passed in argument to a .c file, but i'm having incomprehensible errors coming from the declaration of an array of char pointers; my code is the following:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
if(argc < 2)
{
printf("Incorrect number of arguments.\n");
return -1;
}
char* args[1 + argc - 2]; // this is causing the problem
char cmd[10];
/*
* some operations...
*/
char* test[] = {"ps","-l","NULL"};
execv("/bin/ps",test);
return 0;
}
the arguments of execv should normally be cmd and args, but it didnt work, so i tried a simple command, didnt work either, i'm getting the following error:
error: unsupported option (BSD syntax)
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).
however, if i put 1 as the size of the args array, the ps command is executed... i'm pretty confused, and i'd appreciate any kind of help