char *cd_cleanpath(char *path) { t_dlist *lst; t_dlist *clean; char cpath[2048]; char *pref; if (!ft_strcmp("-", path)) { if (!(path = ft_getenv("OLDPWD"))) cd_exit("OLDPWD needed in the environment"); return (ft_strdup(path)); } else if (*path != '/') { if (!(pref = ft_getenv("PWD"))) cd_exit("PWD needed in the environment"); ft_sprintf(cpath, "%s/%s/", pref, path); } else ft_sprintf(cpath, "%s/", path); if (!(lst = makelst(cpath))) return (NULL); if (!(clean = ft_dlstmap(lst, cleanlst))) return (NULL); ft_dlstdel(&lst, NULL); clean = cleanlstparent(clean); return (convertlst(&clean)); }
void rl_goto(char *buff, size_t from, size_t to) { int co; int eco; int eli; char c; co = tgetnum("co"); if (ABS((eco = ((from % co) - (to % co))))) { if (eco > 0) c = 'D'; else c = 'C'; ft_sprintf(buff, "\033[%d%c", ABS(eco), c); write(1, buff, ft_strlen(buff)); } if (ABS((eli = ((from / co) - (to / co))))) { if (eli > 0) c = 'A'; else c = 'B'; ft_sprintf(buff, "\033[%d%c", ABS(eli), c); write(1, buff, ft_strlen(buff)); } }
static int usage(char c) { char msg[1024]; ft_sprintf(msg, "Illegal option -- %c\nUsage: cd [-L|P] [rep]", c); ft_error(msg); return (-1); }
char *cd_cleanpath(char *path) { char cpath[2048]; char *pref; if (!ft_strcmp("-", path)) return (cd_oldpwd()); else if (*path != '/') { if (!(pref = ft_getenv("PWD"))) { ft_error("PWD missing"); return (NULL); } ft_sprintf(cpath, "%s/%s/", pref, path); } else ft_sprintf(cpath, "%s/", path); return (clean(path, cpath)); }
int main(int ac, char **av) { int n; int opt; char *oldrep; char *rep; char *tmp; char msg[2048]; ft_init(av[0]); opt = ft_getopt(ac, av, "LP"); if (!(n = ft_countarg(ac, av))) oldrep = cd_currentrep(); else oldrep = av[ac - n]; if (!(rep = cd_cleanpath(oldrep))) ft_exit(1, "Memory allocation fail"); if (CD_ISO(opt, CD_O_P) && CD_ISO(opt, CD_O_L)) return (cd_usage()); tmp = getcwd(NULL, 0); if (access(rep, F_OK)) { ft_sprintf(msg, "No such directory: %s", oldrep); ft_error(msg); return (-1); } if (chdir(rep)) { ft_sprintf(msg, "Permission denied: %s", oldrep); ft_error(msg); return (-1); } ft_setenv("OLDPWD=", ft_getenv("PWD")); if (CD_ISO(opt, CD_O_P)) ft_setenv("PWD=", tmp); else ft_setenv("PWD=", rep); free(tmp); ft_printf("%s\n", ft_getenv("PWD")); return (0); }
char *print_plan(t_plan *p) { char *str; str = ft_sprintf("Type Objet : Plan\n" " - couleur : %p\n" " - vecteur normal : (%f,%f,%f)\n" " - constante : %d\n" " - reflet : %d", p->color, p->normal->x, p->normal->y, p->normal->z, p->constante, (int)p->ref); return str; }
static int ft_generate_new_name(void) { static char name[0x10]; size_t i; int fd; i = 0; ft_bzero((void *)name, 0x10); while (i != 10) name[i++] = '0' + rand() % 10; ft_sprintf(name + 10, ".bmp"); if ((fd = open(name, O_WRONLY | O_CREAT, 0777)) <= 0) ft_fprintf(2, "Error while generating new name: %s\n", name); else ft_fprintf(1, "Generating %s... ", name); return (fd); }
void verbose_move(t_process *process, int jump) { int i; int index; char str[1024]; if (sgt_corewar()->option.verbose.move == false) return ; i = ft_sprintf(str, "ADV %u (0x%04x -> 0x%04x) ", jump, (int)process->pc, (int)(process->pc + jump)); index = 0; while (index < jump) { s_convert_byte_to_hex(str + i, sgt_corewar()->ram[(process->pc + index) % MEM_SIZE].data); ++index; i += 3; } ft_printf("%s\n", str); }
char *search_cmd2(t_app *app, char *path) { DIR *dirp; struct dirent *d; t_stat m_stat; char *tmp; dirp = opendir(path); while ((d = readdir(dirp))) if (ft_strcmp(d->d_name, app->cur_cmd->first->command) == 0) { ft_sprintf(&tmp, "%s/%s", path, app->cur_cmd->first->command); stat(tmp, &m_stat); if (m_stat.st_mode & 0111) { closedir(dirp); return (tmp); } } closedir(dirp); return (0); }