真的有避風港,還在淡水...
檢視較大的地圖
2012/07/06
Get cpu cores number in linux
Output:
cpu count: 4
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int main() {
- int num = 1;
- char key[512], deli[512], value[512];
- FILE* fd = fopen("/proc/cpuinfo", "r");
- if(fd != 0)
- {
- while(!feof(fd))
- {
- memset(key, 0, sizeof(key));
- memset(value, 0, sizeof(value));
- fscanf(fd, "%s%s%[^\n]", key, deli, value);
- if(strcmp(key, "processor") == 0)
- {
- sscanf(value, "%d", &num);
- num++; /* processor index is 0, 1.... should add 1 */
- }
- }
- fclose(fd);
- }
- return 0;
- }
2012/06/20
smtp over starttls
Beware, "\r\n" means the return key right after you issue a command.
C:\>telnet smtp.gmail.com 587
220 mx.google.com ESMTP nh6sm29764930pbc.44
Then input
EHLO\r\n
250-mx.google.com at your service, [114.34.198.249]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
STARTTLS\r\n
220 2.0.0 Ready to start TLS
And then SSL connect should be established.
2012/04/19
Size of memory allocation(malloc) for c language
From: BASIC programming forum - "C": Size of memory allocated to a pointer?
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
void *my_alloc(int size) {
int *block;
/* bump up request so we have a place to store the size */
block = malloc(size+sizeof(int));
if (block == NULL)
return NULL;
/* store the size */
*block = size;
/* and return pointer to next place (automatically scaled) */
return block + 1;
}
void my_free(void *block0) {
int *block = block0;
/* go back to the actual allocation address */
block--;
free(block);
}
int my_msize(void *block0) {
int *block;
/* back up to the size of the block */
block = (int *)block0 - 1;
return *block;
}
int main() {
char *p;
int rc;
p = my_alloc(50);
printf("Size of p is %d\n", my_msize(p));
my_free(p);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
void *my_alloc(int size) {
int *block;
/* bump up request so we have a place to store the size */
block = malloc(size+sizeof(int));
if (block == NULL)
return NULL;
/* store the size */
*block = size;
/* and return pointer to next place (automatically scaled) */
return block + 1;
}
void my_free(void *block0) {
int *block = block0;
/* go back to the actual allocation address */
block--;
free(block);
}
int my_msize(void *block0) {
int *block;
/* back up to the size of the block */
block = (int *)block0 - 1;
return *block;
}
int main() {
char *p;
int rc;
p = my_alloc(50);
printf("Size of p is %d\n", my_msize(p));
my_free(p);
return 0;
}
2012/01/06
c# hex str transformation
Form looks like this...
Code is below...
Code is below...
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace StrHex
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- EncodingInfo[] infos = Encoding.GetEncodings();
- for (int i = 0; i < infos.Length; i++)
- comboBox1.Items.Add(string.Format("{0} - {1}", infos[i].DisplayName, infos[i].Name));
- if (comboBox1.Items.Count > 0)
- comboBox1.SelectedIndex = 0;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Encoding encode = Encoding.GetEncoding(name);
- if (encode != null)
- {
- Byte[] bs = encode.GetBytes(textBox1.Text);
- if (bs.Length * 2 > textBox1.MaxLength)
- return;
- foreach (Byte b in bs)
- sb.AppendFormat("{0:X2}", b);
- textBox1.Text = sb.ToString();
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Encoding encode = Encoding.GetEncoding(name);
- if (encode != null)
- {
- if (textBox1.TextLength % 2 == 0)
- {
- for (int i = 0; i < textBox1.TextLength; i += 2)
- {
- Byte b = 0;
- string hex = textBox1.Text.Substring(i, 2);
- if (Byte.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out b))
- al.Add(b);
- else
- return;
- }
- }
- }
- }
- private void textBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Control && e.KeyCode == Keys.A)
- textBox1.SelectAll();
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Control && e.KeyCode == Keys.A)
- {
- textBox1.SelectAll();
- textBox1.Focus();
- }
- if (e.Control && e.KeyCode == Keys.H)
- button1.PerformClick();
- if (e.Control && e.KeyCode == Keys.S)
- button2.PerformClick();
- if (e.Control && e.KeyCode == Keys.W)
- this.Close();
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- label2.Text = string.Format("Word Count: {0}", textBox1.TextLength);
- }
- }
- }
2011/12/29
2011/12/26
net snmp 尋找 mib tree
先看 net snmp
把mib檔設定一下 然後載入mib檔
找名字含有ssl的snmp oid
snmptranslate -TB '.*ssl.*'
從 http://www.net-snmp.org/wiki/index.php/TUT:snmptranslate 看來的...
把mib檔設定一下 然後載入mib檔
找名字含有ssl的snmp oid
snmptranslate -TB '.*ssl.*'
從 http://www.net-snmp.org/wiki/index.php/TUT:snmptranslate 看來的...
2011/12/13
html encode/decode using javascript
- String.prototype.encodeHTML = function() { var d=document.createElement("div");(d.textContent)?(d.textContent=this):d.innerText=this;return d.innerHTML };
- String.prototype.decodeHTML = function() { var d=document.createElement("div");d.innerHTML=this;return d.innerText||d.textContent };
2011/11/23
net snmp
net-snmp download
安裝至預設路徑C:\netsnmp
設定 C:\netsnmp\usr\etc\snmp\snmp.conf
document -> C:\netsnmp\usr\docs\Net-SNMP.chm
#放mib的地方
mibdirs C:/netsnmp/usr/share/snmp/mibs-XXXX
persistentDir C:/netsnmp/usr/snmp/persist
tempFilePattern C:/netsnmp/usr/temp/snmpdXXXXXX
#載入所有的mib
mibs all
#預設版本 snmpv2c
defVersion 2c
#預設Community
defCommunity private
然後就可以用cmd來下指令了...
P.S.
記得要加環境變數 ;C:\netsnmp\usr\bin 在PATH後面...
snmp走走...
snmpwalk 192.168.1.1 system
從頭走到尾...
snmpwalk 192.168.1.1 .1
snmp翻譯成樹狀...
snmptranslate -IR -Tp system
安裝至預設路徑C:\netsnmp
設定 C:\netsnmp\usr\etc\snmp\snmp.conf
document -> C:\netsnmp\usr\docs\Net-SNMP.chm
#放mib的地方
mibdirs C:/netsnmp/usr/share/snmp/mibs-XXXX
persistentDir C:/netsnmp/usr/snmp/persist
tempFilePattern C:/netsnmp/usr/temp/snmpdXXXXXX
#載入所有的mib
mibs all
#預設版本 snmpv2c
defVersion 2c
#預設Community
defCommunity private
然後就可以用cmd來下指令了...
P.S.
記得要加環境變數 ;C:\netsnmp\usr\bin 在PATH後面...
snmp走走...
snmpwalk 192.168.1.1 system
從頭走到尾...
snmpwalk 192.168.1.1 .1
snmp翻譯成樹狀...
snmptranslate -IR -Tp system
2011/08/23
Git get diff file list and diff content from some branch
- BRANCH_NAME='dev'
- SOURCE_DIR='/home/me/src'
- EXPORT_TO='/home/me/out'
- cd $SOURCE_DIR
- git diff --name-only $BRANCH_NAME > $EXPORT_TO/changes.txt
- git diff $BRANCH_NAME > $EXPORT_TO/changes.diff
2011/08/12
Unix like shell - find_keywords
- #!/bin/sh
- if [ "xx$2" != "xx" ] && [ -e $2 ]; then
- echo finding $1 in $2
- if [ "xx$WINDIR" != "xx" ]; then
- grep -lr $1 $2 | sed 's~/~\\~g' | sed 's~^\\\(\w\)\\~\U\1:\\~g'
- else
- grep -lr $1 $2
- fi
- else
- echo first param must be search string
- echo second param must be full path
- fi
2011/08/10
CodeBlocks 中文化
下載mo檔:
codeblocks translation page
www.badongo.com - Click Here to Download codeblocks zh_TW
www.u-file.net - Click Here to Download codeblocks zh_TW
然後建一個目錄 C:\Program Files\CodeBlocks\share\CodeBlocks\locale\zh_TW
其中 C:\Program Files\CodeBlocks 是安裝路徑
然後把mo檔放到裏面去
Settings -> Environment -> View -> Internationalization
勾起來 -> 選 Chinese
然後重開CodeBlocks
搞定...
BTW...中文化以後, syntex highlight好像消失了...
所以還是把勾勾拿掉好了....sigh.....
codeblocks translation page
www.badongo.com - Click Here to Download codeblocks zh_TW
www.u-file.net - Click Here to Download codeblocks zh_TW
然後建一個目錄 C:\Program Files\CodeBlocks\share\CodeBlocks\locale\zh_TW
其中 C:\Program Files\CodeBlocks 是安裝路徑
然後把mo檔放到裏面去
Settings -> Environment -> View -> Internationalization
勾起來 -> 選 Chinese
然後重開CodeBlocks
搞定...
BTW...中文化以後, syntex highlight好像消失了...
所以還是把勾勾拿掉好了....sigh.....
2011/07/15
Compare a Copy of a Project
Usage:
compare-projects /home/me/project/ /home/me/old/project/ | grep -v OK > /home/me/compare_result 2>&1
Source: compare-projects
- #!/usr/bin/sh
- #
- # settings
- #
- md5=~/bin/md5files
- #
- # inputs
- # src: main project
- # dst: another project
- #
- src=$1
- dst=$2
- #
- # limit file size smaller than 1024k (pass to find)
- # md5 all files, and then replace the path to destination
- # finally compare the md5 "use md5sum -c"
- #
- $md5 $src -size -1024k | sed "s!${src}!${dst}!g" | md5sum -c 2>&1
Get All Files Recursively and Get MD5
- #!/usr/bin/sh
- #
- # find all files recursively, and enum them to get md5sum
- # args: pass all to find
- #
- for f in $(find $@ -type f);
- do
- md5sum $f
- done
訂閱:
文章 (Atom)