static char *cat_paths(const char *a, const char *b) { char *str; size_t sz; int as, bs; size_t na, nb; if (a && !b) return copy_string(a); if (b && !a) return copy_string(b); if (!a && !b) return NULL; as = 0; bs = 0; na = strlen(a); nb = strlen(b); if (na && a[na - 1] == '/') as = 1; if (nb && b[0] == '/') bs = 1; if ((as && !bs) || (!as && bs)) return cat_strings(a, b); if (as && bs) return cat_strings(a, b + 1); sz = na + nb + 2; str = (char *) malloc(sz); CHECK(str != NULL); strcpy(str, a); strcat(str, "/"); strcat(str, b); return str; }
int main(int argc, char *argv[]) { char message[1024]; memset(message, 0, sizeof(message)); cat_strings(message, 4, "I love ", "his ", "dark ", "hair."); printf("%s\n", message); return 0; }
// removing temporary files from the HDD int rm_tmp_file(char* tmp_name) { char *command; if ( file_exist(tmp_name) ) { #if defined(__linux) || defined(__APPLE__) command = strdup("rm -f "); #else command = strdup("del "); #endif command = cat_strings(command, tmp_name); if ( system(command) == -1 ) { fprintf(stderr,"Error on removing %s.\n", tmp_name); return -1; } free(command); } return 0; }