Thread: Logical AND in execl()?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    121

    Logical AND in execl()?

    Hi,

    I'm trying to create a program that will grab the contents of the clipboard (youtube video link), and download the video. DONE.

    Problem encounter is that I need to know if a video was successfully downloaded or if it needs further attention. My thought was to have my program open up an xterm window and execute the command. This way if an error has occurred, the window will display the error. And if I do a bunch of videos, successfully, I end up with a bunch of windows that I need to close. I would like it to close the window if the downloading was successful, and leave the window if there's something that needs more attention.

    To do this, I feel, I need a logical AND in my `execl()` call. But all forms that I have tried don't work. I am not sure where to put the logical AND, or if it can be used at all.

    Can someone offer some insight into this, please?

    I've tried:
    Code:
    execl("/usr/bin/xterm", "xterm && exit", "-name", "Downloader", "-e", "yt-dlp", url, (char *) NULL);
    execl("/usr/bin/xterm", "xterm", "-name", "Downloader", "-e", "yt-dlp && exit", url, (char *) NULL);
    execl("/usr/bin/xterm", "xterm", "-name", "Downloader", "-e", "yt-dlp", url, "&&", "exit", (char *) NULL);
    execl("/usr/bin/xterm", "xterm", "-name", "Downloader", "-e", "yt-dlp", url, "&& exit", (char *) NULL);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well the short answer is no you can't.

    execl is NOT a command parser, which is what you need to interpret what && means.
    execl will literally pass the string "&&" as some argv[i] to whatever program you try and invoke.

    The first question is, how are you determining success or failure?

    I'm concerned that your program is really just a shell script masquerading as a C program, consisting of a bunch of system()/execl() calls.

    Anyhow, I managed to make this work.
    Logical AND in execl()?-screenshot-2023-09-19-20-49-33-png
    Essentially, you need a second script to do all the work, and you 'source' it from the command line of your xterm invocation.

    The execl call would be something like
    Code:
    execl("/usr/bin/xterm", "xterm", "-title", "Downloader", "-e", "/bin/bash", "-c", "source bar.sh 1 2", (char *) NULL);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    121
    Quote Originally Posted by Salem View Post
    The first question is, how are you determining success or failure?
    I am determining success from the return code of `yt-dlp`. Or at least, I'd like to.

    Quote Originally Posted by Salem View Post
    I'm concerned that your program is really just a shell script masquerading as a C program, consisting of a bunch of system()/execl() calls.
    I understand where you're coming from. I have seen the types of questions you occasionally get here. But I have been here for, I think, over 10 years. I have stopped using global variables (if absolutely possible), learned how to properly use pointers and I WILL NOT use system calls. Here is my working code thus far.
    Code:
    // Declare includes.
    #include <errno.h>
    #include <gtk/gtk.h>
    #include <libgen.h>
    #include <stdio.h>
    #include <sys/stat.h>
    
    // Declare defines.
    #define DOWNLOAD_DIRECTORY   "/home/download_directory/"
    
    // Declare function prototypes.
    void clipboard_callback (GtkClipboard *clip, const gchar *url, gpointer data);
    void gtk_program (void);
    
    int main (int argc, char *argv[])
    {
    // Check number of arguments.
        if(argc != 1)
        {
            fprintf(stderr, "Usage: %s\n", program_invocation_short_name);
            exit(EXIT_FAILURE);
        }
    
    // Must be at the start of the program, before any GTK code.
        gtk_init(&argc, &argv);
    
    // Run process.
        gtk_program();
    
    // Exit cleanly.
        exit(EXIT_SUCCESS);
    }
    
    void gtk_program (void)
    {
    // Declare variables.
        GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    
    // Get clipboard contents and process.
        gtk_clipboard_request_text(clip, clipboard_callback, NULL);
        gtk_main();
    }
    
    void clipboard_callback (__attribute__ ((unused)) GtkClipboard *clip, const gchar *url, __attribute__ ((unused)) gpointer data)
    {
    // Create download directory.
        if(mkdir(DOWNLOAD_DIRECTORY, 0755) == -1 && errno != EEXIST)
        {
            fprintf(stderr, "%s: %s error: mkdir failed (%s) (%s)\n", program_invocation_short_name, __func__, strerror(errno), DOWNLOAD_DIRECTORY);
            exit(EXIT_FAILURE);
        }
    
    // Move to download directory.
        if(chdir(DOWNLOAD_DIRECTORY) == -1)
        {
            fprintf(stderr, "%s: %s error: chdir failed (%s) (%s)\n", program_invocation_short_name, __func__, strerror(errno), DOWNLOAD_DIRECTORY);
            exit(EXIT_FAILURE);
        }
    
        execl("/usr/bin/xterm", "xterm", "-name", "Downloader", "-e", "yt-dlp", url, (char *) NULL);
    //    gtk_main_quit();
    }
    Quote Originally Posted by Salem View Post
    Essentially, you need a second script to do all the work, and you 'source' it from the command line of your xterm invocation.
    Yeah, I feel if I need to put this all in a script, then having a program call the script seems superfluous.

    This was a desire to try and run a bash function (located in my `.aliases` file) from a program that reads from the clipboard. Didn't readily see a way to run bash functions in an `.aliases' file from a c program. Figured it was easy to re-create (which it was), but the remaining windows (after success) are a little much. If I run the function from a bash window, it closes on success all ready.

    Thank you, I appreciate your help Salem.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    At a command prompt, manually type in a couple of commands

    Code:
    yt-dlp https://www.youtube.com/watch?v=HeQX2HjkcNo&pp=ygUKdmVyaXRhc2l1bQ%3D%3D
    echo $?
    yt-dlp https://www.youtube.com/watch?v=badstring
    echo $?
    Do you get like 0 for the first one and 1 for the second?

    In other words, does it return a meaningful exit status you can use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    121
    Quote Originally Posted by Salem View Post
    Do you get like 0 for the first one and 1 for the second?

    In other words, does it return a meaningful exit status you can use.
    Yes and yes. 0 for completed download. 1 for download incomplete for a myriad of reasons (bad link, interrupted download, no internet connection...). Basically, if it's 0, remove the window.

    It can be done another way, I was just trying something new. I can always open a pipe and deal with `yt-dlp` and then close the window on success.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need fork / exec / wait.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/wait.h>
    int main()
    {
        pid_t child = fork();
        if ( child == 0 ) {
            char *cmd[] = {
                "/bin/bash",
                "-c",
                "true", // or "false",
                NULL
            };
            execv(cmd[0],cmd);
        } else {
            int status;
            wait(&status);
            printf("Child %lu exited with status %d\n", (unsigned long)child, WEXITSTATUS(status));
        }
    }
    For advanced use, you can use waitpid(), keep track of spawned children (and corresponding URLs) in some data structure to implement concurrent downloads.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    121
    Quote Originally Posted by Salem View Post
    You need fork / exec / wait.
    For advanced use, you can use waitpid(), keep track of spawned children (and corresponding URLs) in some data structure to implement concurrent downloads.
    This is better than what I was going to resort to, thank you.

    Quote Originally Posted by Salem View Post
    At a command prompt, manually type in a couple of commands
    Code:
    yt-dlp https://www.youtube.com/watch?v=HeQX2HjkcNo
    I have a problem with this video. They talk about the fundamental flaw in 'Maths', but he is talking with infinities. I don't know of any laws of science or 'Maths' that deal with truth in infinity. The only time infinity seems to be considered is in theory. Theory is what you use when you don't fully understand what you're talking about. And to be so naive to think we know about all numbers and then go off on a jag about how there is this problem. I feel this video is very short sighted... assuming it's not to sow the seeds of deception.

  8. #8
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by Yonut View Post
    I have a problem with this video. They talk about the fundamental flaw in 'Maths', but he is talking with infinities. I don't know of any laws of science or 'Maths' that deal with truth in infinity. The only time infinity seems to be considered is in theory. Theory is what you use when you don't fully understand what you're talking about. And to be so naive to think we know about all numbers and then go off on a jag about how there is this problem. I feel this video is very short sighted... assuming it's not to sow the seeds of deception.
    To be clear, the word "theory" in maths implies something which has been *proven*. Something which has neither been proven nor disproven is known as a "conjecture".

    As far as mathematics is concerned, infinity is very much a real thing. For example, the sequence 1/2 + 1/4 + 1/8 + 1/16 + ... converges to unity if and only if the process is allowed to go to infinity. Stopping at any point always leads to a result ever-so-somewhat-less than one. "In the limit" we do know that it absolutely does converge.

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    121
    Quote Originally Posted by Sir Galahad View Post
    For example, the sequence 1/2 + 1/4 + 1/8 + 1/16 + ... converges to unity if and only if the process is allowed to go to infinity. Stopping at any point always leads to a result ever-so-somewhat-less than one. "In the limit" we do know that it absolutely does converge.
    What you just said contains the problem. The sequence 1/2 + 1/4 + 1/8 + 1/16 + ... never equals one. No matter how far into "infinity" you go. That theory suggests that the moment you stop the sequence, the sum can not be equal to 1. But some how if you keep adding, what equates to "not enough to total 1", forever, it will reach 1. How can you have a mathematical equation that never ends?!? Especially when being presented as proof of something.

    I believe the term "infinity" is being abused or is not properly understood.

    And yes, I understand this is a programming forum.

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    121
    Real world example:

    I have two boxes. One box contains nothing, we'll say it's equal to 0. The other box contains and apple, we'll say it's equal to 1.

    Now if we do the 'infinite series' to the apple and move the additions to the first box, we see that "infinity" can never be reached, just the total apple (or 1). The main thing that is missing in the "infinite series" is the real world where everybody else exists. You cannot achieve "infinity" in my example because of the hard limit of 'atoms'. Once you get down to the limit, the decimal example of "infinity" can't be proven.

    I believe, because we don't know all "Maths", we can't see the hard limit that MAY exist in what people are referring to "infinity".

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Yonut View Post
    I believe, because we don't know all "Maths", we can't see the hard limit that MAY exist in what people are referring to "infinity".
    Infinity doesn't exist as some hard limit. Like many things in math, it's an abstract concept that doesn't (and can't) exist in the real world. You can count "things" in the real world using the natural numbers (1, 2, 3, etc.) but there's no real-world counterpart to infinity.

    Of course, like many other abstract math concepts, it's still very useful for a lot of different types of problems. Calculus, for one, would not exist in its current form without infinities.

    Arguing that infinity doesn't exist is like saying zero doesn't exist (perhaps you could say that if something exists then there's at least one of them, so zero can't exist, or some sort of logic like that). Math got along just fine for years before zero was discovered, but there were limits (no pun intended) to what could be done without zero. Likewise with infinity.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Jeez, get back on topic and stop discussing a random URL I posted as an example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem execl()
    By Ramsis94 in forum C Programming
    Replies: 2
    Last Post: 04-21-2020, 10:40 AM
  2. logical not !7 logical whaaaa?
    By Charbot in forum C Programming
    Replies: 2
    Last Post: 03-22-2011, 07:19 PM
  3. Using execl()
    By dudeomanodude in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2010, 02:13 AM
  4. C Execl Telnet
    By karcheee in forum C Programming
    Replies: 1
    Last Post: 04-26-2005, 02:31 PM
  5. execl
    By laasunde in forum C Programming
    Replies: 2
    Last Post: 11-19-2002, 05:07 PM

Tags for this Thread