Untitled

 avatar
unknown
plain_text
2 years ago
1.5 kB
5
Indexable
#include <windows.h>
#include <stdio.h>
#include <stdbool.h>

bool ArchiveFiles(const char* sourceDir, const char* archiveName) {
    char command[256];
    sprintf(command, "tar -cf \"%s\" \"%s\"", archiveName, sourceDir);

    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    memset(&si, 0, sizeof(si));
    memset(&pi, 0, sizeof(pi));
    si.cb = sizeof(si);

    if (CreateProcess(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
        printf("Archiving complete.\n");
        return true;
    } else {
        printf("Failed to create process.\n");
        return false;
    }
}

int main() {
    char sourceDir[MAX_PATH];
    char archiveName[MAX_PATH];

    printf("Enter the source directory: ");
    if (fgets(sourceDir, sizeof(sourceDir), stdin) == NULL) {
        printf("Invalid input.\n");
        return 1;
    }
    sourceDir[strcspn(sourceDir, "\n")] = '\0';  // Remove trailing newline

    printf("Enter the archive name: ");
    if (fgets(archiveName, sizeof(archiveName), stdin) == NULL) {
        printf("Invalid input.\n");
        return 1;
    }
    archiveName[strcspn(archiveName, "\n")] = '\0';  // Remove trailing newline

    if (sourceDir[0] == '\0' || archiveName[0] == '\0') {
        printf("Source directory and archive name cannot be empty.\n");
        return 1;
    }

    if (ArchiveFiles(sourceDir, archiveName)) {
        return 0;
    } else {
        return 1;
    }
}
Editor is loading...