t_namelist *namelist_append(t_namelist *listwas, const char *s, int allowdup) { t_namelist *nl, *nl2; nl2 = (t_namelist *)(getbytes(sizeof(*nl))); nl2->nl_next = 0; nl2->nl_string = (char *)getbytes(strlen(s) + 1); strcpy(nl2->nl_string, s); sys_unbashfilename(nl2->nl_string, nl2->nl_string); if (!listwas) return (nl2); else { for (nl = listwas; ;) { if (!allowdup && !strcmp(nl->nl_string, s)) { freebytes(nl2->nl_string, strlen(nl2->nl_string) + 1); return (listwas); } if (!nl->nl_next) break; nl = nl->nl_next; } nl->nl_next = nl2; } return (listwas); }
static void normalize_path(t_folder_list* x, char *normalized, const char *original) { char buf[FILENAME_MAX]; t_symbol *cwd = canvas_getdir(x->x_canvas); #ifdef _WIN32 sys_unbashfilename(original, buf); #else strncpy(buf, original, FILENAME_MAX); #endif if(sys_isabsolutepath(buf)) { strncpy(normalized, buf, FILENAME_MAX); return; } strncpy(normalized, cwd->s_name, FILENAME_MAX); if(normalized[(strlen(normalized)-1)] != '/') { strncat(normalized, "/", 1); } if(buf[0] == '.') { if(buf[1] == '/') { strncat(normalized, buf + 2, FILENAME_MAX - strlen(normalized)); } else if(buf[1] == '.' && buf[2] == '/') { strncat(normalized, buf, FILENAME_MAX - strlen(normalized)); } } else if(buf[0] != '/') { strncat(normalized, buf, FILENAME_MAX - strlen(normalized)); } else { strncpy(normalized, buf, FILENAME_MAX); } }
int sys_trytoopenone(const char *dir, const char *name, const char* ext, char *dirresult, char **nameresult, unsigned int size, int bin) { int fd; char buf[MAXPDSTRING]; if (strlen(dir) + strlen(name) + strlen(ext) + 4 > size) return (-1); sys_expandpath(dir, buf, MAXPDSTRING); strcpy(dirresult, buf); if (*dirresult && dirresult[strlen(dirresult)-1] != '/') strcat(dirresult, "/"); strcat(dirresult, name); strcat(dirresult, ext); sys_bashfilename(dirresult, dirresult); DEBUG(post("looking for %s",dirresult)); /* see if we can open the file for reading */ if ((fd=open(dirresult,O_RDONLY | MSWOPENFLAG(bin))) >= 0) { /* in unix, further check that it's not a directory */ #ifdef HAVE_UNISTD_H struct stat statbuf; int ok = ((fstat(fd, &statbuf) >= 0) && !S_ISDIR(statbuf.st_mode)); if (!ok) { if (sys_verbose) post("tried %s; stat failed or directory", dirresult); close (fd); fd = -1; } else #endif { char *slash; if (sys_verbose) post("tried %s and succeeded", dirresult); sys_unbashfilename(dirresult, dirresult); slash = strrchr(dirresult, '/'); if (slash) { *slash = 0; *nameresult = slash + 1; } else *nameresult = dirresult; return (fd); } } else { if (sys_verbose) post("tried %s and failed", dirresult); } return (-1); }
t_symbol *pd_getdirname(void) { char buf[MAXPDSTRING], buf2[MAXPDSTRING]; int len; #ifdef _WIN32 if ((len = GetModuleFileName(NULL, buf, sizeof(buf))) == 0) strcpy(buf, "."); else buf[len] = '\0'; sys_unbashfilename(buf, buf); #elif defined(__APPLE__) len = sizeof(buf); _NSGetExecutablePath(buf, &len); if (len != -1) buf[len] = '\0'; #elif defined(__FreeBSD__) len = (ssize_t)(readlink("/proc/curproc/file", buf, sizeof(buf)-1)); if (len != -1) buf[len] = '\0'; #else len = (ssize_t)(readlink("/proc/self/exe", buf, sizeof(buf)-1)); if (len != -1) buf[len] = '\0'; #endif if (len != -1) { char *lastslash; lastslash = strrchr(buf, '/'); if (lastslash) { lastslash++; *lastslash= '\0'; strncpy(buf2, buf, lastslash-buf); buf2[lastslash-buf] = '\0'; } else strcpy(buf2, "."); } else { return 0; } t_symbol *foo = gensym(buf2); return foo; }
/* this routine tries to figure out where to find the auxilliary files Pd will need to run. This is either done by looking at the command line invokation for Pd, or if that fails, by consulting the variable INSTALL_PREFIX. In MSW, we don't try to use INSTALL_PREFIX. */ void sys_findprogdir(char *progname) { char sbuf[MAXPDSTRING], sbuf2[MAXPDSTRING], *sp; char *lastslash; #ifndef _WIN32 struct stat statbuf; #endif /* NOT _WIN32 */ /* find out by what string Pd was invoked; put answer in "sbuf". */ #ifdef _WIN32 GetModuleFileName(NULL, sbuf2, sizeof(sbuf2)); sbuf2[MAXPDSTRING-1] = 0; sys_unbashfilename(sbuf2, sbuf); #else strncpy(sbuf, progname, MAXPDSTRING); sbuf[MAXPDSTRING-1] = 0; #endif /* _WIN32 */ lastslash = strrchr(sbuf, '/'); if (lastslash) { /* bash last slash to zero so that sbuf is directory pd was in, e.g., ~/pd/bin */ *lastslash = 0; /* go back to the parent from there, e.g., ~/pd */ lastslash = strrchr(sbuf, '/'); if (lastslash) { strncpy(sbuf2, sbuf, lastslash-sbuf); sbuf2[lastslash-sbuf] = 0; } else strcpy(sbuf2, ".."); } else { /* no slashes found. Try INSTALL_PREFIX. */ #ifdef INSTALL_PREFIX strcpy(sbuf2, INSTALL_PREFIX); #else strcpy(sbuf2, "."); #endif } /* now we believe sbuf2 holds the parent directory of the directory pd was found in. We now want to infer the "lib" directory and the "gui" directory. In "simple" unix installations, the layout is .../bin/pd .../bin/pd-watchdog (etc) .../bin/pd-gui.tcl .../doc and in "complicated" unix installations, it's: .../bin/pd .../lib/pd/bin/pd-watchdog .../lib/pd/bin/pd-gui.tcl .../lib/pd/doc To decide which, we stat .../lib/pd; if that exists, we assume it's the complicated layout. In MSW, it's the "simple" layout, but "wish" is found in bin: .../bin/pd .../bin/wish80.exe .../doc */ #ifdef MSW sys_libdir = gensym(sbuf2); #else strncpy(sbuf, sbuf2, MAXPDSTRING-30); sbuf[MAXPDSTRING-30] = 0; strcat(sbuf, "/lib/pd"); if (stat(sbuf, &statbuf) >= 0) { /* complicated layout: lib dir is the one we just stat-ed above */ sys_libdir = gensym(sbuf); } else { /* simple layout: lib dir is the parent */ sys_libdir = gensym(sbuf2); } #endif }
/* this routine tries to figure out where to find the auxilliary files Pd will need to run. This is either done by looking at the command line invokation for Pd, or if that fails, by consulting the variable INSTALL_PREFIX. In MSW, we don't try to use INSTALL_PREFIX. */ void sys_findprogdir(char *progname) { char sbuf[FILENAME_MAX], sbuf2[FILENAME_MAX], *sp; char *lastslash; #ifdef UNISTD struct stat statbuf; #endif /* find out by what string Pd was invoked; put answer in "sbuf". */ #ifdef MSW GetModuleFileName(NULL, sbuf2, sizeof(sbuf2)); sbuf2[FILENAME_MAX-1] = 0; sys_unbashfilename(sbuf2, sbuf); #endif /* MSW */ #ifdef UNISTD strncpy(sbuf, progname, FILENAME_MAX); sbuf[FILENAME_MAX-1] = 0; #endif #ifdef INSTALL_PREFIX strcpy(sbuf2, INSTALL_PREFIX); //#else // strcpy(sbuf2, "."); #endif /*lastslash = strrchr(sbuf, '/'); if (!strcmp(sbuf2, "") && lastslash) { // bash last slash to zero so that sbuf is directory pd was in, // e.g., ~/pd/bin *lastslash = 0; // go back to the parent from there, e.g., ~/pd lastslash = strrchr(sbuf, '/'); if (lastslash) { strncpy(sbuf2, sbuf, lastslash-sbuf); sbuf2[lastslash-sbuf] = 0; } else strcpy(sbuf2, ".."); }*/ /* now we believe sbuf2 holds the parent directory of the directory pd was found in. We now want to infer the "lib" directory and the "gui" directory. In "simple" unix installations, the layout is .../bin/pd .../bin/pd-gui .../doc and in "complicated" unix installations, it's: .../bin/pd .../lib/pd-l2ork/bin/pd-gui .../lib/pd-l2ork/doc To decide which, we stat .../lib/pd-l2ork; if that exists, we assume it's the complicated layout. In MSW, it's the "simple" layout, but the gui program is straight wish80: .../bin/pd .../bin/wish80.exe .../doc */ #ifdef MSW sys_libdir = gensym(sbuf2); sys_guidir = &s_; /* in MSW the guipath just depends on the libdir */ #else realpath(sbuf2, sbuf); strncpy(sbuf2, sbuf, FILENAME_MAX-30); sbuf[FILENAME_MAX-30] = 0; strcat(sbuf2, "/lib/pd-l2ork"); if (stat(sbuf2, &statbuf) >= 0) { /* complicated layout: lib dir is the one we just stat-ed above */ sys_libdir = gensym(sbuf2); /* gui lives in .../lib/pd-l2ork/bin */ strncpy(sbuf2, sbuf, FILENAME_MAX-30); sbuf[FILENAME_MAX-30] = 0; strcat(sbuf2, "/lib/pd-l2ork/bin"); sys_guidir = gensym(sbuf2); } else { /* simple layout: lib dir is the parent */ sys_libdir = gensym(sbuf); /* gui lives in .../bin */ strncpy(sbuf2, sbuf, FILENAME_MAX-30); sbuf[FILENAME_MAX-30] = 0; strcat(sbuf2, "/bin"); sys_guidir = gensym(sbuf2); } #endif }