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()
No comments:
Post a Comment