Mandalika's scratchpad [ Work blog @Oracle | Stock Market Notes | My Music Compositions ]

Old Posts: 09.04  10.04  11.04  12.04  01.05  02.05  03.05  04.05  05.05  06.05  07.05  08.05  09.05  10.05  11.05  12.05  01.06  02.06  03.06  04.06  05.06  06.06  07.06  08.06  09.06  10.06  11.06  12.06  01.07  02.07  03.07  04.07  05.07  06.07  08.07  09.07  10.07  11.07  12.07  01.08  02.08  03.08  04.08  05.08  06.08  07.08  08.08  09.08  10.08  11.08  12.08  01.09  02.09  03.09  04.09  05.09  06.09  07.09  08.09  09.09  10.09  11.09  12.09  01.10  02.10  03.10  04.10  05.10  06.10  07.10  08.10  09.10  10.10  11.10  12.10  01.11  02.11  03.11  04.11  05.11  07.11  08.11  09.11  10.11  11.11  12.11  01.12  02.12  03.12  04.12  05.12  06.12  07.12  08.12  09.12  10.12  11.12  12.12  01.13  02.13  03.13  04.13  05.13  06.13  07.13  08.13  09.13  10.13  11.13  12.13  01.14  02.14  03.14  04.14  05.14  06.14  07.14  09.14  10.14  11.14  12.14  01.15  02.15  03.15  04.15  06.15  09.15  12.15  01.16  03.16  04.16  05.16  06.16  07.16  08.16  09.16  12.16  01.17  02.17  03.17  04.17  06.17  07.17  08.17  09.17  10.17 


Saturday, January 31, 2015
 
Programming in C: Few Tidbits #4

1. Using Wildcards in Filename Pattern Matching

Relying on *stat() API is not much of an option when using wildcards to match a filename pattern. Some of the options involve traversing a directory checking each file for a match using fnmatch(), or to use system() function to execute an equivalent shell command. Another option that is well suited for this task is the glob*() API, which is part of Standard C Library Functions. (I believe glob() depends on fnmatch() for finding matches).

Here is a simple example that displays the number of matches found for pattern "/tmp/lint_" along with the listing of matches.

% ls -1 /tmp/lint_*
/tmp/lint_AAA.21549.0vaOfQ
/tmp/lint_BAA.21549.1vaOfQ
/tmp/lint_CAA.21549.2vaOfQ
/tmp/lint_DAA.21549.3vaOfQ
/tmp/lint_EAA.21549.4vaOfQ
/tmp/lint_FAA.21549.5vaOfQ
/tmp/lint_GAA.21549.6vaOfQ


% cat match.c
#include <stdio.h>
#include <glob.h>

int main(int argc, char **argv) {

        glob_t buf;

        if (argc == 1) return 0;

        glob(argv[1], 0 , NULL , &buf);

        printf("\nNumber of matches found for pattern '%s': %d\n",
                argv[1], buf.gl_pathc);

        for (int i = 0; i < buf.gl_pathc; ++i) {
                printf("\n\t%d. %s", (i + 1), buf.gl_pathv[i]);
        }

        globfree(&buf);

        return 0;
}

% cc -o match match.c

% ./match /tmp/lint_\*

Number of matches found for pattern '/tmp/lint_*': 7

        1. /tmp/lint_AAA.21549.0vaOfQ
        2. /tmp/lint_BAA.21549.1vaOfQ
        3. /tmp/lint_CAA.21549.2vaOfQ
        4. /tmp/lint_DAA.21549.3vaOfQ
        5. /tmp/lint_EAA.21549.4vaOfQ
        6. /tmp/lint_FAA.21549.5vaOfQ
        7. /tmp/lint_GAA.21549.6vaOfQ

Please check the man page out for details -- glob(3C).

2. Microtime[stamp]

One of the old blog posts has an example to extract the current timestamp using time API. It shows the timestamp in standard format month-date-year hour:min:sec. In this post, let's add microseconds to the timestamp.

Here is the sample code.

% cat microtime.c
#include <stdio.h>
#include <time.h>

int main() {

        char timestamp[80], etimestamp[80];
        struct timeval tmval;
        struct tm *curtime;

        gettimeofday(&tmval, NULL);

        curtime = localtime(&tmval.tv_sec);
        if (curtime == NULL) return 1;

        strftime(timestamp, sizeof(timestamp), "%m-%d-%Y %X.%%06u", curtime);
        snprintf(etimestamp, sizeof(etimestamp), timestamp, tmval.tv_usec);

        printf("\ncurrent time: %s\n", etimestamp);
        return 0;

}

% cc -o mtime microtime.c

% ./mtime
current time: 01-31-2015 15:49:26.041111

% ./mtime
current time: 01-31-2015 15:49:34.575214

One major change from old approach is the reliance on gettimeofday() since it returns a structure [timeval] with a member variable [tv_usec] to represent the microseconds.

strftime() fills up the date/time data in timestamp variable as per the specifiers used in time format (third argument). By the time strftime() completes execution, timestamp will have month-date-year hr:min:sec filled out. Subsequent snprintf fills up the only remaining piece of time data - microseconds - using the tv_usec member in timeval structure and writes the updated timestamp to a new variable, etimestamp.

Credit: stackoverflow user unwind.

3. Concatenating Multi-Formatted Strings

I have my doubts about this header - so, let me show an example first. The following rudimentary example attempts to construct a sentence that is something like "value of pi = (22/7) = 3.14". In other words, the sentence has a mixture of character strings, integers, floating point number and special characters.

% cat fmt.c
#include <stdio.h>
#include <string.h>

int main() {

        char tstr[48];
        char pistr[] = "value of pi = ";
        int num = 22, den = 7;
        float pi = ((float)num/den);

        char snum[8], sden[8], spi[8];

        sprintf(sden, "%d", den);
        sprintf(snum, "%d", num);
        sprintf(spi, "%0.2f", pi);

        strcpy(tstr, pistr);
        strcat(tstr, "(");
        strcat(tstr, snum);
        strcat(tstr, "/");
        strcat(tstr, sden);
        strcat(tstr, ") = ");
        strcat(tstr, spi);

        puts(tstr);
        return 0;

}

% cc -o fmt fmt.c

% ./fmt
value of pi = (22/7) = 3.14

Nothing seriously wrong with the above code. It is just that it uses a bunch of sprintf(), strcpy() and strcat() calls to construct the target string. Also it overallocates the memory required for the actual string.

The same effect can be achieved by using asprintf(). The resulting code will be much smaller and easy to maintain however. This function also eases the developer from the burden of allocating memory of appropriate size. In general, overallocation leads to memory wastage and underallocation likely leads to buffer overflows posing unnecessary security risks. When relying on asprintf(), developers are not relieved from two factors though -- checking the return value to see if the call succeeded, and in freeing up the buffer when done with it. Ignoring those two aspects lead to program failures in the worst case, and memory leaks are almost guaranteed.

Here is the alternate version that achieves the desired effect by making use of asprintf().

% cat ifmt.c
#include <stdio.h>
#include <stdlib.h>

int main() {

        char *tstr;
        int num = 22, den = 7;
        float pi = ((float)num/den);

        int ret = asprintf(&tstr, "value of pi = (%d/%d) = %0.2f", num, den, pi);

        if (ret == -1) return 1;

        puts(tstr);
        free(tstr);
        return 0;

}

% cc -o ifmt ifmt.c

% ./ifmt
value of pi = (22/7) = 3.14

Also see: snprintf()

Labels:





2004-2017 

This page is powered by Blogger. Isn't yours?