Sponsored Content
Full Discussion: ambiguity in program output
Top Forums Programming ambiguity in program output Post 302160151 by bishweshwar on Monday 21st of January 2008 03:10:07 AM
Old 01-21-2008
ambiguity in program output

1 #include <fcntl.h>
2
3 main(int argc, char *argv[])
4 {
5 char buf[1];
6 int fd, count = 0;
7
8 if (argc > 1)
9 fd = open(argv[1], O_RDONLY);
10 else
11 fd = 0; /* Use standard input */
12
13 while (read(fd, buf, 1) > 0) {
14 if (count < 5) write(1, buf, 1);
15 if (buf[0] == '\n') count++;
16 }
17 }

Here buf can store only one character.
But if i run it as:-

"prog_name /etc/passwd" how does it give first five lines.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Capturing output from C++ program

Hi I have a C++ program that generates a lot of log information on the console, I need this output (printed using printf function) to go to a file since I will use crontab to schedule the job. I know I can do this: myprog > myfile but I don't know how to enter this in crontab. I use... (3 Replies)
Discussion started by: GMMike
3 Replies

2. UNIX for Dummies Questions & Answers

how can I use the stream output in other program

Hello I wander if im doing : ls -l and its giving me lets say 3 results : -rw-r--r-- 1 blah other 1789 May 19 2003 foo.c -rw-r--r-- 1 blah other 1014 May 19 2003 foo.h -rw-r--r-- 1 blah other 270 May 19 2003 foo1.c now I would like to use the first... (1 Reply)
Discussion started by: umen
1 Replies

3. Programming

C program Output

The output I got for this pgm is "4 4 4 4". Can any one help me to understand how I got this output. Also please suggest me some links to learn about argumnets evaluation in C. # include <stdio.h> void func(int a, int b, int c, int d) { printf("%d %d %d %d", a, b, c, d); } int... (3 Replies)
Discussion started by: arunviswanath
3 Replies

4. AIX

Fs space ambiguity

Hi All, I found a strange thing in one of our DB server. one of the file system /orcale/ABC/rman size df output showing 100% full eventhough its occupying 2.18MB $ df -mP /oraclev/ABC/rman Filesystem MB blocks Used Available Capacity Mounted on /dev/lv20906 1024.00 1024.00 0.00 100%... (2 Replies)
Discussion started by: ram1729
2 Replies

5. UNIX for Dummies Questions & Answers

Program output overflows

I Wrote code that forks into two processes, a parent process, and a child process. The parent process will take the arguments to main() and send them one character at a time to the child process through a pipe (one call to write for each character). The child process will count the characters... (1 Reply)
Discussion started by: hansel13
1 Replies

6. UNIX for Advanced & Expert Users

how do collect shell output in a C program

i use the system command to execute a shell command... ca i collect the out put in the form of a string or something using the same C program? (5 Replies)
Discussion started by: damn_bkb
5 Replies

7. Shell Programming and Scripting

Ambiguity in unicode, Perl CGI

Hello, I was written a cgi with a textarea to save some words from web. I grab and write words like this: $cgiparams{'CONTENTS'} =~ s/\r//g; #$cgiparams{'CONTENTS'} =~ s/á/&aacute;/g; open(TM, ">$editedfilename"); #binmode(TM,... (1 Reply)
Discussion started by: Zaxon
1 Replies

8. Programming

Please help me with my output for my program

Hello All, iam a new memeber today i joined this forum. hope i will get help. the below program takes input strings and give reverse of input string. && mv /home/test1/programs/display /home/test1/programs/old echo " Please enter the test " read a echo "$a" > file wc -c file > file1 perl... (1 Reply)
Discussion started by: ameyrk
1 Replies

9. Programming

Ambiguity in operator []

Hi All, When i try to compile the following code for 64-bit it works, whereas for 32-bit version, Compiler barfs: #include <iostream> #include <string> class String { public: String() { } String(const char* src) : myStr(src) { } String(const std::string& src) :... (0 Replies)
Discussion started by: 14341
0 Replies

10. Shell Programming and Scripting

getting no output with my perl program

hi, i have posted the same kind of the question in some other forum of the same site. but realized that it is supposed to be here so i am reposting it .this is the perl script written to check for particular pattern. my file 1 would look like this hwk:678:9878:asd:09: abc cfgb 12 nmjk ......... (3 Replies)
Discussion started by: anurupa777
3 Replies
SHQUOTE(3)						   BSD Library Functions Manual 						SHQUOTE(3)

