c – Implementation of GNU shuf (1) which now does not support characters

I implemented some time ago shuf(1), but there was a problem: No characters were ignored in the input.

This new version tries to solve this problem (but uses twice the memory).

shuf(1) merges and prints the lines of files specified as arguments or as standard input (if no argument is specified). If the -n NUM Option were given to prints NUM random lines from the files.

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#ifdef LIBBSD
#include 
#include 
#endif

/* The buffer structure */
struct Buffer {
    char  *chars;       /* array of characters in the buffer */
    char **linebeg;     /* array of pointers to beginning of lines in chars() */
    char **lineend;     /* array of pointers to end of lines in chars() */
    size_t maxchars;    /* size of the array of characters */
    size_t maxlines;    /* size of the array of lines */
    size_t nchars;      /* number of characters in the buffer */
    size_t nlines;      /* number of lines in the buffer */
};

static int readfile(int fd, struct Buffer *buf);
static void getlines(struct Buffer *buf);
static void shuf(struct Buffer *buf, size_t nshuf);
static void printbuf(struct Buffer *buf, size_t nshuf);
static size_t getnshuf(const char *s);
static void usage(void);

/* sortnsearch: read a file into memory, sort it by lines and search a line */
int
main(int argc, char *argv())
{
    int c;
    int fd;
    int exitval;
    size_t nshuf;
    char *filename;
    struct Buffer buf = {NULL, NULL, NULL, 0, 0, 0, 0};

    nshuf = 0;
    while ((c = getopt(argc, argv, "n:")) != -1) {
        switch (c) {
        case 'n':
            nshuf = getnshuf(optarg);
            if (nshuf == 0)
                errx(EXIT_FAILURE, "%s: invalid number of lines", optarg);
            break;
        default:
            usage();
            break;
        }
    }
    argc -= optind;
    argv += optind;

    exitval = EXIT_SUCCESS;

    fd = STDIN_FILENO;
    filename = "stdin";
    do {
        if (*argv) {
            if (**argv == '-' && **(argv+1) == '') {
                fd = STDIN_FILENO;
            } else if ((fd = open(*argv, O_RDONLY)) == -1) {
                warn("%s", *argv);
                exitval = EXIT_FAILURE;
                argv++;
                continue;
            }
            filename = *argv++;
        }
        if (readfile(fd, &buf) == -1) {
            warn("%s", *argv);
            exitval = EXIT_FAILURE;
        }
        if (fd != STDIN_FILENO)
            (void)close(fd);
    } while (*argv);

    getlines(&buf);

    if (nshuf == 0 || nshuf > buf.nlines)
        nshuf = buf.nlines;

    shuf(&buf, nshuf);

    printbuf(&buf, nshuf);

    free(buf.chars);
    free(buf.linebeg);
    free(buf.lineend);

    return exitval;
}

/* read file fd into memory in buf->chars */
static int
readfile(int fd, struct Buffer *buf)
{
    char tmpbuf(BUFSIZ), *tmp;
    size_t size = buf->maxchars;
    ssize_t n;

    while ((n = read(fd, tmpbuf, sizeof tmpbuf)) != 0 && n != -1) {
        if (n + buf->nchars >= size) {
            size = buf->maxchars + sizeof tmpbuf;

            /* overflow check */
            if (size <= buf->maxchars) {
                (void)free(buf->chars);
                errc(EXIT_FAILURE, EOVERFLOW, NULL);
            }

            if ((tmp = realloc(buf->chars, size)) == NULL) {
                (void)free(buf->chars);
                err(EXIT_FAILURE, "realloc");
            }
            buf->chars = tmp;
            buf->maxchars = size;
        }
        memcpy(buf->chars + buf->nchars, tmpbuf, n);
        buf->nchars += n;
    }

    if (n == -1)
        return -1;

    return 1;
}

