Ejemplo n.º 1
0
int CLDEditTxtFile::LoadData()
{
	if (TRUE == PathFileExists(m_strTxtFile))
	{
		//_LoadTxtFileWithItems(m_arrFileItem);
		return _fgets(m_arrFileItem);
	}

	return 0;
}
char *
read_stdin1(int *stdin_size) {
    int input_size;
	char *temp = NULL; 

    struct stat stat;
    fstat(fileno(input_file), &stat);
    input_size = stat.st_size;

    char *result = _malloc(input_size);

    do {
      assert(_fgets(result, input_size, input_file));
    } while (strncmp(result, ">THREE", 6));

    int read = 0;


    while (_fgets(result + read, input_size - read, input_file)) {

		
        int len = strlen(result + read);
        if (len == 0 || result[read] == '>') {
            break;
        }

		fprintf(stdout, "%s \n",result + read);

        read += len;
        if (result[read - 1] == '\n') {
            read--;
        }
    }



    result[read++] = '>';
    result = realloc(result, read);
    *stdin_size = read;

    return result;
}
Ejemplo n.º 3
0
char *
fgets(char *s, int size, FILE * stream)
{
    libc_func(fgets, char *, char *, int, FILE *);
    char *res;
    int len;

    res = _fgets(s, size, stream);
    if (res != NULL) {
	len = strlen(res);
	script_record_op('r', fileno(stream), s, len);
    }
    return res;
}
Ejemplo n.º 4
0
char * fgets ( char * str, int num, FILE * stream )
{
    //printf("INTERCEPTED fgets\n");

    clock_t start = clock();

    char* ret = _fgets(str,num,stream);
    
    clock_t end = clock();

    double called_time = (double)(start-program_start)/(double)(CLOCKS_PER_SEC);

    double exec_time = (double)(end-start)/(double)(CLOCKS_PER_SEC);

    fprintf(logFile,"%lf %lf fgets %s %d %p = %s\n",called_time,exec_time,str,num,stream,ret);

    return ret;

}
int login() {
	char buff[20];

	fprintf(FD, "R NAME %s\n", Robot.name);
	_fgets(buff, 20, FD);

	if (strcmp(buff, "S ERROR FULL") == 0) {
		debug printf("Arena is full, disconnect\n");

		return FALSE;
	} else if (strcmp(buff, "S ERROR NAME") == 0) {
		debug printf("name not given or invalid, disconnect\n");

		return FALSE;
	} else if (strcmp(buff, "S WAIT") == 0) {
		debug printf("Accepted, waiting for challenge\n");

		return TRUE;
	} else {
		return FALSE;
	}
}
Ejemplo n.º 6
0
static void
script_start_record(int fd, const char *logname, const char *recording_path, enum script_record_format fmt)
{
    FILE *log;
    struct script_record_info *srinfo;

    if (fd_map_get(&script_recorded_fds, fd, NULL)) {
	fprintf(stderr, "script_start_record: internal error: fd %i is already being recorded\n", fd);
	abort();
    }

    log = fopen(logname, "a+");
    if (log == NULL) {
	perror("umockdev: failed to open script record file");
	exit(1);
    }

    /* if we have a previous record... */
    fseek(log, 0, SEEK_END);
    if (ftell(log) > 0) {
	DBG(DBG_SCRIPT, "script_start_record: Appending to existing record of format %i for path %s\n", fmt, recording_path);
	/* ...and we're going to record the device name... */
	if (recording_path) {
	    /* ... ensure we're recording the same device... */
	    char *existing_device_path;
	    char line[1000];
	    libc_func(fgets, char *, char *, int, FILE *);

	    fseek(log, 0, SEEK_SET);
	    while (_fgets(line, sizeof(line), log)) {
		switch (fmt) {
		    case FMT_DEFAULT:
			/* Start by skipping any leading comments */
			if (line[0] == '#')
			    continue;
			if (sscanf(line, "d 0 %ms\n", &existing_device_path) == 1)
			{
			    DBG(DBG_SCRIPT, "script_start_record: recording %s, existing device spec in record %s\n", recording_path, existing_device_path);
			    /* We have an existing "d /dev/something" directive, check it matches */
			    if (strcmp(recording_path, existing_device_path) != 0) {
				fprintf(stderr, "umockdev: attempt to record two different devices to the same script recording\n");
				exit(1);
			    }
			    free(existing_device_path);
			}
			// device specification must be on the first non-comment line
			break;

		    case FMT_EVEMU:
			if (strncmp(line, "E: ", 3) == 0)
			    break;
			if (sscanf(line, "# device %ms\n", &existing_device_path) == 1) {
			    DBG(DBG_SCRIPT, "script_start_record evemu format: recording %s, existing device spec in record %s\n", recording_path, existing_device_path);
			    /* We have an existing "/dev/something" directive, check it matches */
			    if (strcmp(recording_path, existing_device_path) != 0) {
				fprintf(stderr, "umockdev: attempt to record two different devices to the same evemu recording\n");
				exit(1);
			    }
			    free(existing_device_path);
			}
			break;

		    default:
			fprintf(stderr, "umockdev: unknown script format %i\n", fmt);
			abort();
		}
	    }

	    fseek(log, 0, SEEK_END);
	}

	/* ...finally, make sure that we start a new line */
	putc('\n', log);
    } else if (recording_path) { /* this is a new record, start by recording the device path */
	DBG(DBG_SCRIPT, "script_start_record: Starting new record of format %i\n", fmt);
	switch (fmt) {
	    case FMT_DEFAULT:
		fprintf(log, "d 0 %s\n", recording_path);
		break;

	    case FMT_EVEMU:
		fprintf(log, "# EVEMU 1.2\n# device %s\n", recording_path);
		break;

	    default:
		fprintf(stderr, "umockdev: unknown script format %i\n", fmt);
		abort();
	}
    }

    srinfo = malloc(sizeof(struct script_record_info));
    srinfo->log = log;
    assert(clock_gettime(CLOCK_MONOTONIC, &srinfo->time) == 0);
    srinfo->op = 0;
    srinfo->fmt = fmt;
    fd_map_add(&script_recorded_fds, fd, srinfo);
}
Ejemplo n.º 7
0
/*
Seq file should be like this:
;
[Empty line or comment or id ...]
[Empty line or sequence data]
*/
bool t_structure::verify_seq(char* seq_fp)
{
	return(true);

	FILE* f_seq = open_f(seq_fp, "r");

	char line_buffer[MAX_HEADER_LENGTH];

	_fgets(line_buffer, MAX_HEADER_LENGTH, f_seq);

	// If the first character of first line is not semicolon, 
	// this is not a valid sequence file.
	if(line_buffer[0] != ';')
	{
		printf("Verification failed for sequence file %s @ %s(%d)\n", seq_fp, __FILE__, __LINE__);
		return(false);
	}

	int current_line_cnt = 2;
	int i_seq = 0;
	char seq_data[MAX_HEADER_LENGTH];

	// Read file and fill lines.
	while(1)
	{
		// Read next line starting with 2nd line.
		if(_fgets(line_buffer, MAX_HEADER_LENGTH, f_seq))
		{
			//printf("Current line_buffer: %s\n", line_buffer);

			// If currently read line is after 2nd line
			// the sequence data is being retrieved.
			if(current_line_cnt > 2)
			{
				for(int i = 0; i < (int)strlen(line_buffer); i++)
				{
					// If this is end of sequence, 
					if(seq_data[i_seq - 1] == '1') // Is sequence data already finished?
					{
						printf("Sequence data is ending before file ends, exiting at %s(%d)\n", __FILE__, __LINE__);
						return(false);
					}

					if(line_buffer[i] != '1' &&
						line_buffer[i] != 'A' &&
						line_buffer[i] != 'C' &&
						line_buffer[i] != 'G' &&
						line_buffer[i] != 'U' &&
						line_buffer[i] != 'T' &&
						line_buffer[i] != 'a' &&
						line_buffer[i] != 'c' &&
						line_buffer[i] != 'g' &&
						line_buffer[i] != 'u' &&
						line_buffer[i] != 't')
					{
						printf("Unknown nucleotide in sequence: %c, exiting at %s(%d)\n", line_buffer[i], __FILE__, __LINE__);
						return(false);
					}
					seq_data[i_seq++] = line_buffer[i];
				}
			}

			current_line_cnt++;
		}
		else
		{
			break;
		}
	}

	// If 2nd line is not read OR no sequence data is read,
	// return false.
	/*
	if(current_line_cnt < 3 ||		// Check if at least 3 lines are read.
		i_seq == 0 ||				// Check if sequence data is read.
		seq_data[i_seq - 1] != '1') // Check correct ending of seq_data
	{
		printf("Verification failed for sequence file %s @ %s(%d)\n", seq_fp, __FILE__, __LINE__);
		return(false);
	}
	*/

	if(current_line_cnt < 3)	// Check if at least 3 lines are read.
	{
		printf("Verification failed for sequence file %s @ %s(%d)\n", seq_fp, __FILE__, __LINE__);
		return(false);
	}

	if(i_seq == 0)				// Check if sequence data is read.
	{
		printf("Verification failed for sequence file %s @ %s(%d): No sequence data\n", seq_fp, __FILE__, __LINE__);
		return(false);
	}

	if(seq_data[i_seq - 1] != '1') // Check correct ending of seq_data
	{
		printf("Verification failed for sequence file %s @ %s(%d): %c\n", seq_fp, __FILE__, __LINE__, seq_data[i_seq - 1]);
		return(false);
	}

	fclose(f_seq);

	return(true);
}
int main(int argc,char *argv[]) {
	struct sockaddr_in server;
	int
		sock,
		port,
		err;
	char
		buff[2000];

	if (argc != 3) {
		printf("Numero de argumentos invalido\n");
		printf("Modo de uso:\n\n");
		printf("\t robot [IP] [PORT]\n\n");
		printf("\t IP: dirección ip del servidor. e.j. 127.0.0.1\n");
		printf("\t PORT: número del puerto del servidor. e.j. 5600\n\n");
		exit(-1);
	}

	sock = socket(PF_INET, SOCK_STREAM, 0);
	port = atoi(argv[2]);
	server.sin_family = AF_INET;
	server.sin_port = htons(port);
	server.sin_addr.s_addr = inet_addr(argv[1]);

	err = connect(sock, (struct sockaddr *)&server, sizeof(server));

	if (err == -1) {
		printf("No se pudo establecer la conexión con el servidor\n");
		exit(-1);
	}

	FD = fdopen(sock, "w+");

    signal(SIGCONT, s_cont);

    if (pipe(pipeFD) == -1) {
        printf("Error al inicial el pipe. pipe() returned -1\n");
        exit(-1);
    }
    
	setbuf(FD, NULL);
	initRobot();

	buff[0] = '\0';

	if (login() == FALSE) {
		printf("Error al iniciar la sesion\n");
		fclose(FD);
		exit(-1);
	}

	CONTROLLER_ID = getpid();
	ROBOT_ID = fork();

    if (ROBOT_ID == -1) {
        printf("Error al iniciar, fork() returned -1\n");
        exit(-1);
    }

	if (ROBOT_ID > 0) {
	    signal(NTFY_ROBOT_PAUSE, pausedRobot);

		// CONTROLLER
		while (_fgets(buff, 2000, FD) != NULL) {
		    debug printf("[Controller %d] msg from server: %s\n", getpid(), buff);

			if (strpos(buff, "S ACCEPTED") != -1) {
				kill(ROBOT_ID, S_ACCEPTED);
			} else if (strpos(buff, "S WIN") != -1) {
				kill(ROBOT_ID, S_WIN);
				break;
			} else if (strpos(buff, "S LOSE") != -1) {
				kill(ROBOT_ID, S_LOSE);
				break;
			} else if (strpos(buff, "S DRAW") != -1) {
				kill(ROBOT_ID, S_DRAW);
				break;
			} else if (strpos(buff, "S URTURN") != -1) {
			    if (resumeRobot() == 0) {
                    write(pipeFD[1], buff, strlen(buff) + 1);
			    }

                debug printf("[Controller %d]: Durmiendo\n", getpid());
                pause();
                debug printf("[Controller %d]: Despertado\n", getpid());
			}
		}

        kill(ROBOT_ID, SIGTERM);
		wait(ROBOT_ID);
		shutdown(sock, 2);
		fclose(FD);
		close(pipeFD[0]);
		close(pipeFD[1]);
		debug printf("## Fin del programa ##\n");
		exit(0);
	} else if (ROBOT_ID == 0) {
		// ROBOT
		ROBOT_ID = getpid();

		signal(S_ACCEPTED, s_accepted);
		signal(S_WIN, s_win);
		signal(S_LOSE, s_lose);
		signal(S_DRAW, s_draw);

    	while(1) {
			// se bloquea a la espera del tubo
    	    doTurn();
    	}
	}
}
Ejemplo n.º 9
0
Archivo: fgets.c Proyecto: sbrocket/plr
char *fgets(char *s, int size, FILE *stream) {
  // Get libc syscall function pointer & offset in image
  libc_func(fgets, char *, char *, int, FILE *);
    
  if (plr_checkInsidePLR()) {
    // If already inside PLR code, just call original syscall & return
    return _fgets(s, size, stream);
  } else {
    plr_setInsidePLR();
    int fn = fileno(stream);
    plrlog(LOG_SYSCALL, "[%d:fgets] Read line from fileno %d\n", getpid(), fn);
    
    syscallArgs_t args = {
      .addr = _off_fgets,
      .arg[0] = fn,
      .arg[1] = size
    };
    plr_checkSyscallArgs(&args);
    
    // Nested function actually performed by master process only
    char *ret;
    fgetsShmData_t shmDat;
    int masterAct() {
      // Call original libc function
      ret = _fgets(s, size, stream);
      
      // Use ftell to get new file offset
      shmDat.err = errno;
      shmDat.retNull = (ret == NULL);
      shmDat.sLen = (ret == NULL) ? 0 : strlen(s)+1;
      shmDat.offs = ftell(stream);
      shmDat.eof = feof(stream);
      shmDat.ferr = ferror(stream);
      
      // Store return value & returned data in shared memory for slave processes
      plr_copyToShm(&shmDat, sizeof(shmDat), 0);
      if (!shmDat.retNull) {
        plr_copyToShm(s, shmDat.sLen, sizeof(shmDat));
      }
      
      if (shmDat.ferr) {
        // Not sure how to handle passing ferror's to slaves yet, no way 
        // to manually set error state
        plrlog(LOG_ERROR, "[%d:fgets] ERROR: ferror (%d) occurred (%d)\n", getpid(), shmDat.ferr, fn);
        exit(1);
      }
      
      return 0;
    }
    // All processes call plr_masterAction() to synchronize at this point
    plr_masterAction(masterAct);
    
    if (!plr_isMasterProcess()) {
      // Slaves copy return values from shared memory
      plr_copyFromShm(&shmDat, sizeof(shmDat), 0);
      if (!shmDat.retNull) {
        plr_copyFromShm(s, shmDat.sLen, sizeof(shmDat));
      }
      
      // Slaves seek to new fd offset
      // Can't use SEEK_CUR and advance by ret because the slave processes
      // may have been forked from each other after the fd was opened, in which
      // case the fd & its offset are shared, and that would advance more than needed
      fseek(stream, shmDat.offs, SEEK_SET);
      
      // Necessary to manually reset EOF flag because fseek clears it
      if (shmDat.eof) {
        // fgetc at EOF to set feof indicator
        int c;
        if ((c = fgetc(stream)) != EOF) {
          const char *fmt = "[%d:fgets] ERROR: fgetc to cause EOF actually got data (%c %d), ftell = %d, feof = %d\n";
          plrlog(LOG_ERROR, fmt, getpid(), c, c, ftell(stream), feof(stream));
          exit(1);
        }
      }
      if (shmDat.ferr) {
        plrlog(LOG_ERROR, "[%d:fgets] ERROR: ferror (%d) from master (%d)\n", getpid(), shmDat.ferr, fn);
        exit(1);
      }
    }
    
    // TEMPORARY
    // Slave processes sometimes end up with the wrong file offset, even after SEEK_SET
    // Compare file state at exit to make sure everything is consistent
    // Piggybacking off checkSyscallArgs mechanism to do this
    syscallArgs_t exitState = {
      .arg[0] = ftell(stream),
      .arg[1] = feof(stream),
      .arg[2] = ferror(stream),
    };
    plr_checkSyscallArgs(&exitState);
    
    // All procs return same value & errno
    ret = (shmDat.retNull) ? NULL : s;
    errno = shmDat.err;
    
    plr_clearInsidePLR();
    return ret;
  }