예제 #1
0
/* Changes fileLines to point to an array of strings, one per each
 * line in the named file.  Returns the number of strings, or 0 if
 * there was a problem reading the file.
 */
int fgetlines(char* fileName, char ***fileLines) {
    /* This implementation uses dynamic memory allocation to ask the
     * operating system for just enough memory to hold the data.  The
     * code has extensive error checking of return values.  Don't worry
     * if you don't understand this code yet; we'll cover dynamic
     * memory allocation more in future sessions.  But it is good
     * practice to see if you can work out what we're doing here.
     */
    int MAX_LINES = 255;
    int lineCount = 0;
    char **result = calloc(MAX_LINES, sizeof(char *));
    if (result == NULL)
        return 0;

    FILE *filePtr;
    filePtr = fopen(fileName, "r");
    if (filePtr == NULL) {
        free(result);
        return 0;
    }

    char *nextLine = calloc(MAX_LINE_LENGTH, sizeof(char));
    if (nextLine == NULL) {
        free(result);
        return 0;
    }
    int len = fgetline(filePtr, nextLine, MAX_LINE_LENGTH);
    while (len && lineCount < MAX_LINES) {
        result[lineCount] = nextLine;
        lineCount++;
        nextLine = calloc(MAX_LINE_LENGTH, sizeof(char));
        if (nextLine == NULL) {
            freelines(result, lineCount);
            return 0;
        }
        len = fgetline(filePtr, nextLine, MAX_LINE_LENGTH);
    }

    /* Frees the last line, which wasn't used. */
    free(nextLine);

    /* Closes the file. */
    int e = fclose(filePtr);
    if (e == EOF) {
        /* If there's an error closing the file, then release all the memory
         * we asked for and return 0. */
        freelines(result, lineCount);
        return 0;
    }

    /* Shrinks the line array to just the size needed. */
    char **reallocResult = realloc(result, lineCount * sizeof(char *));
    if (reallocResult == NULL)
        *fileLines = result;
    else
        *fileLines = reallocResult;
    return lineCount;
}
예제 #2
0
파일: xyz2tle.c 프로젝트: cbassa/sattools
// Read data file
struct data read_data(char *filename,double mjd0)
{
  int i=0,status;
  char line[LIM];
  FILE *file;
  struct data d;
  int min;
  double ra,de,ra0,de0,r;
  double x,y,z;

  // Open file
  file=fopen(filename,"r");
  if (file==NULL) {
    fprintf(stderr,"Failed to open %s\n",filename);
    exit(1);
  }

  // Count lines
  while (fgetline(file,line,LIM)>0) 
    i++;
  d.n=i;

  // Allocate
  d.p=(struct point *) malloc(sizeof(struct point)*d.n);

  // Rewind file
  rewind(file);

  // Read data
  i=0;
  while (fgetline(file,line,LIM)>0) {
    status=sscanf(line,"%d,%lf,%lf,%lf",&min,&x,&y,&z);
    if (d.n==1008)
      min*=10;
    d.p[i].mjd=mjd0+(double) min/1440.0;

    // Precess position
    r=sqrt(x*x+y*y+z*z);
    ra0=atan2(y,x)*R2D;
    de0=asin(z/r)*R2D;

    precess(51544.5,ra0,de0,d.p[i].mjd,&ra,&de);
    d.p[i].r.x=r*cos(de*D2R)*cos(ra*D2R);
    d.p[i].r.y=r*cos(de*D2R)*sin(ra*D2R);
    d.p[i].r.z=r*sin(de*D2R);

    d.p[i].flag=0;
    i++;
  }

  // Close file
  fclose(file);

  return d;
}
예제 #3
0
/* Reads the file with the given name, one line at a time and
 * writes the characters to the console. */
