Cannot use system("cls"); in Xcode in mac - xcode

I tried to execute
system (cls):
in Xcode in mac, but it doesn't work.

Two problems.
1 - You didn't use quotes.
2 - The command on OS X is clear, not cls.
system("clear");
Instead of doing that, a better way is to add these #includes and also this ClearScreen function which sends the terminal a clear command directly, instead of starting a separate process. Cribbed from http://www.cplusplus.com/articles/4z18T05o/#POSIX
#include <unistd.h>
#include <term.h>
void ClearScreen()
{
if (!cur_term)
{
int result;
setupterm( NULL, STDOUT_FILENO, &result );
if (result <= 0) return;
}
putp( tigetstr( "clear" ) );
}

Related

Calling pthread_cond_destroy results in "Function not implemented" ENOSYS on macOS

I am trying to make some Linux-based code run on macOS. It is the POSIX OSAL layer for NASA Core Flight System as found here: https://github.com/nasa/osal.
I am observing that the code uses POSIX conditions and in particular, there is a call like the following:
if (pthread_cond_destroy(&(sem->cv)) != 0) {
printf("pthread_cond_destroy %d %s\n", errno, strerror(errno)); // my addition
...
}
On macOS, the tests related to this code provided in the OSAL repository always fail because the call to pthread_cond_destroy always results in:
pthread_cond_destroy 78 Function not implemented
I have found an example in the Apple documentation which shows an example of Using Conditions (Threading Programming Guide / Synchronization / Using Conditions) and in that example there is no call to pthread_cond_destroy but I cannot make any conclusions on whether that call should be there or not because the example is simplified.
This is how the header looks like on my machine:
__API_AVAILABLE(macos(10.4), ios(2.0))
int pthread_cond_destroy(pthread_cond_t *);
I am wondering if pthread_cond_* functionality is simply missing on macOS and I have to implement a replacement for it or there is some way to make it work.
EDIT: The minimal example is working fine for me. The problem should be somewhere around the problematic code. What I still don't understand is why I am getting ENOSYS/78 error code, for one thing it is not mentioned on the man page man/3/pthread_cond_destroy:
#include <iostream>
#include <pthread.h>
int main() {
pthread_cond_t condition;
pthread_cond_init(&condition, NULL);
int result = pthread_cond_destroy(&condition);
assert(result == 0);
assert(errno == 0);
std::cout << "Hello, World!" << std::endl;
return 0;
}

I am trying to write a shell program to execute more than one command at a time

