The Linux Channel

Videos :: Code Snippets

Written by: Kiran Kankipati - Published: 16-Mar-2017


Watch Video: 323 The Purpose of writing a software code - Expressing Skills vs Addressing Requirement

* Click the image above to watch this video on Youtube ↗
Watch Video: 228 writing custom Shell or CLI via readline library in C and PHP

* Click the image above to watch this video on Youtube ↗

Download this episode's my sample source code HERE. And here is the source code for a quick reference.

/* testcli.c
 * The Linux Channel
 * Author: Kiran Kankipati
 * Updated: 05-Jul-2017
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned char command_buf[500];

void main()
{
	while(1)
	{
		printf("testcli> ");
		scanf("%s", command_buf);
		//gets(command_buf);
		if(!strcmp(command_buf, "ifconfig")) { system("ifconfig"); }
		else if(!strcmp(command_buf, "date")) { system("date"); }
		else if(!strcmp(command_buf, "ls")) { system("ls"); }
		else if(!strcmp(command_buf, "exit") || !strcmp(command_buf, "quit") || !strcmp(command_buf, "e") || !strcmp(command_buf, "q")) { break; }
	}
}

/* testcli_readline.c
 * The Linux Channel
 * Author: Kiran Kankipati
 * Updated: 05-Jul-2017
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>

//Install if not installed !!
//sudo apt-get install libreadline6 libreadline6-dev

unsigned char *command_buf;

void main()
{
	while(1)
	{
		command_buf = readline("testcli_readline> ");
		if(strlen(command_buf)>0) { add_history(command_buf); }
		if(!strcmp(command_buf, "ifconfig")) { system("ifconfig"); }
		else if(!strcmp(command_buf, "date")) { system("date"); }
		else if(!strcmp(command_buf, "ls")) { system("ls"); }
		else if(!strcmp(command_buf, "exit") || !strcmp(command_buf, "quit") || !strcmp(command_buf, "e") || !strcmp(command_buf, "q")) { break; }
	}
}

/* testcli_readline.php
 * The Linux Channel
 * Author: Kiran Kankipati
 * Updated: 05-Jul-2017
 */
 
$prompt = `hostname`;
$prompt .= "#testcli_readline> ";
while(1)
{	$command_buf = readline($prompt);
	if(strlen($command_buf)>0) { readline_add_history($command_buf); }
	if(!strcmp($command_buf, "ifconfig")) { system("ifconfig"); }
	else if(!strcmp($command_buf, "date")) { system("date"); }
	else if(!strcmp($command_buf, "ls")) { system("ls"); }
	else if(!strcmp($command_buf, "exit") || !strcmp($command_buf, "quit") || !strcmp($command_buf, "e") || !strcmp($command_buf, "q")) { break; }	
}

Watch Video: 200 Coding a simple look-up-table in C - without Linked lists and a binary search

* Click the image above to watch this video on Youtube ↗

Download this episode's my sample source code HERE. And here is the source code for a quick reference.

/* lookuptable.c - coding a look-up-table in C
 * The Linux Channel
 * Author: Kiran Kankipati
 * Updated: 16-mar-2017
 */
 
#include <stdio.h>
#include <stdbool.h>

typedef struct __mytable_
{	int value;
	char name[50];
	bool enable;
} mytable_t;

#define MAX_ENTRIES 100
mytable_t mytable_array[MAX_ENTRIES];

bool init_mytable()
{	int i=0;
	for(i=0;i<MAX_ENTRIES;i++) { mytable_array[i].enable=false; }
}

void print_mytable()
{	int i=0;
	printf("mytable_array[] contents\n");
	for(i=0;i<MAX_ENTRIES;i++) { if(mytable_array[i].enable==true) printf("[%d] - value: %d\n", i, mytable_array[i].value); }
	printf("------------------------\n\n");
}

void add_item_mytable(int value)
{	int i=0;
	//check for duplicate ?
	for(i=0;i<MAX_ENTRIES;i++) { if(mytable_array[i].enable==true) { if(mytable_array[i].value==value) { return; } } }
	
	//add item
	for(i=0;i<MAX_ENTRIES;i++) { if(mytable_array[i].enable==false) { mytable_array[i].value=value; mytable_array[i].enable=true; return; } }
}

void del_item_mytable(int value)
{	int i=0;
	//check for match ?
	for(i=0;i<MAX_ENTRIES;i++) { if(mytable_array[i].enable==true) { if(mytable_array[i].value==value) { mytable_array[i].enable=false; return; } } }
}

int find_item_mytable(int value)
{	int i=0;
	//check for match ?
	for(i=0;i<MAX_ENTRIES;i++) { if(mytable_array[i].enable==true) { if(mytable_array[i].value==value) { return i; } } }
}

void main()
{
	init_mytable(); print_mytable();
	
	add_item_mytable(5); print_mytable();
	add_item_mytable(3); print_mytable();
	add_item_mytable(1); print_mytable();
	add_item_mytable(2); print_mytable();
	
	del_item_mytable(3); print_mytable();
	
	printf("value: %d is in position [%d]\n", 1, find_item_mytable(1));
	
	
}


Suggested Topics:


Linux (user-space), Systems Architecture and Networking :: Video Episodes
 IPUtils - Source Code Walk ↗
21-Aug-2017


 net-tools - Source Code Walk ↗
09-Sep-2017


 Systems Architecture ↗
05-Jul-2016


 Wireshark Packet Capture ↗
27-Oct-2016


 Online Course - Linux CLI Scripting ↗
04-Jul-2016


 Online Course - Linux TUN/TAP virtual network interfaces ↗
25-Jan-2017


 Data Visualization and Analytics ↗
07-Apr-2018


 VRF - Virtual Routing and Forwarding ↗
24-Feb-2018


 Online Course - Networking Protocols ↗
04-Jul-2016


 CPU Performance and Benchmarks ↗
13-Mar-2017


 Code Snippets ↗
16-Mar-2017


 Linux (user-space) RAW Socket Programming ↗
27-Aug-2016


 Networking and Q&A ↗
27-Aug-2016


 CUDA GPU Distributed Parallel Computing ↗
02-Aug-2018


 Oracle VM VirtualBox ↗
27-Mar-2017


 Ethtool - Source Code Walk ↗
30-Sep-2017


 

Join The Linux Channel :: Facebook Group

* This website is driven by AI.

Join a course:

💎 Linux Kernel and Systems Software: PDF Brochure

💎 Linux and Network Software Developer: PDF Brochure


💗 Help shape the future: Sponsor/Donate


Recommended Topics:
Featured Video:

* Click the image above to watch this video on Youtube ↗


Trending Video:

* Click the image above to watch this video on Youtube ↗

TUTORIALS :: Research Socket overhead in Linux vs Message Queues and benchmarking:


Linux Kernel TCP Congestion Control Algorithms:
Linux Kernel Programming - Device Drivers:


Research :: Content Delivery Networks (CDN):
CDN Networks


Recommended Video:

* Click the image above to watch this video on Youtube ↗

The Linux Channel - v8.8 :: Updated: 30-Mar-2018 :: © 2018 :: Author: Kiran Kankipati
Your IP: 54.167.243.214 :: Browser: CCBot/2.0 (https://commoncrawl.org/faq/)