| 1 |
# Copyright 1999-2012 Gentoo Foundation
|
| 2 |
# Distributed under the terms of the GNU General Public License v2
|
| 3 |
# $Header: /var/cvsroot/gentoo-x86/mail-mta/netqmail/netqmail-1.05-r8.ebuild,v 1.14 2012/06/04 21:02:40 zmedico Exp $
|
| 4 |
|
| 5 |
inherit eutils toolchain-funcs fixheadtails flag-o-matic user
|
| 6 |
|
| 7 |
TLS_AUTH_PATCH=20070417
|
| 8 |
QMAIL_SPP_PATCH=0.42
|
| 9 |
|
| 10 |
DESCRIPTION="qmail -- a secure, reliable, efficient, simple message transfer agent"
|
| 11 |
HOMEPAGE="
|
| 12 |
http://netqmail.org/
|
| 13 |
http://cr.yp.to/qmail.html
|
| 14 |
http://qmail.org/
|
| 15 |
"
|
| 16 |
SRC_URI="
|
| 17 |
mirror://qmail/netqmail-${PV}.tar.gz
|
| 18 |
!vanilla? (
|
| 19 |
ssl? ( http://shupp.org/patches/netqmail-${PV}-tls-smtpauth-${TLS_AUTH_PATCH}.patch )
|
| 20 |
highvolume? ( mirror://qmail/big-todo.103.patch )
|
| 21 |
qmail-spp? ( mirror://sourceforge/qmail-spp/qmail-spp-${QMAIL_SPP_PATCH}.tar.gz )
|
| 22 |
)
|
| 23 |
"
|
| 24 |
|
| 25 |
LICENSE="public-domain"
|
| 26 |
SLOT="0"
|
| 27 |
KEYWORDS="alpha amd64 arm hppa ia64 m68k ~mips ppc ppc64 s390 sh sparc x86"
|
| 28 |
IUSE="gencertdaily highvolume noauthcram qmail-spp ssl vanilla"
|
| 29 |
RESTRICT="test"
|
| 30 |
|
| 31 |
DEPEND="
|
| 32 |
!mail-mta/qmail
|
| 33 |
sys-apps/groff
|
| 34 |
net-mail/queue-repair
|
| 35 |
ssl? ( dev-libs/openssl )
|
| 36 |
"
|
| 37 |
RDEPEND="
|
| 38 |
!mail-mta/courier
|
| 39 |
!mail-mta/esmtp
|
| 40 |
!mail-mta/exim
|
| 41 |
!mail-mta/mini-qmail
|
| 42 |
!mail-mta/msmtp
|
| 43 |
!mail-mta/nullmailer
|
| 44 |
!mail-mta/postfix
|
| 45 |
!mail-mta/qmail-ldap
|
| 46 |
!mail-mta/sendmail
|
| 47 |
!mail-mta/ssmtp
|
| 48 |
sys-apps/ucspi-tcp
|
| 49 |
virtual/daemontools
|
| 50 |
net-mail/dot-forward
|
| 51 |
!noauthcram? (
|
| 52 |
|| ( >=net-mail/checkpassword-0.90 >=net-mail/checkpassword-pam-0.99 )
|
| 53 |
>=net-mail/cmd5checkpw-0.30
|
| 54 |
)
|
| 55 |
${DEPEND}
|
| 56 |
"
|
| 57 |
|
| 58 |
# Important: QMAIL_CONF_SPLIT should always be a prime number!
|
| 59 |
MY_CONF_SPLIT="${QMAIL_CONF_SPLIT:-23}"
|
| 60 |
|
| 61 |
MY_S="${S}/netqmail-${PV}"
|
| 62 |
QMAIL_SPP_S="${WORKDIR}/qmail-spp-${QMAIL_SPP_PATCH}"
|
| 63 |
TCPRULES_DIR=/etc/tcprules.d
|
| 64 |
|
| 65 |
if use gencertdaily; then
|
| 66 |
CRON_FOLDER=cron.daily
|
| 67 |
else
|
| 68 |
CRON_FOLDER=cron.hourly
|
| 69 |
fi
|
| 70 |
|
| 71 |
src_unpack() {
|
| 72 |
unpack netqmail-${PV}.tar.gz
|
| 73 |
use qmail-spp && \
|
| 74 |
unpack qmail-spp-${QMAIL_SPP_PATCH}.tar.gz
|
| 75 |
|
| 76 |
cd "${S}"
|
| 77 |
|
| 78 |
./collate.sh || die "patching failed"
|
| 79 |
|
| 80 |
cd "${MY_S}"
|
| 81 |
|
| 82 |
if ! use vanilla; then
|
| 83 |
use ssl && epatch ${DISTDIR}/netqmail-${PV}-tls-smtpauth-${TLS_AUTH_PATCH}.patch
|
| 84 |
|
| 85 |
if use highvolume; then
|
| 86 |
epatch ${DISTDIR}/big-todo.103.patch
|
| 87 |
fi
|
| 88 |
|
| 89 |
if use qmail-spp; then
|
| 90 |
if use ssl; then
|
| 91 |
epatch ${QMAIL_SPP_S}/qmail-spp-smtpauth-tls-20060105.diff
|
| 92 |
else
|
| 93 |
epatch ${QMAIL_SPP_S}/netqmail-spp.diff
|
| 94 |
fi
|
| 95 |
fi
|
| 96 |
fi
|
| 97 |
|
| 98 |
if [[ -n "${QMAIL_PATCH_DIR}" && -d "${QMAIL_PATCH_DIR}" ]]
|
| 99 |
then
|
| 100 |
echo
|
| 101 |
ewarn "You enabled custom patches from ${QMAIL_PATCH_DIR}."
|
| 102 |
ewarn "Be warned that you won't get any support when using "
|
| 103 |
ewarn "this feature. You're on your own from now!"
|
| 104 |
ebeep
|
| 105 |
epatch "${QMAIL_PATCH_DIR}/"*
|
| 106 |
echo
|
| 107 |
fi
|
| 108 |
|
| 109 |
is_prime ${MY_CONF_SPLIT} || die 'QMAIL_CONF_SPLIT is not a prime number.'
|
| 110 |
einfo "Using conf-split value of ${MY_CONF_SPLIT}."
|
| 111 |
|
| 112 |
# Fix bug #33818 but for netqmail (Bug 137015)
|
| 113 |
if use noauthcram; then
|
| 114 |
einfo "Disabling CRAM_MD5 support"
|
| 115 |
sed -e 's,^#define CRAM_MD5$,/*&*/,' -i ${MY_S}/qmail-smtpd.c
|
| 116 |
else
|
| 117 |
einfo "Enabled CRAM_MD5 support"
|
| 118 |
fi
|
| 119 |
|
| 120 |
ht_fix_file Makefile*
|
| 121 |
|
| 122 |
# -DTLS is now set by the SSL/TLS patch
|
| 123 |
#use ssl && append-flags -DTLS
|
| 124 |
|
| 125 |
# The following commands patch the conf-{cc,ld} files to use the user's
|
| 126 |
# specified CFLAGS and LDFLAGS. These rather complex commands are needed
|
| 127 |
# because a user supplied patch might apply changes to these files, too.
|
| 128 |
# Fixes Bug #165981.
|
| 129 |
echo "$(head -n 1 "${MY_S}/conf-cc" | sed -e "s#^g\?cc\s\+\(-O2\)\?#$(tc-getCC) #")" \
|
| 130 |
"${CFLAGS}" > "${MY_S}/conf-cc.tmp" &&
|
| 131 |
mv "${MY_S}/conf-cc.tmp" "${MY_S}/conf-cc" || die 'Patching conf-cc failed.'
|
| 132 |
|
| 133 |
echo "$(head -n 1 "${MY_S}/conf-ld" | sed -e "s#^g\?cc\s\+\(-s\)\?#$(tc-getCC) #")" \
|
| 134 |
"${LDLAGS}" > "${MY_S}/conf-ld.tmp" &&
|
| 135 |
mv "${MY_S}/conf-ld.tmp" "${MY_S}/conf-ld" || die 'Patching conf-ld failed.'
|
| 136 |
|
| 137 |
echo -n "${MY_CONF_SPLIT}" > "${MY_S}/conf-split"
|
| 138 |
}
|
| 139 |
|
| 140 |
src_compile() {
|
| 141 |
cd "${MY_S}"
|
| 142 |
emake it man || die "make failed"
|
| 143 |
}
|
| 144 |
|
| 145 |
src_install() {
|
| 146 |
cd "${MY_S}"
|
| 147 |
|
| 148 |
einfo "Setting up directory hierarchy ..."
|
| 149 |
|
| 150 |
diropts -m 755 -o root -g qmail
|
| 151 |
dodir /var/qmail/{,bin,boot,control}
|
| 152 |
|
| 153 |
keepdir /var/qmail/users
|
| 154 |
|
| 155 |
diropts -m 755 -o alias -g qmail
|
| 156 |
dodir /var/qmail/alias
|
| 157 |
|
| 158 |
einfo "Installing the qmail software ..."
|
| 159 |
|
| 160 |
insopts -o root -g qmail -m 755
|
| 161 |
insinto /var/qmail/boot
|
| 162 |
doins home home+df proc proc+df binm1 binm1+df binm2 \
|
| 163 |
binm2+df binm3 binm3+df
|
| 164 |
|
| 165 |
insinto /var/qmail/bin
|
| 166 |
|
| 167 |
insopts -o qmailq -g qmail -m 4711
|
| 168 |
doins qmail-queue
|
| 169 |
|
| 170 |
insopts -o root -g qmail -m 700
|
| 171 |
doins qmail-{lspawn,start,newu,newmrh}
|
| 172 |
|
| 173 |
insopts -o root -g qmail -m 711
|
| 174 |
doins qmail-{getpw,local,remote,rspawn,clean,send,pw2u} splogger
|
| 175 |
|
| 176 |
insopts -o root -g qmail -m 755
|
| 177 |
doins bouncesaying condredirect config-fast datemail elq \
|
| 178 |
except forward maildir2mbox maildirmake maildirwatch \
|
| 179 |
mailsubj pinq predate preline qail qbiff \
|
| 180 |
qmail-{inject,pop3d,popup,qmqpc,qmqpd,qmtpd,qread} \
|
| 181 |
qmail-{qstat,showctl,smtpd,tcpok,tcpto} \
|
| 182 |
qreceipt qsmhook sendmail tcp-env
|
| 183 |
|
| 184 |
einfo "Installing manpages"
|
| 185 |
into /usr
|
| 186 |
doman *.[1-8]
|
| 187 |
|
| 188 |
dodoc BLURB* CHANGES FAQ INSTALL* PIC* README* REMOVE* SECURITY \
|
| 189 |
SENDMAIL SYSDEPS TARGETS TEST* THANKS* THOUGHTS TODO* \
|
| 190 |
UPGRADE VERSION*
|
| 191 |
|
| 192 |
# use the correct maildirmake
|
| 193 |
# the courier-imap one has some extensions that are nicer
|
| 194 |
[[ -e /usr/bin/maildirmake ]] && \
|
| 195 |
MAILDIRMAKE="/usr/bin/maildirmake" || \
|
| 196 |
MAILDIRMAKE="${D}/var/qmail/bin/maildirmake"
|
| 197 |
|
| 198 |
einfo "Adding env.d entry for qmail"
|
| 199 |
insopts -m 644
|
| 200 |
doenvd ${FILESDIR}/99qmail
|
| 201 |
|
| 202 |
einfo "Creating sendmail replacement ..."
|
| 203 |
diropts -m 755
|
| 204 |
dodir /usr/sbin /usr/lib
|
| 205 |
|
| 206 |
dosym /var/qmail/bin/sendmail /usr/sbin/sendmail
|
| 207 |
dosym /var/qmail/bin/sendmail /usr/lib/sendmail
|
| 208 |
|
| 209 |
einfo "Setting up the default aliases ..."
|
| 210 |
diropts -m 700 -o alias -g qmail
|
| 211 |
${MAILDIRMAKE} ${D}/var/qmail/alias/.maildir
|
| 212 |
keepdir /var/qmail/alias/.maildir/{cur,new,tmp}
|
| 213 |
|
| 214 |
for i in /var/qmail/alias/.qmail-{mailer-daemon,postmaster,root}
|
| 215 |
do
|
| 216 |
if [[ ! -f ${i} ]]; then
|
| 217 |
touch ${D}${i}
|
| 218 |
fowners alias:qmail ${i}
|
| 219 |
fi
|
| 220 |
done
|
| 221 |
|
| 222 |
einfo "Setting up maildirs by default in the account skeleton ..."
|
| 223 |
diropts -m 755 -o root -g root
|
| 224 |
insinto /etc/skel
|
| 225 |
newins ${FILESDIR}/dot-qmail .qmail.sample
|
| 226 |
fperms 644 /etc/skel/.qmail.sample
|
| 227 |
${MAILDIRMAKE} ${D}/etc/skel/.maildir
|
| 228 |
keepdir /etc/skel/.maildir/{cur,new,tmp}
|
| 229 |
|
| 230 |
einfo "Setting up all services (send, smtp, qmtp, qmqp, pop3) ..."
|
| 231 |
insopts -o root -g root -m 755
|
| 232 |
diropts -m 755 -o root -g root
|
| 233 |
dodir /var/qmail/supervise
|
| 234 |
|
| 235 |
for i in send smtpd qmtpd qmqpd pop3d; do
|
| 236 |
insopts -o root -g root -m 755
|
| 237 |
diropts -m 755 -o root -g root
|
| 238 |
dodir /var/qmail/supervise/qmail-${i}{,/log}
|
| 239 |
fperms +t /var/qmail/supervise/qmail-${i}{,/log}
|
| 240 |
insinto /var/qmail/supervise/qmail-${i}
|
| 241 |
newins ${FILESDIR}/run-qmail-${i} run
|
| 242 |
insinto /var/qmail/supervise/qmail-${i}/log
|
| 243 |
newins ${FILESDIR}/run-qmail-${i}-log run
|
| 244 |
diropts -m 755 -o qmaill
|
| 245 |
keepdir /var/log/qmail/qmail-${i}
|
| 246 |
done
|
| 247 |
|
| 248 |
dodir ${TCPRULES_DIR}
|
| 249 |
insinto ${TCPRULES_DIR}
|
| 250 |
newins ${FILESDIR}/tcprules.d-Makefile.qmail Makefile.qmail
|
| 251 |
for i in smtp qmtp qmqp pop3; do
|
| 252 |
newins ${FILESDIR}/tcp.${i}.sample tcp.qmail-${i}
|
| 253 |
done
|
| 254 |
|
| 255 |
einfo "Installing some stock configuration files"
|
| 256 |
insinto /var/qmail/control
|
| 257 |
insopts -o root -g root -m 644
|
| 258 |
doins ${FILESDIR}/conf-{common,send,qmtpd,qmqpd,pop3d}
|
| 259 |
newins ${FILESDIR}/conf-smtpd conf-smtpd
|
| 260 |
newins ${FILESDIR}/dot-qmail defaultdelivery
|
| 261 |
use ssl && \
|
| 262 |
doins ${FILESDIR}/servercert.cnf
|
| 263 |
|
| 264 |
einfo "Configuration sanity checker and launcher"
|
| 265 |
into /var/qmail
|
| 266 |
insopts -o root -g root -m 644
|
| 267 |
dobin ${FILESDIR}/qmail-config-system
|
| 268 |
|
| 269 |
if use qmail-spp; then
|
| 270 |
einfo "Installing files for qmail-spp"
|
| 271 |
insinto /var/qmail/control/
|
| 272 |
doins ${QMAIL_SPP_S}/samples/smtpplugins
|
| 273 |
keepdir /var/qmail/plugins/
|
| 274 |
fi
|
| 275 |
|
| 276 |
if use ssl; then
|
| 277 |
einfo "SSL Certificate creation script"
|
| 278 |
dobin ${FILESDIR}/mkservercert
|
| 279 |
einfo "RSA key generation cronjob"
|
| 280 |
insinto /etc/${CRON_FOLDER}
|
| 281 |
doins ${FILESDIR}/qmail-genrsacert.sh
|
| 282 |
chmod +x ${D}/etc/${CRON_FOLDER}/qmail-genrsacert.sh
|
| 283 |
|
| 284 |
# for some files
|
| 285 |
keepdir /var/qmail/control/tlshosts/
|
| 286 |
fi
|
| 287 |
}
|
| 288 |
|
| 289 |
rootmailfixup() {
|
| 290 |
# so you can check mail as root easily
|
| 291 |
local TMPCMD="ln -sf /var/qmail/alias/.maildir/ ${ROOT}/root/.maildir"
|
| 292 |
if [[ -d "${ROOT}/root/.maildir" && ! -L "${ROOT}/root/.maildir" ]] ; then
|
| 293 |
elog "Previously the qmail ebuilds created /root/.maildir/ but not"
|
| 294 |
elog "every mail was delivered there. If the directory does not"
|
| 295 |
elog "contain any mail, please delete it and run:"
|
| 296 |
elog "${TMPCMD}"
|
| 297 |
else
|
| 298 |
${TMPCMD}
|
| 299 |
fi
|
| 300 |
chown -R alias:qmail ${ROOT}/var/qmail/alias/.maildir 2>/dev/null
|
| 301 |
}
|
| 302 |
|
| 303 |
buildtcprules() {
|
| 304 |
for i in smtp qmtp qmqp pop3; do
|
| 305 |
# please note that we don't check if it exists
|
| 306 |
# as we want it to make the cdb files anyway!
|
| 307 |
f=tcp.qmail-${i}
|
| 308 |
src=${ROOT}${TCPRULES_DIR}/${f}
|
| 309 |
cdb=${ROOT}${TCPRULES_DIR}/${f}.cdb
|
| 310 |
tmp=${ROOT}${TCPRULES_DIR}/.${f}.tmp
|
| 311 |
[[ -e ${src} ]] && tcprules ${cdb} ${tmp} < ${src}
|
| 312 |
done
|
| 313 |
}
|
| 314 |
|
| 315 |
pkg_postinst() {
|
| 316 |
einfo "Setting up the message queue hierarchy ..."
|
| 317 |
/usr/bin/queue-repair.py \
|
| 318 |
--create --split "${MY_CONF_SPLIT}" \
|
| 319 |
$(use highvolume && echo '--bigtodo' || echo '--no-bigtodo') \
|
| 320 |
${ROOT}/var/qmail >/dev/null || \
|
| 321 |
die 'queue-repair failed'
|
| 322 |
|
| 323 |
rootmailfixup
|
| 324 |
buildtcprules
|
| 325 |
|
| 326 |
elog "To setup qmail to run out-of-the-box on your system, run:"
|
| 327 |
elog "emerge --config =${CATEGORY}/${PF}"
|
| 328 |
elog
|
| 329 |
elog "To start qmail at boot you have to add svscan to your startup"
|
| 330 |
elog "and create the following links:"
|
| 331 |
elog "ln -s /var/qmail/supervise/qmail-send /service/qmail-send"
|
| 332 |
elog "ln -s /var/qmail/supervise/qmail-smtpd /service/qmail-smtpd"
|
| 333 |
elog
|
| 334 |
elog "To start the pop3 server as well, create the following link:"
|
| 335 |
elog "ln -s /var/qmail/supervise/qmail-pop3d /service/qmail-pop3d"
|
| 336 |
elog
|
| 337 |
elog "Additionally, the QMTP and QMQP protocols are supported, "
|
| 338 |
elog "and can be started as:"
|
| 339 |
elog "ln -s /var/qmail/supervise/qmail-qmtpd /service/qmail-qmtpd"
|
| 340 |
elog "ln -s /var/qmail/supervise/qmail-qmqpd /service/qmail-qmqpd"
|
| 341 |
elog
|
| 342 |
elog "Additionally, if you wish to run qmail right now, you should "
|
| 343 |
elog "run this before anything else:"
|
| 344 |
elog "source /etc/profile"
|
| 345 |
elog
|
| 346 |
elog "If you are looking for documentation, check those links:"
|
| 347 |
elog "http://www.gentoo.org/doc/en/qmail-howto.xml"
|
| 348 |
elog " -- qmail/vpopmail Virtual Mail Hosting System Guide"
|
| 349 |
elog "http://www.lifewithqmail.com/"
|
| 350 |
elog " -- Life with qmail"
|
| 351 |
}
|
| 352 |
|
| 353 |
pkg_preinst() {
|
| 354 |
mkdir -p ${TCPRULES_DIR}
|
| 355 |
for proto in smtp qmtp qmqp pop3; do
|
| 356 |
for ext in '' .cdb; do
|
| 357 |
old="/etc/tcp.${proto}${ext}"
|
| 358 |
new="${TCPRULES_DIR}/tcp.qmail-${proto}${ext}"
|
| 359 |
fail=0
|
| 360 |
if [[ -f "$old" && ! -f "$new" ]]; then
|
| 361 |
einfo "Moving $old to $new"
|
| 362 |
cp $old $new || fail=1
|
| 363 |
else
|
| 364 |
fail=1
|
| 365 |
fi
|
| 366 |
if [[ "${fail}" = 1 && -f ${old} ]]; then
|
| 367 |
eerror "Error moving $old to $new, be sure to check the"
|
| 368 |
eerror "configuration! You may have already moved the files,"
|
| 369 |
eerror "in which case you can delete $old"
|
| 370 |
fi
|
| 371 |
done
|
| 372 |
done
|
| 373 |
}
|
| 374 |
|
| 375 |
# Candidate for eclass
|
| 376 |
pkg_setup() {
|
| 377 |
# keep in sync with mini-qmail pkg
|
| 378 |
einfo "Creating groups and users"
|
| 379 |
enewgroup qmail 201
|
| 380 |
enewuser alias 200 -1 /var/qmail/alias 200
|
| 381 |
enewuser qmaild 201 -1 /var/qmail 200
|
| 382 |
enewuser qmaill 202 -1 /var/qmail 200
|
| 383 |
enewuser qmailp 203 -1 /var/qmail 200
|
| 384 |
enewuser qmailq 204 -1 /var/qmail 201
|
| 385 |
enewuser qmailr 205 -1 /var/qmail 201
|
| 386 |
enewuser qmails 206 -1 /var/qmail 201
|
| 387 |
}
|
| 388 |
|
| 389 |
pkg_config() {
|
| 390 |
# avoid some weird locale problems
|
| 391 |
export LC_ALL=C
|
| 392 |
|
| 393 |
if [[ ${ROOT} = / ]] ; then
|
| 394 |
if [[ ! -f ${ROOT}var/qmail/control/me ]] ; then
|
| 395 |
export qhost=$(hostname --fqdn)
|
| 396 |
${ROOT}var/qmail/bin/config-fast $qhost
|
| 397 |
fi
|
| 398 |
else
|
| 399 |
ewarn "Skipping some configuration as it MUST be run on the final host"
|
| 400 |
fi
|
| 401 |
|
| 402 |
einfo "Accepting relaying by default from all ips configured on this machine."
|
| 403 |
LOCALIPS=$(/sbin/ifconfig | grep inet | cut -d' ' -f 12 -s | cut -b 6-20)
|
| 404 |
TCPSTRING=":allow,RELAYCLIENT=\"\",RBLSMTPD=\"\""
|
| 405 |
for ip in $LOCALIPS; do
|
| 406 |
myline="${ip}${TCPSTRING}"
|
| 407 |
for proto in smtp qmtp qmqp; do
|
| 408 |
f="${ROOT}${TCPRULES_DIR}/tcp.qmail-${proto}"
|
| 409 |
egrep -q "${myline}" ${f} || echo "${myline}" >>${f}
|
| 410 |
done
|
| 411 |
done
|
| 412 |
|
| 413 |
buildtcprules
|
| 414 |
|
| 415 |
if use ssl; then
|
| 416 |
ebegin "Generating RSA keys for SSL/TLS, this can take some time"
|
| 417 |
${ROOT}/etc/${CRON_FOLDER}/qmail-genrsacert.sh
|
| 418 |
eend $?
|
| 419 |
einfo "Creating a self-signed ssl-certificate:"
|
| 420 |
${ROOT}/var/qmail/bin/mkservercert
|
| 421 |
einfo "If you want to have a properly signed certificate "
|
| 422 |
einfo "instead, do the following:"
|
| 423 |
# space at the end of the string because of the current implementation
|
| 424 |
# of einfo
|
| 425 |
einfo "openssl req -new -nodes -out req.pem \\ "
|
| 426 |
einfo " -config /var/qmail/control/servercert.cnf \\ "
|
| 427 |
einfo " -keyout /var/qmail/control/servercert.pem"
|
| 428 |
einfo "Send req.pem to your CA to obtain signed_req.pem, and do:"
|
| 429 |
einfo "cat signed_req.pem >> /var/qmail/control/servercert.pem"
|
| 430 |
fi
|
| 431 |
}
|
| 432 |
|
| 433 |
# --- TODO: The following code can be moved to prime.eclass --
|
| 434 |
# Original Author: Michael Hanselmann <hansmi@gentoo.org>
|
| 435 |
# Purpose: Functions for prime numbers
|
| 436 |
|
| 437 |
# Prints a list of primes between min and max inclusive
|
| 438 |
#
|
| 439 |
# Note: this functions gets very slow when used with large numbers.
|
| 440 |
#
|
| 441 |
# Syntax: primes <min> <max>
|
| 442 |
primes() {
|
| 443 |
local min=${1} max=${2}
|
| 444 |
local result= primelist=2 i p
|
| 445 |
|
| 446 |
[[ ${min} -le 2 ]] && result="${result} 2"
|
| 447 |
|
| 448 |
for ((i = 3; i <= max; i += 2))
|
| 449 |
do
|
| 450 |
for p in ${primelist}
|
| 451 |
do
|
| 452 |
[[ $[i % p] == 0 || $[p * p] -gt ${i} ]] && \
|
| 453 |
break
|
| 454 |
done
|
| 455 |
if [[ $[i % p] != 0 ]]
|
| 456 |
then
|
| 457 |
primelist="${primelist} ${i}"
|
| 458 |
[[ ${i} -ge ${min} ]] && \
|
| 459 |
result="${result} ${i}"
|
| 460 |
fi
|
| 461 |
done
|
| 462 |
|
| 463 |
echo ${result}
|
| 464 |
}
|
| 465 |
|
| 466 |
# Checks wether a number is a prime number
|
| 467 |
#
|
| 468 |
# Syntax: is_prime <number>
|
| 469 |
is_prime() {
|
| 470 |
local number=${1} i
|
| 471 |
for i in $(primes ${number} ${number})
|
| 472 |
do
|
| 473 |
[[ ${i} == ${number} ]] && return 0
|
| 474 |
done
|
| 475 |
return 1
|
| 476 |
}
|
| 477 |
# --- end of prime.eclass ---
|