/* get beginnings end ends of lines from buf->chars into buf->linebeg and buf->lineend */
static void
getlines(struct Buffer *buf)
{
    size_t i;
    size_t size;
    char **tmp;

    if ((buf->linebeg = reallocarray(NULL, BUFSIZ, sizeof *buf->linebeg)) == NULL)
        err(EXIT_FAILURE, "realloc");
    if ((buf->lineend = reallocarray(NULL, BUFSIZ, sizeof *buf->lineend)) == NULL)
        err(EXIT_FAILURE, "realloc");
    buf->linebeg(buf->nlines++) = buf->chars;
    buf->maxlines = size = BUFSIZ;

    for (i = 0; i < buf->nchars; i++) {
        if (buf->chars(i) == 'n') {
            if (buf->nlines + 1 >= size) {
                size = buf->maxlines << 1;

                if ((tmp = reallocarray(buf->linebeg, size, sizeof *buf->linebeg)) == NULL)
                    err(EXIT_FAILURE, "realloc");
                buf->linebeg = tmp;
                if ((tmp = reallocarray(buf->lineend, size, sizeof *buf->lineend)) == NULL)
                    err(EXIT_FAILURE, "realloc");
                buf->lineend = tmp;
                buf->maxlines = size;
            }
            buf->lineend(buf->nlines-1) = buf->chars+i+1;
            buf->linebeg(buf->nlines++) = buf->chars+i+1;
        }
    }
    buf->lineend(--buf->nlines) = buf->chars+i+1;
}

/* shuffle buf->linebeg and buf->lineend parallelly */
static void
shuf(struct Buffer *buf, size_t nshuf)
{
    size_t i, j;
    char *tmp;

    for (i = buf->nlines-1; i > 0 && nshuf > 0; i--, nshuf--) {
        j = arc4random_uniform(i + 1);

        tmp = buf->linebeg(i);
        buf->linebeg(i) = buf->linebeg(j);
        buf->linebeg(j) = tmp;

        tmp = buf->lineend(i);
        buf->lineend(i) = buf->lineend(j);
        buf->lineend(j) = tmp;
    }
}

/* print at most nshuf lines */
static void
printbuf(struct Buffer *buf, size_t nshuf)
{
    while (nshuf-- > 0)
        write(STDOUT_FILENO, buf->linebeg(nshuf),
              buf->lineend(nshuf) - buf->linebeg(nshuf));
}

/* return number from s */
static size_t
getnshuf(const char *s)
{
    size_t n;
    char *endp;

    n = strtoull(s, &endp, 10);
    if (errno == ERANGE || endp == s || *endp != '')
        return 0;

    return (size_t) n;
}

/* show usage */
static void
usage(void)
{
    (void)fprintf(stderr, "usage: shuf (-n number) (file...)n");
    exit(EXIT_FAILURE);
}

Dual Boot – Gnu Grub version 2.04 blocks me from booting

When I try to boot into Ubuntu, the grub error appears. This happened after using AOMEI Partition Assistant to merge my Windows partition with unallocated space and immediately afterwards I started getting this error. I can still boot perfectly with Windows 10. I was looking for fixes but couldn't find anything that would solve the same problem as me.

https://imgur.com/a/uFwBpue: This is a video that shows what happens

gnu screen – Apache + PHP or Node.js for creating a web interface

background: I manage a VPS (Ubuntu 16.04) for an esport team and we run a number of game servers and language servers there. Since the performance of the VPS is limited, I created a "web interface" with PHP to start, stop and manage the game servers. My (not so good) solution is that the user of www-data executes commands as sudo with all commands that he has to explicitly specify in the sudoers file. Since I think this may not be the best and safest solution, I have considered other ways to create this web interface.

question: What is the best technology for creating a web interface?

requirement::

  • Users have to log in / register etc. (basic user management also)
  • The user can press a button that executes a command on the VPS (e.g., launching a screen for a game server).
  • It would be great to "include" the screen in the web interface. I've seen this with some professional hosting providers, so it's possible, I just don't know how complicated it is
  • I also plan to include a teamspeak3 bot that can assign groups / perms to users. There is also a Node.js library for teamspeak3 queries

I also thought about using some pre-built web interfaces like https://easy-wi.com/, but since I like programming, I thought I could try it myself 🙂

I look forward to suggestions, tips and answers.

Thank you and have a nice weekend,
Basti

Licensing – If I use a licensed library under GNU, do I have to do anything when distributing my project?

I am currently playing a game and am going to use LibNoise http://libnoise.sourceforge.net/index.html, but the version has been ported to C # for the sake of unity. It is under the GNU license and I would like to know if I need to do anything special for that reason. Example: If I use this, I have to show the player the license every time he plays the game. Or do I need to add a tab to the game where they can view the license?

