From 842d41c923d3f1feb7ec8b23dbcf236d7fdb4b78 Mon Sep 17 00:00:00 2001 From: Kirk McKusick Date: Sun, 21 Jun 1981 23:46:37 -0800 Subject: [PATCH] fix code to read from pipes to request only what it needs SCCS-vsn: usr.bin/pascal/px/int.c 1.4 --- usr/src/usr.bin/pascal/px/int.c | 72 +++++++++++++-------------------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/usr/src/usr.bin/pascal/px/int.c b/usr/src/usr.bin/pascal/px/int.c index 072e16666c..28e959bdd7 100644 --- a/usr/src/usr.bin/pascal/px/int.c +++ b/usr/src/usr.bin/pascal/px/int.c @@ -24,11 +24,10 @@ main(ac,av) { register char *objprog, *file; - register long bytesread, block; + register long bytesread, bytestoread, block; register FILE *prog; struct pxhdr pxhd; # define pipe 3 -# define pipesize 4096 /* * Initialize everything @@ -40,88 +39,73 @@ main(ac,av) /* * Determine how PX was invoked, and how to process the program */ - if (_argv[0][0] == '-' && _argv[0][1] == 'o') - { + if (_argv[0][0] == '-' && _argv[0][1] == 'o') { file = &_argv[0][2]; _mode = PIX; - } - else if (_argc <= 1) - { + } else if (_argc <= 1) { file = "obj"; _mode = PX; - } - else if (_argv[1][0] != '-') - { + } else if (_argv[1][0] != '-') { file = _argv[1]; _mode = PX; - } - else if (_argv[1][1] == 0) - { + } else if (_argv[1][1] == 0) { file = _argv[0]; _mode = PIPE; _argc -= 1; _argv[1] = _argv[0]; _argv = &_argv[1]; - } - else - { + } else { fputs("Improper specification of object file to PX\n",stderr); exit(1); - } + } /* * Process program header information */ - if (_mode == PIPE) + if (_mode == PIPE) { read(pipe,&pxhd,sizeof(struct pxhdr)); - else - { + } else { prog = fopen(file,"r"); - if (prog == NULL) - { + if (prog == NULL) { perror(file); exit(1); - } + } fseek(prog,(long)(HEADER_BYTES-sizeof(struct pxhdr)),0); fread(&pxhd,sizeof(struct pxhdr),1,prog); - } - if (pxhd.maketime < createtime) - { + } + if (pxhd.maketime < createtime) { fprintf(stderr,"%s is obsolete and must be recompiled\n",file); exit(1); - } - if (pxhd.magicnum != MAGICNUM) - { + } + if (pxhd.magicnum != MAGICNUM) { fprintf(stderr,"%s is not a Pascal interpreter file\n",file); exit(1); - } + } /* * Load program into memory */ objprog = malloc((int)pxhd.objsize); - if (_mode == PIPE) - { + if (_mode == PIPE) { + bytestoread = pxhd.objsize; bytesread = 0; - do - { - block = read(pipe,(int)(objprog+bytesread),pipesize); - bytesread += block; + do { + block = read(pipe,(int)(objprog+bytesread),bytestoread); + if (block > 0) { + bytesread += block; + bytestoread -= block; } - while (block); - } - else - { + } while (block > 0); + } else { bytesread = fread(objprog,1,(int)pxhd.objsize,prog); fclose(prog); if (_mode == PIX) unlink(file); - } - if (bytesread != pxhd.objsize) - { + } + if (bytesread != pxhd.objsize) { fprintf(stderr,"Read error occurred while loading %s\n",file); exit(1); - } + } if (_mode == PIX) fputs("Execution begins...\n",stderr); /* -- 2.20.1