static int check_manual_file(struct sdirs *sdirs, const char *cname) { int ret=-1; char *manual=NULL; // A 'backup' file placed in the storage directory tells this script // that a backup needs to be done right now. // This gives the 'server initiates a manual backup' feature. // The path should probably be part of sdirs so we do not have to build // it here. if(astrcat(&manual, sdirs->clients, __func__) || astrcat(&manual, "/", __func__) || astrcat(&manual, cname, __func__) || astrcat(&manual, "/backup", __func__)) goto end; if(is_reg_lstat(manual)>0) { logp("Found %s\n", manual); unlink(manual); ret=0; goto end; } ret=1; end: free_w(&manual); return ret; }
int parse_options_subcommand(int argc, const char **argv, const struct option *options, const char *const subcommands[], const char *usagestr[], int flags) { struct parse_opt_ctx_t ctx; /* build usage string if it's not provided */ if (subcommands && !usagestr[0]) { char *buf = NULL; astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]); for (int i = 0; subcommands[i]; i++) { if (i) astrcat(&buf, "|"); astrcat(&buf, subcommands[i]); } astrcat(&buf, "}"); usagestr[0] = buf; } parse_options_start(&ctx, argc, argv, flags); switch (parse_options_step(&ctx, options, usagestr)) { case PARSE_OPT_HELP: exit(129); case PARSE_OPT_DONE: break; case PARSE_OPT_LIST_OPTS: while (options->type != OPTION_END) { if (options->long_name) printf("--%s ", options->long_name); options++; } putchar('\n'); exit(130); case PARSE_OPT_LIST_SUBCMDS: if (subcommands) { for (int i = 0; subcommands[i]; i++) printf("%s ", subcommands[i]); } putchar('\n'); exit(130); default: /* PARSE_OPT_UNKNOWN */ if (ctx.argv[0][1] == '-') astrcatf(&error_buf, "unknown option `%s'", ctx.argv[0] + 2); else astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt); usage_with_options(usagestr, options); } return parse_options_end(&ctx); }
/* * File logistics. */ static void processinout(const char *ifn, const char *ofn) { struct stat st; if (ifn == NULL || strcmp(ifn, "-") == 0) { filename = "[stdin]"; linefile = NULL; input = fbinmode(stdin); } else { filename = ifn; linefile = ifn; input = fopen(ifn, "rb"); if (input == NULL) err(2, "can't open %s", ifn); } if (strcmp(ofn, "-") == 0) { output = fbinmode(stdout); process(); return; } if (stat(ofn, &st) < 0) { output = fopen(ofn, "wb"); if (output == NULL) err(2, "can't create %s", ofn); process(); return; } tempname = astrcat(ofn, ".XXXXXX"); output = mktempmode(tempname, st.st_mode); if (output == NULL) err(2, "can't create %s", tempname); process(); if (backext != NULL) { char *backname = astrcat(ofn, backext); if (rename(ofn, backname) < 0) err(2, "can't rename \"%s\" to \"%s\"", ofn, backname); free(backname); } /* leave file unmodified if unifdef made no changes */ if (!altered && backext == NULL) { if (remove(tempname) < 0) warn("can't remove \"%s\"", tempname); } else if (replace(tempname, ofn) < 0) err(2, "can't rename \"%s\" to \"%s\"", tempname, ofn); free(tempname); tempname = NULL; }
static struct fzp *open_backup_log(struct bu *bu, const char *logfile) { char *path=NULL; struct fzp *fzp=NULL; char logfilereal[32]=""; if(!strcmp(logfile, "backup")) snprintf(logfilereal, sizeof(logfilereal), "log"); else if(!strcmp(logfile, "restore")) snprintf(logfilereal, sizeof(logfilereal), "restorelog"); else if(!strcmp(logfile, "verify")) snprintf(logfilereal, sizeof(logfilereal), "verifylog"); else if(!strcmp(logfile, "backup_stats")) snprintf(logfilereal, sizeof(logfilereal), "backup_stats"); else if(!strcmp(logfile, "restore_stats")) snprintf(logfilereal, sizeof(logfilereal), "restore_stats"); else if(!strcmp(logfile, "verify_stats")) snprintf(logfilereal, sizeof(logfilereal), "verify_stats"); if(!(path=prepend_s(bu->path, logfilereal))) goto end; if(!(fzp=fzp_gzopen(path, "rb"))) { if(astrcat(&path, ".gz", __func__) || !(fzp=fzp_gzopen(path, "rb"))) goto end; } end: free_w(&path); return fzp; }
int main(void) { char *container = NULL; char text[] = "Once upon a time in a dark night something happened"; char other_text[] = " and happened some more"; astrcpy(&container, text); printf("Copied: %s\n", container); astrcat(&container, other_text); printf("Concatenated: %s\n", container); return 0; }
static char *gen_fullpath(const char *prefix, char **comps) { uint32_t i; uint32_t number_of_components; char *path=NULL; char *file=NULL; number_of_components=prng_next()%6; if(prefix) { fail_unless(!astrcat(&path, "", __func__)); fail_unless(!astrcat(&path, prefix, __func__)); } for(i=0; i<number_of_components; i++) { uint32_t choice; choice=prng_next()%COMP_SIZE; fail_unless(!astrcat(&path, "/", __func__)); fail_unless(!astrcat(&path, comps[choice], __func__)); } fail_unless((file=comp_gen())!=NULL); fail_unless(!astrcat(&path, "/", __func__)); fail_unless(!astrcat(&path, file, __func__)); free_w(&file); return path; }
// Return 0 for OK, -1 for error, 1 for finished reading the file. static int get_next_set_of_hooks(struct hooks **hnew, struct sbuf *sb, struct fzp *spzp, char **path, char **fingerprints, struct conf **confs) { while(1) { switch(sbuf_fill(sb, NULL /* struct async */, spzp, NULL, NULL, confs)) { case -1: goto error; case 1: // Reached the end. if(hooks_alloc(hnew, path, fingerprints)) goto error; return 1; } if(sb->path.cmd==CMD_MANIFEST) { if(hooks_alloc(hnew, path, fingerprints)) break; free_w(fingerprints); free_w(path); *path=sb->path.buf; sb->path.buf=NULL; sbuf_free_content(sb); if(*hnew) return 0; } else if(sb->path.cmd==CMD_FINGERPRINT) { if(astrcat(fingerprints, sb->path.buf, __func__)) break; sbuf_free_content(sb); } else { iobuf_log_unexpected(&sb->path, __func__); break; } } error: return -1; }
static void list_commands_in_dir(struct cmdnames *cmds, const char *path, const char *prefix) { int prefix_len; DIR *dir = opendir(path); struct dirent *de; char *buf = NULL; if (!dir) return; if (!prefix) prefix = "perf-"; prefix_len = strlen(prefix); astrcatf(&buf, "%s/", path); while ((de = readdir(dir)) != NULL) { int entlen; if (!strstarts(de->d_name, prefix)) continue; astrcat(&buf, de->d_name); if (!is_executable(buf)) continue; entlen = strlen(de->d_name) - prefix_len; if (has_extension(de->d_name, ".exe")) entlen -= 4; add_cmdname(cmds, de->d_name + prefix_len, entlen); } closedir(dir); free(buf); }
static int CdTextParse( vlc_meta_t ***ppp_tracks, int *pi_tracks, const uint8_t *p_buffer, int i_buffer ) { char *pppsz_info[128][0x10]; int i_track_last = -1; if( i_buffer < 4 ) return -1; memset( pppsz_info, 0, sizeof(pppsz_info) ); for( int i = 0; i < (i_buffer-4)/18; i++ ) { const uint8_t *p_block = &p_buffer[4 + 18*i]; char psz_text[12+1]; const int i_pack_type = p_block[0]; if( i_pack_type < 0x80 || i_pack_type > 0x8f ) continue; const int i_track_number = (p_block[1] >> 0)&0x7f; const int i_extension_flag = ( p_block[1] >> 7)& 0x01; if( i_extension_flag ) continue; //const int i_sequence_number = p_block[2]; //const int i_charater_position = (p_block[3] >> 0) &0x0f; //const int i_block_number = (p_block[3] >> 4) &0x07; /* TODO unicode support * I need a sample */ //const int i_unicode = ( p_block[3] >> 7)&0x01; //const int i_crc = (p_block[4+12] << 8) | (p_block[4+13] << 0); /* */ memcpy( psz_text, &p_block[4], 12 ); psz_text[12] = '\0'; /* */ int i_track = i_track_number; char *psz_track = &psz_text[0]; while( i_track <= 127 && psz_track < &psz_text[12] ) { //fprintf( stderr, "t=%d psz_track=%p end=%p", i_track, (void *)psz_track, (void *)&psz_text[12] ); if( *psz_track ) { astrcat( &pppsz_info[i_track][i_pack_type-0x80], psz_track ); i_track_last = __MAX( i_track_last, i_track ); } i_track++; psz_track += 1 + strlen(psz_track); } } if( i_track_last < 0 ) return -1; vlc_meta_t **pp_tracks = calloc( i_track_last+1, sizeof(*pp_tracks) ); if( !pp_tracks ) goto exit; for( int j = 0; j < 0x10; j++ ) { for( int i = 0; i <= i_track_last; i++ ) { /* */ if( pppsz_info[i][j] ) EnsureUTF8( pppsz_info[i][j] ); /* */ const char *psz_default = pppsz_info[0][j]; const char *psz_value = pppsz_info[i][j]; if( !psz_value && !psz_default ) continue; vlc_meta_t *p_track = pp_tracks[i]; if( !p_track ) { p_track = pp_tracks[i] = vlc_meta_New(); if( !p_track ) continue; } switch( j ) { case 0x00: /* Album/Title */ if( i == 0 ) { vlc_meta_SetAlbum( p_track, psz_value ); } else { if( psz_value ) vlc_meta_SetTitle( p_track, psz_value ); if( psz_default ) vlc_meta_SetAlbum( p_track, psz_default ); } break; case 0x01: /* Performer */ vlc_meta_SetArtist( p_track, psz_value ? psz_value : psz_default ); break; case 0x05: /* Messages */ vlc_meta_SetDescription( p_track, psz_value ? psz_value : psz_default ); break; case 0x07: /* Genre */ vlc_meta_SetGenre( p_track, psz_value ? psz_value : psz_default ); break; /* FIXME unsupported: * 0x02: songwriter * 0x03: composer * 0x04: arrenger * 0x06: disc id */ } } } /* */ exit: for( int j = 0; j < 0x10; j++ ) for( int i = 0; i <= i_track_last; i++ ) free( pppsz_info[i][j] ); *ppp_tracks = pp_tracks; *pi_tracks = i_track_last+1; return pp_tracks ? 0 : -1; }
static int process_entry(struct strlist *ig, struct conf **confs) { int ret=-1; size_t len1=0; char *sav=NULL; char **splitstr1=NULL; WIN32_FIND_DATA ffd; HANDLE hFind=INVALID_HANDLE_VALUE; convert_backslashes(&ig->path); if(ig->path[strlen(ig->path)-1]!='*') { if(!(splitstr1=xstrsplit(ig->path, "*", &len1))) goto end; } if(len1>2) { logp("include_glob error: '%s' contains at least" " two '*' which is not currently supported\n", ig->path); goto end; } if(len1>1) { char *tmppath=NULL; if(astrcat(&tmppath, splitstr1[0], __func__) || !(sav=strdup_w(tmppath, __func__)) || astrcat(&tmppath, "*", __func__)) goto end; hFind=FindFirstFileA(tmppath, &ffd); free_w(&tmppath); } else hFind=FindFirstFileA(ig->path, &ffd); if(hFind==INVALID_HANDLE_VALUE) { LPVOID lpMsgBuf; DWORD dw=GetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL ); logp("Error: %s\n", lpMsgBuf); LocalFree(lpMsgBuf); goto end; } do { if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(ffd.cFileName, ".") && strcmp(ffd.cFileName, "..")) { char *tmppath=NULL; if(len1<2) { if(ig->path[strlen(ig->path)-1]=='*') { if(!(tmppath=xstrsub(ig->path, 0, strlen(ig->path)-1)) || astrcat(&tmppath, ffd.cFileName, __func__)) goto end; } else if(!(tmppath=strdup_w(ig->path, __func__))) goto end; } else { if(astrcat(&tmppath, sav, __func__) || astrcat(&tmppath, ffd.cFileName, __func__) || astrcat(&tmppath, splitstr1[1], __func__)) goto end; } if(add_to_strlist(confs[OPT_INCLUDE], tmppath, 1)) goto end; free_w(&tmppath); } } while(FindNextFileA(hFind, &ffd)!=0); FindClose(hFind); ret=0; end: if(splitstr1) { free_w(&sav); xfree_list(splitstr1, len1); } return ret; }