C
Clunixchit
I would appreciate if you can criticise my code as well as if possible
give advice on how to improve it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#define LINE_LENGTH 20
#define nLINES 10
#define MYAIRC "myAIc"
typedef struct Loading {
char *name; // Name of the program
int pid; // current pid of the program
struct Loading *prec;
} Loading ;
/* ----array_to_structure
* n = number of lines in the file
*/
void array_to_structure ( Loading **myLoad , char
tmp[nLINES][LINE_LENGTH] , int n ) {
int i=0;
Loading *program;
while ( i < n )
{ //printf("1.%s\n",tmp);
Loading *program = malloc ( sizeof(Loading) );
if (!program){
fprintf(stdout,"program %s
failed",tmp);
return;
}
(*program).name = malloc (
strlen(tmp) + 1 );
strcpy( (*program).name,tmp );
(*program).pid = -1;
(*program).prec = *myLoad;
*myLoad=program;
i++;
}
}
void find_comment(char *tline) {
char *comment;
comment = strchr(tline, '\n');/* Replace \n with \0 for
consistency. */
if (comment != NULL) {
*comment = '\0';
}
}
void config_file ( Loading **myLoad , char *config_file
){
int i=0,j=0;
char tmp[nLINES][LINE_LENGTH];
FILE *stream = fopen( config_file , "r" );
if ( stream == NULL ) {
fprintf (stdout , "fopen %s failed" , *config_file
);
return;
}
while( fgets( tmp , LINE_LENGTH-1 , stream )
!= NULL ){
size_t l = strlen(tmp);
if (l > 0 && tmp[l-1] ==
'\n')
tmp[l-1] = '\0';
//printf("%zu
%s\n",strlen(tmp),tmp);
i++;
}
fclose (stream);
j=i;
while(j>=0) {
find_comment(tmp[j]);
j--;
}
array_to_structure(myLoad,tmp,i);
}
void view( Loading *p ) {
char *arg_list[]={(*p).name,NULL};
if ( fork() == 0 ) {
(*p).pid = getpid();
printf("Loading: %s
(%d)\n",(*p).name,(*p).pid);
if (execvp((*p).name,arg_list)) {
perror("execvp");
return;
}
}
}
void view_structure( Loading *T ) {
while ( T ){
view(T);
T=T->prec;
}
}
void clear ( Loading **T) {
Loading *tmp;
while(*T){
tmp=(**T).prec;
free(*T);
*T=tmp;
}
}
int main () {
Loading *myLoad = NULL;
printf("\n---- myAI mainboard:
%d\n",getpid());
config_file(&myLoad,MYAIRC);
view_structure(myLoad);
wait(NULL);
clear(&myLoad);
printf("\n");
return 0;
}
give advice on how to improve it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#define LINE_LENGTH 20
#define nLINES 10
#define MYAIRC "myAIc"
typedef struct Loading {
char *name; // Name of the program
int pid; // current pid of the program
struct Loading *prec;
} Loading ;
/* ----array_to_structure
* n = number of lines in the file
*/
void array_to_structure ( Loading **myLoad , char
tmp[nLINES][LINE_LENGTH] , int n ) {
int i=0;
Loading *program;
while ( i < n )
{ //printf("1.%s\n",tmp);
Loading *program = malloc ( sizeof(Loading) );
if (!program){
fprintf(stdout,"program %s
failed",tmp);
return;
}
(*program).name = malloc (
strlen(tmp) + 1 );
strcpy( (*program).name,tmp );
(*program).pid = -1;
(*program).prec = *myLoad;
*myLoad=program;
i++;
}
}
void find_comment(char *tline) {
char *comment;
comment = strchr(tline, '\n');/* Replace \n with \0 for
consistency. */
if (comment != NULL) {
*comment = '\0';
}
}
void config_file ( Loading **myLoad , char *config_file
){
int i=0,j=0;
char tmp[nLINES][LINE_LENGTH];
FILE *stream = fopen( config_file , "r" );
if ( stream == NULL ) {
fprintf (stdout , "fopen %s failed" , *config_file
);
return;
}
while( fgets( tmp , LINE_LENGTH-1 , stream )
!= NULL ){
size_t l = strlen(tmp);
if (l > 0 && tmp[l-1] ==
'\n')
tmp[l-1] = '\0';
//printf("%zu
%s\n",strlen(tmp),tmp);
i++;
}
fclose (stream);
j=i;
while(j>=0) {
find_comment(tmp[j]);
j--;
}
array_to_structure(myLoad,tmp,i);
}
void view( Loading *p ) {
char *arg_list[]={(*p).name,NULL};
if ( fork() == 0 ) {
(*p).pid = getpid();
printf("Loading: %s
(%d)\n",(*p).name,(*p).pid);
if (execvp((*p).name,arg_list)) {
perror("execvp");
return;
}
}
}
void view_structure( Loading *T ) {
while ( T ){
view(T);
T=T->prec;
}
}
void clear ( Loading **T) {
Loading *tmp;
while(*T){
tmp=(**T).prec;
free(*T);
*T=tmp;
}
}
int main () {
Loading *myLoad = NULL;
printf("\n---- myAI mainboard:
%d\n",getpid());
config_file(&myLoad,MYAIRC);
view_structure(myLoad);
wait(NULL);
clear(&myLoad);
printf("\n");
return 0;
}