Run busybox netcat in background script? - background

Note, I have looked at Using netcat/cat in a background shell script (How to avoid Stopped (tty input)? ), but it doesn't seem to apply to my case. I'm using the netcat (nc) that comes with busybox, and none of the workarounds I've found seem to work for me. Also, there is no -d option, nor are there any -q options.
I'm trying to use netcat to receive a file in a shell script, and this script is in a background process that apparently doesn't have a stdin. I have tried several different approaches, but none seem to work. Here's what I've tried:
nc -l -p 8888 > file returns the instant the remote sender connects, dropping the connection early.
nc -l -p 8888 < /dev/null > file does the same.
echo -n | nc -l -p 8888 > file does the same.
tail -f /dev/null | nc -l -p 8888 > file will receive the file, but it doesn't quit when the file transfer is finished.
I'm running out of ideas. Is this version of netcat fundamentally broken?

Well, it's been days, and nobody had an answer, so I had to roll my own. Below is the code, in case it's useful to anyone.
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <unistd.h>
void bail(const char *s)
{
perror(s);
exit(-1);
}
int listen_socket;
struct sockaddr_in my_addr;
void setup_socket(int port)
{
int er;
listen_socket = socket(AF_INET, SOCK_STREAM, 0);
if (listen_socket < 0) bail("socket");
int on=1;
er = setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (er < 0) bail("setsockopt");
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
er = bind(listen_socket, (struct sockaddr *)&my_addr,
(socklen_t)sizeof(struct sockaddr_in));
if (er < 0) bail("bind");
er = listen(listen_socket, 1);
if (er < 0) bail("listen");
}
int listen_wait()
{
socklen_t len = sizeof(struct sockaddr_in);
int s = accept(listen_socket, (struct sockaddr *)&my_addr, &len);
if (s < 0) bail("accept");
return s;
}
char buf[1024];
int main(int argc, char *argv[])
{
int port = 8888;
int sock;
int tryagain;
if (argc>1) {
port = atoi(argv[1]);
}
setup_socket(port);
sock = listen_wait();
do {
int i = read(sock, buf, 1024);
tryagain = (errno==EAGAIN);
if ((i<1) && (!tryagain)) {
shutdown(sock, 2);
close(sock);
return 0;
}
if (i>0) {
fwrite(buf, 1, i, stdout);
}
} while (1);
return 0;
}

This will do the trick
sleep 99999 | nc -l -p 8888 > file

Related

eclipse error in c/c++ project in run time.output is printed in first will be blank. we enterd

#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a1=20,b1=40,temp;
temp=a1;
a1=b1;
b1=temp;
printf("a1: %d, b1: %d",a1,b1);
return EXIT_SUCCESS;
OUTPUT
2
4
5
enter three numbersresult is 3.666667
#include <stdio.h>
#include <stdlib.h>
int main(void) {
setbuf(stdout,NULL);
float num1,num2,num3,average;
printf("enter three numbers");
scanf("%f%f%f",&num1,&num2,&num3);
average=(num1+num2+num3)/3;
return EXIT_SUCCESS;
}

Simple Regex pattern unmatched with Flex/Bison (Lex/Yacc)

I have built a trivial compiler using Flex and Bison which is supposed to recognize a simple string in a source file and I use the standard error stream to output a message if the string is recognized correctly.
Below is my code and my unexpected result.
This is the source file (testsource.txt) with the string I try to recognize:
\end{document}
This is the Flex file (UnicTextLang.l):
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"
void yyerror(char *);
int yylex(void);
/* "Connect" with the output file */
extern FILE *yyout;
extern int yyparse();
%}
%%
^\\end\{document\}$ { yyerror("end matched"); return END; }
/* skip whitespace */
[ \t] ;
/* anything else is an error */
. yyerror("invalid character");
%%
int main(int argc, char *argv[]) {
if ( argc < 3 )
yyerror("You need 2 args: inputFileName outputFileName");
else {
yyin = fopen(argv[1], "r");
yyout = fopen(argv[2], "w");
yyparse();
fclose(yyin);
fclose(yyout);
}
return 0;
}
This is the Bison file (UnicTextLang.y):
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"
void yyerror(char *);
int yylex(void);
/* "Connect" with the output file */
extern FILE *yyout;
%}
%token END
%%
document:
END
|
;
%%
int yywrap(void) {
return 1;
}
void yyerror(char *s) {
fprintf(stderr, "%s\n", s); /* Prints to the standard error stream */
}
I run the following commands:
flex UnicTextLang.l
bison -dl -o y.tab.c UnicTextLang.y
gcc lex.yy.c y.tab.c -o UnicTextLang
UnicTextLang.exe testsource.txt output.txt
What I expect to see printed in the console is
end matched
But this is what I get:
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
invalid character
What’s wrong?
This issue is caused by the end-of-line code for a Windows machine being two characters (\r\n) when on other systems it is one (\n).
This is explained in the flex manual:
‘r$’
an ‘r’, but only at the end of a line (i.e., just before a newline). Equivalent to ‘r/\n’.
Note that flex’s notion of “newline” is exactly whatever the C compiler used to compile flex interprets ‘\n’ as; in particular, on some DOS systems you must either filter out ‘\r’s in the input yourself, or explicitly use ‘r/\r\n’ for ‘r$’.
The quick solution is to change:
^\\end\{document\}$
to
^\\end\{document\}\r\n
However, if your expression is at the end-of-file without an end-of-line, which is possible in Windows, then you would have to specifically match that case also. Flex does permit the matching of end-of-file with:
<<EOF>>
but this will cause all kinds of other side effects and it is often easier not to anchor the pattern to the end (of line or file).