NAME
shquote, shquotev -- quote argument strings for use with the shell LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdlib.h> size_t shquote(const char *arg, char *buf, size_t bufsize); size_t shquotev(int argc, char * const *argv, char *buf, size_t bufsize); DESCRIPTION
The shquote() and shquotev() functions copy strings and transform the copies by adding shell escape and quoting characters. They are used to encapsulate arguments to be included in command strings passed to the system() and popen() functions, so that the arguments will have the correct values after being evaluated by the shell. The exact method of quoting and escaping may vary, and is intended to match the conventions of the shell used by system() and popen(). It may not match the conventions used by other shells. In this implementation, the following transformation is applied to each input string: o it is surrounded by single quotes ('), o any single quotes in the input are escaped by replacing them with the four-character sequence: ''', and o extraneous pairs of single quotes (caused by multiple adjacent single quotes in the input string, or by single quotes at the begin- ning or end of the input string) are elided. The shquote() function transforms the string specified by its arg argument, and places the result into the memory pointed to by buf. The shquotev() function transforms each of the argc strings specified by the array argv independently. The transformed strings are placed in the memory pointed to by buf, separated by spaces. It does not modify the pointer array specified by argv or the strings pointed to by the pointers in the array. Both functions write up to bufsize - 1 characters of output into the buffer pointed to by buf, then add a NUL character to terminate the out- put string. If bufsize is given as zero, the buf parameter is ignored and no output is written. RETURN VALUES
The shquote() and shquotev() functions return the number of characters necessary to hold the result from operating on their input strings, not including the terminating NUL. That is, they return the length of the string that would have been written to the output buffer, if it were large enough. If an error occurs during processing, the value ((size_t)-1) is returned and errno is set appropriately. EXAMPLES
The following code fragment demonstrates how you might use shquotev() to construct a command string to be used with system(). The command uses an environment variable (which will be expanded by the shell) to determine the actual program to run. Note that the environment vari- able may be expanded by the shell into multiple words. The first word of the expansion will be used by the shell as the name of the program to run, and the rest will be passed as arguments to the program. char **argv, c, *cmd; size_t cmdlen, len, qlen; int argc; ... /* * Size buffer to hold the command string, and allocate it. * Buffer of length one given to snprintf() for portability. */ cmdlen = snprintf(&c, 1, "${PROG-%s} ", PROG_DEFAULT); qlen = shquotev(argc, argv, NULL, 0); if (qlen == (size_t)-1) { ... } cmdlen += qlen + 1; cmd = malloc(cmdlen); if (cmd == NULL) { ... } /* Create the command string. */ len = snprintf(cmd, cmdlen, "${PROG-%s} ", PROG_DEFAULT); qlen = shquotev(argc, argv, cmd + len, cmdlen - len); if (qlen == (size_t)-1) { /* Should not ever happen. */ ... } len += qlen; /* "cmd" can now be passed to system(). */ The following example shows how you would implement the same functionality using the shquote() function directly. char **argv, c, *cmd; size_t cmdlen, len, qlen; int argc, i; ... /* * Size buffer to hold the command string, and allocate it. * Buffer of length one given to snprintf() for portability. */ cmdlen = snprintf(&c, 1, "${PROG-%s} ", PROG_DEFAULT); for (i = 0; i < argc; i++) { qlen = shquote(argv[i], NULL, 0); if (qlen == (size_t)-1) { ... } cmdlen += qlen + 1; } cmd = malloc(cmdlen); if (cmd == NULL) { ... } /* Start the command string with the env var reference. */ len = snprintf(cmd, cmdlen, "${PROG-%s} ", PROG_DEFAULT); /* Quote all of the arguments when copying them. */ for (i = 0; i < argc; i++) { qlen = shquote(argv[i], cmd + len, cmdlen - len); if (qlen == (size_t)-1) { /* Should not ever happen. */ ... } len += qlen; cmd[len++] = ' '; } cmd[--len] = ''; /* "cmd" can now be passed to system(). */ SEE ALSO
sh(1), popen(3), system(3) BUGS
This implementation does not currently handle strings containing multibyte characters properly. To address this issue, /bin/sh (the shell used by system() and popen()) must first be fixed to handle multibyte characters. When that has been done, these functions can have multi- byte character support enabled. BSD
September 7, 2008 BSD
All times are GMT -4. The time now is 06:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy