#define _POSIX_SOURCE #include #include "stdlib.h" #include "string.h" #include "unistd.h" #include #include #include #include #ifdef __STDC__ extern int Error(char*,...); #endif #ifndef F_OK #include /* pre-POSIX */ #endif /* * The author of this software is Eric Grosse. Copyright (c) 1993 by AT&T. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */ #ifdef __STDC__ void iprCreate(char *path) #else iprCreate(path) char *path; #endif { fprintf(stderr,"Create %s\n",path); } #ifdef __STDC__ void (*prCreate)(char*) = iprCreate; /* user hook for different printing */ #else int (*prCreate)() = iprCreate; /* user hook for different printing */ #endif #ifdef __STDC__ int Create(char *name, mode_t mode) #else int Create(name,mode) char *name; int mode; #endif { /* like creat(), but makes intermediate directories and dies on error. */ int fd; char *path, *e, *f; fd = creat(name,mode); if( fd<0 && errno==ENOENT ){ path = (char*)malloc(strlen(name)+1); path || Error("malloc failed"); strcpy(path,name); while(e = strrchr(path,'/')){ *e = '\0'; if(access(path,F_OK)==0) break; } if(!e) e = path-1; strcpy(path,name); /* restore slashes */ /* now e points to slash after existing directory */ while(e = strchr(e+1,'/')){ *e = '\0'; prCreate(path); mkdir(path,0111|mode)==0 || Error("can't mkdir %s",path); *e = '/'; } free(path); fd = creat(name,mode); } if(fd<0) Error("can't Create %s",name); return fd; } #ifdef __STDC__ #include extern char *progname; /* from main() */ int Error(char *fmt, ...) { va_list ap; fprintf(stderr,"%s: ",progname); va_start(ap,fmt); if(vfprintf(stderr,fmt,ap)<0) fprintf(stderr,""); fputc('\n',stderr); va_end(ap); perror(""); /* may or may not be relevant */ exit(1); /* NOTREACHED */ return(0); /* so caller can use crunch() || Error("bad"); */ } #else int Error(fmt) char* fmt; { fputs(fmt,stderr); fputc('\n',stderr); perror(""); exit(1); /* NOTREACHED */ return(0); } #endif