void printFileToConsole2(char *name) {
    char line[MAX_LINE_LENGTH];
    FILE *fp;
    fp = fopen(name, "r");
    int len;
    len = fgetline(fp, line, MAX_LINE_LENGTH);
    while (len) {
        printf("%s", line);
        len = fgetline(fp, line, MAX_LINE_LENGTH);
    }
    fclose(fp);
}
예제 #4
0
파일: tle2ole.c 프로젝트: cbassa/sattools
int main(int argc,char *argv[])
{
  int reverse=0;
  char line[LIM],pline[LIM],tlefile[LIM];
  char line0[70],line1[70],line2[70];
  FILE *file;
  int arg=0;

  // Decode options
  while ((arg=getopt(argc,argv,"c:rh"))!=-1) {
    switch(arg) {

    case 'c':
      strcpy(tlefile,optarg);
      break;

    case 'r':
      reverse=1;
      break;

    case 'h':
    default:
      printf("-c <catalog>\n-r reverse\n");
      return 0;
    }
  }

  if (reverse==0) {
    file=fopen(tlefile,"r");
    while (fgetline(file,line,LIM)>0) {
      if (line[0]=='1') {
	strcpy(line0,pline);
	strcpy(line1,line);
	fgetline(file,line,LIM);
	strcpy(line2,line);
	printf("%s | %s | %s\n",line1,line2,line0);
      } else {
	strcpy(pline,line);
      }
    }
    fclose(file);
  } else {
    file=fopen(tlefile,"r");
    while (fgetline(file,line,LIM)>0) {
      printf("%.70s\n%.70s\n%.70s\n",line+144,line,line+72);
    }
    fclose(file);
  }

  return 0;
}
예제 #5
0
gint read_nwout(gchar *filename, struct model_pak *model)
{
gchar line[LINELEN];
FILE *fp;

fp = fopen(filename, "rt");
if (!fp)
  return(1);

while (!fgetline(fp, line))
  {
  if (g_strrstr(line, "Geometry \"geometry\" -> \"geometry\"") != NULL)
    {
/* go through all frames to count them */
    read_nwout_block(fp, model);
    animate_frame_store(model);
    }
  }

/* done */
g_free(model->filename);
model->filename = g_strdup(filename);

g_free(model->basename);
model->basename = parse_strip(filename);

model_prep(model);

fclose(fp);

return(0);
}
예제 #6
0
파일: filecmd.c 프로젝트: badcodes/c
DEFINE_CMD_MAIN(filecmd,int argc,TCHAR** argv) {
    int i;
    TCHAR* fn;
    FILE* fp;
    for(i=1;i<argc;i++) {
        fn=argv[i];
        if(_tcscmp(fn,TEXT("-"))==0)
            fp=stdin;
        else {
            fp=_tfopen(fn,TEXT("r"));
        }
        if(fp) {
            TCHAR* line = malloc(MAX_ARG_LENGTH*sizeof(TCHAR)+1);
            int l=0;
            DEBUG_PRINT(TEXT("reading file %s\n"),fn);
            while((l=fgetline(line,MAX_ARG_LENGTH,fp))!=-1) {
                if(l==0)
                    continue;
                if(!process_line(APPNAME,line))
                    break;
            }
            fclose(fp);
        }
        else {
            fprintf(stderr,TEXT("File not accessable:\"%s\"\n"),fn);
        }
    }
    return 0;
}
예제 #7
0
파일: fgetline.c 프로젝트: 257/pbook
int
main() {
ifp = fopen(filename, "r");
if(ifp == NULL)
	{
	fprintf(stderr, "can't open %s\n", filename);
	exit(EXIT_FAILURE);
	}

while(fgetline(ifp, line, MAXLINE) != EOF)
	{
	if(nrows >= MAXROWS)
		{
		fprintf(stderr, "too many rows\n");
		exit(EXIT_FAILURE);
		}

	// n = getwords(line, words, MAXCOLS);
	/* for(words = strtok(l, " ");
				words != NULL && i <= LAST;
				i++, tokenp = strtok(NULL, delim)) {
				*/
		
	/*for(i = 0; i < n; i++)
		array[nrows][i] = atoi(words[i]);
	nrows++;
	*/
	for(words = strtok(line, delim); words != NULL;
						words = strtok(NULL, delim))
			printf("%s\n", words);
	}
return 0;
}
예제 #8
0
파일: collect.c 프로젝트: Babar/check_multi
static int
include_file(FILE *fbuf, char *name, int *linecount, int *charcount, int echo)
{
	char *interactive, *linebuf = NULL;
	size_t linesize = 0, linelen, count;

	if (fbuf == NULL)
		fbuf = Fopen(name, "r");
	if (fbuf == NULL) {
		perror(name);
		return -1;
	}
	interactive = value("interactive");
	*linecount = 0;
	*charcount = 0;
	fflush(fbuf);
	rewind(fbuf);
	count = fsize(fbuf);
	while (fgetline(&linebuf, &linesize, &count, &linelen, fbuf, 0)
			!= NULL) {
		if (fwrite(linebuf, sizeof *linebuf, linelen, collf)
				!= linelen) {
			Fclose(fbuf);
			return -1;
		}
		if (interactive != NULL && echo)
			fwrite(linebuf, sizeof *linebuf, linelen, stdout);
		(*linecount)++;
		(*charcount) += linelen;
	}
	if (linebuf)
		free(linebuf);
	Fclose(fbuf);
	return 0;
}
예제 #9
0
파일: sdiff.c 프로젝트: andreiw/polaris
static int
put1(void)
{
	/* len1 = length of left side */
	/* nchars = num of chars including tabs */

	char	*bp;


	if ((bp = fgetline(fdes1)) != NULL) {
		len1 = getlen(0, bp);
		if ((!silent || change != ' ') && len1 != 0)
			putline(stdout, bp, nchars);

		if (oflag) {
		/*
		 * put left side either to output file
		 * if identical to right
		 * or left temp file if not
		 */

			if (change == ' ')
				putline(odes, bp, strlen(bp));
			else
				putline(left, bp, strlen(bp));
		}
		if (change != ' ')
			putmid(1);
		num1++;
		return (1);
	} else
		return (0);
}
예제 #10
0
파일: ctfutils.cpp 프로젝트: LabShare/IMOD
/*
 * Ruueads a file of tilt angles whose name is in angleFile, and which must
 * have at least nzz lines.  angleSign should be 1., or -1. to invert the
 * sign of the angles.  Minimum and maximum angles are returned in minAngle
 * and maxAngle.  The return value is the array of tilt angles.
 */
