/*
 * uniquecheck_user.c - Implementation of libuniquecheck (user-specific)
 * Copyright (C) 1995-2026  Artyomov Alexander
 * http://self-made-free.ru/
 * aralni@mail.ru
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include "uniquecheck.h"

/* Declare external variables from uniquecheck.c */
extern char *program_name;
extern int initialized;

int uniquecheck_is_running_user(void) {
    DIR *proc_dir;
    struct dirent *entry;
    char path[512];
    char comm[256];
    char *environ_buf = NULL;
    FILE *fp;
    pid_t current_pid;
    char *username;
    char user_search[256];
    int found = 0;
    
    if (!initialized || !program_name) {
        return 0;
    }
    
    current_pid = getpid();
    
    username = getenv("USER");
    if (!username) {
        username = getenv("LOGNAME");
    }
    if (!username) {
        username = "unknown";
    }
    snprintf(user_search, sizeof(user_search), "USER=%s", username);
    
    proc_dir = opendir("/proc");
    if (proc_dir == NULL) {
        return 0;
    }
    
    while ((entry = readdir(proc_dir)) != NULL) {
        if (!isdigit(entry->d_name[0])) {
            continue;
        }
        
        if (atoi(entry->d_name) == current_pid) {
            continue;
        }
        
        snprintf(path, sizeof(path), "/proc/%s/comm", entry->d_name);
        fp = fopen(path, "r");
        if (fp == NULL) {
            continue;
        }
        
        if (fgets(comm, sizeof(comm), fp) == NULL) {
            fclose(fp);
            continue;
        }
        fclose(fp);
        
        size_t len = strlen(comm);
        if (len > 0 && comm[len-1] == '\n') {
            comm[len-1] = '\0';
        }
        
        if (strcmp(comm, program_name) == 0) {
            snprintf(path, sizeof(path), "/proc/%s/environ", entry->d_name);
            fp = fopen(path, "r");
            if (fp == NULL) {
                continue;
            }
            
            size_t buf_size = 4096;
            size_t total_read = 0;
            int ch;
            
            environ_buf = (char*)malloc(buf_size);
            if (!environ_buf) {
                fclose(fp);
                continue;
            }
            
            while ((ch = fgetc(fp)) != EOF) {
                if (total_read >= buf_size - 1) {
                    buf_size *= 2;
                    char *new_buf = (char*)realloc(environ_buf, buf_size);
                    if (!new_buf) {
                        free(environ_buf);
                        fclose(fp);
                        environ_buf = NULL;
                        break;
                    }
                    environ_buf = new_buf;
                }
                environ_buf[total_read++] = (char)ch;
            }
            fclose(fp);
            
            if (environ_buf && total_read > 0) {
                environ_buf[total_read] = '\0';
                
                char *env_ptr = environ_buf;
                while (env_ptr < environ_buf + total_read) {
                    if (strcmp(env_ptr, user_search) == 0) {
                        found = 1;
                        break;
                    }
                    env_ptr += strlen(env_ptr) + 1;
                }
            }
            
            if (environ_buf) {
                free(environ_buf);
            }
            
            if (found) {
                break;
            }
        }
    }
    
    closedir(proc_dir);
    return found;
}