PHP like str_replace function in C

This post is part of a series of posts where I want to document a bunch of C functions I use for my projects that I have written, collected and modified over the past few years. One of the functions that I use regularly is a string replacement function like the str_replace in PHP. This function replaces all occurences of a particular substring with a replacement pattern. The code for this was borrowed from here. In the original code, a newly allocated string is returned after all the substrings are replaced. I have modified it so that the replacements are made in place. Continue reading

Modify argv to hide sensitive information like passwords etc

A lot of programs require you to pass sensitive information like passwords etc as arguments to the program. However if you pass passwords as arguments, it will be visible through the /proc filesystem or the ps ax output while the program is in execution. To avoid the possibility of anybody prying on sensitive information, programmers should modify the memory location where the input parameters are stored (argv array), so that it is not visible to any other users, who might have the access levels to see what processes you’re running. Jotted below is some sample code which modifies its input parameters to hide it from the proc file system. Continue reading

Sample Bluetooth RFCOMM Client App in C

I recently worked with a client who wanted a Bluetooth RFCOMM client-server application. The server would sit on an android phone, while the client will run on an embedded linux device. The rfcomm client portion was just sample code that he wanted to base his implementation on. I wrote a quick app for the same. The code you are about to see is ugly and can be rewritten to look more pleasing to the eye. Also includes a horrible goto hack. But it works and can be used to base rfcomm implementations on. Continue reading

Algorithm and Sample C Code to Check if a Number is a Palindrome – Without Reversing the Digits

A friend posted this question on my college mailing list.

How do we find if a Number is a Palindrome?

Note: No conversion to string. And also, no traditional method of dividing the number to construct the reversed number and then compare.

Here is how you go about it.

  1. Lets say the number is 1987891 (odd number of digits)
  2. Reduce it to 198891. If the number were 19877891 (even number of digits) don’t change it (basically change it to a number with even number of digits, by excluding the middle digit)
  3. Now Split it into 198 and 891
  4. If ((891-198)%9 == 0 and (891*1000+198)%11 == 0 and (198*1000+891)%11 == 0) then its a palindrome

Sample C Code

#include <stdio.h>

int numdigits(int n) {
    int count = 0;
    while(n != 0)   {
        n /= 10;
        ++count;
    }
    return count;
}

int main(void) {
    int n;
    int l;
    int a,x;
    int n1, n2, n3;
    scanf("%d",&n);
    l=numdigits(n);     /* Find the number of digits */
    a=1;
    for(x=0;x<l/2;x++)
        a*=10;
    n1=n%a;
    n2=n/a;
    if(l%2)
        n2/=10;
    if (((n1-n2)%9 == 0) && ((n2*a+n1)%11 == 0) && ((n1*a+n2)%11 == 0)) {
        printf("%d\t",n);
        return 0;
    }
    return 1;
}

To compile it

gcc -o palindrome palindrome.c

You can verify that above code works using the below script.

#!/bin/bash

for (( i = 0; i < 100000; i++ )); do
  echo $i | ./palindrome
done

The script assumes that the palindrome binary is in the present working directory.

Jotted this down for school students scouring the web for their homework answers: 😉

Creating Unique Temporary Files in C using mkstemp

There are multiple ways of creating unique temporary files in C, namely tempnam, tmpfile, mkstemp. mkstemp and tmpfile are more secure than tempnam. mkstemp additionally lets you specificy a template for the file name and the directory in which these temporary files should be created. Here’s a code snippet which uses mkstemp to create temporary files in the /tmp directory.

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

#define TEMPLATE_NAME "CLIXXXXXX"       /*Template Name*/
#define TEMPLATE_DIR "/tmp"             /*Directory*/

int main (void) {
    char *template;
    int templateSize, dirSize;
    int fd;

    templateSize  = strlen( (const char *) TEMPLATE_NAME );
    dirSize =  strlen( (const char *) TEMPLATE_DIR );
    template = (char *) malloc ((size_t) ((sizeof(char)) * (templateSize + dirSize + 2)));
    strncpy(template, (const char *) TEMPLATE_DIR, dirSize);
    strncpy(template + dirSize, (const char *) "/", (int) 1);
    strncpy(template + dirSize + 1, (const char *) TEMPLATE_NAME, templateSize);
    template[dirSize + 1 + templateSize] = '\0';
    if ((fd = mkstemp (template)) < 0 ) {
        printf("Error!!!\n");
        return -1;
    } else {
        printf("filename - %s\n", template);
        close(fd);
    }
    return 0;
}
The template name should always end with the six characters XXXXXX