Posts Tagged ‘linux’

How To Capture GPS NMEA Output in C Program

This post is divided into two sections, first one talks about send shell output to C program and second section makes use of this to show how to redirect GPS NMEA output in C program.

How to Capture the Output of a Linux Shell Command in C

If you google about how to redirect your shell output in C program, you will find couple of answers. There are few ways like using  freopen (“/dev/tty“, “a”, stdout) and playing around with ‘stdout’ file descriptor.

Other common approach is using ‘system’ command. The ‘system’ command executes the command.

int system (const char* command);

 With ‘system‘ function returns an integer indicating success or failure, but not the actual output of the string or command. This is useful when you do not need to stream the command output to your program.

When you need to re-direct command output to your program then you will need to use ‘popen’

popen - pipe to/from shell

FILE *popen(const char *command, const char *type);int pclose(FILE *stream);
Typically usage is:

     FILE *fp;
     char *command;

     /* command contains the command string (a character array) */

     /* If you want to read output from command */
     fp = popen(command,"r"); 
          /* read output from command */
           fscanf(fp,....);   /* or other STDIO input functions */

     /* If you want to send input to command */
     fp = popen(command,"w"); 
          /* write to command */
           fprintf(fp,....);   /* or other STDIO output functions */

The program cannot both send input to the command and read its output. You pass the command as a string to ‘popen’ function.

How to Capture GPS NMEA Output in C Program

Here is sample program using ‘popen’ to do this. What it does is creates a result buffer, opens up a read-only stream, runs the command, and captures the output, stuffs it into the buffer, then returns it as a string.

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

