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_struct ure
* n = number of lines in the file
*/
void array_to_struct ure ( Loading **myLoad , char
tmp[nLINES][LINE_LENGTH] , int n ) {
int i=0;
Loading *program;
while ( i < n )
{ //printf("1.%s\n" ,tmp[i]);
Loading *program = malloc ( sizeof(Loading) );
if (!program){
fprintf(stdout, "program %s
failed",tmp[i]);
return;
}
(*program).name = malloc (
strlen(tmp[i]) + 1 );
strcpy( (*program).name ,tmp[i] );
(*program).pid = -1;
(*program).prec = *myLoad;
*myLoad=program ;
i++;
}
}
void find_comment(ch ar *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[i] , LINE_LENGTH-1 , stream )
!= NULL ){
size_t l = strlen(tmp[i]);
if (l > 0 && tmp[i][l-1] ==
'\n')
tmp[i][l-1] = '\0';
//printf("%zu
%s\n",strlen(tm p[i]),tmp[i]);
i++;
}
fclose (stream);
j=i;
while(j>=0) {
find_comment(tm p[j]);
j--;
}
array_to_struct ure(myLoad,tmp, i);
}
void view( Loading *p ) {
char *arg_list[]={(*p).name,NUL L};
if ( fork() == 0 ) {
(*p).pid = getpid();
printf("Loading : %s
(%d)\n",(*p).na me,(*p).pid);
if (execvp((*p).na me,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(&my Load,MYAIRC);
view_structure( myLoad);
wait(NULL);
clear(&myLoad);
printf("\n");
return 0;
} 6 1715
Clunixchit wrote: 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>
Not a Standard C header
#include <sys/wait.h>
Not a Standard C header
#include <unistd.h>
Not a Standard C header
[snip]
Your code utilizes non-Standard functionality which makes it off-topic
here. comp.unix.progr ammer would be more appropriate, but before you
run over there and post your code, you might want to explain what your
code is supposed to do and clearly articulate what your questions are.
Most people won't be too receptive to "Please figure out what my code
does and QA it for me for free".
Robert Gamble ch******@gmail-dot-com.no-spam.invalid (Clunixchit) writes: I would appreciate if you can criticise my code as well as if possible give advice on how to improve it.
As a first comment, your indentation style seems completely random:
[...]
void array_to_struct ure ( Loading **myLoad , char tmp[nLINES][LINE_LENGTH] , int n ) { int i=0; Loading *program;
while ( i < n ) { //printf("1.%s\n" ,tmp[i]); Loading *program = malloc ( sizeof(Loading) ); if (!program){ fprintf(stdout, "program %s failed",tmp[i]); return; }
--
"You call this a *C* question? What the hell are you smoking?" --Kaz
On Wed, 1 Jun 2005 09:42:44 +0000 (UTC), ch******@gmail-dot-com.no-spam.invalid (Clunixchit) wrote: 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_struct ure * n = number of lines in the file */
void array_to_struct ure ( Loading **myLoad , char tmp[nLINES][LINE_LENGTH] , int n ) { int i=0; Loading *program;
while ( i < n )
Is there a reason you didn't use the more natural for loop?
{ //printf("1.%s\n" ,tmp[i]); Loading *program = malloc ( sizeof(Loading) ); if (!program){ fprintf(stdout, "program %s failed",tmp[i]); return;
At this point, how is the calling function supposed to know the
program failed? Should you set *myLoad to NULL? Then again, how will
the calling function know on which iteration this function failed?
} (*program).name = malloc ( strlen(tmp[i]) + 1 ); strcpy( (*program).name ,tmp[i] ); (*program).pid = -1; (*program).prec = *myLoad; *myLoad=program ; i++; }
} void find_comment(ch ar *tline) {
A strange title for a function that doesn't look for comments.
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[i] , LINE_LENGTH-1 , stream )
fgets already subtracts the 1 for you.
!= NULL ){ size_t l = strlen(tmp[i]);
C89 does not support declarations after executable code in a block.
if (l > 0 && tmp[i][l-1] == '\n') tmp[i][l-1] = '\0'; //printf("%zu %s\n",strlen(t mp[i]),tmp[i]); i++; } fclose (stream); j=i; while(j>=0) {
A for loop would be simpler.
find_comment(tm p[j]);
You already removed the \n above. Why are you trying to remove it
again?
j--; }
array_to_struct ure(myLoad,tmp, i); }
void view( Loading *p ) { char *arg_list[]={(*p).name,NUL L};
if ( fork() == 0 ) { (*p).pid = getpid(); printf("Loading : %s (%d)\n",(*p).n ame,(*p).pid); if (execvp((*p).na me,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);
You forgot to free (*T).name first.
*T=tmp; } }
int main () { Loading *myLoad = NULL;
printf("\n---- myAI mainboard: %d\n",getpid() ); config_file(&my Load,MYAIRC); view_structure( myLoad); wait(NULL); clear(&myLoad); printf("\n"); return 0; }
<<Remove the del for email>>
Clunixchit wrote: 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 ;
using C99 better
typedef struct Loading {
struct Loading *prec;
pid_t pid;
char name[];
} Loading;
/* ----array_to_struct ure * n = number of lines in the file */
void array_to_struct ure ( Loading **myLoad , char tmp[nLINES][LINE_LENGTH] , int n ) {
Better return the number of generated list items, so the caller
can check for success
n == array_to_struct ure(*,*,n)
int i=0; Loading *program;
What happens if myLoad == NULL ?
while ( i < n )
use for(i = 0; i < n; ++i) instead
{//printf("1.%s\n" ,tmp[i]); Loading *program = malloc ( sizeof(Loading) ); if (!program){ fprintf(stdout, "program %s failed",tmp[i]); return; } (*program).name = malloc ( strlen(tmp[i]) + 1 ); strcpy( (*program).name ,tmp[i] );
A faster and more memory efficient solution using the modified Loading type
would be
for(i = 0; i < n; ++i)
if (NULL != tmp[i]){
size_t len = strlen(tmp[i] + 1u);
program = malloc(sizeof(L oading) + len);
if (NULL == program) {
perror(tmp[i]);
break;
}
program->prec = *myLoad;
program->pid = -1;
(void)memcpy(pr ogram->name, tmp[i], len);
*myLoad = program;
}
return i;
}
(*program).pid = -1; (*program).prec = *myLoad;
use program->prec instead
*myLoad=program ; i++; }
} void find_comment(ch ar *tline) { char *comment;
comment = strchr(tline, '\n');/* Replace \n with \0 for consistency. */ if (comment != NULL) { *comment = '\0'; } }
drop the whole function void config_file ( Loading **myLoad , char *config_file ){
double use of config_file use filename instead
How can the caller check if there were no error ?
int i=0,j=0; char tmp[nLINES][LINE_LENGTH];
What happens if myLoad == NULL or config_file == NULL ?
FILE *stream = fopen( config_file , "r" ); if ( stream == NULL ) { fprintf (stdout , "fopen %s failed" , *config_file );
Write to stderr instead or just use
perror(file_nam e);
return; }
This whole loop leads to a memory access failure if more then
nLINES are in the file. So again better
for (i = 0; i < nLINES; ++i) {
if (NULL == fgets(tmp[i], LINE_LENGTH-1, stream)) {
if (feof(stream)
break;
perror(file_nam e);
...
}
while( fgets( tmp[i] , LINE_LENGTH-1 , stream ) != NULL ){
Here you are doing the same thing twice better do it once
{
char *ptr =strchr(tmp[i], '\n');
if (ptr)
*ptr = '\000';
and maybe you want do drop empty lines
if ('\000' == tmp[i][0])
--i
} size_t l = strlen(tmp[i]); if (l > 0 && tmp[i][l-1] == '\n') tmp[i][l-1] = '\0'; //printf("%zu %s\n",strlen(tm p[i]),tmp[i]); i++; } fclose (stream); j=i; while(j>=0) { find_comment(tm p[j]); j--; }
array_to_struct ure(myLoad,tmp, i); }
void view( Loading *p ) { char *arg_list[]={(*p).name,NUL L};
if ( fork() == 0 ) {
fork may fail and nobody gets a message
(*p).pid = getpid(); printf("Loading : %s (%d)\n",(*p).na me,(*p).pid);
The check here is not necessary if execvp was successfull the code is
"overridden ". On the other side if it fails you should exit the
childprocess here exit(EXIT_FAILU RE) otherwise you will have your programm
running twice if (execvp((*p).na me,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);
With the above change using C99 this is correct otherwise you will have to
add free((*T)->name) above your call to free *T=tmp; } }
int main () {
int main(void) is correct Loading *myLoad = NULL;
printf("\n---- myAI mainboard: %d\n",getpid()) ; config_file(&my Load,MYAIRC); view_structure( myLoad); wait(NULL);
If any of your subtask finished the wait will "come back" clear(&myLoad); printf("\n"); return 0; }
--
Michael Knaup
Barry Schwarz wrote: On Wed, 1 Jun 2005 09:42:44 +0000 (UTC), ch******@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:!= NULL ){ size_t l = strlen(tmp[i]);
C89 does not support declarations after executable code in a block.
There is no declaration after executable code in the block.
Dietmar Schindler wrote: Barry Schwarz wrote: On Wed, 1 Jun 2005 09:42:44 +0000 (UTC), ch******@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:!= NULL ){ size_t l = strlen(tmp[i]);
C89 does not support declarations after executable code in a block.
There is no declaration after executable code in the block.
.... and
C89 doesn't support declarations after *statements* in a block,
but object definitions, especially ones with initialization,
are executable code themselves
and may be followed by various declarations.
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Shufen |
last post by:
Hi,
I'm a newbie that just started to learn python, html and etc. I have
some questions to ask and hope that someone can help me on.
I'm trying to code a python script (with HTML) to get values from a
html form that consists of about 10 checkbox and a textbox where user
have to key in a value to perform a search.
From python tutors, I learned that I have to use the following method:
|
by: rked |
last post by:
I get nameSPAN1 is undefined when I place cursor in comments box..
<%@ LANGUAGE="VBScript" %>
<% DIM ipAddress
ipAddress=Request.Servervariables("REMOTE_HOST")
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
|
by: x muzuo |
last post by:
Hi guys,
I have got a prob of javascript form validation which just doesnt work
with my ASP code. Can any one help me out please.
Here is the code:
{////<<head>
<title>IIBO Submit Page</title>
</head>
<style type="text/css">
|
by: Zellan |
last post by:
Hi All,
Have added a Strconv function to the AfterUpdate event of a field to give me
first letter capitalization (vbProperCase). I have added a msgbox that will
allow me to over-ride where necessary however it does not work.
The code below will allow me to manually type in capitals into the full
string when I hit the "Yes" button, but when I elect "No" the vbProperCase
is not activated and no change is made. Can anyone please help me...
|
by: Fuzzyman |
last post by:
Sorry for this hurried message - I've done a new implementation of out
ordered dict. This comes out of the discussion on this newsgroup (see
blog entry for link to archive of discussion).
See the latest blog entry to get at it :
http://www.voidspace.org.uk/python/weblog/index.shtml
Criticism solicited (honestly) :-)
We (Nicola Larosa and I) haven't yet made any optimisations - but there
| |
by: KiMcHeE |
last post by:
Hi! I'm having a real difficult time trying to figure out what is
wrong with my program code. I'm trying to make a "calculator" in the C
language and I only know basic stuff. Here's my code:
#include <stdio.h>
/* global variables */
int e, f;
|
by: jd2007 |
last post by:
Why the Ajax code below in ajax.js is causing my form not to work ?
ajax.js:
var a=0;
var b=0;
var c=0;
var d=0;
var e=0;
var f=0;
|
by: =?Utf-8?B?cm9kY2hhcg==?= |
last post by:
hey all,
please reference: http://pastebin.com/mef78b3d
As shown in the reference I am dynamically building my controls in my class.
I'd like to be able to attach javascript calls as attributes to each of my
controls if applies.
for example,
String varString = "alert('i'm a textbox')";
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |