d = 'date'
char *shell_execute(char *command) {
static char buf[512]; // static so we can return it to caller
FILE *fp;
fp = popen(command, "r"); // open a pipe with command.
// read output into buf
fgetf( buf, 512, fp );
// close opened pipe
pclose(fp);
// return output to caller.
return buf;
}
// Reads output into str
void fgets( char *str, int length, FILE *fp ) {
char c = '\0';
// while we're not out of length
while(–length) {
// grab a character from the pipe and break if it is the EOF marker
if ((c = fgetc(fp)) == EOF)
break;
// copy this character into our passed buffer and break if it is a NULL delimiter.
if ((*str++ = c) == '\0')
break;
}
// Regardless, delimit the string will NULL to be sure.
*str = '\0';
}
send_to_char(ch, shell_execute("ps xu"))
char buf[1];
fgets(buf, 1, bla);
–>
while(–length) {
… bla …
… str++ …
}
// str now points past the end of buf!
*str = '\0'; // oops
char buf[1];
fgets(buf, 1, bla);
–>
while(–length) {
… bla …
… str++ …
}
// str now points past the end of buf!
*str = '\0'; // oops
#include <iostream>
using namespace std;
int main () {
int l = 1;
while (–l)
cout << "Any passes!" << endl;
cout << l << endl;
return 0;
}
#include <iostream>
using namespace std;
int main () {
int l = 1;
while (l–)
cout << "Any passes!" << endl;
cout << l << endl;
return 0;
}
pseudo code: