C - Some Simple Examples of using strcat

16. February 2013 11:06

 

This post shows some simple example of using strcat to join two strings together in C

 

The msot common example you will find on the internet will work by just making an array that is "big enough" to store both the strings. However this can create problems when the strings you use are too long so it would need these conditions handled correctly in C. So it probably isn't the best example to use.

 

 

void basic(char *s1, char *s2)
{
	char str[200] = "";
	
	strcat(str, s1);
	strcat(str, s2);
	printf("basic: %s\n", str);
}

 

 

A better example of using strcat is to allocate an array dynamically. In this case on the stack. So this will work slightly better however it can cause a significant amount of stack space to be used and can also cause crashes if the strings are too long. However it will execute very quickly because dynamic allocation of memory on the stack will perform well.

 

 

void better1(char *s1, char *s2)
{
	char str[strlen(s1) + strlen(s2) * sizeof(*s1) + 1];
	str[0] = '\0';

	strcat(str, s1);
	strcat(str, s2);
	printf("basic: %s\n", str);
}

 

 

A much better example of this is to use malloc / free for the allocation of string. This will be slightly slower than the previoud example but will be able to handler much larger strings and much safer to use. It can of course still crash but only if the machine run's out of memory. How ever this error can be handled to prevent it crashing by changing the call to abort() to use proper error handling.

 

 

void better2(char *s1, char *s2)
{
	char *str = malloc(strlen(s1) + strlen(s2) + 1 * sizeof(*s1));
	if (str == NULL)
		abort();
	str[0] = '\0';
	
	strcat(str, s1);
	strcat(str, s2);
	printf("basic: %s\n", str);
		
	free(str);
}

 

Finally this is an example about joining multiple strings so we can just keep making our string longer and longer or join it from multiple different sources. Note that the following code really needs a check around the realloc function to see if it returns NULL. If it does then it should fail by returning the previous pointer. Or call abort because the system has run out of memory.

 

 

char *multiple(char *str, char *s2)
{
	int len;
	char *s;
	if (str != NULL)
		len = strlen(str);
	len += strlen(s2) + 1 * sizeof(*s2);
	s = realloc(str, len);
	strcat(s, s2);
	return s;
}

 

With the above code we can call it in the following way

 

 

char *tmp = multiple(NULL, "Hello ");
	tmp = multiple(tmp, " World");
	tmp = multiple(tmp, " We");
	tmp = multiple(tmp, " Can");
	tmp = multiple(tmp, " Join");
	tmp = multiple(tmp, " strings");
	printf("%s\n", tmp);
	free(tmp);

 

E-mail Kick it! DZone it! del.icio.us Permalink


Linux Programming - Using inotify for detecting file modifications

22. September 2012 09:21

 

This is a short introduction to using the inofity api in linux to detect file system modifications for specific files. Which can be useful for a number of reasons. The particular reason why I wrote the example program here was to reload parts of apache when python scripts were being modified while doing some web development. the problem being that apache + django caches the python files.

 

The example here is simple. It will take a file list to monitor and will then execute a command when one of thoose files are modified. Though it also shows a simple example of how to use the inofity api in linux.

 

To start with you will need to get a suitable file descriptor by calling inotify_init and then setup the files you would like to monitor you can do this by using the function inotify_add_watch with the correct arguments to be notified when files changes. See below for the first half of the program. Note that the program expects the file list to be passed on the command line.

 

