Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void cpy(char** d, char** s) {
- int n = strlen(*s);
- (*d) = (char*)malloc(sizeof(char) * n);
- for (int i = 0; i < n; i++) {
- (*d)[i] = (*s)[i];
- }
- }
- char* get_command(char* commands[], int n) {
- int size = n;
- for (int i = 0; i < n; i++) {
- size += strlen(commands[i]);
- }
- char* result = (char*)malloc(sizeof(char) * size);
- int itr = 0;
- for (int i = 0; i < n; i++) {
- int cur_len = strlen(commands[i]);
- for (int j = 0; j < cur_len; j++) {
- result[itr++] = commands[i][j];
- }
- result[itr++] = ' ';
- }
- result[itr - 1] = '\0';
- return result;
- }
- FILE* execute(char* commands[], int n, const char* type) {
- FILE* stream = popen(get_command(commands, n), type);
- return stream;
- }
- int main(int argc, char* argv[]) {
- if (argc < 3) {
- printf("WRONG INPUT\n");
- return 1;
- }
- int size1 = 1;
- char border = 0;
- int size2 = 1;
- for (int i = 2; i < argc; i++) {
- if (argv[i][0] != '-') {
- border = 1;
- } else if (border) {
- size2++;
- } else {
- size1++;
- }
- }
- char* argv1[size1 + 1];
- argv1[size1] = NULL;
- char* argv2[size2 + 1];
- argv2[size2] = NULL;
- for (int i = 0; i < size1; i++) {
- cpy(&argv1[i], &argv[i + 1]);
- }
- for (int i = 0; i < size2; i++) {
- cpy(&argv2[i], &argv[i + 1 + size1]);
- }
- FILE* first_stream = execute(argv1, size1, "r");
- FILE* second_stream = execute(argv2, size2, "w");
- const int BUFFER_SIZE = 1 << 10;
- char BUFFER[BUFFER_SIZE];
- while (fgets(BUFFER, sizeof(BUFFER), first_stream)) {
- fprintf(second_stream, "%s", BUFFER);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement