int main() { char line[] = "is textbook was bought from that bookstore"; //char line[] = "aaaaaa"; char *p1, *p2; //set p1 to the beginning of string line; p1 = line; while ( *p1 != '\0' ) //more to check with p1 ) { //set p2 to the position immediately after p1 p2 = p1+1; while ( *(p2+LEN) != '\0') //more to check with p2 ) { //if a match is found int x = strncmp(p1, p2, LEN); if(x == 0) goto done; //advance p2 to the next position p2++; } //advance p1 to the next position p1++; } done: printf ("the first substring: %s\n", strnsub(p1, LEN)); printf ("the second substring: %s\n", strnsub(p2, LEN)); return 0; }
int _CfnTYPE repstr(FILE *fpIn, FILE *fpOut, char *PatternTable[][2]) { char szBuffer[BUFFERSIZE+1] ; char szTmpBuffer[BUFFERSIZE+1] ; long lFileLength ; long lFilePos = 0 ; int i = 0 ; int iStrLength ; /*----------------------------------------*/ memset(szBuffer, '\0', BUFFERSIZE) ; iStrLength = fread(szBuffer, BLOKSIZE, 1, fpIn); /* Read first block */ lFilePos = BLOKSIZE ; iStrLength = fread(&szBuffer[BLOKSIZE], BLOKSIZE, 1, fpIn) ; lFilePos += BLOKSIZE ; while( 0 != iStrLength ) { /* WHILE able to read */ i = 0 ; lFilePos += BLOKSIZE ; while ( NULL != PatternTable[i][0][0] ) { /* For all patterns */ while (0 != strnsub(szBuffer, PatternTable[i][0], PatternTable[i][1], BUFFERSIZE)) { ; /* If found replace */ } i++ ; } /* Replace pattern with replacement */ fwrite(&szBuffer, strlen(szBuffer) - BLOKSIZE, 1, fpOut) ; /* Write szBuffer execept last block */ fflush(fpOut) ; memmove(&szBuffer, &szBuffer[strlen(szBuffer) - BLOKSIZE], BLOKSIZE+1) ; memset(&szBuffer[BLOKSIZE], '\0', BLOKSIZE) ; iStrLength = fread(&szBuffer[BLOKSIZE], BLOKSIZE, 1, fpIn) ; szBuffer[BLOKSIZE + BLOKSIZE] = '\0' ; } i = 0 ; while (PatternTable[i][0][0] != NULL) { /* For all patterns */ while (0 != strnsub(szBuffer, PatternTable[i][0], PatternTable[i][1], BUFFERSIZE)) { ; } i++ ; } /* Replace pattern with replacement */ fwrite(&szBuffer, strlen(szBuffer), 1, fpOut) ; /* Write buffer */ lFilePos += strlen(&szBuffer[BLOKSIZE]) ; return(0) ; }
//! //! //! //! @param[in] loc //! @param[in] str //! //! @return EUCA_OK on success or EUCA_ERROR on failure //! //! @pre //! //! @post //! int parse_img_spec(img_loc * loc, const char *str) { #define _SIZE 512 int i = 0; int status = 0; int bufLen = 0; char *t = NULL; char *ex = NULL; char *pt = NULL; char s[1] = ""; char port[_SIZE] = ""; char low[_SIZE] = ""; char path[SIZE] = ""; regex_t re = { 0 }; regmatch_t pmatch[8] = { {0} }; bzero(loc, sizeof(img_loc)); // so all strings are null-terminated for (i = 0; i < _SIZE && i < strlen(str); i++) { low[i] = tolower(str[i]); } if (!strncmp(low, "http://", 7) || !strncmp(low, "https://", 8)) { if (strstr(str, "services/objectstorage")) { loc->type = OBJECTSTORAGE; euca_strncpy(loc->url, str, sizeof(loc->url)); } else if (strstr(str, "dcPath=")) { // EXAMPLE: https://192.168.7.236/folder/i-4DD50852?dcPath=ha-datacenter&dsName=S1 ex = "^[Hh][Tt][Tt][Pp][Ss]?://([^/]+)/([^\\?]+)\\?dcPath=([^&]+)&dsName=(.*)$"; if (regcomp(&re, ex, REG_EXTENDED) != 0) { LOGERROR("failed to compile regular expression for vSphere URL: %s\n", ex); return EUCA_ERROR; } status = regexec(&re, str, (size_t) 5, pmatch, 0); regfree(&re); if (status != 0) { LOGERROR("failed to match the syntax of vSphere URL (%s)\nwith regular expression %s\n", str, ex); return EUCA_ERROR; } if (re.re_nsub != 4) { LOGERROR("unexpected number of matched elements in %s\n", ex); return EUCA_ERROR; } loc->type = VSPHERE; euca_strncpy(loc->url, str, sizeof(loc->url)); strnsub(loc->host, str, pmatch[1].rm_so, pmatch[1].rm_eo, sizeof(loc->host)); strnsub(path, str, pmatch[2].rm_so, pmatch[2].rm_eo, sizeof(loc->path)); strnsub(loc->vsphere_dc, str, pmatch[3].rm_so, pmatch[3].rm_eo, sizeof(loc->vsphere_dc)); strnsub(loc->vsphere_ds, str, pmatch[4].rm_so, pmatch[4].rm_eo, sizeof(loc->vsphere_ds)); // extract path, split into directory and filename loc->dir[0] = '\0'; loc->file[0] = '\0'; loc->path[0] = '\0'; t = strtok(path, "/"); if (t == NULL || strcmp(t, "folder") != 0) { LOGERROR("failed to parse path in URL (must begin with 'folder'): %s...\n", path); return EUCA_ERROR; } bufLen = sizeof(loc->dir) - 1; while ((t = strtok(NULL, "/")) != NULL) { if (pt) { if (loc->dir[0] != '\0') { strncat(loc->dir, "/", (sizeof(loc->dir) - strlen(loc->dir) - 1)); } strncat(loc->dir, pt, (sizeof(loc->dir) - strlen(loc->dir) - 1)); } pt = t; } if (pt) { strncat(loc->file, pt, (sizeof(loc->file) - strlen(loc->file) - 1)); } if (loc->dir[0] == '\0') { euca_strncpy(loc->path, loc->file, sizeof(loc->path)); } else { snprintf(loc->path, sizeof(loc->path), "%s/%s", loc->dir, loc->file); } LOGDEBUG("re_nsub=%ld host=%s path='%s' ('%s' + '%s') dc=%s ds=%s\n", re.re_nsub, loc->host, loc->path, loc->dir, loc->file, loc->vsphere_dc, loc->vsphere_ds); } else { loc->type = HTTP; // EXAMPLE: http://192.168.7.236:902/foobar?foo=bar ex = "^[Hh][Tt][Tt][Pp]([Ss])?://([^/^:]+)(:([0-9]+))?(/[^\\?]+)?(\\?(.*))?$"; if (regcomp(&re, ex, REG_EXTENDED) != 0) { LOGERROR("failed to compile regular expression for URL: %s\n", ex); return EUCA_ERROR; } status = regexec(&re, str, (size_t) 8, pmatch, 0); regfree(&re); if (status != 0) { LOGERROR("failed to match the syntax of URL (%s)\nwith regular expression %s\n", str, ex); return EUCA_ERROR; } if (re.re_nsub != 7) { LOGERROR("unexpected number of matched elements in %s\n", ex); return EUCA_ERROR; } bzero(port, sizeof(port)); strnsub(s, str, pmatch[1].rm_so, pmatch[1].rm_eo, sizeof(s)); if (s[0] == 's') loc->type = HTTPS; euca_strncpy(loc->url, str, sizeof(loc->url)); strnsub(loc->host, str, pmatch[2].rm_so, pmatch[2].rm_eo, sizeof(loc->host)); strnsub(port, str, pmatch[4].rm_so, pmatch[4].rm_eo, sizeof(port) - 1); loc->port = atoi(port); strnsub(loc->path, str, pmatch[5].rm_so, pmatch[5].rm_eo, sizeof(loc->path)); strnsub(loc->params, str, pmatch[7].rm_so, pmatch[7].rm_eo, sizeof(loc->params)); } } else { loc->type = PATH; euca_strncpy(loc->path, str, sizeof(loc->path)); } return EUCA_OK; #undef _SIZE }