kjohri

Uname command with examples

Uname command in Linux

1. uname command

The uname command prints information about the system. uname is a portmanteau word made up from "unix" and "name". uname can be called from the command line in Linux as,

uname [OPTION]...

The most common way to use uname is with the -a option, which stands for "all" the information.… Read more

nginx 502 Bad Gateway

1.0 502 Bad Gateway

nginx 502 Bad Gateway

After the update of Linux on the web server, the website would not open in the browser. Instead the following error was displayed.

502 Bad Gateway
nginx/1.14.0

2.0 Solution

First we check the nginx server.

$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-03-21 15:16:47 UTC; 16h ago
     Docs: man:nginx(8)
 Main PID: 1354 (nginx)
    Tasks: 8 (limit: 1158)
   CGroup: /system.slice/nginx.service
           ├─1354 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─1355 nginx: worker process

Warning: Journal has been rotated since unit was started.
Read more

How to trim a string in C

Trim a string

The problem of removing leading and trailing whitespace characters in strings occurs in programming quite often. Here is a solution.

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

void trim (char *dest, char *src); 

int main (int argc, char **argv)
{
    char inbuf [1024];
    char outbuf [1024];

    printf ("Type a string : ");
    while (fgets (inbuf, 1024, stdin) != NULL) {
        trim (outbuf, inbuf);      
        printf ("input string -%s-\n\n", inbuf);
        printf ("string -%s-\n\n", outbuf);
        printf ("Type a string : ");
    }

}

// trim: leading and trailing whitespace of string
void trim (char *dest, char *src)
{
    if (!src || !dest)
       return;

    int len = strlen (src);

    if (!len) {
        *dest = '\0';
        return;
    }
    char *ptr = src + len - 1;

    // remove trailing whitespace
    while (ptr > src) {
        if (!isspace (*ptr))
            break;
        ptr--;
    }

    ptr++;

    char *q;
    // remove leading whitespace
    for (q = src; (q < ptr && isspace (*q)); q++)
        ;

    while (q < ptr)
        *dest++ = *q++;

    *dest = '\0';
}

We can compile and run the above program.… Read more