예제 #1
0
/* ----------------------------------------------------------------------------*/
char* programaDisciplina(char* disc){
    
    FILE *fd;
    char texto[10];
    string str;

    /* Normaliza e obtem o nome do arquivo correspondente
     * a disciplina passada como parametro*/
    uppercase(disc);
    strcpy(texto, disc);
    strcat(texto, ".txt");

    fd = fopen(texto, "r");
    /* Verifica se a abertura do arquivo foi bem sucedida */
    if(fd == NULL){
        printf("Erro ao abrir o arquivo da disciplina %s.\n", disc);
        exit(-1);
    }
    else{
        char tmp[200];
        char *pch;

        /* Inicializa um vetor contendo a resposta */
        str = str_initialize(((size_t)NUM_MAX_DISCIPLINAS * TAM_MAX_NOME_DISCIPLINA));
        
        /* Le o titulo da disciplina */
        fgets(tmp, sizeof(tmp), fd);

        str_concat_chararr(str, tmp, sizeof(tmp) -1);

        /* Le o resto do arquivo */
        while(fgets(tmp, sizeof(tmp), fd)!=NULL){
            /* Separa a linha lida */
            pch = strtok(tmp, " ");
            /* Imprime a linha lida */
            while(pch !=NULL){
                /* Para a impressao caso se depare com a palavra "Horário:" */
                
                if(strcmp(pch, "Horário:\n") ==0){
                    str_concat_chararr(str, "\n", sizeof(char));
                    fclose(fd);
                    return str->s;
                }
                else{
                    str_concat_chararr(str, pch, strlen(pch));
                    str_concat_chararr(str, " ", sizeof(char));
                }
                pch = strtok(NULL, " ");
                
                
            }
            

        }
        str_concat_chararr(str, "\n", sizeof(char));
        fclose(fd);

        return str->s;
    }
}
예제 #2
0
/* ----------------------------------------------------------------------------*/
char * listaDisciplinas(){
    FILE *fd;
	string str;
    int i=0;
    
    /* Inicializa uma string para conter as disciplinas */

    str = str_initialize(((size_t)NUM_MAX_DISCIPLINAS * TAM_MAX_NOME_DISCIPLINA));


    /*Abre o arquivo de codigos das disciplinas*/
    fd = fopen("codigo_disciplinas.txt", "r");

    /* Verifica se a abertura do arquivo foi bem sucedida */
    if(fd == NULL){
        printf("Erro ao abrir o arquivo com codigos das disciplinas.\n");
        exit(-1);
    }
    else{
        /*Arquivo aberto com sucesso*/ 
        char temp[120];
        i=0;
        
        while(fgets(temp, sizeof(temp), fd) != NULL){
                    
            /*Passa a string lida para a estrutura de strings*/
            str_concat_chararr(str, temp, strlen(temp)-1);
			str_concat_chararr(str, "\n", sizeof(char));
            i++;
        }
        fclose(fd);
    }
    return str->s; 
}
예제 #3
0
파일: main.c 프로젝트: cglinden/autocook
int
main(int argc, char **argv)
{
    string_ty       *s;
    string_list_ty  sl;
    size_t          j;

    arglex_init(argc, argv, argtab);
    str_initialize();
    switch (arglex())
    {
    case arglex_token_help:
        help((char *)0, usage);
        exit(0);

    case arglex_token_version:
        version();
        exit(0);

    default:
        break;
    }

    string_list_constructor(&sl);
    while (arglex_token != arglex_token_eoln)
    {
        switch (arglex_token)
        {
        default:
            generic_argument(usage);
            continue;

        case arglex_token_warning:
            warning++;
            break;

        case arglex_token_string:
            s = str_from_c(arglex_value.alv_string);
            string_list_append_unique(&sl, s);
            str_free(s);
            break;
        }
        arglex();
    }

    if (!sl.nstrings)
    {
        error_intl(0, i18n("no files named"));
        usage();
    }

    for (j = 0; j < sl.nstrings; ++j)
        file_check(sl.string[j]);
    exit(0);
    return 0;
}
예제 #4
0
파일: main.c 프로젝트: cglinden/autocook
int
main(int argc, char **argv)
{
    int             retval;

    /*
     * Some versions of cron(8) and at(1) set SIGCHLD to SIG_IGN.
     * This is kinda dumb, because it breaks assumprions made in
     * libc (like pclose, for instance).  It also blows away most
     * of Cook's process handling.  We explicitly set the SIGCHLD
     * signal handling to SIG_DFL to make sure this signal does what
     * we expect no matter how we are invoked.
     */
#ifdef SIGCHLD
    signal(SIGCHLD, SIG_DFL);
#else
    signal(SIGCLD, SIG_DFL);
#endif

    /*
     * initialize things
     * (order is critical here)
     */
    progname_set(argv[0]);
    str_initialize();
    id_initialize();
    lex_initialize();

    /*
     * parse the COOK environment variable
     */
    arglex_init_from_env(argv[0], argtab);
    argparse(OPTION_LEVEL_ENVIRONMENT);

    /*
     * parse the command line
     */
    arglex_init(argc, argv, argtab);
    argparse(OPTION_LEVEL_COMMAND_LINE);

    option_tidy_up();

    log_open();

    /*
     * turn on progress stars if they asked for them
     */
    if (option_test(OPTION_STAR))
        star_enable();

    /*
     * If we were asked to update the fingerprints, do it here.
     * We don't actually ant to read in the cookbook.
     */
    if (option.fingerprint_update)
    {
        fp_tweak();
        quit(0);
    }

    /*
     * read in the cook book
     *
     * If there are #include-cooked directives,
     * we may need to do it more than once.
     */
    if (!option.o_book)
        fatal_intl(0, i18n("no book found"));
    for (;;)
    {
        int             status;
        size_t          j;

        builtin_initialize();

        /*
         * instanciate the command line variable assignments
         */
        for (j = 0; j < option.o_vardef.nstrings; ++j)
        {
            char            *s;
            char            *cp;
            string_ty       *name;
            string_ty       *value;
            string_list_ty  wl;
            opcode_context_ty *ocp;

            s = option.o_vardef.string[j]->str_text;
            cp = strchr(s, '=');
            assert(cp);
            if (!cp)
                continue;
            name = str_n_from_c(s, cp - s);
            value = str_from_c(cp + 1);
            str2wl(&wl, value, (char *)0, 0);
            str_free(value);
            ocp = opcode_context_new(0, 0);
            opcode_context_id_assign(ocp, name, id_variable_new(&wl), 0);
            opcode_context_delete(ocp);
            str_free(name);
            string_list_destructor(&wl);
        }

        set_command_line_goals();

        parse(option.o_book);
        status = cook_auto_required();
        if (status < 0)
            quit(1);
        if (!status)
            break;
        id_reset();
        cook_reset();
    }

    /*
     * work out what to cook.
     * If no targets have been given, use the first explicit recipe.
     */
    set_command_line_goals();
    if (!option.o_target.nstrings)
        cook_find_default(&option.o_target);
    assert(option.o_target.nstrings);

    /*
     * cook the target
     */
    if (option.pairs)
        retval = cook_pairs(&option.o_target);
    else if (option.script)
        retval = cook_script(&option.o_target);
    else if (option.web)
        retval = cook_web(&option.o_target);
    else
        retval = cook(&option.o_target);

#ifdef DEBUG
    fflush_slowly_report();
#endif

    quit(retval);
    /*NOTREACHED*/
    return 0;
}
예제 #5
0
/* ----------------------------------------------------------------------------*/
char* infoDisciplinas(){
    
    FILE *fd, *nomefd;
    char *texto;
    string str;
    char nomedisc[200];

    nomefd = fopen("codigo_disciplinas.txt", "r");
    if(nomefd == NULL){
        printf("Erro ao abrir o arquivo com nome das disciplinas.\n");
        exit(-1);
    }
    else{

            /* Inicializa um vetor contendo a resposta */
            str = str_initialize(((size_t)NUM_MAX_DISCIPLINAS * TAM_MAX_NOME_DISCIPLINA));
            while(fgets(nomedisc, sizeof(nomedisc), nomefd) !=NULL){
                /* Normaliza e obtem o nome do arquivo correspondente
                * a disciplina passada como parametro*/
                uppercase(nomedisc);
                texto = strndup(nomedisc, 5);
                strcat(texto, ".txt");

                fd = fopen(texto, "r");
                /* Verifica se a abertura do arquivo foi bem sucedida */
                if(fd == NULL){
                    printf("Erro ao abrir o arquivo da disciplina %s.\n", nomedisc);
                    exit(-1);
                }
                else{
                    char tmp[200];
                    char *pch;

                    /* Le o titulo da disciplina */
                    fgets(tmp, sizeof(tmp), fd);
                    str_concat_chararr(str, tmp, sizeof(tmp) -1);

                    /* Le o resto do arquivo */
                    while(fgets(tmp, sizeof(tmp), fd)!=NULL){
                        /* Separa a linha lida */
                        pch = strtok(tmp, " ");
                        /* Imprime a linha lida */
                        while(pch !=NULL){
                            str_concat_chararr(str, pch, strlen(pch));
                            str_concat_chararr(str, " ", sizeof(char));
                            pch = strtok(NULL, " ");
                
                
                        }
            

                    }
                    str_concat_chararr(str, "\n", sizeof(char));
                    fclose(fd);
            }

        }
            return str->s;

    }
}
예제 #6
0
/* ----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
	int sockfd, numbytes;  
	char buf[MAXDATASIZE];
	struct addrinfo hints, *servinfo, *p;
	int rv;
	char s[INET6_ADDRSTRLEN];

    struct timeval startTime; //Estrutura contendo o tempo de início de execução
    struct timeval endTime; //Estrutura contendo o tempo final de execução
    double time;
    string parametro;//Parametro a ser enviado para o servidor
    char *resposta; //Resposta do servidor

	if (argc < 3 || (argc !=5 && argv[2][0] == 'w') || (argc == 3 && argv[2][0] == 'p')) {
	    printUsage();
        return 0;
	}

	memset(&hints, 0, sizeof hints);
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	if ((rv = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
	}

	// loop through all the results and connect to the first we can
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if ((sockfd = socket(p->ai_family, p->ai_socktype,
				p->ai_protocol)) == -1) {
			perror("client: socket");
			continue;
		}

		if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close(sockfd);
			perror("client: connect");
			continue;
		}

		break;
	}

	if (p == NULL) {
		fprintf(stderr, "client: failed to connect\n");
		return 2;
	}

	inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
			s, sizeof s);
	printf("client: connecting to %s\n", s);

	freeaddrinfo(servinfo); // all done with this structure

    //Inicia a contagem de tempo, antes do write (send)
    gettimeofday(&startTime, NULL);

    //Inicializa a string a ser enviada ao servidor
    parametro = str_initialize(((size_t)MAXDATASIZE));
    //Funções envolvendo disciplinas específicas
    if(argc == 4){

        str_concat_chararr(parametro, argv[2], strlen(argv[2]));
        str_concat_chararr(parametro, argv[3], strlen(argv[3])); 
    }
    //Escrever comentário
    else if(argc == 5){
        str_concat_chararr(parametro, argv[2], strlen(argv[2]));
        str_concat_chararr(parametro, argv[3], strlen(argv[3]));
        str_concat_chararr(parametro, argv[4], strlen(argv[4]));
    }
    else{

        str_concat_chararr(parametro, argv[2], strlen(argv[2]));
    }
	if (send(sockfd, parametro->s, str_cur_size(parametro), 0) == -1){
		perror("send");
		close(sockfd);
		exit(0);
	}
    
    //Libera a string de parametros
    str_free(parametro);
    //Imprime a resposta na tela, recebendo os dados do socket
    while(1){
        if((numbytes = recv(sockfd, buf, MAXDATASIZE-1,0)) >0){
            resposta = strndup(buf, numbytes);
            printf("%s", resposta);
        }
        else if(numbytes <0 && errno != EINTR){
            perror("recv");
            close(sockfd);
            exit(1);
        }
        else if(numbytes == 0){
            break;
        }
    }				
    //Termina a contagem de tempo, após o read (recv)
    gettimeofday(&endTime, NULL);

    time = calculaTempo(startTime, endTime);
   
    printf("\nTotal time: %.2lf microseconds\n", time);

	close(sockfd);

	return 0;
}