|
Home > Archive > Unix Programming > August 2006 > I need help understanding the system() function.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
I need help understanding the system() function.
|
|
| K-mart Cashier 2006-08-30, 1:23 am |
| On one of the computers I use, there is a program called tel. It works
like this
% tel cdalten hi
on the other end, the person sees
Telegram to cdalten on ttypa...Telegram from cdalten on ttypa at 23:01
EDT ...
hi
SENT
EOF (cdalten)
The question revolves around using system() to send messages. Here is
the code in question
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <sys/param.h>
#define MAXLINE 200
#define LOGIN_NAME_MAX 32
void *xmalloc(size_t size) {
register void *value = malloc (size);
if (value == NULL)
fprintf(stderr, "out of memory");
return value;
}
void init(void) {
system("mesg -ye -r -xw");
}
char *xstring(char *ptr, size_t len) {
register char *value = (char *) xmalloc (len + 1);
memcpy (value, ptr, len);
value[len] = '\0';
return value;
}
int main(int argc, char **argv) {
char name[LOGIN_NAME_MAX];
char *output;
char *xname;
int i = 0;
int j;
size_t buffer;
size_t len;
char *msg_list[] = {
" test\ntest\n", "la\nla\n"
};
int count = (sizeof msg_list / sizeof msg_list[0]);
if(argc != 2) {
fprintf(stderr, "too few arguements \n");
exit(1);
}
len = strlen(argv[1]) + 1;
if(len > LOGIN_NAME_MAX) {
fprintf(stderr, "too long \n");
exit(1);
}
init();
for(i=0; i < count; ++i) {
/*tel + space (4) + message len + name len */
buffer = 4+strlen(msg_list[i])+1+ strlen(argv[1]) +1;
output = xmalloc(buffer);
if (snprintf(output, MAXLINE, "tel %s", argv[1]) < 0 ) {
fprintf(stderr, "string is too long \n");
exit(1);
}
strncat(output, msg_list[i] , strlen(msg_list[i]) + 1 );
system(output);
free(output);
sleep(5);
}
printf("finished \n");
return 0;
}
The output is:
Telegram to cdalten on ttypa...Telegram from cdalten on ttypa at 23:28
EDT ...
test
SENT
EOF (cdalten)
cdaltenla is not logged on
la: not found
I'm assuming the problem line is:
char *msg_list[] = {
" test\ntest\n", "la\nla\n"
};
I looked up the system() function in the book "Advanced programming in
the Unix Environment", second edition by Rago and Stevens. From what I
can tell, execl() in system() is used to invoke the shell. I guess I
really don't fully understand the error. Ie how the string gets parsed
by the shell. Can someone point me in the right direction?
Chad
| |
| Barry Margolin 2006-08-30, 1:23 am |
| In article <1156909707.956492.47980@74g2000cwt.googlegroups.com>,
"K-mart Cashier" <cdalten@gmail.com> wrote:
> On one of the computers I use, there is a program called tel. It works
> like this
>
> % tel cdalten hi
>
> on the other end, the person sees
> Telegram to cdalten on ttypa...Telegram from cdalten on ttypa at 23:01
> EDT ...
> hi
> SENT
> EOF (cdalten)
>
> The question revolves around using system() to send messages. Here is
> the code in question
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <unistd.h>
> #include <limits.h>
> #include <sys/param.h>
>
> #define MAXLINE 200
> #define LOGIN_NAME_MAX 32
>
> void *xmalloc(size_t size) {
> register void *value = malloc (size);
> if (value == NULL)
> fprintf(stderr, "out of memory");
> return value;
> }
>
> void init(void) {
> system("mesg -ye -r -xw");
> }
>
> char *xstring(char *ptr, size_t len) {
> register char *value = (char *) xmalloc (len + 1);
> memcpy (value, ptr, len);
> value[len] = '\0';
> return value;
> }
>
> int main(int argc, char **argv) {
>
> char name[LOGIN_NAME_MAX];
> char *output;
> char *xname;
> int i = 0;
> int j;
> size_t buffer;
> size_t len;
>
> char *msg_list[] = {
> " test\ntest\n", "la\nla\n"
> };
>
>
> int count = (sizeof msg_list / sizeof msg_list[0]);
>
> if(argc != 2) {
> fprintf(stderr, "too few arguements \n");
> exit(1);
> }
>
> len = strlen(argv[1]) + 1;
>
> if(len > LOGIN_NAME_MAX) {
> fprintf(stderr, "too long \n");
> exit(1);
> }
>
> init();
>
> for(i=0; i < count; ++i) {
> /*tel + space (4) + message len + name len */
> buffer = 4+strlen(msg_list[i])+1+ strlen(argv[1]) +1;
> output = xmalloc(buffer);
>
> if (snprintf(output, MAXLINE, "tel %s", argv[1]) < 0 ) {
> fprintf(stderr, "string is too long \n");
> exit(1);
> }
>
> strncat(output, msg_list[i] , strlen(msg_list[i]) + 1 );
>
> system(output);
> free(output);
>
> sleep(5);
>
> }
>
> printf("finished \n");
> return 0;
> }
>
> The output is:
>
> Telegram to cdalten on ttypa...Telegram from cdalten on ttypa at 23:28
> EDT ...
> test
> SENT
> EOF (cdalten)
> cdaltenla is not logged on
> la: not found
>
> I'm assuming the problem line is:
> char *msg_list[] = {
> " test\ntest\n", "la\nla\n"
> };
>
> I looked up the system() function in the book "Advanced programming in
> the Unix Environment", second edition by Rago and Stevens. From what I
> can tell, execl() in system() is used to invoke the shell. I guess I
> really don't fully understand the error. Ie how the string gets parsed
> by the shell. Can someone point me in the right direction?
The first time through your loop, it executes the commands:
tel cdalten test
test
It executes two commands because \n is a command delimiter in the shell.
This sends the message "test" to cdalten, and then executes the command
named "test", which doesn't have any visible effects (it's mostly useful
in conjunction with "if").
The second time through your loop it executes the commands:
tel cdaltenla
la
The first command has "la" as part of the name of the user to send a
message to because you left out the space at the beginning of
msg_list[1]. So it tries to send an empty message to the nonexistent
user named cdaltenla. Then, because of the newline in the command
string, it tries to execute the command "la", which doesn't exist, so
you get an error.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Hubble 2006-08-30, 1:23 am |
| K-mart Cashier schrieb:
> On one of the computers I use, there is a program called tel. It works
> like this
>
> % tel cdalten hi
>
> on the other end, the person sees
> Telegram to cdalten on ttypa...Telegram from cdalten on ttypa at 23:01
> EDT ...
> hi
> SENT
> EOF (cdalten)
>
> void init(void) {
> system("mesg -ye -r -xw");
> }
> I looked up the system() function in the book "Advanced programming in
> the Unix Environment", second edition by Rago and Stevens. From what I
> can tell, execl() in system() is used to invoke the shell. I guess I
> really don't fully understand the error. Ie how the string gets parsed
> by the shell. Can someone point me in the right direction?
What you need, is popen(3) instead of system. Also, on be linux your
mesg command seems to write(1). The following does what you seem to
discribe on linux:
FILE * fp;
if (fp=popen("write user","w")) {
fprintf(fp,"hello, there\nHow do you do?\n");
if (fclose(fp)<0)
perror("write");
} else {
perror("popen");
}
system(3) does not allow to feed inout unless you write to file and use
<file in the command. popen(3) is like systen(3) but allows to deliver
stdin (or receive stdout) through a file pointer.
Reiner Huober
|
|
|
|
|