int b_unixpath(int argc, char *argv[], void *context) { int flags = UWIN_W2U; int quote = 0; int n; char* cp; const char* dp; char buff[PATH_MAX+1]; NoP(argc); #if _AST_VERSION >= 20060701L cmdinit(argc, argv, context, NULL, 0); #else cmdinit(argv, context, NULL, 0); #endif while (n = optget(argv, usage)) switch (n) { case 'a': flags |= UWIN_U2W; break; case 'q': quote = 1; break; case ':': error(2, "%s", opt_info.arg); break; case '?': error(ERROR_usage(2), "%s", opt_info.arg); break; } argv += opt_info.index; if(!*argv || error_info.errors) error(ERROR_usage(2),"%s",optusage(NiL)); while(cp = *argv++) { uwin_pathmap(cp, buff, sizeof(buff), flags); if(quote) { for(dp=special;*dp;dp++) { if(strchr(buff, *dp)) break; } if(*dp) { sfprintf(sfstdout,"'%s'\n",buff); continue; } } sfprintf(sfstdout,"%s\n",buff); } return(0); }
int b_logname(int argc, char** argv, void* context) { register char* logname; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case ':': error(2, "%s", opt_info.arg); continue; case '?': error(ERROR_usage(2), "%s", opt_info.arg); continue; } break; } if (error_info.errors) error(ERROR_usage(2), "%s", optusage(NiL)); if (!(logname = getlogin())) logname = fmtuid(getuid()); sfputr(sfstdout, logname, '\n'); return 0; }
int b_ldd(int argc, char** argv) { int n; char* cp; SetErrorMode(SEM_NOGPFAULTERRORBOX); cmdinit(argc, argv, 0, 0, 0); for (;;) { switch (optget(argv, usage)) { case '?': error(ERROR_usage(2), "%s", opt_info.arg); continue; case ':': error(2, "%s", opt_info.arg); continue; } break; } argv += opt_info.index; argc -= opt_info.index; if (error_info.errors || !*argv) error(ERROR_usage(2), "%s", optusage((char*)0)); n = 0; while (cp = *argv++) { if (argc > 1) sfprintf(sfstdout, "%s:\n", cp); if (doldd(cp) < 0) n = 1; } return n; }
int b_sync(int argc, char** argv, Shbltin_t* context) { cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { 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)); #if _lib_sync sync(); #else error(ERROR_usage(2), "failed -- the native system does not provide a sync(2) call"); #endif return 0; }
int b_comm(int argc, char *argv[], Shbltin_t* context) { register int mode = C_FILE1|C_FILE2|C_COMMON; register char *cp; Sfio_t *f1, *f2; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case '1': mode &= ~C_FILE1; continue; case '2': mode &= ~C_FILE2; continue; case '3': mode &= ~C_COMMON; continue; case ':': error(2, "%s",opt_info.arg); break; case '?': error(ERROR_usage(2), "%s",opt_info.arg); break; } break; } argv += opt_info.index; argc -= opt_info.index; if(error_info.errors || argc!=2) error(ERROR_usage(2),"%s",optusage(NiL)); cp = *argv++; if(streq(cp,"-")) f1 = sfstdin; else if(!(f1 = sfopen(NiL, cp,"r"))) error(ERROR_system(1),"%s: cannot open",cp); cp = *argv; if(streq(cp,"-")) f2 = sfstdin; else if(!(f2 = sfopen(NiL, cp,"r"))) error(ERROR_system(1),"%s: cannot open",cp); if(mode) { if(comm(f1,f2,sfstdout,mode) < 0) error(ERROR_system(1)," write error"); } else if(f1==sfstdin || f2==sfstdin) sfseek(sfstdin,(Sfoff_t)0,SEEK_END); if(f1!=sfstdin) sfclose(f1); if(f2!=sfstdin) sfclose(f2); return error_info.errors; }
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; }
int b_expr(int argc, char** argv, Shbltin_t* context) { State_t state; Node_t node; int n; cmdinit(argc, argv, context, ERROR_CATALOG, 0); state.standard = !!conformance(0, 0); #if 0 if (state.standard) state.arglist = argv+1; else #endif { while (n=optget(argv, usage)) { /* * NOTE: this loop ignores all but literal -- and -? * out of kindness for obsolescent usage * (and is ok with the standard) but strict * getopt conformance would give usage for all * unknown - options */ if(n=='?') error(ERROR_usage(2), "%s", opt_info.arg); if (opt_info.option[1] != '?') break; error(ERROR_usage(2), "%s", opt_info.arg); } if (error_info.errors) error(ERROR_usage(2),"%s",optusage((char*)0)); state.arglist = argv+opt_info.index; } if (expr_or(&state, &node)) error(ERROR_exit(2),"syntax error"); if (node.type&T_STR) { if (*node.str) sfprintf(sfstdout,"%s\n",node.str); } else sfprintf(sfstdout,"%d\n",node.num); return numeric(&node)?node.num==0:*node.str==0; }
int b_dirname(int argc, char** argv, Shbltin_t* context) { int mode = 0; char buf[PATH_MAX]; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case 'f': mode |= PATH_REGULAR; continue; case 'r': mode &= ~PATH_REGULAR; mode |= PATH_READ; continue; case 'x': mode |= PATH_EXECUTE; continue; case ':': error(2, "%s", opt_info.arg); break; case '?': error(ERROR_usage(2), "%s", opt_info.arg); break; } break; } argv += opt_info.index; argc -= opt_info.index; if(error_info.errors || argc != 1) error(ERROR_usage(2),"%s", optusage(NiL)); if(!mode) l_dirname(sfstdout,argv[0]); else if(pathpath(argv[0], "", mode, buf, sizeof(buf))) sfputr(sfstdout, buf, '\n'); else error(1|ERROR_WARNING, "%s: relative path not found", argv[0]); return 0; }
int b_basename(int argc, register char** argv, Shbltin_t* context) { char* string; char* suffix = 0; int all = 0; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case 'a': all = 1; continue; case 's': all = 1; suffix = 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; argc -= opt_info.index; if (error_info.errors || argc < 1 || !all && argc > 2) error(ERROR_usage(2), "%s", optusage(NiL)); if (!all) namebase(sfstdout, argv[0], argv[1]); else while (string = *argv++) namebase(sfstdout, string, suffix); return 0; }
int b_basename(int argc,register char *argv[], void* context) { register int n; cmdinit(argc, argv, context, ERROR_CATALOG, 0); while (n = optget(argv, usage)) switch (n) { case ':': error(2, "%s", opt_info.arg); break; case '?': error(ERROR_usage(2), "%s", opt_info.arg); break; } argv += opt_info.index; argc -= opt_info.index; if(error_info.errors || argc < 1 || argc > 2) error(ERROR_usage(2), "%s", optusage(NiL)); namebase(sfstdout,argv[0],argv[1]); return(0); }
int b_head(int argc, register char** argv, void* context) { static const char header_fmt[] = "\n==> %s <==\n"; register Sfio_t* fp; register char* cp; register off_t keep = 10; register off_t skip = 0; register int delim = '\n'; int header = 1; char* format = (char*)header_fmt+1; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case 'c': delim = -1; /*FALLTHROUGH*/ case 'n': if (opt_info.offset && argv[opt_info.index][opt_info.offset] == 'c') { delim = -1; opt_info.offset++; } if ((keep = opt_info.number) <=0) error(2, "%s: %I*d: positive numeric option argument expected", opt_info.name, sizeof(keep), keep); continue; case 'q': header = argc; continue; case 'v': header = 0; continue; case 's': skip = opt_info.number; continue; case '?': error(ERROR_usage(2), "%s", opt_info.arg); continue; case ':': error(2, "%s", opt_info.arg); continue; } break; } argv += opt_info.index; argc -= opt_info.index; if (error_info.errors) error(ERROR_usage(2), "%s", optusage(NiL)); if (cp = *argv) argv++; do { if (!cp || streq(cp, "-")) { cp = "/dev/stdin"; fp = sfstdin; sfset(fp, SF_SHARE, 1); } else if (!(fp = sfopen(NiL, cp, "r"))) { error(ERROR_system(0), "%s: cannot open", cp); continue; } if (argc > header) sfprintf(sfstdout, format, cp); format = (char*)header_fmt; if (skip > 0) sfmove(fp, NiL, skip, delim); if (sfmove(fp, sfstdout, keep, delim) < 0 && errno != EPIPE) error(ERROR_system(0), "%s: read error", cp); if (fp != sfstdin) sfclose(fp); } while (cp = *argv++); if (sfsync(sfstdout)) error(ERROR_system(0), "write error"); return error_info.errors != 0; }
static void registercommands() { COMMAND* cmd = command_list = cmdinit(); //debug control dbgcmdnew("InitDebug\1init\1initdbg", cbDebugInit, false); //init debugger arg1:exefile,[arg2:commandline] dbgcmdnew("StopDebug\1stop\1dbgstop", cbDebugStop, true); //stop debugger dbgcmdnew("AttachDebugger\1attach", cbDebugAttach, false); //attach dbgcmdnew("DetachDebugger\1detach", cbDebugDetach, true); //detach dbgcmdnew("run\1go\1r\1g", cbDebugRun, true); //unlock WAITID_RUN dbgcmdnew("erun\1egun\1er\1eg", cbDebugErun, true); //run + skip first chance exceptions dbgcmdnew("pause", cbDebugPause, true); //pause debugger dbgcmdnew("StepInto\1sti", cbDebugStepInto, true); //StepInto dbgcmdnew("eStepInto\1esti", cbDebugeStepInto, true); //StepInto + skip first chance exceptions dbgcmdnew("StepOver\1step\1sto\1st", cbDebugStepOver, true); //StepOver dbgcmdnew("eStepOver\1estep\1esto\1est", cbDebugeStepOver, true); //StepOver + skip first chance exceptions dbgcmdnew("SingleStep\1sstep\1sst", cbDebugSingleStep, true); //SingleStep arg1:count dbgcmdnew("eSingleStep\1esstep\1esst", cbDebugeSingleStep, true); //SingleStep arg1:count + skip first chance exceptions dbgcmdnew("StepOut\1rtr", cbDebugRtr, true); //rtr dbgcmdnew("eStepOut\1ertr", cbDebugeRtr, true); //rtr + skip first chance exceptions dbgcmdnew("DebugContinue\1con", cbDebugContinue, true); //set continue status dbgcmdnew("LibrarianSetBreakPoint\1bpdll", cbDebugBpDll, true); //set dll breakpoint dbgcmdnew("LibrarianRemoveBreakPoint\1bcdll", cbDebugBcDll, true); //remove dll breakpoint dbgcmdnew("switchthread\1threadswitch", cbDebugSwitchthread, true); //switch thread dbgcmdnew("suspendthread\1threadsuspend", cbDebugSuspendthread, true); //suspend thread dbgcmdnew("resumethread\1threadresume", cbDebugResumethread, true); //resume thread dbgcmdnew("killthread\1threadkill", cbDebugKillthread, true); //kill thread dbgcmdnew("suspendallthreads\1threadsuspendall", cbDebugSuspendAllThreads, true); //suspend all threads dbgcmdnew("resumeallthreads\1threadresumeall", cbDebugResumeAllThreads, true); //resume all threads dbgcmdnew("setthreadpriority\1setprioritythread\1threadsetpriority", cbDebugSetPriority, true); //set thread priority dbgcmdnew("symdownload\1downloadsym", cbDebugDownloadSymbol, true); //download symbols dbgcmdnew("setjit\1jitset", cbDebugSetJIT, false); //set JIT dbgcmdnew("getjit\1jitget", cbDebugGetJIT, false); //get JIT dbgcmdnew("getjitauto\1jitgetauto", cbDebugGetJITAuto, false); //get JIT Auto dbgcmdnew("setjitauto\1jitsetauto", cbDebugSetJITAuto, false); //set JIT Auto dbgcmdnew("getcmdline\1getcommandline", cbDebugGetCmdline, true); //Get CmdLine dbgcmdnew("setcmdline\1setcommandline", cbDebugSetCmdline, true); //Set CmdLine dbgcmdnew("loadlib", cbDebugLoadLib, true); //Load DLL dbgcmdnew("skip", cbDebugSkip, true); //skip one instruction //breakpoints dbgcmdnew("bplist", cbDebugBplist, true); //breakpoint list dbgcmdnew("SetBPXOptions\1bptype", cbDebugSetBPXOptions, false); //breakpoint type dbgcmdnew("SetBPX\1bp\1bpx", cbDebugSetBPX, true); //breakpoint dbgcmdnew("DeleteBPX\1bpc\1bc", cbDebugDeleteBPX, true); //breakpoint delete dbgcmdnew("EnableBPX\1bpe\1be", cbDebugEnableBPX, true); //breakpoint enable dbgcmdnew("DisableBPX\1bpd\1bd", cbDebugDisableBPX, true); //breakpoint disable dbgcmdnew("SetHardwareBreakpoint\1bph\1bphws", cbDebugSetHardwareBreakpoint, true); //hardware breakpoint dbgcmdnew("DeleteHardwareBreakpoint\1bphc\1bphwc", cbDebugDeleteHardwareBreakpoint, true); //delete hardware breakpoint dbgcmdnew("EnableHardwareBreakpoint\1bphe\1bphwe", cbDebugEnableHardwareBreakpoint, true); //enable hardware breakpoint dbgcmdnew("DisableHardwareBreakpoint\1bphd\1bphwd", cbDebugDisableHardwareBreakpoint, true); //disable hardware breakpoint dbgcmdnew("SetMemoryBPX\1membp\1bpm", cbDebugSetMemoryBpx, true); //SetMemoryBPX dbgcmdnew("DeleteMemoryBPX\1membpc\1bpmc", cbDebugDeleteMemoryBreakpoint, true); //delete memory breakpoint dbgcmdnew("EnableMemoryBreakpoint\1membpe\1bpme", cbDebugEnableMemoryBreakpoint, true); //enable memory breakpoint dbgcmdnew("DisableMemoryBreakpoint\1membpd\1bpmd", cbDebugDisableMemoryBreakpoint, true); //enable memory breakpoint //variables dbgcmdnew("varnew\1var", cbInstrVar, false); //make a variable arg1:name,[arg2:value] dbgcmdnew("vardel", cbInstrVarDel, false); //delete a variable, arg1:variable name dbgcmdnew("varlist", cbInstrVarList, false); //list variables[arg1:type filter] dbgcmdnew("mov\1set", cbInstrMov, false); //mov a variable, arg1:dest,arg2:src //misc dbgcmdnew("strlen\1charcount\1ccount", cbStrLen, false); //get strlen, arg1:string dbgcmdnew("cls\1lc\1lclr", cbCls, false); //clear the log dbgcmdnew("chd", cbInstrChd, false); //Change directory dbgcmdnew("disasm\1dis\1d", cbDebugDisasm, true); //doDisasm dbgcmdnew("HideDebugger\1dbh\1hide", cbDebugHide, true); //HideDebugger dbgcmdnew("dump", cbDebugDump, true); //dump at address dbgcmdnew("sdump", cbDebugStackDump, true); //dump at stack address dbgcmdnew("refinit", cbInstrRefinit, false); dbgcmdnew("refadd", cbInstrRefadd, false); dbgcmdnew("asm", cbInstrAssemble, true); //assemble instruction dbgcmdnew("sleep", cbInstrSleep, false); //Sleep //user database dbgcmdnew("cmt\1cmtset\1commentset", cbInstrCmt, true); //set/edit comment dbgcmdnew("cmtc\1cmtdel\1commentdel", cbInstrCmtdel, true); //delete comment dbgcmdnew("lbl\1lblset\1labelset", cbInstrLbl, true); //set/edit label dbgcmdnew("lblc\1lbldel\1labeldel", cbInstrLbldel, true); //delete label dbgcmdnew("bookmark\1bookmarkset", cbInstrBookmarkSet, true); //set bookmark dbgcmdnew("bookmarkc\1bookmarkdel", cbInstrBookmarkDel, true); //delete bookmark dbgcmdnew("savedb\1dbsave", cbInstrSavedb, true); //save program database dbgcmdnew("loaddb\1dbload", cbInstrLoaddb, true); //load program database dbgcmdnew("functionadd\1func", cbInstrFunctionAdd, true); //function dbgcmdnew("functiondel\1funcc", cbInstrFunctionDel, true); //function dbgcmdnew("commentlist", cbInstrCommentList, true); //list comments dbgcmdnew("labellist", cbInstrLabelList, true); //list labels dbgcmdnew("bookmarklist", cbInstrBookmarkList, true); //list bookmarks dbgcmdnew("functionlist", cbInstrFunctionList, true); //list functions //memory operations dbgcmdnew("alloc", cbDebugAlloc, true); //allocate memory dbgcmdnew("free", cbDebugFree, true); //free memory dbgcmdnew("Fill\1memset", cbDebugMemset, true); //memset dbgcmdnew("getpagerights\1getrightspage", cbDebugGetPageRights, true); dbgcmdnew("setpagerights\1setrightspage", cbDebugSetPageRights, true); //plugins dbgcmdnew("StartScylla\1scylla\1imprec", cbDebugStartScylla, false); //start scylla //general purpose dbgcmdnew("cmp", cbInstrCmp, false); //compare dbgcmdnew("gpa", cbInstrGpa, true); dbgcmdnew("add", cbInstrAdd, false); dbgcmdnew("and", cbInstrAnd, false); dbgcmdnew("dec", cbInstrDec, false); dbgcmdnew("div", cbInstrDiv, false); dbgcmdnew("inc", cbInstrInc, false); dbgcmdnew("mul", cbInstrMul, false); dbgcmdnew("neg", cbInstrNeg, false); dbgcmdnew("not", cbInstrNot, false); dbgcmdnew("or", cbInstrOr, false); dbgcmdnew("rol", cbInstrRol, false); dbgcmdnew("ror", cbInstrRor, false); dbgcmdnew("shl", cbInstrShl, false); dbgcmdnew("shr", cbInstrShr, false); dbgcmdnew("sub", cbInstrSub, false); dbgcmdnew("test", cbInstrTest, false); dbgcmdnew("xor", cbInstrXor, false); //script dbgcmdnew("scriptload", cbScriptLoad, false); dbgcmdnew("msg", cbScriptMsg, false); dbgcmdnew("msgyn", cbScriptMsgyn, false); dbgcmdnew("log", cbInstrLog, false); //log command with superawesome hax //data dbgcmdnew("reffind\1findref\1ref", cbInstrRefFind, true); //find references to a value dbgcmdnew("refstr\1strref", cbInstrRefStr, true); //find string references dbgcmdnew("find", cbInstrFind, true); //find a pattern dbgcmdnew("findall", cbInstrFindAll, true); //find all patterns dbgcmdnew("modcallfind", cbInstrModCallFind, true); //find intermodular calls dbgcmdnew("findasm\1asmfind", cbInstrFindAsm, true); //find instruction dbgcmdnew("reffindrange\1findrefrange\1refrange", cbInstrRefFindRange, true); dbgcmdnew("yara", cbInstrYara, true); //yara test command dbgcmdnew("yaramod", cbInstrYaramod, true); //yara rule on module dbgcmdnew("analyse\1analyze\1anal", cbInstrAnalyse, true); //secret analysis command //undocumented dbgcmdnew("bench", cbDebugBenchmark, true); //benchmark test (readmem etc) dbgcmdnew("dprintf", cbPrintf, false); //printf dbgcmdnew("setstr\1strset", cbInstrSetstr, false); //set a string variable dbgcmdnew("getstr\1strget", cbInstrGetstr, false); //get a string variable dbgcmdnew("copystr\1strcpy", cbInstrCopystr, true); //write a string variable to memory dbgcmdnew("looplist", cbInstrLoopList, true); //list loops dbgcmdnew("capstone", cbInstrCapstone, true); //disassemble using capstone dbgcmdnew("visualize", cbInstrVisualize, true); //visualize analysis dbgcmdnew("meminfo", cbInstrMeminfo, true); //command to debug memory map bugs dbgcmdnew("cfanal\1cfanalyse\1cfanalyze", cbInstrCfanalyse, true); //control flow analysis dbgcmdnew("analyse_nukem\1analyze_nukem\1anal_nukem", cbInstrAnalyseNukem, true); //secret analysis command #2 dbgcmdnew("exanal\1exanalyse\1exanalyze", cbInstrExanalyse, true); //exception directory analysis dbgcmdnew("virtualmod", cbInstrVirtualmod, true); //virtual module }
int b_uname(int argc, char** argv, void* context) { register long flags = 0; register int sep = 0; register int n; register char* s; char* t; char* e; char* sethost = 0; int list = 0; struct utsname ut; char buf[257]; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (optget(argv, usage)) { case 'a': flags |= OPT_all|((1L<<OPT_ALL)-1); continue; case 'b': flags |= OPT_base; continue; case 'c': flags |= OPT_vendor; continue; case 'd': flags |= OPT_domain; continue; case 'f': list = 1; continue; case 'h': flags |= OPT_hostid; continue; case 'i': flags |= OPT_implementation; continue; case 'm': flags |= OPT_machine; continue; case 'n': flags |= OPT_nodename; continue; case 'o': flags |= OPT_operating_system; continue; case 'p': flags |= OPT_processor; continue; case 'r': flags |= OPT_release; continue; case 's': flags |= OPT_system; continue; case 't': flags |= OPT_machine_type; continue; case 'v': flags |= OPT_version; continue; case 'x': flags |= OPT_extra; continue; case 'A': flags |= OPT_total|((1L<<OPT_TOTAL)-1); continue; case 'R': flags |= OPT_extended_release; continue; case 'S': sethost = opt_info.arg; continue; case ':': s = "/usr/bin/uname"; if (!streq(argv[0], s) && (!eaccess(s, X_OK) || !eaccess(s+=4, X_OK))) { argv[0] = s; return sh_run(context, argc, argv); } 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 && (flags || sethost) || sethost && flags) error(ERROR_usage(2), "%s", optusage(NiL)); if (sethost) { #if _lib_sethostname if (sethostname(sethost, strlen(sethost) + 1)) #else #ifdef ENOSYS errno = ENOSYS; #else errno = EPERM; #endif #endif error(ERROR_system(1), "%s: cannot set host name", sethost); } else if (list) astconflist(sfstdout, NiL, ASTCONF_base|ASTCONF_defined|ASTCONF_lower|ASTCONF_quote|ASTCONF_matchcall, "CS|SI"); else if (*argv) { e = &buf[sizeof(buf)-1]; while (s = *argv++) { t = buf; *t++ = 'C'; *t++ = 'S'; *t++ = '_'; while (t < e && (n = *s++)) *t++ = islower(n) ? toupper(n) : n; *t = 0; sfprintf(sfstdout, "%s%c", *(t = astconf(buf, NiL, NiL)) ? t : *(t = astconf(buf+3, NiL, NiL)) ? t : "unknown", *argv ? ' ' : '\n'); } } else { s = buf; if (!flags) flags = OPT_system; memzero(&ut, sizeof(ut)); if (uname(&ut) < 0) error(ERROR_usage(2), "information unavailable"); output(OPT_system, ut.sysname, "sysname"); if (flags & OPT_nodename) { #if !_mem_nodeext_utsname && _lib_gethostname if (sizeof(ut.nodename) > 9 || gethostname(s, sizeof(buf))) #endif s = ut.nodename; output(OPT_nodename, s, "nodename"); } output(OPT_release, ut.release, "release"); output(OPT_version, ut.version, "version"); output(OPT_machine, ut.machine, "machine"); if (flags & OPT_processor) { if (!*(s = astconf("ARCHITECTURE", NiL, NiL))) s = ut.machine; output(OPT_processor, s, "processor"); } if (flags & OPT_implementation) { if (!*(s = astconf("PLATFORM", NiL, NiL)) && !*(s = astconf("HW_NAME", NiL, NiL))) { if (t = strchr(hosttype, '.')) t++; else t = (char*)hosttype; strncpy(s = buf, t, sizeof(buf) - 1); } output(OPT_implementation, s, "implementation"); } if (flags & OPT_operating_system) { s = astconf("OPERATING_SYSTEM", NiL, NiL); if (!*s) #ifdef _UNAME_os_DEFAULT s = _UNAME_os_DEFAULT; #else s = ut.sysname; #endif output(OPT_operating_system, s, "operating-system"); } if (flags & OPT_extended_release) { s = astconf("RELEASE", NiL, NiL); output(OPT_extended_release, s, "extended-release"); } #if _mem_idnumber_utsname output(OPT_hostid, ut.idnumber, "hostid"); #else if (flags & OPT_hostid) { if (!*(s = astconf("HW_SERIAL", NiL, NiL))) #if _lib_gethostid sfsprintf(s = buf, sizeof(buf), "%08x", gethostid()); #else /*NOP*/; #endif output(OPT_hostid, s, "hostid"); } #endif if (flags & OPT_vendor) { s = astconf("HW_PROVIDER", NiL, NiL); output(OPT_vendor, s, "vendor"); } if (flags & OPT_domain) { if (!*(s = astconf("SRPC_DOMAIN", NiL, NiL))) #if _lib_getdomainname getdomainname(s, sizeof(buf)); #else /*NOP*/; #endif output(OPT_domain, s, "domain"); } #if _mem_m_type_utsname s = ut.m_type; #else s = astconf("MACHINE", NiL, NiL); #endif output(OPT_machine_type, s, "m_type"); #if _mem_base_rel_utsname s = ut.base_rel; #else s = astconf("BASE", NiL, NiL); #endif output(OPT_base, s, "base_rel"); if (flags & OPT_extra) { char* last = (char*)&ut; extra(sysname); extra(nodename); extra(release); extra(version); extra(machine); #if _mem_idnumber_utsname extra(idnumber); #endif #if _mem_m_type_utsname extra(m_type); #endif #if _mem_base_rel_utsname extra(base_rel); #endif if (last < ((char*)(&ut + 1))) { s = t = last; while (s < (char*)(&ut + 1)) { if (!(n = *s++)) { if ((s - t) > 1) { if (sep) sfputc(sfstdout, ' '); else sep = 1; sfputr(sfstdout, t, -1); } t = s; } else if (!isprint(n)) break; } } } if (sep) sfputc(sfstdout, '\n'); } return error_info.errors; }
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; }
int b_tee(int argc, register char** argv, void* context) { register Tee_t* tp = 0; register int oflag = O_WRONLY|O_TRUNC|O_CREAT|O_BINARY; register int* hp; register char* cp; int line; if (argc <= 0) { if (context && (tp = (Tee_t*)sh_context(context)->data)) { sh_context(context)->data = 0; tee_cleanup(tp); } return 0; } cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_CALLBACK); line = -1; for (;;) { switch (optget(argv, usage)) { case 'a': oflag &= ~O_TRUNC; oflag |= O_APPEND; continue; case 'i': signal(SIGINT, SIG_IGN); continue; case 'l': line = sfset(sfstdout, 0, 0) & SF_LINE; if ((line == 0) == (opt_info.num == 0)) line = -1; else sfset(sfstdout, SF_LINE, !!opt_info.num); continue; case ':': error(2, "%s", opt_info.arg); break; case '?': error(ERROR_usage(2), "%s", opt_info.arg); break; } break; } if (error_info.errors) error(ERROR_usage(2), "%s", optusage(NiL)); argv += opt_info.index; argc -= opt_info.index; #if _ANCIENT_BSD_COMPATIBILITY if (*argv && streq(*argv, "-")) { signal(SIGINT, SIG_IGN); argv++; argc--; } #endif if (argc > 0) { if (tp = (Tee_t*)stakalloc(sizeof(Tee_t) + argc * sizeof(int))) { memset(&tp->disc, 0, sizeof(tp->disc)); tp->disc.writef = tee_write; if (context) sh_context(context)->data = (void*)tp; tp->line = line; hp = tp->fd; while (cp = *argv++) { if ((*hp = open(cp, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0) error(ERROR_system(0), "%s: cannot create", cp); else hp++; } if (hp == tp->fd) tp = 0; else { *hp = -1; sfdisc(sfstdout, &tp->disc); } } else error(ERROR_exit(0), "out of space"); } if ((sfmove(sfstdin, sfstdout, SF_UNBOUND, -1) < 0 || !sfeof(sfstdin)) && errno != EPIPE) error(ERROR_system(0), "read error"); if (sfsync(sfstdout)) error(ERROR_system(0), "write error"); tee_cleanup(tp); return error_info.errors; }
main(int argc, char *argv[]) { register int n; register char *cp; Sfio_t *in, *out; error_info.id = argv[0]; NoP(id[0]); #if _AST_VERSION >= 20060701L cmdinit(argc, argv,0, NULL, 0); #else cmdinit(argv,0, NULL, 0); #endif while (n = optget(argv, " [file ...]")) switch (n) { case ':': error(2, opt_info.arg); break; case '?': error(ERROR_usage(2), opt_info.arg); break; } argv += opt_info.index; if(error_info.errors) error(ERROR_usage(2),optusage((char*)0)); if(cp = *argv) argv++; do { if(!cp || strcmp(cp,"-")==0) { in = sfstdin; out = sfstdout; } else { if(!(in = sfopen((Sfio_t*)0,cp,"rb"))) { error(ERROR_system(0),"%s: cannot open for input",cp); error_info.errors = 1; continue; } if(!(out = sfopen((Sfio_t*)0,cp,"r+b"))) { error(ERROR_system(0),"%s: cannot open for writing",cp); error_info.errors = 1; continue; } } nocrnl(in,out); if(in!=sfstdin) sfclose(in); if(out!=sfstdout) { sfsync(out); ftruncate(sffileno(out),sftell(out)); sfclose(out); } } while(cp= *argv++); return(error_info.errors); }
int b_dlls(int argc, char** argv, Shbltin_t* context) { int i; int r; int flags; int only; unsigned long ver; char** syms; char* arg[3]; void* dll; void* sym; Dllscan_t* dls; Dllent_t* dle; Dllinfo_t* dli; cmdinit(argc, argv, context, ERROR_CATALOG, 0); flags = 0; only = 0; for (;;) { switch (optget(argv, usage)) { case 0: break; case 'b': flags |= LIST_BASE; continue; case 'c': only = 1; continue; case 'i': flags |= LIST_INFO; continue; case 'l': flags |= LIST_LONG; continue; case 'p': flags |= LIST_PATH; 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) error(ERROR_usage(2), "%s", optusage(NiL)); r = 0; if (flags & LIST_INFO) { if (!(dli = dllinfo())) error(2, "cannot determine native dll info"); sfprintf(sfstdout, "sibling=(%s", dli->sibling[0]); for (i = 1; dli->sibling[i]; i++) sfprintf(sfstdout, " %s", dli->sibling[i]); sfprintf(sfstdout, ") env=%s prefix=%s suffix=%s flags=", dli->env, dli->prefix, dli->suffix); i = 0; if (dli->flags & DLL_INFO_PREVER) { i = 1; sfprintf(sfstdout, "PREVER"); } if (dli->flags & DLL_INFO_DOTVER) { if (i) sfputc(sfstdout, '|'); sfprintf(sfstdout, "DOTVER"); } sfputc(sfstdout, '\n'); } else if (!(flags & (LIST_BASE|LIST_PATH))) flags |= LIST_BASE|LIST_PATH; if (flags &= (LIST_BASE|LIST_PATH|LIST_LONG)) { for (i = 0; i < elementsof(arg); i++) if (arg[i] = *argv) argv++; if (only && !*argv) error(ERROR_usage(2), "%s", optusage(NiL)); r = 1; for (;;) { if (dls = dllsopen(arg[0], arg[1], arg[2])) { while (dle = dllsread(dls)) { r = 0; if (!only) { if (!(flags & LIST_LONG)) dll = 0; else if (dll = dlopen(dle->path, RTLD_LAZY)) ver = dllversion(dll, NiL); else ver = 0; switch (flags) { case LIST_BASE: sfprintf(sfstdout, "%s\n", dle->name); break; case LIST_BASE|LIST_LONG: sfprintf(sfstdout, "%14s %08lu\n", dle->name, ver); break; case LIST_PATH|LIST_LONG: sfprintf(sfstdout, "%08lu %s\n", ver, dle->path); break; case LIST_PATH: sfprintf(sfstdout, "%s\n", dle->path); break; case LIST_BASE|LIST_PATH: sfprintf(sfstdout, "%14s %s\n", dle->name, dle->path); break; default: sfprintf(sfstdout, "%14s %08lu %s\n", dle->name, ver, dle->path); break; } if (*(syms = argv)) { if (dll || (dll = dlopen(dle->path, RTLD_LAZY))) { do { sfprintf(sfstdout, " %14s ", *syms); if (sym = dlllook(dll, *syms)) sfprintf(sfstdout, "%p\n", sym); else sfprintf(sfstdout, "%s\n", dlerror()); } while (*++syms); dlclose(dll); } else sfprintf(sfstdout, " %s\n", dlerror()); } } else if (dll = dlopen(dle->path, RTLD_LAZY)) { i = 1; for (syms = argv; *syms; syms++) if (sym = dlllook(dll, *syms)) { if (i) { i = 0; switch (flags) { case LIST_BASE: sfprintf(sfstdout, "%s\n", dle->name); break; case LIST_PATH: sfprintf(sfstdout, "%s\n", dle->path); break; default: sfprintf(sfstdout, "%14s %s\n", dle->name, dle->path); break; } } sfprintf(sfstdout, " %14s %p\n", *syms, sym); } dlclose(dll); } } dllsclose(dls); } if (!r || !arg[0] || streq(arg[0], "-") || !arg[1] || streq(arg[1], "-")) break; arg[0] = 0; } } return r || error_info.errors; }
int b_enum(int argc, char** argv, Shbltin_t *context) #endif { bool pflag=false, iflag=false; int i,n; ssize_t sz = -1; Namval_t *np, *tp, *mp; Namarr_t *ap; char *cp,*sp; struct Enum *ep; Shell_t *shp = context->shp; struct { Optdisc_t opt; Namval_t *np; } optdisc; cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY); for (;;) { switch (optget(argv, enum_usage)) { case 'p': pflag = true; continue; case 'i': iflag = true; continue; case '?': error(ERROR_USAGE|4, "%s", opt_info.arg); break; case ':': error(2, "%s", opt_info.arg); break; } break; } argv += opt_info.index; if (error_info.errors) { error(ERROR_USAGE|2, "%s", optusage(NiL)); return 1; } if(!*argv) sh_outenum(shp,sfstdout,(Namval_t*)0); while(cp = *argv++) { if(!(np = nv_open(cp, shp->var_tree, NV_VARNAME|NV_NOADD)) || !(ap=nv_arrayptr(np)) || ap->fun || (sz=ap->nelem) < 2) error(ERROR_exit(1), "%s must name an array containing at least two elements",cp); n = stktell(shp->stk); sfprintf(shp->stk,"%s.%s%c",NV_CLASS,np->nvname,0); tp = nv_open(stkptr(shp->stk,n), shp->var_tree, NV_VARNAME); if(pflag) { sh_outenum(shp,sfstdout,tp); continue; } stkseek(shp->stk,n); n = sz; i = 0; nv_onattr(tp, NV_UINT16); nv_putval(tp, (char*)&i, NV_INTEGER); nv_putsub(np, (char*)0, 0L, ARRAY_SCAN); do { sz += strlen(nv_getval(np)); } while(nv_nextsub(np)); sz += n*sizeof(char*); if(!(ep = newof(0,struct Enum,1,sz))) error(ERROR_system(1), "out of space"); mp = nv_namptr(ep->node,0); mp->nvshell = shp; nv_setsize(mp,10); nv_onattr(mp, NV_UINT16); ep->iflag = iflag; ep->nelem = n; cp = (char*)&ep->values[n+1]; nv_putsub(np, (char*)0, 0L, ARRAY_SCAN); ep->values[n] = 0; i = 0; do { ep->values[i++] = cp; sp = nv_getval(np); n = strlen(sp); memcpy(cp,sp,n+1); cp += n+1; } while(nv_nextsub(np)); ep->hdr.dsize = sizeof(struct Enum)+sz; ep->hdr.disc = &ENUM_disc; ep->hdr.type = tp; nv_onattr(tp, NV_RDONLY); nv_disc(tp, &ep->hdr,NV_FIRST); memset(&optdisc,0,sizeof(optdisc)); optdisc.opt.infof = enuminfo; optdisc.np = tp; nv_addtype(tp, enum_type, &optdisc.opt, sizeof(optdisc)); nv_onattr(np,NV_LTOU|NV_UTOL); } nv_open(0,shp->var_tree,0); return error_info.errors != 0; }
int b_date(int argc, register char** argv, void* context) { register int n; register char* s; register Fmt_t* f; char* t; unsigned long u; Time_t now; Time_t ts; Time_t te; Time_t e; char buf[1024]; Fmt_t* fmts; Fmt_t fmt; struct stat st; char* cmd = argv[0]; /* original command path */ char* format = 0; /* tmxfmt() format */ char* string = 0; /* date string */ int elapsed = 0; /* args are start/stop pairs */ int filetime = 0; /* use this st_ time field */ int increment = 0; /* incrementally adjust time */ int last = 0; /* display the last time arg */ Tm_zone_t* listzones = 0; /* known time zone table */ int network = 0; /* don't set network time */ int show = 0; /* show date and don't set */ int unelapsed = 0; /* fmtelapsed() => strelapsed */ cmdinit(argc, argv, context, ERROR_CATALOG, 0); tm_info.flags = TM_DATESTYLE; fmts = &fmt; fmt.format = ""; fmt.next = 0; for (;;) { switch (optget(argv, usage)) { case 'a': case 'c': case 'm': filetime = opt_info.option[1]; continue; case 'd': string = opt_info.arg; show = 1; continue; case 'e': format = "%#"; continue; case 'E': elapsed = 1; continue; case 'f': format = opt_info.arg; continue; case 'i': increment = 1; continue; case 'l': tm_info.flags |= TM_LEAP; continue; case 'L': last = 1; continue; case 'n': network = 1; continue; case 'p': if (!(f = newof(0, Fmt_t, 1, 0))) error(ERROR_SYSTEM|3, "out of space [format]"); f->next = fmts; f->format = opt_info.arg; fmts = f; continue; case 's': show = 1; continue; case 'u': tm_info.flags |= TM_UTC; continue; case 'U': unelapsed = (int)opt_info.num; continue; case 'z': listzones = tm_data.zone; continue; case '?': error(ERROR_USAGE|4, "%s", opt_info.arg); continue; case ':': error(2, "%s", opt_info.arg); continue; } break; } argv += opt_info.index; if (error_info.errors) error(ERROR_USAGE|4, "%s", optusage(NiL)); now = tmxgettime(); if (listzones) { s = "-"; while (listzones->standard) { if (listzones->type) s = listzones->type; sfprintf(sfstdout, "%3s %4s %4s %4d %4d\n", s, *listzones->standard ? listzones->standard : "-", listzones->daylight ? listzones->daylight : "-", listzones->west, listzones->dst); listzones++; show = 1; } } else if (elapsed) { e = 0; while (s = *argv++) { if (!(t = *argv++)) { argv--; t = "now"; } ts = convert(fmts, s, now); te = convert(fmts, t, now); if (te > ts) e += te - ts; else e += ts - te; } sfputr(sfstdout, fmtelapsed((unsigned long)tmxsec(e), 1), '\n'); show = 1; } else if (unelapsed) { while (s = *argv++) { u = strelapsed(s, &t, unelapsed); if (*t) error(3, "%s: invalid elapsed time", s); sfprintf(sfstdout, "%lu\n", u); } show = 1; } else if (filetime) { if (!*argv) error(ERROR_USAGE|4, "%s", optusage(NiL)); n = argv[1] != 0; while (s = *argv++) { if (stat(s, &st)) error(2, "%s: not found", s); else { switch (filetime) { case 'a': now = tmxgetatime(&st); break; case 'c': now = tmxgetctime(&st); break; default: now = tmxgetmtime(&st); break; } tmxfmt(buf, sizeof(buf), format, now); if (n) sfprintf(sfstdout, "%s: %s\n", s, buf); else sfprintf(sfstdout, "%s\n", buf); show = 1; } } } else { if ((s = *argv) && !format && *s == '+') { format = s + 1; argv++; s = *argv; } if (s || (s = string)) { if (*argv && string) error(ERROR_USAGE|4, "%s", optusage(NiL)); now = convert(fmts, s, now); if (*argv && (s = *++argv)) { show = 1; do { if (!last) { tmxfmt(buf, sizeof(buf), format, now); sfprintf(sfstdout, "%s\n", buf); } now = convert(fmts, s, now); } while (s = *++argv); } } else show = 1; if (format || show) { tmxfmt(buf, sizeof(buf), format, now); sfprintf(sfstdout, "%s\n", buf); } else if (settime(context, cmd, now, increment, network)) error(ERROR_SYSTEM|3, "cannot set system time"); } while (fmts != &fmt) { f = fmts; fmts = fmts->next; free(f); } tm_info.flags = 0; if (show && sfsync(sfstdout)) error(ERROR_system(0), "write error"); return error_info.errors != 0; }
int b_enum(int argc, char **argv, Shbltin_t *context) { bool pflag = false, iflag = false; int i, n; ssize_t sz = -1; Namval_t *np, *tp, *mp; Namarr_t *ap; char *cp; const char *sp; struct Enum *ep; Shell_t *shp = context->shp; struct { Optdisc_t opt; Namval_t *np; } optdisc; if (cmdinit(argc, argv, context, ERROR_NOTIFY)) return -1; while ((n = optget(argv, enum_usage))) { switch (n) { case 'p': { pflag = true; break; } case 'i': { iflag = true; break; } case ':': { errormsg(SH_DICT, 2, "%s", opt_info.arg); break; } case '?': { errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg); __builtin_unreachable(); } default: { break; } } } argv += opt_info.index; argc -= opt_info.index; if (error_info.errors || argc != 1) { error(ERROR_USAGE | 2, "%s", optusage(NULL)); return 1; } while ((cp = *argv++)) { np = nv_open(cp, shp->var_tree, NV_VARNAME | NV_NOADD); if (!np || !(ap = nv_arrayptr(np)) || ap->fun || ap->nelem < 2) { error(ERROR_exit(1), "%s must name an array containing at least two elements", cp); } n = stktell(shp->stk); sfprintf(shp->stk, "%s.%s%c", NV_CLASS, np->nvname, 0); tp = nv_open(stkptr(shp->stk, n), shp->var_tree, NV_VARNAME); if (pflag) { sh_outenum(shp, sfstdout, tp); continue; } stkseek(shp->stk, n); n = ap->nelem; i = 0; nv_onattr(tp, NV_UINT16); nv_putval(tp, (char *)&i, NV_INTEGER); nv_putsub(np, NULL, 0L, ARRAY_SCAN); do { sz += strlen(nv_getval(np)); } while (nv_nextsub(np)); ep = calloc(1, sizeof(struct Enum)); if (!ep) { error(ERROR_system(1), "out of space"); __builtin_unreachable(); } ep->nelem = n; mp = nv_namptr(ep->node, 0); mp->nvshell = shp; nv_setsize(mp, 10); nv_onattr(mp, NV_UINT16); ep->iflag = iflag; ep->values = malloc(n * sizeof(*ep->values)); nv_putsub(np, NULL, 0L, ARRAY_SCAN); i = 0; do { sp = nv_getval(np); ep->values[i++] = strdup(sp); } while (nv_nextsub(np)); assert(n == i); ep->namfun.dsize = sizeof(struct Enum); ep->namfun.disc = &ENUM_disc; ep->namfun.type = tp; nv_onattr(tp, NV_RDONLY); nv_disc(tp, &ep->namfun, DISC_OP_FIRST); memset(&optdisc, 0, sizeof(optdisc)); optdisc.opt.infof = enuminfo; optdisc.np = tp; nv_addtype(tp, enum_type, &optdisc, sizeof(optdisc)); nv_onattr(np, NV_LTOU | NV_UTOL); } nv_open(0, shp->var_tree, 0); return error_info.errors != 0; }
int b_paste(int argc, char** argv, Shbltin_t* context) { register int n, sflag=0; register Sfio_t *fp, **streams; register char *cp, *delim; char *ep; Delim_t *mp; int dlen, dsiz; char defdelim[2]; cmdinit(argc, argv, context, ERROR_CATALOG, 0); delim = 0; for (;;) { switch (optget(argv, usage)) { case 'd': delim = opt_info.arg; continue; case 's': sflag++; 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) error(ERROR_usage(2),"%s", optusage(NiL)); if(!delim || !*delim) { delim = defdelim; delim[0] = '\t'; delim[1] = 0; } if (!(delim = strdup(delim))) error(ERROR_system(1), "out of space"); dlen = dsiz = stresc(delim); mp = 0; if (mbwide()) { cp = delim; ep = delim + dlen; dlen = 0; while (cp < ep) { mbchar(cp); dlen++; } if(dlen < dsiz) { if (!(mp = newof(0, Delim_t, dlen, 0))) { free(delim); error(ERROR_system(1), "out of space"); } cp = delim; dlen = 0; while (cp < ep) { mp[dlen].chr = cp; mbchar(cp); mp[dlen].len = cp - mp[dlen].chr; dlen++; } } } if(cp = *argv) { n = argc - opt_info.index; argv++; } else n = 1; if(!sflag) { if (!(streams = (Sfio_t**)stakalloc(n*sizeof(Sfio_t*)))) error(ERROR_exit(1), "out of space"); n = 0; } do { if(!cp || streq(cp,"-")) fp = sfstdin; else if(!(fp = sfopen(NiL,cp,"r"))) error(ERROR_system(0),"%s: cannot open",cp); if(fp && sflag) { if(spaste(fp,sfstdout,delim,dsiz,dlen,mp) < 0) error(ERROR_system(0),"write failed"); if(fp!=sfstdin) sfclose(fp); } else if(!sflag) streams[n++] = fp; } while(cp= *argv++); if(!sflag) { if(error_info.errors==0 && paste(n,streams,sfstdout,delim,dsiz,dlen,mp) < 0) error(ERROR_system(0),"write failed"); while(--n>=0) if((fp=streams[n]) && fp!=sfstdin) sfclose(fp); } if (mp) free(mp); free(delim); return(error_info.errors); }
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_cut(int argc, char** argv, void* context) { register char* cp = 0; register Sfio_t* fp; char* s; int n; Cut_t* cut; int mode = 0; Delim_t wdelim; Delim_t ldelim; size_t reclen = 0; cmdinit(argc, argv, context, ERROR_CATALOG, 0); wdelim.chr = '\t'; ldelim.chr = '\n'; wdelim.len = ldelim.len = 1; for (;;) { switch (n = optget(argv, usage)) { case 0: break; case 'b': case 'c': if(mode&C_FIELDS) { error(2, "f option already specified"); continue; } cp = opt_info.arg; if(n=='b') mode |= C_BYTES; else mode |= C_CHARS; continue; case 'D': ldelim.str = opt_info.arg; if (mbwide()) { s = opt_info.arg; ldelim.chr = mbchar(s); if ((n = s - opt_info.arg) > 1) { ldelim.len = n; continue; } } ldelim.chr = *(unsigned char*)opt_info.arg; ldelim.len = 1; continue; case 'd': wdelim.str = opt_info.arg; if (mbwide()) { s = opt_info.arg; wdelim.chr = mbchar(s); if ((n = s - opt_info.arg) > 1) { wdelim.len = n; continue; } } wdelim.chr = *(unsigned char*)opt_info.arg; wdelim.len = 1; continue; case 'f': if(mode&(C_CHARS|C_BYTES)) { error(2, "c option already specified"); continue; } cp = opt_info.arg; mode |= C_FIELDS; continue; case 'n': mode |= C_NOSPLIT; continue; case 'N': mode |= C_NONEWLINE; continue; case 'R': case 'r': if(opt_info.num>0) reclen = opt_info.num; continue; case 's': mode |= C_SUPRESS; 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) error(ERROR_usage(2), "%s",optusage(NiL)); if(!cp) { error(2, "b, c or f option must be specified"); error(ERROR_usage(2), "%s", optusage(NiL)); } if(!*cp) error(3, "non-empty b, c or f option must be specified"); if((mode & (C_FIELDS|C_SUPRESS)) == C_SUPRESS) error(3, "s option requires f option"); cut = cutinit(mode, cp, &wdelim, &ldelim, reclen); if(cp = *argv) argv++; do { if(!cp || streq(cp,"-")) fp = sfstdin; else if(!(fp = sfopen(NiL,cp,"r"))) { error(ERROR_system(0),"%s: cannot open",cp); continue; } if(mode&C_FIELDS) cutfields(cut,fp,sfstdout); else cutcols(cut,fp,sfstdout); if(fp!=sfstdin) sfclose(fp); } while(cp = *argv++); if (sfsync(sfstdout)) error(ERROR_system(0), "write error"); return error_info.errors != 0; }
int b_fmt(int argc, char** argv, void *context) { register int n; char* cp; Fmt_t fmt; char outbuf[8 * 1024]; fmt.flags = 0; fmt.out = sfstdout; fmt.outbuf = outbuf; fmt.outp = 0; fmt.endbuf = &outbuf[72]; fmt.indent = 0; fmt.nextdent = 0; fmt.nwords = 0; fmt.prefix = 0; fmt.quote = 0; fmt.retain = 0; fmt.section = 1; cmdinit(argc, argv, context, ERROR_CATALOG, 0); for (;;) { switch (n = optget(argv, usage)) { case 'c': case 'o': case 's': case 'u': setoption(&fmt, n); continue; case 'w': if (opt_info.num < TABSZ || opt_info.num>= sizeof(outbuf)) error(2, "width out of range"); fmt.endbuf = &outbuf[opt_info.num]; 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) error(ERROR_usage(2), "%s", optusage(NiL)); if (isoption(&fmt, 'o')) setoption(&fmt, 'c'); if (isoption(&fmt, 's')) clroption(&fmt, 'u'); if (cp = *argv) argv++; do { if (!cp || streq(cp, "-")) fmt.in = sfstdin; else if (!(fmt.in = sfopen(NiL, cp, "r"))) { error(ERROR_system(0), "%s: cannot open", cp); error_info.errors = 1; continue; } dofmt(&fmt); if (fmt.in != sfstdin) sfclose(fmt.in); } while (cp = *argv++); outline(&fmt); if (sfsync(sfstdout)) error(ERROR_system(0), "write error"); return error_info.errors != 0; }
int b_fold(int argc, char** argv, Shbltin_t* context) { register int n, width=WIDTH; register Sfio_t *fp; register char *cp; char *cont="\n"; size_t contsize = 1; char cols[1<<CHAR_BIT]; cmdinit(argc, argv, context, ERROR_CATALOG, 0); memset(cols, 0, sizeof(cols)); cols['\t'] = T_TAB; cols['\b'] = T_BS; cols['\n'] = T_NL; cols['\r'] = T_RET; for (;;) { switch (optget(argv, usage)) { case 'b': cols['\r'] = cols['\b'] = 0; cols['\t'] = cols[' ']; continue; case 'c': contsize = stresc(cont = strdup(opt_info.arg)); continue; case 'd': if (n = *opt_info.arg) cols[n] = T_SP; continue; case 's': cols[' '] = T_SP; if(cols['\t']==0) cols['\t'] = T_SP; continue; case 'w': if ((width = opt_info.num) <= 0) error(2, "%d: width must be positive", opt_info.num); continue; case ':': error(2, "%s", opt_info.arg); continue; case '?': error(ERROR_usage(2), "%s", opt_info.arg); continue; } break; } argv += opt_info.index; argc -= opt_info.index; if(error_info.errors) error(ERROR_usage(2),"%s", optusage(NiL)); if(cp = *argv) argv++; do { if(!cp || streq(cp,"-")) fp = sfstdin; else if(!(fp = sfopen(NiL,cp,"r"))) { error(ERROR_system(0),"%s: cannot open",cp); error_info.errors = 1; continue; } fold(fp,sfstdout,width,cont,contsize,cols); if(fp!=sfstdin) sfclose(fp); } while(cp= *argv++); return(error_info.errors); }
int b_mime(int argc, char** argv, Shbltin_t* context) { Mime_t* mp; char* s; Sfio_t* sp; int silent; Mimedisc_t disc; cmdinit(argc, argv, context, ERROR_CATALOG, 0); silent = 0; for (;;) { switch (optget(argv, usage)) { case 0: break; case 's': silent = 1; continue; case ':': error(2, "%s", opt_info.arg); continue; case '?': error(ERROR_usage(2), "%s", opt_info.arg); continue; } break; } argc -= opt_info.index; argv += opt_info.index; if (error_info.errors || argc > 1 && argc < 3) error(ERROR_usage(2), "%s", optusage(NiL)); disc.version = MIME_VERSION; disc.flags = 0; disc.errorf = errorf; if (!(mp = mimeopen(&disc))) error(ERROR_exit(1), "mime library error"); if (mimeload(mp, NiL, 0)) { mimeclose(mp); error(ERROR_exit(1), "mime load error"); } if (argc <= 1) mimelist(mp, sfstdout, argv[0]); else if (!(sp = sfstropen())) error(ERROR_exit(1), "out of space"); else { for (argc = 3; s = argv[argc]; argc++) sfputr(sp, s, ';'); if (!(s = sfstruse(sp))) error(ERROR_exit(1), "out of space"); if (!(s = mimeview(mp, argv[0], argv[1], argv[2], s))) { if (silent) exit(1); error(ERROR_exit(1), "no %s view for mime type %s", argv[0], argv[2]); } else sfputr(sfstdout, s, '\n'); sfclose(sp); } mimeclose(mp); return 0; }
int b_rm(int argc, register char** argv, void* context) { State_t state; FTS* fts; FTSENT* ent; int set3d; cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY); memset(&state, 0, sizeof(state)); state.context = context; state.fs3d = fs3d(FS3D_TEST); state.terminal = isatty(0); for (;;) { switch (optget(argv, usage)) { case 'd': state.directory = 1; continue; case 'f': state.force = 1; state.interactive = 0; continue; case 'i': state.interactive = 1; state.force = 0; continue; case 'r': case 'R': state.recursive = 1; continue; case 'F': #if _lib_fsync state.clobber = 1; #else error(1, "%s not implemented on this system", opt_info.name); #endif continue; case 'u': state.unconditional = 1; continue; case 'v': state.verbose = 1; continue; case '?': error(ERROR_USAGE|4, "%s", opt_info.arg); break; case ':': error(2, "%s", opt_info.arg); break; } break; } argv += opt_info.index; if (*argv && streq(*argv, "-") && !streq(*(argv - 1), "--")) argv++; if (error_info.errors || !*argv) error(ERROR_USAGE|4, "%s", optusage(NiL)); /* * do it */ if (state.interactive) state.verbose = 0; state.uid = geteuid(); state.unconditional = state.unconditional && state.recursive && state.force; if (state.recursive && state.fs3d) { set3d = state.fs3d; state.fs3d = 0; fs3d(0); } else set3d = 0; if (fts = fts_open(argv, FTS_PHYSICAL, NiL)) { while (!sh_checksig(context) && (ent = fts_read(fts)) && !rm(&state, ent)); fts_close(fts); } else if (!state.force) error(ERROR_SYSTEM|2, "%s: cannot remove", argv[0]); if (set3d) fs3d(set3d); return error_info.errors != 0; }
int b_stty(int argc, char** argv, Shbltin_t* context) { struct termios tty; register int n; register int flags = 0; int fd = 0; const Tty_t* tp; Optdisc_t disc; cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_INTERACTIVE); memset(&disc, 0, sizeof(disc)); disc.version = OPT_VERSION; disc.infof = infof; opt_info.disc = &disc; for (;;) { switch (n = optget(argv, usage)) { case 'f': fd = (int)opt_info.num; continue; case 'a': case 'g': case 't': if (!opt_info.offset || !argv[opt_info.index][opt_info.offset]) { switch (n) { case 'a': flags |= A_FLAG; break; case 'g': flags |= G_FLAG; break; case 't': flags |= T_FLAG; break; } continue; } /*FALLTHROUGH*/ case ':': if (!opt_info.offset) error(2, "%s", opt_info.arg); else if (!(tp = lookup(argv[opt_info.index]+1)) || (tp->type != BIT && tp->type != TABS)) error(ERROR_exit(1), "%s: unknown mode", argv[opt_info.index]); break; case '?': error(ERROR_usage(2), "%s", opt_info.arg); break; } break; } argv += opt_info.index; if (error_info.errors || (flags && *argv) || (flags&(flags-1))) error(ERROR_usage(2), "%s", optusage(NiL)); if (tcgetattr(fd, &tty) < 0) error(ERROR_system(1), "not a tty"); if (flags & T_FLAG) sfprintf(sfstdout, "%d\n", tcgetpgrp(0)); else if (*argv) { if (!argv[1] && **argv == ':') gin(*argv, &tty); else set(argv, &tty); if (tcsetattr(0, TCSANOW, &tty) < 0) error(ERROR_system(1), "cannot set tty"); } else output(&tty, flags); return error_info.errors; }