Skip to content

Commit

Permalink
Merge pull request #90 from cs50/develop
Browse files Browse the repository at this point in the history
eliminates gnu11 dependency
  • Loading branch information
Kareem Zidane authored Jul 15, 2017
2 parents 02c16bd + 35b1504 commit 139dcde
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 70 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
build/
debian/changelog
debian/docs/
debian/files
debian/libcs50
debian/*.log
hackerrank/
libcs50-*
libcs50_*
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dist: trusty
sudo: required
branches:
except: "/^v\\d/"
before_install:
- sudo apt-get install build-essential debhelper devscripts dh-make lintian
- gem install asciidoctor
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION := 8.0.3
VERSION := 8.0.4

# soname - libcs50.so.<major_version>
SONAME := libcs50.so.$(shell echo $(VERSION) | head -c 1)
Expand All @@ -8,7 +8,7 @@ DESTDIR ?= /usr/local

.PHONY: build
build: clean
$(CC) -c -fPIC -std=gnu99 -Wall -o cs50.o src/cs50.c
$(CC) -c -fPIC -std=c99 -Wall -o cs50.o src/cs50.c
$(CC) -shared -Wl,-soname,$(SONAME) -o libcs50.so.$(VERSION) cs50.o
rm -f cs50.o
ln -s libcs50.so.$(VERSION) $(SONAME)
Expand Down Expand Up @@ -37,7 +37,7 @@ docs:
deb: build docs
@echo "libcs50 ($(VERSION)-0ubuntu1) trusty; urgency=low" > debian/changelog
@echo " * v$(VERSION)" >> debian/changelog
@echo " -- CS50 Sysadmins <[email protected]> $$(date --rfc-2822)" >> debian/changelog
@echo " -- CS50 Sysadmins <[email protected]> $$(date --rfc-2822)" >> debian/changelog
mkdir -p libcs50-$(VERSION)/usr
rsync -a build/* libcs50-$(VERSION)/usr --exclude=hack
tar -cvzf libcs50_$(VERSION).orig.tar.gz libcs50-$(VERSION)
Expand Down
5 changes: 5 additions & 0 deletions debian/libcs50.postinst.debhelper
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Automatically added by dh_makeshlibs
if [ "$1" = "configure" ]; then
ldconfig
fi
# End automatically added section
5 changes: 5 additions & 0 deletions debian/libcs50.postrm.debhelper
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Automatically added by dh_makeshlibs
if [ "$1" = "remove" ]; then
ldconfig
fi
# End automatically added section
2 changes: 2 additions & 0 deletions debian/libcs50.substvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
shlibs:Depends=libc6 (>= 2.3)
misc:Depends=
39 changes: 39 additions & 0 deletions debian/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh
# postinst script for libcs50
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
# * <postinst> `configure' <most-recently-configured-version>
# * <old-postinst> `abort-upgrade' <new version>
# * <conflictor's-postinst> `abort-remove' `in-favour' <package>
# <new-version>
# * <postinst> `abort-remove'
# * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
# <failed-install-package> <version> `removing'
# <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
configure)
;;

abort-upgrade|abort-remove|abort-deconfigure)
;;

*)
echo "postinst called with unknown argument \`$1'" >&2
exit 1
;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
37 changes: 37 additions & 0 deletions debian/postrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh
# postrm script for libcs50
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
# * <postrm> `remove'
# * <postrm> `purge'
# * <old-postrm> `upgrade' <new-version>
# * <new-postrm> `failed-upgrade' <old-version>
# * <new-postrm> `abort-install'
# * <new-postrm> `abort-install' <old-version>
# * <new-postrm> `abort-upgrade' <old-version>
# * <disappearer's-postrm> `disappear' <overwriter>
# <overwriter-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
;;

*)
echo "postrm called with unknown argument \`$1'" >&2
exit 1
;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
61 changes: 34 additions & 27 deletions src/cs50.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
*
* Based on Eric Roberts' genlib.c and simpio.c.
*
* Copyright (c) 2017,
* Glenn Holloway <[email protected]>
* David J. Malan <[email protected]>
* Copyright (c) 2017.
* All rights reserved.
*
* BSD 3-Clause License
Expand Down Expand Up @@ -52,14 +50,23 @@

#include "cs50.h"

// temporarily here for backwards compatibility
#undef get_char
#undef get_double
#undef get_float
#undef get_int
#undef get_long_long
#undef get_string
#define null (&(struct prompt) {0})

/**
* Prints an error message, formatted like printf, to standard error, prefixing it with
* file name and line number from which function was called (which a macro provides).
*
* This function is not intended to be called directly. Instead, call the macro of the same name,
* which expects fewer arguments.
*
* Inspired by http://www.gnu.org/software/libc/manual/html_node/Variable-Arguments-Output.html
* Inspired by http://www.gnu.org/software/libc/manual/html_node/Variable-Arguments-Output.html,
* http://www.gnu.org/software/libc/manual/html_node/Error-Messages.html#Error-Messages, and
* https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html.
*/
Expand Down Expand Up @@ -87,13 +94,13 @@ void eprintf(const char *file, int line, const char *format, ...)
* equivalent char; if text is not a single char, user is prompted
* to retry. If line can't be read, returns CHAR_MAX.
*/
char get_char(string prompt)
char get_char(struct prompt *p)
{
// try to get a char from user
while (true)
{
// get line of text, returning CHAR_MAX on failure
string line = get_string(prompt);
string line = get_string(p);
if (line == NULL)
{
return CHAR_MAX;
Expand All @@ -107,15 +114,15 @@ char get_char(string prompt)
}

// temporarily here for backwards compatibility
if (prompt == NULL)
if (p->prompt == NULL)
{
printf("Retry: ");
}
}
}
char GetChar(void)
{
return get_char(NULL);
return get_char(null);
}

