c - my execv() function not working in linux ubuntu -
i wrote next code output: "error!" (the execv function not scheduled return)
what doing wrong???
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <math.h> #include <string.h> #include <malloc.h> #include "lineparser.h" #define location_len 200 char* getl(void); int main(int argc,char *argv[]) { char *loc = getl(); char *args[] = {loc,"ls",null}; int i; execv(args[0],args); printf("error!"); free(loc); } char* getl(void) { char *buff = (char**)malloc(sizeof(char)*location_len); getcwd(buff,location_len); homecoming buff; }
read documentation of execv(3) , of execve(2) , of perror(3). @ least, should code
int main(int argc,char *argv[]) { char *loc = getl(); char *args[] = {loc,"ls",null}; int i; execv(args[0],args); perror("execv"); free(loc); }
you should compile gcc -wall -g
utilize gdb
debugger.
your usage of execv
wrong (you need total path, e.g. "/bin/ls"
, , order of arguments wrong). want exevcp(3) , should in fact code @ least:
char*args= {"ls", loc, null); execvp ("ls", args); perror("execvp")
if insist on using execv(3) try
char*args= {"ls", loc, null); execv ("/bin/ls", args); perror("execv")
i don't understand code supposed do. might interested glob(7) & glob(3).
you should read advanced linux programming. seems there several concepts don't understand enough. guess strace(1) useful (at to the lowest degree running strace ls *.c
understand happening).
maybe getl
gnu function get_current_dir_name(3) doing, (char**)
cast within grossly wrong. , should improve clear buffer buff
using memset(3) before calling getcwd(2) (and should test against failure of ̀ mallocand of
getcwd`)
perhaps want opendir(3), readdir(3), asprintf(3), stat(2); these, avoid running ls
if coding shell, should strace
existing shell, , after having read references giving here, study source code of free software shells sash , gnu bash
c linux ubuntu-14.04 execv
No comments:
Post a Comment