os.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /* Copyright (c) 1993-2002
  2. * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
  3. * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
  4. * Copyright (c) 1987 Oliver Laumann
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program (see the file COPYING); if not, write to the
  18. * Free Software Foundation, Inc.,
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. *
  21. ****************************************************************
  22. * $Id$ FAU
  23. */
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <sys/param.h>
  27. /* In strict ANSI mode, HP-UX machines define __hpux but not hpux */
  28. #if defined(__hpux) && !defined(hpux)
  29. # define hpux
  30. #endif
  31. #if defined(__bsdi__) || defined(__386BSD__) || defined(_CX_UX) || defined(hpux) || defined(_IBMR2) || defined(linux)
  32. # include <signal.h>
  33. #endif /* __bsdi__ || __386BSD__ || _CX_UX || hpux || _IBMR2 || linux */
  34. #ifdef ISC
  35. # ifdef ENAMETOOLONG
  36. # undef ENAMETOOLONG
  37. # endif
  38. # ifdef ENOTEMPTY
  39. # undef ENOTEMPTY
  40. # endif
  41. # include <sys/bsdtypes.h>
  42. # include <net/errno.h>
  43. #endif
  44. #ifdef sun
  45. # define getpgrp __getpgrp
  46. # define exit __exit
  47. #endif
  48. #ifdef POSIX
  49. # include <unistd.h>
  50. # if defined(__STDC__)
  51. # include <stdlib.h>
  52. # endif /* __STDC__ */
  53. #endif /* POSIX */
  54. #ifdef sun
  55. # undef getpgrp
  56. # undef exit
  57. #endif /* sun */
  58. #ifndef linux /* all done in <errno.h> */
  59. extern int errno;
  60. #endif /* linux */
  61. #ifndef HAVE_STRERROR
  62. /* No macros, please */
  63. #undef strerror
  64. #endif
  65. #if !defined(SYSV) && !defined(linux)
  66. # ifdef NEWSOS
  67. # define strlen ___strlen___
  68. # include <strings.h>
  69. # undef strlen
  70. # else /* NEWSOS */
  71. # include <strings.h>
  72. # endif /* NEWSOS */
  73. #else /* SYSV */
  74. # if defined(SVR4) || defined(NEWSOS)
  75. # define strlen ___strlen___
  76. # include <string.h>
  77. # undef strlen
  78. # if !defined(NEWSOS) && !defined(__hpux)
  79. extern size_t strlen(const char *);
  80. # endif
  81. # else /* SVR4 */
  82. # include <string.h>
  83. # endif /* SVR4 */
  84. #endif /* SYSV */
  85. #ifdef USEVARARGS
  86. # if defined(__STDC__)
  87. # include <stdarg.h>
  88. # define VA_LIST(var) va_list var;
  89. # define VA_DOTS ...
  90. # define VA_DECL
  91. # define VA_START(ap, fmt) va_start(ap, fmt)
  92. # define VA_ARGS(ap) ap
  93. # define VA_END(ap) va_end(ap)
  94. # else
  95. # include <varargs.h>
  96. # define VA_LIST(var) va_list var;
  97. # define VA_DOTS va_alist
  98. # define VA_DECL va_dcl
  99. # define VA_START(ap, fmt) va_start(ap)
  100. # define VA_ARGS(ap) ap
  101. # define VA_END(ap) va_end(ap)
  102. # endif
  103. #else
  104. # define VA_LIST(var)
  105. # define VA_DOTS p1, p2, p3, p4, p5, p6
  106. # define VA_DECL unsigned long VA_DOTS;
  107. # define VA_START(ap, fmt)
  108. # define VA_ARGS(ap) VA_DOTS
  109. # define VA_END(ap)
  110. # undef vsnprintf
  111. # define vsnprintf xsnprintf
  112. #endif
  113. #if !defined(sun) && !defined(B43) && !defined(ISC) && !defined(pyr) && !defined(_CX_UX)
  114. # include <time.h>
  115. #endif
  116. #include <sys/time.h>
  117. #ifdef M_UNIX /* SCO */
  118. # include <sys/stream.h>
  119. # include <sys/ptem.h>
  120. # define ftruncate(fd, s) chsize(fd, s)
  121. #endif
  122. #ifdef SYSV
  123. # define index strchr
  124. # define rindex strrchr
  125. # define bzero(poi,len) memset(poi,0,len)
  126. # define bcmp memcmp
  127. # define killpg(pgrp,sig) kill( -(pgrp), sig)
  128. #endif
  129. #ifndef HAVE_GETCWD
  130. # define getcwd(b,l) getwd(b)
  131. #endif
  132. #ifndef USEBCOPY
  133. # ifdef USEMEMMOVE
  134. # define bcopy(s,d,len) memmove(d,s,len)
  135. # else
  136. # ifdef USEMEMCPY
  137. # define bcopy(s,d,len) memcpy(d,s,len)
  138. # else
  139. # define NEED_OWN_BCOPY
  140. # define bcopy xbcopy
  141. # endif
  142. # endif
  143. #endif
  144. #ifdef hpux
  145. # define setreuid(ruid, euid) setresuid(ruid, euid, -1)
  146. # define setregid(rgid, egid) setresgid(rgid, egid, -1)
  147. #endif
  148. #if defined(HAVE_SETEUID) || defined(HAVE_SETREUID)
  149. # define USE_SETEUID
  150. #endif
  151. #if !defined(HAVE__EXIT) && !defined(_exit)
  152. #define _exit(x) exit(x)
  153. #endif
  154. #ifndef HAVE_UTIMES
  155. # define utimes utime
  156. #endif
  157. #ifdef BUILTIN_TELNET
  158. # include <netinet/in.h>
  159. # include <arpa/inet.h>
  160. #endif
  161. #if defined(USE_LOCALE) && (!defined(HAVE_SETLOCALE) || !defined(HAVE_STRFTIME))
  162. # undef USE_LOCALE
  163. #endif
  164. /*****************************************************************
  165. * terminal handling
  166. */
  167. #if defined (POSIX) || defined (__FreeBSD__)
  168. # include <termios.h>
  169. # ifdef hpux
  170. # include <bsdtty.h>
  171. # endif /* hpux */
  172. # ifdef NCCS
  173. # define MAXCC NCCS
  174. # else
  175. # define MAXCC 256
  176. # endif
  177. #else /* POSIX */
  178. # ifdef TERMIO
  179. # include <termio.h>
  180. # ifdef NCC
  181. # define MAXCC NCC
  182. # else
  183. # define MAXCC 256
  184. # endif
  185. # ifdef CYTERMIO
  186. # include <cytermio.h>
  187. # endif
  188. # else /* TERMIO */
  189. # include <sgtty.h>
  190. # endif /* TERMIO */
  191. #endif /* POSIX */
  192. #ifndef VDISABLE
  193. # ifdef _POSIX_VDISABLE
  194. # define VDISABLE _POSIX_VDISABLE
  195. # else
  196. # define VDISABLE 0377
  197. # endif /* _POSIX_VDISABLE */
  198. #endif /* !VDISABLE */
  199. /* on sgi, regardless of the stream head's read mode (RNORM/RMSGN/RMSGD)
  200. * TIOCPKT mode causes data loss if our buffer is too small (IOSIZE)
  201. * to hold the whole packet at first read().
  202. * (Marc Boucher)
  203. *
  204. * matthew green:
  205. * TIOCPKT is broken on dgux 5.4.1 generic AViiON mc88100
  206. *
  207. * Joe Traister: On AIX4, programs like irc won't work if screen
  208. * uses TIOCPKT (select fails to return on pty read).
  209. */
  210. #if defined(sgi) || defined(DGUX) || defined(_IBMR2)
  211. # undef TIOCPKT
  212. #endif
  213. /* linux ncurses is broken, we have to use our own tputs */
  214. #if defined(linux) && defined(TERMINFO)
  215. # define tputs xtputs
  216. #endif
  217. /* Alexandre Oliva: SVR4 style ptys don't work with osf */
  218. #ifdef __osf__
  219. # undef HAVE_SVR4_PTYS
  220. #endif
  221. /*****************************************************************
  222. * utmp handling
  223. */
  224. #ifdef GETUTENT
  225. typedef char *slot_t;
  226. #else
  227. typedef int slot_t;
  228. #endif
  229. #if defined(UTMPOK) || defined(BUGGYGETLOGIN)
  230. # if defined(SVR4) && !defined(DGUX) && !defined(__hpux) && !defined(linux)
  231. # include <utmpx.h>
  232. # define UTMPFILE UTMPX_FILE
  233. # define utmp utmpx
  234. # define getutent getutxent
  235. # define getutid getutxid
  236. # define getutline getutxline
  237. # define pututline pututxline
  238. # define setutent setutxent
  239. # define endutent endutxent
  240. # define ut_time ut_xtime
  241. # else /* SVR4 */
  242. # include <utmp.h>
  243. # endif /* SVR4 */
  244. # ifdef apollo
  245. /*
  246. * We don't have GETUTENT, so we dig into utmp ourselves.
  247. * But we save the permanent filedescriptor and
  248. * open utmp just when we need to.
  249. * This code supports an unsorted utmp. jw.
  250. */
  251. # define UTNOKEEP
  252. # endif /* apollo */
  253. # ifndef UTMPFILE
  254. # ifdef UTMP_FILE
  255. # define UTMPFILE UTMP_FILE
  256. # else
  257. # ifdef _PATH_UTMP
  258. # define UTMPFILE _PATH_UTMP
  259. # else
  260. # define UTMPFILE "/etc/utmp"
  261. # endif /* _PATH_UTMP */
  262. # endif
  263. # endif
  264. #endif /* UTMPOK || BUGGYGETLOGIN */
  265. #if !defined(UTMPOK) && defined(USRLIMIT)
  266. # undef USRLIMIT
  267. #endif
  268. #ifdef LOGOUTOK
  269. # ifndef LOGINDEFAULT
  270. # define LOGINDEFAULT 0
  271. # endif
  272. #else
  273. # ifdef LOGINDEFAULT
  274. # undef LOGINDEFAULT
  275. # endif
  276. # define LOGINDEFAULT 1
  277. #endif
  278. /*****************************************************************
  279. * file stuff
  280. */
  281. #ifndef F_OK
  282. #define F_OK 0
  283. #endif
  284. #ifndef X_OK
  285. #define X_OK 1
  286. #endif
  287. #ifndef W_OK
  288. #define W_OK 2
  289. #endif
  290. #ifndef R_OK
  291. #define R_OK 4
  292. #endif
  293. #ifndef S_IFIFO
  294. #define S_IFIFO 0010000
  295. #endif
  296. #ifndef S_IREAD
  297. #define S_IREAD 0000400
  298. #endif
  299. #ifndef S_IWRITE
  300. #define S_IWRITE 0000200
  301. #endif
  302. #ifndef S_IEXEC
  303. #define S_IEXEC 0000100
  304. #endif
  305. #if defined(S_IFIFO) && defined(S_IFMT) && !defined(S_ISFIFO)
  306. #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
  307. #endif
  308. #if defined(S_IFSOCK) && defined(S_IFMT) && !defined(S_ISSOCK)
  309. #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
  310. #endif
  311. #if defined(S_IFCHR) && defined(S_IFMT) && !defined(S_ISCHR)
  312. #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
  313. #endif
  314. #if defined(S_IFDIR) && defined(S_IFMT) && !defined(S_ISDIR)
  315. #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
  316. #endif
  317. #if defined(S_IFLNK) && defined(S_IFMT) && !defined(S_ISLNK)
  318. #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
  319. #endif
  320. /*
  321. * SunOS 4.1.3: `man 2V open' has only one line that mentions O_NOBLOCK:
  322. *
  323. * O_NONBLOCK Same as O_NDELAY above.
  324. *
  325. * on the very same SunOS 4.1.3, I traced the open system call and found
  326. * that an open("/dev/ttyy08", O_RDWR|O_NONBLOCK|O_NOCTTY) was blocked,
  327. * whereas open("/dev/ttyy08", O_RDWR|O_NDELAY |O_NOCTTY) went through.
  328. *
  329. * For this simple reason I now favour O_NDELAY. jw. 4.5.95
  330. */
  331. #if defined(sun) && !defined(SVR4)
  332. # undef O_NONBLOCK
  333. #endif
  334. #if !defined(O_NONBLOCK) && defined(O_NDELAY)
  335. # define O_NONBLOCK O_NDELAY
  336. #endif
  337. #if !defined(FNBLOCK) && defined(FNONBLOCK)
  338. # define FNBLOCK FNONBLOCK
  339. #endif
  340. #if !defined(FNBLOCK) && defined(FNDELAY)
  341. # define FNBLOCK FNDELAY
  342. #endif
  343. #if !defined(FNBLOCK) && defined(O_NONBLOCK)
  344. # define FNBLOCK O_NONBLOCK
  345. #endif
  346. #ifndef POSIX
  347. #undef mkfifo
  348. #define mkfifo(n,m) mknod(n,S_IFIFO|(m),0)
  349. #endif
  350. #if !defined(HAVE_LSTAT) && !defined(lstat)
  351. # define lstat stat
  352. #endif
  353. /*****************************************************************
  354. * signal handling
  355. */
  356. #ifdef SIGVOID
  357. # define SIGRETURN
  358. # define sigret_t void
  359. #else
  360. # define SIGRETURN return 0;
  361. # define sigret_t int
  362. #endif
  363. /* Geeeee, reverse it? */
  364. #if defined(SVR4) || (defined(SYSV) && defined(ISC)) || defined(_AIX) || defined(linux) || defined(ultrix) || defined(__386BSD__) || defined(__bsdi__) || defined(POSIX) || defined(NeXT)
  365. # define SIGHASARG
  366. #endif
  367. #ifdef SIGHASARG
  368. # define SIGPROTOARG (int)
  369. # define SIGDEFARG (sigsig) int sigsig;
  370. # define SIGARG 0
  371. #else
  372. # define SIGPROTOARG (void)
  373. # define SIGDEFARG ()
  374. # define SIGARG
  375. #endif
  376. #ifndef SIGCHLD
  377. #define SIGCHLD SIGCLD
  378. #endif
  379. #if defined(POSIX) || defined(hpux)
  380. # define signal xsignal
  381. #else
  382. # ifdef USESIGSET
  383. # define signal sigset
  384. # endif /* USESIGSET */
  385. #endif
  386. /* used in screen.c and attacher.c */
  387. #ifndef NSIG /* kbeal needs these w/o SYSV */
  388. # define NSIG 32
  389. #endif /* !NSIG */
  390. /*****************************************************************
  391. * Wait stuff
  392. */
  393. #if (!defined(sysV68) && !defined(M_XENIX)) || defined(NeXT) || defined(M_UNIX)
  394. # include <sys/wait.h>
  395. #endif
  396. #ifndef WTERMSIG
  397. # ifndef BSDWAIT /* if wait is NOT a union: */
  398. # define WTERMSIG(status) (status & 0177)
  399. # else
  400. # define WTERMSIG(status) status.w_T.w_Termsig
  401. # endif
  402. #endif
  403. #ifndef WSTOPSIG
  404. # ifndef BSDWAIT /* if wait is NOT a union: */
  405. # define WSTOPSIG(status) ((status >> 8) & 0377)
  406. # else
  407. # define WSTOPSIG(status) status.w_S.w_Stopsig
  408. # endif
  409. #endif
  410. /* NET-2 uses WCOREDUMP */
  411. #if defined(WCOREDUMP) && !defined(WIFCORESIG)
  412. # define WIFCORESIG(status) WCOREDUMP(status)
  413. #endif
  414. #ifndef WIFCORESIG
  415. # ifndef BSDWAIT /* if wait is NOT a union: */
  416. # define WIFCORESIG(status) (status & 0200)
  417. # else
  418. # define WIFCORESIG(status) status.w_T.w_Coredump
  419. # endif
  420. #endif
  421. #ifndef WEXITSTATUS
  422. # ifndef BSDWAIT /* if wait is NOT a union: */
  423. # define WEXITSTATUS(status) ((status >> 8) & 0377)
  424. # else
  425. # define WEXITSTATUS(status) status.w_T.w_Retcode
  426. # endif
  427. #endif
  428. /*****************************************************************
  429. * select stuff
  430. */
  431. #if defined(M_XENIX) || defined(M_UNIX) || defined(_SEQUENT_)
  432. #include <sys/select.h> /* for timeval + FD... */
  433. #endif
  434. /*
  435. * SunOS 3.5 - Tom Schmidt - Micron Semiconductor, Inc - 27-Jul-93
  436. * tschmidt@vax.micron.com
  437. */
  438. #ifndef FD_SET
  439. # ifndef SUNOS3
  440. typedef struct fd_set { int fds_bits[1]; } fd_set;
  441. # endif
  442. # define FD_ZERO(fd) ((fd)->fds_bits[0] = 0)
  443. # define FD_SET(b, fd) ((fd)->fds_bits[0] |= 1 << (b))
  444. # define FD_ISSET(b, fd) ((fd)->fds_bits[0] & 1 << (b))
  445. # define FD_SETSIZE 32
  446. #endif
  447. /*****************************************************************
  448. * user defineable stuff
  449. */
  450. #ifndef TERMCAP_BUFSIZE
  451. # define TERMCAP_BUFSIZE 2048
  452. #endif
  453. #ifndef MAXPATHLEN
  454. # define MAXPATHLEN 1024
  455. #endif
  456. /*
  457. * you may try to vary this value. Use low values if your (VMS) system
  458. * tends to choke when pasting. Use high values if you want to test
  459. * how many characters your pty's can buffer.
  460. */
  461. #define IOSIZE 4096