float *readTiltAngles(const char *angleFile, int nzz, float angleSign,
                      float &minAngle, float &maxAngle)
{
  char angleStr[MAX_LINE];
  FILE *fpAngle;
  int k, err;
  float currAngle;
  float *tiltAngles = (float *)malloc(nzz * sizeof(float));
  minAngle = 10000.;
  maxAngle = -10000.;
  if (!tiltAngles)
    exitError("Allocating array for tilt angles");
  if ((fpAngle = fopen(angleFile, "r")) == 0)
    exitError("Opening tilt angle file %s", angleFile);
  for (k = 0; k < nzz; k++) {
    do {
      err = fgetline(fpAngle, angleStr, MAX_LINE);
    } while (err == 0);
    if (err == -1 || err == -2)
      exitError("%seading tilt angle file %s\n",
                err == -1 ? "R" : "End of file while r", angleFile);
    sscanf(angleStr, "%f", &currAngle);
    currAngle *= angleSign;
    minAngle = B3DMIN(minAngle, currAngle);
    maxAngle = B3DMAX(maxAngle, currAngle);
    tiltAngles[k] = currAngle;
  }
  fclose(fpAngle);
  return tiltAngles;
}
예제 #11
0
파일: beast_lib.c 프로젝트: binlv/BEaST
int read_configuration(char *filename, beast_conf *conf){
  int size=0;
  FILE *fd;
  char line[FILENAMELENGTH];

  fd=fopen(filename,"r");

  if (fd==NULL){
    fprintf(stderr,"ERROR! Cannot open configuration file %s\n",filename);
    exit(-1);
  }

  while (fgetline(fd, line, FILENAMELENGTH)!=EOF){
    /* if not a comment */
    if (line[0] != 35){
      sscanf(line,"%d %d %d %lf %lf %lf %d",&conf[size].voxelsize,&conf[size].patchsize,&conf[size].searcharea,&conf[size].alpha,&conf[size].beta,&conf[size].threshold,&conf[size].selectionsize);
      size++;
    }

  }

  fclose(fd);


  return size;
}
예제 #12
0
파일: status.cpp 프로젝트: fishman/asfr
int initstatus(struct JOB_PARM *My_Job)
{
	int i;
	char *DashPtr = strrchr(My_Job->URL, '/');
	if( DashPtr == NULL ) DashPtr = My_Job->URL;
	else DashPtr += 1;

	strcpy(My_Job->StaFileName, DashPtr);
	strcat(My_Job->StaFileName, ".resume");

	My_Job->StaFile = fopen(My_Job->StaFileName, "r+");
	if(My_Job->StaFile != NULL)	/* A sta file exists */
	{
		fgetline(My_Job->StaFile, My_Job->URL);
		fgetline(My_Job->StaFile, My_Job->Proxy);
		fgetline(My_Job->StaFile, My_Job->Username);
		fgetline(My_Job->StaFile, My_Job->Password);
		fscanf(My_Job->StaFile, "{%d %d %ld %ld}\n", &My_Job->MaxThreads, &My_Job->MaxTime, &My_Job->SpecOffset, &My_Job->SpecLength);
		if(My_Job->MaxThreads > MAX_THREADS) My_Job->MaxThreads=MAX_THREADS;
		if(My_Job->MaxThreads < 1) My_Job->MaxThreads=1;
		for(i=0 ; i<My_Job->MaxThreads ; i++)
		{
			if( My_Job->ti[i] != NULL ) free(My_Job->ti[i]);
			if( (My_Job->ti[i] = (struct THREAD_INFO *)malloc(sizeof(struct THREAD_INFO))) == NULL) return 1;
			memset(My_Job->ti[i], 0, sizeof(struct THREAD_INFO));
		}

		for(i=0 ; i<My_Job->MaxThreads ; i++)
			fscanf(My_Job->StaFile, "{%ld %ld}\n", &My_Job->ti[i]->SourOffset, &My_Job->ti[i]->DestLength);

		myfclose( & My_Job->StaFile);
	}
	else /* Create one */
	{
		if(My_Job->MaxThreads > MAX_THREADS) My_Job->MaxThreads=MAX_THREADS;
		if(My_Job->MaxThreads < 1) My_Job->MaxThreads=1;
		for(i=0 ; i<My_Job->MaxThreads ; i++)
		{
			if( My_Job->ti[i] != NULL ) free(My_Job->ti[i]);
			if( (My_Job->ti[i] = (struct THREAD_INFO *)malloc(sizeof(struct THREAD_INFO))) == NULL) return -1;
			memset(My_Job->ti[i], 0, sizeof(struct THREAD_INFO));
		}
		savestatus(My_Job);
	}
	return 0;
}
예제 #13
0
파일: io.c 프로젝트: goshng/psi
static void next_line (int upto, FILE *fp) {
  int i;
  char *line[PSI_IO_GMEL_LINE_MAX];
  for (i = 0; i < upto; i++) {
     fgetline(line, PSI_IO_GMEL_LINE_MAX, fp);
 /* fprintf(stderr, "NEXT LINE: %s", line); */
  }
}
예제 #14
0
파일: xpand.c 프로젝트: n-t-roff/DWB3.3
int
main(int argc, char **argv)
   {
   register int filenum,i;
   char line[LINSIZE],current_file[100];
   FILE *filept[LEVELIMIT];
   register int anyso;
   anyso=0;

   for (filenum = 1; filenum < argc; filenum++)
      {
      i = 0;
      if (*argv[filenum] == '-')
         filept[i] = stdin;
      else
	 {
	 filept[i] = fopen(argv[filenum],"r");    
         if (filept[i] == NULL)
            {
            fprintf(stderr,"xpand: can't open %s : ",argv[filenum]);
	    perror("");
            exit(2);
	    }
         }
      while (i >= 0)
         {
         while (fgetline(line,LINSIZE,filept[i]) != 0)
            {
            if (sscanf(line,".so %100s",current_file) == 1)
               {
               i++;
               anyso++;
               if (i >= LEVELIMIT)
                  {
                  fprintf(stderr,"xpand: limit of %d levels exceeded : ",LEVELIMIT);
		  perror("");
                  exit(2);
                  };
               filept[i] = fopen(current_file,"r");
               if (filept[i] == NULL)
                  {
                  fprintf(stderr,"xpand: can't open %s : ",current_file);
		  perror("");
                  exit(2);
                  }
	       }
            else
               {
               printf("%s",line);
               }
            }
         fclose(filept[i]);
         i--;
         }
      }
   if (anyso == 0) exit(0);
   return (1);
   }
