473,473 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Catch < Ctrl + C > on Linux ==> can not run execvp

5 New Member
Hi. I have to write a simple Shell with History feature. First, I get a string from command line. The command line is split into tokens. And then, I call fork ( ) to create a new process. The child process will call execvp(args[0], args) in order to execute the string command line. When user press < Ctrl + C > ( signal SIGINT ) , I have to read a file and print out 10 last commands.

My problem is: when I embed small code to catch < Ctrl + C > into my program as bold text below, I can not run execvp( ). The error is : bad address !!!

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

#define BUFFER_SIZE 50

static char buffer[BUFFER_SIZE];

void handle_SIGINT(int sig_num ){
write(STDOUT_FILENO, buffer, strlen(buffer));
exit(0);
}


int main(){

/*
struct sigaction handler;
handler.sa_handler = handle_SIGINT; //* <=== ERROR */
handler.sa_flags = 0;
sigemptyset(&handler.sa_mask);
sigaction(SIGINT, &handler, NULL);
strcpy(buffer, "Caught <Ctrl> <c>\n");


*/
char inputBuffer[MAX_LINE]; /*buffer to hold the command entired */
char* args[MAX_LINE/ +1]; /*command line ( of 80 ) has max of 40 argument */
int background; /*equals 1 if a command is folowed by '&' */
setup(inputBuffer, args , &background); /*setup the command line */

pid_t pid;
pid = fork();

switch(pid):
case -1:
//fork() error.

case 0:
// call execvp(args[0], args) to run shell commands
default:
//.................
return(0);
}

How can I fix this prolem ? Help me, please !!!
Feb 3 '09 #1
2 6837
horace1
1,510 Recognized Expert Top Contributor
The code would not compile initially so I fixed a couple of problems, indicated by // ** comments
Expand|Select|Wrap|Line Numbers
  1. include <stdio.h>
  2. #include <signal.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #define BUFFER_SIZE 50   //  ** added
  8. #define MAX_LINE 100
  9.  
  10. static char buffer[BUFFER_SIZE];
  11.  
  12. void handle_SIGINT(int sig_num ){
  13. write(STDOUT_FILENO, buffer, strlen(buffer));
  14. exit(0);
  15. }
  16.  
  17. int main(){
  18.  
  19.  
  20. struct sigaction handler;
  21. handler.sa_handler = handle_SIGINT; //* <=== ERROR */
  22. handler.sa_flags = 0;
  23. sigemptyset(&handler.sa_mask);
  24. sigaction(SIGINT, &handler, NULL);
  25. strcpy(buffer, "Caught <Ctrl> <c>\n");
  26.  
  27.  
  28. char inputBuffer[MAX_LINE]; /*buffer to hold the command entired */
  29. char* args[MAX_LINE/ +1]; /*command line ( of 80 ) has max of 40 argument */
  30. int background; /*equals 1 if a command is folowed by '&' */
  31. //setup(inputBuffer, args , &background); /*setup the command line */
  32.  
  33. pid_t pid;
  34. pid = fork();
  35.  
  36. switch(pid){                 //  *** changed : to {
  37. case -1:
  38. //fork() error.
  39.  
  40. case 0:
  41. // call execvp(args[0], args) to run shell commands
  42. default:;
  43. }                // ** added }
  44. //.................
  45. printf("Done");
  46. while(1);        // added
  47. return(0);        // added
  48. }
  49.  
it compiles and when run forks() a child proceess and the parent and child loop at the while(1); statement.
when I hit <CTRL/C> it is caught and both processes print "Caught <Ctrl> <c>" then "Done" then stop. e.g. a run gave
Expand|Select|Wrap|Line Numbers
  1. $sig
  2. Caught <Ctrl> <c>
  3. DoneCaught <Ctrl> <c>
  4. Done
  5. $
  6.  
Feb 6 '09 #2
thanhnh
5 New Member
Oh, many thanks to you. Wish a best health 4 you :)
Feb 6 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: haoren | last post by:
Can anybody help me with this problem: How can I echo a string that contain <? and <?php? For example, $str="test <? and <?php echo"; echo $str;
11
by: Ted Mayett | last post by:
OK. Here is a glitch, sorry if this has been mentioned before. This is an erratic glitch. I am now up to three other people besides myself who have been able to see this glitch. It seems it...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
6
by: Paul Gorodyansky | last post by:
Hi, Question about event.altKey event.ctrlKey event.shiftKey If I need to catch when a user does say Ctrl+Alt+G - somehow event.altKey is not working, i.e.:
16
by: chandanlinster | last post by:
/* if the program is executed as shown below: * $./a.out * ges<ctrl-d><ctrl-d> * * OUTPUT: * Number of characters = 3 * * Question: What happens to the first and second <ctrl-d>? */
1
by: sweetpotatop | last post by:
Hi, I got an pop-up error when I tried to run my window application. However, I have no idea which line of code is causing the problem. Is there a way to figurate that out? In VB, when I do...
7
by: =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post by:
I need to emulate the missing "maxlegth" attribute in "textarea" fields but all my Google searches either lead to obsolete scripts that overwrite the "value" property (thus losing caret position)...
24
by: Chris F.A. Johnson | last post by:
On 2008-07-09, Denis McMahon wrote: If the left-hand column contains any text, use em rather than px to size it. In CSS, less is more. Most problems are caused by specifying too much rather...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.