I'm sorry I'm not very well trained with this stuff! Reading the license made me more confused, haha! Thank you so much!

boot – GNU GRUB terminal when dual-booting Ubuntu with Windows 10

I am a Windows 10 user and want to install Ubuntu to get into the world of Linux. I want to dual boot Ubuntu with Windows 10. However, when I try to install boot from a USB stick, I always get the GNU GRUB command line window. Please let me be clear that I want to do a dual boot. The problem remains with Kali. I would be grateful if someone would help me or point me to an answer.

Homebrew – installing GNU compilers on PureDarwin / Darwin?

I hope this is not an issue as there is no better place to ask such questions about SO / SE. I downloaded and ran PureDarwin on QEMU. To do this, use the following instructions:

     

Now I want to know if there are package managers? For example, can I preferably install HomeBrew here or even MacPorts? If this is not possible, how can I install GNU C / C ++ / Fortran compiler (GCC / g ++ / gfortran) here? or if there is no way, is there a compiler on this operating system at all? Finally, my goal is to offer some open source developers who don't have Macs an alternative to testing their software and possibly creating MacOS compatible binaries / packages if possible. Thank you in advance for your support.

Windows – Can I legally use GNU Linux Wine now?

I'm afraid to ask this question under my regular username, for fear of being beaten, so I ask anonymously.

I have a lot of expensive software and hardware that doesn't work very well in the latest versions of Windows 10 anymore. Unfortunately, this software requires relatively low latency, so virtual machines are not a very good option. It seems to me that Microsoft is ready to leave me behind and move forward for the mainstream users who don't have such varied and ancient needs.

The strange thing is that I can show that the hardware I need still works fine in recent GNU Linux builds, and I don't have to worry about my PC being at risk due to lack of security patches. I also suspect that I can run many of my required programs in Wine with reasonable success.

While I would absolutely love If Microsoft were using Open Source XP so I could run my old games, software, and hardware on a non-networked computer, I don't think they will soon. On the other hand, Microsoft is now "supposedly" GNU Linux friendly. In fact, I've seen them adopt technologies that are not important to me to be more open source friendly.

Given Microsoft's new stance, do you think it will subside and stop "tracking" wine developers and users?

Don't get me wrong, the fact that GNU Linux names the background driver daemons REALLY irritates me, but I am unable to replace all of my previously expensive software and hardware. GNU Linux and Wine could possibly allow me to continue using my older assets if Microsoft stopped chasing Wine users as if they were bad. I think maybe once (a long time ago) Microsoft was even considering suing Wine developers. That's why I stopped using Wine at the time.

Finally, a version recommended or sponsored by Microsoft or a dialect of Wine could be really cool. Maybe even a Microsoft version of GNU Linux that doesn't name the background driver daemons. I can't say I absolutely have the money, but people might be willing to pay for a Microsoft distribution of GNU Linux provided it has legal MPEG / multimedia decoding / coding and efficient Ability to run Microsoft programs.

I'm sorry if this post reads like an editorial. I'm broke and very frustrated.

NOTE: I know we shouldn't be asking for legal advice here …

Tamron tap in console with GNU / Linux

Does anyone of you have experience with this combo? Do you calibrate the lens under GNU / Linux? Maybe with Wine or Virtual Box? It works?

I already have 3 Tamron SP lenses and I think one of them requires calibration of the 90mm macro. Further tests are required. I am selling my last Nikon lenses and will only have Tamron all SP versions that work with tap in the console. Need to check all lenses.

I use Fedora GNU / Linux as the main operating system on my private laptop and I want to buy Tap in Console. Will I be able to use it? Does it work with Wine or VirtualBox?

Does anyone have experience?

You can find information about Sigma Dock on Linux, but one has even asked Tamron Tap in the console.

4/18 – Working Gnu screen alternative for WSL minecraft servers

I want to start my minecraft server on SSH under WSL (Ubuntu 18.04) under Windows.

That's how I plan it with a phone or something: connect to vpn, then ssh in Windows, type wsl (works fine) and finally open a session (for example, screen) and start the script for the Minecraft server there.

The problem is, when I finally start running the script and disconnecting the screen and closing the SSH connection, the screen closes automatically after a while. WSL says it has ended. I do not know what to do.

I should also add that the script should run permanently. The mc server runs on Java and constantly outputs something. I want it to be available at all times.