예제 #15
0
파일: bench.c 프로젝트: cutechess/sloppy
/* Run a test suite like:
   - Win at Chess (WAC)
   - Winning Chess Sacrifices and Combinations (WCSAC)
   - Encyclopedia of Chess Middlegames (ECM)
   
   The tests in the suite must be in the format test_pos() uses.  */
void
test_suite(Chess *chess, const char *filename)
{
	S64 timer;
	int npos = 0;
	int nsolved = 0;
	int nfailed = 0;
	char pos[MAX_BUF];
	SearchData *sd;
	SearchData sd_total;
	FILE *fp;

	if ((fp = fopen(filename, "r")) == NULL) {
		my_perror("Can't open file %s", filename);
		return;
	}

	sd = &chess->sd;
	init_search_data(&sd_total);

	printf("Running test suite...\n");
	timer = get_ms();
	while (fgetline(pos, MAX_BUF, fp) != EOF) {
		if (strlen(pos) <= 1)
			continue;
		npos++;
		printf("%d.: ", npos);
		switch (test_pos(chess, pos)) {
		case -1:
			printf("Invalid test position: %s\n", pos);
			continue;
		case 0:
			printf("Couldn't solve test: %s\n", pos);
			nfailed++;
			break;
		case 1:
			printf("Solved test: %s\n", pos);
			nsolved++;
			break;
		case 2:
			printf("Test suite cancelled by user\n");
			my_close(fp, filename);
			return;
		}
		sd_total.nnodes += sd->nnodes;
		sd_total.nqs_nodes += sd->nqs_nodes;
		sd_total.nhash_hits += sd->nhash_hits;
		sd_total.nhash_probes += sd->nhash_probes;
		sd_total.bfactor += sd->bfactor;
	}
	my_close(fp, filename);

	sd_total.bfactor /= nsolved + nfailed;
	timer = get_ms() - timer;
	printf("\n");
	print_search_data(&sd_total, (int)timer);
	printf("\n%d of %d tests were solved.\n", nsolved, nsolved + nfailed);
}
예제 #16
0
static DWORD WINAPI command_read_thread(void *param)
{
	struct command_read_ctx *ctx = (struct command_read_ctx *) param;

	ctx->line = fgetline(stdin);

	SetEvent(ctx->event);

	return 0;
}
예제 #17
0
파일: nss.c 프로젝트: Babar/check_multi
static FILE *
encode(FILE *ip, FILE **hp, FILE **bp, NSSCMSMessage *msg,
		void    (*cb)(void *, const char *, unsigned long))
{
	NSSCMSEncoderContext	*ctx;
	char	*buf = NULL, *cp;
	size_t	bufsize = 0, buflen, count;
	FILE	*op;

	if (smime_split(ip, hp, bp, -1, 0) == STOP)
		return NULL;
	if ((op = Ftemp(&cp, "Ry", "w+", 0600, 1)) == NULL) {
		perror("tempfile");
		return NULL;
	}
	rm(cp);
	Ftfree(&cp);
	if ((ctx = NSS_CMSEncoder_Start(msg,
			cb, op,
			NULL, NULL,
			password_cb, "Pass phrase:",
			NULL, NULL,
			NULL, NULL)) == NULL) {
		fprintf(stderr, "Cannot create encoder context.\n");
		Fclose(op);
		return NULL;
	}
	count = fsize(*bp);
	while (fgetline(&buf, &bufsize, &count, &buflen, *bp, 0) != NULL) {
		buf[buflen-1] = '\r';
		buf[buflen] = '\n';
		if (NSS_CMSEncoder_Update(ctx, buf, buflen+1) != 0) {
			fprintf(stderr, "Failed to add data to encoder.\n");
			Fclose(op);
			return NULL;
		}
	}
	free(buf);
	if (NSS_CMSEncoder_Finish(ctx) != 0) {
		fprintf(stderr, "Failed to encode data.\n");
		Fclose(op);
		return NULL;
	}
	rewind(*bp);
	cb(op, (void *)-1, 0);
	fflush(op);
	if (ferror(op)) {
		perror("tempfile");
		Fclose(op);
		return NULL;
	}
	rewind(op);
	return op;
}
예제 #18
0
/*
 * find_cdrom
 *
 * Determine the name of the CD-ROM device.
 *
 * Read through the boot records (via a call to uerf) and find the SCSI
 * address of the CD-ROM.
 */