int main( int argc, char *argv[] )

  FILE *fp;
  int status;
  char result[512];
  int i=0;
  /* Open the command for reading. */
  fp = popen("gpspipe -r -x 5 | grep '$GPGGA'", "r");
  if (fp == NULL) {
    printf("Failed to run command\n" );
  /* Read the output a line at a time - output it. */
  if (fgets(result, sizeof(result), fp) != NULL) {
    printf("%s", result);

  else printf("no output \n");
  /* close */

  return 0;

This program uses ‘gpsd’ client programs to redirect NMEA output to command line. For more on how to install and test with ‘gpsd’ refer this post.

You should test the output of command first on command line. As you know making work GPS module is tricky.

Number of View :384

Tags: ,

No Comments

How To Resolve Error __curr_eh_stack_entry and __eh_curr_region With pgcpp C++ Compiler

Was getting Error __curr_eh_stack_entry and __eh_curr_region With pgcpp C++ Compiler.
check out complete man pgcpp – The Portland Group Inc. C++ compiler

Building an application software using pgcc compiler, developed by C++ Language generates two external references of symbols [__curr_eh_stack_entry] and [__eh_curr_region], and results in kink error. Why?

How To Resolve Error __curr_eh_stack_entry and __eh_curr_region With pgcpp C++ Compiler?

Compiler automatically generates these symbols when -exception (C++ exception handling) and -rtti=on (Runtime type information) options are used. If these option is not required to use, Please do not enable them.

-rtti(default) –no_rtti An pgcc compiler option switch.
Enable (disable) support for RTTI (runtime type Information) features: dynamic_cast, typeid.Number of View :5491

Tags: , , ,

No Comments

How To _access Function For Long File Names As Path In Winddows Programming Use UNC

I use the function _access to check for file/directory existence in my program,
Problem definition: I have noticed that the function can’t deal with long file names.  I wanted to check if file is available or not at path located “C:\Documents and Settings\0001”, did Google search on how to give long file names in windows..

How To _access Function For Long File Names As Path In Winddows Programming make Use of UNC standards

Solution use the UNC(Universal Naming Convention) conventions for path to be specified while working with windows programs.

ie the path should be “C:\\Documents and Settings\\0001”

Now The next problem is wanted to use period ie . in file name path,

Eg. I need to use the path as “C:\\Documents and Settings\\.test\\0001”
So tried with something like “\\.\” But The “\\.\” prefix will access the Win32 device namespace instead of the Win32 file namespace.

Then how to Turns off automatic expansion of the path string? Use  the “\\?\” prefix also allows the use of “..” and “.” in the path names. To specify an extended-length path, use the “\\?\” prefix. For example, “\\?\D:\<very long path>”.  Read more about Naming Files, Paths, and Namespaces

That did not help much, and tried directly with “C:\\Documents and Settings\\.test\\0001” It works.
you can also try the PathFileExists function

access function Return Value

Each function returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode;

Do share your questions and views..Number of View :3740

Tags: , , ,

No Comments

Fork Implementation In Windows Operating System Vfork Implement in Win32

With little understanding of fork basics here are few more findings on how to vfork implementation in windows operating system and
fork implementation on win32
AT&T Labs Research – Software Tools pick out UWIN: Unix on Windows 95 and NT Machines
Major difference in Linux/Unix functionality and WIN32 applications
Process control and management:
Fork implementation
processes can be created using the fork(2) function, in win32 includes a spawn family of functions that combines the functionality of fork/exec for efficiency.
File descriptor
Open files, pipes, sockets, fifos, and character and block special devices files have file descriptors associated with them. In windows it is called handle.
Most of the unix/linux signals are absent in windows, like SIGALRM
these can be implemented using replacement methods like SIGALRM Time-out alarm can be implemented using SetTimer – WM_TIMER – CreateWaitableTimer
SIGCHLD Change in status of child can be implemented using WaitForSingleObject
Terminal interface for consoles, sockets, and serial lines

sockets based on WINSOCK:

File control locking:

Memory mapping and shared memory

System V IPC
semaphore; Shmget can be implemented using CreateFileMaping or
shmdt can be implemented using UnmapViewOfFile
shmat can be implemented using MapViewOfFile
Pipe to _pipe
Close to _close

Runtime linking of dynamically linked libraries : like The dlopen(), dlsym() interface
Error mapping from Windows to UNIX

Symbolic links
Symbolic links to files and directories can be created and can be implemented as Windows shortcuts.

Few Good References for Unix2Win porting

  1. Porting Socket Applications to Winsock
  2. Process and Thread Functions
  3. Winsock Programmer’s FAQ Articles: BSD Sockets Compatibility
  4. LibGW32C for GNU C library for Windows
  5. Unix to Windows Porting Dictionary for HPC
  6. UNIX Application Migration Guide

Number of View :5938

Tags: , ,

No Comments

CreateProcess And fork() In Windows Differences And Implementation

Although there is a one-to-one mapping between C UNIX APIs and Win32 APIs for most of the APIs like open to CreateFile, read to ReadFile, write to WriteFile, ioctl to DeviceIOControl, close to CloseFile, and so on. Still, While Porting applications from UNIX to Win32 natively,
One of the largest areas of difference is in the process model. UNIX has fork; Win32 does not. Depending on the use of fork and the code base, Win32 has two APIs that can be used: CreateProcess and CreateThread. A UNIX application that forks multiple copies of itself can be reworked in Win32 to have either multiple processes or a single process with multiple threads read Porting from UNIX to Win32 for further study.

What’s the best way to duplicate fork() in windows?
Forking a Win32 Process

Process Creation in cygwin
The fork call in Cygwin is particularly interesting because it does not map well on top of the Win32 API. This makes it very difficult to implement correctly. Currently, the Cygwin fork is a non-copy-on-write implementation similar to what was present in early flavors of UNIX.  The first thing that happens when a parent process forks a child process is that the parent initializes a space in the Cygwin process table for the child. It then creates a suspended child process using the Win32 CreateProcess call.

Read further to know why process creation in cygwin is slow? windows fork process; dynamic forking process win32

Here are Some Differences between CreateProcess() in WIn32 and fork()/exec(); has two primitives: fork (create new process identical to calling process) and exec (replace details of current process with those loaded from executable file).
Fork also copies open file descriptors, including network sockets (like filehandles) are inherited by children.
Windows has one primitive: CreateProcess, which is roughly equivalent to fork-then-exec.Number of View :13058

Tags: , , ,

1 Comment

Solution – C Requires That a Struct Or Union Has At Least One Member

Windows errors are strange,
I have one header file in which one structure is declared,

typedef struct {
key_t xyz;

For this When I compile using cl.exe I get following list of errors.

: error C2016: C requires that a struct or union has at least one member
: error C2061: syntax error : identifier 'key_t'
: error C2059: syntax error : '}'

Well I am doing Code migration from Unix system to Windows system so key_t is not known in windows.

It does not make sense for the

Compiler Error C2016

which says no closing single quotation mark. Scratching my head and going around with the error.
I do not see any mistake in struct declaration,
Solution – C requires that a struct or union has at least one member
Just add the declaration of key_t in same header file before structure declaration.

typedef int key_t;

And compile again you are done.Number of View :10548

Tags: , , , ,

1 Comment

Pipes Example Code For Linux Process Synchronizaion

Pipe is a buffer of fixed size written by one related process and read by another related process. where related process means, parent and child process.
Usage : int pipe(int fildes[2])
creates a pipe and returns two file descriptors, fildes[0] and fildes[1] for reading and writing.
mutual exclusion is handled by operating system, ie only one process at a time can read or write to pipe. If one process is reading or writing on pipe other process will wait.
processes sharing the pipe must have same parent in common and are unaware of each other’s existence

Pipe example

int n;
int   pipefd[2];
char buff[100];
if (pipe(pipefd) < 0)       // create a pipe
perror(“pipe error”);
printf(“read fd = %d, writefd = %d\n”, pipefd[0], pipefd[1]);
if (write(pipefd[1], “hello world\n”, 12) !=12) // write to pipe
perror(“write error”);
if ((n=read(pipefd[0], buff, sizeof(buff))) <=0) //read from pipe
perror(“read error”);
write(1, buff, n );   /* write to stdout */
read fd = 3, writefd = 4
hello world
default FD for any process under Linux is from 0, 1 and 2 ie for STDIN, STDOUT and STRERR, whatever new descriptors are created will be assigned FD greater than 2. So read FD for pipe is 3 and write FD for Pipe is 4.

Number of View :5693

Tags: , ,

No Comments

Cross Compiling Python 2.6.2 For ARM Cross Compiling Python for Embedded Linux

Tried to Cross Compile Python 2.6.2 For my ARM board, got success to successfully port python on ARM board, On ARM board Embedded Linux is already ported and running fine. ==>>

Here are simple steps to Cross compile python for ARM target board.

1) setup ARM tool chain with X11

install dependencies

Dependencies taken from DEBIAN
1) libdb

2) libsqlite3
copy and change prefix in sqlite3.pc

