I've made a C program that implements printenv | grep [arguments] | sort | less, but for some reason I'm getting a bunch of newlines at the start of my output. Probably it's because of how I'm writing the environment variables from the parent process to the grep process. My results if I run myProgram PATH:
/*40 blank lines */
INFOPATH=/usr/share/info
MANPATH=/usr/local/vol/matlab/7.4.0/man:/usr/kerberos/man:/usr/local/share/man:/usr /share/man/en:/usr/share/man:/usr/man
MODULEPATH=/usr/local/vol/modulefiles:/pkg/modules/modulefiles:/etc/site/modulefiles: /usr/share/Modules/modulefiles:/etc/modulefiles
PATH=/usr/lib/heimdal/bin:/usr/local/vol/comsol/3.4/bin:/usr/local/vol/maple/10.05/bin:/usr/local/vol/matlab/7.4.0/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/opt/real/RealPlayer
WMMENUPATH=/usr/local/vol/maple/menudef/10.05:/usr/local/vol/fvwm2/menudefs/emacs:/usr/local/vol/fvwm2/menudefs/acroread
I'm using C to call and connect shell programs with pipes. Every program (grep, sort, less/more) is called in it's own child process and piped to the others. The parent process writes the environment variables to grep through a pipe like this:
for(i=0; envp[i]!=NULL; i++)
{
return_value = write(pipe_fd[0][PIPE_WRITE],envp[i], strlen(envp[i]));
errorCheck(return_value, "Cannot write to pipe in parent\n");
return_value=write(pipe_fd[0][PIPE_WRITE],"\n",1);
errorCheck(return_value, "Cannot write to pipe in parent\n");
}
I've tried adding a bunch of options to the sort call, but nothing has worked so far. My question is if there's a way to ignore multiple newlines in a row with either grep or sort?