My Code
#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
char * arg_list[3];
arg_list[0] = "ls";
arg_list[1] = "-l";
arg_list[2] = 0;
char *arg_list2[3];
arg_list2[0] = " ps";
arg_list2[1] = "-ef";
arg_list2[2] = 0;
for(int i=0;i<5;i++){ // loop will run n times (n=5)
if(fork() == 0) {
if (i == 0){
execvp("ls", arg_list);
}else if(i==1){
execvp("ps" , arg_list2);
}else if(i>1){
printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
exit(0);
}
}
}
for(int i=0;i<5;i++) // loop will run n times (n=5)
wait(NULL);
}
ME trying to modify it
#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
for(int i=0;i<5;i++){ // loop will run n times (n=5)
if(fork() == 0) {
printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
execlp(argv[i],argv[i],argv[i+1],(char*)NULL);
exit(0);
}
}
for(int i=0;i<5;i++) // loop will run n times (n=5)
wait(NULL);
}
-- NEED GUIDANCE AND UNDERSTANDING
I am trying to make my own tiny little shell program. When I run my first code works fine, runs all commands on the command line. But I cannot know and define all commands the user might enter. So i am trying to get a base code which could run any commands single or multiple entered by user. I tried using execlp where it does not compile saying argv is not defined which is true as i don't want to specifically define it.
I am trying to make my own tiny little shell program. When I run my first code works fine, runs all commands on the command line. But I cannot know and define all commands the user might enter.
For sure.... A shell program purpose is basically:
Read user input
Execute user input
Return result of execution.
There's nothing in your code that read user input....
So i am trying to get a base code which could run any commands single or multiple entered by user.
So read user input ;-)
I tried using execlp where it does not compile saying argv is not defined which is true as i don't want to specifically define it.
For sure ... but how would GCC guessed that `argv[]̀ must be automaticallty filled with user input ?
There's nothing automatic when coding in C language. You have to manage this manually.
Also, note that argc, argv et envp are usually reserved for main() function:
main(int argc, char **argv, char **envp)
So you may use something else to build your command array.
In pseudo code, what you must implement is:
quit=0
while (quit = 0) {
command_to_run = read_user_input();
if (command_to_run == "exit") {
quit = 1;
} else {
execute(command_to_run);
}
}
Some advices:
Try to use more functions. For example, implement a fork_and_run(char **cmd) function to fork and then execute command provided by the user. Il will make your code more readable and easy to maintain.
Read carefully manpages: everything you should know (like, for example, the fact that array provided to execvp() must be NULL-terminated) is written in it.
Your debugging messages should be printed to stderr. The result of the command run must be printed to stdin, so use fprintf() instead of printf() to write to the correct stream.
I would use a #define debug(x) fprintf(stderr, x) or something similar for debugging output so that you can easily disable later ;-)

getch() returns \377! instead of reading character

I am using Xcode and using the C language trying to understand how to use the Getch function to start a project. I copied some code someone had written as an example that should work, however instead of the code waiting for me to enter a character, it returns one for me:
#include <midi_lib.h> <---------- this needs to be here for later
#include <curses.h>
int main() {
keypad(stdscr, TRUE); /* I dont know if this is needed*/
char c;
puts("Enter a character\n");
c = getch();
printf("You just typed a %c! \n", c);
getch();
}
This is the output:
Enter a character
You just typed a \377!
It works if i use Getchar(), but i dont want to have to press enter.
I'm really new to coding so it maybe something simple, but i have no idea....
LT
Well, you didn't even initialize the Ncurses library with initscr().
There really are a lot of things you are missing, so I recommend you to read the Ncurses howto that explains you everything from initializing (e.g. what that keypad(stdscr, true) is) to input and a lot more.
Here's an example that works:
#include <ncurses.h>
int main()
{
int ch;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
ch = getch();
endwin();
printf("The key pressed is %d\n", ch);
}
Try this to get you started
#include <midi_lib.h> <---------- this needs to be here for later
#include <curses.h>
int main()
{
char c;
keypad(stdscr, TRUE); /* I dont know if this is needed*/
puts("Enter a character\n");
do {
c = getch();
} while (c == (char)-1);
printf("You just typed a %c! \n",c);
}
Note that getch() is constantly polling the keyboard for input, when there is
none, it will get -1 and retry until there is some in which case it will fall out of the while loop and continue.
This probably isn't how you should be coding this, but it does answer your question. The computer will act busy while it is constantly polling the keyboard.

Parent process not ending when using fork() and execl(...)

I'm new to programming with linux and I was trying to understand how fork() and exec functions work. To make it easier for myself, I created a simply Dummy executable (with gcc -o Dummy.exe ...) and tried to call fork function so I can replace the child with the Dummy.exe executable I have created.
The problem I'm coming accross is that when I run the code, it shows me the contents of the Dummy.exe, however, I don't see anything past that - meaning, I don't see the parent process ending.
When I run my code Ex1.cpp, I get the output:
Program is Running
--- ****** ---
Ended
me#mdev>
The only way I can get the program to end is by pressing return key - you will see a blank line after word Ended.
Here is the code in my Ex1.cpp
#include <iostream>
using namespace std;
int main()
{
pid_t retVal;
int newStatus;
switch(retVal = fork())
{
case -1:
cout<<"Error occured with fork"<<endl;
break;
case 0:
cout<<"Child forked"<<endl;
newStatus = execl("Dummy.exe","Dummy.exe", NULL);
break;
default:
cout<<"Parent has a new child: "<<retVal<<endl;
}
cout<<"Ended ..."<<endl;
return 0;
}
My Dummy.cpp code is below:
#include <iostream>
using namespace std;
int main()
{
cout<<"Program is Running"<<endl;
cout<<"--- ****** ---"<<endl;
cout<<"Ended"<<endl;
return 0;
}
My background is Windows development and all this is new for me - I appreciate your help.
#include <iostream>
using namespace std;
int main ()
{
pid_t retVal;
int newStatus;
int returnStatus;
if (retVal = fork() < 0)
{
cout << "Error on fork" << endl;
exit (1);
}
// Child process
else if (retVal == 0)
{
// No code is executed after an exec statement
newStatus = execl("./Dummy", "Dummy", NULL);
}
else
{
// This waits for the child to finish and clean up
waitpid(retVal, &returnStatus, NULL);
}
return returnStatus;
}
It is not a good idea to use a switch statement in your case since no code is executed after an exec statement. You can read up on that further by reading the man page at the command prompt by typing man 2 exec. Also it is always a good idea to wait for the child to exit successfully which is what the waitpid function does. Try this and let me know if it works and if you have any questions as well.

Command prompt in debug mode for Eclipse? (OpenCV + Eclipse + Win7)

I am a beginner for Eclipse. I now have Eclipse C/C++ IDE with OpenCV library running on Windows 7. So far it works after spending hours trying to get it running. But then I realize that Eclipse does not pop up a command prompt as VS2010 does while debugging. And moreover Eclipse's debug mode is just stuck in there and refuse to output anything. But if the code doesn't involve the OpenCV things it works again.
Below is the code I use for testing. It captures images from webcam and output it to the screen. The infinite loop (until you press 'q') makes sure it constantly grabs new inputs from the camera.
I browsed through the workspace and run the exe just compiled and it worked flawlessly. So I don't think there's anything wrong in the code (it's an example code anyway
In brief, can I just pop up a command prompt window in debug mode? And why is Eclipse console stuck when the code involves some OpenCV functions?
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
CvCapture *capture = 0;
IplImage *frame = 0;
int key = 0;
/* initialize camera */
capture = cvCaptureFromCAM( 0 );
/* always check */
if ( !capture ) {
printf("Cannot open initialize webcam!\n");
return 1;
}
/* create a window for the video */
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
while( key != 'q' ) {
/* get a frame */
frame = cvQueryFrame( capture );
/* always check */
if( !frame ) break;
/* display current frame */
cvShowImage( "result", frame );
/* exit if user press 'q' */
key = cvWaitKey( 1 );
}
/* free memory */
cvDestroyWindow( "result" );
cvReleaseCapture( &capture );
return 0;
}
This is because you have already set the windows 7 system variable PATH to your MinGw/bin and compiled opencv bin directories. So when you run the program from your folder your system automatically take the required binaries from its PATH and the program runs correctly.
I don't know why but Eclipse does not take it directly from the system environment PATH variable. So we have to set it ourself.
go to Preferences > C/C++ (Expand it) > Environment > Add:
"Name:PATH"
"Value:C:\MinGW\bin;C:\opencv_MinGW\bin"
where opencv_MinGW is the folder where I compiled my opencv

Resources