Pete Ryland wrote:
> > I have a slight problem.
> > I am checking if a structure variable is empty to display a warning window
> > (with
> > motif) and if the variable holds a value (file name), I want to go ahead and
> > delete the file using a system call to "rm -f"
> >
> > My code:
> > --------
> > if (strcmp(castedPtr->tname, "") == 0)
> > /* check if string is empty and display warning message */
> > missingInputCB (w, clientData, callData);
> > else /* delete file name that matches the value of castedPtr->tname */
> > system("rm -f castedPtr->tname");
> >
> > However, system("rm -f castedPtr->tname")
> > is not working because the rm command will be searching for a file called
> > castedPtr->tname to delete it while in fact this pointer holds the file name.
>
> Um. Maybe you've been using perl too much lately.
> Try the following.
>
> #include <strings.h>
>
> if (strcmp(castedPtr->tname, "") == 0)
> /* check if string is empty and display warning message */
> missingInputCB (w, clientData, callData);
> else { /* delete file name that matches the value of castedPtr->tname */
> char *command = (char*)malloc(strlen(castedPtr->tname)+6);
> if (!command) {
> crash_and_burn();
> }
> strcpy(command, "rm -f");
> strcat(command, castedPtr->tname);
> system(command);
> free(command);
> }
>
> Something to that effect should work.
... unless the filename contains whitespace or shell metacharacters.
If you're building a string to pass to system(), it is important that
any such characters are escaped. In such cases, it's usually simpler
to avoid system() and use fork/exec/wait instead.
Of course, all of this is largely irrelevant here, as invoking `rm' as
a subprocess isn't exactly the best way to delete a file.
--
Glynn Clements <[EMAIL PROTECTED]>