VOID ComboAdd(COMBO ptr, LPTSTR Item) { INT i; Combo *c = (Combo*) ptr; if(StringIsEmpty(Item)) return; for(i=0; i<c->HistoryLen; i++) if(StringCmp(c->History[i],Item)==0) { LPTSTR aux = c->History[c->HistoryLen-1]; c->History[c->HistoryLen-1] = c->History[i]; c->History[i] = aux; return; } if(c->HistoryLen<MAX_HISTORY) { c->History[c->HistoryLen] = StringDup(Item); (c->HistoryLen)++; } else { free(c->History[0]); for(i=0;i<c->HistoryLen-1;i++) c->History[i] = c->History[i+1]; c->History[c->HistoryLen-1] = StringDup(Item); } }
COMBO NewCombo(LPTSTR Name, INT CtrlID, LPTSTR DefaultEntry) { Combo *c = malloc(sizeof(Combo)); c->Name = StringDup(Name); c->CtrlID = CtrlID; c->HistoryLen = 0; c->DefaulEntry = StringDup(DefaultEntry); return (COMBO) c; }
void processPurchase() { String customer_name; String object; int quantity; int customer_present = 0; readString(&customer_name); readString(&object); readNum(&quantity); for (int k = 0; k < num_customers; k += 1) { //check every existing customer if (StringIsEqualTo(&(customers[k].name), &customer_name)) { customer_present = 1; processCustomerRequest(&customer_name, &object, quantity, k); } } //non existing customers here if (!customer_present && num_customers < MAX_CUSTOMERS) { if(!processCustomerRequest(&customer_name, &object, quantity, num_customers) && quantity > 0){ //enough inventory to purchase, add customer to database customers[num_customers].name = StringDup(&customer_name); num_customers += 1; } } //destroy strings used for error messages StringDestroy(&customer_name); StringDestroy(&object); }
VOID ReadComboFromRegistry(COMBO ptr) { INT i; TCHAR Buf[1024]; LPTSTR Res; Combo *c = (Combo*) ptr; for(i=0;i<MAX_HISTORY; i++) c->History[i] = NULL; for(i=0;i<MAX_HISTORY; i++) { wsprintf(Buf,TEXT("%s%d"),c->Name,i); Res = readRegStrDup(Buf, TEXT("")); if (StringIsEmpty(Res)) { free(Res); break; } else c->History[i] = Res; } c->HistoryLen = i; if(c->HistoryLen==0) { c->History[0] = StringDup(c->DefaulEntry); (c->HistoryLen)++; } }
void *Mount( struct FHandler *s, struct TagItem *ti, User *usr ) { File *dev = NULL; char *path = NULL; char *name = NULL; void *sb; if( s == NULL ) { return NULL; } DEBUG("Mounting RAM filesystem!\n"); if( ( dev = calloc( 1, sizeof( File ) ) ) != NULL ) { struct TagItem *lptr = ti; // // checking passed arguments while( lptr->ti_Tag != TAG_DONE ) { switch( lptr->ti_Tag ) { case FSys_Mount_Path: break; case FSys_Mount_Server: break; case FSys_Mount_Port: break; case FSys_Mount_Name: name = (char *)lptr->ti_Data; break; case FSys_Mount_SysBase: sb = (void *)lptr->ti_Data; break; } lptr++; } SpecialData *srd = calloc( 1, sizeof( SpecialData ) ); srd->root = INRAMFileNew( INRAM_ROOT, name, name ); dev->f_SpecialData = srd; srd->sb = sb; dev->f_FSys = s; dev->f_Position = 0; dev->f_User = usr; dev->f_Name = StringDup( name ); dev->f_Type = FType_Directory; dev->f_Size = 0; } DEBUG("RAMFS mount ok\n"); return dev; }
int main(void) { String s = StringCreate("Craig"); String t = StringDup(s); t = s; printf("Hello "); StringPrint(s); printf(" I see you're %d characters long\n", StringSize(s)); StringDestroy(s); StringDestroy(t); }
VOID FireCommandExt(LPCTSTR Command, BOOL WaitForResponse, BOOL startThread, BOOL StopStdoutPrinterThread, BOOL WantPreprocess) { if(startThread) { DWORD ThreadId; FireCommandThreadArgs *pArgs = (FireCommandThreadArgs*) malloc(sizeof(FireCommandThreadArgs)); pArgs->WaitForResponse = WaitForResponse; pArgs->StopStdoutPrinterThread = StopStdoutPrinterThread; pArgs->Command = StringDup(Command); pArgs->WantPreprocess = WantPreprocess; if(!CreateThread(NULL,0,FireCommandThread,pArgs,0,&ThreadId)) ErrorExit(TEXT("CreateThread FireCommandThread failed\n")); } else FireCommandAux(Command, WaitForResponse, startThread, StopStdoutPrinterThread, WantPreprocess, NULL); }
int Rename( struct File *s, const char *path, const char *nname ) { DEBUG("Rename!\n"); int res = 0; int error = 0; SpecialData *srd = (SpecialData *) s->f_SpecialData; INRAMFile *dir =INRAMFileGetLastPath( srd->root, path, &error ); if( dir != NULL ) { free( dir->nf_Name ); free( dir->nf_Path ); dir->nf_Name = StringDup( nname ); int len = strlen( path ); char *temp = calloc( len+512, sizeof(char) ); if( temp != NULL ) { int i = len; strcpy( temp, path ); for( ; i >= 0 ; i-- ) { if( temp[ i ] == '/' ) { temp[ i+1 ] = 0; } } strcat( temp, nname ); dir->nf_Path = temp; } else { FERROR("Cannot allocate memory\n"); } } return res; }
void *FileOpen( struct File *s, const char *path, char *mode ) { int spath = strlen( path ); char *tmppath = FCalloc( spath+10, sizeof(char) ); memcpy( tmppath, path, spath ); File *locfil = NULL; DEBUG("File open\n"); SpecialData *srd = (SpecialData *)s->f_SpecialData; int error = 0; // we are takeing filename from path, path will be used to make directories only char *nameptr = (char *)path; int i; for( i=spath ; i >= 0 ; i-- ) { if( path[ i ] == '/' ) { nameptr = (char *)&path[i+1]; tmppath[ i ] = 0; break; } } DEBUG("File open 1 path %s tmppath %s nameptr %s\n", path, tmppath, nameptr ); INRAMFile *directory = NULL; if( strcmp( path, nameptr ) != 0 ) { directory = INRAMFileMakedirPath( srd->root, tmppath, &error ); } else { directory = srd->root; } if( directory != NULL ) { DEBUG("Directory where file will be create/exist %s\n", directory->nf_Name ); INRAMFile *nf = NULL; nf = INRAMFileGetChildByName( directory, nameptr ); // read DEBUG("\nINRAM opened file for %s\n\n", mode ); if( mode[ 0 ] == 'r' ) { if( nf == NULL ) { free( tmppath ); FERROR("Cannot open file %s\n", path ); return NULL; } nf->nf_Offset = 0; } else // write { if( nf == NULL ) { nf = INRAMFileNew( INRAM_FILE, tmppath, nameptr ); if( INRAMFileAddChild( directory, nf ) == 0 ) { } } else { } } // Ready the file structure if( ( locfil = calloc( sizeof( File ), 1 ) ) != NULL ) { locfil->f_Path = StringDup( path ); DEBUG("Fileopen, path duplicated %s\n", path ); locfil->f_SpecialData = calloc( 1, sizeof( SpecialData ) ); SpecialData *sd = (SpecialData *)locfil->f_SpecialData; if( sd ) { sd->fp = nf; } DEBUG("\nOffset set to %ld\n\n", nf->nf_Offset ); DEBUG("File open, descriptor returned\n"); return locfil; } } FFree( tmppath ); DEBUG("File open end\n"); return NULL; }
BOOL RtfNotify(HWND hDlg, NMHDR* nmhdr) { static BOOL FindFirst = TRUE; #define BUFF_LEN 2048 static TCHAR FindWhat[BUFF_LEN]; if (nmhdr->code == EN_PROTECTED && !PuttingChar) { //block ENPROTECTED* enp = (ENPROTECTED*) nmhdr; CHARRANGE cr; INT TextLen = RtfWindowTextLength(); BOOL Reset = FALSE, Disallow = FALSE; // just let it go ahead anyway if (enp->msg == WM_COPY) return FALSE; // they hit backspace if (enp->wParam == VK_BACK) { if ((DWORD) enp->chrg.cpMin < StartOfInput || ((DWORD) enp->chrg.cpMin == StartOfInput && enp->chrg.cpMin == enp->chrg.cpMax)) { Reset = TRUE; Disallow = TRUE; } } else if ((DWORD) enp->chrg.cpMin < StartOfInput) { Reset = TRUE; Disallow = (enp->wParam == VK_DELETE); } if (Reset) { cr.cpMin = TextLen; cr.cpMax = cr.cpMin; SendMessage(hWndRtf, EM_EXSETSEL, 0, (LPARAM) &cr); } // we don't want to paste rich text, as that makes it look weird // so send only plain text paste commands if ((enp->msg == WM_PASTE) && !Disallow) { LPTSTR Buffer = NULL; Disallow = TRUE; #if defined _UNICODE #define CLIP_FORMAT CF_UNICODETEXT #else #define CLIP_FORMAT CF_TEXT #endif if (IsClipboardFormatAvailable(CLIP_FORMAT) && OpenClipboard(hWndMain)) { HGLOBAL hGlb; LPTSTR str; if ((hGlb = GetClipboardData(CLIP_FORMAT)) != NULL && (str = GlobalLock(hGlb)) != NULL) { Buffer = StringDup(str); GlobalUnlock(hGlb); } CloseClipboard(); } if (Buffer != NULL) { // strip trailing new line characters INT i; for (i = StringLen(Buffer)-1; i >= 0 && (Buffer[i] == TEXT('\r') || Buffer[i] == TEXT('\n')); i--) Buffer[i] = 0; #if defined _UNICODE { SETTEXTEX stt; stt.codepage = CP_UNICODE; stt.flags = ST_SELECTION; SendMessage(hWndRtf,EM_SETTEXTEX,(WPARAM)&stt,(LPARAM)Buffer); } #else SendMessage(hWndRtf, EM_REPLACESEL, FALSE, (LPARAM)Buffer); #endif free(Buffer); } } return (Disallow ? TRUE : FALSE); } else if (nmhdr->code == EN_LINK) { // should really fire on up // but that screws up the cursor position ENLINK* enl = (ENLINK*) nmhdr; if (enl->msg == WM_LBUTTONDOWN) { TEXTRANGE tr; TCHAR Buffer[1000]; tr.lpstrText = Buffer; tr.chrg.cpMin = enl->chrg.cpMin; tr.chrg.cpMax = enl->chrg.cpMax; SendMessage(hWndRtf, EM_GETTEXTRANGE, 0, (LPARAM) &tr); ExecuteFile(Buffer); return TRUE; } } else if (nmhdr->code == EN_MSGFILTER) { MSGFILTER* mf = (MSGFILTER*) nmhdr; #if 0 //if (mf->msg == WM_CHAR && Running) { if (mf->msg == WM_CHAR && 0) { WinGHCiReceiveC((TCHAR)mf->wParam == TEXT('\r') ? TEXT('\n') : mf->wParam); SetWindowLong(hDlg, DWL_MSGRESULT, 1); return FALSE; } //else if (Running && mf->msg == WM_KEYDOWN) { else if (0) { SHORT n = GetKeyState(VK_CONTROL); BOOL Control = (n & (1 << 16)); if(((CHAR)(mf->wParam) ==(CHAR)TEXT('C')) && Control) AbortExecution(); else SetWindowLong(hDlg, DWL_MSGRESULT, 1); return FALSE; } //else if (mf->msg == WM_KEYDOWN && !Running) { else #endif if (mf->msg == WM_CHAR) { if (mf->wParam == VK_TAB) { INT pos; if(FindFirst) { RtfWindowGetCommand(FindWhat,BUFF_LEN); pos = FindFirstHistory(FindWhat); FindFirst = FALSE; } else pos = FindNextHistory(); if(pos>=0) RtfWindowSetCommand(GoToHistory(pos)); else MessageBeep((UINT)-1); return TRUE; } else { // any other key resets search FindFirst = TRUE; if (mf->wParam == VK_ESCAPE) { //Clear current command RtfWindowSetCommand(TEXT("")); // Go to last item in history AddHistory(TEXT("")); return TRUE; } else { return FALSE; } } } else if (mf->msg == WM_KEYDOWN) { BOOL History = (mf->wParam == VK_UP || mf->wParam == VK_DOWN); SHORT n = GetKeyState(VK_CONTROL); BOOL Control = (n & (1 << 16)); if(((CHAR)(mf->wParam) ==(CHAR)TEXT('C')) && Control) { if(RtfWindowCanCutCopy() && DROPEFFECT_COPY) RtfWindowClipboard(WM_COPY); else AbortExecution(); } else if (History && (mf->lParam & (1 << 24))) { CHARRANGE cr; SendMessage(hWndRtf, EM_EXGETSEL, 0, (LPARAM) &cr); if ((DWORD) cr.cpMin >= StartOfInput) { RtfWindowRelativeHistory(mf->wParam == VK_UP ? -1 : +1); return TRUE; } } else if (mf->wParam == VK_RETURN) { #define BUFF_LEN 2048 TCHAR Buffer[BUFF_LEN]; RtfWindowGetCommand(Buffer,BUFF_LEN); if(Running) { if(!(mf->lParam & (1<<30))) //avoid repetition FireCommandExt(Buffer,FALSE,TRUE,FALSE,FALSE); } else //if(!(mf->lParam & (1<<30))) FireAsyncCommand(Buffer); return TRUE; } else if (mf->wParam == VK_HOME) { CHARRANGE cr; SendMessage(hWndRtf, EM_EXGETSEL, 0, (LPARAM) &cr); if ((DWORD) cr.cpMin >= StartOfInput) { SHORT n = GetKeyState(VK_SHIFT); BOOL Shift = (n & (1 << 16)); cr.cpMin = StartOfInput; cr.cpMax = (Shift ? cr.cpMax : StartOfInput); SendMessage(hWndRtf, EM_EXSETSEL, 0, (LPARAM) &cr); return TRUE; } } } } else if (nmhdr->code == EN_SELCHANGE) { EnableButtons(); return FALSE; } return FALSE; }
void *FileOpen( struct File *s, const char *path, char *mode ) { //Read //system.library/module?module=files&command=read&sessionid=78346h89379376&path=some:directory/file.txt SpecialData *sd = (SpecialData *)s->f_SpecialData; char *comm = NULL; if( ( comm = calloc( strlen( path ) +512, sizeof(char) ) ) != NULL ) { strcpy( comm, s->f_Name ); strcat( comm, ":" ); if( path != NULL ) { strcat( comm, path ); } if( sd != NULL ) { if( mode[0] == 'r' || strcmp( mode, "rb" ) == 0 ) { char tmpfilename[ 712 ]; int lockf; // Make sure we can make the tmp file unique with lock! int retries = 100; do { sprintf( tmpfilename, "/tmp/%s_read_%d%d%d%d", s->f_SessionID, rand()%9999, rand()%9999, rand()%9999, rand()%9999 ); DEBUG( "[fsysphp] Trying to lock %s\n", tmpfilename ); if( ( lockf = open( tmpfilename, O_CREAT|O_EXCL|O_RDWR ) ) >= 0 ) { break; } unlink( tmpfilename ); // Failed.. bailing if( retries-- <= 0 ) { ERROR( "[fsysphp] [FileOpen] Failed to get exclusive lock on lockfile.\n" ); return NULL; } } while( TRUE ); DEBUG( "[fsysphp] Success in locking %s\n", tmpfilename ); // Open the tmp file and get a file lock! // Get the data char command[ 1024 ]; // maybe we should count that... sprintf( command, "php \"modules/system/module.php\" \"type=%s&module=files&args=false&command=read&authkey=false&sessionid=%s&path=%s&mode=%s\";", sd->type, s->f_SessionID, comm, mode ); DEBUG( "[fsysphp] Getting data for tempfile, seen below as command:\n" ); DEBUG( "[fsysphp] %s\n", command ); int answerLength = 0; ListString *result = PHPCall( command, &answerLength ); // Open a file pointer if( result && result->ls_Data ) { // Write the buffer to the file int written = write( lockf, ( void *)result->ls_Data, result->ls_Size ); // Clean out result ListStringDelete( result ); result = NULL; // Remove lock! FILE *locfp = NULL; fcntl( lockf, F_SETLKW, F_UNLCK ); fchmod( lockf, 0755 ); close( lockf ); lockf = -1; if( ( locfp = fopen( tmpfilename, mode ) ) != NULL ) { // Flick the lock off! fseek ( locfp, 0, SEEK_SET ); // Ready the file structure File *locfil = NULL; if( ( locfil = FCalloc( 1, sizeof( File ) ) ) != NULL ) { locfil->f_Path = StringDup( path ); if( (locfil->f_SpecialData = FCalloc( 1, sizeof( SpecialData ) ) ) != NULL ) { sd->fp = locfp; SpecialData *locsd = (SpecialData *)locfil->f_SpecialData; locsd->fp = locfp; locsd->mode = MODE_READ; locsd->fname = StringDup( tmpfilename ); locsd->path = StringDup( path ); locfil->f_SessionID = StringDup( s->f_SessionID ); DEBUG("[fsysphp] FileOpened, memory allocated for reading.\n" ); return locfil; } // Free this one FFree( locfil->f_Path ); FFree( locfil ); } // Close the dangling fp fclose( locfp ); } else { ERROR("[fsysphp] Cannot open temporary file %s\n", tmpfilename ); } } else { ERROR("[fsysphp] Cannot create temporary file %s\n", tmpfilename ); } // Close lock if( lockf >= 0 ) { DEBUG( "[fsysphp] Closing lock..\n" ); close( lockf ); } } else if( mode[0] == 'w' ) { char tmpfilename[ 712 ]; // Make sure we can make the tmp file unique do { sprintf( tmpfilename, "/tmp/%s_write_%d%d%d%d", s->f_SessionID, rand()%9999, rand()%9999, rand()%9999, rand()%9999 ); } while( access( tmpfilename, F_OK ) != -1 ); DEBUG("[fsysphp] WRITE FILE %s\n", tmpfilename ); FILE *locfp; if( ( locfp = fopen( tmpfilename, "w+" ) ) != NULL ) { File *locfil = NULL; if( ( locfil = FCalloc( sizeof( File ), 1 ) ) != NULL ) { locfil->f_Path = StringDup( path ); if( ( locfil->f_SpecialData = FCalloc( 1, sizeof( SpecialData ) ) ) != NULL ) { SpecialData *locsd = (SpecialData *)locfil->f_SpecialData; locsd->fp = locfp; locsd->mode = MODE_WRITE; locsd->fname = StringDup( tmpfilename ); locsd->path = StringDup( path ); locfil->f_SessionID = StringDup( s->f_SessionID ); DEBUG("[fsysphp] FileOpened, memory allocated\n"); return locfil; } // Free it FFree( locfil->f_Path ); FFree( locfil ); } // Close the dangling fp fclose( locfp ); } else { ERROR("Cannot create temporary file %s\n", tmpfilename ); } } else { ERROR("Mode not supported\n"); } } free( comm ); } // comm return NULL; }
void *Mount( struct FHandler *s, struct TagItem *ti ) { File *dev = NULL; char *path = NULL; char *name = NULL; User *usr = NULL; char *module = NULL; char *type = NULL; char *authid = NULL; if( s == NULL ) { return NULL; } DEBUG("[fsysphp] Mounting PHPFS filesystem!\n"); if( ( dev = FCalloc( 1, sizeof( File ) ) ) != NULL ) { struct TagItem *lptr = ti; // typical mount call // 'php "modules/system/module.php" "type=corvo&devname=HomeSql&path=&module=system&unmount=%5Bobject%20Object%5D&sessionid=0eff3a525c8e0495301f7418bd6b6ce358a995e6";' // // checking passed arguments // while( lptr->ti_Tag != TAG_DONE ) { switch( lptr->ti_Tag ) { //printf("TAG %x\n", lptr->ti_Tag); case FSys_Mount_Path: path = (char *)lptr->ti_Data; //DEBUG("Mount FS path set '%s' len %d\n", path, strlen( path ) ); break; case FSys_Mount_Host: break; case FSys_Mount_Port: break; case FSys_Mount_Name: name = (char *)lptr->ti_Data; break; case FSys_Mount_User: usr = (User *)lptr->ti_Data; break; case FSys_Mount_Type: type = (char *)lptr->ti_Data; //INFO("TYPE PASSED %s size %d\n", type, strlen( type ) ); break; case FSys_Mount_Module: module = (char *)lptr->ti_Data; break; } lptr++; } // /* if( path == NULL )// || strlen( path ) < 1 ) { DEBUG("[ERROR]: Path option not found!\n"); free( dev ); return NULL; } */ init( s ); // we are trying to open folder/connection if( path != NULL ) { if( strlen( path ) < 1 ) { dev->f_Path = calloc( 2, sizeof(char) ); } else { dev->f_Path = StringDup( path ); } DEBUG("[fsysphp] phpfs path is ok '%s' (ignore this message, unimplemented!)\n", dev->f_Path ); } dev->f_FSys = s; dev->f_Position = 0; dev->f_User = usr; dev->f_Name = StringDup( name ); dev->f_Type = FType_Directory; dev->f_Size = 0; SpecialData *sd = calloc( 1, sizeof( SpecialData ) ); if( sd != NULL ) { sd->module = StringDup( module ); dev->f_SessionID = StringDup( usr->u_SessionID ); sd->type = StringDup( type ); dev->f_SpecialData = sd; char command[ 2048 ]; // maybe we should count that... sprintf( command, "php \"modules/system/module.php\" \"command=mount&type=%s&devname=%s&path=%s&module=%s&sessionid=%s\";", type, name, path, module, usr->u_SessionID ); int answerLength; ListString *result = PHPCall( command, &answerLength ); if( result && result->ls_Size >= 0 ) { DEBUG( "[fsysphp] Return was \"%s\"\n", result->ls_Data ); if( strncmp( result->ls_Data, "ok", 2 ) != 0 ) { DEBUG( "[fsysphp] Failed to mount device %s..\n", name ); DEBUG( "[fsysphp] Output was: %s\n", result->ls_Data ); if( sd->module ) FFree( sd->module ); if( dev->f_SessionID ) FFree( dev->f_SessionID ); if( sd->type ) FFree( sd->type ); if( dev->f_Name ) FFree( dev->f_Name ); if( dev->f_Path ) FFree( dev->f_Path ); FFree( sd ); FFree( dev ); // Free up buffer ListStringDelete( result ); return NULL; } } else { DEBUG( "[fsysphp] Error mounting device %s..\n", name ); if( sd->module ) FFree( sd->module ); if( dev->f_SessionID ) FFree( dev->f_SessionID ); if( sd->type ) FFree( sd->type ); if( dev->f_Name ) FFree( dev->f_Name ); if( dev->f_Path ) FFree( dev->f_Path ); FFree( sd ); FFree( dev ); // Free up buffer if( result ) ListStringDelete( result ); return NULL; } if( result ) ListStringDelete( result ); } DEBUG("[fsysphp] IS DIRECTORY data filled\n"); } DEBUG("[fsysphp] mount ok\n"); return dev; }
void *Mount( struct FHandler *s, struct TagItem *ti ) { File *dev = NULL; char *path = NULL; char *name = NULL; char *host = NULL; char *ulogin = NULL; char *upass = NULL; int port = 22; User *usr = NULL; if( s == NULL ) { return NULL; } DEBUG("Mounting ssh2 filesystem!\n"); if( ( dev = calloc( sizeof( File ), 1 ) ) != NULL ) { struct TagItem *lptr = ti; // // checking passed arguments while( lptr->ti_Tag != TAG_DONE ) { switch( lptr->ti_Tag ) { case FSys_Mount_Path: path = (char *)lptr->ti_Data; DEBUG("Mount FS path set '%s'\n", path ); break; case FSys_Mount_Host: host = (char *)lptr->ti_Data; break; case FSys_Mount_Port: port = atol( (char *)lptr->ti_Data ); break; case FSys_Mount_Name: name = (char *)lptr->ti_Data; break; case FSys_Mount_User: usr = (User *)lptr->ti_Data; break; case FSys_Mount_LoginUser: ulogin = (char *)lptr->ti_Data; break; case FSys_Mount_LoginPass: upass = (char *)lptr->ti_Data; break; } lptr++; } // if( path == NULL ) { DEBUG("[ERROR]: Path option not found!\n"); free( dev ); return NULL; } init( s ); // we are trying to open folder/connection struct stat st; if( stat( path, &st ) == 0 && S_ISDIR( st.st_mode ) ) { DEBUG("Mounting localfsys, Its directory FSYS: %s!\n", s->GetPrefix() ); dev->f_Path = StringDup( path ); DEBUG("localfs path is ok '%s'\n", dev->f_Path ); dev->f_FSys = s; dev->f_Type = FType_Directory; dev->f_Size = 0; dev->f_Position = 0; dev->f_User = usr; dev->f_Name = StringDup( name ); DEBUG("data filled\n"); } } // // we will hold here special data SSH2 // dev->f_SpecialData = calloc( sizeof(SpecialData), 1 ); SpecialData *sdat = (SpecialData *) dev->f_SpecialData; if( sdat != NULL ) { sdat->sd_Host = StringDup( host ); sdat->sd_Port = port; sdat->sd_LoginUser = StringDup( ulogin ); sdat->sd_LoginPass = StringDup( upass ); sdat->rc = libssh2_init (0); if( sdat->rc != 0 ) { ERROR ( "libssh2 initialization failed (%d)\n", sdat->rc ); return NULL; } // Ultra basic "connect to port 22 on localhost". Your code is //responsible for creating the socket establishing the connection /// sdat->hostaddr = inet_addr( sdat->sd_Host ); sdat->sock = socket( AF_INET, SOCK_STREAM, 0 ); sdat->sin.sin_family = AF_INET; sdat->sin.sin_port = htons( sdat->sd_Port ); sdat->sin.sin_addr.s_addr = sdat->hostaddr; if ( connect( sdat->sock, (struct sockaddr*)( &(sdat->sin) ), sizeof(struct sockaddr_in)) != 0) { ERROR( "failed to connect!\n"); goto shutdown; } // Create a session instance and start it up. This will trade welcome // banners, exchange keys, and setup crypto, compression, and MAC layers // sdat->session = libssh2_session_init( ); if (libssh2_session_handshake( sdat->session, sdat->sock) ) { ERROR("Failure establishing SSH session\n"); goto shutdown; } // At this point we havn't authenticated. The first thing to do is check // the hostkey's fingerprint against our known hosts Your app may have it // hard coded, may go to a file, may present it to the user, that's your // call // sdat->fingerprint = libssh2_hostkey_hash( sdat->session, LIBSSH2_HOSTKEY_HASH_SHA1 ); DEBUG("Fingerprint: "); int i; for(i = 0; i < 20; i++) { DEBUG( "%02X ", (unsigned char)sdat->fingerprint[i]); } DEBUG("\n"); sdat->rc = libssh2_userauth_password( sdat->session, sdat->sd_LoginUser, sdat->sd_LoginPass ); /* if (!(sdat->channel = libssh2_channel_open_session(session))) { ERROR( "Unable to open a session\n"); goto shutdown; }*/ sdat->sftp_session = libssh2_sftp_init( sdat->session ); if (!sdat->sftp_session) { DEBUG("Unable to init SFTP session\n"); goto shutdown; } /* Since we have not set non-blocking, tell libssh2 we are blocking */ libssh2_session_set_blocking( sdat->session, 1); return dev; } DEBUG("localfs mount ok\n"); shutdown: if( sdat != NULL ) { UnMount( s, dev ); } return NULL; }
void *FileOpen( struct File *s, const char *path, char *mode ) { int spath = strlen( path ); int rspath = strlen( s->f_Path ); File *locfil = NULL; char *comm = calloc( rspath + spath + 5, sizeof( char ) ); DEBUG("FileOpen new: %s %s\n", s->f_Path, path ); // Create a string that has the real file path of the file if( comm != NULL ) { FILE *f = NULL; if( s->f_Path[ rspath-1 ] == '/' ) { sprintf( comm, "%s%s", s->f_Path, path ); } else { sprintf( comm, "%s/%s", s->f_Path, path ); } // Make the directories that do not exist int slashes = 0, i = 0; for( ; i < spath; i++ ) { if( path[i] == '/' ) slashes++; } int off = 0, slash = 0; for( i = 0; i < spath; i++ ) { if( path[i] == '/' ) { char *directory = calloc( rspath + i + 1, sizeof( char *) ); sprintf( directory, "%s%.*s", s->f_Path, i, path ); struct stat filest; // Create if not exist! if( stat( directory, &filest ) == -1 ) mkdir( directory, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ); free( directory ); slash++; } } DEBUG("FileOpen in progress\n"); // Only go on if we can find the file and open it if( ( f = fopen( comm, mode ) ) != NULL ) { // Ready the file structure if( ( locfil = calloc( sizeof( File ), 1 ) ) != NULL ) { locfil->f_Path = StringDup( path ); locfil->f_SpecialData = calloc( sizeof( SpecialData ), 1 ); SpecialData *sd = (SpecialData *)locfil->f_SpecialData; if( sd ) sd->fp = f; /* struct stat myStat; if( lstat( comm, &myStat ) != -1 ) { locfil->f_Buffer = (char *)MakeString( myStat.st_size ); locfil->f_DataPassed = myStat.st_size; } else { locfil->f_Buffer = NULL; //calloc( FILE_MAX_BUFFER, sizeof(char) ); locfil->f_DataPassed = 0; } */ DEBUG("FileOpened, memory allocated for localfs\n"); // Free temp string free( comm ); return locfil; } free( comm ); return NULL; } else { ERROR("Cannot open file: %s mode %s\n", comm, mode ); } free( comm ); } ERROR("Cannot open file %s\n", path ); return NULL; }
void *Mount( struct FHandler *s, struct TagItem *ti ) { File *dev = NULL; char *path = NULL; char *name = NULL; User *usr = NULL; if( s == NULL ) { return NULL; } DEBUG("Mounting local filesystem!\n"); if( ( dev = calloc( 1, sizeof( File ) ) ) != NULL ) { struct TagItem *lptr = ti; // // checking passed arguments while( lptr->ti_Tag != TAG_DONE ) { switch( lptr->ti_Tag ) { case FSys_Mount_Path: path = (char *)lptr->ti_Data; DEBUG("Mount FS path set '%s'\n", path ); break; case FSys_Mount_Host: break; case FSys_Mount_Port: break; case FSys_Mount_Name: name = (char *)lptr->ti_Data; break; case FSys_Mount_User: usr = (User *)lptr->ti_Data; break; } lptr++; } // if( path == NULL ) { ERROR("[ERROR]: Path option not found!\n"); free( dev ); return NULL; } init( s ); // we are trying to open folder/connection dev->f_Path = StringDup( path ); DEBUG("LOCALFS: localfs path is ok '%s'\n", dev->f_Path ); dev->f_FSys = s; dev->f_Position = 0; dev->f_User = usr; dev->f_Name = StringDup( name ); struct stat st; if( stat( path, &st ) == 0 && S_ISDIR( st.st_mode ) ) { DEBUG("Mounting localfsys, Its directory FSYS: %s!\n", s->GetPrefix() ); dev->f_Type = FType_Directory; dev->f_Size = 0; DEBUG("LOCALFS IS DIRECTORY data filled\n"); } else { dev->f_Type = FType_File; } } DEBUG("LOCALFS localfs mount ok\n"); return dev; }