snippets / flaith / 

All flaith's snippets (4)

  1. Make your own dmesg !

    This is a sample use of the klogctl kernel function which is called syslog in kernel, but the syslog function for glibc is completely different. This program gets the size of the available kernel messages, put it in a buffer with the right size, empty the kernel messages, and show messages to stdout. To disable clearing (and make this program works as non super user), replace klogctl(4 by klogctl(3, for more information look at the klogctl's man page

     1 #include <stdio.h>
    2 #include <sys/klog.h>
    3
    4 int main(int argc, char **argv, char **envp) {
    5 int ret;
    6 //This call returns the size of the kernel messages
    7 ret=klogctl(9, NULL,0);
    8 char *buf=malloc(ret);
    9 //That one send all messages in "buf" with "ret" as maximum size, and empty the messages
    10 ret=klogctl(4, buf, ret);
    11 //And display the buffer.
    12 printf("%s\n", buf);
    13 perror("klogctl");
    14 return 1;
    15 }
    first posted by phh to c dmesg klogctl ... saved by 2 persons ... 0 comments ... 1 year, 1 month
  2. Yet another uname command

    Tested on linux, FreeBSD, MacOS X, and solaris with gcc and suncc

     1 /**
    2 DO WHAT THE FUCK YOU WANT TO, BUT IT'S NOT MY FAULT PUBLIC LICENSE
    3 Version 1, November 2007
    4
    5 Copyright (C) 2004 Sam Hocevar
    6 14 rue de Plaisance, 75014 Paris, France
    7 Everyone is permitted to copy and distribute verbatim or modified
    8 copies of this license document, and changing it is allowed as long
    9 as the name is changed.
    10
    11 DO WHAT THE FUCK YOU WANT TO, BUT IT'S NOT MY FAULT PUBLIC LICENSE
    12 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
    13
    14 0. You just DO WHAT THE FUCK YOU WANT TO, BUT IT'S NOT MY FAULT.
    15 **/
    16
    17 #include <stdlib.h>
    18 #include <stdio.h>
    19 #include <sys/utsname.h>
    20
    21 int main(void)
    22 {
    23 struct utsname uname_s;
    24
    25 if (uname(&uname_s) >= 0) {
    26 printf("Sysname\t: %s\nNodename: %s\nRelease\t: %s\nVersion\t: %s\nMachine\t: %s\n",
    27 uname_s.sysname,
    28 uname_s.nodename,
    29 uname_s.release,
    30 uname_s.version,
    31 uname_s.version);
    32 #ifdef _GNU_SOURCE
    33 printf("Domain\t: %s\n", uname_s.domainname);
    34 #endif
    35 printf("\n");
    36 return EXIT_SUCCESS;
    37 }
    38
    39 return EXIT_FAILURE;
    40 }
    first posted by slubman to c code inutilitaire ... saved by 2 persons ... 0 comments ... 1 year, 1 month
  3. connection a une base de donnée MySQL en C

    Code en C permettant l'accès a une base de donnée MySQL C code wich permit MySQL access

     1 /*********************************************************
    2 **Attention il est impératif de compiler ce code avec l'option de compilation -lmysqlclient
    3 **********************************************************/
    4
    5 #include <stdio.h>
    6 #include <errno.h>
    7 #include <stdlib.h>
    8 #include <iostream.h>
    9 #include <fstream>
    10 #include <mysql/mysql.h>
    11 #include <sys/types.h>
    12 #include <sys/wait.h>
    13
    14 #define MY_SERVER_HOST "localhost"
    15 #define MY_SERVER_PORT 0
    16 #define MY_ACCOUNT "root"
    17 #define MY_PASS "test"
    18 #define MY_DB_NAME "itest"
    19 #define MY_TABLE_NAME "test_tbl"
    20 #define MY_UX_SOCK NULL
    21 #define MY_CLIENT_FLAG 0
    22
    23 void main()
    24 {
    25 MYSQL *mysql;
    26 MYSQL_RES *res;
    27 MYSQL_ROW row;
    28 MYSQL_ROW rowchamps;
    29 const char *query;
    30 string qutmp;
    31
    32 int t,f,tt,ff;
    33
    34 mysql=mysql_init(NULL);
    35
    36 if (!mysql_real_connect(mysql,MY_SERVER_HOST,MY_ACCOUNT,MY_PASS,bd,MY_SERVER_PORT,MY_UX_SOCK,MY_CLIENT_FLAG)) {
    37 printf( "Erreur de connexion : %s\n",mysql_error(mysql));
    38 }
    39 else {
    40 qutmp = "SHOW FULL COLUMNS FROM TABLE ";
    41 query = qutmp.c_str();
    42 t=mysql_real_query(mysql, query, (unsigned int) strlen(query));
    43 if((res=mysql_use_result(mysql))) {
    44 f=mysql_num_fields(res);
    45 while((row=mysql_fetch_row(res))) {
    46 for(f=0;f<t;f++){
    47 cout<<row[f]<<endl;
    48 }
    49 }
    50 mysql_free_result(res);
    51 }
    52 }
    53 mysql_close(mysql);
    54 }
    first posted by cybersax to c c c++ ... saved by 7 persons ... 0 comments ... 1 year, 1 month
  4. replace ^M by new line with vim

    tip to **replace ^M by unix new line ending** that appear on some file. ^ mean _ctrl key_.

    1 :%s/^V^M/\r/g
    first posted by benoitc to shell vim convert convertir content ... saved by 4 persons ... 1 comments ... 1 year, 1 month
showing 10, 25, 50 items per pages

Pages : 1