static bool concat_argv(const char *argv[SIZEOF_ARG], char *buf, size_t buflen, const char *sep, bool quoted) { size_t bufpos, argc; for (bufpos = 0, argc = 0; argv[argc]; argc++) { const char *arg_sep = argc ? sep : ""; const char *arg = argv[argc]; int pos; if (quoted && arg[(pos = strcspn(arg, " \t\""))]) { if (!string_nformat(buf, buflen, &bufpos, "%s\"", arg_sep)) return FALSE; while (*arg) { int pos = strcspn(arg, "\""); const char *qesc = arg[pos] == '"' ? "\\\"" : ""; if (!string_nformat(buf, buflen, &bufpos, "%.*s%s", pos, arg, qesc)) return FALSE; arg += pos + 1; } if (!string_nformat(buf, buflen, &bufpos, "\"")) return FALSE; continue; } if (!string_nformat(buf, buflen, &bufpos, "%s%s", arg_sep, arg)) return FALSE; } return TRUE; }
static int check_configuration( const char *function, int xsize, int ysize ) { char path[PATH_MAX]; int i; printf("Checking for presence of function %s...\n",function); if(access(function,R_OK|X_OK)!=0) { fprintf(stderr, "Error: Cannot access %s: %s\n",function,strerror(errno)); fprintf(stderr, "You must provide an executable program named %s\n",function); return 0; } printf("Checking for initial data files...\n"); for(i=0;i<=xsize;i++) { string_nformat(path,PATH_MAX,"R.%d.%d",xstart+i-1,ystart-1); if(access(path,R_OK)!=0) { fprintf(stderr, "Cannot access initial file %s: %s\n",path,strerror(errno)); return 0; } } for(i=0;i<=ysize;i++) { string_nformat(path,PATH_MAX,"R.%d.%d",xstart-1,ystart+i-1); if(access(path,R_OK)!=0) { fprintf(stderr, "Cannot access initial file %s: %s\n",path,strerror(errno)); return 0; } } return 1; }
int wavefront_task_submit_recursive( struct wavefront_task *n ) { int i,j; char command[PATH_MAX]; char filename[PATH_MAX]; char extra_output_files[PATH_MAX]; char *extra_input_files=NULL; string_nformat(command, PATH_MAX, "./wavefront -M -X %d -Y %d ./%s %d %d >output.%d.%d 2>&1", n->x,n->y,function,n->width,n->height,n->x,n->y); string_nformat(extra_output_files, PATH_MAX, "output.%d.%d",n->x,n->y); extra_input_files = string_format("wavefront,%s",function); for(i=-1;i<n->width;i++) { string_nformat(filename, PATH_MAX, "R.%d.%d", n->x+i,n->y-1); extra_input_files=string_combine(extra_input_files, ","); extra_input_files=string_combine(extra_input_files, filename); } for(j=0;j<n->height;j++) { string_nformat(filename, PATH_MAX, "R.%d.%d",n->x-1,n->y+j); extra_input_files=string_combine(extra_input_files, ","); extra_input_files=string_combine(extra_input_files, filename); } batch_job_id_t job_id = batch_job_submit_simple(batch_q,command,extra_input_files,extra_output_files); free(extra_input_files); return job_id; }
static const char * get_relative_date(const struct time *time, char *buf, size_t buflen, bool compact) { struct timeval now; time_t timestamp = time->sec + time->tz; time_t seconds; int i; if (time_now(&now, NULL)) return ""; seconds = now.tv_sec < timestamp ? timestamp - now.tv_sec : now.tv_sec - timestamp; for (i = 0; i < ARRAY_SIZE(reldate); i++) { if (seconds >= reldate[i].interval && reldate[i].interval) continue; seconds /= reldate[i].in_seconds; if (compact) { if (!string_nformat(buf, buflen, NULL, "%s%ld%c", now.tv_sec >= timestamp ? "" : "-", seconds, reldate[i].compact_symbol)) return ""; } else if (!string_nformat(buf, buflen, NULL, "%ld %s%s %s", seconds, reldate[i].name, seconds > 1 ? "s" : "", now.tv_sec >= timestamp ? "ago" : "ahead")) return ""; return buf; } return ""; }
enum status_code format_ref_formats(struct ref_format **formats, char buf[], size_t bufsize) { const struct enum_map *map = reference_type_map; char name[SIZEOF_STR]; enum reference_type type; size_t bufpos = 0; const char *sep = ""; for (type = 0; type < map->size; type++) { struct ref_format *format = formats[type]; if (!format) continue; if (!enum_name_copy(name, sizeof(name), map->entries[type].name) || !string_nformat(buf, bufsize, &bufpos, "%s%s%s%s", sep, format->start, name, format->end)) return error("No space left in buffer"); sep = " "; } return SUCCESS; }
static bool append_key(char *buf, size_t *pos, const struct keybinding *keybinding, bool all) { const char *sep = *pos > 0 ? ", " : ""; const char *keyname = get_key_name(keybinding->key, keybinding->keys, all); return string_nformat(buf, BUFSIZ, pos, "%s%s", sep, keyname); }
int wavefront_task_submit_single( struct wavefront_task *n ) { char command[PATH_MAX * 5]; //Ugly, fix, should be the value from sysconf(ARG_MAX) char leftfile[PATH_MAX]; char bottomfile[PATH_MAX]; char diagfile[PATH_MAX]; char extra_input_files[PATH_MAX*4]; string_nformat(leftfile, PATH_MAX, "R.%d.%d", n->x-1, n->y); string_nformat(bottomfile, PATH_MAX, "R.%d.%d", n->x, n->y-1); string_nformat(diagfile, PATH_MAX, "R.%d.%d", n->x-1, n->y-1); string_nformat(extra_input_files, PATH_MAX*4, "%s,%s,%s,%s", function,leftfile,bottomfile,diagfile); string_nformat(command, PATH_MAX * 5, "./%s %s %s %s >R.%d.%d", function,leftfile,bottomfile,diagfile,n->x,n->y); return batch_job_submit_simple(batch_q,command,extra_input_files,0); }
bool argv_to_string(const char *argv[SIZEOF_ARG], char *buf, size_t buflen, const char *sep) { size_t bufpos, argc; for (bufpos = 0, argc = 0; argv[argc]; argc++) if (!string_nformat(buf, buflen, &bufpos, "%s%s", argc ? sep : "", argv[argc])) return FALSE; return TRUE; }
static bool concat_argv(const char *argv[], char *buf, size_t buflen, const char *sep, bool quoted) { size_t bufpos, argc; for (bufpos = 0, argc = 0; argv[argc]; argc++) { const char *arg_sep = argc ? sep : ""; const char *arg = argv[argc]; if (quoted && arg[strcspn(arg, " \t\"")]) { if (!string_nformat(buf, buflen, &bufpos, "%s\"", arg_sep)) return false; while (*arg) { int pos = strcspn(arg, "\""); const char *qesc = arg[pos] == '"' ? "\\\"" : ""; if (!string_nformat(buf, buflen, &bufpos, "%.*s%s", pos, arg, qesc)) return false; if (!arg[pos]) break; else arg += pos + 1; } if (!string_nformat(buf, buflen, &bufpos, "\"")) return false; continue; } if (!string_nformat(buf, buflen, &bufpos, "%s%s", arg_sep, arg)) return false; } return true; }
static bool add_describe_ref(char *buf, size_t *bufpos, const char *commit_id, const char *sep) { const char *describe_argv[] = { "git", "describe", commit_id, NULL }; char ref[SIZEOF_STR]; if (!io_run_buf(describe_argv, ref, sizeof(ref)) || !*ref) return TRUE; /* This is the only fatal call, since it can "corrupt" the buffer. */ if (!string_nformat(buf, SIZEOF_STR, bufpos, "%s%s", sep, ref)) return FALSE; return TRUE; }
static const char * get_relative_date(const struct time *time, char *buf, size_t buflen) { static const struct enum_map_entry reldate[] = { { "second", 1, 60 * 2 }, { "minute", 60, 60 * 60 * 2 }, { "hour", 60 * 60, 60 * 60 * 24 * 2 }, { "day", 60 * 60 * 24, 60 * 60 * 24 * 7 * 2 }, { "week", 60 * 60 * 24 * 7, 60 * 60 * 24 * 7 * 5 }, { "month", 60 * 60 * 24 * 30, 60 * 60 * 24 * 365 }, { "year", 60 * 60 * 24 * 365, 0 }, }; struct timeval now; time_t timestamp = time->sec + time->tz; time_t seconds; int i; if (time_now(&now, NULL)) return ""; seconds = now.tv_sec < timestamp ? timestamp - now.tv_sec : now.tv_sec - timestamp; for (i = 0; i < ARRAY_SIZE(reldate); i++) { if (seconds >= reldate[i].value && reldate[i].value) continue; seconds /= reldate[i].namelen; if (!string_nformat(buf, buflen, NULL, "%ld %s%s %s", seconds, reldate[i].name, seconds > 1 ? "s" : "", now.tv_sec >= timestamp ? "ago" : "ahead")) return ""; return buf; } return ""; }