/**
Expand All @@ -124,13 +131,13 @@ char GetChar(void)
* a double or if value would cause underflow or overflow, user is
* prompted to retry. If line can't be read, returns DBL_MAX.
*/
double get_double(string prompt)
double get_double(struct prompt *p)
{
// try to get a double from user
while (true)
{
// get line of text, returning DBL_MAX on failure
string line = get_string(prompt);
string line = get_string(p);
if (line == NULL)
{
return DBL_MAX;
Expand All @@ -153,15 +160,15 @@ double get_double(string prompt)
}

// temporarily here for backwards compatibility
if (prompt == NULL)
if (p->prompt == NULL)
{
printf("Retry: ");
}
}
}
double GetDouble(void)
{
return get_double(NULL);
return get_double(null);
}

/**
Expand All @@ -170,13 +177,13 @@ double GetDouble(void)
* a float or if value would cause underflow or overflow, user is prompted
* to retry. If line can't be read, returns FLT_MAX.
*/
float get_float(string prompt)
float get_float(struct prompt *p)
{
// try to get a float from user
while (true)
{
// get line of text, returning FLT_MAX on failure
string line = get_string(prompt);
string line = get_string(p);
if (line == NULL)
{
return FLT_MAX;
Expand All @@ -199,15 +206,15 @@ float get_float(string prompt)
}

// temporarily here for backwards compatibility
if (prompt == NULL)
if (p->prompt == NULL)
{
printf("Retry: ");
}
}
}
float GetFloat(void)
{
return get_float(NULL);
return get_float(null);
}

/**
Expand All @@ -216,13 +223,13 @@ float GetFloat(void)
* or would cause underflow or overflow, user is prompted to retry. If line
* can't be read, returns INT_MAX.
*/
int get_int(string prompt)
int get_int(struct prompt *p)
{
// try to get an int from user
while (true)
{
// get line of text, returning INT_MAX on failure
string line = get_string(prompt);
string line = get_string(p);
if (line == NULL)
{
return INT_MAX;
Expand All @@ -241,15 +248,15 @@ int get_int(string prompt)
}

// temporarily here for backwards compatibility
if (prompt == NULL)
if (p->prompt == NULL)
{
printf("Retry: ");
}
}
}
int GetInt(void)
{
return get_int(NULL);
return get_int(null);
}

/**
Expand All @@ -258,13 +265,13 @@ int GetInt(void)
* [-2^63, 2^63 - 1) or would cause underflow or overflow, user is
* prompted to retry. If line can't be read, returns LLONG_MAX.
*/
long long get_long_long(string prompt)
long long get_long_long(struct prompt *p)
{
// try to get a long long from user
while (true)
{
// get line of text, returning LLONG_MAX on failure
string line = get_string(prompt);
string line = get_string(p);
if (line == NULL)
{
return LLONG_MAX;
Expand All @@ -283,15 +290,15 @@ long long get_long_long(string prompt)
}

// temporarily here for backwards compatibility
if (prompt == NULL)
if (p->prompt == NULL)
{
printf("Retry: ");
}
}
}
long long GetLongLong(void)
{
return get_long_long(NULL);
return get_long_long(null);
}

/**
Expand All @@ -312,7 +319,7 @@ static string *strings = NULL;
* upon error or no input whatsoever (i.e., just EOF). Stores string
* on heap, but library's destructor frees memory on program's exit.
*/
string get_string(string prompt)
string get_string(struct prompt *p)
{
// check whether we have room for another string
if (allocations * sizeof(string) == SIZE_MAX)
Expand All @@ -333,9 +340,9 @@ string get_string(string prompt)
int c;

// prompt user
if (prompt != NULL)
if (p->prompt != NULL)
{
printf("%s", prompt);
printf("%s", p->prompt);
}

// iteratively get characters from standard input, checking for CR (Mac OS), LF (Linux), and CRLF (Windows)
Expand Down
Loading

0 comments on commit 139dcde

Please sign in to comment.