Exemplo n.º 1
0
int main(){
	
	int len; /* current line length */
	char string[LINE_LENGTH];/*current line*/

	/* while there's some input */
	while( ( len = get_line(string, LINE_LENGTH) ) > 0 ){
		#if defined(DEBUG)
			printf("In Main While loop\n");
			printf("Original string length: %d\n", len);
		#endif
		
		len = remove_blanks(string, len);
	
	#if defined(DEBUG)
		printf("String length after removing blanks: %d\n", len);
	#endif
		/* only print the string if it contains an actual character
		 * not just a newline */
		if(len > 1){
			printf("%s",string);
		}
	
	}
	
	return 0;
}
Exemplo n.º 2
0
Arquivo: foo.c Projeto: leolas95/c
int main(void)
{
        char line[MAXLN];
        int len;
        int i;
        int num_blanks = 0, num_tabs = 0;


        /* ARREGLAR: No imprime [BLANK LINE] */
        while ((len = getline(line, MAXLN)) >= 0) {
                if (len == 1 && line[0] == '\n') {
                        printf("[BLANK LINE]\n");
                        continue;
                }
                for (i = 0; i < len; ++i) {
                        if (line[i] == ' ') {
                                num_blanks++;
                        } else if (line[i] == '\t') {
                                num_tabs++;
                        }
                }
                while (num_blanks > 0) {
                        remove_blanks(line, len);
                        num_blanks--;
                }
                while (num_tabs > 0) {
                        remove_tabs(line, len);
                        num_tabs--;
                }
                printf("%s\n\n", line);
        }
        return 0;
}
Exemplo n.º 3
0
int main(){
    int len, max=0;
    char line[MAX_LEN];
    while((len = get_line(line,MAX_LEN)) > 0){
        remove_blanks(line);
        printf("Line is: %s", line);
    }
    return 0;
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: osgh/clang
int main()
{

	printf("\n");

	/* count string blanks */
	printf("\"hello world\" has %d blank(s)\n",count_blanks("hello world"));

	
	printf("\n");
	

	/* remove blanks of string */
	char str[50] = "h ell o wo rl d!"; 
	remove_blanks(str,50);
	printf("\"h ell o wo rl d!\" remove blanks is \"%s\"\n",str);


	printf("\n");


	/* count ones of integer number */
	int ones = 0xfffffff0;
	printf("ones of %d total are %d\n", ones, int_ones_count(ones));


	printf("\n");


	/* reverse array of integer numbers */
	int arr[] = {1,2,3,4,5,6,7,8,9};
	int len = sizeof(arr)/sizeof(arr[0]);

	array_reverse(arr, len);
	
	printf("1 2 3 4 5 6 7 8 9 reversed to ");
	int i;
	for(i = 0; i < len; i++){
		printf("%d ",arr[i]);
	}

	printf("\n");

	return 0;
}
int main (int argc, char *argv[]) {

	int len;
	char line[MAXLINE];

	for (int i = 0; i < MAXLINE; i++) {
		line[i] = 0;
	}
	
	while ((len = get_line(line, MAXLINE)) > 0) {

		len = remove_blanks(line, len);
		printf("%s\n", line);
		printf("%d\n", len);
	}

	return 0;
}
Exemplo n.º 6
0
int main()
{
  char line[MAXLINE];
  int len = 0;
  int print_len = 0;
  int i = 0;

  while ((len = get_line(line, MAXLINE)) > 0) {
    print_len = remove_blanks(line, len);
    if (print_len > 0) {
      for (i = 0; i < print_len; i++) {
        putchar(line[i]);
      }
      putchar('\n');
    }
  }

  return 0;
}
Exemplo n.º 7
0
void dhcp_edit_read_dhcpd(int red)
{
	int reading = 1, i;		/* reading */
	char *s;
	char line[MAX_LEN];
	FILE *f;

	f = fopen(redes[red].file, "r");
	if (f == NULL)
		goto read_dhcpd_error;

	i = 0;
	while (reading) {
		s = fgets(line, MAX_LEN, f);
		if (s == NULL) {
			reading = 0;
		} else {

			s = remove_blanks(s);

			if (is_comment(s) || is_empty(s))
				continue;

			if (is_end_of_block(s))
				i++;
			else
				dhcp_edit_parse(s, i);
		}
	}

	fclose(f);
	return;

read_dhcpd_error:
	printf("Problema al leer %s\n", redes[red].file);
	exit(1);
}
Exemplo n.º 8
0
int		brtopc(t_env* env)
{
  int		fd; /* fd of a temporary file */
  unsigned int		lines = 0; /* number of packets transfered */
  unsigned char destext = 0;  /* conversion of the destination file */
  unsigned char		buf[BUFFER_SIZE]; /* received buffer */
  unsigned char	str[BUFFER_SIZE];  /* sent buffer */
  unsigned char ext = extensions[env->curext];  /* current extension */
  unsigned char filename[15]; /* The transfered file name */
  unsigned char filename1[15]; /* The filename without extension */

  while (1)
    {
      /* Asking name of current selected file */
      strcpy((char*)buf, "\005FN");
      buf[3] = ext;
      buf[4] = (env->brfilenum & 0xff00) >> 2;
      buf[5] = env->brfilenum & 0x00ff;
      brl_write(buf+1, 5);
      brl_read(buf);
      strcpy((char*)str, "Conv ");
      strncat((char*)str, ((char*)&(buf[6])), buf[0] - 5);
      filename[0] = 0;
      strncat((char*)filename, ((char*)&(buf[6])), buf[0] - 5);
      strcat((char*)str, ".");
      remove_blanks(filename);
      strcpy((char*)filename1, (char*)filename);
      strcat((char*)filename, ".");
      if (destext == 0) {
	strncat((char*)str, ((char*)&(extensions[env->curext])), 1);
	strncat((char*)filename, ((char*)&(extensions[env->curext])), 1);
      }
      else {
	strcat((char*)str, "TXT");
	strcat((char*)filename, "TXT");
      }
      brl_message((char*)str, 0);
      brl_read(buf);
      if (!strncmp((char*)buf, "\003KT*", 4))
	return 1;
      if (!strncmp((char*)buf, "\003KT#", 4))
	break;
      if (!strncmp((char*)buf, "\003KT8", 4))
	destext = !destext;
      if (!strncmp((char*)buf, "\003KT2", 4))
	destext = !destext;
    }
  strcpy((char*)buf, "\005FO");
  buf[3] = ext;
  buf[4] = (env->brfilenum & 0xff00) >> 2;
  buf[5] = env->brfilenum & 0x00ff;
  brl_write(buf+1, 5);
  brl_read(buf);
  if (strncmp((char*)buf, "\003FE\x10", 4))
    {
      brl_lasting_message("Erreur ouv br");
      return 1;
    }
  if ((fd = open((char*)filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1)
    {
      perror("open");
      brl_message("! Err ecriture PC", 0);
      sleep(1);
      return 1;
    }
  while (1)
    {
      brl_writeStr(READ_LINE);
      brl_read(buf);
      if (!strncmp((char*)buf, "\003KT", 3))
	{
	  printf("touche appuyée\n");
	  if (!strncmp((char*)buf, "\003KT*", 4))
	    {
	      printf("touche * appuyée\n");
	      brl_lasting_message("! interrompu ");
	      return 1;
	    }
	}
      if (!strncmp((char*)buf, "\003FE", 3))
	{
	  if (buf[3] == '\x13')
	      break;
	  else
	    {
	      printf("code inattendu\n");
	      brl_lasting_message("! transfert interrompu");
	      return 1;
	    }
	}
      sprintf((char*)str, "... %s %d", filename, lines);
      brl_message((char*)str, 0);
      write(fd, &(buf[3]), buf[0] - 2);
      lines++;
    }
  brl_writeStr(CLOSE_FILE);
  brl_read(buf);
  if (strncmp((char*)buf, "\003FE\x10", 4))
    {
      brl_lasting_message("! erreur");
      return 1;
    }
  printf("fichier fermé\n");
  close(fd);
  if (destext)
    {
      strcat((char*)filename1, ".TXT");
      if (!k2txt(env, (char*)filename, (char*)filename1))
	{
	  brl_lasting_message("! Erreur conversion");
	  return 1;
	}
    }
  brl_lasting_message("! Fin transfert");
  return 1;
}
Exemplo n.º 9
0
VL_EXPORT int
vl_pgm_extract_head (FILE* f, VlPgmImage *im)
{
  char magic [2] ;
  int c ;
  int is_raw ;
  int width ;
  int height ;
  int max_value ;
  size_t sz ;
  vl_bool  good ;

  VlThreadSpecificState * threadState = vl_get_thread_specific_state() ;

  /* -----------------------------------------------------------------
   *                                                check magic number
   * -------------------------------------------------------------- */
  sz = fread(magic, 1, 2, f) ;

  if (sz < 2) {
    threadState->lastError  = VL_ERR_PGM_INV_HEAD ;
    return -1 ;
  }

  good = magic [0] == 'P' ;

  switch (magic [1]) {
  case '2' : /* ASCII format */
    is_raw = 0 ;
    break ;

  case '5' : /* RAW format */
    is_raw = 1 ;
    break ;

  default :
    good = 0 ;
    break ;
  }

  if( ! good ) {
    return (threadState->lastError= VL_ERR_PGM_INV_HEAD) ;
  }

  /* -----------------------------------------------------------------
   *                                    parse width, height, max_value
   * -------------------------------------------------------------- */
  good = 1 ;

  c = remove_blanks(f) ;
  good &= c > 0 ;

  c = fscanf(f, "%d", &width) ;
  good &= c == 1 ;

  c = remove_blanks(f) ;
  good &= c > 0 ;

  c = fscanf(f, "%d", &height) ;
  good &= c == 1 ;

  c = remove_blanks(f) ;
  good &= c > 0 ;

  c = fscanf(f, "%d", &max_value) ;
  good &= c == 1 ;

  /* must end with a single blank */
  c = fgetc(f) ;
  good &=
    c == '\n' ||
    c == '\t' ||
    c == ' '  ||
    c == '\r' ;

  if(! good) {
    return (threadState->lastError = VL_ERR_PGM_INV_META) ;
  }

  if(! max_value >= 65536) {
    return (threadState->lastError = VL_ERR_PGM_INV_META) ;
  }

  /* exit */
  im-> width     = width ;
  im-> height    = height ;
  im-> max_value = max_value ;
  im-> is_raw    = is_raw ;
  return 0 ;
}
Exemplo n.º 10
0
int
parse_cmd(char *cmd_str, long int **cmd, int *cmd_len)
    /* read a command string, check if it is valid and translate it */
{
    long int buf[SOCKET_MSG_LEN];
    int word_size = 0;
    int i = 0, j = 0, rank = -1;
    long int int_buf = 0;
    struct passwd *pass = NULL;
#ifdef SYSFCRONTAB
    long int sysfcrontab_uid = SYSFCRONTAB_UID;
#endif

    bzero(buf, sizeof(buf));
    *cmd_len = 0;
    remove_blanks(cmd_str);     /* at the end of the string */

    if ((word_size = get_word(&cmd_str)) == 0) {
        fprintf(stderr, "Warning : Zero-length command name : line ignored.\n");
        return ZEROLEN_CMD;
    }

    for (i = 0; i < cmd_list_len; i++) {
        int j;
        if (Strncmp(cmd_str, cmd_list[i].cmd_name, word_size) == 0) {
            rank = i;
            break;
        }
        for (j = 0; j < MAX_NUM_ALIAS && cmd_list[i].cmd_alias[j] != NULL; j++) {
            if (Strncmp(cmd_str, cmd_list[i].cmd_alias[j], word_size) == 0) {
                rank = i;
                break;
            }
        }
    }
    if (rank == (-1)) {
        fprintf(stderr, "Error : Unknown command.\n");
        return CMD_NOT_FOUND;
    }
    else if (cmd_list[rank].cmd_code == QUIT_CMD) {
        if (debug_opt)
            fprintf(stderr, "quit command\n");
        return QUIT_CMD;
    }
    else if (cmd_list[rank].cmd_code == HELP_CMD) {
        if (debug_opt)
            fprintf(stderr, "Help command\n");
        return HELP_CMD;
    }

    Write_cmd(cmd_list[rank].cmd_code);

    if (debug_opt)
        fprintf(stderr, "command : %s\n", cmd_list[i].cmd_name);

    cmd_str += word_size;
    for (i = 0; i < cmd_list[rank].cmd_numopt; i++) {

        if ((word_size = get_word(&cmd_str)) == 0) {

            if (cmd_list[rank].cmd_default[i] == ARG_REQUIRED) {
                fprintf(stderr, "Error : arg required !\n");
                return INVALID_ARG;
            }

            /* use default value : currently, works only with CUR_USER */
            if (user_uid == rootuid) {
                /* default for root = all */
                int_buf = ALL;
                Write_cmd(int_buf);
                if (debug_opt)
                    fprintf(stderr, "  uid = ALL\n");
            }
            else {
                Write_cmd(user_uid);
                if (debug_opt)
                    fprintf(stderr, "  uid = %d\n", (int)user_uid);
            }

        }

        else {

            /* get value from line ... */
            switch (cmd_list[rank].cmd_opt[i]) {

            case USER:
                int_buf = (long int)*(cmd_str + word_size);
                *(cmd_str + word_size) = '\0';
#ifdef SYSFCRONTAB
                if (strcmp(cmd_str, SYSFCRONTAB) == 0) {
                    Write_cmd(sysfcrontab_uid);
                }
                else {
#endif
                    if ((pass = getpwnam(cmd_str)) == NULL) {
                        fprintf(stderr,
                                "Error : '%s' isn't a valid username.\n",
                                cmd_str);
                        return INVALID_ARG;
                    }
                    Write_cmd(pass->pw_uid);
#ifdef SYSFCRONTAB
                }
#endif
                *(cmd_str + word_size) = (char)int_buf;
                cmd_str += word_size;
                if (debug_opt)
                    fprintf(stderr, "  uid = %d\n",
#ifdef SYSFCRONTAB
                            (pass) ? (int)pass->pw_uid : (int)SYSFCRONTAB_UID
#else
                            (int)pass->pw_uid
#endif
                        );
                break;

            case JOBID:
                /* after strtol(), cmd_str will be updated (first non-number char) */
                if ((int_buf = strtol(cmd_str, &cmd_str, 10)) < 0
                    || int_buf >= LONG_MAX || (!isspace((int)*cmd_str)
                                               && *cmd_str != '\0')) {
                    fprintf(stderr, "Error : invalid jobid.\n");
                    return INVALID_ARG;
                }
                Write_cmd(int_buf);
                if (debug_opt)
                    fprintf(stderr, "  jobid = %ld\n", int_buf);
                break;

            case TIME_AND_DATE:
                /* argghh !!! no standard function ! */
                break;

            case NICE_VALUE:
                /* after strtol(), cmd_str will be updated (first non-number char) */
                if ((int_buf = strtol(cmd_str, &cmd_str, 10)) > 20
                    || (int_buf < 0 && getuid() != rootuid) || int_buf < -20
                    || (!isspace((int)*cmd_str) && *cmd_str != '\0')) {
                    fprintf(stderr, "Error : invalid nice value.\n");
                    return INVALID_ARG;
                }
                Write_cmd(int_buf);
                if (debug_opt)
                    fprintf(stderr, "  nicevalue = %ld\n", int_buf);
                break;

            case SIGNAL:
                if (isalpha((int)*cmd_str)) {
                    for (j = 0; j < word_size; j++)
                        *(cmd_str + j) = tolower(*(cmd_str + j));
                    if (Strncmp(cmd_str, "hup", word_size) == 0)
                        int_buf = SIGHUP;
                    else if (Strncmp(cmd_str, "int", word_size) == 0)
                        int_buf = SIGINT;
                    else if (Strncmp(cmd_str, "quit", word_size) == 0)
                        int_buf = SIGQUIT;
                    else if (Strncmp(cmd_str, "kill", word_size) == 0)
                        int_buf = SIGKILL;
                    else if (Strncmp(cmd_str, "alrm", word_size) == 0)
                        int_buf = SIGALRM;
                    else if (Strncmp(cmd_str, "term", word_size) == 0)
                        int_buf = SIGTERM;
                    else if (Strncmp(cmd_str, "usr1", word_size) == 0)
                        int_buf = SIGUSR1;
                    else if (Strncmp(cmd_str, "usr2", word_size) == 0)
                        int_buf = SIGUSR2;
                    else if (Strncmp(cmd_str, "cont", word_size) == 0)
                        int_buf = SIGCONT;
                    else if (Strncmp(cmd_str, "stop", word_size) == 0)
                        int_buf = SIGSTOP;
                    else if (Strncmp(cmd_str, "tstp", word_size) == 0)
                        int_buf = SIGTSTP;
                    else {
                        fprintf(stderr,
                                "Error : unknow signal (try integer value)\n");
                        return INVALID_ARG;
                    }
                    cmd_str += word_size;
                }
                /* after strtol(), cmd_str will be updated (first non-number char) */
                else if ((int_buf = strtol(cmd_str, &cmd_str, 10)) <= 0
                         || int_buf >= LONG_MAX || (!isspace((int)*cmd_str)
                                                    && *cmd_str != '\0')) {
                    fprintf(stderr, "Error : invalid signal value.\n");
                    return INVALID_ARG;
                }
                Write_cmd(int_buf);
                if (debug_opt)
                    fprintf(stderr, "  signal = %ld\n", int_buf);
                break;

            default:
                fprintf(stderr, "Error : Unknown arg !");
                return INVALID_ARG;
            }
        }
    }

    Skip_blanks(cmd_str);
    if (*cmd_str != '\0')
        fprintf(stderr, "Warning : too much arguments : '%s' ignored.\n",
                cmd_str);

    /* This is a valid command ... */
    *cmd = alloc_safe(*cmd_len * sizeof(long int), "command string");
    memcpy(*cmd, buf, *cmd_len * sizeof(long int));

    return OK;
}