# C shell program

**URL:** <https://community.unix.com/t/c-shell-program/371617>\
**Category:** Homework & Coursework Questions\
**Created:** [April 3, 2018, 2:59pm UTC](https://community.unix.com/t/c-shell-program/371617 "2018-04-03T14:59:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zorro\_phu](https://community.unix.com/user_avatar/community.unix.com/zorro_phu/32/9988_2.png) [@zorro\_phu](https://community.unix.com/u/zorro_phu)\
**Post date:** [April 3, 2018, 2:59pm UTC](https://community.unix.com/t/c-shell-program/371617/1 "2018-04-03T14:59:35Z")

</div>

1. I've have to write a shell program that accepts Ctrl+T (in linux os in c language) and should print out the current time and date to the screen. I've written the following code but i've to type ^T individual rather than pressing ctrl+T(^T) to get the output. :

2. How do i make the shell program to react when i press ctrl+T without having to press enter. For the following code which i wrote requires me to press enter to execute the command

```nohighlight
#include<stdio.h>
#include<time.h>
#include<unistd.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>

int main(void){
		
	time_t t;
	char buffer[100];
	int count;
	
	printf("My Prompt$: ");
	fflush(stdout);
	
	for(;;){
		count = read(0,buffer,99);
		buffer[count] = '\0';
		
		if(strncmp(buffer,"^T",2)==0){
			time(&t);
			printf("%s\n",ctime(&t));
		}
		if(strncmp(buffer,"exit",4)==0){
			exit(0);
		}
		printf("My Promts$ : ");
		fflush(stdout);
	}	
}

```

Output:  
[zorro@localhost Desktop]$ gcc hw8.c  
[zorro@localhost Desktop]$ ./a.out  
My Prompt$: ^T // have to type ^T  
Tue Apr 3 14:47:10 2018

My Promts$ : ^T // when i pressed ctrl+T & have to press enter  
My Promts$ : ^T  
My Promts$ : exit  
[zorro@localhost Desktop]$

1. Assumption University, Bangkok, Thailand, Wanchat.C, CE4207

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [April 3, 2018, 7:54pm UTC](https://community.unix.com/t/c-shell-program/371617/2 "2018-04-03T19:54:49Z")

</div>

> [@zorro\_phu](#):
>
> 1. How do i make the shell program to react when i press ctrl+T without having to press enter. For the following code which i wrote requires me to press enter to execute the command

When you read the stdin with `read()` as you do it you basically read linewise. You have some input field, where you can type and edit some text and you finally press \<enter\> to submit it - quite the same way you enter commands in the shell.

When you want to intercept single characters you need to use the `getch()` function or one of its variations ( `getch()` , `getchar()` , `getche()` ).

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [April 3, 2018, 8:30pm UTC](https://community.unix.com/t/c-shell-program/371617/3 "2018-04-03T20:30:03Z")

</div>

In addition to using functions that get characters (rather than lines) from a file, you'll also need to put the input file descriptor into raw (rather than cooked) input mode if you want to read characters from a terminal device without waiting for a full line to be entered. Look at the man pages for `tcgetattr()` , `tcsetattr()` , and `termios.h` to see how to do this.
