All pastes #491818 Raw Edit

Someone

public text v1 · immutable
·published 2007-05-16 21:22 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>

#define WEBDIR "/tmp"

int mkwebdir(char *, char *);

int main(char *argv[], int argc) {
    mkwebdir("testing", "sjk");
}

int mkwebdir(char *dirname, char *owner) {
    printf("Dirname: %s (length %d)\nOwner: %s\n", dirname, strlen(dirname), owner);
    printf("WEBDIR: %s, length: %d\n", WEBDIR, sizeof(WEBDIR));
    printf("Mallocing mem for %s + / + %s (%d + 1 + %d)\n", WEBDIR, dirname, sizeof(WEBDIR), strlen(dirname));
    char *mydir;
    if ((mydir = malloc(sizeof(WEBDIR) + 1 + strlen(dirname))) == NULL) {
        fprintf(stderr, "Couldn't malloc!\n");
        exit(1);
    }

    printf("Just alloced mydir. sizeof(mydir) = %d\n", sizeof(mydir));
    strlcat(mydir, WEBDIR, sizeof(WEBDIR));
    printf("mydir is now %s\n", mydir);
    strlcat(mydir, dirname, strlen(dirname));
    printf("And now it's %s\n", mydir);

    return 0;
}

Output:
Dirname: testing (length 7)
Owner: sjk
WEBDIR: /tmp, length: 5
Mallocing mem for /tmp + / + testing (5 + 1 + 7)
Just alloced mydir. sizeof(mydir) = 4
mydir is now /tmp
And now it's /tmpte