LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2019, 11:19 AM   #1
alibabapk
LQ Newbie
 
Registered: Dec 2019
Location: Lahore, Pakistan.
Posts: 2

Rep: Reputation: Disabled
WHat is signal for ctl-D?


Like for ctrl+C is SIGINT. What is this SIG??? like for ctrl+D?
 
Old 12-12-2019, 11:36 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
There is no such thing.
 
1 members found this post helpful.
Old 12-12-2019, 05:14 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Ctrl+D usually sends EOF to stdin, but that's not a signal like SIGINT.
 
1 members found this post helpful.
Old 12-12-2019, 06:58 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
The thing OP is looking for might be `stty eof` or `stty raw`
 
Old 12-13-2019, 09:06 AM   #5
alibabapk
LQ Newbie
 
Registered: Dec 2019
Location: Lahore, Pakistan.
Posts: 2

Original Poster
Rep: Reputation: Disabled
Actually I want to write a signal hander for ctrl + D

Making my own shell I wrote this code;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define MAX_LEN 512
#define MAXARGS 10
#define ARGLEN 30
#define PROMPT "\033[1;34mcs525shell@\033[0m"
int pid;
int execute(char* arglist[]);
char** tokenize(char* cmdline);
char* read_cmd(char*, FILE*);
void sigint_handler(int signum){
kill(pid,9);
signal(SIGINT,SIG_DFL);
system("stty echo");
}
int main(){
char *cmdline;
char** arglist;
char* prompt = PROMPT;
signal(SIGINT,sigint_handler); //SEE HERE I AM CATCHING CTRL + C WHICH IS QUITING LESS PROCESS LIKE "LESS /ETC/PASSWD"
//BUT NOT QUITING MY SHELL. NOW I WANT TO CATCH CTRL + D TO QUIT MY SHELL PROCESS
//
while((cmdline = read_cmd(prompt,stdin)) != NULL){
if((arglist = tokenize(cmdline)) != NULL){
execute(arglist);
// need to free arglist
for(int j=0; j < MAXARGS+1; j++)
free(arglist[j]);
free(arglist);
free(cmdline);
}
}//end of while loop

printf("\n");
return 0;
}
int execute(char* arglist[]){
int status;
pid = fork();
switch(pid){
case -1:
perror("fork failed");
exit(1);
case 0:
execvp(arglist[0], arglist);
perror("Command not found...");
exit(1);
default:
waitpid(pid, &status, 0);
// printf("child exited with status %d \n", status >> 8);
return 0;
}
}
char** tokenize(char* cmdline){

if(cmdline[0] == '\0')//if user has entered nothing and pressed enter key. Writing it before the for loop of allocating
return NULL; //memory is better in a way that it returns NULL and no need to allocate memory.

//allocate memory
char** arglist = (char**)malloc(sizeof(char*)* (MAXARGS+1));
for(int j=0; j < MAXARGS+1; j++){
arglist[j] = (char*)malloc(sizeof(char)* ARGLEN);
bzero(arglist[j],ARGLEN);
}

int argnum = 0; //slots used
char*cp = cmdline; // pos in string
char*start;
int len;
while(*cp != '\0'){
while(*cp == ' ' || *cp == '\t') //skip leading spaces
cp++;
start = cp; //start of the word
len = 1;
//find the end of the word
while(*++cp != '\0' && !(*cp ==' ' || *cp == '\t'))
len++;
strncpy(arglist[argnum], start, len);
arglist[argnum][len] = '\0';
argnum++;
}
arglist[argnum] = NULL;
return arglist;
}

char* read_cmd(char* prompt, FILE* fp){
printf("%s", prompt);
int c; //input character
int pos = 0; //position of character in cmdline
char* cmdline = (char*) malloc(sizeof(char)*MAX_LEN);
while((c = getc(fp)) != EOF){
if(c == '\n')
break;
cmdline[pos++] = c;
}
//these two lines are added, in case user press ctrl+d to exit the shell
if(c == EOF && pos == 0)
return NULL;
cmdline[pos] = '\0';
return cmdline;
}


Version01:
[10]
The first version of cs525shell has been discussed in the class and developed in the video lecture available online at
(http://www.arifbutt.me/lec22-design-...if-butt-pucit/). It has the
following capabilities/characteristics:
a. The shell program displays a prompt, which is cs525@pwd, i.e., the present working directory. You may like to indicate
other things like machine name or username or any other information you like. Hint: getcwd()
b. The cs525shell allow the user to type a string (command with options and arguments) (if any) in a single line. Parse the
line in tokens, fork and then pass those tokens to some exec family of function(s) for execution. The parent process waits
for the child process to terminate. Once the child process terminates the parent process, i.e., our cs525shell program again
displays the prompt and waits for the user to enter next command.
c. The shell program allows the user to quit the shell program by pressing <CTRL+D>
d. If the user run the less /etc/passwd program and press <CTRL+C>, i.e., send SIGINT to the less program. Both
the less program and the shell should not terminate. Rather the signal should be delivered to the less program only and
not to cs525shell
$ ./cs525shellv1
cs525shell@/home/arif/:- ls –l /etc/passwd
-rw-r—r-- 1 root root 2102 Feb 7 07:35 /etc/passwd
cs525shell@/home/arif/:-
 
Old 12-13-2019, 04:24 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
You don't use a signal handler to catch CTRL+D. You check for END OF FILE when you're getting the user's input.

See for example:
https://cboard.cprogramming.com/c-pr...rl-d-mean.html

Note that the requirements do not say to use a signal handler to handle ctrl+d.

Last edited by dugan; 12-13-2019 at 08:57 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does Ctl-Alt-F1 kill X, or just put in background? TreeDragon60 Linux - General 18 03-31-2004 02:17 AM
alsamixer- invalid CTL default dbkluck Debian 3 10-14-2003 09:50 PM
ctl-alt-del equivalent in linux? pekuekfir Linux - General 14 09-20-2003 12:14 PM
ctl-alt-f8 thru f10 give nothing desolat Mandriva 2 09-17-2003 07:28 PM
CTL+ALT+DEL logoff screen gone ScreeminChikin Linux - General 2 09-19-2002 04:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration