Thread: Putting variables into spawnl

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    4

    Putting variables into spawnl

    Code:
    #include <stdio.h>
    #include <process.h>
    
    int main()
    {    
    	char p[5] = "less";	/* pager */
    	char buff[30];
    
    	/* this shows test.txt in less */
    	sprintf(buff, "\"%s\" \"test.txt\"", p);
    	spawnlp(P_WAIT, "less", buff, NULL);
    	
    	/* this does NOT show test.txt in less */
    	sprintf(buff, "\"%s\", \"%s\" \"test.txt\"", p, p);
    	spawnlp(P_WAIT, buff, NULL);
    	
    	return 0;
    }
    I'm sure I must be missing something obvious, but how can I make the second example work properly?

    Thanks in advance.

  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
    I wonder if your first one also shows "file not found" before going on to page your test.txt.
    It seems broken, only less so than your 2nd attempt.

    > but how can I make the second example work properly?
    By realising that it's completely broken by design and just go with the first one.

    This is it's prototype.
    Code:
    intptr_t _spawnlp(  
       int mode,  
       const char *cmdname,  
       const char *arg0,  
       const char *arg1,  
       ... const char *argn,  
       NULL   
    );
    spawn is NOT a command line parser that will break up space-separated commands.
    That's YOUR task, so you can pass separate strings for each argn parameter.


    You're literally trying to execute a program with the rather long name of
    "less", "less" "test.txt"

    I suppose you could pass the whole mess to cmd.exe, like
    spawnlp(P_WAIT, "cmd.exe", buff, NULL);
    and perhaps it'll eventually get around to doing what you want.
    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
    Feb 2018
    Posts
    4
    Quote Originally Posted by Salem View Post
    I wonder if your first one also shows "file not found" before going on to page your test.txt.
    If it happens, it's gone too quickly to be noticed.

    This is it's prototype.
    Code:
    intptr_t _spawnlp(  
       int mode,  
       const char *cmdname,  
       const char *arg0,  
       const char *arg1,  
       ... const char *argn,  
       NULL   
    );
    And, overlooked colon notwithstanding, that is what I thought I was doing; it's still broken.

    Any suggestions?

    Edit: to be clear, the value for "p" needs to be a variable (hence the title). Can spawnl not take variables, or am I just hamhanding it?
    Last edited by Satori; 02-18-2018 at 03:27 PM. Reason: more information

  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
    So long as your variables are char pointer or char array, spawn couldn't care less.
    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
    Feb 2018
    Posts
    4
    It works if I use a separate array for cmdname. I really can't imagine why I would need 2 sprintf's for one spawn though.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Satori View Post
    It works if I use a separate array for cmdname. I really can't imagine why I would need 2 sprintf's for one spawn though.
    Why isn't it as simple as:

    Code:
    #include <stdio.h>
    #include <process.h>
    
    int main()
    {
        const char *cmd = "less";
    
        spawnlp(P_WAIT, cmd, cmd, "test.txt", NULL);
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2018
    Posts
    4
    Because I have this tendency to make things more complicated than they need to be.

    Thank you, that example helped a lot!

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Satori View Post
    Because I have this tendency to make things more complicated than they need to be.

    Thank you, that example helped a lot!
    No problem.

    You understand why 'cmd' is given to spawnlp twice, right? Just in case you don't it's because by convention argv[0] is the name of the process/program. You could make the third argument to spawnlp something different but that wouldn't be how it's done by convention.


    Code:
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
        printf("My process/program name is %s\n", argv[0]);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting variables together in system
    By bluehead in forum C++ Programming
    Replies: 16
    Last Post: 08-18-2003, 05:09 PM
  2. Putting variables in SYSTEM
    By mycro in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2003, 07:59 PM
  3. spawnl !!!
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 06:00 PM
  4. about spawnl
    By xacoolboy in forum C Programming
    Replies: 1
    Last Post: 05-13-2002, 09:01 AM
  5. putting variables into array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2002, 12:56 PM

Tags for this Thread