static const char * executable_dir(const char *progname) { SB sb; struct stat sbuf; if (! progname) return 0; sbinit(&sb); if (progname[0]=='/') sbaddn(&sb, progname, strlen(progname)); else if (strchr(progname,'/')) sbaddsf(&sb,catpath(NULL,progname)); else { char *p = getenv("PATH"); for (;;) { sb.len = 0; if (! (p && *p)) return sbfree(&sb); while (*p && *p!=':') sbadd1(&sb, *p++); if (*p ==':') p++; sbadd1(&sb, 0); sb.len = 0; sbaddsf(&sb, catpath(sb.buffer, progname)); sbadd1(&sb, 0); if (sb.buffer && stat(sb.buffer,&sbuf)>=0 && S_ISREG(sbuf.st_mode)) if (access(sb.buffer, X_OK) >= 0) break; } } #ifdef S_ISLNK sbadd1(&sb, 0); while(sb.buffer && lstat(sb.buffer,&sbuf)>=0 && S_ISLNK(sbuf.st_mode)) { int h,l; sbaddn(&sb,"../",3); h = sb.len; while ((l=readlink(sb.buffer,sb.buffer+h,sb.maxlen-h))>=sb.maxlen-h) sbgrow(&sb, sb.maxlen - h + SBINCREMENT); if (l < 0) return sbfree(&sb); sb.len = h + l; sbadd1(&sb,0); sb.len = 0; sbaddsf(&sb, catpath(sb.buffer,sb.buffer+h-3)); sbadd1(&sb,0); } #endif while (sb.buffer && sb.len>0 && sb.buffer[sb.len-1]=='/') sb.len -= 1; while (sb.buffer && sb.len>0 && sb.buffer[sb.len-1]!='/') sb.len -= 1; while (sb.buffer && sb.len>0 && sb.buffer[sb.len-1]=='/') sb.len -= 1; sbadd1(&sb, 0); return sb.buffer; }
static void fscreate(Chan *c, char *name, int mode, ulong perm) { int m; ulong t; wchar_t *newpath; Ufsinfo *uif; m = fsomode(mode&3); uif = c->aux; t = pathtype(uif->path); newpath = catpath(uif->path, name, nil); if(waserror()){ free(newpath); nexterror(); } if(perm & DMDIR) { wchar_t *p; DIR *d; if(m || t!=TPATH_FILE) error(Eperm); if(!CreateDirectory(newpath, NULL)) oserror(); d = malloc(sizeof(*d)); p = catpath(newpath, "*.*", nil); d->handle = FindFirstFile(p, &d->wfd); free(p); if(d->handle == INVALID_HANDLE_VALUE){ free(d); oserror(); } d->keep = 1; uif->dir = d; } else { uif->dir = nil; if((uif->fh = CreateFile( newpath, fsaccess(mode), FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0)) == INVALID_HANDLE_VALUE) oserror(); } free(uif->path); uif->path = newpath; poperror(); c->qid = wfdtoqid(newpath, nil); c->offset = 0; c->flag |= COPEN; c->mode = openmode(mode); }
static int fswalk1(Chan *c, char *name) { WIN32_FIND_DATA wfd; HANDLE h; wchar_t *p; Ufsinfo *uif; uif = c->aux; p = catpath(uif->path, name, nil); switch(pathtype(p)){ case TPATH_VOLUME: if(!checkvolume(p)){ free(p); return 0; } case TPATH_ROOT: c->qid = wfdtoqid(p, nil); break; case TPATH_FILE: if((h = FindFirstFile(p, &wfd)) == INVALID_HANDLE_VALUE){ free(p); return 0; } FindClose(h); c->qid = wfdtoqid(p, &wfd); break; } free(uif->path); uif->path = p; return 1; }
static char * catpath(const char *from, const char *fname) { SB sb; sbinit(&sb); if (fname && fname[0]=='/') sbadd1(&sb, '/'); else if (from) sbaddsf(&sb, catpath(NULL,from)); else { char *cwd = 0; while (sb.buffer && !cwd) { cwd = getcwd(sb.buffer + sb.len, sb.maxlen - sb.len); if (cwd || errno != ERANGE) break; sbgrow(&sb, sb.maxlen - sb.len + SBINCREMENT); } if (cwd) sb.len += strlen(cwd); else sbfree(&sb); } for (;;) { while (fname && fname[0]=='/') fname++; if (!fname || !fname[0]) { while (&sb.buffer && sb.len>0 && sb.buffer[sb.len-1]=='/') sb.len -= 1; sbadd1(&sb, 0); return sb.buffer; } if (fname[0]=='.') { if (fname[1]=='/' || fname[1]==0) { fname +=1; continue; } if (fname[1]=='.') if (fname[2]=='/' || fname[2]==0) { fname +=2; while (sb.buffer && sb.len>0 && sb.buffer[sb.len-1]=='/') sb.len -= 1; while (sb.buffer && sb.len>0 && sb.buffer[sb.len-1]!='/') sb.len -= 1; continue; } } if (sb.len==0 || (sb.buffer && sb.buffer[sb.len-1]!='/')) sbadd1(&sb, '/'); while (*fname!=0 && *fname!='/') sbadd1(&sb, *fname++); } sbadd1(&sb, 0); return sb.buffer; }
void catWithNULLDir(char * fullpath) { char path[CP_MAXPATH]; char drive[CP_MAXPATH], dir[CP_MAXPATH], fname[CP_MAXPATH], ext[CP_MAXPATH]; printf("Full path = %s\n", fullpath); slicepath(fullpath, drive, dir, fname, ext); printf("sliced path = [%s] [%s] [%s] [%s]\n", drive, dir, fname, ext); catpath(path, drive, NULL, fname, ext); printf("cat path minus dir = [%s]\n", path); printf("\n"); }
void printSplitPrintCatPrint(char * fullpath) { char *path; char drive[CP_MAXPATH], dir[CP_MAXPATH], fname[CP_MAXPATH], ext[CP_MAXPATH]; printf("Full path = %s\n", fullpath); slicepath(fullpath, drive, dir, fname, ext); printf("sliced path = [%s] [%s] [%s] [%s]\n", drive, dir, fname, ext); path = catpath(NULL, drive, dir, fname, ext); printf("concatenated path = %s\n", path); free(path); printf("\n"); }
static Chan* fsopen(Chan *c, int mode) { ulong t; int m, isdir; wchar_t *p; Ufsinfo *uif; m = mode & (OTRUNC|3); switch(m) { case 0: break; case 1: case 1|16: break; case 2: case 0|16: case 2|16: break; case 3: break; default: error(Ebadarg); } isdir = c->qid.type & QTDIR; if(isdir && mode != OREAD) error(Eperm); m = fsomode(m & 3); c->mode = openmode(mode); uif = c->aux; uif->offset = 0; t = pathtype(uif->path); if(isdir){ DIR *d; d = malloc(sizeof(*d)); switch(t){ case TPATH_ROOT: d->drivebuf = malloc(sizeof(wchar_t)*MAX_PATH); if(GetLogicalDriveStrings(MAX_PATH-1, d->drivebuf) == 0){ free(d->drivebuf); d->drivebuf = nil; oserror(); } d->drivep = d->drivebuf; break; case TPATH_VOLUME: case TPATH_FILE: p = catpath(uif->path, "*.*", nil); d->handle = FindFirstFile(p, &d->wfd); free(p); if(d->handle == INVALID_HANDLE_VALUE){ free(d); oserror(); } break; } d->keep = 1; uif->dir = d; } else { uif->dir = nil; if((uif->fh = CreateFile( uif->path, fsaccess(mode), FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, (mode & OTRUNC) ? TRUNCATE_EXISTING : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)) == INVALID_HANDLE_VALUE) oserror(); } c->offset = 0; c->flag |= COPEN; return c; }
static void rm(size_t pend, const char *base, const int olddir, int ssub, int level) { struct stat st; if (lstat(base, &st) < 0) { if (fflag == 0 || errno != ENOENT) { if (sysv3) fprintf(stderr, "%s: %s non-existent\n", progname, path); else fprintf(stderr, "%s: %s\n", path, strerror(errno)); errcnt |= 4; } return; } if ((st.st_mode&S_IFMT) == S_IFDIR) { if (rflag) { struct direc *dp; struct getdb *db; int df, err; if (ssub == 0 && (iflag #ifdef SUS || (!fflag && ontty && access(base, W_OK) < 0) #endif )) { msg("directory %s: ? ", path); if (confirm() == 0) return; } if ((df = open(base, O_RDONLY #ifdef O_DIRECTORY | O_DIRECTORY #endif #ifdef O_NOFOLLOW | O_NOFOLLOW #endif )) < 0 || (db = getdb_alloc(base, df)) == NULL) { if (errno == EMFILE) { int sres; sres = subproc(pend, base, level); if (sres >= 0) { errcnt |= sres; goto remove; } } /* * Maybe the directory is empty and can just * be removed. */ if (rmfile(base, &st) < 0) { fprintf(stderr, "%s: cannot read " "directory %s\n", progname, path); errcnt |= 4; } return; } if (fchdir(df) < 0) { if (rmfile(base, &st) < 0) { fprintf(stderr, "%s: cannot chdir to %s\n", progname, path); errcnt |= 4; } getdb_free(db); close(df); return; } setlevel(level, db, df); while ((dp = getdir(db, &err)) != NULL) { if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || dp->d_name[1] == '.' && dp->d_name[2] == '\0')) continue; rm(catpath(pend, dp->d_name), dp->d_name, df, 0, level + 1); path[pend] = '\0'; } if (err) { fprintf(stderr, "%s: error reading directory %s\n", progname, path); errcnt |= 4; } if (olddir >= 0 && fchdir(olddir) < 0) { fprintf(stderr, "%s: cannot change backwards\n", progname); exit(1); } getdb_free(db); close(df); } else { fprintf(stderr, "%s: %s directory\n", progname, path); errcnt |= 1; return; } } if (ssub == 0) remove: rmfile(base, &st); }