int main(){ char str_old[] = "Hello world it is great"; int count_old = strlen(str_old); int count_new = count_old; for (int i = 0; i < count_old; ++i) // make sure have enough space if(str_old[i] == ' ') count_new += 2; char str_new[count_new]; // str_new[count_new] = 0; replace_space(str_old, str_new); printf("%d %d\n", count_old, count_new); printf("%s\n", str_new); return 0; }
/* * The private data structures are written into a * binary blob. These contain module private data * information */ int main(void) { unsigned int i; FILE *fd; char path[128]; char new_path[128]; struct snd_soc_tplg_private *priv = NULL; memset(path, 0, sizeof(path)); memset(new_path, 0, sizeof(new_path)); priv = calloc(1, sizeof(dfw_wrap) + sizeof(uint32_t)); for (i = 0; i < ARRAY_SIZE(dfw_wrap); i++) { strcat(path, "../"); strcat(path, dfw_wrap[i].name); strcat(path, ".bin"); replace_space(path, new_path); priv->size = (uint32_t)sizeof(dfw_wrap[i].skl_dfw_mod); memcpy(priv->data, &dfw_wrap[i].skl_dfw_mod, priv->size); fd = fopen(new_path, "wb"); if (fd == NULL) return -ENOENT; if (fwrite(priv->data, priv->size, 1, fd) != 1) { fclose(fd); return -1; } memset(path, 0, sizeof(path)); } free(priv); return 0; }
int main( int argc , char ** argv ) { std::map<std::string,std::string> args; std::set<std::string> skip; args["ID"] = "ID"; args["REF"] = "REF"; args["ALT"] = "ALT"; args["QUAL"] = "QUAL"; args["FILTER"] = "FILTER"; std::map<std::string,std::string> types; std::map<std::string,std::string> number; std::map<std::string,std::string> desc; for (int i=1; i<argc; i++) { int t; char_tok tok( argv[i] , &t , '=' ); if ( t == 2 ) { if ( strcmp( tok(0) , "SKIP" ) == 0 || strcmp( tok(0) , "skip" ) == 0 ) skip.insert(tok(1)); else args[ tok(0) ] = tok(1); } else if ( t == 1 ) { int t2; char_tok tok2( argv[i] , &t2 , ':' ); if ( t2 == 4 ) { number[ tok2(0) ] = tok2(1); types[ tok2(0) ] = tok2(2); desc[ tok2(0) ] = tok2(3); } } } // take a file with HEADERs from STDIN; // write as a VCF to STDOUT // Get header LINE std::string hdr; std::getline( std::cin , hdr ); if ( hdr == "" ) exit(1); // remove a leading '#' if ( hdr.substr(0,1) == "#" ) hdr = hdr.substr(1); // get fields int n; char_tok tok( hdr , &n , '\t' ); // find positional fields // either POS, or CHR and BP (or BP1 and BP2) int pos = -1 , chr = -1 , bp1 = -1 , bp2 = -1; // other special fields int qual = -1 , id = -1 , ref = -1 , alt = -1 , filter = -1; std::vector<std::string> field(n); std::vector<int> info; for (int i=0; i<n; i++) { bool special_field = false; field[i] = tok(i); if ( field[i] == "VAR" ) { special_field = true; pos = i; } else if ( field[i] == "LOC" ) { special_field = true; pos = i; } else if ( field[i] == "CHR" ) { special_field = true; chr = i; } else if ( field[i] == "CHROM" ) { special_field = true; chr = i; } else if ( field[i] == "BP" ) { special_field = true; bp1 = i; } else if ( field[i] == "BP1" ) { special_field = true; bp1 = i; } else if ( field[i] == "BP2" ) { special_field = true; bp2 = i; } else if ( field[i] == "POS" ) { special_field = true; bp1 = i; } else if ( field[i] == "POS1" ) { special_field = true; bp1 = i; } else if ( field[i] == "POS2" ) { special_field = true; bp2 = i; } std::map<std::string,std::string>::iterator ii = args.begin(); while ( ii != args.end() ) { if ( ii->second == field[i] ) { if ( ii->first == "QUAL" || ii->first == "qual" ) { special_field = true; qual = i; } else if ( ii->first == "FILTER" || ii->first == "filter" ) { special_field = true; filter = i; } else if ( ii->first == "ID" || ii->first == "id" ) { special_field = true; id = i; } else if ( ii->first == "REF" || ii->first == "ref" ) { special_field = true; ref = i; } else if ( ii->first == "ALT" || ii->first == "alt" ) { special_field = true; alt = i; } } ++ii; } // skip this field? if ( skip.find( field[i] ) != skip.end() ) special_field = true; // normal INFO field? if ( ! special_field ) info.push_back(i); } // next header entry // does this contain valid positional information? if ( pos == -1 && ( chr == -1 || bp1 == -1 ) ) exit(1); bool use_chr_bp = chr != -1 && bp1 != -1; bool use_bp2 = use_chr_bp && bp2 != -1; const int s = info.size(); // write VCF header std::cout << "##fileformat=VCFv4.1\n" << "##source=tab2vcf\n"; for (int i=0; i<s; i++) { if ( types.find( field[ info[i] ] ) != types.end() ) { std::cout << "##INFO=<ID=" << field[info[i]] << ",Number=" << number[field[info[i]]] << ",Type=" << types[field[info[i]]] << ",Description=\"" << desc[field[info[i]]] << "\">\n"; } else std::cout << "##INFO=<ID=" << field[info[i]] << ",Number=1,Type=Float,Description=\"n/a\">\n"; } // also display any additional headers from the command line but not seen in the file (i.e. // to describe Flags in the body of the text that are not in the headers std::set<std::string> fset; for (int i=0; i<s; i++) fset.insert( field[info[i]] ); std::map<std::string,std::string>::iterator ii = types.begin(); while ( ii != types.end() ) { if ( fset.find( ii->first ) == fset.end() ) std::cout << "##INFO=<ID=" << ii->first << ",Number=" << number[ii->first] << ",Type=" << types[ii->first] << ",Description=\"" << desc[ii->first] << "\">\n"; ++ii; } // second header std::cout << "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n"; // information while ( ! std::cin.eof() ) { std::string l; int m; std::getline( std::cin , l ); // replace spaces with ';' unless they are quoted char_tok tok( l , &m , '\t' ); if ( l == "" ) continue; if ( m != n ) { std::cout << "unequal number of fields for line\n" << l << "\n(expecting " << n << " found " << m << ")\n"; exit(1); } if ( use_chr_bp ) { // chromosome std::cout << tok(chr) << "\t"; // position std::cout << tok(bp1) ; if ( bp2 != -1 ) std::cout << ".." << tok(bp2); std::cout << "\t"; } else { std::string pstr = tok( pos ); // expect 'chr:1234' // expect 'chr:1234..4596' // or 'chr:1234-5674' int k; char_tok stok( pstr , &k , ':' ); if ( k == 0 ) std::cout << ".\t.\t"; else if ( k == 1 ) std::cout << stok(0) << "\t.\t"; else if ( k >= 2 ) { std::cout << stok(0) << "\t" << stok(1) << "\t"; } } // id if ( id == -1 ) std::cout << ".\t"; else std::cout << tok(id) << "\t"; // ref if ( ref == -1 ) std::cout << ".\t"; else std::cout << tok(ref) << "\t"; // alt if ( alt == -1 ) std::cout << ".\t"; else std::cout << tok(alt) << "\t"; // qual if ( qual == -1 ) std::cout << ".\t"; else std::cout << tok(qual) << "\t"; // filter if ( filter == -1 ) std::cout << ".\t"; else std::cout << replace_space( tok(filter) , ';' ) << "\t"; // info for (int i=0; i<s; i++) { if ( i ) std::cout << ";"; std::cout << field[info[i]] << "=" << opt_quote( replace_space( tok(info[i]) , ',' ) ); } std::cout << "\n"; } }
/** * @brief * parses path and prepares complete path name * * @param[in] path_in - the path name provided as input to be parsed * @param[out] path_out - contains final parsed and prepared path, must * be at least MAXPATHLEN+1 bytes. * * @return int * @retval 0 - success in parsing * @retval nonzero - error encountered in parsing */ int prepare_path(char *path_in, char *path_out) { char *c = NULL; int have_fqdn = 0; /* Initialization with {'\0'} populates entire array */ char host_name[PBS_MAXSERVERNAME + 1] = {'\0'}; /* short host name */ int h_pos = 0; char path_name[MAXPATHLEN + 1] = {'\0'}; size_t path_len; int p_pos = 0; char *host_given = NULL; struct stat statbuf = {0}; dev_t dev = 0; ino_t ino = 0; if (!path_out) return 1; *path_out = '\0'; if (!path_in) return 1; /* Begin the parse */ for (c = path_in; *c; c++) { if (isspace(*c) == 0) break; } if (*c == '\0') return 1; #ifdef WIN32 /* Check for drive letter in Windows */ if (!(isalpha(*c) && (*(c + 1) == ':'))) #endif { /* Looking for a hostname */ if ((host_given = strchr(c, ':')) != NULL) { /* Capture the hostname portion */ for (h_pos = 0; (h_pos < sizeof(host_name)); h_pos++, c++) { if (isalnum(*c) || (*c == '.') || (*c == '-') #ifdef WIN32 /* Underscores are legal in Windows */ || (*c == '_') #endif ) { host_name[h_pos] = *c; } else { break; } } if (*c != ':') return 1; /* Advance past the colon */ c++; } } /* Looking for a posix path */ for (p_pos = 0; p_pos < sizeof(path_name); p_pos++, c++) { if (!isprint(*c)) break; path_name[p_pos] = *c; } /* Should be at end of string */ if (*c != '\0') return 1; path_len = strlen(path_name); if (path_len == 0 && strlen(host_name) == 0) return 1; /* appending a slash in the end to indicate that it is a directory */ if ((path_name[path_len - 1] != '/') && (path_name[path_len - 1] != '\\') && (stat(path_name, &statbuf) == 0) && S_ISDIR(statbuf.st_mode)) { if ((path_len + 1) < sizeof(path_name)) { strcat(path_name, "/"); path_len++; } } #ifdef WIN32 if (IS_UNCPATH(path_name)) { /* * given path is UNC path * so just skip hostname * as UNC path dose not require it */ host_given = NULL; host_name[0] = '\0'; } else #endif { /* get full host name */ if (host_name[0] == '\0') { if (pbs_conf.pbs_output_host_name) { /* use the specified host for returning the file */ snprintf(host_name, sizeof(host_name), "%s", pbs_conf.pbs_output_host_name); have_fqdn = 1; } else { if (gethostname(host_name, sizeof(host_name)) != 0) return 2; host_name[sizeof(host_name) - 1] = '\0'; } } if (have_fqdn == 0) { char host_fqdn[PBS_MAXSERVERNAME + 1] = {'\0'}; /* need to fully qualify the host name */ if (get_fullhostname(host_name, host_fqdn, PBS_MAXSERVERNAME) != 0) return 2; strncpy(path_out, host_fqdn, MAXPATHLEN); /* FQ host name */ } else { strncpy(path_out, host_name, MAXPATHLEN); /* "localhost" or pbs_output_host_name */ } path_out[MAXPATHLEN - 1] = '\0'; /* finish preparing complete host name */ if (strlen(path_out) < MAXPATHLEN) strcat(path_out, ":"); } #ifdef WIN32 if (path_name[0] != '/' && path_name[0] != '\\' && host_given == NULL && strchr(path_name, ':') == NULL ) #else if (path_name[0] != '/' && host_given == NULL) #endif { char cwd[MAXPATHLEN + 1] = {'\0'}; c = getenv("PWD"); /* PWD carries a name that will cause */ if (c != NULL) { /* the NFS to mount */ if (stat(c, &statbuf) < 0) { /* can't stat PWD */ c = NULL; } else { dev = statbuf.st_dev; ino = statbuf.st_ino; if (stat(".", &statbuf) < 0) { perror("prepare_path: cannot stat current directory:"); *path_out = '\0'; return (1); } } if (dev == statbuf.st_dev && ino == statbuf.st_ino) { snprintf(cwd, sizeof(cwd), "%s", c); } else { c = NULL; } } if (c == NULL) { c = getcwd(cwd, MAXPATHLEN); if (c == NULL) { perror("prepare_path: getcwd failed : "); *path_out = '\0'; return (1); } } #ifdef WIN32 /* get UNC path (if available) if it is mapped drive */ get_uncpath(cwd); if (IS_UNCPATH(cwd)) { strcpy(path_out, cwd); if (cwd[strlen(cwd)-1] != '\\') strcat(path_out, "\\"); } else #endif { strncat(path_out, cwd, (MAXPATHLEN + 1) - strlen(path_out)); if (strlen(path_out) < MAXPATHLEN) strcat(path_out, "/"); } } #ifdef WIN32 /* get UNC path (if available) if it is mapped drive */ get_uncpath(path_name); if (IS_UNCPATH(path_name)) strcpy(path_out, path_name); else { /* * check whether given <path_name> is relative path * without drive on localhost and <cwd> is not UNC path? * if yes then do not append drive into <path_out> * otherwise append drive into <path_out> */ if (is_local_host(host_name) && (strchr(path_name, ':') == NULL) && (path_out[strlen(path_out) - 1] != '/') && (!IS_UNCPATH(path_out))) { char drivestr[3] = {'\0'}; char drivestr_unc[MAXPATHLEN + 1] = {'\0'}; drivestr[0] = _getdrive() + 'A' - 1; drivestr[1] = ':'; drivestr[2] = '\0'; /* * check whether <drivestr> is mapped drive? * by calling get_uncpath() * if yes then remove <hostname> part from <path_out> * * This is the case when user submit job * from mapped drive with relative path without drive * in path ex. localhost:err or localhost:out */ snprintf(drivestr_unc, sizeof(drivestr_unc), "%s\\", drivestr); get_uncpath(drivestr_unc); if (IS_UNCPATH(drivestr_unc)) { strncpy(path_out, drivestr_unc, MAXPATHLEN); } else { strncat(path_out, drivestr, MAXPATHLEN - strlen(path_out)); } } strncat(path_out, path_name, MAXPATHLEN - strlen(path_out)); } back2forward_slash(path_out); /* "\" translate to "/" for path */ strcpy(path_out, replace_space(path_out, "\\ ")); path_out[MAXPATHLEN - 1] = '\0'; #else strncat(path_out, path_name, (MAXPATHLEN + 1) - strlen(path_out)); #endif return (0); }
/** * @brief * register_scm - return 0 for success; non-zero for fail * * @param[in] svc_name - service name * @param[in] svc_exec - service executable path * @param[in] svc_account - service account * @param[in] svc_password - service password * * @return int * @retval 0 : good to go! * @retval 1 : something bad happened. */ int register_scm(char *svc_name, char *svc_exec, char *svc_account, char *svc_password) { SC_LOCK sclLock = NULL; SC_HANDLE schService = NULL; SC_HANDLE schSCManager = NULL; int ret = 1; schSCManager = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS); if (!schSCManager) { fprintf(stderr, "OpenSCManager failed - %d\n", GetLastError()); goto register_scm_cleanup; } /* Get the SCM database lock before changing the password. */ sclLock = LockServiceDatabase(schSCManager); if (sclLock == NULL) { fprintf(stderr, "LockServiceDatabase failed - %d\n", GetLastError()); goto register_scm_cleanup; } /* Set the account and password that the service uses at */ /* startup. */ schService = CreateService(schSCManager, svc_name, __TEXT(svc_name), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, replace_space(svc_exec, ""), 0, 0, 0, svc_account, svc_password); if (!schService) { fprintf(stderr, "CreateService(%s, path=%s, account=%s) failed - %d\n", svc_name, svc_exec, svc_account, GetLastError()); goto register_scm_cleanup; } printf("\nCreated service %s with path=%s and account=%s\n", svc_name, svc_exec, svc_account); if (strcmpi(svc_name, "PBS_SCHED") == 0) { SERVICE_FAILURE_ACTIONS sfa; SC_ACTION sca[1]; sca[0].Type = SC_ACTION_RESTART; sca[0].Delay = 60*1000; sfa.dwResetPeriod = INFINITE; sfa.lpRebootMsg = NULL; sfa.lpCommand = NULL; sfa.cActions = 1; sfa.lpsaActions = sca; ChangeServiceConfig2(schService, SERVICE_CONFIG_FAILURE_ACTIONS, &sfa); printf("\nConfigured %s to restart on failure\n", svc_name); } ret = 0; register_scm_cleanup: if (sclLock) UnlockServiceDatabase(sclLock); if (schService) CloseServiceHandle(schService); if (schSCManager) CloseServiceHandle(schSCManager); return (ret); }
/** * @brief * main - the initialization and main loop of pbs_comm * * @param[in] argc - argument count. * @param[in] argv - argument values. * * @return int * @retval 0 - success */ main(int argc, char *argv[]) { SC_HANDLE schManager; SC_HANDLE schSelf; int reg = 0; int unreg = 0; TCHAR szFileName[MAX_PATH]; /*the real deal or just pbs_version and exit*/ execution_mode(argc, argv); if (argc > 1) { if (strcmp(argv[1], "-R") == 0) reg = 1; else if (strcmp(argv[1], "-U") == 0) unreg = 1; else if (strcmp(argv[1], "-N") == 0) stalone = 1; } if (reg || unreg) { schManager = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS); if (schManager == 0) { ErrorMessage("OpenSCManager"); } if (reg) { GetModuleFileName(0, szFileName, sizeof(szFileName)/sizeof(*szFileName)); printf("Installing service %s\n", g_PbsCommName); schSelf = CreateService(schManager, g_PbsCommName, __TEXT("PBS COMM"), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, replace_space(szFileName, ""), 0, 0, 0, 0, 0); if (schSelf) { printf("Service %s installed succesfully!\n", g_PbsCommName); } else { ErrorMessage("CreateService"); } if (schSelf != 0) CloseServiceHandle(schSelf); } else if (unreg) { schSelf = OpenService(schManager, g_PbsCommName, DELETE); if (schSelf) { if (DeleteService(schSelf)) { printf("Service %s uninstalled successfully!\n", g_PbsCommName); } else { ErrorMessage("DeleteService"); } } else { ErrorMessage("OpenService failed"); } if (schSelf != 0) CloseServiceHandle(schSelf); } if (schManager != 0) CloseServiceHandle(schManager); } else if (stalone) { struct arg_param *pap; int i, j; pap = create_arg_param(); if (pap == NULL) ErrorMessage("create_arg_param"); pap->argc = argc-1; /* don't pass the second argument */ for (i=j=0; i < argc; i++) { if (i == 1) continue; pap->argv[j] = strdup(argv[i]); j++; } main_thread((void *)pap); free_arg_param(pap); } else { /* running as a service */ SERVICE_TABLE_ENTRY rgste[] = { {(TCHAR*)g_PbsCommName, PbsCommMain }, { 0, 0 } }; if (getenv("PBS_CONF_FILE") == NULL) { char conf_path[80]; char *p; char psave; struct stat sbuf; if (p = strstr(argv[0], "exec")) { psave = *p; *p = '\0'; _snprintf(conf_path, 79, "%spbs.conf", argv[0]); *p = psave; if (stat(conf_path, &sbuf) == 0) { setenv("PBS_CONF_FILE", conf_path, 1); } } } if (!StartServiceCtrlDispatcher(rgste)) { ErrorMessage("StartServiceCntrlDispatcher"); } } return (0); }