C program on Frame encapsulation using strtok

/**************************************************************************///code for framing the packet stream (both encapsulation and decapsulation) // encapsulation using "strcat"//decapsulation using "strtok"// using delimiter as '@' in between the fields/**************************************************************************/


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

#define DELIMITER "@"

void bingo (char *argp[]);

void bingo (char *argp[])
{
   
    char str_complete_packet[50] = "process_start@1st_index";
   
    int i = 0;
   
    while (argp[i] != NULL) {
         printf("%s\n", argp[i]);
       
        strcat(str_complete_packet, DELIMITER);
        strcat(str_complete_packet, argp[i]);
        i++;
    }
   
    printf("\n complete packet : %s \n\n", str_complete_packet);

    char *operation;
    char *index;
   

 #if 0
     operation = strtok(str_complete_packet, DELIMITER );
     printf("Operation: %s \n", operation);
     index = strtok(NULL, DELIMITER );
     printf("rule index: %s \n", index);
 #endif    
     printf("\n Now print the arguments extracted from packet in order \n");

     char *tokenPtr;
     int j =0;
     char temp[10][20];
   
     memset(temp, 0, sizeof(temp));
   
     // initialize the string tokenizer and receive pointer to first token
   
     tokenPtr = strtok(str_complete_packet, DELIMITER);
     while(tokenPtr != NULL)
     {
         printf("%s\n",tokenPtr);
         sprintf(temp[j],"%s",tokenPtr );
         printf("%s\n",temp[j]);
                 tokenPtr = strtok(NULL, DELIMITER);
            j++;
     }
   
   
}

int main()
{
    char *argp[] = {"neel", "-p", "bambo", NULL};
   
    bingo(argp);
     
    return 0;
}


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;

}