void
find_cdrom()
{
	char	*data, *fgetline();
	FILE	*uerf;
	int	fds[2];
	int	pid;
	extern char *getenv();

	pipe(fds);

	cd_device = getenv("CDROM");
	if (cd_device != NULL)
	    return;
	if ((pid = fork()) == 0) {
		close(fds[0]);
		dup2(fds[1], 1);
		execl("/etc/uerf", "uerf", "-R", "-r", "300", NULL);
		execl("/usr/sbin/uerf", "uerf", "-R", "-r", "300", NULL);
		_exit(1);
	} else if (pid < 0) {
		perror("fork");
		exit(1);
	}

	close(fds[1]);
	uerf = fdopen(fds[0], "r");

	while (data = fgetline(uerf))
		if (strstr(data, "RRD42")) {
			char	*device;

			cd_device = (char *)malloc(sizeof("/dev/rrz##c"));
			strcpy(cd_device, "/dev/r");
			device = strstr(data, "rz");
			device[(int)(strchr(device, ' ') - device)] = '\0';
			strcat(cd_device, device);
			strcat(cd_device, "c");
			break;
		}

	fclose(uerf);

	if (cd_device == NULL) {
		fprintf(stderr,
			"No cdrom (RRD42) is installed on this system\n");
		exit(1);
	}

	kill(pid, 15);
	(void)wait((int *)NULL);
}
예제 #19
0
static int md5_checkHash(const char *path) {
    FILE		*pFile;
    //FF_ERROR	ffError;
    char		filePath[FF_MAX_PATH];
    char		chkHash[33], fileHash[33];
    char		chkFilename[FF_MAX_FILENAME];
    char		chkLineBuffer[33 + 2 + FF_MAX_FILENAME + 2];
    char		*c, *d;

    //pFile = FF_Open(pEnv->pIoman, path, FF_MODE_READ, &ffError);
    pFile = fopen(path, "r");

    if(pFile) {
        while(fgetline(pFile, chkLineBuffer, 33 + 2 + FF_MAX_FILENAME + 2)) {
            memcpy(chkHash, chkLineBuffer, 32);
            chkHash[32] = '\0';

            if(chkLineBuffer[32] != ' ' || chkLineBuffer[33] != ' ') {
                printf("Not a properly formatted md5sum file\n");
            } else {

                c = &chkLineBuffer[34];
                d = chkFilename;

                while(*c) {
                    *d++ = *c++;
                }

                *d = '\0';

                ProcessLinuxPath(filePath, chkFilename);
//				strcpy(filePath, chkFilename);
                if(!md5_getHash(filePath, fileHash)) {

                    if(!strcmp(chkHash, fileHash)) {
                        printf("%s: OK\n", chkFilename);
                    } else {
                        printf("%s: FAILED\n", chkFilename);
                    }
                } else {
                    printf("%s: %s: No such file or directory\n", "md5sum", chkFilename);
                }
            }
        }

        //FF_Close(pFile);
        fclose(pFile);
    }

    return 0;
}
예제 #20
0
EXPORT struct passwd *
getpwent()
{
	char	*arr[7];

	if (pwdf == (FILE *)NULL) {
		pwdf = fileopen("/etc/passwd", "r");
		if (pwdf == (FILE *)NULL)
			return ((struct passwd *)0);
	}
	if (fgetline(pwdf, pwdbuf, sizeof (pwdbuf)) == EOF)
		return ((struct passwd *)0);
	breakline(pwdbuf, ':', arr, 7);
	return (mkpwd(arr));
}
예제 #21
0
파일: jeff.c 프로젝트: rdebath/sgt
void do_file(FILE *fp, int unjeff, int (*start_line)(char *line))
{
    if (unjeff) {
	/*
	 * Unjeffing does not depend on the start_line function. We
	 * destroy all \n, and replace all \r with \n.
	 */
	char buf[4096];
	int len;
	int i, j;

	while ((len = fread(buf, 1, sizeof(buf), stdin)) > 0) {
	    for (i = j = 0; i < len; i++) {
		if (buf[i] == '\r')
		    buf[j++] = '\n';
		else if (buf[i] != '\n')
		    buf[j++] = buf[i];
	    }
	    fwrite(buf, 1, j, stdout);
	}
    } else {
	/*
	 * To jeff, we read each line of the file and translate its
	 * trailing \r into a \n. Then we write a \n before output
	 * if the line matches the start_line() criterion and we're
	 * not right at the start of the file; finally, we write a
	 * trailing \n if we've output anything at all.
	 */
	int firstline = TRUE;

	while (1) {
	    char *p = fgetline(fp);
	    char *q;

	    if (!firstline && (!p || start_line(p)))
		fputc('\n', stdout);
	    if (!p)
		break;
	    for (q = p; *q; q++)
		if (*q == '\n')
		    *q = '\r';
	    fputs(p, stdout);
	    firstline = FALSE;

	    free(p);
	}
    }
}
예제 #22
0
파일: fbar.c 프로젝트: wol22/MotleyTools
void function (char start, char space, size_t width, size_t length, flag_t flags) 

