void moveFile(char* from, char* destination) // TODO: try to reduce the amount of dynamic memory allocation { char* filename = strrfind(from, '/') + 1; size_t name_len = strlen(destination) + strlen(filename) + 1; char* end_name; if (!strend(destination, "/")) { ++name_len; end_name = malloc(name_len); snprintf(end_name, name_len, "%s/%s", destination, filename); } else { end_name = malloc(name_len); snprintf(end_name, name_len, "%s%s", destination, filename); } // add (some number) to the end of the filename to avoid overwriting data char* safe_name = malloc(name_len); strcpy(safe_name, end_name); size_t safe_len; for (int m = 1; access(safe_name, F_OK) != -1; ++m) { free(safe_name); safe_len = name_len + getDigitCount(m) + 3; safe_name = malloc(safe_len); snprintf(safe_name, safe_len, "%s (%d)", end_name, m); } free(end_name); end_name = safe_name; name_len = safe_len; // shouldn't be necessary, but it keeps a safe state // write to status log writeDebug("Rename \"%s\" to \"%s\"", from, end_name); if (rename(from, end_name) < 0) { writeWarning("Unable to move file to destination"); } else { // send notification char notify_message[FILE_NAME_MAX]; snprintf(notify_message, FILE_NAME_MAX, "Rename \"%s\" to \"%s\"", from , end_name); if (isPaused()) { sendPausedNotification(from, end_name); } else { sendMovingNotification(from, end_name); } } free(end_name); }
int html_argument_parse(AUTHORITY authority, char *data, char *result) { int len1, len2, size, total_size=0; char **tag, *arg, *arg1, zero_str[1]={0x00}; while (1) { tag = strrfind(data); if (tag) { len1 = tag[0]-data; arg = tag[0]+2; len2 = tag[1]-data+2; memcpy(result, data, len2); *tag[1] = '\0'; arg1 = strchr(arg, '.'); if (arg1) *arg1++ = '\0'; else arg1 = zero_str; size = TranslateWebPara(authority, result+len1, arg, arg1); if (size < 0) { result += len2; total_size += len2; } else { result += (len1+size); total_size += (len1+size); } data += len2; } else { size = strlen(data); total_size += size; memcpy(result, data, size); break; } } return total_size; }