Stranica: 1/2.
[C] problem
Postano: 12 sij 2013, 20:00
Postao/la Bug
Pozdrav, pomagajte ko boga vas molim
Treba jednu malu funkciju (programcic) da ispisem u C-u tj da mi pomogne, moje znanje C-a je slabo pa mi ne ide
Program A(c app) u sebi sadrzi IP adresu i PATH, program A poziva program B(python app) na PATH lokaciji i salje mu IP. Program B vraca 1 i 0 informaciju za to.
Program B je napisan (u pythonu) meni samo treba da C app hvata output od B(pythona)
Kod: Označi sve
#include <stdio.h>
#include <string.h>
int check_ip(char* ip, char* path) {
char command[4096];
char out[4096];
int res;
int i = 0;
sprintf(out, "%s --%s\n", path, ip);
strcat(command, out);
FILE *fp = popen(command, "w");
res = 1; /* fprint() */
pclose(fp);
if (res == 1) {
return 1;
} else {
return 0;
}
}
int main()
{
char* ip;
char* path;
int res;
ip = "192.168.1.1";
path = "/home/user/file.py";
res = check_ip(ip, path);
printf("%s", res);
}
Program navravno ne radi jer ne znam koji data type za IP i za PATH i celu komandu, ako neko moze da mi pomogne ili da ispravi ovu app bio bih mu zahvalan

Re: C problem
Postano: 12 sij 2013, 20:48
Postao/la Medojedni jazavac
Za ip i path koristi konst. stringove, odnosno pokazivac na niz charova
e.g.
Kod: Označi sve
char* path = "path/to/my/documenté"
Naravno ako vec znas kako izgledaju ip i path.
Sad ovo mi nije sto posto jasno, ti zelis da tvoj program hvata stdout od python modula?
Re: C problem
Postano: 12 sij 2013, 21:16
Postao/la Bug
Da za char si upravu, a sad sam video da sam i los kod pastova (ovih dana ne znam sta radim) ispravio sam sada izgleda kako bi trebalo...
Da trebao bih da hvatam stdout od python aplikacij
Re: C problem
Postano: 12 sij 2013, 22:34
Postao/la jasox
Bug je napisao/la:Da za char si upravu, a sad sam video da sam i los kod pastova (ovih dana ne znam sta radim) ispravio sam sada izgleda kako bi trebalo...
Da trebao bih da hvatam stdout od python aplikacij
Nadam se da ce ti ovo pomoci. Uradio sam tako da u command line-u kucas ip i path, al ako ti se to ne svidja ne bi trebalo biti tesko promijeniti.
Kod: Označi sve
/**
* @file send-ip.c
* @author jaaso
* @date 013-01-12
* Time-stamp: <2013-01-12 22:29:50 jaaso>
*
* Copyright (c) 2013 jaaso
* jaaso
*
* @version 0.1 $Rev$
* $Id$
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
struct in_addr *atoaddr(char *address);
#define PATH_MAX 4999
int
main(int argc, char *argv[])
{
int i, status;
char *path;
struct in_addr *ip;
char out[PATH_MAX], command[PATH_MAX];
if (argc != 3) {
fprintf(stderr,"./send-ip ip path");
exit(1);
}
ip = atoaddr(argv[1]);
path = argv[2];
sprintf(out, "%s %s\n", path, argv[1]);
strcpy(command, out);
FILE *fp = popen(command, "w");
while (fgets(path, PATH_MAX, fp) != NULL)
printf("%s", path);
status = pclose(fp);
if (fp == NULL) {
printf("Can not find that command!");
return 1;
}
if (status == -1) {
/* Error reported by pclose() */
printf("FIX THIS");
} else {
/* Use macros described under wait() to inspect `status' in order
to determine success/failure of command executed by popen() */
printf("FIX THIS");
}
return 0;
}
/* Converts ascii text to in_addr struct. NULL is returned if the
address can not be found. */
struct in_addr *atoaddr(char *address)
{
struct hostent *host;
static struct in_addr saddr;
/* First try it as aaa.bbb.ccc.ddd. */
saddr.s_addr = inet_addr(address);
if (saddr.s_addr != -1) {
return &saddr;
}
host = gethostbyname(address);
if (host != NULL) {
return (struct in_addr *) *host->h_addr_list;
}
return NULL;
}
Re: C problem
Postano: 12 sij 2013, 22:40
Postao/la Bug
Pa od pomoci je

