From 8b2405d5a2de1e37a8aad18832696745ea0d2467 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Wed, 6 Mar 2019 18:27:00 -0500 Subject: added feature you can now force domains to use certain programs --- urlopen.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 105 insertions(+), 32 deletions(-) diff --git a/urlopen.c b/urlopen.c index 097b9d3..dbe1223 100644 --- a/urlopen.c +++ b/urlopen.c @@ -21,6 +21,9 @@ #include #include #include +#include + +#define NOFORK 1 /* if set we wont fork and execute programs */ #define LEN(arr) ((int) (sizeof (arr) / sizeof (arr)[0])) #define BUFFSIZE 256 /* size of malloc buffers (max program/extension list length) */ @@ -33,6 +36,16 @@ char *programs[][2] = {"pdf", "/usr/bin/mupdf"} }; +char *forceddomains[][2] = +{ + /* if the odmain is listed here, the index into programs[][] + * will be the number, don't add the protocol identifier + */ + {"youtube.com", "2"}, + {"youtu.be", "2"}, + {"streamable.com", "2"} +}; + int islink(char *url) { @@ -87,6 +100,61 @@ getext(char *url) return ret; } +int +checkforceddomains(char *url, int ext) +{ + /* + * check if the domain is one we should force to use a program + */ + + /* + * checking process: + * domain is already probably a url + * check if first x characters after protocol identifier is in the list? + * return the number in specified + */ + int ret = ext; + char *buff = malloc(BUFFSIZE); + char *buff2 = malloc(BUFFSIZE); + if (buff == NULL || buff2 == NULL) + goto exitdomaincheck; /* exit right away */ + strncpy(buff, url, BUFFSIZE-1); + char *p = NULL; + p = strstr(buff, "://"); + if (p == NULL) + goto exitdomaincheck; /* exit right away */ + p += 3; /* get rid of the '://' */ + /* + * at this point we know: + * there exists '://' + * so we can tokenise a new buffer at '/' and _assume_ that is the domain + */ + strncpy(buff2, p, BUFFSIZE-1); + // wow this is bad + char *tmp = strchr(buff2, '/'); + if (tmp == NULL) /* nothing specificed after the domain, just exit */ + goto exitdomaincheck; + *(tmp) = '\0'; + /* + * now we need to loop through the array and check if there is a number to force + * we can reuse our first buffer and char pointer here + */ + for (int i = 0; i < LEN(forceddomains); i++) + { + strncpy(buff, forceddomains[i][0], BUFFSIZE-1); + if (strcmp(buff, buff2) == 0) + { + //printf("domain should be forced as %s\n", buff); + ret = atoi(forceddomains[i][1]); + goto exitdomaincheck; + } + } +exitdomaincheck: + free(buff); + free(buff2); + return ret; +} + int main(int argc, char *argv[]) { @@ -106,43 +174,48 @@ main(int argc, char *argv[]) if (link == 1) { ext = getext(argv[i]); - //printf("program to run is: \"%s %s\"\n", programs[ext][1], argv[i]); - pid_t pid = fork(); - if (pid == 0) + // check if the domain should be forced to a program + ext = checkforceddomains(argv[i], ext); + printf("program to run is: \"%s %s\"\n", programs[ext][1], argv[i]); + if (NOFORK == 0) { - // child process - // close std{out,err} - fclose(stdout); - fclose(stderr); - char *args[20]; - char *buff = malloc(BUFFSIZE); - if (buff == NULL) + pid_t pid = fork(); + if (pid == 0) { - perror("malloc"); - return -1; + // child process + // close std{out,err} + fclose(stdout); + fclose(stderr); + char *args[20]; + char *buff = malloc(BUFFSIZE); + if (buff == NULL) + { + perror("malloc"); + return -1; + } + /* + * the program we're calling might have arguments, + * so we tokenise the string and add each part to an array + * that we will use in execvp + */ + strncpy(buff, programs[ext][1], BUFFSIZE-1); + char *t = strtok(buff, " "); + int z = 0; + while (t != NULL) + { + args[z] = t; + t = strtok(NULL, " "); + z++; + } + args[z] = argv[i]; + args[z+1] = (char *)0; + execvp(args[0], args); + _exit(1); } - /* - * the program we're calling might have arguments, - * so we tokenise the string and add each part to an array - * that we will use in execvp - */ - strncpy(buff, programs[ext][1], BUFFSIZE-1); - char *t = strtok(buff, " "); - int z = 0; - while (t != NULL) + else if (pid == -1) { - args[z] = t; - t = strtok(NULL, " "); - z++; + perror("fork error"); } - args[z] = argv[i]; - args[z+1] = (char *)0; - execvp(args[0], args); - _exit(1); - } - else if (pid == -1) - { - perror("fork error"); } } } -- cgit v1.2.3