
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct myProcess {
    char name[64];
    int pid;
} v[16384];

inline int convertIntToStr(char *str) {
    int n = strlen(str), i = 0, ans = 0;
    for(i = 0; i < n; ++ i)
        ans = ans * 10 + (str[i] - '0');
    return ans;
}

int main() {
    FILE *fp;
    char path[1035];

    /* Open the command for reading. */
    fp = popen("tasklist", "r");
    if (fp == NULL) {
        printf("Failed to run command\n" );
        exit(1);
    }

    /* Read the output a line at a time - output it. */
    int i, pos = 0;
    for(i = 0; i < 4; ++ i)
        fgets(path, sizeof(path) - 1, fp);
    while(fgets(path, sizeof(path) - 1, fp) != NULL) {
        //printf("%s", path);
        char *pch = strtok(path, " "), *name, *pidstr;
        struct myProcess p;
        strcpy(p.name, pch);
        pch = strtok(NULL, " ");
        p.pid = convertIntToStr(pch);
        //printf("%s %d\n", p.name, p.pid);
        v[pos ++] = p;
    }

    char pname[64];
    printf("Enter process name: ");
    gets(pname);
    for(i = 0; i < pos; ++ i) {
        //TODO: add strstr
        if(!strcmp(v[i].name, pname)) {
            printf("%s %d\n", v[i].name, v[i].pid);
        }
    }

    /* close */
    pclose(fp);
    return 0;
}