ali aj nesmem da u C-u koristim puno headera i da gledam koji je ip i sl..duga prica
Uglavnom resio sam
Kod: Označi sve
#include <stdio.h>
#include <string.h>
int check_ip(char* ip, char* path) {
char command[4096];
char out[4096];
char str[10];
char* res;
int rs;
sprintf(out, "%s --%s\n", path, ip);
strcat(command, out);
FILE *fp = popen(command, "r");
while(fgets(str,sizeof(str),fp) != NULL)
{
res = str;
}
pclose(fp);
rs = atoi(res);
if(rs == 1){
return 1;
}else{
return 0;
}
}
int main()
{
char* ip;
char* path;
int res;
ip = "192.168.1.1";
path = "/media/Working/Downloads/test/file.py";
res = check_ip(ip, path);
printf("%i\n", res);
}
ako ima neko TIP kako ovo bolje da izgleda , slobodno

Re: C problem
Postano: 12 sij 2013, 22:45
Postao/la jurastublic
Evo možda ti ovaj jednostavniji način pomogne:
Kod: Označi sve
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(){
char ip[]="255.255.255.255";
char path[]="python /home/tralala/pithonapp.py"; // staviš u python da prima argument na komandnoj liniji
char s[]=" "; // i razmak
int odgovor; //ako python vraća int - to ti znaš ...
strcat(s,ip);
strcat(path,s);
odgovor= system(path);
if (WEXITSTATUS(odgovor)== 0) printf("evo vraća 0 \n");
else if (WEXITSTATUS(odgovor)== 1) printf("vraća 1 \n");
else printf("greska \n");
return 0;
}
Ovi printf-ovi su samo za debugiranje, da vidiš šta se zbiva. Zamijeni kako ti paše.
Re: C problem
Postano: 12 sij 2013, 22:47
Postao/la jasox
Bug je napisao/la:Pa od pomoci je

ali aj nesmem da u C-u koristim puno headera i da gledam koji je ip i sl..duga prica
Uglavnom resio sam
Kod: Označi sve
#include <stdio.h>
#include <string.h>
int check_ip(char* ip, char* path) {
char command[4096];
char out[4096];
char str[10];
char* res;
int rs;
sprintf(out, "%s --%s\n", path, ip);
strcat(command, out);
FILE *fp = popen(command, "r");
while(fgets(str,sizeof(str),fp) != NULL)
{
res = str;
}
pclose(fp);
rs = atoi(res);
if(rs == 1){
return 1;
}else{
return 0;
}
}
int main()
{
char* ip;
char* path;
int res;
ip = "192.168.1.1";
path = "/media/Working/Downloads/test/file.py";
res = check_ip(ip, path);
printf("%i\n", res);
}
ako ima neko TIP kako ovo bolje da izgleda , slobodno

Mislim da imam visak headera. Moram provjerit. ^^
Re: C problem
Postano: 13 sij 2013, 13:59
Postao/la jurastublic
ako ima neko TIP kako ovo bolje da izgleda , slobodno
Onaj command i strcat(command, out) je suvišan.
Može samo *fp = popen(out, "r").
Re: C problem
Postano: 13 sij 2013, 19:15
Postao/la Bug
Dobijam ova upozrenja, koja mi nisu bas jasna
Warning: passing argument 1 of 'check_ip' discards 'const' qualifier from pointer target type [enabled by default]
note: expected 'char *' but argument is of type 'const char *'
warning: passing argument 2 of 'check_ip' from incompatible pointer type [enabled by default]
note: expected 'char *' but argument is of type 'struct buffer *'
Greske se odnose na sledecu funkciju
Kod: Označi sve
int check_ip(char* ip, char* path) {
char command[4096];
char out[4096];
char str[10];
char* res;
int rs;
sprintf(out, "%s --ip=%s\n", path, ip);
strcat(command, out);
FILE *fp = popen(command, "r");
while (fgets(str, sizeof (str), fp) != NULL) {
res = str;
}
pclose(fp);
rs = atoi(res);
if (rs == 1) {
return 1;
} else {
return 0;
}
}
I ova upozorenja
warning: passing argument 1 of 'fopen' from incompatible pointer type [enabled by default]
expected 'const char * restrict' but argument is of type 'struct buffer *'
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
Za ovaj kod
Kod: Označi sve
FILE *file;
if (file = fopen(s->script_path, "r")) {
fclose(file);
} else {
log_error_write(srv, __FILE__, __LINE__, "s",
"Helper Script not found. Check lighttpd config.");
return HANDLER_ERROR;
}
Ja sam zbunjen, moze pomoc?

Re: C problem
Postano: 13 sij 2013, 19:57
Postao/la jurastublic
Onaj ip u main() ti je const char pointer (string u navodnicima), a u funkciji samo char pointer. Umjesto char *ip="to i to" u main() stavi char ip[]="to i to" (ili const char *ip). U funkciju stavi int check_ip(char ip[], char path[]) ili const char *ip.
Ovo drugo, ne mogu reći pošto nisi pokazao dio gdje je definiran script_path ali vjerojatno ista stvar. Jedno si deklarirao u funkciji a drugo šalješ.