int main(int argc, char **argv) {
	int fdnotify = -1;
	int i;

	fdnotify = inotify_init();
	if (fdnotify < 0) {
		fprintf(stderr, "inotify_init failed: %s\n", strerror(errno));
		exit(-1);
	}

	for(i = 2;i<argc;i++) {
		int wd = inotify_add_watch(fdnotify, argv[i], IN_MODIFY);
		if (wd < 0) {
			fprintf(stderr, "inotify_add_watch failed: %s\n", strerror(errno));
		}
	}

 

Once the initial list has been completed you just need to wait for a notification which is done by reading from the file decriptor with the inotify_event data structure and just keep reading from that file descriptor in a loop.

 

while(1) {
		char buffer[4096];
		struct inotify_event *event = NULL;
		int exec = 0;

		int len = read(fdnotify, buffer, sizeof(buffer));
		if (len < 0) {
			fprintf(stderr, "read: %s\n", strerror(errno));
			exit(-1);
		}

		event = (struct inotify_event *) buffer;
		while(event != NULL) {
			if ( (event->mask & IN_MODIFY) && event->len > 0) {
				printf("File Modified: %s\n", event->name);
				exec = 1;
			} else {
				printf("Unknown Mask 0x%.8x\n", event->mask);
			}
				
			/* Move to next struct */	
			len -= sizeof(*event) + event->len;
			if (len > 0)
				event = ((void *) event) + sizeof(event) + event->len;
			else
				event = NULL;
		}
	
		if (exec)	
			system(argv[1]);
	}

And that is really it. Note that I wanted to "eat" most of the file notification events before running the external script so that it would only execute the script once. It can also serve other nice purposes eg it can be used to auto rebuild a c project on the file by using it like this "./inotify make `find . -type f -name '*.[ch]'" which will run make any time any of the .c, .h files change. the program has 100's of useful ways to be used for being able to trigger shell scripts from filesystem events.

 

Here is a copy of the full program

 

 

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

#include <sys/inotify.h>

void printhelp(FILE *out, char *app) {
	fprintf(out, "Usage: %s <command> <dirs to watch>\n", app);
}

int main(int argc, char **argv) {
	int fdnotify = -1;
	int i;

	fdnotify = inotify_init();
	if (fdnotify < 0) {
		fprintf(stderr, "inotify_init failed: %s\n", strerror(errno));
		exit(-1);
	}

	if (argc < 3) {
		fprintf(stderr, argv[0]);
		exit(-1);
	}

	for(i = 2;i<argc;i++) {
		int wd = inotify_add_watch(fdnotify, argv[i], IN_MODIFY);
		if (wd < 0) {
			fprintf(stderr, "inotify_add_watch failed: %s\n", strerror(errno));
		}
	}

	while(1) {
		char buffer[4096];
		struct inotify_event *event = NULL;
		int exec = 0;

		int len = read(fdnotify, buffer, sizeof(buffer));
		if (len < 0) {
			fprintf(stderr, "read: %s\n", strerror(errno));
			exit(-1);
		}

		event = (struct inotify_event *) buffer;
		while(event != NULL) {
			if ( (event->mask & IN_MODIFY) && event->len > 0) {
				printf("File Modified: %s\n", event->name);
				exec = 1;
			} else {
				printf("Unknown Mask 0x%.8x\n", event->mask);
			}
				
			/* Move to next struct */	
			len -= sizeof(*event) + event->len;
			if (len > 0)
				event = ((void *) event) + sizeof(event) + event->len;
			else
				event = NULL;
		}
	
		if (exec)	
			system(argv[1]);
	}

	return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink


C - Is the input a tty?

15. March 2012 08:00

 

In a C program it can be useful sometimes to know if the stdin / stdout / stderr is actually connected to a termianl or not. There is a function alled isatty() which is dedicated to detecting this. An example is below.

 

 

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

int main(int argc, char **argv) {
    if (argc < 2) {
        printf("ERROR: Need a file descriptor\n");
        exit(0);
    }
    printf("%d\n", isatty(atoi(argv[1])));
    return 0;
}

 

E-mail Kick it! DZone it! del.icio.us Permalink


C - getenv

10. March 2012 08:00

 

A C example to use getenv to get and print an enviroment variable.

 

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

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

	char *var = getenv("HOME");
	
	if (var == NULL)
		printf("Not Set");
	else
		printf("var = %s\n", var);

	return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink


C - get home dir location in linux

20. February 2012 08:00

 

This is a short example of how to get the home directory location in linux in a C program. There is really a few choices here and depending on what your requirements are you might want to use one or other methods. The first method can be changed by the user. The second method cannot. However if the first method fails you should drop faill back on the other method.

 

The simple method is to pull the enviroment variable "HOME"

 

The slightly more complex method is to read it from the system user database.

 

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>


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


        char *homedir = getenv("HOME");

        if (homedir != NULL) {
                printf("Home dir in enviroment");
                printf("%s\n", homedir);
        }

        uid_t uid = getuid();
        struct passwd *pw = getpwuid(uid);

        if (pw == NULL) {
                printf("Failed\n");
                exit(EXIT_FAILURE);
        }

        printf("%s\n", pw->pw_dir);

        return 0;
}

 

E-mail Kick it! DZone it! del.icio.us Permalink