Ejemplo n.º 1
0
int		force_file_fd(char *path, int (*f)(char *))
{
    int		fd;
    char		*print;
    char		*tmp;

    if ((fd = open(path, O_DIRECTORY, S_IRUSR)) > 0)
        return (error_file(fd, my_strncat(path, " is a folder", 12), f));
    if ((fd = open(path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) > 0)
        return (fd);
    tmp = my_strncat("can't access to ", path, my_strlen(path));
    if (errno == EACCES)
        print = my_strncat(tmp, ": permission denied", 19);
    else if  (errno == ENOENT)
        print = my_strncat(tmp, ": file not found", 16);
    else if  (errno == ENAMETOOLONG)
        print = my_strncat(tmp, ": too long pathname", 19);
    else
        print = tmp;
    if (print != tmp)
        free(tmp);
    (*f)(print);
    free(print);
    return (-1);
}
Ejemplo n.º 2
0
int		my_file_fd(char *path, int (*f)(char *), int flag)
{
    int		fd;
    char		*print;
    char		*tmp;

    if ((fd = open(path, O_DIRECTORY, S_IRUSR)) > 0)
        return (error_file(fd, my_strncat(path, " is a folder", 12), f));
    if ((fd = open(path, flag, S_IWUSR)) > 0)
        return (fd);
    tmp = my_strncat("can't access to ", path, my_strlen(path));
    if (errno == EACCES)
        print = my_strncat(tmp, " permission denied", 18);
    else if  (errno == ENOENT)
        print = my_strncat(tmp, " file not found", 15);
    else if  (errno == ENAMETOOLONG)
        print = my_strncat(tmp, " too long pathname", 18);
    else
        print = tmp;
    if (print != tmp)
        free(tmp);
    (*f)(print);
    free(print);
    return (-1);
}
Ejemplo n.º 3
0
void		gsend_to_server(t_data *data, char *msg)
{
  char		*str;

  if ((str = malloc(sizeof(char) * (my_strlen(msg) + 23))) == NULL)
    return ;
  str[0] = 0;
  str = my_strncpy(str, data->player[0].name, my_strlen(data->player[0].name));
  str[my_strlen(data->player[0].name)] = 0;
  str = my_strncat(str, ": \0", 3);
  str = my_strncat(str, msg, my_strlen(msg));
  str = my_strncat(str, "\0", 1);
  write(data->sock.sock, str, my_strlen(str));
  free(str);
}
Ejemplo n.º 4
0
int main()
{
	char before1[] = "________";
	char after1[] = "--------";
	printf("strncopy 3\n");
	printf("\t before:\t %s\n", before1);
	printf("\t after: \t %s\n", after1);
	my_strncopy(before1, after1, 3);
	printf("\t\t*poof*\n");
	printf("\t before:\t %s\n\n", before1);

	char before2[] = "________";
	char after2[] = "--------";
	printf("strncat 3\n");
	printf("\t before:\t %s\n", before2);
	printf("\t after: \t %s\n", after2);
	my_strncat(before2, after2, 3);
	printf("\t\t*poof*\n");
	printf("\t before:\t %s\n\n", before2);

	
	char first[] = "star wars";
	char second[] = "star trek";

	printf("Do the first %i letters of '%s' match '%s'?\n", 4, first, second);
	int end = my_strncmp(first, second, 4);
	end == 0 ? printf("\tyes\n\n") : printf("\tno\n\n");

	printf("Do the first %i letters of '%s' match '%s'?\n", 6, first, second);
	end = my_strncmp(first, second, 6);
	end == 0 ? printf("\t yes\n\n") : printf("\t no\n\n");
}
Ejemplo n.º 5
0
static int      check_path(char **tab, t_shell *shell)
{
  char          *path;
  char          sub_path[PWD_BUFFER_SIZE];
  int           index;

  path = get_env_value(ENV_PATH, shell->env);
  if (!path || !my_strlen(path))
    return (0);
  index = 0;
  do
  {
    get_sub_path(path, sub_path, &index);
    my_strncat(sub_path, tab[0], PWD_BUFFER_SIZE);
    if (access(sub_path, F_OK | X_OK | R_OK) == 0)
    {
      exec(tab, NULL, sub_path);
      return (1);
    }
    else if (errno != ENOENT)
    {
      print_errno(sub_path);
      return (1);
    }
  } while (index < my_strlen(path));
  return (0);
}
Ejemplo n.º 6
0
void	*traitement(t_get *g, const int fd)
{
  while (g->buff[g->i] != '\n' && g->buff[g->i] != 0)
    if (g->j + 1 == g->len || (g->buff[g->i + 1] == '\n'
			       || g->buff[g->i + 1] < 32))
      {
    	while (g->str[g->count++]);
    	if (g->str)
    	  CHECK_RETURN(g->str = my_realloc(g->str, (g->count + g->i + 1)),
		       NULL, NULL);
	my_strncat(g->str, g->buff, g->i + 1);
	tronque(g->buff, g->i + 1, g->len);
    	++(g->j);
    	g->i = 0;
    	if (g->buff[g->i] == '\n' || g->buff[g->i] < 32)
    	  if (g->buff[g->i] == '\0')
    	    {
    	      if ((g->len = read(fd, g->buff, READ)) == 0)
    		return (g->str);
    	      g->j = 0;
    	    }
      }
    else
      {
    	++(g->j);
    	++(g->i);
      }
}
Ejemplo n.º 7
0
int main(void)
{
	char * A = "Hello!";
	char * B = "Hello from Chicago";
	char * C = " Excellent. Perfect.";

	char result[1024] = "Hi ";

	printf("Test 1: ");
	printf("%s\n", my_strcat(result, A));
	   
	   printf("Test 2: ");
	   printf("%s\n", my_strncpy(result, B, 2));

	   printf("Test 3: ");
	   printf("%s\n", my_strncpy(result, B, 100));

	   printf("Test 4: ");
	   printf("%s\n", my_strncat(result, C, 10));

	   printf("Test 5: ");
	   printf("%d\n", my_strncmp(A, B, 5));

	   printf("Test 6: ");
	   printf("%d\n", my_strncmp(A, B, 10));

	return 0;
}
Ejemplo n.º 8
0
t_uchar		run_setenv(char ***env, char **argv)
{
  char		*tmp;

  if (count_args(argv) < 2)
    {
      my_printenv(*env, '\n');
      return (0);
    }
  else if (count_args(argv) > 3 || !my_str_isalpha(argv[1]) ||
	   (!my_char_islower(argv[1][0]) && !my_char_islower(argv[1][0])))
    {
      if (count_args(argv) > 3)
	my_dprintf(STDERR, "setenv: Too many arguments.\n");
      else if ((!my_char_islower(argv[1][0]) && !my_char_islower(argv[1][0])))
	my_dprintf(STDERR, "setenv: Variable name must begin with a letter.\n");
      else if (!my_str_isalpha(argv[1]))
	my_dprintf(STDERR, ERALPH);
      return (1);
    }
  if ((tmp = malloc(sizeof(char) * (my_strlen(argv[1]) + 2))) == NULL)
    my_exit(EXIT_FAILURE, "ERROR: Out of memory! malloc() failed\n");
  tmp = my_strncpy(tmp, argv[1], my_strlen(argv[1]));
  tmp = my_strncat(tmp, "=", 1);
  my_setenv(env, tmp, argv[2]);
  free(tmp);
  return (0);
}
Ejemplo n.º 9
0
Archivo: 5.c Proyecto: leicj/books
int main(void)
{
    char dst[20];
    char str1[] = "hello ";
    char str2[] = "world.";
    char str3[] = "i love this world";

    my_strncat(dst, str1, 20);
    printf("%s\n", dst);
    my_strncat(dst, str2, 20);
    printf("%s\n", dst);
    my_strncat(dst, str3, 20);
    printf("%s\n", dst);

    return 0;
}
Ejemplo n.º 10
0
char *my_strcat2ptr(MyString *mstr, const char *str, const char *endp) {
	size_t cat_len = endp - str - 1;

	if (cat_len > 0) {
		my_strncat(mstr, str, cat_len);
	}

	return (char *)mstr->str;
}
Ejemplo n.º 11
0
char		*get_exec_full_path(char *input, char **path)
{
  char		*exec_dir;
  char		*exec_path;

  exec_dir = get_exec_dir(input, path);
  if (exec_dir != NULL)
    {
      exec_path = malloc(sizeof(char) * (my_strlen(exec_dir)
					 + my_strlen(input) + 2));
      if (exec_path == NULL)
	my_exit(EXIT_FAILURE, "ERROR: Out of memory! malloc() failed\n");
      exec_path = my_strncpy(exec_path, exec_dir, my_strlen(exec_dir));
      exec_path = my_strncat(exec_path, "/", 1);
      exec_path = my_strncat(exec_path, input, my_strlen(input));
      return (exec_path);
    }
  else
    return (my_strdup(input));
}
Ejemplo n.º 12
0
int			main(int ac, char **av)
{
  char			*ret;
  char			*ret2;

  ret = my_strncat(av[1], av[2], 3);
  ret2 = strncat(av[1], av[2], 3);
  my_printf("%s\n", ret);
  my_printf("%s\n", ret2);
  return (0);
}
Ejemplo n.º 13
0
char	*my_strdup(char *str)
{
  char	*newstr;

  newstr = malloc(sizeof(*newstr) * my_strlen(str) + 1);
  if (newstr == NULL)
    {
      return (NULL);
    }
  my_strcpy(newstr, str);
  my_strncat(newstr, "\0", 1);
  return (newstr);
}
int main(int argc, char *argv[]){

  char *a = "hello world";
  char *b = " yes";
  char c[50] = "\0";

  my_strcat(c, a);
  printf("%s\n", c); 
  my_strncat(c, b, 2);
  printf("%s\n", c); 

  return 0; 
}
Ejemplo n.º 15
0
Archivo: strncat.c Proyecto: kevinng/kr
int main(int argc, char const *argv[])
{
	char s1[12] = "hello*";
	char s2[] = "world";

	printf("s1:%s::\n", s1);
	printf("s2:%s::\n", s2);

	char *s3 = my_strncat(s1, s2, 3);

	printf("s3:%s::\n", s3);
	
	return 0;
}
Ejemplo n.º 16
0
char	*my_realloc(char *str, int size)
{
  char	*result;
  int	len;

  if (str)
    result = str;
  len = my_strlen(result);
  if ((str = malloc(sizeof(char) * (size + 1))) == NULL)
    return (NULL);
  str[0] = 0;
  my_strncat(str, result, len);
  str[size] = 0;
  return (str);
}
Ejemplo n.º 17
0
Archivo: main.c Proyecto: Wobow/Tek1
int		check_case(t_a *t, t_l *cur, int i)
{
  int		j;

  j = 0;
  while (j < t->nb_col)
    {
      if (CUR_CASE != ' ' && CUR_CASE != '|')
	return (1);
      if (CUR_CASE == '|')
	cur->nb_alum++;
      cur->content = my_strncat(cur->content, &CUR_CASE, 1);
      cur->selected = 0;
      j++;
    }
  return (0);
}
Ejemplo n.º 18
0
int main(void)
{
    int len = 20;
    char first[20] = { "" };
    char second[20] = { "" };

    puts("\n\tSay something nice:");
    fgets(first, 100, stdin);
    puts("\n\tAgain please:");
    fgets(second, 100, stdin);

    puts("Cool! So you've said..:\n\tConcatenated..\n");
    my_strncat(first, second, len);
    putchar('\n');

    return 0;
}
Ejemplo n.º 19
0
void		notify_all(t_socket *sock, char *pseudo, int todo)
{
  char		tmp[24];

  tmp[0] = 0;
  if (todo == 1)
    my_strncpy(tmp, "/a ", 3);
  else if (todo == 2)
    my_strncpy(tmp, "/r ", 3);
  else
    my_strncpy(tmp, "/s ", 3);
  tmp[3] = '\0';
  if (pseudo != NULL)
    {
      my_strncat(tmp, pseudo, my_strlen(pseudo));
      socket_send_msg_all(sock, tmp);
    }
}
Ejemplo n.º 20
0
int	param_direct_4(char *save, int i, int fd)
{
  int	k;
  char	*dest;
  
  k = 0;
  write(fd, " ", 1);
  while (k < (DIR_SIZE - 1))
    {
      write(fd, "0x00 ", 5);
      k++;
    }
  i += my_strlen(&save[i], SEPARATOR_CHAR);
  dest = my_strncat(&save[i + 1], my_strlen(&save[i + 1], SEPARATOR_CHAR));
  write(fd, "0x", 2);
  if (my_getnbr(dest) < 10)
    write(fd, "0", 1);
  write(fd, dec_to_hex(my_getnbr(dest)), my_strlen(dest, '\0'));
  return (i + 2);
}
Ejemplo n.º 21
0
t_uchar		run_setenv(char ***env, char **argv)
{
  char		*tmp;

  if (count_args(argv) < 2)
    {
      my_printenv(*env, '\n');
      return (0);
    }
  else if (count_args(argv) > 3)
    {
      my_dprintf(STDERR, "setenv: Too many arguments.\n");
      return (1);
    }
  if ((tmp = malloc(sizeof(char) * (my_strlen(argv[1]) + 2))) == NULL)
    my_exit(EXIT_FAILURE, "ERROR: Out of memory! malloc() failed\n");
  tmp = my_strncpy(tmp, argv[1], my_strlen(argv[1]));
  tmp = my_strncat(tmp, "=", 1);
  my_setenv(env, tmp, argv[2]);
  free(tmp);
  return (0);
}
Ejemplo n.º 22
0
int		check_common_char(char **pathv, char **send,
				  char *str, t_comp *count)
{
  int		match;

  if (pathv == NULL)
    {
      my_putstr(*send);
      return (-1);
    }
  match  = find_biggest_match(pathv);
  if (match > my_strlen(str))
    {
      *send = my_strncat((*send),
			 &pathv[0][clear_c(pathv[0], '/') +
				   my_strlen(&str[clear_c(str, '/')])],
			 match - my_strlen(&str[clear_c(str, '/')]));
      count->compt = my_strlen(*send);
      count->save = my_strlen(*send);
    }
  my_putstr(*send);
  return (0);
}
Ejemplo n.º 23
0
int	fill_largeur(char *format, t_spec *spec, va_list *vlist)
{
  int	cpt;
  char	*slargeur;

  cpt = 0;
  if (*format == '*')
    return (fill_largeur_star(spec, vlist));
  while (my_char_is_num(format[cpt]))
    cpt++;
  if (cpt != 0)
    {
      if ((slargeur = malloc(sizeof(char) * (cpt + 1))) == NULL)
        exit(-1);
      slargeur[0] = '\0';
      my_strcat(slargeur, "");
      my_strncat(slargeur, format, cpt);
      spec->largeur = my_getnbr(slargeur);
      free(slargeur);
    }
  else
    spec->largeur = 0;
  return (cpt);
}
Ejemplo n.º 24
0
int main(int argc, char *argv[])
{
   // if(testme())
     //   return 0;
    if (argc == 2 && (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0)) 
	{
	    printUsage(argv[0]);
	    return EXIT_SUCCESS;
	}

    if (argc != 3) {
	printUsage(argv[0]);
	return EXIT_FAILURE;
    }

    FILE * f;
    if ((f = fopen(argv[2], "r")) == NULL) {
	printf("Unable to open file '%s', aborting!\n", argv[2]);
	return EXIT_FAILURE;
    }
    FILE * fptrout = stdout;

    int line_count = 0;
    char line_buffer[LINE_SIZE];
    while (fgets(line_buffer, LINE_SIZE, f) != NULL) 
	{
	    line_count++;
	}
    int file_length = ftell(f);
    rewind(f);
    
    char * * src = malloc(sizeof(char *) * line_count);
    char * * copy = malloc(sizeof(char *) * line_count);
    int i;
    for (i = 0; i < line_count; i++) 
	{
	    if (feof(f)) 
		{
		    printf("Not enough lines in file!\n");
		    fclose(f);
		    return EXIT_FAILURE;
		}
	    copy[i] = malloc(sizeof(char) * LINE_SIZE);
	    fgets(copy[i], LINE_SIZE, f);
	    trim(copy[i]);
	    src[i] = strdup(copy[i]);
	}
    fclose(f);

    char * buffer = malloc(sizeof(char) * file_length);
    buffer[0] = '\0';
    //Partitioning outputs
    const char * command = argv[1];

    if (strcmp(command, "my_strlen") == 0) {
	for (i = 0; i < line_count; i++) {
	    fprintf(fptrout, "length: %d\n", my_strlen(copy[i]));
	}
    } else if (strcmp(command, "my_countchar") == 0) {
	for (i = 0; i < line_count; i++) {
	    fprintf(fptrout, "count(%c): %d\n", copy[i][0], my_countchar(copy[i], copy[i][0]));
	}
    } else if (strcmp(command, "my_strupper") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strupper(copy[i]);
	    fprintf(fptrout, "uppercase: %s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strlower") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strlower(copy[i]);
	    fprintf(fptrout, "lowercase: %s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strcat") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strcat(copy[i], " ");
	    my_strcat(copy[i], src[i]);
	    fprintf(fptrout, "%s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strncat") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strncat(copy[i], "   ", 2);
	    my_strncat(copy[i], "........", 1);
	    fprintf(fptrout, "%s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strcpy") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strcpy(copy[i], "Copying this String.");
	    fprintf(fptrout, "%s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strncpy") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strncpy(copy[i], src[i], 5);
	    fprintf(fptrout, "%s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strstr") == 0) {
	for (i = 0; i < line_count; i++) {
	    fprintf(fptrout, "%d\n", my_strstr(copy[i], src[i]) != NULL);
	}
    } else if (strcmp(command, "my_strinsert") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strinsert(buffer, "\n", 0);
	    my_strinsert(buffer, src[i], 0);
	}
	fprintf(fptrout, "[%s]\n", buffer);
    } else if (strcmp(command, "my_strdelete") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strinsert(buffer, "\n", 0);
	    my_strinsert(buffer, src[i], 0);
	    my_strdelete(buffer, 0, 10);
	}
	fprintf(fptrout, "[%s]\n", buffer);
    }

    for (i = 0; i < line_count; i++) {
	free(copy[i]);
	free(src[i]);
    }
    free(src);
    free(copy);
    free(buffer);
    fclose(fptrout);
    return EXIT_SUCCESS;
}