Markus Rechberger / C
 
StartSeite | MarkusRechberger/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern

Unterseiten

9 Seiten:Datum der letzten Änderung
MarkusRechberger/C/ResolveFilename16. April 2005 18:26
MarkusRechberger/C/libdvdread9. April 2005 18:15
MarkusRechberger/C/regex7. April 2005 14:52
MarkusRechberger/C/CoreDump4. April 2005 10:53
MarkusRechberger/C/Semaphoren5. März 2005 21:52
MarkusRechberger/C/Obstack24. Februar 2005 21:27
MarkusRechberger/C/ThreadQueues21. Februar 2005 22:19
MarkusRechberger/C/imlib226. Januar 2005 10:40
MarkusRechberger/C/DotConf26. Januar 2005 10:35

Tips/Tricks

Another nice thought from izik about reverse engineering using {LD PRELOAD}?

 *  http://neworder.box.sk/newsread.php?newsid=13857

Shifting

Durch das nach rechts shiften von foo wird der ganze wert durch 2 dividiert, durch links shiften wird das ganze *2 multipliziert

#include <stdio.h>

int main(){
        int foo=28;
        printf("foo/2 = %d\n",foo>>1);
        printf("foo*2 = %d\n",foo<<1);
        return(0);
}

http://www.evergreen.edu/biophysics/technotes/misc/bin_math.htm, Wiederholung Grundlagen Datenverarbeitung!

Bitgröße eines Datentyps in structs

> What's the meaning of the following definition:
> int a:8;
>
> thanks

This declaration creates an 8-bit integer.  This syntax is only used within
structures for defining bit fields.
struct Anything
{
    unsigned int   condition_code:4;
    unsigned int   operation_code:4;
    unsigned int   offset:8;
};

#include <stdio.h>

struct test{
        unsigned int foo:1;
};

int main(){
        struct test mare;
        mare.foo=5;
        printf("%d\n",mare.foo);
        return(0);
}

mare.foo is 1 in that case

segfault vs alles ok

gcc4

 GCC no longer accepts the -fwritable-strings  option. Use named character arrays when you need a writable string.

gcc3.3

"Markus" ist ohne -fwriteable-strings schreibgeschützt

#include <stdio.h>

int main(){
        char *foo="Markus";
        sprintf(foo,"mare!\n");
        printf(foo);
        return(0);
}

Result:
/devel/c/aufgabe42$ gcc -fwritable-strings  test.c -o test
/devel/c/aufgabe42$ ./test
mare!
/devel/c/aufgabe42$ gcc test.c -o test
/devel/c/aufgabe42$ ./test
Segmentation fault

Linker

Eine Object file gegen ein Shared Library linken

ld -dynamic-linker /lib/ld-linux.so.2 /usr/lib/libc.so -o test test.o

Notes

aus der c mailing list:

printf("\e[0m\e[2J\e[1;1H"); /* ]]] */

It's ascii control character. 2J means clear the screen and 1;1H set the cursor position

Another way to do it:
   char *area[100], bp[2048] ;

   tgetent( bp, getenv( "TERM" )) ;
   puts( tgetstr( "cl", area )) ;

{LD PRELOAD}?

{LD PRELOAD}? erlaubt eine Überlagerung von Funktionen in Libraries,...

Beispiel: main.c
#include <dlfcn.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdarg.h>

int fprintf(FILE *stream, const char *format,...){
        va_list testing;
        int retval;
        printf("-- ACCESSING FPRINTF WRAPPER!! --\n");
        va_start (testing, format);
        retval=vfprintf(stream,format,testing);
        va_end(testing);
        printf("-- HIJACKED --\n");
        return(retval);
}
libtool --mode=link gcc -shared -{D REENTRANT}? main.c -o libmain.so

test.c
#include <stdio.h>

int main(){
        fprintf(stderr,"hallo!!\n");
        return(0);
}
make test

# export LD_PRELOAD=`pwd`/libmain.so
# ./test
hijacked

in diesem Fall könnte man auch Pointer Functions einsetzen zu bestimmten Funktionen, die Adresse des nächsten Symbols kann jeweils mit dlsym({RTLD NEXT}?,"symbol") ausgelesen werden..

PointerFunctions?

auch wenn ich's sogut wie nie benötige...

main.c
#include <stdio.h>


int testing(void){
        printf("this is a testfunc!\n");
        return(0);
}

int main(){
        int (*test)(void);
        
        test=&testing;
        test();

        return(0);
}
make main

misc

schon wieder beinahe vergessen..

       #include <ctype.h>

       int isalnum(int c);
       int isalpha(int c);
       int isascii(int c);
       int isblank(int c);
       int iscntrl(int c);
       int isdigit(int c);
       int isgraph(int c);
       int islower(int c);
       int isprint(int c);
       int ispunct(int c);
       int isspace(int c);
       int isupper(int c);
       int isxdigit(int c);

#include <stdio.h>

