[CRIU] [PATCH 1/7] util: Introduce strstartswith helper

Pavel Emelyanov xemul at parallels.com
Thu May 8 06:23:36 PDT 2014


On 05/08/2014 05:19 PM, Cyrill Gorcunov wrote:
> On Thu, May 08, 2014 at 05:07:23PM +0400, Pavel Emelyanov wrote:
>> Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
>> ---
>>  include/util.h | 18 ++++++++++++++++++
>>  1 file changed, 18 insertions(+)
>>
>> diff --git a/include/util.h b/include/util.h
>> index c4b8e69..40dd1dc 100644
>> --- a/include/util.h
>> +++ b/include/util.h
>> @@ -277,4 +277,22 @@ extern int read_fd_link(int lfd, char *buf, size_t size);
>>  
>>  int vaddr_to_pfn(unsigned long vaddr, u64 *pfn);
>>  
>> +/*
>> + * Check whether @str starts with @sub
>> + */
>> +static inline bool strstartswith(char *str, char *sub)
>> +{
>> +	while (1) {
>> +		if (*sub == '\0') /* end of sub -- match */
>> +			return true;
>> +		if (*str == '\0') /* end of str, sub is NOT ended -- miss */
>> +			return false;
>> +		if (*str != *sub)
>> +			return false;
>> +
>> +		str++;
>> +		sub++;
>> +	}
>> +}
> 
> Don't you rather compare two strings? Why not to use strcmp here?

Because

strcmp("foo/bar", "foo") == false
strstartswith("foo/bar", "foo") == true

:)


More information about the CRIU mailing list