diff options
-rw-r--r-- | urlopen.c | 137 |
1 files changed, 105 insertions, 32 deletions
@@ -21,6 +21,9 @@ #include <unistd.h> #include <errno.h> #include <signal.h> +#include <sys/types.h> + +#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) { @@ -88,6 +101,61 @@ getext(char *url) } 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[]) { int link = 0; @@ -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"); } } } |