uint16_t Charstats::calculateaccuracy() const { float dexacc = static_cast<float>(getstat(MS_DEX)) * 0.8f; float lukacc = static_cast<float>(getstat(MS_LUK)) * 0.5f; return static_cast<uint16_t>(dexacc + lukacc); }
/* TODO return a string, "?" if stat fails */ nlink_t getlinkcount(File *file) { struct stat *pstat = getstat(file); if (!pstat) return 0; return pstat->st_nlink; }
/* * The out of control system call * This is audit kitchen sink aka auditadm, aka auditon */ int auditctl( int cmd, caddr_t data, int length) { int result; switch (cmd) { case A_GETAMASK: case A_GETCOND: case A_GETCAR: case A_GETCLASS: case A_GETCWD: case A_GETKAUDIT: case A_GETKMASK: case A_GETPINFO: case A_GETPINFO_ADDR: case A_GETPOLICY: case A_GETQCTRL: case A_GETSTAT: if (secpolicy_audit_getattr(CRED(), B_FALSE) != 0) return (EPERM); break; default: if (secpolicy_audit_config(CRED()) != 0) return (EPERM); break; } switch (cmd) { case A_GETPOLICY: result = getpolicy(data); break; case A_SETPOLICY: result = setpolicy(data); break; case A_GETAMASK: result = getamask(data); break; case A_SETAMASK: result = setamask(data); break; case A_GETKMASK: result = getkmask(data); break; case A_SETKMASK: result = setkmask(data); break; case A_GETKAUDIT: result = getkaudit(data, length); break; case A_SETKAUDIT: result = setkaudit(data, length); break; case A_GETQCTRL: result = getqctrl(data); break; case A_SETQCTRL: result = setqctrl(data); break; case A_GETCWD: result = getcwd(data, length); break; case A_GETCAR: result = getcar(data, length); break; case A_GETSTAT: result = getstat(data); break; case A_SETSTAT: result = setstat(data); break; case A_SETUMASK: result = setumask(data); break; case A_SETSMASK: result = setsmask(data); break; case A_GETCOND: result = getcond(data); break; case A_SETCOND: result = setcond(data); break; case A_GETCLASS: result = getclass(data); break; case A_SETCLASS: result = setclass(data); break; case A_GETPINFO: result = getpinfo(data); break; case A_GETPINFO_ADDR: result = getpinfo_addr(data, length); break; case A_SETPMASK: result = setpmask(data); break; default: result = EINVAL; break; } return (result); }
BCU1SerialLowLevelDriver::BCU1SerialLowLevelDriver (const char *dev, Trace * tr) { struct termios ti; t = tr; pth_sem_init (&in_signal); pth_sem_init (&out_signal); pth_sem_init (&send_empty); pth_sem_set_value (&send_empty, 1); getwait = pth_event (PTH_EVENT_SEM, &out_signal); TRACEPRINTF (t, 1, this, "Open"); fd = open (dev, O_RDWR | O_NOCTTY | O_NDELAY | O_SYNC); if (fd == -1) return; set_low_latency (fd, &sold); close (fd); fd = open (dev, O_RDWR | O_NOCTTY | O_NDELAY | O_SYNC); if (fd == -1) return; if (tcgetattr (fd, &told)) { restore_low_latency (fd, &sold); close (fd); fd = -1; return; } if (tcgetattr (fd, &ti)) { restore_low_latency (fd, &sold); close (fd); fd = -1; return; } ti.c_cflag = CS8 | CLOCAL | CREAD; ti.c_iflag = IGNBRK | INPCK | ISIG; ti.c_oflag = 0; ti.c_lflag = 0; ti.c_cc[VTIME] = 1; ti.c_cc[VMIN] = 0; cfsetospeed (&ti, B9600); cfsetispeed (&ti, 0); if (tcsetattr (fd, TCSAFLUSH, &ti)) { restore_low_latency (fd, &sold); close (fd); fd = -1; return; } setstat (getstat () & ~(TIOCM_RTS | TIOCM_CTS)); while ((getstat () & TIOCM_CTS)); Start (); TRACEPRINTF (t, 1, this, "Opened"); }
/* TODO return a string instead of long, "?" on failure */ long getsize(File *file) { struct stat *pstat = getstat(file); if (!pstat) return -1; return (long)pstat->st_size; }
APR_DECLARE(apr_status_t) apr_stat(apr_finfo_t *finfo, const char *fname, apr_int32_t wanted, apr_pool_t *pool) { struct stat info; int srv; NXPathCtx_t pathCtx = 0; getcwdpath(NULL, &pathCtx, CTX_ACTUAL_CWD); #ifdef APR_HAS_PSA srv = getstat(pathCtx, (char*)fname, &info, ST_STAT_BITS|ST_NAME_BIT); #else srv = cstat(pathCtx, (char*)fname, &info, ST_STAT_BITS|ST_NAME_BIT, pool); #endif errno = srv; if (srv == 0) { finfo->pool = pool; finfo->fname = fname; fill_out_finfo(finfo, &info, wanted); if (wanted & APR_FINFO_LINK) wanted &= ~APR_FINFO_LINK; if (wanted & APR_FINFO_NAME) { finfo->name = apr_pstrdup(pool, info.st_name); finfo->valid |= APR_FINFO_NAME; } return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS; } else { #if !defined(ENOENT) || !defined(ENOTDIR) #error ENOENT || ENOTDIR not defined; please see the #error comments at this line in the source for a workaround. /* * If ENOENT || ENOTDIR is not defined in one of the your OS's * include files, APR cannot report a good reason why the stat() * of the file failed; there are cases where it can fail even though * the file exists. This opens holes in Apache, for example, because * it becomes possible for someone to get a directory listing of a * directory even though there is an index (eg. index.html) file in * it. If you do not have a problem with this, delete the above * #error lines and start the compile again. If you need to do this, * please submit a bug report to http://www.apache.org/bug_report.html * letting us know that you needed to do this. Please be sure to * include the operating system you are using. */ /* WARNING: All errors will be handled as not found */ #if !defined(ENOENT) return APR_ENOENT; #else /* WARNING: All errors but not found will be handled as not directory */ if (errno != ENOENT) return APR_ENOENT; else return errno; #endif #else /* All was defined well, report the usual: */ return errno; #endif } }
bool isfifo(File *file) { struct stat *pstat = getstat(file); return pstat && S_ISFIFO(pstat->st_mode); }
/* TODO return a string, "?" on error */ time_t getmtime(File *file) { struct stat *pstat = getstat(file); if (!pstat) return 0; return pstat->st_mtime; }
bool isdir(File *file) { struct stat *pstat = getstat(file); return pstat && S_ISDIR(pstat->st_mode); }
bool isexec(File *file) { struct stat *pstat = getstat(file); return pstat && pstat->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH); }
bool ischardev(File *file) { struct stat *pstat = getstat(file); return pstat && S_ISCHR(pstat->st_mode); }
bool isblockdev(File *file) { struct stat *pstat = getstat(file); return pstat && S_ISBLK(pstat->st_mode); }
int64_t Charstats::getexpneeded() const { return exptable[getstat(MS_LEVEL)]; }
char *getmodes(File *file) { char *unknownmodes = "???????????"; char *modes = malloc((strlen(unknownmodes) + 1) * sizeof(*modes)); if (!modes) { return strdup(unknownmodes); } struct stat *pstat = getstat(file); if (!pstat) { free(modes); return strdup(unknownmodes); } char *p = modes; if (islink(file)) *p++ = 'l'; else if (isdir(file)) *p++ = 'd'; else if (isblockdev(file)) *p++ = 'b'; else if (ischardev(file)) *p++ = 'c'; else if (isfifo(file)) *p++ = 'p'; else if (issock(file)) *p++ = 's'; else *p++ = '-'; if (pstat->st_mode & S_IRUSR) *p++ = 'r'; else *p++ = '-'; if (pstat->st_mode & S_IWUSR) *p++ = 'w'; else *p++ = '-'; if (issetuid(file)) { if (pstat->st_mode & S_IXUSR) *p++ = 's'; else *p++ = 'S'; } else { if (pstat->st_mode & S_IXUSR) *p++ = 'x'; else *p++ = '-'; } if (pstat->st_mode & S_IRGRP) *p++ = 'r'; else *p++ = '-'; if (pstat->st_mode & S_IWGRP) *p++ = 'w'; else *p++ = '-'; if (issetgid(file)) { if (pstat->st_mode & S_IXGRP) *p++ = 's'; else *p++ = 'S'; } else { if (pstat->st_mode & S_IXGRP) *p++ = 'x'; else *p++ = '-'; } if (pstat->st_mode & S_IROTH) *p++ = 'r'; else *p++ = '-'; if (pstat->st_mode & S_IWOTH) *p++ = 'w'; else *p++ = '-'; if (issticky(file)) { if (pstat->st_mode & S_IXOTH) *p++ = 't'; else *p++ = 'T'; } else { if (pstat->st_mode & S_IXOTH) *p++ = 'x'; else *p++ = '-'; } /* POSIX says we should print a space if there are no extended ACLs, GNU ls prints nothing follow POSIX */ if (!islink(file) && hasacls(file)) *p++ = '+'; else *p++ = ' '; *p++ = '\0'; return modes; }
bool islink(File *file) { struct stat *pstat = getstat(file); return pstat && S_ISLNK(pstat->st_mode); }
/* TODO return a string, "?" on error */ ino_t getinode(File *file) { struct stat *pstat = getstat(file); if (!pstat) return 0; return pstat->st_ino; }
bool issetuid(File *file) { struct stat *pstat = getstat(file); return pstat && pstat->st_mode & S_ISUID; }
/* TODO return a string instead of uid_t, "?" on failure */ uid_t getownernum(File *file) { struct stat *stat = getstat(file); if (!stat) return 0; return stat->st_uid; }
bool issock(File *file) { struct stat *pstat = getstat(file); return pstat && S_ISSOCK(pstat->st_mode); }
int cstat (NXPathCtx_t ctx, char *path, struct stat *buf, unsigned long requestmap, apr_pool_t *p) { apr_pool_t *gPool = (apr_pool_t *)getGlobalPool(); apr_hash_t *statCache = NULL; apr_thread_rwlock_t *rwlock = NULL; NXPathCtx_t pathctx = 0; char *ptr = NULL, *tr; int len = 0, x; char *ppath; char *pinfo; if (ctx == 1) { /* If there isn't a global pool then just stat the file and return */ if (!gPool) { char poolname[50]; if (apr_pool_create(&gPool, NULL) != APR_SUCCESS) { return getstat(ctx, path, buf, requestmap); } setGlobalPool(gPool); apr_pool_tag(gPool, apr_pstrdup(gPool, "cstat_mem_pool")); statCache = apr_hash_make(gPool); apr_pool_userdata_set ((void*)statCache, "STAT_CACHE", stat_cache_cleanup, gPool); apr_thread_rwlock_create(&rwlock, gPool); apr_pool_userdata_set ((void*)rwlock, "STAT_CACHE_LOCK", apr_pool_cleanup_null, gPool); } else { apr_pool_userdata_get((void**)&statCache, "STAT_CACHE", gPool); apr_pool_userdata_get((void**)&rwlock, "STAT_CACHE_LOCK", gPool); } if (!gPool || !statCache || !rwlock) { return getstat(ctx, path, buf, requestmap); } for (x = 0,tr = path;*tr != '\0';tr++,x++) { if (*tr == '\\' || *tr == '/') { ptr = tr; len = x; } if (*tr == ':') { ptr = "\\"; len = x; } } if (ptr) { ppath = apr_pstrndup (p, path, len); strlwr(ppath); if (ptr[1] != '\0') { ptr++; } /* If the path ended in a trailing slash then our result path will be a single slash. To avoid stat'ing the root with a slash, we need to make sure we stat the current directory with a dot */ if (((*ptr == '/') || (*ptr == '\\')) && (*(ptr+1) == '\0')) { pinfo = apr_pstrdup (p, "."); } else { pinfo = apr_pstrdup (p, ptr); } } /* If we have a statCache then try to pull the information from the cache. Otherwise just stat the file and return.*/ if (statCache) { apr_thread_rwlock_rdlock(rwlock); pathctx = (NXPathCtx_t) apr_hash_get(statCache, ppath, APR_HASH_KEY_STRING); apr_thread_rwlock_unlock(rwlock); if (pathctx) { return getstat(pathctx, pinfo, buf, requestmap); } else { int err; err = NXCreatePathContext(0, ppath, 0, NULL, &pathctx); if (!err) { apr_thread_rwlock_wrlock(rwlock); apr_hash_set(statCache, apr_pstrdup(gPool,ppath) , APR_HASH_KEY_STRING, (void*)pathctx); apr_thread_rwlock_unlock(rwlock); return getstat(pathctx, pinfo, buf, requestmap); } } } } return getstat(ctx, path, buf, requestmap); }
bool issticky(File *file) { struct stat *pstat = getstat(file); return pstat && pstat->st_mode & S_ISVTX; }
int evalcond(Estate state, char *fromtest) { struct stat *st; char *left, *right, *overridename, overridebuf[13]; Wordcode pcode; wordcode code; int ctype, htok = 0, ret; rec: left = right = overridename = NULL; pcode = state->pc++; code = *pcode; ctype = WC_COND_TYPE(code); switch (ctype) { case COND_NOT: if (tracingcond) fprintf(xtrerr, " %s", condstr[ctype]); ret = evalcond(state, fromtest); if (ret == 2) return ret; else return !ret; case COND_AND: if (!(ret = evalcond(state, fromtest))) { if (tracingcond) fprintf(xtrerr, " %s", condstr[ctype]); goto rec; } else { state->pc = pcode + (WC_COND_SKIP(code) + 1); return ret; } case COND_OR: if ((ret = evalcond(state, fromtest)) == 1) { if (tracingcond) fprintf(xtrerr, " %s", condstr[ctype]); goto rec; } else { state->pc = pcode + (WC_COND_SKIP(code) + 1); return ret; } case COND_REGEX: { char *modname = isset(REMATCHPCRE) ? "zsh/pcre" : "zsh/regex"; sprintf(overridename = overridebuf, "-%s-match", modname+4); (void)ensurefeature(modname, "C:", overridename+1); ctype = COND_MODI; } /*FALLTHROUGH*/ case COND_MOD: case COND_MODI: { Conddef cd; char *name = overridename, *errname; char **strs; int l = WC_COND_SKIP(code); if (name == NULL) name = ecgetstr(state, EC_NODUP, NULL); if (ctype == COND_MOD) strs = ecgetarr(state, l, EC_DUP, NULL); else { char *sbuf[3]; sbuf[0] = ecgetstr(state, EC_NODUP, NULL); sbuf[1] = ecgetstr(state, EC_NODUP, NULL); sbuf[2] = NULL; strs = arrdup(sbuf); l = 2; } if (name && name[0] == '-') errname = name; else if (strs[0] && *strs[0] == '-') errname = strs[0]; else errname = "<null>"; if (name && name[0] == '-' && (cd = getconddef((ctype == COND_MODI), name + 1, 1))) { if (ctype == COND_MOD && (l < cd->min || (cd->max >= 0 && l > cd->max))) { zwarnnam(fromtest, "unknown condition: %s", name); return 2; } if (tracingcond) tracemodcond(name, strs, ctype == COND_MODI); return !cd->handler(strs, cd->condid); } else { char *s = strs[0]; if (overridename) { /* * Standard regex function not available: this * is a hard error. */ zerrnam(fromtest, "%s not available for regex", overridename); return 2; } strs[0] = dupstring(name); name = s; if (name && name[0] == '-' && (cd = getconddef(0, name + 1, 1))) { if (l < cd->min || (cd->max >= 0 && l > cd->max)) { zwarnnam(fromtest, "unknown condition: %s", errname); return 2; } if (tracingcond) tracemodcond(name, strs, ctype == COND_MODI); return !cd->handler(strs, cd->condid); } else { zwarnnam(fromtest, "unknown condition: %s", errname); } } /* module not found, error */ return 2; } } left = ecgetstr(state, EC_DUPTOK, &htok); if (htok) { cond_subst(&left, !fromtest); untokenize(left); } if (ctype <= COND_GE && ctype != COND_STREQ && ctype != COND_STRNEQ) { right = ecgetstr(state, EC_DUPTOK, &htok); if (htok) { cond_subst(&right, !fromtest); untokenize(right); } } if (tracingcond) { if (ctype < COND_MOD) { fputc(' ',xtrerr); quotedzputs(left, xtrerr); fprintf(xtrerr, " %s ", condstr[ctype]); if (ctype == COND_STREQ || ctype == COND_STRNEQ) { char *rt = dupstring(ecrawstr(state->prog, state->pc, NULL)); cond_subst(&rt, !fromtest); quote_tokenized_output(rt, xtrerr); } else quotedzputs((char *)right, xtrerr); } else { fprintf(xtrerr, " -%c ", ctype); quotedzputs(left, xtrerr); } } if (ctype >= COND_EQ && ctype <= COND_GE) { mnumber mn1, mn2; if (fromtest) { /* * For test and [, the expressions must be base 10 integers, * not integer expressions. */ char *eptr, *err; mn1.u.l = zstrtol(left, &eptr, 10); if (!*eptr) { mn2.u.l = zstrtol(right, &eptr, 10); err = right; } else err = left; if (*eptr) { zwarnnam(fromtest, "integer expression expected: %s", err); return 2; } mn1.type = mn2.type = MN_INTEGER; } else { mn1 = matheval(left); mn2 = matheval(right); } if (((mn1.type|mn2.type) & (MN_INTEGER|MN_FLOAT)) == (MN_INTEGER|MN_FLOAT)) { /* promote to float */ if (mn1.type & MN_INTEGER) { mn1.type = MN_FLOAT; mn1.u.d = (double)mn1.u.l; } if (mn2.type & MN_INTEGER) { mn2.type = MN_FLOAT; mn2.u.d = (double)mn2.u.l; } } switch(ctype) { case COND_EQ: return !((mn1.type & MN_FLOAT) ? (mn1.u.d == mn2.u.d) : (mn1.u.l == mn2.u.l)); case COND_NE: return !((mn1.type & MN_FLOAT) ? (mn1.u.d != mn2.u.d) : (mn1.u.l != mn2.u.l)); case COND_LT: return !((mn1.type & MN_FLOAT) ? (mn1.u.d < mn2.u.d) : (mn1.u.l < mn2.u.l)); case COND_GT: return !((mn1.type & MN_FLOAT) ? (mn1.u.d > mn2.u.d) : (mn1.u.l > mn2.u.l)); case COND_LE: return !((mn1.type & MN_FLOAT) ? (mn1.u.d <= mn2.u.d) : (mn1.u.l <= mn2.u.l)); case COND_GE: return !((mn1.type & MN_FLOAT) ? (mn1.u.d >= mn2.u.d) : (mn1.u.l >= mn2.u.l)); } } switch (ctype) { case COND_STREQ: case COND_STRNEQ: { int test, npat = state->pc[1]; Patprog pprog = state->prog->pats[npat]; if (pprog == dummy_patprog1 || pprog == dummy_patprog2) { char *opat; int save; right = dupstring(opat = ecrawstr(state->prog, state->pc, &htok)); singsub(&right); save = (!(state->prog->flags & EF_HEAP) && !strcmp(opat, right) && pprog != dummy_patprog2); if (!(pprog = patcompile(right, (save ? PAT_ZDUP : PAT_STATIC), NULL))) { zwarnnam(fromtest, "bad pattern: %s", right); return 2; } else if (save) state->prog->pats[npat] = pprog; } state->pc += 2; test = (pprog && pattry(pprog, left)); return !(ctype == COND_STREQ ? test : !test); } case COND_STRLT: return !(strcmp(left, right) < 0); case COND_STRGTR: return !(strcmp(left, right) > 0); case 'e': case 'a': return (!doaccess(left, F_OK)); case 'b': return (!S_ISBLK(dostat(left))); case 'c': return (!S_ISCHR(dostat(left))); case 'd': return (!S_ISDIR(dostat(left))); case 'f': return (!S_ISREG(dostat(left))); case 'g': return (!(dostat(left) & S_ISGID)); case 'k': return (!(dostat(left) & S_ISVTX)); case 'n': return (!strlen(left)); case 'o': return (optison(fromtest, left)); case 'p': return (!S_ISFIFO(dostat(left))); case 'r': return (!doaccess(left, R_OK)); case 's': return !((st = getstat(left)) && !!(st->st_size)); case 'S': return (!S_ISSOCK(dostat(left))); case 'u': return (!(dostat(left) & S_ISUID)); case 'w': return (!doaccess(left, W_OK)); case 'x': if (privasserted()) { mode_t mode = dostat(left); return !((mode & S_IXUGO) || S_ISDIR(mode)); } return !doaccess(left, X_OK); case 'z': return !!(strlen(left)); case 'h': case 'L': return (!S_ISLNK(dolstat(left))); case 'O': return !((st = getstat(left)) && st->st_uid == geteuid()); case 'G': return !((st = getstat(left)) && st->st_gid == getegid()); case 'N': #if defined(GET_ST_MTIME_NSEC) && defined(GET_ST_ATIME_NSEC) if (!(st = getstat(left))) return 1; return (st->st_atime == st->st_mtime) ? GET_ST_ATIME_NSEC(*st) > GET_ST_MTIME_NSEC(*st) : st->st_atime > st->st_mtime; #else return !((st = getstat(left)) && st->st_atime <= st->st_mtime); #endif case 't': return !isatty(mathevali(left)); case COND_NT: case COND_OT: { time_t a; #ifdef GET_ST_MTIME_NSEC long nsecs; #endif if (!(st = getstat(left))) return 1; a = st->st_mtime; #ifdef GET_ST_MTIME_NSEC nsecs = GET_ST_MTIME_NSEC(*st); #endif if (!(st = getstat(right))) return 1; #ifdef GET_ST_MTIME_NSEC if (a == st->st_mtime) { return !((ctype == COND_NT) ? nsecs > GET_ST_MTIME_NSEC(*st) : nsecs < GET_ST_MTIME_NSEC(*st)); } #endif return !((ctype == COND_NT) ? a > st->st_mtime : a < st->st_mtime); } case COND_EF: { dev_t d; ino_t i; if (!(st = getstat(left))) return 1; d = st->st_dev; i = st->st_ino; if (!(st = getstat(right))) return 1; return !(d == st->st_dev && i == st->st_ino); } default: zwarnnam(fromtest, "bad cond code"); return 2; } }
time_t getctime(File *file) { struct stat *pstat = getstat(file); return pstat && pstat->st_ctime; }
void BCU1SerialLowLevelDriver::Run (pth_sem_t * stop1) { pth_event_t stop = pth_event (PTH_EVENT_SEM, stop1); pth_event_t input = pth_event (PTH_EVENT_SEM, &in_signal); pth_event_t timeout = pth_event (PTH_EVENT_RTIME, pth_time (0, 10)); while (pth_event_status (stop) != PTH_STATUS_OCCURRED) { int error; timeout = pth_event (PTH_EVENT_RTIME | PTH_MODE_REUSE, timeout, pth_time (0, 150)); pth_event_concat (stop, input, timeout, NULL); pth_wait (stop); pth_event_isolate (stop); pth_event_isolate (input); timeout = pth_event (PTH_EVENT_RTIME | PTH_MODE_REUSE, timeout, pth_time (0, 200)); pth_event_concat (stop, timeout, NULL); struct timeval v1, v2; gettimeofday (&v1, 0); CArray e; CArray r; uchar s; if (!inqueue.isempty ()) { const CArray & c = inqueue.top (); e.resize (c () + 1); s = c () & 0x1f; s |= 0x20; s |= 0x80 * bitcount (s); e[0] = s; e.setpart (c, 1); } else { e.resize (1); e[0] = 0xff; } if (!startsync ()) { error = 1; goto err; } if (!exchange (e[0], s, stop)) { error = 3; goto err; } if (!endsync ()) { error = 2; goto err; } if (s == 0xff && e[0] != 0xff) { for (unsigned i = 1; i < e (); i++) { if (!startsync ()) { error = 1; goto err; } if (!exchange (e[i], s, stop)) { error = 3; goto err; } if (endsync ()) { error = 2; goto err; } } if (s != 0x00) { error = 10; goto err; } inqueue.get (); TRACEPRINTF (t, 0, this, "Sent"); pth_sem_dec (&in_signal); if (inqueue.isempty ()) pth_sem_set_value (&send_empty, 1); } else if (s != 0xff) { r.resize ((s & 0x1f)); for (unsigned i = 0; i < (s & 0x1f); i++) { if (!startsync ()) { error = 1; goto err; } if (!exchange (0, r[i], stop)) { error = 3; goto err; } if (!endsync ()) { error = 2; goto err; } } TRACEPRINTF (t, 0, this, "Recv"); outqueue.put (new CArray (r)); pth_sem_inc (&out_signal, 1); } gettimeofday (&v2, 0); TRACEPRINTF (t, 1, this, "Recvtime: %d", v2.tv_sec * 1000000L + v2.tv_usec - (v1.tv_sec * 1000000L + v1.tv_usec)); if (0) { err: gettimeofday (&v2, 0); TRACEPRINTF (t, 1, this, "ERecvtime: %d", v2.tv_sec * 1000000L + v2.tv_usec - (v1.tv_sec * 1000000L + v1.tv_usec)); setstat (getstat () & ~(TIOCM_RTS | TIOCM_CTS)); pth_usleep (2000); while ((getstat () & TIOCM_CTS)); TRACEPRINTF (t, 0, this, "Restart %d", error); } pth_event_isolate (timeout); } pth_event_free (timeout, PTH_FREE_THIS); pth_event_free (stop, PTH_FREE_THIS); pth_event_free (input, PTH_FREE_THIS); }
/* TODO return the number as a string or "?" if the group could not be determined */ gid_t getgroupnum(File *file) { struct stat *pstat = getstat(file); if (!pstat) return 0; return pstat->st_gid; }
int main() { /* MIRACL rational calculator */ int i,j,k,p,q,c,hpos; BOOL over,help; screen(); #if MIRACL==16 mip=mirsys(10,0); /*** 16-bit computer ***/ #else mip=mirsys(6,0); /*** 32-bit computer ***/ #endif mip->ERCON=TRUE; x=mirvar(0); for (i=0;i<=top;i++) y[i]=mirvar(0); m=mirvar(0); t=mirvar(0); radeg=mirvar(0); loge2=mirvar(0); loge10=mirvar(0); eps=mirvar(0); mip->pi=mirvar(0); cinstr(mip->pi,cpi); /* read in constants */ fpmul(mip->pi,1,180,radeg); cinstr(loge2,clg2); cinstr(loge10,clg10); cinstr(eps,ceps); help=OFF; show(TRUE); p=6; q=0; flag=OFF; newx=OFF; over=FALSE; setopts(); clrall(); drawit(); while (!over) { /* main loop */ if (mip->ERNUM) { aprint(ORDINARY,4+5*p,6+3*q,keys[q][p]); p=5,q=0; } if (width==80 || !help) { aprint(INVER,4+5*p,6+3*q,keys[q][p]); curser(1,24); c=gethit(); aprint(ORDINARY,4+5*p,6+3*q,keys[q][p]); } else while ((c=gethit())!='H') ; result=TRUE; if ((k=arrow(c))!=0) { /* arrow key hit */ if (k==1 && q>0) q--; if (k==2 && q<5) q++; if (k==3 && p<6) p++; if (k==4 && p>0) p--; continue; } if (c=='H') { /* switch help on/off */ help=!help; for (i=1;i<=24;i++) { if (width==80) hpos=41; else hpos=1; if (help) aprint(HELPCOL,hpos,i,htext[i-1]); else lclr(hpos,i); } if (width==40 && !help) drawit(); continue; } if (c>='A' && c<='F') { /* hex only */ if (!next(c)) putchar(BELL); else show(FALSE); continue; } for (j=0;j<6;j++) for (i=0;i<7;i++) if (c==qkeys[j][i]) p=i,q=j,c=' '; if (c==8 || c==127) p=6,q=1,c=' '; /* aliases */ if (c==',' || c=='a') p=5,q=5,c=' '; if (c=='O' || c==ESC) p=6,q=0,c=' '; if (c==13) p=6,q=5,c=' '; if (c=='[' || c=='{') p=3,q=5,c=' '; if (c==']' || c=='}') p=4,q=5,c=' '; if (c=='d') p=5,q=2,c=' '; if (c=='b') p=5,q=3,c=' '; if (c=='^') p=3,q=2,c=' '; if (c==' ') over=act(p,q); else continue; absol(x,t); if (fcomp(t,eps)<0) zero(x); if (result) { /* output result to display */ cotstr(x,mip->IOBUFF); just((char *)mip->IOBUFF); if (mip->ERNUM<0) { /* convert to radix and try again */ mip->ERNUM=0; mip->RPOINT=ON; cotstr(x,mip->IOBUFF); putchar(BELL); just((char *)mip->IOBUFF); } clr(); } if (newx) { /* update display */ getstat(); show(FALSE); } } curser(1,24); restore(); return 0; }
bool ChooseAnImage(int sx,int sy, char *incoming_dir, int slot, char **filename, bool *isdir, int *index_file) { /* Parameters: sx, sy - window size, incoming_dir - in what dir find files, slot - in what slot should an image go (common: #6 for 5.25' 140Kb floppy disks, and #7 for hard-disks). slot #5 - for 800Kb floppy disks, but we do not use them in Apple][? (They are as a rule with .2mg extension) index_file - from which file we should start cursor (should be static and 0 when changing dir) Out: filename - chosen file name (or dir name) isdir - if chosen name is a directory */ /* Surface: */ SDL_Surface *my_screen; // for background if(font_sfc == NULL) if(!fonts_initialization()) return false; //if we don't have a fonts, we just can do none List<char> files; // our files List<char> sizes; // and their sizes (or 'dir' for directories) int act_file; // current file int first_file; // from which we output files printf("Disckchoose! We are here: %s\n",incoming_dir); // files.Delete(); // sizes.Delete(); #ifndef _WIN32 /* POSIX specific routines of reading directory structure */ DIR *dp; struct dirent *ep; dp = opendir (incoming_dir); // open and read incoming directory char *tmp; int i,j, B, N; // for cycles, beginning and end of list // build prev dir if(strcmp(incoming_dir, "/")) { tmp = new char[3]; strcpy(tmp, ".."); files.Add(tmp); tmp = new char[5]; strcpy(tmp, "<UP>"); sizes.Add(tmp); // add sign of directory B = 1; } else B = 0; // for sorting dirs if (dp != NULL) { while (ep = readdir (dp)) // first looking for directories { int what = getstat(incoming_dir, ep->d_name, NULL); if (strlen(ep->d_name) > 0 && /*strcmp(ep->d_name,".")*/// omit "." (cur dir) ep->d_name[0] != '.'/*strcmp(ep->d_name,"..")*/ && what == 1) // is directory! { tmp = new char[strlen(ep->d_name)+1]; // add entity to list strcpy(tmp, ep->d_name); files.Add(tmp); tmp = new char[6]; strcpy(tmp, "<DIR>"); sizes.Add(tmp); // add sign of directory } /* if */ } } #else /* Windows specific functions of reading directory structure */ /* Find subdirs: */ if(strcmp(incoming_dir, "/")) { // we are not in upper direcory tmp = new char[3]; strcpy(tmp, ".."); files.Add(tmp); tmp = new char[5]; strcpy(tmp, "<UP>"); sizes.Add(tmp); // add sign of directory B = 1; } else B = 0; // for sorting dirs WIN32_FIND_DATA finfo; HANDLE h; h=FindFirstFile(incoming_dir,&finfo); if (h!=INVALID_HANDLE_VALUE) { char *tmp; if(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0) { // add this entry if it is directory tmp=new char[strlen(finfo.cFileName)+1]; strcpy(tmp,finfo.cFileName); files.Add(tmp); tmp = new char[6]; strcpy(tmp, "<DIR>"); sizes.Add(tmp); // add sign of directory } while(FindNextFile(h,&finfo)==TRUE) { if(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0) { // add this entry if it is directory tmp=new char[strlen(finfo.cFileName)+1]; strcpy(tmp,finfo.cFileName); files.Add(tmp); tmp = new char[6]; strcpy(tmp, "<DIR>"); sizes.Add(tmp); // add sign of directory } } /* while */ } /* if */ #endif // sort directories. Please, don't laugh at my bubble sorting - it the simplest thing I've ever seen --bb if(files.Length() > 2) { N = files.Length() - 1; // B = 1;`- defined above for(i = N; i > B; i--) for(j = B; j < i; j++) if(strcasecmp(files[j], files[j + 1]) > 0) { files.Swap(j,j + 1); sizes.Swap(j,j + 1); } } B = files.Length(); // start for files #ifndef _WIN32 /* POSIX specific routines of reading directory structure */ (void) rewinddir (dp); // to the start // now get all regular files while (ep = readdir (dp)) { int fsize; if (strlen(ep->d_name) > 4 && ep->d_name[0] != '.' && (getstat(incoming_dir, ep->d_name, &fsize) == 2)) // is normal file! { tmp = new char[strlen(ep->d_name)+1]; // add this entity to list strcpy(tmp, ep->d_name); files.Add(tmp); tmp = new char[10]; // 1400000KB snprintf(tmp, 9, "%dKB", fsize); sizes.Add(tmp); // add this size to list } /* if */ } (void) closedir (dp); #else /* Windows specific functions of reading directory structure */ /* Find files: */ h=FindFirstFile(incoming_dir,&finfo); if (h!=INVALID_HANDLE_VALUE) { // char *tmp; - must be defined in previous section, when searching subdirs if(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == 0) { // add this entry if it is NOT directory! tmp=new char[strlen(finfo.cFileName)+1]; strcpy(tmp,finfo.cFileName); files.Add(tmp); tmp = new char[10]; // 1400000KB snprintf(tmp, 9, "%dKB", ((finfo.nFileSizeHigh * (MAXDWORD+1)) + finfo.nFileSizeLow)); sizes.Add(tmp); // add this size to list } while(FindNextFile(h,&finfo)==TRUE) { if(finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == 0) { // add this entry if it is NOT directory! tmp=new char[strlen(finfo.cFileName)+1]; strcpy(tmp,finfo.cFileName); files.Add(tmp); tmp = new char[10]; // 1400000KB snprintf(tmp, 9, "%dKB", ((finfo.nFileSizeHigh * (MAXDWORD+1)) + finfo.nFileSizeLow)); sizes.Add(tmp); // add this size to list } } /* while */ } /* if */ #endif // do sorting for files if(files.Length() > 2 && B < files.Length()) { N = files.Length() - 1; // B = 1; for(i = N; i > B; i--) for(j = B; j < i; j++) if(strcasecmp(files[j], files[j + 1]) > 0) { files.Swap(j,j + 1); sizes.Swap(j,j + 1); } } // Count out cursor position and file number output act_file = *index_file; if(act_file >= files.Length()) act_file = 0; // cannot be more than files in list first_file = act_file - (FILES_IN_SCREEN / 2); if (first_file < 0) first_file = 0; // cannot be negativ... // Show all directories (first) and files then // char *tmp; char *siz; // int i; // prepare screen double facx = double(g_ScreenWidth) / double(SCREEN_WIDTH); double facy = double(g_ScreenHeight) / double(SCREEN_HEIGHT); SDL_Surface *tempSurface = NULL; if(!g_WindowResized) { if(g_nAppMode == MODE_LOGO) tempSurface = g_hLogoBitmap; // use logobitmap else tempSurface = g_hDeviceBitmap; } else tempSurface = g_origscreen; if(tempSurface == NULL) tempSurface = screen; // use screen, if none available my_screen = SDL_CreateRGBSurface(SDL_SWSURFACE, tempSurface->w, tempSurface->h, tempSurface->format->BitsPerPixel, 0, 0, 0, 0); if(tempSurface->format->palette && my_screen->format->palette) SDL_SetColors(my_screen, tempSurface->format->palette->colors, 0, tempSurface->format->palette->ncolors); surface_fader(my_screen, 0.2F, 0.2F, 0.2F, -1, 0); // fade it out to 20% of normal SDL_BlitSurface(tempSurface, NULL, my_screen, NULL); while(true) { SDL_BlitSurface(my_screen, NULL, screen, NULL); // show background font_print_centered(sx/2 ,5*facy , incoming_dir, screen, 1.5*facx, 1.3*facy); if (slot == 6) font_print_centered(sx/2,20*facy,"Choose image for floppy 140KB drive", screen, 1*facx, 1*facy); else if (slot == 7) font_print_centered(sx/2,20*facy,"Choose image for Hard Disk", screen, 1*facx, 1*facy); else if (slot == 5) font_print_centered(sx/2,20*facy,"Choose image for floppy 800KB drive", screen, 1*facx, 1*facy); else if (slot == 1) font_print_centered(sx/2,20*facy,"Select file name for saving snapshot", screen, 1*facx, 1*facy); else if (slot == 0) font_print_centered(sx/2,20*facy,"Select snapshot file name for loading", screen, 1*facx, 1*facy); font_print_centered(sx/2,30*facy, "Press ENTER to choose, or ESC to cancel",screen, 1.4*facx, 1.1*facy); files.Rewind(); // from start sizes.Rewind(); i = 0; // printf("We've printed some messages, go to file list!\n"); // show all fetched dirs and files // topX of first fiel visible int TOPX = int(45*facy); while(files.Iterate(tmp)) { sizes.Iterate(siz); // also fetch size string if (i >= first_file && i < first_file + FILES_IN_SCREEN) { // FILES_IN_SCREEN items on screen // char tmp2[80],tmp3[256]; if (i == act_file) { // show item under cursor (in inverse mode) SDL_Rect r; r.x= 2; r.y= TOPX + (i-first_file) * 15 * facy - 1; if(strlen(tmp) > 46) r.w = 46 * FONT_SIZE_X /* 6 */ * 1.7 * facx + 2; else r.w= strlen(tmp) * FONT_SIZE_X /* 6 */ * 1.7 * facx + 2; // 6- FONT_SIZE_X r.h= 9 * 1.5 * facy; SDL_FillRect(screen, &r, SDL_MapRGB(screen->format,255,0,0));// in RED } /* if */ // print file name with enlarged font char ch; ch = 0; if(strlen(tmp) > 46) { ch = tmp[46]; tmp[46] = 0;} //cut-off too long string font_print(4, TOPX + (i - first_file) * 15 * facy, tmp, screen, 1.7*facx, 1.5*facy); // show name font_print(sx - 70 * facx, TOPX + (i - first_file) * 15 * facy, siz, screen, 1.7*facx, 1.5*facy);// show info (dir or size) if(ch) tmp[46] = ch; //restore cut-off char } /* if */ i++; // next item } /* while */ ///////////////////////////////////////////////////////////////////////////////////////////// // draw rectangles rectangle(screen, 0, TOPX - 5, sx, 320*facy, SDL_MapRGB(screen->format, 255, 255, 255)); rectangle(screen, 480*facx, TOPX - 5, 0, 320*facy, SDL_MapRGB(screen->format, 255, 255, 255)); SDL_Flip(screen); // show the screen SDL_Delay(KEY_DELAY); // wait some time to be not too fast ////////////////////////////////// // Wait for keypress ////////////////////////////////// SDL_Event event; // event Uint8 *keyboard; // key state event.type = SDL_QUIT; while(event.type != SDL_KEYDOWN) { // wait for key pressed SDL_Delay(10); SDL_PollEvent(&event); } // control cursor keyboard = SDL_GetKeyState(NULL); // get current state of pressed (and not pressed) keys if (keyboard[SDLK_UP] || keyboard[SDLK_LEFT]) { if (act_file>0) act_file--; // up one position if (act_file<first_file) first_file=act_file; } /* if */ if (keyboard[SDLK_DOWN] || keyboard[SDLK_RIGHT]) { if (act_file < (files.Length() - 1)) act_file++; if (act_file >= (first_file + FILES_IN_SCREEN)) first_file=act_file - FILES_IN_SCREEN + 1; } /* if */ if (keyboard[SDLK_PAGEUP]) { act_file-=FILES_IN_SCREEN; if (act_file<0) act_file=0; if (act_file<first_file) first_file=act_file; } /* if */ if (keyboard[SDLK_PAGEDOWN]) { act_file+=FILES_IN_SCREEN; if (act_file>=files.Length()) act_file=(files.Length()-1); if (act_file>=(first_file+FILES_IN_SCREEN)) first_file=act_file-FILES_IN_SCREEN + 1; } /* if */ // choose an item? if (keyboard[SDLK_RETURN]) { // dup string from selected file name *filename = strdup(files[act_file]); // printf("files[act_file]=%s, *filename=%s\n\n", files[act_file], *filename); if(!strcmp(sizes[act_file], "<DIR>") || !strcmp(sizes[act_file], "<UP>")) *isdir = true; else *isdir = false; // this is directory (catalog in Apple][ terminology) *index_file = act_file; // remember current index files.Delete(); sizes.Delete(); SDL_FreeSurface(my_screen); return true; } /* if */ if (keyboard[SDLK_ESCAPE]) { files.Delete(); sizes.Delete(); SDL_FreeSurface(my_screen); return false; // ESC has been pressed } /* if */ if (keyboard[SDLK_HOME]) { // HOME? act_file=0; first_file=0; } /* if */ if (keyboard[SDLK_END]) { // END? act_file=files.Length() - 1; // go to the last possible file in list first_file=act_file - FILES_IN_SCREEN + 1; if(first_file < 0) first_file = 0; } /* if */ } } /* ChooseAnImage */