C code to reset all usb devices (because ubuntu laptop disables usb mouse after sleep) I don't program much in c, but cobbled together some code from different places in google. I'm sure this is backwards especially in how it works with strings and directory files. Please let me know what the best practices would have been.
/* usbresetall -- reset all of the usb devices on bus 001 */

#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;errno.h&gt;
#include &lt;sys/ioctl.h&gt;

#include &lt;dirent.h&gt; 
#include &lt;string.h&gt;

#include &lt;linux/usbdevice_fs.h&gt;


int main(int argc, char **argv)
{
    int fd;
    int rc;

    DIR *d;
    char filename[256];  
    struct dirent *dir;

    d = opendir(&quot;/dev/bus/usb/001/&quot;);
    strcpy(filename,&quot;/dev/bus/usb/001/&quot;);
    
    if (d) {
        while ((dir = readdir(d)) != NULL) {
            strcat(filename,dir-&gt;d_name);
            printf(&quot;%s\n&quot;, filename);
            
        fd = open(filename, O_WRONLY);
        if (fd &lt; 0) {
        perror(&quot;Error opening output file&quot;);
        } else {

        printf(&quot;Resetting USB device %s\n&quot;, filename);
        rc = ioctl(fd, USBDEVFS_RESET, 0);
        if (rc &lt; 0) {
            perror(&quot;Error in ioctl&quot;);
        } else printf(&quot;Reset successful\n&quot;);
            
        close(fd);
            }         
            
            strcpy(filename,&quot;/dev/bus/usb/001/&quot;);
        }
        closedir(d);
    }

    return 0;
}
Use consistent indentation
I don't know if the indentation style of the code you pasted here is as you wrote it, but if it looks the same in your code editor, you should definitely try to fix it and make it more consistent. I would not bother trying to fix this manually, instead use either code formatting functions of your editor, or use an external tool such as GNU indent, ClangFormat or Artistic Style to reformat your source code.
Return a non-zero exit code on error
When you encounter an error you call perror(), which is great, but then you let the program continue to run, and eventually you will always return 0. It would be better if your program would return a non-zero exit code when it has encountered an error, in particular return EXIT_FAILURE, which is declared in &lt;stdlib.h&gt;.
Don't hardcode the USB bus number
You only open USB bus 1. What if you attach your mouse to a different USB port, which might be on a different bus? I would make it so that the USB bus number has to be passed as a command line option. This makes the program a lot more flexible.
Beware of buffer overflows
While arguably the /dev/bus/usb pathnames can never be longer than 255 bytes, it is bad practice to make any assumptions. At the very least, make sure you never overflow this buffer, by using strncpy() and strncat() instead of strcpy() and strcat() (read the documentation of these functions carefully and be aware what happens if the destination buffer is not large enough to hold the input), or use snprintf(). However, there is another way to sidestep this issue entirely:
Avoid the need for concatenating paths
Instead of manipulating filename all the time, you can avoid all this by calling chdir() to go into the USB bus's directory, like so:
if (chdir(&quot;/dev/bus/usb/001/&quot;) == -1) {
    perror(&quot;Error accessing USB bus directory&quot;);
    return EXIT_FAILURE;
}

DIR *d = opendir(&quot;.&quot;);

if (!d) {
    perror(&quot;Error reading USB bus directory&quot;);
    return EXIT_FAILURE;
}

struct dirent *ent;

while ((ent = readdir(d)) != NULL) {
    int fd = open(ent-&gt;d_name, O_WRONLY);
    ...

Declare variables as close as possible to where they are used
In C99 and later, you can declare a variable anywhere. It is considered good practice to declare a variable close to where it is used, and ideally you also declare and initialize it in one go. I've used this in the example above.