Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,702 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include <stdio.h> | ||
|
||
int main(int argc, char* argv[]) { | ||
printf("%d, %s", argc, argv[0]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
void binary(char c) { | ||
int i; | ||
for (i = 7; i >= 0; i--) { | ||
if(c & (1 << i)) | ||
printf("1"); | ||
else | ||
printf("0"); | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
char *s = argv[1]; | ||
int l = strlen(s); | ||
int i; | ||
|
||
for (i = 0; i < l; i++) { | ||
binary(s[i]); | ||
printf(" "); | ||
} | ||
printf("\n"); | ||
for (i = 0; i < l; i++) { | ||
printf("%8d", s[i]); | ||
printf(" "); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
void binary(char c) { | ||
int i; | ||
for (i = 7; i >= 0; i--) { | ||
if(c & (1 << i)) | ||
printf("1"); | ||
else | ||
printf("0"); | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
FILE *f = fopen(argv[1], "r"); | ||
char s[1000] = {}; | ||
fread(s, 1, 10000, f); | ||
int l = strlen(s); | ||
int i; | ||
printf("--------------------%d----------------------", l); | ||
for (i = 0; i < l; i++) { | ||
binary(s[i]); | ||
printf(" "); | ||
} | ||
printf("\n"); | ||
for (i = 0; i < l; i++) { | ||
printf("%8d", s[i]); | ||
printf(" "); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
|
||
static const char UTF8_BYTE_PREFIX = 0x80; | ||
static const char UTF8_BYTE_MASK = 0xc0; | ||
|
||
int clear_non_ascii_or_utf8(char *str, size_t len) | ||
{ | ||
unsigned int i = 0; | ||
while(i < len) { | ||
uint16_t unicode = 0; | ||
if((str[i] & 0x80) == 0x00) // one byte | ||
{ | ||
i++; | ||
} | ||
else if((str[i] & 0xe0) == 0xc0) // two bytes | ||
{ | ||
if (i + 1 > len) { | ||
str[i] = '_'; | ||
return 1; | ||
} | ||
/* check whether the byte is in format 10xxxxxx */ | ||
if (((str[i + 1]) & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) { | ||
str[i] = '_'; | ||
if ((str[i + 1] & 0x80) != 0x00) | ||
str[i + 1] = '_'; | ||
i = i + 2; | ||
continue; | ||
} | ||
/* get unicode value */ | ||
unicode = (((str[i] & 0x1f) << 6) | (str[i + 1] & 0x3f)); | ||
/* validate unicode range */ | ||
if (!(unicode >= 0x80 && unicode <= 0x7ff)) { | ||
str[i] = '_'; | ||
str[i + 1] = '_'; | ||
i = i + 2; | ||
continue; | ||
} | ||
i = i + 2; | ||
} | ||
else if((str[i] & 0xf0) == 0xe0) // three bytes | ||
{ | ||
if (i + 1 > len) { | ||
str[i] = '_'; | ||
return 1; | ||
} | ||
if (i + 2 > len) { | ||
str[i] = '_'; | ||
if ((str[i + 1] & 0x80) != 0x00) | ||
str[i + 1] = '_'; | ||
return 1; | ||
} | ||
/* check whether the byte is in format 10xxxxxx */ | ||
if ((str[i + 1] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) { | ||
str[i] = '_'; | ||
if ((str[i + 1] & 0x80) != 0x00) | ||
str[i + 1] = '_'; | ||
i = i + 2; | ||
continue; | ||
} | ||
if ((str[i + 2] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) { | ||
str[i] = '_'; | ||
str[i + 1] = '_'; | ||
if ((str[i + 2] & 0x80) != 0x00) | ||
str[i + 2] = '_'; | ||
i = i + 3; | ||
continue; | ||
} | ||
/* get unicode value */ | ||
unicode = (((str[i] & 0x0f) << 12) | (( str[i + 1] & 0x3f) << 6) | (str[i + 2] & 0x3f)); | ||
/* validate unicode range */ | ||
if (!(unicode >= 0x800 /* && unicode <= 0xffff */)) { | ||
str[i] = '_'; | ||
str[i + 1] = '_'; | ||
str[i + 2] = '_'; | ||
i = i + 3; | ||
continue; | ||
} | ||
i = i + 3; | ||
} | ||
else if((str[i] & 0xf8) == 0xf0) // four bytes | ||
{ | ||
if (i + 1 > len) { | ||
str[i] = '_'; | ||
return 1; | ||
} | ||
if (i + 2 > len) { | ||
str[i] = '_'; | ||
if ((str[i + 1] & 0x80) != 0x00) | ||
str[i + 1] = '_'; | ||
return 1; | ||
} | ||
if (i + 3 > len) { | ||
str[i] = '_'; | ||
if ((str[i + 1] & 0x80) != 0x00) | ||
str[i + 1] = '_'; | ||
if ((str[i + 2] & 0x80) != 0x00) | ||
str[i + 2] = '_'; | ||
return 1; | ||
} | ||
/* check whether the byte is in format 10xxxxxx */ | ||
if ((str[i + 1] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) { | ||
str[i] = '_'; | ||
if ((str[i + 1] & 0x80) != 0x00) | ||
str[i + 1] = '_'; | ||
i = i + 2; | ||
continue; | ||
} | ||
if ((str[i + 2] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) { | ||
str[i] = '_'; | ||
str[i + 1] = '_'; | ||
if ((str[i + 2] & 0x80) != 0x00) | ||
str[i + 2] = '_'; | ||
i = i + 3; | ||
continue; | ||
} | ||
if ((str[i + 3] & UTF8_BYTE_MASK) != UTF8_BYTE_PREFIX) { | ||
str[i] = '_'; | ||
str[i + 1] = '_'; | ||
str[i + 2] = '_'; | ||
if ((str[i + 3] & 0x80) != 0x00) | ||
str[i + 3] = '_'; | ||
i = i + 4; | ||
continue; | ||
} | ||
uint32_t unicode = 0x00000000; // for 4 bytes utf-8 encoding | ||
unicode = ((str[i] & 0x07) << 18) | ((str[i + 1] & 0x3f) << 12) | | ||
((str[i + 2] & 0x3f) << 6) | ((str[i + 3]& 0x3f)); | ||
if (!(unicode >= 0x00010000 && unicode <= 0x0010ffff)) { /* validate unicode range */ | ||
str[i] = '_'; | ||
str[i + 1] = '_'; | ||
str[i + 2] = '_'; | ||
str[i + 3] = '_'; | ||
i = i + 4; | ||
continue; | ||
} | ||
i = i + 4; | ||
} | ||
else | ||
{ | ||
str[i] = '_'; | ||
i++; | ||
continue; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
FILE *f = fopen(argv[1], "r"); | ||
char s[3000]; | ||
|
||
while (NULL != fgets(s, (int)sizeof(s), f)) { | ||
clear_non_ascii_or_utf8(s, strlen(s)); | ||
printf("%s\n", s); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#define _GNU_SOURCE | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <sched.h> | ||
#include <pthread.h> | ||
#include <stdlib.h> | ||
|
||
#define app_panic(format, args...) \ | ||
do { \ | ||
printf(format, ## args); \ | ||
abort(); \ | ||
} while(0) | ||
|
||
static inline void print_cpu_mask(cpu_set_t cpu_mask) | ||
{ | ||
uint8_t flag; | ||
uint32_t i; | ||
printf("Cpu affinity is "); | ||
flag = 0; | ||
for (i = 0; i < sizeof(cpu_set_t); i ++) { | ||
if (CPU_ISSET(i, &cpu_mask)) { | ||
if (flag == 0) { | ||
flag = 1; | ||
printf("%d", i); | ||
} else { | ||
printf(",%d", i); | ||
} | ||
} | ||
} | ||
printf(".\n"); | ||
} | ||
|
||
static inline void get_cpu_mask(pid_t pid, cpu_set_t *mask) { | ||
if (sched_getaffinity(pid, sizeof(cpu_set_t), mask) == -1) { | ||
app_panic("Get cpu affinity failed.\n"); | ||
} | ||
} | ||
|
||
static inline void set_cpu_mask(pid_t pid, cpu_set_t *mask) { | ||
if (sched_setaffinity(pid, sizeof(cpu_set_t), mask) | ||
== -1) { | ||
app_panic("Set cpu affinity failed.\n"); | ||
} | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
cpu_set_t cpu_mask; | ||
|
||
get_cpu_mask(0, &cpu_mask); | ||
print_cpu_mask(cpu_mask); | ||
|
||
CPU_ZERO(&cpu_mask); | ||
CPU_SET(1, &cpu_mask); | ||
CPU_SET(2, &cpu_mask); | ||
set_cpu_mask(0, &cpu_mask); | ||
|
||
get_cpu_mask(0, &cpu_mask); | ||
print_cpu_mask(cpu_mask); | ||
|
||
while(1); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#define _GNU_SOURCE | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <sched.h> | ||
#include <pthread.h> | ||
#include <stdlib.h> | ||
|
||
#define app_panic(format, args...) \ | ||
do { \ | ||
printf(format, ## args); \ | ||
abort(); \ | ||
} while(0) | ||
|
||
static inline void print_cpu_mask(cpu_set_t cpu_mask) | ||
{ | ||
uint8_t flag; | ||
uint32_t i; | ||
printf("Cpu affinity is "); | ||
flag = 0; | ||
for (i = 0; i < sizeof(cpu_set_t); i ++) { | ||
if (CPU_ISSET(i, &cpu_mask)) { | ||
if (flag == 0) { | ||
flag = 1; | ||
printf("%d", i); | ||
} else { | ||
printf(",%d", i); | ||
} | ||
} | ||
} | ||
printf(".\n"); | ||
} | ||
|
||
static inline void get_cpu_mask(pid_t pid, cpu_set_t *mask) { | ||
if (sched_getaffinity(pid, sizeof(cpu_set_t), mask) == -1) { | ||
app_panic("Get cpu affinity failed.\n"); | ||
} | ||
} | ||
|
||
static inline void set_cpu_mask(pid_t pid, cpu_set_t *mask) { | ||
if (sched_setaffinity(pid, sizeof(cpu_set_t), mask) | ||
== -1) { | ||
app_panic("Set cpu affinity failed.\n"); | ||
} | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
/* argv[1] is cpu number */ | ||
uint32_t active_cpu = atoi(argv[1]); | ||
cpu_set_t cpu_mask; | ||
|
||
get_cpu_mask(0, &cpu_mask); | ||
print_cpu_mask(cpu_mask); | ||
|
||
CPU_ZERO(&cpu_mask); | ||
CPU_SET(active_cpu, &cpu_mask); | ||
set_cpu_mask(0, &cpu_mask); | ||
|
||
get_cpu_mask(0, &cpu_mask); | ||
print_cpu_mask(cpu_mask); | ||
|
||
while(1); | ||
return 0; | ||
} |
Oops, something went wrong.