char *totoken(char *s){ char buf[1000]; char *p = buf; if ('0' <= *s && *s <= '9') *p++ = '_'; for (;*s;s++) { char c = *s; if (('a'<=c && c<='z') || ('A'<=c && c<='Z') || ('0' <= c && c <= '9')) { *p++ = c; continue; } switch(c){ case '*': laydown("star",&p); break; case '<': laydown("less",&p); break; case '+': laydown("plus",&p); break; case '-': laydown("minus",&p); break; case '/': laydown("slash",&p); break; case '_': laydown("underscore",&p); break; case '>': laydown("greater",&p); break; case '=': laydown("equal",&p); break; case '$': laydown("dollar",&p); break; case '@': laydown("at",&p); break; case '!': laydown("pt",&p); break; case '#': laydown("sharp",&p); break; case '%': laydown("pct",&p); break; case '^': laydown("circ",&p); break; case '&': laydown("amp",&p); break; case '`': laydown("bq",&p); break; case '~': laydown("tilde",&p); break; case '|': laydown("or",&p); break; case '\\': laydown("backslash",&p); break; case '[': laydown("lbracket",&p); break; case ']': laydown("rbracket",&p); break; case ':': laydown("colon",&p); break; case ';': laydown("semicolon",&p); break; case '"': laydown("quotes",&p); break; case '\'': laydown("quote",&p); break; case '?': laydown("question",&p); break; case '{': laydown("lbrace",&p); break; case '}': laydown("rbrace",&p); break; case '.': laydown("period",&p); break; case ',': laydown("comma",&p); break; default : assert(FALSE); } assert(p < buf + sizeof(buf)); } *p = '\0'; return strperm(buf); }
int b_mkfifo(int argc, char *argv[], void* context) { register char* arg; register mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; register mode_t mask = 0; register int mflag = 0; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case 'm': mflag = 1; mode = strperm(arg = opt_info.arg, &opt_info.arg, mode); if (*opt_info.arg) error(ERROR_exit(0), "%s: invalid mode", arg); continue; case ':': error(2, "%s", opt_info.arg); break; case '?': error(ERROR_usage(2), "%s", opt_info.arg); break; } break; } argv += opt_info.index; if (error_info.errors || !*argv) error(ERROR_usage(2), "%s", optusage(NiL)); mask = umask(0); if (!mflag) { mode &= ~mask; umask(mask); mask = 0; } while (arg = *argv++) if (mkfifo(arg, mode) < 0) error(ERROR_system(0), "%s:", arg); if (mask) umask(mask); return error_info.errors != 0; }
static void put_mode(Namval_t* np, const char* val, int flag, Namfun_t* nfp) { if(val) { int32_t mode; char *last; if(flag&NV_INTEGER) { if(flag&NV_LONG) mode = *(Sfdouble_t*)val; else mode = *(double*)val; } else { mode = strperm(val, &last,0); if(*last) errormsg(SH_DICT,ERROR_exit(1),"%s: invalid mode string",val); } nv_putv(np,(char*)&mode,NV_INTEGER,nfp); } else nv_putv(np,val,flag,nfp); }
node integer(int n){ node q = newnode(INT_CONST,int_const_tag); q->body.int_const.contents = strperm(intToString(n)); return q; }
int b_mktemp(int argc, char** argv, Shbltin_t* context) { mode_t mode = 0; mode_t mask; int fd; int i; int quiet = 0; int unsafe = 0; int* fdp = &fd; char* dir = ""; char* pfx; char* t; char path[PATH_MAX]; cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY); for (;;) { switch (optget(argv, usage)) { case 'd': fdp = 0; continue; case 'm': mode = strperm(pfx = opt_info.arg, &opt_info.arg, S_IRWXU); if (*opt_info.arg) error(ERROR_exit(0), "%s: invalid mode", pfx); continue; case 'p': if ((t = getenv("TMPDIR")) && *t) dir = 0; else dir = opt_info.arg; continue; case 'q': quiet = 1; continue; case 't': dir = 0; continue; case 'u': unsafe = 1; fdp = 0; continue; case 'R': if (!pathtemp(NiL, 0, opt_info.arg, "/seed", NiL)) error(2, "%s: regression test initializtion failed", opt_info.arg); continue; case ':': error(2, "%s", opt_info.arg); break; case '?': error(ERROR_usage(2), "%s", opt_info.arg); break; } break; } argv += opt_info.index; if (error_info.errors || (pfx = *argv++) && *argv) error(ERROR_usage(2), "%s", optusage(NiL)); mask = umask(0); if (!mode) mode = (fdp ? (S_IRUSR|S_IWUSR) : S_IRWXU) & ~mask; umask(~mode & (S_IRWXU|S_IRWXG|S_IRWXO)); if (!pfx) { pfx = "tmp_"; if (dir && !*dir) dir = 0; } if (t = strrchr(pfx, '/')) { i = ++t - pfx; dir = fmtbuf(i); memcpy(dir, pfx, i); dir[i] = 0; pfx = t; } for (;;) { if (!pathtemp(path, sizeof(path), dir, pfx, fdp)) { if (quiet) error_info.errors++; else error(ERROR_SYSTEM|2, "cannot create temporary path"); break; } if (fdp || unsafe || !mkdir(path, mode)) { if (fdp) close(*fdp); sfputr(sfstdout, path, '\n'); break; } if (sh_checksig(context)) { error_info.errors++; break; } } umask(mask); return error_info.errors != 0; }
int b_umask(int argc,char *argv[],Shbltin_t *context) { register char *mask; register int flag = 0; register bool sflag=false, pflag=false; NOT_USED(context); while((argc = optget(argv,sh_optumask))) switch(argc) { case 'p': pflag = true; break; case 'S': sflag = true; break; case ':': errormsg(SH_DICT,2, "%s", opt_info.arg); break; case '?': errormsg(SH_DICT,ERROR_usage(2), "%s",opt_info.arg); break; } if(error_info.errors) errormsg(SH_DICT,ERROR_usage(2),"%s",optusage((char*)0)); argv += opt_info.index; if(mask = *argv) { register int c; if(isdigit(*mask)) { while(c = *mask++) { if (c>='0' && c<='7') flag = (flag<<3) + (c-'0'); else errormsg(SH_DICT,ERROR_exit(1),e_number,*argv); } } else { char *cp = mask; flag = umask(0); c = strperm(cp,&cp,~flag&0777); if(*cp) { umask(flag); errormsg(SH_DICT,ERROR_exit(1),e_format,mask); } flag = (~c&0777); } umask(flag); } else { char *prefix = pflag?"umask ":""; umask(flag=umask(0)); if(sflag) sfprintf(sfstdout,"%s%s\n",prefix,fmtperm(~flag&0777)); else sfprintf(sfstdout,"%s%0#4o\n",prefix,flag); } return(0); }
int main(int argc, char **argv){ int i; char *p; GC_INIT(); ::cgc1::cgc_root_t hash_bucket_root(hash_buckets); hash_buckets=reinterpret_cast<node*>(::cgc1::cgc_malloc(sizeof(node)*7313)); progname = BaseName(argv[0]); yyinit(); for (p=argv[0]; *p; p++) if (*p=='/') progname = p+1; for (i=1; i<argc; i++) { if (EQUAL == strcmp(argv[i],"--help")) { usage(); exit(0); } if (EQUAL == strcmp(argv[i],"-dep")) { stop_after_dep = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-cxx")) { do_cxx = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-noline")) { noline = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-pthreadlocal")) { pthreadThreadLocal=TRUE; compilerThreadLocal=FALSE; continue; } if (EQUAL == strcmp(argv[i],"-typecodes")) { printtypecodes(); return 0; } if (EQUAL == strcmp(argv[i],"-noarraychks")) { arraychks = FALSE; continue; } if (EQUAL == strcmp(argv[i],"-nocasechks")) { casechks = FALSE; continue; } if (EQUAL == strcmp(argv[i],"-nomacros")) { nomacros = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-O")) { arraychks = FALSE; casechks = FALSE; continue; } if (EQUAL == strcmp(argv[i],"-tabwidth")) { i++; if (i < argc) tabwidth = atoi(argv[i]); continue; } if (EQUAL == strcmp(argv[i],"-yydebug")) { yydebug = 1; continue; } if (EQUAL == strcmp(argv[i],"-debug")) { debug = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-v")) { puts(Version); puts(Copyright); continue; } if ('-' == argv[i][0] && 'I' == argv[i][1]) { if (argv[i][2] == 0) { error("-I option: missing directory"); usage(); exit(1); } char buf[256]; strcpy(buf,sigpath); strcat(buf,":"); strcat(buf,argv[i]+2); sigpath = strperm(buf); continue; } if ('-' == argv[i][0]) { error("unrecognized option %s\n",argv[i]); usage(); exit(1); } if ( EQUAL == strcmp(".d",tail(argv[i])) || EQUAL == strcmp(".dd",tail(argv[i])) ) { node f; do_this_cxx = do_cxx || EQUAL == strcmp(".dd",tail(argv[i])); global_scope = new(struct SCOPE); readsetup(global_scope); targetname = newsuffixbase(argv[i],""); f = readfile(argv[i]); if (debug) { char *n = newsuffixbase(argv[i],".out"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } put("After parsing:\n"); pp(f); fflush(stdout); } outfilename = newsuffixbase(argv[i], do_this_cxx ? "-tmp.cc" : "-tmp.c"); { char *n = newsuffixbase(argv[i],".dep.tmp"); dependfile = fopen(n,"w"); if (dependfile == NULL) fatal("can't open file %s",n); } f = chkprogram(f); if (debug) { char *n = newsuffixbase(argv[i],".log"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } pprintl(f); } { node t = global_scope->signature; char *n = newsuffixbase(argv[i],".sig.tmp"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } printf("-- generated by %s\n\n",progname); while (t != NULL) { dprint(CAR(t)); put(";\n"); t = CDR(t); } } if (stop_after_dep) quit(); checkfordeferredsymbols(); if (debug) { char *n = newsuffixbase(argv[i],".sym"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } printsymboltable(); printtypelist(); printstringlist(); } if (n_errors > 0) { quit(); } if (TRUE) { char *n = newsuffixbase(argv[i],"-exports.h.tmp"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } printf("#ifndef %s_included\n",targetname); printf("#define %s_included\n",targetname); declarationsstrings = reverse(declarationsstrings); while (declarationsstrings) { node s = unpos(car(declarationsstrings)); assert(isstrconst(s)); put_unescape(s->body.string_const.characters); put("\n"); declarationsstrings = cdr(declarationsstrings); } put(declarations_header); /* printtypecodes(); */ cprinttypes(); put(declarations_trailer); put("#endif\n"); } if (TRUE) { if (NULL == freopen(outfilename,"w", stdout)) { fatal("can't open file %s",outfilename); } printf("#include \"%s\"\n",newsuffixbase(argv[i],"-exports.h")); put(code_header); headerstrings = reverse(headerstrings); while (headerstrings) { locn(car(headerstrings)); printpos(); node s = unpos(car(headerstrings)); assert(isstrconst(s)); put_unescape(s->body.string_const.characters); put("\n"); locn(NULL); headerstrings = cdr(headerstrings); } cprintsemi(f); } } else { fprintf(stderr,"unknown file type %s\n",argv[i]); usage(); exit(1); } } quit(); return 0; }
int b_mkdir(int argc, char** argv, void* context) { register char* arg; register int n; register mode_t mode = DIRMODE; register mode_t mask = 0; register int mflag = 0; register int pflag = 0; register int vflag = 0; char* name; mode_t dmode; struct stat st; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case 0: break; case 'm': mflag = 1; mode = strperm(arg = opt_info.arg, &opt_info.arg, mode); if (*opt_info.arg) error(ERROR_exit(0), "%s: invalid mode", arg); continue; case 'p': pflag = 1; continue; case 'v': vflag = 1; continue; case ':': error(2, "%s", opt_info.arg); continue; case '?': error(ERROR_usage(2), "%s", opt_info.arg); continue; } break; } argv += opt_info.index; if (error_info.errors || !*argv) error(ERROR_usage(2), "%s", optusage(NiL)); mask = umask(0); if (mflag || pflag) { dmode = DIRMODE & ~mask; if (!mflag) mode = dmode; dmode |= S_IWUSR | S_IXUSR; } else { mode &= ~mask; umask(mask); mask = 0; } while (arg = *argv++) { if (mkdir(arg, mode) < 0) { if (!pflag || !(errno == ENOENT || errno == EEXIST || errno == ENOTDIR)) { error(ERROR_system(0), "%s:", arg); continue; } if (errno == EEXIST) continue; /* * -p option, preserve intermediates * first eliminate trailing /'s */ n = strlen(arg); while (n > 0 && arg[--n] == '/'); arg[n + 1] = 0; for (name = arg, n = *arg; n;) { /* skip over slashes */ while (*arg == '/') arg++; /* skip to next component */ while ((n = *arg) && n != '/') arg++; *arg = 0; if (mkdir(name, n ? dmode : mode) < 0 && errno != EEXIST && access(name, F_OK) < 0) { *arg = n; error(ERROR_system(0), "%s:", name); break; } if (vflag) error(0, "%s: directory created", name); if (!(*arg = n) && (mode & (S_ISVTX|S_ISUID|S_ISGID))) { if (stat(name, &st)) { error(ERROR_system(0), "%s: cannot stat", name); break; } if ((st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)) != (mode & (S_ISVTX|S_ISUID|S_ISGID)) && chmod(name, mode)) { error(ERROR_system(0), "%s: cannot change mode from %s to %s", name, fmtperm(st.st_mode & (S_ISVTX|S_ISUID|S_ISGID)), fmtperm(mode)); break; } } } } else if (vflag) error(0, "%s: directory created", arg); } if (mask) umask(mask); return error_info.errors != 0; }
static void* aso_init_fcntl(void* data, const char* details) { APL_t* apl = (APL_t*)data; char* path; char* opt; size_t size; size_t references; int n; int fd; int drop; int perm; struct flock lock; char buf[PATH_MAX]; char tmp[64]; if (apl) { lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = apl->size; lock.l_len = sizeof(references); if (fcntl(apl->fd, F_SETLKW, &lock) >= 0) { if (lseek(apl->fd, apl->size, SEEK_SET) != apl->size) references = 0; else if (read(apl->fd, &references, sizeof(references)) != sizeof(references)) references = 0; else if (references > 0) { references--; if (lseek(apl->fd, apl->size, SEEK_SET) != apl->size) references = 0; else if (write(apl->fd, &references, sizeof(references)) != sizeof(references)) references = 0; } lock.l_type = F_UNLCK; fcntl(apl->fd, F_SETLK, &lock); if (!references) remove(apl->path); } close(apl->fd); free(apl); return 0; } fd = -1; perm = S_IRUSR|S_IWUSR; drop = 0; size = 32 * 1024 - sizeof(references); if (path = (char*)details) while (opt = strchr(path, ',')) { if (strneq(path, "perm=", 5)) { if ((n = opt - (path + 5)) >= sizeof(tmp)) n = sizeof(tmp) - 1; memcpy(tmp, path + 5, n); tmp[n] = 0; perm = strperm(tmp, NiL, perm); } else if (strneq(path, "size=", 5)) { size = strtoul(path + 5, NiL, 0); if (size <= sizeof(references)) goto bad; size -= sizeof(references); } path = opt + 1; } if (!path || !*path) { if (!(path = pathtemp(buf, sizeof(buf), NiL, "aso", &fd))) return 0; drop = 1; } if (!(apl = newof(0, APL_t, 1, strlen(path)))) goto bad; if (fd >= 0 || (fd = open(path, O_RDWR)) < 0 && (fd = open(path, O_CREAT|O_RDWR, perm)) >= 0) { if (lseek(fd, size, SEEK_SET) != size) goto bad; references = 1; if (write(fd, &references, sizeof(references)) != sizeof(references)) goto bad; } else { if ((size = lseek(fd, 0, SEEK_END)) <= sizeof(references)) goto bad; size -= sizeof(references); lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = sizeof(references); if (fcntl(fd, F_SETLKW, &lock) < 0) goto bad; if (lseek(fd, size, SEEK_SET) != size) goto bad; if (read(fd, &references, sizeof(references)) != sizeof(references)) goto bad; references++; if (lseek(fd, size, SEEK_SET) != size) goto bad; if (write(fd, &references, sizeof(references)) != sizeof(references)) goto bad; lock.l_type = F_UNLCK; fcntl(fd, F_SETLK, &lock); } apl->fd = fd; apl->size = size; strcpy(apl->path, path); return apl; bad: if (apl) free(apl); if (fd >= 0) close(fd); if (drop) remove(path); return 0; }