{ 
	char buffer [length]; 
	size_t column = 0; 
	width++; 
	width++; 
	if (width > length) 
	{ 
		width = length; 
	} 
	for (column = 0; ~ (length = fgetline (buffer, sizeof (buffer), stdin)); column = 0) 
	{ 
		if (buffer [column++] != start) 
		{ 
			fputline (buffer, length, stdout); 
			continue; 
		} 
		if (buffer [column++] != space) 
		{ 
			fputline (buffer, length, stdout); 
			continue; 
		} 
		if (buffer [column] == CUPPER) 
		{ 
			while (column < width) 
			{ 
				buffer [column++] = CUPPER; 
			} 
			fputline (buffer, 0, stdout); 
			fputline (buffer, column, stdout); 
			continue; 
		} 
		if (buffer [column] == CLOWER) 
		{ 
			while (column < width) 
			{ 
				buffer [column++] = CLOWER; 
			} 
			fputline (buffer, column, stdout); 
			fputline (buffer, 0, stdout); 
			continue; 
		} 
		fputline (buffer, length, stdout); 
	} 
	fputline (buffer, column, stdout); 
	return; 
} 
예제 #23
0
파일: readobj.c 프로젝트: NREL/Radiance
void
readobj(				/* read in an object file or stream */
	char  *inpspec
)
{
	OBJECT  lastobj;
	FILE  *infp;
	char  buf[2048];
	int  c;

	lastobj = nobjects;
	if (inpspec == NULL) {
		infp = stdin;
		inpspec = "standard input";
	} else if (inpspec[0] == '!') {
		if ((infp = popen(inpspec+1, "r")) == NULL) {
			sprintf(errmsg, "cannot execute \"%s\"", inpspec);
			error(SYSTEM, errmsg);
		}
	} else if ((infp = fopen(inpspec, "r")) == NULL) {
		sprintf(errmsg, "cannot open scene file \"%s\"", inpspec);
		error(SYSTEM, errmsg);
	}
	while ((c = getc(infp)) != EOF) {
		if (isspace(c))
			continue;
		if (c == '#') {				/* comment */
			fgets(buf, sizeof(buf), infp);
		} else if (c == '!') {			/* command */
			ungetc(c, infp);
			fgetline(buf, sizeof(buf), infp);
			readobj(buf);
		} else {				/* object */
			ungetc(c, infp);
			getobject(inpspec, infp);
		}
	}
	if (inpspec[0] == '!')
		pclose(infp);
	else if (infp != stdin)
		fclose(infp);
	if (nobjects == lastobj) {
		sprintf(errmsg, "(%s): empty file", inpspec);
		error(WARNING, errmsg);
	}
}
예제 #24
0
Config *Config::load()
{
FILE *fpi;
char *filename;
char line[1024];
char *equ, *ptr;

	settings = (Config *)(new BMessage(M_SETTINGS));
	
	filename = GetConfigFileName();
	if (!filename) return settings;
	
	fpi = fopen(filename, "rb");
	if (!fpi) return settings;
	
	while(!feof(fpi))
	{
		fgetline(fpi, line, sizeof(line) - 1);
		
		equ = strchr(line, '=');
		if (!equ) continue;
		
		*equ = 0;
		ptr = equ-1;
		while(ptr > line && (*ptr == ' ')) { *ptr = 0; ptr--; }
		equ++;
		while(*equ == ' ') equ++;
		
		switch(line[0])
		{
			case '%':
				settings->AddInt32(line+1, atoi(equ));
			break;
			
			case '$':
				settings->AddString(line+1, equ);
			break;
		}
	}


	fclose(fpi);
	return settings;
}
예제 #25
0
파일: sdiff.c 프로젝트: andreiw/polaris
static int
put2(void)
{
	char	*bp;

	if ((bp = fgetline(fdes2)) != NULL) {
		(void) getlen((hlen + WGUTTER) % 8, bp);

		/*
		 * if the left and right are different they are always
		 * printed.
		 * If the left and right are identical
		 * right is only printed if leftonly is not specified
		 * or silent mode is not specified
		 * or the right contains other than white space (len1 !=0)
		 */
		if (change != ' ') {

		/*
		 * put right side to right temp file only
		 * because left side was written to output for
		 * identical lines
		 */

			if (oflag)
				putline(right, bp, strlen(bp));

			if (midflg == 0)
				putmid(1);
			putline(stdout, bp, nchars);
		} else
			if (!silent && !leftonly && len1 != 0) {
				if (midflg == 0)
					putmid(1);
				putline(stdout, bp, nchars);
			}
		num2++;
		len1 = 0;
		return (1);
	} else {
		len1 = 0;
		return (0);
	}
}
예제 #26
0
파일: beast_lib.c 프로젝트: binlv/BEaST
int read_list(char *filename, char **list,char *basedir) {
  FILE *fd;
  char line[FILENAMELENGTH];
  int size=0;
  
  fd=fopen(filename,"r");

  while (fgetline(fd, line, FILENAMELENGTH)!=EOF){
    if(basedir && *basedir)
      sprintf(list[size],"%s/%s",basedir,line);
    else
      sprintf(list[size],"%s",line);
    size++;
  }

  fclose(fd);

  return size;
}
예제 #27
0
void listload (LIST * list, char const *file, char buffer [], size_t length) 

