歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

port from solaris to linux for c

在intel的linux的小端機上進行大端編譯1 sigwait() requires one more parameter in Linux.函數參數不同

dbftam_signal= sigwait(&sigset);--- solaris

retval = sigwait(&sigset,&dbftam_signal); ---linux

2 signal 變成sigaction

(void) signal( SIGALRM, ITsigalrm ); --solaris

(void)sigaction(SIGALRM, &sigcntl, (struct sigaction*)NULL);----linux

3 strlcpy()is not defined in Linux. (linux solaris 函數名不同)#ifdef APXAPLINUX

#define strlcpy(dst, src, dstsize) strncpy(dst, src, dstsize)

#endif

4 cftime()is not defined in Linux. 函數名不同#if !defined(APXAPLINUX)

cftime(timeBuf,"%D %T", (const time_t *) ×tamp);

#else /* defined(APXAPLINUX) */

strftime(timeBuf,sizeof(timeBuf), "%D %T", localtime((const time_t *)&DBinuse[dbid].timestamp));

#endif

5 #ifdefAPXAPLINUX 頭文件不同

#include<strstream>

#else

#include<strstream.h>

#endif

6 帶地址的變量 (變成 小端地址定義變量)

• msgrcvreturn only the 4th parameter matches. Need change

• unsignedlong all; /* References the whole word. */

• to

• unsignedlong all; __attribute__((littleendian));

int request __attribute__((littleendian));

scanf("%d",&request);定義小端變量。因為c庫是小端編譯的。小端存(放入內存),小端讀。c庫是 小端編譯。返回值是小端存儲。

notes: int ret_code;

ret_code = SUCCESS;

fprintf(STDERR, "ERROR: send_reorig: send message to ap fail %d\n", ret_code);

fprintf也是c庫函數,但是它的入參,可以大端,可以小端。不用轉7 字節對齊

longlong in Linux• Needto add more padding fields for Linux.

Codepattern• typedefstruct t_size{

• long f1;

• long long f2;

• }T_SIZE;

Iccbuild results:• Noerror or warning detected by icc

Runningresult:• Solaris:sizeof(T_SIZE) = 16

• Linux:sizeof(T_SIZE) = 12

Solution:• Addpadding fields

• typedefstruct t_size{

• long f1;

• #ifdefAPXAPLINUX

• Long padding;

• #endif

• long long f2;

• }T_SIZE;

8 將int變成socklen_t

#ifndef APXAPLINUX

extern int errno;-----------solaris沒有 這個global變量,linux有

#endif

9 4.1.6 solaris 沒有(__sighandler_t)

#ifdef APXAPLINUX

signal(UXSIG_DEBUG,(__sighandler_t)UXtog_trap_signal);

#else

signal(UXSIG_DEBUG,UXtog_trap_signal);

#endif

Notes: __sighandler_t is notaccepted by sparc build.

11 last parameter in pthread_create 參數不匹配

rc= pthread_create(&id, &attr, (OURTYPE) _UX_thread_entry, (void *) p);

12

// intitalize to hold given restrictions

tcpaddr.sin_addr.s_addr = htonl(INADDR_ANY);

tcpaddr.sin_port = htons(port);

// make bindcall

if ( (retVal= bind(m_scktNum, reinterpret_cast<struct sockaddr *>(&tcpaddr), sizeof(tcpaddr) )) < 0 ) {

ELOG((char*)"CISipSckt::bindToPort(). bind() call failed. Error: %d, %s, port: %d \n", errno, ( strerror(errno) ),port);

}

bind等網絡函數是大端函數,要的就是大端的變量,所以用htonl。如果是大端,這個函數不轉。如果是小端,這個函數會轉.

//理解為rcvaddr是大端編譯的變量

struct sockaddr_in rcvaddr;

//recvfrom是網絡函數,用大端編譯變量

pktLen =recvfrom(dhcpScktNum, dhcpMsgBufPtr, bytesToRead, 0, (struct sockaddr*)&rcvaddr, &rcv_addrlen);

13 : //這個和extern道理一樣,告訴編譯器,入參要變成小端。因為是void *p ,不能確定是類型.如果是char *p;則無所謂.

#ifdef LINUX

#pragma byte_order(push,littleendian)

#endif

void *

createClientThMain(void *arg)

#ifdef LINUX

#pragma byte_order(pop)

#endif

同理,非char*

char* convertDecimalToString(short i);

14 test 大小端的程序

union裡的數據都是從同一個地址的低端開始放置,而長度大於1字節的類型需要考慮大小端(只有char不用考慮大小端)

union

{

charstr;

intdata;

};

data=0x01020304;

if(str==0x01)

{

cout<< "the meachine is big-endian"<<endl;

}

elseif(str==0x04){

cout<<"the meachine is little endia"<<endl;

}

Copyright © Linux教程網 All Rights Reserved