summaryrefslogtreecommitdiff
path: root/do/gsent
diff options
context:
space:
mode:
Diffstat (limited to 'do/gsent')
-rw-r--r--do/gsent77
1 files changed, 77 insertions, 0 deletions
diff --git a/do/gsent b/do/gsent
new file mode 100644
index 0000000000..ac705164b2
--- /dev/null
+++ b/do/gsent
@@ -0,0 +1,77 @@
+int
+do_gsent(which,gimme,arglast)
+int which;
+int gimme;
+int *arglast;
+{
+ register ARRAY *ary = stack;
+ register int sp = arglast[0];
+ register char **elem;
+ register STR *TARG;
+ struct servent *getservbyname();
+ struct servent *getservbynumber();
+ struct servent *getservent();
+ struct servent *sent;
+
+ if (which == O_GSBYNAME) {
+ char *name = str_get(ary->ary_array[sp+1]);
+ char *proto = str_get(ary->ary_array[sp+2]);
+
+ if (proto && !*proto)
+ proto = Nullch;
+
+ sent = getservbyname(name,proto);
+ }
+ else if (which == O_GSBYPORT) {
+ int port = (int)str_gnum(ary->ary_array[sp+1]);
+ char *proto = str_get(ary->ary_array[sp+2]);
+
+ sent = getservbyport(port,proto);
+ }
+ else
+ sent = getservent();
+
+ if (gimme != G_ARRAY) {
+ astore(ary, ++sp, TARG = str_mortal(&str_undef));
+ if (sent) {
+ if (which == O_GSBYNAME) {
+#ifdef HAS_NTOHS
+ str_numset(TARG, (double)ntohs(sent->s_port));
+#else
+ str_numset(TARG, (double)(sent->s_port));
+#endif
+ }
+ else
+ str_set(TARG, sent->s_name);
+ }
+ return sp;
+ }
+
+ if (sent) {
+#ifndef lint
+ (void)astore(ary, ++sp, TARG = str_mortal(&str_no));
+ str_set(TARG, sent->s_name);
+ (void)astore(ary, ++sp, TARG = str_mortal(&str_no));
+ for (elem = sent->s_aliases; *elem; elem++) {
+ str_cat(TARG, *elem);
+ if (elem[1])
+ str_ncat(TARG," ",1);
+ }
+ (void)astore(ary, ++sp, TARG = str_mortal(&str_no));
+#ifdef HAS_NTOHS
+ str_numset(TARG, (double)ntohs(sent->s_port));
+#else
+ str_numset(TARG, (double)(sent->s_port));
+#endif
+ (void)astore(ary, ++sp, TARG = str_mortal(&str_no));
+ str_set(TARG, sent->s_proto);
+#else /* lint */
+ elem = Nullch;
+ elem = elem;
+ (void)astore(ary, ++sp, str_mortal(&str_no));
+#endif /* lint */
+ }
+
+ return sp;
+}
+