How does execlp in this program? - process

What will be the following output if the execlp("james","james",10,NULL);
The system call execlp executes the code of the james program, which is defined as:
int main(int argc,char*argv[])
{
printf("i=%s",argv[1]);
}
What will be the execution of this code

Related

C++ Assigning a value to char *argv[] from inside a program

The customary way of passing values through argv in this declaration
int main(int argc, char* argv[]) {
is to pass it from your console through argv array into a program. I came across a situation where I wanted to achieve something different as follows. The program has the usual
int main(int argc, char* argv[]) {
declaration and inside my program I have a
function void foo(argv[1], 0).
Here is the layout and flow of my program's logic:
program ggg.cpp
:
void foo(char* x, int y) {
:
}//end foo
:
int main(int argc, char* argv[]) {
:
foo(argv[1], 0);
:
}//end main
(1) My normal method. Normally, when I compile the ggg.cpp program and my console input is:
ggg.exe . //the second input is a dot "."
the program runs fine (because argv[1] = '.' inside the program)
(2) However, I wanted to get innovative and execute the program as:
ggg.exe //here, the dot is not included
so that inside the program, I can assign the dot to argv[1] but I kept getting errors - no matter how I try other variations of the assignment statement.
By the way, I tried the assignment statement
argv[1]='.';
inside the main program (in method (2)) but it didn't work. I got an error message.
Background Information. I used to be very good in programming in my hay days but now I have forgotten a lot of things because I left coding for a very long time to do other things.

Creating multiple windows in gtkmm

I started to learn gtkmm library and probably don't understand the way it works. Here's the problem: I've copied simple example from gtkmm tutorial, and want to modify it to create as many windows as I want by clicking the button.
Why can't I just write code like in function on_button_clicked() below?
class Hello : public Gtk::Window {
public:
Hello() :m_button("create copy") {
set_border_width(20);
m_button.signal_clicked().connect(sigc::mem_fun(*this, &Hello::on_button_clicked));
add(m_button);
show_all_children();
}
protected:
void on_button_clicked();
Gtk::Button m_button;
};
void Hello::on_button_clicked() {
Hello new_window;
new_window.show();
}
int main (int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
Hello hw;
return app->run(hw);
}
The reason a new window is not displayed is how C++ has been used in the method Hello::on_button_clicked().
The line :
Hello new_window;
creates a new window with local scope.
new_window.show();
This marks the window to be shown when GTK+ is back in control.
The line
}
exits the method and all local variables are destroyed. Which means that new_window is deleted before it can be seen.
To keep the window and have it shown the object must be stored so that it is not automatically destroyed. This could be allocated on the heap and a pointer kept to it in another class for easy access to the window.

How to make a loadable dll to use tcl code functionality by any program

I have created a GUI using tcl. I want to make some of the core functionalities of the tcl code available to be used by any program which supports dll. For that i have taken a very simple tcl code example, which adds two integer numbers and i have written a c wrapper function to use this functionality. This is working for me. Now how can i create a dll for these two c and tcl files, so that any program can use this addition functionality by simply loading the dll.
Here is my simple tcl code :
/* Filename : simple_addition.tcl */
#!/usr/bin/env tclsh8.5
proc add_two_nos { } {
set a 10
set b 20
set c [expr { $a + $b } ]
puts " c is $c ......."
}
And here is my c wrapper function which uses the above tcl addition functionality :
#include <tcl.h>
#include <tclDecls.h>
#include <tclPlatDecls.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char **argv) {
Tcl_Interp *interp;
int code;
char *result;
printf("inside main function \n");
Tcl_FindExecutable(argv[0]);
interp = Tcl_CreateInterp();
code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");
/* Retrieve the result... */
result = Tcl_GetString(Tcl_GetObjResult(interp));
/* Check for error! If an error, message is result. */
if (code == TCL_ERROR) {
fprintf(stderr, "ERROR in script: %s\n", result);
exit(1);
}
/* Print (normal) result if non-empty; we'll skip handling encodings for now */
if (strlen(result)) {
printf("%s\n", result);
}
/* Clean up */
Tcl_DeleteInterp(interp);
exit(0);
}
This c wrapper is working fine for me and gives correct results.
Now I want to create a dll file, so that if i include that dll to any program that supports dll, it should be able to use this addition functionality of the above tcl code. Can anybody please tell me the way i can do it. Please help me. I am new to this dll concept.
In order to create the .dll you'll have to use something like Visual Studio and C or C++ to create the .dll (there are lots of other tools out there that can create .dll files but VS is easy to get hold of and to use.) So in VS create a new project, this needs to be a C++ WIN32 project. Select the DLL application type and the Export Symbols additional option.
VS will create a basic .dll that you can then amend to do what you want. I short I'd look at putting the creating/destruction of the intrepter into the dllmain:
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
Tcl_FindExecutable(NULL);
interp = Tcl_CreateInterp();
}
case DLL_THREAD_ATTACH:
break ;
case DLL_THREAD_DETACH:
break ;
case DLL_PROCESS_DETACH:
{
Tcl_DeleteInterp(interp);
break;
}
}
return TRUE;
}
and then create functions exported by the .dll that make use of the interpreter. If you aren't familiar with the concept of shared libaries then I'd suggest spending a little time reading up on them, try here and here for some background reading.

how to write a program for addition of two numbers in google test?

I am working with visual studio 2005...
Can anybody tell me how to write most basic program for addition of two numbers in google test??
I have gone through almost all the references given...but somehow didnt understand how to get started?
The following would be about as basic as it can get:
#include "gtest/gtest.h"
int GetSum(int a, int b) { return a + b; }
TEST(MathsFunctions, GetSum) {
ASSERT_EQ(3, GetSum(1, 2));
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
You need to provide the path to gtest's include directory and link to gtest.lib

How is the CodeRunner Program Input field used?

How is the Custom Run sheet Program Input field used in CodeRunner say for a C or Objective-C program?
The text entered in the Program Input text box will be sent to your program/script through the standard input. You can access the input in the same way that you would access the standard input using your language of choice.
In C, you'd use the standard fread, fgets, fgetc etc functions. The following example will echo the supplied text in the Program Input to the console:
#include <stdio.h>
int main(int argc, char *argv[]) {
char str[80];
while(fgets(str, sizeof(str), stdin)) {
printf("%s", str);
}
}