1

I am making an app blocker with c++ and I need it to block urls. To block urls it will get the window name of the current tab that it is focused on. Here is the code I've got so far:

#include <iostream>
#include <glob.h>
#include <cstdio>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

using namespace std;

pid_t find_pid(const char *process_name)
{
    pid_t pid = -1;
    glob_t pglob;
    char *procname, *readbuf;
    int buflen = strlen(process_name) + 2;
    unsigned i;

    /* Get a list of all comm files. man 5 proc */
    if (glob("/proc/*/comm", 0, NULL, &pglob) != 0)
        return pid;

    /* The comm files include trailing newlines, so... */
    procname = static_cast<char*>(malloc(buflen));
    strcpy(procname, process_name);
    procname[buflen - 2] = '\n';
    procname[buflen - 1] = 0;

    /* readbuff will hold the contents of the comm files. */
    readbuf = static_cast<char*>(malloc(buflen));

    for (i = 0; i < pglob.gl_pathc; ++i) {
        FILE *comm;
        char *ret;

        /* Read the contents of the file. */
        if ((comm = fopen(pglob.gl_pathv[i], "r")) == NULL)
            continue;
        ret = fgets(readbuf, buflen, comm);
        fclose(comm);
        if (ret == NULL)
            continue;

        /*
        If comm matches our process name, extract the process ID from the
        path, convert it to a pid_t, and return it.
        */
        if (strcmp(readbuf, procname) == 0) {
            pid = (pid_t)atoi(pglob.gl_pathv[i] + strlen("/proc/"));
            break;
        }
    }

    /* Clean up. */
    free(procname);
    free(readbuf);
    globfree(&pglob);

    return pid;
}

int main(int argc, char const *argv[])
{
    cout << find_pid("brave") << endl;

    return 0;
}

I copied and modified code from this post - linux - get pid of process. Please help me.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • What is the problem you are facing? – SethMMorton Mar 18 '21 at 23:15
  • I need it to get the window title so I can check if it has words like youtube or something else so I can block it. – MintMellowCat Mar 18 '21 at 23:19
  • But the window of what? – SethMMorton Mar 18 '21 at 23:21
  • You need to narrow the question down a lot: in X11, you might do this one way. In a Wayland compositor, another. The command-line does not tell you anything (er .. not necessarily, anyway) about the things the application is displaying. – Adriaan de Groot Mar 18 '21 at 23:26
  • X11, not Wayland. I use KDE with Arch Linux and the window of the current browser that is focused on. I got the pid of that window but I don't know how to get the window title. (the window has a pid just so you know) – MintMellowCat Mar 18 '21 at 23:54
  • Please help me... – MintMellowCat Mar 19 '21 at 01:41
  • Welcome to Stack Overflow! It looks like you are asking for someone to write some code from scratch for you. Unfortunately, Stack Overflow is not a code writing service, but rather a question and answer service, where answers to specific questions are given. If you can narrow this down to specifically where you are stuck you are likely to get some help. – SethMMorton Mar 24 '21 at 04:08

0 Answers0