Style question (Was: Using a pipe as a stack)
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Style question (Was: Using a pipe as a stack)




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Style question (Was: Using a pipe as a stack)  
Richard


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-30-04 11:02 PM


Materialised@privacy.net wrote...
> Viktor Lofgren wrote: 
> You could reach the limit of the size of the pipe.
> Why not use something like the following: (All credits to Steve Summit)
>
> char           *
> mgetline()
> {
> 	char           *line;
> 	int             nalloc = 10;
> 	int             nch = 0;
> 	int             c;
>
> 	line = malloc(nalloc + 1);
> 	if (line == NULL) {
> 		printf("out of memory\n");
> 		exit(1);
> 	}
> 	while ((c = getchar()) != EOF) {
> 		if (c == '\n')
> 			break;
>
> 		if (nch >= nalloc) {
> 			char           *newp;
> 			nalloc += 10;
> 			newp = realloc(line, nalloc + 1);
> 			if (newp == NULL) {
> 				printf("out of memory\n");
> 				exit(1);
> 			}
> 			line = newp;
> 		}
> 		line[nch++] = c;
> 	}
>
> 	if (c == EOF && nch == 0) {
> 		free(line);
> 		return NULL;
> 	}
> 	line[nch] = '\0';
>
> 	return line;
> }

This brings up a style question:  mgetline() calls exit().  I've
always been guided by the idea that calling exit() in a worker
function such as this one is a Bad Idea -- that, instead, worker
functions should return a value which indicates failure, and the
caller of mgetline() (or mgetline()'s caller's caller, etc.) should
call exit().  At any rate, exit() should be invoked much higher in
the call hierarchy than it is here.

(Not criticizing the poster [or mgetline()'s author], just using this
as an illustration for the style question.)

--
Don't believe anything unless you have thought it through for
yourself.                      (Anna Pell Wheeler, 1883-1966)





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:29 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register