-1

Can someone please help me figure out why I'm not getting the results im supposed to? from the textbook

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

int main(int argc, char *argv[])
{
    char *my_env[] = {"JUICE = peaches and apples", NULL};
    execle("diner info", "diner info", "4", NULL, my_env);

    printf("Diners: %s\n", argv[1]);
    printf("Juice env: %s\n", getenv("JUICE"));
    return 0;

}

The program prints

Diners: (null)
Juice env: (null)`
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 1
    Note that, if successful, `execle` does *not* return. So if you're seeing output from the subsequent `printf` statements it means the call to [`execle` failed](https://man7.org/linux/man-pages/man3/exec.3.html#RETURN_VALUE). – G.M. Aug 01 '22 at 08:32
  • 1
    The first argument to `execle` needs to be path of the program you wish to run. So unless you have an executable called `diner info`, it will fail. To do the test you want, you should have the program you exec print the environment variables. That way you'll know for sure if it had the desired effect. – Tom Karzes Aug 01 '22 at 08:41
  • any idea how i can create the executable? i'm still learning – pythonnewbie Aug 01 '22 at 08:46
  • Also, in that text book the you have 2 programs: The first one (`my_exec_program`) calls the second one (`dinner_info`) and provides the environmant to it. The second queries that envirobment. Your code is doing something completely different. – Gerhardh Aug 01 '22 at 08:54
  • the dinner info typo isnt the issue, but ive fixed it. i want to know how to write the code in a way that prints what its supposed to. – pythonnewbie Aug 01 '22 at 08:56
  • Note that there are no spaces around the = when setting and environment variable. – Jonathan Leffler Aug 01 '22 at 09:11
  • 1
    The example does what is documented, **if you repeat it exactly** as documented. You will not get the same result, if you change it. – the busybee Aug 01 '22 at 09:25
  • thats not the problem either @JonathanLeffler – pythonnewbie Aug 01 '22 at 09:25
  • oh wow, so how do i get the results like they did in the textbook then? any idea? @G.M. – pythonnewbie Aug 01 '22 at 09:27
  • 1
    You try to change your own environment. The textbook provides an environment for a different program. Use 2 programs as in the textbook. – Gerhardh Aug 01 '22 at 09:28
  • The spaces around the equals sign are not the whole problem, no. However, it is one of the problems. That's why it's a comment not an answer. – Jonathan Leffler Aug 01 '22 at 12:33

1 Answers1

1

Try it as it is shown in the text book:

They are 2 programs:

The first program prepares an environment and passes it to a second program that it also executes.

// my_exec_program.c
#include <unistd.h>

int main(void)
{
    char *my_env[] = {"JUICE=peaches and apples", NULL};
    execle("diner_info", "diner_info", "4", NULL, my_env);
}

The second program reads its environment and prints it.

// diner_info.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  if (argc > 1)
  {
    printf("Diners: %s\n", argv[1]);
  }
  printf("Juice env: %s\n", getenv("JUICE"));
  return 0;
}

Terminal:

gerhardh@xy:~/tests$ gcc -Wall -Wextra -pedantic my_exec_program.c -o my_exec_program
gerhardh@xy:~/tests$ gcc -Wall -Wextra -pedantic diner_info.c  -o diner_info
gerhardh@xy:~/tests$ ./diner_info 
Juice env: (null)
gerhardh@xy:~/tests$ ./my_exec_program 
Diners: 4
Juice env: peaches and apples
gerhardh@xy:~/tests$ 

Mixing the code and squeezing everything into one file will not work.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • it still doesnt work. on my terminal, when i execute diner_info, it prints Juice env: (null) like it did in your terminal, but when i execute exec program, it just returns a new line. i use a windows though, could that be a reason why it doesnt work? – pythonnewbie Aug 01 '22 at 12:18
  • apparently, the name in the argument is supposed to be the same as the name of the exe. i just figured that out smh. THANK YOU! – pythonnewbie Aug 01 '22 at 12:41
  • Well, yes. the first argument of `execle` is the name of the program to start. It must match the name of the exectutable you compiled separately – Gerhardh Aug 01 '22 at 12:46