Delete character "\n" from string with read

I've to do a project but I hit a issue.
I receive a string from a read but when I would see what data are in my buffer, it shows an "\n" at the end of the file. However I don't need it to use after to deal with a argument in my function.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
int main()
{
char buf[100];
read(1, buf, sizeof(buf));
printf("%s", &buf);
// If I write: "/tmp/", printf shows: "/tmp/\n"
DIR* drp = opendir(buf);
// Logically: no such file or directory
}
Thanks
The problem in how you read string from stdin. read function waits until you enter a newline or other EOF key. But it includes this last symbol in the result.
1) I think you better use scanf.
scanf("%s", buf);
2) Or you need to take care of the last symbol by yourself.
char buf[100];
char res[100];
int n = read(1, buf, sizeof(buf));
if(n > 0) {
memcpy(res, buf, n - 1);
} else {
printf("Error while reading\n");
}

why calles function yyerror() in sample programm

curs.l :
%{
#include <stdlib.h>
#include "tree.c"
#include "yycurs.h"
%}
L [a-zA-Z_]
D [0-9]
D4 [0-3]
IDENTIFIER ({L})({L}|{D})*
INT4 {D4}+'q'
INT {D}+
%%
{IDENTIFIER} {return VARIABLE;}
%%
int yywrap(void){
return 0;
}
curs.y:
%{
#include stdio.h
void yyerror(char*);
int yylex(void);
%}
%token VARIABLE INTEGER
%%
var: VARIABLE {printf($1);};
%%
void yyerror(char *s){
fprintf(stderr, "11\n");
fprintf(stderr, "%s\n", s);
}
int main(void){
yyparse();
return 0;
}
when i run my compiled progrum, i have such result:
./curs
ff //I introduced
//result
ff //I introduced
11 //result
syntax error //result
evgeniy#evgeniy-desktop:~/documents/compilers$
Can anybody explain me, why there appears 'syntax error'?
Thanks in advance.
Your grammar defiles that a valid file consists of exactly one VARIABLE. To have more then one, you need to introduce a recursive rule.
%start vars
%%
var: VARIABLE {printf($1);};
vars: var
| vars var;
%%

Getting CPU info from Process ID

If anyone could please help me out, that would be great :)
This seems to be a tough one. Starting from the process ID, I need to be able to grab:
How much CPU the process is taking up in %
How long the process has been using the CPU
This needs to be written in Cocoa/ Objective-C or C. It also needs to work on Tiger through Snow Leopard.
Thanks!
A crude way would be to spawn of a popen command and grab some output from ps.
Ie like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void get_process_info(int pid) {
char ps_cmd[256];
sprintf(ps_cmd, "ps -O %%cpu -p %d", pid); // see man page for ps
FILE *fp = popen(ps_cmd, "r");
if (fp) {
char line[4096];
while (line == fgets(line, 4096, fp)) {
if (atoi(line) == pid) {
char dummy[256];
char cpu[256];
char time[256];
// PID %CPU TT STAT TIME COMMAND
// 32324 0,0 s001 S+ 0:00.00 bc
sscanf(line, "%s %s %s %s %s", dummy, cpu, dummy, dummy, time);
printf("%s %s\n", cpu, time); // you will need to parse these strings
pclose(fp);
return;
}
}
pclose(fp);
}
}
int main() {
get_process_info(32324);
return 0;
}