3) ncursesw

Dependencies installed from source
1) bzip2
edit Makefile


make install

2) gdbm
./configure –host=arm-linux –target=arm-linux –prefix=/xxx/yyy –enable-shared
make install
3) tcl-8.4.19
cd to unix folder

export ac_cv_func_strtod=yes
export tcl_cv_strtod_buggy=1

CC=arm-linux-gcc ./configure –host=arm-linux –prefix=/xxx/yyy

make install DESTDIR=/xxx/yyy

Note: dont install in prefix otherwise tk is not compiling.

4) tk-8.4.19

./configure –host=arm-linux CC=arm-linux-gcc
–prefix=/xxx/yyy –with-tcl=<path to unix folder in source of tcl>


make install

Note: you may have to move X11R6 from toolchain to /usr because its looking  in /usr for the X11 libs. if you do so remember to take backup of your /usr/X11R6 and then restore when you are done cross compiling python. Later find out why Makefiles are looking for X11R6 in /usr and change the path in those Makefiles..

5) readline-6.1
make install

6) openssl-0.9.8
There is no need to type configure, a Makefile is already existing. We just need to edit it and change it with way:


CC= xxx-yyy-linux-gnu-gcc
AR=xxx-yyy-linux-gnu-ar $(ARFLAGS) r
RANLIB= xxx-yyy-linux-gnu-ranlib
To compile and install the OpenSSL project, type successively make and make install. You should find the directories and files listed above under /opt/external_packages/openssl/0.9.8g/compiled/xxx-yyy-linux-gnu/.

change pkg-config files

Patching Python-2.6.2 before crosscompile
1) ./configure
2) make python Parser/pgen
3) mv python hostpython
4) mv Parser/pgen Parser/hostpgen
5) make distclean

6) apply the patch Python-2.6.2-xcompile.patch
patch -p1 < Python-2.6.2-xcompile.patch

7) In Modules/Setup.config and according to
Setup.config and (TODO: make patches for and Setup.config).
8) edit Modules/getaddrinfo.c and change “u_” to “unsigned ”

Compiling Python-2.6.2
CC=arm-linux-gcc CXX=arm-linux-g++ AR=arm-linux-ar RANLIB=arm-linux-ranlib ./configure –host=arm-linux –build=i686-pc-linux-gnu –prefix=/xxx/yyy
make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED=”arm-linux-gcc -shared” CROSS_COMPILE=arm-linux- CROSS_COMPILE_TARGET=yes
make  install HOSTPYTHON=./hostpython BLDSHARED=”arm-linux-gcc -shared” CROSS_COMPILE=arm-linux- CROSS_COMPILE_TARGET=yes prefix=~/Python-3.1.2

Dependencies – Setup.config and are configuration files are required to be modify for ARM board.

Downloads required for Cross Compiling Python for Embedded Linux

  1. Setup.config

Note -Rename python_setup_file to
Links -  Download Python

python corss compile patch used.Number of View :6744

Tags: ,


Steps To Install And Use RSYNC

These are some simple steps to install and use RSYNC client.

1) Download RSYNC and its dependencies from

2) Copy all binaries in /sbin folder

3) Copy all libraries in /lib folder

4) If some library is missing download it from debian,
or compile from source if available and copy in /lib folder.

5) create /root/.ssh directories

6) Now you need to create a key,type the following command:

ssh-keygen -t dsa

If it asks you for a passphrase, just hit , to avoid putting one in.

This will create three files


7) To run rsync client run

rsync -a [email protected]:/source/ /destination/


rsync -a [email protected]:/client_pc/media /home/sami/

8) Enter the password.

In the above example,all the files in
will be copied in /home/sami/media/

Running RSYNC without prompting for password.
1) Copy the contents of created in step 6 above on the server pc in
file ~/.ssh/authorized_keys.

$ cat >>~/.ssh/authorized_keys
$ chmod 700 ~/.ssh/authorized_keys

2) Run rsync from client and test.It will not ask for password.Number of View :2875


No Comments

Content Protected Using Blog Protector By: PcDrome.