{
	FILE *fp;
	char *sp;
	if ((fp = efopen (file, "r")) != (FILE *) (0)) 
	{
		while (fgetline (buffer, length, fp) != -1) 
		{
			for (sp = buffer; isblank (*sp); ++sp);
			if (strchr ("#;\n", *sp) == (char *) (0)) 
			{
				listinsert (list, sp);
			}
		}
		fclose (fp);
	}
	return;
}
예제 #28
0
파일: wcsfit.c 프로젝트: ppapadeas/sattools
// Read catalog
struct catalog read_catalog(char *filename)
{
  int i=0;
  char line[LIM];
  FILE *file;
  struct catalog c;

  file=fopen(filename,"r");
  while (fgetline(file,line,LIM)>0) {
    sscanf(line,"%f %f %lf %lf",&c.x[i],&c.y[i],&c.ra[i],&c.de[i]);
    c.usage[i]=1;
    
    i++;
  }
  fclose(file);
  c.n=i;

  return c;
}
예제 #29
0
파일: devpolicy.c 프로젝트: andreiw/polaris
static int
loadprivs(const char *infile)
{
	char *line, *col;
	FILE *in;
	struct fileentry *fep;
	int res = 0;

	in = fopen(infile, "r");

	if (in == NULL)
		return (0);

	while ((fep = fgetline(in)) != NULL && fep->entry != NULL) {
		line = fep->entry;

		if (*line == '\0')
			continue;

		line[strlen(line)-1] = '\0';

		col = strchr(line, ':');

		if (col != NULL) {
			major_t maj;
			*col = '\0';

			if (modctl(MODGETMAJBIND, line, col - line + 1, &maj)
			    != 0)
				continue;

			line = col + 1;
		}

		if (modctl(MODALLOCPRIV, line) != 0) {
			(void) err_print("modctl(MODALLOCPRIV, %s): %s\n",
				line, strerror(errno));
			res = -1;
		}
	}
	return (res);
}
예제 #30
0
int
fmBuffer::readfile(char* m) { 
   if ( _mode != FMODE_FREAD) return 1;
   char * s = new char[256];
   int sz = 256;
   _buffer.erase();
   FILE * f = fopen(m , "r");
   std::string nl = "\n";
   if ( f ) { 
      while ( fgetline(&s, &sz, f) != EOF ) {   
         _buffer += s;
         _buffer += nl;
      }
      _ready = true;
      _pos = std::string::npos;
      fclose(f);
      return 0;
   }
   else return 1;
}