Re: Little C Help Please

From: Peter (shaggy_at_australis.net.STOP.SPAM)
Date: 02/05/04


Date: Thu, 05 Feb 2004 08:18:37 GMT

Groovy hepcat Ravi was jivin' on Sun, 01 Feb 2004 10:32:46 +0530 in
comp.os.linux.development.apps.
Re: Little C Help Please's a cool scene! Dig it!

>On Sat, 31 Jan 2004 20:57:44 +0000, Materialised
><materialised@privacy.net> wrote:
>
>>#include <stdio.h>
>>#include <stdlib.h>
>>#define MAXLEN 33
>>#define EXTRA 5
>>/* 4 for field name "data", 1 for "=" */
>>#define MAXINPUT MAXLEN+EXTRA+2
>>/* 1 for added line break, 1 for trailing NUL */
>>#define DATAFILE "lines.txt"
>>
>>void unencode(char *src, char *last, char *dest)
>>{
>> for(; src != last; src++, dest++)
>> if(*src == '+')
>> *dest = ' ';
>> else if(*src == '%') {
>> int code;
>> if(sscanf(src+1, "%2x", &code) != 1) code = '?';
>> *dest = code;
>> src +=2; }
>> else
>> *dest = *src;
>> *dest = '\n';
>> *++dest = '\0';
>>}
>>
>>int main(void)
>>{
>> char *lenstr;
>> char command[100];
>> char input[MAXINPUT], data[MAXINPUT];
>> long len;
>> int i;
>> printf("%s%c%c\n",
>> "Content-Type:text/html;charset=iso-8859-1",13,10);
>> printf("<TITLE>Yahoo Shared Ignore Project</TITLE>\n");
>> lenstr = getenv("CONTENT_LENGTH");
>> if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > MAXLEN)
>> printf("<P>Error in invocation - Your Propably entered no data.");
>> else {
>> fgets(input, len+1, stdin);
>> unencode(input+EXTRA, input+len, data);
>> sprintf(command, "sed '/%s/d' lines.txt > output.txt", data);
>> printf("<br>Data:<br>%s<p>", data);
>> printf("Command: %s", command);
>> system(command);
>>
>> }
>> return 0;
>>}
>>
>>This program is called by a html form on my webserver, and is used to
>>remove a item from a text file.
>>The problem is the vairiable data, has a trailing space so the call to
>>sed fails.
>>
>>Does anyone know a way I can remove this training space?
>>Thanks
>
>Are you looking for something like this?

  I doubt that anyone is looking for something like that, except virus
writers.
  I'm assuming, just for the hell of it, that you meant to include the
necessary headers. Otherwise you have a case of undefined behaviour on
your hands (by calling a variadic function without a prototype, for
example).

#include <stdio.h>
#include <string.h>

>int main()
>{
> char text[80];

  You'll need this too (See below.):

  char *p;

> printf("Enter text with trailing spaces:\n");
> gets(text);

  Never use gets(). It is a very dangerous function. There's no safe
way to use it because there's no way to tell gets() how many
characters to read, so it hapily goes on reading even when the buffer
into which it is storing the data is exhausted. Malicious bastards who
write viruses could use this to plant a virus in the system. Or it
could crash the system. Or anything else could happen, good, bad or
ugly. Instead, use fgets(), as it takes an argument intended to
represent the size of the buffer, and stops reading before that number
of characters have been read.

  fgets(text, sizeof text, stdin);

  Of course, fgets() retains the newline read in from the stream, so
you need to remove it. The usual idiom is to use a temporary pointer
variable and the strchr() function (You'll need to include string.h
for that, of course), like so:

  p = strchr(text, '\n');
  if(p)
    *p = '\0';

> remspc(text);

  You have not declared this function. New compilers that support the
C99 standard will reject this function call. Even with an older (C90)
compiler it's a bad idea to call a function without providing a
prototype first.

> printf("Without space: %s", text);
> return 0;
>}
>
>remspc(char* text)
>{
> int i;
> for (i = strlen(text) - 1; i > 0; i--)
> if (isspace(text[i]))
> text[i] = '\0';
> else
> return 0;

  strrchr() could probably be used in a loop more easily, or perhaps a
single strchr() call (if there can be no spaces in the file name).

> return 0;
>}

  Come to think of it, I think all this is moot anyhow, since the
server will escape any space characters in the URL (part of which
becomes the input to this program), so simply reading from stdin
cannot get a string with a space in it. You'd need to unescape the
escaped characters when reading the input.
  I think the OP's problem was not a space in the input, but a newline
read in by fgets(). I have shown how to deal with that above. Starting
from the end and going backward using isspace() as you have done in
your remspc() function would also work, but is a little clunky for the
removal of a single newline.

-- 
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?

Quantcast