Sponsored Content
Full Discussion: K&R C code edits
Homework and Emergencies Homework & Coursework Questions K&R C code edits Post 302562390 by Corona688 on Thursday 6th of October 2011 06:21:23 PM
Old 10-06-2011
It's exactly what I said suggested: Flattening all whitespace so it's only one big 'line' and statements only appear in one fashion. It's the difference between

Code:
int main(        int argc,
                            char *argv[]        )
{
        return(0);
}

and
Code:
int main(int argc, char *argv[]) { return(0); }

The C compiler doesn't care as long as lines beginning with # aren't mangled.

If he thinks that's possible, you're on a system which doesn't barf on lines bigger than 2000 characters.

---------- Post updated at 04:21 PM ---------- Previous update was at 03:47 PM ----------

A start might be:

Code:
$ cat flatc.awk
BEGIN {
        ORS=" " # Records separated by space instead of newline
}

{
        if($0 ~ /^#/)
                printf("\n%s\n", $LINE); # preprocessors statements
        else
        {
                $1=$1;  # awk prints the unmodified line unless you
                        # modify $1..$N somewhere, so $1=$1 flattens

                print;
        }
}

END {
        printf("\n"); # Even one big line must end in \n
}

$ cat popen.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
        int status;
        FILE *fp=popen("gunzip", "r");
        char buf[512];

        while(fgets(buf, 512, fp))
        {
                printf("%s", buf);
        }

        if(WEXITSTATUS(status=pclose(fp)))
        {
                printf("gunzip failed with status %d, do rollback\n",
                        WEXITSTATUS(status));
        }
}

$ awk -f flatc.awk < popen.c

#include <stdio.h>

#include <sys/types.h>

#include <sys/wait.h>
 int main(int argc, char *argv[]) { int status; FILE *fp=popen("gunzip", "r"); char buf[512];  while(fgets(buf, 512, fp)) { printf("%s", buf); }  if(WEXITSTATUS(status=pclose(fp))) { printf("gunzip failed with status %d, do rollback\n", WEXITSTATUS(status)); } }

$ awk -f flatc.awk < popen.c |
        # /^#/p means, if a line begins with #, print it.
        # /^#/d means, if a line begins with #, delete it and move to the next line.
        # All the rest reach s/int/flub/g.
        # This way, we can avoid modifying preprocessor statements.
        sed '/^#/p;/^#/d;s/int/flub/g'

#include <stdio.h>

#include <sys/types.h>

#include <sys/wait.h>
 flub main(flub argc, char *argv[]) { flub status; FILE *fp=popen("gunzip", "r"); char buf[512];  while(fgets(buf, 512, fp)) { prflubf("%s", buf); }  if(WEXITSTATUS(status=pclose(fp))) { prflubf("gunzip failed with status %d, do rollback\n", WEXITSTATUS(status)); } }

$

Having blocks as all one big line means you can do complicated search-replaces on entire valid blocks of code. otherwise, sed couldn't hunt for patterns bigger than one line.
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with scripting mass file edits..

Hello, I am wanting to know a way to shell (ksh)script-edit a file by having a script that searches for a specific string, and then input lines of text in the file after that specific string. Please help, as I will be up all night if I can't figure this out. (16 Replies)
Discussion started by: LinuxRacr
16 Replies

2. Shell Programming and Scripting

Multiple edits to a bunch of html files

I'm trying to upgrade a whole bunch of pages on my site to a new design. I thought one way of doing it would be to enclose the content in special comment tags and then use some form of script to wrap the new html around it. Like this: <!-- content start --> <h1>Blah blah blah</h1> yada yada... (9 Replies)
Discussion started by: dheian
9 Replies

3. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

4. UNIX for Dummies Questions & Answers

Compile & Run Java Code

The java program is a part of speech tagger -> The Stanford NLP (Natural Language Processing) Group The goal is to use this script as part of a webpage to tag parts of speech based on a user-inputted string. I have no idea what to do with the files - I'm a complete *nix noob. I tried running... (4 Replies)
Discussion started by: tguillea
4 Replies

5. UNIX for Dummies Questions & Answers

OpenLDAP DB_CONFIG edits, changes live? or do I need run something

So I am probably missing something , but when I made edits to my DB_CONFIG file to fix form db_lock issues, the changes are not propagating after a service restart. Anyone know if I need to run anything else, or are the changes live? (0 Replies)
Discussion started by: jcejka
0 Replies

6. Shell Programming and Scripting

Problem with call of Java Programm & return code handling & output to several streams.

Hello Everybody, thanks in advance for spending some time in my problem. My problem is this: I want to call a java-Programm out of my shell skript, check if die return code is right, and split the output to the normal output and into a file. The following code doesn't work right, because in... (2 Replies)
Discussion started by: danifunny
2 Replies

7. UNIX for Dummies Questions & Answers

multiple text edits inone pass

File_1 looks like: bunch of text Untitled Placemark bunch of text bunch of text Untitled Placemark bunch of text bunch of text Untitled Placemark bunch of text File_2 looks like: Title_001 Title_002 Title_003 First: I need to replace the 1st occurence of "Untitled Placemark"... (2 Replies)
Discussion started by: kenneth.mcbride
2 Replies

8. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies
POPEN(3)						     Linux Programmer's Manual							  POPEN(3)

NAME
popen, pclose - process I/O SYNOPSIS
#include <stdio.h> FILE *popen(const char *command, const char *type); int pclose(FILE *stream); DESCRIPTION
The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only. The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. The mode argument is a pointer to a null-terminated string which must be either `r' for reading or `w' for writing. The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(). Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the process that called popen(), unless this is altered by the command itself. Conversely, reading from a ``popened'' stream reads the com- mand's standard output, and the command's standard input is the same as that of the process that called popen. Note that output popen streams are fully buffered by default. The pclose function waits for the associated process to terminate and returns the exit status of the command as returned by wait4. RETURN VALUE
The popen function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory. The pclose function returns -1 if wait4 returns an error, or some other error is detected. ERRORS
The popen function does not set errno if memory allocation fails. If the underlying fork() or pipe() fails, errno is set appropriately. If the mode argument is invalid, and this condition is detected, errno is set to EINVAL. If pclose() cannot obtain the child status, errno is set to ECHILD. CONFORMING TO
POSIX.2 BUGS
Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can be avoided by calling fflush(3) before popen. Failure to execute the shell is indistinguishable from the shell's failure to execute command, or an immediate exit of the command. The only hint is an exit status of 127. HISTORY
A popen() and a pclose() function appeared in Version 7 AT&T UNIX. SEE ALSO
fork(2), sh(1), pipe(2), wait4(2), fflush(3), fclose(3), fopen(3), stdio(3), system(3) BSD MANPAGE
1998-05-07 POPEN(3)
All times are GMT -4. The time now is 01:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy