Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why not use the <strong><a href="http://www.manpagez.com/man/3/remove/" rel="nofollow noreferrer">remove</a></strong> or <strong><a href="http://www.manpagez.com/man/2/unlink/" rel="nofollow noreferrer">unlink</a></strong> command instead of <code>system("rm ...")</code>?</p> <p><code>remove(argv[2]);</code> or <code>unlink(argv[2]);</code></p> <hr> <p><strong>Update</strong> in the case that <code>system("rm ...")</code> must be used</p> <p>If you must use <code>system("rm ...")</code>, <a href="https://stackoverflow.com/questions/2250140/using-the-rm-command-in-a-c-program-using-the-system-function-in-stdlib-h/2250150#2250150">ZelluX</a> is correct in stating that you must retrieve the filename from <code>argv[2]</code>. You can retrieve the string in <code>argv[2]</code> using <strong><a href="http://www.manpagez.com/man/3/snprintf/" rel="nofollow noreferrer">snprintf</a></strong> or <strong><a href="http://www.manpagez.com/man/3/strncpy/" rel="nofollow noreferrer">strncpy</a></strong>. Using the variants of the functions that restrict the input size is a <a href="http://www.ibm.com/developerworks/library/s-buffer-defend.html" rel="nofollow noreferrer">good idea</a> as there is no guarantee as to the length of the string in <code>argv[2]</code>.</p> <p>Depending on your application, you may also want to call <strong><a href="http://www.manpagez.com/man/2/stat/" rel="nofollow noreferrer">stat</a></strong> to verify that the string in <code>argv[2]</code> is indeed a file and possibly restrict the type of file.</p> <p><strong>Example:</strong> </p> <p>This example calls <strong><a href="http://www.manpagez.com/man/2/stat/" rel="nofollow noreferrer">stat</a></strong> to verify that <code>argv[2]</code> is a regular file and <strong><a href="http://www.manpagez.com/man/3/asprintf/" rel="nofollow noreferrer">asprintf</a></strong> to dynamically allocate space for the buffer.</p> <pre><code>char *p; struct stat st; if (stat(argv[2], &amp;st) == 0 &amp;&amp; S_ISREG(st-&gt;st_mode)) { if (asprintf(&amp;p, "rm %s", argv[2]) != -1) { system(p); free(p); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload