hi all,
I am wirting a program which will sent a certain recipent a email ( the email address is taken from the command line). The email contains the current processes that are running in the machine. The subject line will have something like "there are X no of users running X number of processes". It should do this with pipes. (popen()). The program is working. But I am having trouble with the SUBJECT line. The reciving mail won't have a subject. ( the subject will be NONE.) The source code is as follows. The section where the mail is sent is in red.
Code:
#include<stdio.h>
#include<stdlib.h>
#define BUFFLEN 40 /* length of the array that will hold all the processes */
#define SIZE 100 /* the initial size of the array to hold all the processes */
#define COMMAND "ps -e -o uid -o pid -o fname" /* get all the process names */
char** list; /*the column vector of the 2D array to hold filenames */
void die(char*); /* die with msg */
int main()
{
FILE* pipe_id;
int i=0,j; /* conter variables */
char buff[BUFFLEN];
int user_count;
int proc_count;
char subject[80]; /* the subject line */
char recpt[50]; /* the recipient of the email*/
char mail_command[80];
printf("The mail recipient : ");
fscanf(stdin,"%s",recpt);
/* allocate the column vector of the 2D character array */
if((list=(char**)malloc(sizeof(char*)*SIZE))==NULL)
die("Memory allocation error.\n");
/* open pipe connection */
if ((pipe_id=popen(COMMAND,"r"))==NULL)
die("Pipe open failiure. Abort");
/*read each line from the pipe and write it to the array */
while(fgets(buff, BUFFLEN, pipe_id)!= NULL)
{
if ((i+1)>SIZE) /* reallocation required */
list=(char**)realloc((char**)list,sizeof(char*)*(i+100)); /* reallocate the column vector with 100 more elements */
if ((list[i]=(char*)malloc(sizeof(char)*BUFFLEN))==NULL) /* allocate rows of the 2D char array */
die("Memory allocation erro. \n");
strcpy(list[i],buff); /* copy the line read from the pipe to the array element */
i++;
}
// get_counts(list,&user_count,&proc_count);
pclose(pipe_id);
sprintf(subject,"\"There are %d no of users using %d processers\"",10,100);
sprintf(mail_command,"mail -s %s %s ",subject,recpt);
printf("%s\n",mail_command);
pipe_id=popen(mail_command,"w");
for(j=0;j<i;j++)
{
fprintf(pipe_id,"%s",list[j]);
}
fprintf(pipe_id,"%c",EOF);
pclose(pipe_id);
}
how to solve this ?