vimdiff shortcuts

vimdiff shortcuts


Keyboard Shortcuts:

do - Get changes from other window into the current window.

dp - Put the changes from current window into the other window.

]c - Jump to the next change.

[c - Jump to the previous change.


Ctrl W + Ctrl W - Switch to the other split window.

How to link dynamic and static libraries to the c program

How to link dynamic and static libraries to the c program


1. create directory libtest   [absolute path: /home/labuser/c_program_expts/libtest]

2. in "libtest" directory, 
create process2.c

filename: process2.c
code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

void neel1(void)
{
  printf("Hi.. i am the library");
}

3. create "Makefile" in libtest folder:
Neelkanth$ cat Makefile

CC := gcc
CFLAGS += -MMD -Wall -fomit-frame-pointer -fPIC -g
LDFLAGS += -shared

# includes
#CFLAGS += -I$(PCD_ROOT)/ipc/include

obj-y := $(patsubst %.c,%.o,$(shell ls *.c 2> /dev/null))
TARGET = libtest

all: $(TARGET) install

$(TARGET): $(obj-y)
        @echo "  LINK           $@"
        @$(AR) rcs $@.a $(obj-y)
        @$(CC) $(CFLAGS) $(obj-y) -o $@.so $(LDFLAGS) -Wl,-Map,$@.map

install: $(TARGET) install_internal

install_internal:
        @@mkdir -p lib
        @install $(TARGET).a lib
        @install $(TARGET).so lib

clean:
        @rm -f $(TARGET).so $(TARGET).a $(obj-y) $(obj-y:.o=.d) $(TARGET).map

%.o: %.c
        @echo "  CC [C]         $@"
        @$(CC) $(CFLAGS) -c $(CURDIR)/$< -o $@

-include $(obj-y:.o=.d)

4. run "make"
Neelkanth$ make
  CC [C]        process2.o
  LINK          libtest


5. create process1.c  [absolute path: /home/labuser/c_program_expts]

filename: process1.c
code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if (argc == 0) {
        printf("\nenter atleast one argument\n");
    }
    else if (argc >= 3) {
        printf("\ncannot accept more than 1 argument\n");
    }
    else {
        neel1();
        printf("\n Argument: %s \n", argv[1]);
    }
    neel1(4, NULL);
    return 1;
}

6. compile process1.c
 gcc -c process1.c -o process1.o 

7.  
If you invoke the executable, the dynamic linker will not be able to find the required shared library. By default it won’t look into current working directory. You have to explicitly instruct the tool chain to provide proper paths. The dynamic linker searches standard paths available in the LD_LIBRARY_PATH and also searches in system cache (for details explore the command ldconfig). We have to add our working directory to the LD_LIBRARY_PATH environment variable. The following command does the same.

export LD_LIBRARY_PATH=libtest/lib:$LD_LIBRARY_PATH

8. 
 gcc -o final process1.o -Llibtest/lib -ltest

9. execute final
Neelkanth$ ./final

Hi.. i am the library
 Argument: (null)
Hi.. i am the library


c program for create a macro for vprintf

C program for create a macro for vprintf


#include <stdarg.h>
#include <stdio.h>

#define TRACE(x) dbg_printf x

void dbg_printf(int i, const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
}

int main()
{
    TRACE((10, "%s\n", "neel"));
    return 1;

}

C Program for finding a file in the directory

C Program for finding a file in the directory


#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;

char *process[10] = {"neel", "peel", "feel"};

int i;
for (i=0; i< 3; i++)
{
    if ((dp = opendir("/home/cg/root")) == NULL)
    {
        printf("can't open %s", "/home/cg/root");
        exit(1);
    }

    while ((dirp = readdir(dp)) != NULL)
    {
         if(!strcmp(dirp->d_name, process[i]))
            printf("\n%s", dirp->d_name);
       
    }
    printf("\n");
   
    closedir(dp);
}

C program to find pid of a file

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

int main(int argc, char *argv[])
{
    int process_id;
    char process_id_from_file[10];
    FILE *pid_fd;
    char command[100];

    sprintf(command,"pidof %s", argv[1]);
    pid_fd = popen(command, "r");
    if (pid_fd == NULL)
    {
       // perror("Error in reading pgrep %s", process_name);
        perror("Error in reading pgrep ");
        return -1;
    }

    if (fgets(process_id_from_file, sizeof process_id_from_file , pid_fd) == NULL)
    {
        return -1;
    }
    pclose(pid_fd);

    process_id = atoi(process_id_from_file);

    if (process_id != 0)
    {
        printf("\nprocess id : %d \n", process_id);
    }
   return 0;
}

c program for strtok

c program for strtok 

#include <string.h>
#include <stdio.h>
  
int main()

{

   char str[80] = "I-am-captain-neel-jack-sparrow";

   const char s[2] = "-";

   char *token;
  
   /* get the first token */

   token = strtok(str, s);
  
   /* walk through other tokens */

   while( token != NULL )

   {

      printf( " %s\n", token );
  
      token = strtok(NULL, s);

   }
  
   return(0);

}

 output:

h-4.2$ main                                                                                                                                                                          

 I                                                                                                                                                                                            

 am                                                                                                                                                                                                

 captain                                                                                                                                                                                          

 neel                                                                                                                                                                                              

 jack                                                                                                                                                                                              

 sparrow  

C program to calculate the time difference between 2 time stamps

#include <time.h>
#include <stdio.h>

int main(void)
{
struct tm str_time;
time_t time_of_day;

struct tm str_time1;
time_t time_of_day1;
       
       
str_time.tm_year = 2012; //-1900;
str_time.tm_mon = 6;
str_time.tm_mday = 5;
str_time.tm_hour = 10;
str_time.tm_min = 3;
str_time.tm_sec = 5;
str_time.tm_isdst = 0;

str_time1.tm_year = 2017-1900;
str_time1.tm_mon = 1;
str_time1.tm_mday = 1;
str_time1.tm_hour = 1;
str_time1.tm_min = 1;
str_time1.tm_sec = 1;
str_time1.tm_isdst = 0;

time_of_day = mktime(&str_time);
printf(ctime(&time_of_day));

time_of_day1 = mktime(&str_time1);
printf(ctime(&time_of_day1));


int f = mktime(&str_time);
printf("\n mktime: %d \n", f);

int g = mktime(&str_time1);
printf("\n mktime: %d \n", g);

return 0;
}


**********************************************************************
output:
**********************************************************************
sh-4.2$ main                                                                                                                                                                                       
Thu Jul  5 10:03:05 2012                                                                                                                                                                           
Wed Feb  1 01:01:01 2017                                                                                                                                                                           
                                                                                                                                                                                                   
 mktime: 1341482585                                                                                                                                                                                
                                                                                                                                                                                                   
 mktime: 1485910861