// ----------------------------------------------------------------------------- int g_del_dir(const char* path) { DIR* dp = opendir(path); if( dp ) { dirent* ep; char pb[PATH_MAX]; while( (ep = readdir(dp)) != NULL ) { if( strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 ) { strvcpy(pb, path); strvcat(pb, ep->d_name); if( g_is_dir(pb) ) { strvcat (pb, "/"); g_del_dir(pb); } else unlink(pb); } } closedir(dp); } return rmdir(path); }
// ----------------------------------------------------------------------------- int g_isa_sdir(const char* path, const char* sdir) { if( g_isa_dir(path) ) { char spath[PATH_MAX]; strvcpy(spath, path); strvcat(spath, "/" ); strvcat(spath, sdir); return g_isa_dir(spath); } return 0; }
static char *__format_uint(unsigned int value, int flags) { char buffer[16]; int i; int base; const char *digits_lower = "0123456789abcdef"; const char *digits_upper = "0123456789ABCDEF"; const char *digits; if (flags & FLAG_OCTAL) base = 8; else if (flags & FLAG_HEX) base = 16; else base = 10; if (value == 0) { return strdup("0"); } if (flags & FLAG_UPPER) { digits = digits_upper; } else { digits = digits_lower; } buffer[15] = '\0'; for (i = 14; i >= 0; i--) { if (!value) break; buffer[i] = digits[value % base]; value /= base; } if (flags & FLAG_SIGN) { buffer[i--] = '+'; } i++; if (flags & FLAG_ALT) { if (flags & FLAG_OCTAL) { return strvcat("0", &buffer[i], NULL); } if (flags & FLAG_HEX) { return strvcat((flags & FLAG_UPPER) ? "0X" : "0x", &buffer[i], NULL); } } return strdup(&buffer[i]); }
int main(int argc, char **argv) { char *path, *args, *reply; uint64_t ctrl; int i; if (argc < 2) { fprintf(stderr, "%s: missing operand\n", argv[0]); return 1; } if (argv[1][0] == '/') { path = strdup(argv[1]); } else if (argv[1][0] == '@') { path = strdup(argv[1]); } else { path = strvcat("/sys/", argv[1]); } ctrl = fs_find(path); if (!ctrl) { fprintf(stderr, "%s: %s: control file not accessible\n", argv[0], path); return 1; } args = malloc(1000); args[0] = '\0'; for (i = 2; i < argc; i++) { strlcat(args, argv[i], 1000); strlcat(args, " ", 1000); } if (args[0]) args[strlen(args)-1] = '\0'; reply = rcall(ctrl, args); if (!reply) { printf("! nosys (not implemented)\n"); return 1; } if (iserror(reply)) { printf("%s (%s)\n", reply, strerror(geterror(reply))); } else { printf("%s\n", reply); } return 0; }
static char *__format_int(int value, int flags) { char *string, *temp; if (value < 0) { temp = __format_uint(-value, flags); string = strvcat("-", temp, NULL); free(temp); } else { string = __format_uint(value, flags); } return string; }
static char *__format_double_int(long double value, int flags) { char *temp, *string; int expt; expt = log10l(value) + 1; value /= (int) powl(10, expt); temp = __format_double_frac(value, flags | FLAG_ALT | FLAG_MEXP, expt); if (value < 0) { string = strvcat("-", temp, NULL); free(temp); } else if (flags & FLAG_SIGN) { string = strvcat("+", temp, NULL); free(temp); } else { string = temp; } return string; }
void *load_exec(const char *name) { int fd; uint64_t size; char *path; void *image; /* attempt to find requested file */ if (name[0] == '/' || name[0] == '@') { path = strdup(name); } else { path = strvcat(getenv("PATH"), "/", name, NULL); } fd = ropen(-1, fs_find(path), STAT_READER); if (fd < 0 || !rp_type(fd_rp(fd), "file")) { /* file not found */ return NULL; } else { /* read whole file into buffer */ size = rp_size(fd_rp(fd)); if (!size) { return NULL; } image = aalloc(size, PAGESZ); if (!image) { return NULL; } if (rp_read(fd_rp(fd), image, size, 0) != size) { free(image); close(fd); return NULL; } close(fd); return image; } }
int main(int argc, char **argv) { struct robject *canvas; char *modesstr0; char *modestr; int i; rdi_init(); canvas = rdi_file_cons(robject_new_index(), ACCS_READ | ACCS_WRITE | ACCS_EVENT); robject_set_data(canvas, "type", (void*) "canvas share"); svga_init(); // generate list of modes modesstr = strdup(""); for (i = 0; i < modelist_count; i++) { modesstr0 = modesstr; modestr = malloc(16); sprintf(modestr, "%d:%d:%d ", modelist[i].w, modelist[i].h, modelist[i].d); modesstr = strvcat(modesstr, modestr, NULL); free(modesstr0); } svga_set_mode(svga_find_mode(640, 480, 24)); buffer = malloc(svga.w * svga.h * 4); /* set up driver interface */ robject_set_call(canvas, "getmode", svga_rcall_getmode, STAT_READER); robject_set_call(canvas, "listmodes", svga_rcall_listmodes, STAT_READER); robject_set_call(canvas, "unshare", svga_rcall_unshare, STAT_WRITER); robject_set_call(canvas, "setmode", svga_rcall_setmode, STAT_WRITER); robject_set_call(canvas, "syncrect", svga_rcall_syncrect, STAT_WRITER); robject_set_call(canvas, "sync", svga_rcall_sync, STAT_WRITER); rdi_global_share_hook = svga_share; /* register the driver as /dev/svga0 */ fs_plink("/dev/svga0", RP_CONS(getpid(), canvas->index), NULL); msendb(RP_CONS(getppid(), 0), PORT_CHILD); _done(); return 0; }
int main(int argc, char **argv) { struct vfs_obj *root; char *modesstr0; char *modestr; int i; root = calloc(sizeof(struct vfs_obj), 1); root->type = RP_TYPE_FILE; root->size = 0; root->acl = acl_set_default(root->acl, PERM_READ | PERM_WRITE); vfs_set_index(0, root); svga_init(); // generate list of modes modesstr = strdup(""); for (i = 0; i < modelist_count; i++) { modesstr0 = modesstr; modestr = malloc(16); sprintf(modestr, "%d:%d:%d ", modelist[i].w, modelist[i].h, modelist[i].d); modesstr = strvcat(modesstr, modestr, NULL); free(modesstr0); } svga_set_mode(svga_find_mode(640, 480, 24)); buffer = malloc(svga.w * svga.h * 4); /* set up driver interface */ di_wrap_sync (svga_sync); di_wrap_share(svga_share); di_wrap_rcall(svga_rcall); di_wrap_read (svga_read); di_wrap_write(svga_write); vfs_wrap_init(); /* register the driver as /dev/svga0 */ io_link("/dev/svga0", RP_CONS(getpid(), 0)); msendb(RP_CONS(getppid(), 0), PORT_CHILD); _done(); return 0; }
// ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- const char* get_home_subpath(const char* sdpath, int splen, bool appHome) { const char* apppth = gRun.appHome; bool appuse = appHome && apppth[0] != '\0'; const char* hompth = appuse ? apppth : (const char*)g_get_home_dir(); const int homlen = strlen(hompth); const int pthlen = homlen + splen + 2; char* pthstr = new char [pthlen]; if( pthstr ) { // strvcpy(pthstr, hompth, pthlen); // TODO: these are okay? strvcpy(pthstr, hompth, homlen+1); // TODO: these are okay? // strvcat(pthstr, sdpath, pthlen-homlen); // strvcat(pthstr, sdpath, pthlen); // } DEBUGLOGP("\nsdpath=%s\napppth=%s\nhompth=%s\npthstr=%s\n", sdpath, apppth, hompth, pthstr); return pthstr; }
// ----------------------------------------------------------------------------- bool extract(const char* spath, const char* dpath) { DEBUGLOGB; int retc; lib::LoadLib* pLLib = lib::create(); bool okay = pLLib && lib::load(pLLib, "archive", ".13", g_symbs, vectsz(g_symbs)); if( okay ) { DEBUGLOGS("archive lib loaded ok - before typedef'd func address setting"); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_entry_pathname)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_entry_set_pathname)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_errno)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_error_string)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_read_free)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_read_new)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_read_next_header)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_read_open_filename)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_read_support_filter_all)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_read_support_format_all)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_write_disk_new)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_write_disk_set_options)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_write_finish_entry)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_write_free)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_write_header)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_read_data_block)); lib::func(pLLib, LIB_FUNC_ADDR(g_archive_write_data_block)); DEBUGLOGS("archive lib loaded ok - after typedef'd func address setting"); } archive *sorc, *dest; if( okay ) { DEBUGLOGS("before archive read/write new calls"); sorc = g_archive_read_new(); dest = g_archive_write_disk_new(); DEBUGLOGS("after archive read/write new calls"); if( !sorc || !dest ) { DEBUGLOGS(" (1) - unable to create new read & write archive objects"); okay = false; } } if( okay ) { g_archive_read_support_filter_all(sorc); g_archive_read_support_format_all(sorc); g_archive_write_disk_set_options(dest, ARCHIVE_EXTRACT_TIME); if( (retc = g_archive_read_open_filename(sorc, spath, 64*1024)) != ARCHIVE_OK ) { int errn = g_archive_errno(sorc); const char* errs = g_archive_error_string(sorc); DEBUGLOGP(" (2) - unable to open archive (%d:%d)\n%s\n%s\n", retc, errn, errs, spath); okay = false; } } if( okay ) { archive_entry* entry; while( true ) { if( (retc = g_archive_read_next_header(sorc, &entry)) == ARCHIVE_EOF ) { DEBUGLOGS("archive_read_next_header returned 'eof'"); break; } if( retc != ARCHIVE_OK ) { DEBUGLOGS("archive_read_next_header returned 'archive not ok' error"); okay = false; break; } char destPath[PATH_MAX]; strvcpy(destPath, dpath); strvcat(destPath, g_archive_entry_pathname(entry)); DEBUGLOGP("extracting %s\n", destPath); g_archive_entry_set_pathname(entry, destPath); if( (retc = g_archive_write_header(dest, entry)) != ARCHIVE_OK ) { DEBUGLOGS("archive_write_header returned 'archive not ok' error"); okay = false; break; } if( !copyData(sorc, dest) ) { DEBUGLOGS("copyData returned 'archive not ok' error"); okay = false; break; } if( (retc = g_archive_write_finish_entry(dest)) != ARCHIVE_OK ) { DEBUGLOGS("archive_write_finish returned 'archive not ok' error"); okay = false; break; } } } if( dest ) g_archive_write_free(dest); if( sorc ) g_archive_read_free(sorc); lib::destroy(pLLib); pLLib = NULL; DEBUGLOGS(okay ? "archive file contents extraction was successful" : "archive file contents extraction was NOT successful"); DEBUGLOGE; return okay; }
static char *__format_double(long double value, int flags, int precision) { char *int_part; char *dec_part; char *string; long double i; if (isnan(value)) { if (flags & FLAG_UPPER) { return strdup("NAN"); } else { return strdup("nan"); } } if (isinf(value)) { if (flags & FLAG_UPPER) { if (value < 0) { return strdup("-INF"); } else { if (flags & FLAG_SIGN) { return strdup("+INF"); } else { return strdup("INF"); } } } else { if (value < 0) { return strdup("-inf"); } else { if (flags & FLAG_SIGN) { return strdup("+inf"); } else { return strdup("inf"); } } } } if (flags & FLAG_EXP || (flags & FLAG_MEXP && value > 1000000000)) { i = log10l(value); value /= powl(10, floorl(i)); int_part = __format_double(value, flags & ~(FLAG_EXP), precision); dec_part = __format_int(i, flags | FLAG_SIGN); string = strvcat(int_part, (flags & FLAG_UPPER) ? "E" : "e", dec_part, NULL); free(int_part); free(dec_part); } else { value = modfl(value, &i); if (value > 2000000000) { int_part = __format_double_int(i, flags); } else { int_part = __format_int(i, flags); } dec_part = __format_double_frac(value, flags, precision); if (i == 0.0 && value < 0) { if (value != 0.0 || flags & FLAG_ALT) { string = strvcat("-", int_part, ".", dec_part, NULL); } else { string = strvcat("-", int_part, NULL); } } else { if (value != 0.0 || flags & FLAG_ALT) { string = strvcat(int_part, ".", dec_part, NULL); } else { string = strdup(int_part); } } free(int_part); free(dec_part); } return string; }
// ----------------------------------------------------------------------------- int strfmtdt(char* s, int size, const char* format, const struct tm* dt, char* tbuf) { // TODO: pass in a callback for extra formatting options, instead of hardcoding? // if callback is null then provide a default? /* o ? for truncating am/pm to a or p s - for getting rid of leading 0 (put it between % and opt chr) - GNU extension already in place s @r for sunrise/set times o ? for user-entered tzone o ? for next alarm time s @f for fuzzy date */ int ret = strftime(s, size, format, dt); if( s && size && format && dt && ret && *s ) { char* ttag; const char* fmts = "fr"; char fmt[] = "@?"; char* tend = tbuf ? tbuf : new char[size]; for( size_t f = 0; f < vectsz(fmts); f++ ) { fmt[1] = fmts[f]; if( (ttag = strstr(s, fmt)) != NULL ) { strvcpy(tend, ttag+2, size); *ttag = '\0'; switch( fmts[f] ) { case 'f': // fuzzy date text { static const char* txts[] = { gRun.cfaceTxta3, gRun.cfaceTxta2, gRun.cfaceTxta1 }; bool catd = false; for( size_t t = 0; t < vectsz(txts); t++ ) { if( *txts[t] ) { // if( *s ) // if( *s && t ) if( *s && catd ) strvcat(s, " ", size); strvcat(s, txts[t], size); catd = true; } } } break; case 'r': // sunrise/set text { if( *gRun.riseSetTxt ) strvcat(s, gRun.riseSetTxt, size); } break; } strvcat(s, tend, size); } } if( tend != tbuf ) delete [] tend; ret = strlen(s); } // return strftime(s, size, format, dt); return ret; }
// ----------------------------------------------------------------------------- bool loadTheme(const char* theme_arch_file_path, char* theme_dir, int theme_dir_size, char* theme_fnm, int theme_fnm_size) { DEBUGLOGB; char temp_dir[PATH_MAX], temp_fnm[64]; const char* utp = get_user_theme_path(); bool okay = false; strvcpy(temp_dir, utp ? utp : "."); strvcat(temp_dir, "/"); strvcat(temp_dir, "clk040XXXXXX"); mkdtemp(temp_dir); mkdir (temp_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); // just in case of mkdtemp failure, create default strvcat(temp_dir, "/"); // g_mkdir_with_parents(temp_dir, 0x755); DEBUGLOGP("arch_file_path is \n%s\n", theme_arch_file_path); DEBUGLOGP("temp_dir is \n%s\n", temp_dir); okay = extract(theme_arch_file_path, temp_dir); if( okay ) { char td[PATH_MAX], pb[PATH_MAX]; // validate (this should be a call to a func since need that functionality elsewhere) DIR* dp = opendir(temp_dir); if( okay = dp != NULL ) { dirent* ep; int ne = 0; while( (ep = readdir(dp)) != NULL ) { if( strcmp (ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0 ) { strvcpy(td, temp_dir); strvcpy(pb, temp_dir); strvcat(pb, ep->d_name); strvcpy(temp_fnm, ep->d_name); if( !(okay = ((++ne == 1) && g_isa_dir(pb))) ) break; } } okay = ne == 1; closedir(dp); } if( okay ) // move the theme sub-dir (pb) to temp_dir's parent dir (utp or .) { char temp_pth[PATH_MAX]; strvcpy(temp_dir, utp ? utp : "."); strvcat(temp_dir, "/"); strvcpy(temp_pth, temp_dir); strvcat(temp_pth, temp_fnm); mkdir (temp_pth, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if( okay = g_isa_dir(temp_pth) ) { DEBUGLOGP("new theme name is %s\n", temp_fnm); DEBUGLOGP("cur theme dir is\n%s\n", pb); // old DEBUGLOGP("new theme dir is\n%s\n", temp_pth); // new int rc = rename(pb, temp_pth); // rename old (pb) to new (temp_pth) okay = rc == 0; DEBUGLOGP("rename rc is %d\n", rc); } } if( okay ) g_del_dir(td); } else { DEBUGLOGS("extract failed (deleting temp dir)"); if( g_isa_dir(temp_dir) ) g_del_dir(temp_dir); } if( utp ) delete [] utp; if( theme_dir && theme_dir_size ) { if( okay ) { strvcpy(theme_dir, temp_dir, theme_dir_size); // TODO: this is okay? } else *theme_dir = '\0'; } if( theme_fnm && theme_fnm_size ) { if( okay ) { strvcpy(theme_fnm, temp_fnm, theme_fnm_size); // TODO: this is okay? } else *theme_fnm = '\0'; } DEBUGLOGE; return okay; }
// ----------------------------------------------------------------------------- bool extract(const char* spath, const char* dpath) { DEBUGLOGB; int retc; bool okay = false; archive* sorc = archive_read_new(); archive* dest = archive_write_disk_new(); if( !sorc || !dest ) { DEBUGLOGE; DEBUGLOGS(" (1) - unable to create new read & write archive objects"); return okay; } archive_read_support_filter_all(sorc); archive_read_support_format_all(sorc); archive_write_disk_set_options(dest, ARCHIVE_EXTRACT_TIME); if( (retc = archive_read_open_filename(sorc, spath, 64*1024)) != ARCHIVE_OK ) { int errn = archive_errno(sorc); const char* errs = archive_error_string(sorc); DEBUGLOGE; DEBUGLOGP(" (2) - unable to open archive (%d:%d)\n%s\n%s\n", retc, errn, errs, spath); return okay; } archive_entry* entry; while( true ) { if( (retc = archive_read_next_header(sorc, &entry)) == ARCHIVE_EOF ) { DEBUGLOGS("archive_read_next_header returned 'eof'"); okay = true; break; } if( retc != ARCHIVE_OK ) { DEBUGLOGS("archive_read_next_header returned 'archive not ok' error"); break; } char destPath[PATH_MAX]; strvcpy(destPath, dpath); strvcat(destPath, archive_entry_pathname(entry)); DEBUGLOGP("extracting %s\n", destPath); archive_entry_set_pathname(entry, destPath); if( (retc = archive_write_header(dest, entry)) != ARCHIVE_OK ) { DEBUGLOGS("archive_write_header returned 'archive not ok' error"); break; } copyData(sorc, dest); if( (retc = archive_write_finish_entry(dest)) != ARCHIVE_OK ) { DEBUGLOGS("archive_write_finish returned 'archive not ok' error"); break; } } archive_write_free(dest); archive_read_free(sorc); DEBUGLOGE; return okay; }
// ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- bool lib::load(const char* name, const char* sufx, symb* symbs, size_t nsymbs) { DEBUGLOGB; bool okay = false; if( !g_module_supported() ) { DEBUGLOGS("runtime module loading NOT supported"); DEBUGLOGE; return okay; } DEBUGLOGS("runtime module loading supported"); char* path = g_module_build_path(NULL, name); if( !path ) { DEBUGLOGP("%s shared library path NOT built\n", name); DEBUGLOGP("%s\n", g_module_error()); DEBUGLOGE; return okay; } DEBUGLOGP("%s shared library path built - path is:\n\t%s\n", name, path); g_pShared = g_module_open(path, GModuleFlags(G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL)); g_pSymbs = NULL; g_nSymbs = 0; g_iSymb = 0; if( !g_pShared && sufx ) { DEBUGLOGP("%s shared library module was NOT opened - trying secondary\n", name); char pths[PATH_MAX]; strvcpy(pths, path); strvcat(pths, sufx); g_pShared = g_module_open(pths, GModuleFlags(G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL)); } g_free(path); if( !g_pShared ) { DEBUGLOGP("%s shared library module was NOT opened\n", name); DEBUGLOGP("%s\n", g_module_error()); DEBUGLOGE; return okay; } DEBUGLOGP("%s shared library module was opened\n", name); okay = load_symbs(g_pShared, symbs, nsymbs); if( okay ) { DEBUGLOGP("%s shared library module requested symbols were loaded\n", name); g_pSymbs = symbs; g_nSymbs = nsymbs; } else { DEBUGLOGP("%s shared library module requested symbols were NOT loaded\n", name); } DEBUGLOGE; return okay; }
/* * Lustre needs remote filesystem and host. */ static char * lustre_match(am_opts *fo) { char *xmtab, *cp; size_t l; char *rhost, *ptr, *remhost; struct in_addr addr; if (fo->opt_fs && !fo->opt_rfs) fo->opt_rfs = fo->opt_fs; if (!fo->opt_rfs) { plog(XLOG_USER, "lustre: no remote filesystem specified"); return NULL; } if (!fo->opt_rhost) { plog(XLOG_USER, "lustre: no remote host specified"); return NULL; } /* * Determine magic cookie to put in mtab */ rhost = xstrdup(fo->opt_rhost); remhost = NULL; for (ptr = strtok(rhost, ":"); ptr; ptr = strtok(NULL, ":")) { char *at = strchr(ptr, '@'); if (at == NULL) { plog(XLOG_USER, "lustre: missing protocol in host `%s'", ptr); XFREE(rhost); return NULL; } *at = '\0'; /* * Convert symbolic addresses to numbers that the kernel likes */ if (inet_aton(ptr, &addr) == 0) { struct hostent *hp; if ((hp = gethostbyname(ptr)) == NULL) { plog(XLOG_USER, "lustre: unknown host `%s'", ptr); XFREE(rhost); return NULL; } if (hp->h_length != sizeof(addr.s_addr)) { plog(XLOG_USER, "lustre: bad address length %zu != %d for %s", sizeof(addr), hp->h_length, ptr); XFREE(rhost); return NULL; } memcpy(&addr.s_addr, hp->h_addr, sizeof(addr)); } *at = '@'; cp = remhost; if (remhost) remhost = strvcat(cp, ":", inet_ntoa(addr), at, NULL); else remhost = strvcat(inet_ntoa(addr), at, NULL); XFREE(cp); } if (remhost == NULL) { plog(XLOG_USER, "lustre: empty host"); XFREE(rhost); return NULL; } XFREE(rhost); XFREE(fo->opt_rhost); fo->opt_rhost = remhost; l = strlen(fo->opt_rhost) + strlen(fo->opt_rfs) + 2; xmtab = xmalloc(l); xsnprintf(xmtab, l, "%s:%s", fo->opt_rhost, fo->opt_rfs); dlog("lustre: mounting remote server \"%s\", remote fs \"%s\" on \"%s\"", fo->opt_rhost, fo->opt_rfs, fo->opt_fs); return xmtab; }