main()
{
  float a1=1.0;
  float a2=2.0;

  printf(" \n");
  printf(" Size of Float > %d \n", sizeof(float) );
  printf(" Size of Int   > %d \n", sizeof(int) );
  printf(" \n");

  printf(" CASE 1 : f-f > %f - %f \n\n", a1, a2);
  printf(" CASE 2 : f-d > %f - %d \n\n", a1, a2);
  printf(" CASE 3 : d-f > %d - %f \n\n", a1, a2);
  printf(" CASE 4 : d-d > %d - %d \n\n", a1, a2);
}

OUTPUT :

Size of Float > 4
Size of Int   > 4

CASE 1 : f-f > 1.000000 - 2.000000

CASE 2 : f-d > 1.000000 - 0

CASE 3 : d-f > 0 - 0.000000

CASE 4 : d-d > 0 - 1072693248

test.c:4: warning: return type defaults to `int'
test.c: In function `main':
test.c:15: warning: int format, double arg (arg 3)
test.c:16: warning: int format, double arg (arg 2)
test.c:18: warning: int format, double arg (arg 2)
test.c:18: warning: int format, double arg (arg 3)
test.c:20: warning: control reaches end of non-void function

it seems to be a feature in C.
[ from another mailing list:

https://lists.openafs.org/pipermail/openafs-devel/2003-July/009525.html
]

The text from the ISO 1999 standard in 6.5.2.2 is:

6 If the expression that denotes the called function has a type that
does
 not include a prototype, the integer promotions are performed on each
 argument, and arguments that have type float are promoted to
 double. These are called the default argument promotions. [...]

7 If the expression that denotes the called function has a type that
does
 include a prototype, the arguments are implicitly converted, as if by
 assignment, to the types of the corresponding parameters, taking the
 type of each parameter to be the unqualified version of its declared
 type. The ellipsis notation in a function prototype declarator causes
 argument type conversion to stop after the last declared parameter.
The
 default argument promotions are performed on trailing arguments.

also check glibc's manual (may be out of date ??):
http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_28.html#SEC478

Large file support

Infos/Historie: http://www.unix.org/version2/whatsnew/lfs.html

Cscope

praktisch um größere Projekte nach funktionen/.. zu durchsuchen

http://cscope.sourceforge.net/cscope_vim_tutorial.html

Embed perl within C

perldoc perlembed
perldoc perlxs
perldoc perlapi

is what you will need to embed your Perl program in C. as a simple example,
the following embeds a Perl script, test.pl, into a C program test.c.
test.c simply runs a regexp and extract the emails address that it finds in
test.pl:

#--
#-- test.pl
#--
$email = '...@perl.com,y...@perl.com';

#--
#-- the following is for demo puprose, don't use this
#-- for extracting email address out of a string. you
#-- can embed any Perl code you want here instead.
#--
@emails = $email =~ /([^,]+@[^,]+)/g;

__END__

the following C program will embed the above Perl script and prints
out the email address in @emails:

#include <EXTERN.h>
#include <perl.h>

static PerlInterpreter* perl;
static AV *emails;

int main(int argc,char** argv){

        int x;
        STRLEN y;

        perl = perl_alloc();
        perl_construct(perl);
        perl_parse(perl,NULL,argc,argv,NULL);
        perl_run(perl);

        // get @emails in test.pl
        emails = get_av("emails",0);

        // av_len(emails) is the same as $#emails
        for(x = 0; x <= av_len(emails); x++){

                // av_fetch(emails,x,0) is the same as $emails[$x]
                SV* email = *av_fetch(emails,x,0);

                // SvPV(email,y) "converts" an array element to a string
                printf("email %d is: %s\n",x+1,SvPV(email,y));
        }

        perl_destruct(perl);
        perl_free(perl);

}

to compile this C program, you will need to know how Perl is build
in your machine, the following usually does the trick:

[panda]# gcc test.c -o test `perl -MExtUtils::Embed -e ccopts -e ldops`

finally, to test your executable:

[panda]# ./test test.pl

assuming test.pl is in the same directory as test.c, you will see:

m...@perl.com
y...@perl.com 

the arrow operator

The symbol -> is known as the ``arrow operator''. The arrow operator can be used with pointers to struct variables and objects. The expression

 p->whatever 

is simply another way of writing
 (*p).whatever

Casting

21:19 < bniemczyk> revenger: and don't cast malloc
21:19 < bniemczyk> btw
21:19 < bniemczyk> revenger: #include <stdlib.h>
21:20 < bniemczyk> to get rid of the warning you were getting that led you to believe you should cast it
21:20 < bniemczyk> also, i'm betting you wanted to make it so that la->test would be changed after a call to function
21:20 < Manny> bniemczyk: what's wrong with casting malloc?
21:20 < bniemczyk> the way you are doing it won't
21:20 < bniemczyk> Manny: it hides the error of not including stdlib.h


StartSeite | MarkusRechberger/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern
Text dieser Seite ändern (zuletzt geändert: 31. Juli 2005 21:32 (diff))
Suchbegriff: gesucht wird
im Titel
im Text