Re: macro to print filename

Aniruddha Bhattacharyya <[email protected]> Thu, 2 Jun 2011 00:54:59 +0530
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
From what I understood, Ratheesh wanted a compile time solution.

My workaround will be to go for  a Makefile based compilation.
1)  "gcc -E yourfile.c"  will preprocess only. so __FILE__ will be expanded .

output file will be yourfile.c (with macro expanded)

2) then run your custom tool/bash script to trim the filepath to filename.
use awk tool maybe.

3) Do the final compile using Gcc


On Thu, Jun 2, 2011 at 12:41 AM, Michal Nazarewicz <[email protected]> wrote:
>
> On Wed, 01 Jun 2011 20:08:50 +0200, Zhongye Jia <[email protected]> wrote:
>>
>> #include <stdio.h>
>>
>> int main()
>> {
>>        char filename[] = __FILE__, *basename;
>>        basename = filename + sizeof(filename) - 2;
>>        while( basename+1 != filename && *basename != '/' ) {
>>                basename--;
>>        }
>>        basename++;
>
> And you are not using strchr() why?  I know OP said about not using
> string libraries but I don't think he meant that he's fine with
> reimplementing the wheel.
>
>>        printf("%s\n%s\n", filename, basename);
>>        return 0;
>> }
>
> --
> Best regards,                                         _     _
> .o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
> ..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
> ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to [email protected]
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html