|
|
1.1 ! root 1: /* ! 2: * Function flatten() concatinates an array of the strings to a string, ! 3: * where the input strings are separated by spaces. ! 4: */ ! 5: #include <misc.h> ! 6: ! 7: #define BUF_SIZE 80 /* Size of the buffer to be alloc */ ! 8: ! 9: char *flatten(argv) ! 10: char *argv[]; ! 11: { ! 12: extern char *realloc(); ! 13: char *buf; /* Buffer for output string */ ! 14: unsigned count = BUF_SIZE; /* Size of the buffer */ ! 15: unsigned len; /* Current length used */ ! 16: unsigned i; /* Length of new addition */ ! 17: ! 18: buf = alloc(BUF_SIZE); ! 19: *buf = '\0'; ! 20: len = 1; /* We always need a terminator */ ! 21: ! 22: for (; *argv != NULL; argv++) { ! 23: while ((len + (i = strlen(*argv))) > count) { ! 24: count += BUF_SIZE; ! 25: if ((buf = realloc(buf, count)) == NULL) ! 26: fatal("out of space"); ! 27: } ! 28: ! 29: /* If it is not the first string, write string separator */ ! 30: if (len > 1) ! 31: strcpy(buf + len++ - 1, " "); ! 32: ! 33: strcpy(buf + len - 1, *argv); ! 34: len += i; ! 35: } ! 36: return (realloc(buf, len)); ! 37: } ! 38: ! 39: #ifdef TEST ! 40: main(argc, argv) ! 41: int argc; ! 42: char **argv; ! 43: { ! 44: puts(flatten(argv)); ! 45: } ! 46: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.