コード例 #1
0
ファイル: main.c プロジェクト: quxiaofeng/zoj-solutions
void sovle (char * basicList, int basicCharsLeft, char * goalList, int goalCharsLeft, char * result, int resultLength, int resultIdx) {
    char tempChar = ' ';
    int i = 0;
    char * searchResult = (char *)malloc(resultLength);
    memcpy(searchResult, result, resultLength);

    if (goalCharsLeft > 1 && basicCharsLeft < 1 && !STtopEqual(goalList[goalCharsLeft - 1]) ) {
        return;
    }

    if (goalCharsLeft < 1) {
        for (i = 0; i < resultLength; i ++) {
            printf("%c", result[i]);
        }
        printf("\n");
        return;
    }

    if ( basicCharsLeft > 0 ) {
        STpush(basicList[basicCharsLeft - 1]);
        basicCharsLeft -= 1;
        searchResult[resultIdx++] = 'i';
        searchResult[resultIdx++] = ' ';
        sovle( basicList, basicCharsLeft, goalList, goalCharsLeft, searchResult, resultLength, resultIdx);
        basicCharsLeft += 1;
        resultIdx -= 2;
        searchResult[resultIdx] = ' ';
        STpop(&tempChar);
    }
    if ( STtopEqual(goalList[goalCharsLeft - 1]) ) {
        STpop(&tempChar);
        goalCharsLeft -= 1;
        searchResult[resultIdx++] = 'o';
        searchResult[resultIdx++] = ' ';
        sovle( basicList, basicCharsLeft, goalList, goalCharsLeft, searchResult, resultLength, resultIdx);
        goalCharsLeft += 1;
        resultIdx -= 2;
        searchResult[resultIdx] = ' ';
        STpush(tempChar);
    }
}
コード例 #2
0
void recursiveCopy(ST stack, char *src_folder, char *dst_folder)
{
	char *fname, *folder_to_copy, *new_folder, *tmp_folder, *src, *dst, *rel_path;
	int src_malloc_size, dst_malloc_size;
	int src_len = strlen(src_folder), dst_len = strlen(dst_folder), tmp_len, fname_len;
	
	struct stat stat_buf, shared_stat_buf;
	DIR *dir_stream;
	struct dirent *dir_entry;

	// The stack only contains folder _without_ the src/dst directory as a prefix.
	// This is a `do {} while(!STempty());`
	do
	{
		tmp_folder = STpop(stack);
		tmp_len = strlen(tmp_folder);

		// This if/else allocates memory for the paths to be read and to be created;
		// the final paths are complete, not relative.
		// It eventually creates the folder in the new location.
		// N.B: In the allocations, the trailing '/' is NOT included
		dst_malloc_size = dst_len + tmp_len + 1 + 1;
		src_malloc_size = src_len + tmp_len + 1 + 1;

		new_folder = malloc(sizeof(char) * dst_malloc_size);
		sprintf(new_folder, "%s%s", dst_folder, tmp_folder);

		folder_to_copy = malloc(sizeof(char) * src_malloc_size);
		sprintf(folder_to_copy, "%s%s", src_folder, tmp_folder);

		if (mkdir(new_folder, 0755) != 0)
		{
			fprintf(stdout, "Errno creating folder in func#%d\n", errno);
			fprintf(stderr, "Error while creating %s\n", new_folder);

		}

		fprintf(stdout, "\n+Copying %s:\n", folder_to_copy);

		if (stat(folder_to_copy, &stat_buf) != 0)
		{
			fprintf(stderr, "Error while getting info on the directory %s\n", folder_to_copy);
			continue;
		}

		dir_stream = opendir(folder_to_copy);
		if (dir_stream == NULL)
		{
			fprintf(stdout, "Error while opening the stream %s\n", folder_to_copy);
			continue;
		}

		dir_entry = readdir(dir_stream);
		while (dir_entry != NULL)
		{
			fname = strdup(dir_entry->d_name);
			fname_len = strlen(fname);

			dst_malloc_size += fname_len;
			dst = malloc(sizeof(char) * dst_malloc_size);
			sprintf(dst, "%s%s", new_folder, fname);
			
			src_malloc_size += fname_len;
			src = malloc(sizeof(char) * src_malloc_size);
			sprintf(src, "%s%s", folder_to_copy, fname);

			if (stat(src, &shared_stat_buf) != 0)
			{
				fprintf(stdout, "Generic stat error\n");
				continue;
			}

			if (!S_ISDIR(shared_stat_buf.st_mode))
			{
				fprintf(stdout, "%s...", fname);
				if (copyFile(src, dst))
				{
					fprintf(stdout, "Error while copying the file");
				}
				fputc('\n', stdout);
			}
		
			else
			{
				if (!(strcmp(fname, DOT) == 0 ||  strcmp(fname, DOTDOT) == 0))
				{
					fprintf(stdout, "%s->Saving this for later\n", fname);
					rel_path = malloc(sizeof(char) * (tmp_len + fname_len + 1 + 1));
					sprintf(rel_path, "%s%s/", tmp_folder, fname);
					STpush(stack, rel_path);
				}
			}
			dir_entry = readdir(dir_stream);

			free(src);
			free(dst);
			free(fname);
		}
		if (closedir(dir_stream))
		{
			fprintf(stderr, "Error while closing DIR* stream\n");
			exit(1);
		}

		free(new_folder);
		free(folder_to_copy);
		free(tmp_folder);
	}
	while (!STempty(stack));
	
	return;
}