Sponsored Content
Full Discussion: Ex1 homework help
Homework and Emergencies Homework & Coursework Questions Ex1 homework help Post 302735581 by alexthelion on Sunday 25th of November 2012 04:18:01 PM
Old 11-25-2012
Rudic maybe you right but I'm not strong in Linux so its difficult for me even trying to understand what you mean with the execv

---------- Post updated at 04:18 PM ---------- Previous update was at 04:13 PM ----------

yeah but it supposed to serach In every path in the pathindex and If it found it should be executed
i'm wrong?

I tried other method and still not working Smilie

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
 pid_t pid;
 int pStatus;
 char *sh="Shell>";
 char runLine[50];
 int i,pathindex=0, ret;
 char line[80];   // getting the user prompt
 char *ListPathes=""; //
 char *currentpath, *path;  // current path to execute in   
 char *argv[10]={NULL} ;
//////////////////////////////////////////////////////////////////////////////
printf("CMD: ");
gets(line);
 while ((strcmp(line,"leave"))!=0)
{
 if ((pid=fork())==0) //child process started
 {
   ListPathes=getenv("PATH");
   currentpath=strtok(ListPathes,":");
   while (currentpath!=NULL)
    {
      strcpy(runLine,currentpath);
      strcat(runLine,"/");
      strcat(runLine,line);
      execv(runLine,argv);
      currentpath=strtok(NULL,":");
    }
 }
 else //parent process continue
 {
 wait(&pStatus);
 if (pStatus!=0)
  printf("Error");
 }
}
}


Last edited by Scott; 11-25-2012 at 05:57 PM.. Reason: Code tags
 

7 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Homework?

https://www.unix.com/shell-programming-scripting/113946-scripts.html Looks like an assignment or homework to me, does it to you? (5 Replies)
Discussion started by: TonyFullerMalv
5 Replies

2. Homework & Coursework Questions

Homework Help.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hello, My name is Jordan and studying at the New Bulgarian University in Sofiya. This is my homework but can... (2 Replies)
Discussion started by: freestar
2 Replies

3. Homework & Coursework Questions

Need some help with my homework

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi, since I'm relatively new to Unix I need some help. I've installed FreeBSD 8 on a virtual machine and I've done... (4 Replies)
Discussion started by: iLeeT777
4 Replies

4. Shell Programming and Scripting

Homework

1. Write a shell script to print the file names of all files having .txt extension of a given directory after converting to uppercase letters. The input (directory name) should be given as command line argument. The script will also check whether sufficient arguments are passed or not and whether... (1 Reply)
Discussion started by: aninda1989
1 Replies

5. Homework & Coursework Questions

Please help me with my homework!!!

A shell script is a script written for the shell, or command line interpreter, of an operating system. Typical operations performed by shell scripts include file manipulation, program execution, printing text etc. Shell : In computing, a shell is a piece of software that provides an interface for... (1 Reply)
Discussion started by: ubun
1 Replies

6. Homework & Coursework Questions

Homework

plz i need code to search about numbers like this : 962785785698 962795565488 962785321565 962777321684 962795979515 i need code to detect just numbers start with "96278" i need it in awk !! thanks (1 Reply)
Discussion started by: eyad mohammad
1 Replies

7. Homework & Coursework Questions

Homework Help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: Write a command to display lines ending with the... (1 Reply)
Discussion started by: elh009
1 Replies
EXEC(3) 						     Linux Programmer's Manual							   EXEC(3)

NAME
execl, execlp, execle, execv, execvp - execute a file SYNOPSIS
#include <unistd.h> extern char **environ; int execl(const char *path, const char *arg, ...); int execlp(const char *file, const char *arg, ...); int execle(const char *path, const char *arg , ..., char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); DESCRIPTION
The exec family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for the function execve(2). (See the manual page for execve for detailed information about the replacement of the current process.) The initial argument for these functions is the pathname of a file which is to be executed. The const char *arg and subsequent ellipses in the execl, execlp, and execle functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the file name associated with the file being executed. The list of arguments must be terminated by a NULL pointer. The execv and execvp functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the file name associated with the file being executed. The array of pointers must be terminated by a NULL pointer. The execle function also specifies the environment of the executed process by following the NULL pointer that terminates the list of argu- ments in the parameter list or the pointer to the argv array with an additional parameter. This additional parameter is an array of point- ers to null-terminated strings and must be terminated by a NULL pointer. The other functions take the environment for the new process image from the external variable environ in the current process. Some of these functions have special semantics. The functions execlp and execvp will duplicate the actions of the shell in searching for an executable file if the specified file name does not contain a slash (/) character. The search path is the path specified in the environment by the PATH variable. If this variable isn't specified, the default path ``:/bin:/usr/bin'' is used. In addition, certain errors are treated specially. If permission is denied for a file (the attempted execve returned EACCES), these functions will continue searching the rest of the search path. If no other file is found, however, they will return with the global variable errno set to EACCES. If the header of a file isn't recognized (the attempted execve returned ENOEXEC), these functions will execute the shell with the path of the file as its first argument. (If this attempt fails, no further searching is done.) RETURN VALUE
If any of the exec functions returns, an error will have occurred. The return value is -1, and the global variable errno will be set to indicate the error. FILES
/bin/sh ERRORS
All of these functions may fail and set errno for any of the errors specified for the library function execve(2). SEE ALSO
sh(1), execve(2), fork(2), environ(5), ptrace(2) COMPATIBILITY
On some other systems the default path (used when the environment does not contain the variable PATH) has the current working directory listed after /bin and /usr/bin, as an anti-Trojan-horse measure. Linux uses here the traditional "current directory first" default path. The behavior of execlp and execvp when errors occur while attempting to execute the file is historic practice, but has not traditionally been documented and is not specified by the POSIX standard. BSD (and possibly other systems) do an automatic sleep and retry if ETXTBSY is encountered. Linux treats it as a hard error and returns immediately. Traditionally, the functions execlp and execvp ignored all errors except for the ones described above and ENOMEM and E2BIG, upon which they returned. They now return if any error other than the ones described above occurs. CONFORMING TO
execl, execv, execle, execlp and execvp conform to IEEE Std1003.1-88 (``POSIX.1''). BSD MANPAGE
1993-11-29 EXEC(3)
All times are GMT -4. The time now is 04:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy