The CUPS library provides a whole collection of interfaces needed to support the internal needs of the CUPS software as well as the needs of applications, filters, printer drivers, and backends.
Unlike the rest of CUPS, the CUPS API library is provided under the GNU Library General Public License. This means that you can use the CUPS API library in both proprietary and open-source programs.
The <cups/cups.h> header file must be included to use the CUPS functions.
Programs using these functions must be linked to the CUPS library: libcups.a, libcups.so.2, libcups.2.dylib, libcups_s.a, or libcups2.lib depending on the platform. The following command compiles myprogram.c using GCC and the CUPS library:
gcc -o myprogram myprogram.c -lcups
Unless otherwise specified, the CUPS API functions require CUPS 1.1 or higher.
Not a typedef'd enum so we can OR
Name | Description |
---|---|
CUPS_PRINTER_AUTHENTICATED CUPS 1.2 | Printer requires authentication |
CUPS_PRINTER_BIND | Can bind output |
CUPS_PRINTER_BW | Can do B&W printing |
CUPS_PRINTER_CLASS | Printer class |
CUPS_PRINTER_COLLATE | Can collage copies |
CUPS_PRINTER_COLOR | Can do color printing |
CUPS_PRINTER_COMMANDS CUPS 1.2 | Printer supports maintenance commands |
CUPS_PRINTER_COPIES | Can do copies |
CUPS_PRINTER_COVER | Can cover output |
CUPS_PRINTER_DEFAULT | Default printer on network |
CUPS_PRINTER_DELETE CUPS 1.2 | Delete printer |
CUPS_PRINTER_DISCOVERED CUPS 1.3 | Printer was automatically discovered and added |
CUPS_PRINTER_DUPLEX | Can do duplexing |
CUPS_PRINTER_FAX | Fax queue |
CUPS_PRINTER_IMPLICIT | Implicit class |
CUPS_PRINTER_LARGE | Can do D/E/A1/A0 |
CUPS_PRINTER_LOCAL | Local printer or class |
CUPS_PRINTER_MEDIUM | Can do Tabloid/B/C/A3/A2 |
CUPS_PRINTER_NOT_SHARED CUPS 1.2 | Printer is not shared |
CUPS_PRINTER_OPTIONS | ~(CLASS | REMOTE | IMPLICIT | DEFAULT | FAX | REJECTING | DELETE | NOT_SHARED | AUTHENTICATED | COMMANDS | DISCOVERED) |
CUPS_PRINTER_PUNCH | Can punch output |
CUPS_PRINTER_REJECTING | Printer is rejecting jobs |
CUPS_PRINTER_REMOTE | Remote printer or class |
CUPS_PRINTER_SMALL | Can do Letter/Legal/A4 |
CUPS_PRINTER_SORT | Can sort output |
CUPS_PRINTER_STAPLE | Can staple output |
CUPS_PRINTER_VARIABLE | Can do variable sizes |
Add a destination to the list of destinations.
This function cannot be used to add a new class or printer queue, it only adds a new container of saved options for the named destination or instance.
If the named destination already exists, the destination list is returned unchanged. Adding a new instance of a destination creates a copy of that destination's options.
Use the cupsSaveDests() function to save the updated list of destinations to the user's lpoptions file.
int
cupsAddDest(
const char * name,
const char * instance,
int num_dests,
cups_dest_t ** dests);
Name | Description |
---|---|
name | Destination name |
instance | Instance name or NULL for none/primary |
num_dests | Number of destinations |
dests | Destinations |
New number of destinations
Add an option to an option array.
int
cupsAddOption(
const char * name,
const char * value,
int num_options,
cups_option_t ** options);
Name | Description |
---|---|
name | Name of option |
value | Value of option |
num_options | Number of options |
options | Pointer to options |
Number of options
Cancel a print job on the default server.
Use the cupsLastError() and cupsLastErrorString() functions to get the cause of any failure.
int
cupsCancelJob(
const char * name,
int job);
Name | Description |
---|---|
name | Name of printer or class |
job | Job ID |
1 on success, 0 on failure
Get the default encryption settings.
The default encryption setting comes from the CUPS_ENCRYPTION environment variable, then the ~/.cupsrc file, and finally the /etc/cups/client.conf file. If not set, the default is HTTP_ENCRYPT_IF_REQUESTED.
http_encryption_t
cupsEncryption(void);
None.
Encryption settings
Free the memory used by the list of destinations.
void
cupsFreeDests(
int num_dests,
cups_dest_t * dests);
Name | Description |
---|---|
num_dests | Number of destinations |
dests | Destinations |
Nothing.
Free memory used by job data.
void
cupsFreeJobs(
int num_jobs,
cups_job_t * jobs);
Name | Description |
---|---|
num_jobs | Number of jobs |
jobs | Jobs |
Nothing.
Free all memory used by options.
void
cupsFreeOptions(
int num_options,
cups_option_t * options);
Name | Description |
---|---|
num_options | Number of options |
options | Pointer to options |
Nothing.
Get a list of printer classes from the default server.
This function is deprecated - use cupsGetDests() instead.
int
cupsGetClasses(
char *** classes);
Name | Description |
---|---|
classes | Classes |
Number of classes
Get the default printer or class for the default server.
This function returns the default printer or class as defined by the LPDEST or PRINTER environment variables. If these environment variables are not set, the server default destination is returned. Applications should use the cupsGetDests() and cupsGetDest() functions to get the user-defined default printer, as this function does not support the lpoptions-defined default printer.
const char *
cupsGetDefault(void);
None.
Default printer or NULL
Get the default printer or class for the specified server.
This function returns the default printer or class as defined by the LPDEST or PRINTER environment variables. If these environment variables are not set, the server default destination is returned. Applications should use the cupsGetDests() and cupsGetDest() functions to get the user-defined default printer, as this function does not support the lpoptions-defined default printer.
const char *
cupsGetDefault2(
http_t * http);
Name | Description |
---|---|
http | HTTP connection |
Default printer or NULL
Get the named destination from the list.
Use the cupsGetDests() or cupsGetDests2() functions to get a list of supported destinations for the current user.
cups_dest_t *
cupsGetDest(
const char * name,
const char * instance,
int num_dests,
cups_dest_t * dests);
Name | Description |
---|---|
name | Destination name or NULL for the default destination |
instance | Instance name or NULL |
num_dests | Number of destinations |
dests | Destinations |
Destination pointer or NULL
Get the list of destinations from the default server.
Starting with CUPS 1.2, the returned list of destinations include the printer-info, printer-is-accepting-jobs, printer-is-shared, printer-make-and-model, printer-state, printer-state-change-time, printer-state-reasons, and printer-type attributes as options.
Use the cupsFreeDests() function to free the destination list and the cupsGetDest() function to find a particular destination.
int
cupsGetDests(
cups_dest_t ** dests);
Name | Description |
---|---|
dests | Destinations |
Number of destinations
Get the list of destinations from the specified server.
Starting with CUPS 1.2, the returned list of destinations include the printer-info, printer-is-accepting-jobs, printer-is-shared, printer-make-and-model, printer-state, printer-state-change-time, printer-state-reasons, and printer-type attributes as options.
Use the cupsFreeDests() function to free the destination list and the cupsGetDest() function to find a particular destination.
int
cupsGetDests2(
http_t * http,
cups_dest_t ** dests);
Name | Description |
---|---|
http | HTTP connection |
dests | Destinations |
Number of destinations
Get a file from the server.
This function returns HTTP_OK when the file is successfully retrieved.
http_status_t
cupsGetFd(
http_t * http,
const char * resource,
int fd);
Name | Description |
---|---|
http | HTTP connection to server |
resource | Resource name |
fd | File descriptor |
HTTP status
Get a file from the server.
This function returns HTTP_OK when the file is successfully retrieved.
http_status_t
cupsGetFile(
http_t * http,
const char * resource,
const char * filename);
Name | Description |
---|---|
http | HTTP connection to server |
resource | Resource name |
filename | Filename |
HTTP status
Get the jobs from the default server.
int
cupsGetJobs(
cups_job_t ** jobs,
const char * mydest,
int myjobs,
int completed);
Name | Description |
---|---|
jobs | Job data |
mydest | NULL = all destinations, * otherwise show jobs for mydest |
myjobs | 0 = all users, 1 = mine |
completed | -1 = show all, 0 = active, * 1 = completed jobs |
Number of jobs
Get the jobs from the specified server.
int
cupsGetJobs2(
http_t * http,
cups_job_t ** jobs,
const char * mydest,
int myjobs,
int completed);
Name | Description |
---|---|
http | HTTP connection |
jobs | Job data |
mydest | NULL = all destinations, * otherwise show jobs for mydest |
myjobs | 0 = all users, 1 = mine |
completed | -1 = show all, 0 = active, * 1 = completed jobs |
Number of jobs
Get an option value.
const char *
cupsGetOption(
const char * name,
int num_options,
cups_option_t * options);
Name | Description |
---|---|
name | Name of option |
num_options | Number of options |
options | Options |
Option value or NULL
Get the PPD file for a printer on the default server.
For classes, cupsGetPPD() returns the PPD file for the first printer in the class.
const char *
cupsGetPPD(
const char * name);
Name | Description |
---|---|
name | Printer name |
Filename for PPD file
Get the PPD file for a printer from the specified server.
For classes, cupsGetPPD2() returns the PPD file for the first printer in the class.
const char *
cupsGetPPD2(
http_t * http,
const char * name);
Name | Description |
---|---|
http | HTTP connection |
name | Printer name |
Filename for PPD file
Get a password from the user.
Uses the current password callback function. Returns NULL if the user does not provide a password.
const char *
cupsGetPassword(
const char * prompt);
Name | Description |
---|---|
prompt | Prompt string |
Password
Get a list of printers from the default server.
This function is deprecated - use cupsGetDests() instead.
int
cupsGetPrinters(
char *** printers);
Name | Description |
---|---|
printers | Printers |
Number of printers
Get an available PPD file from the server.
This function returns the named PPD file from the server. The list of available PPDs is provided by the IPP CUPS_GET_PPDS operation.
You must remove (unlink) the PPD file when you are finished with it. The PPD filename is stored in a static location that will be overwritten on the next call to cupsGetPPD(), cupsGetPPD2(), or cupsGetServerPPD().
char *
cupsGetServerPPD(
http_t * http,
const char * name);
Name | Description |
---|---|
http | HTTP connection |
name | Name of PPD file ("ppd-name") |
Name of PPD file or NULL on error
Return the default language.
cups_lang_t *
cupsLangDefault(void);
None.
Language data
Return the character encoding (us-ascii, etc.) for the given language.
const char *
cupsLangEncoding(
cups_lang_t * lang);
Name | Description |
---|---|
lang | Language data |
Character encoding
Flush all language data out of the cache.
void
cupsLangFlush(void);
None.
Nothing.
Free language data.
This does not actually free anything; use cupsLangFlush() for that.
void
cupsLangFree(
cups_lang_t * lang);
Name | Description |
---|---|
lang | Language to free |
Nothing.
Get a language.
cups_lang_t *
cupsLangGet(
const char * language);
Name | Description |
---|---|
language | Language or locale |
Language data
Return the last IPP status code.
ipp_status_t
cupsLastError(void);
None.
IPP status code from last request
Return the last IPP status-message.
const char *
cupsLastErrorString(void);
None.
status-message text from last request
Mark command-line options in a PPD file.
int
cupsMarkOptions(
ppd_file_t * ppd,
int num_options,
cups_option_t * options);
Name | Description |
---|---|
ppd | PPD file |
num_options | Number of options |
options | Options |
1 if conflicting
Return the subject for the given notification message.
The returned string must be freed by the caller using free().
char *
cupsNotifySubject(
cups_lang_t * lang,
ipp_t * event);
Name | Description |
---|---|
lang | Language data |
event | Event data |
Subject string or NULL
Return the text for the given notification message.
The returned string must be freed by the caller using free().
char *
cupsNotifyText(
cups_lang_t * lang,
ipp_t * event);
Name | Description |
---|---|
lang | Language data |
event | Event data |
Message text or NULL
Parse options from a command-line argument.
This function converts space-delimited name/value pairs according to the PAPI text option ABNF specification. Collection values ("name={a=... b=... c=...}") are stored with the curley brackets intact - use cupsParseOptions() on the value to extract the collection attributes.
int
cupsParseOptions(
const char * arg,
int num_options,
cups_option_t ** options);
Name | Description |
---|---|
arg | Argument to parse |
num_options | Number of options |
options | Options found |
Number of options found
Print a file to a printer or class on the default server.
int
cupsPrintFile(
const char * name,
const char * filename,
const char * title,
int num_options,
cups_option_t * options);
Name | Description |
---|---|
name | Printer or class name |
filename | File to print |
title | Title of job |
num_options | Number of options |
options | Options |
Job ID
Print a file to a printer or class on the specified server.
int
cupsPrintFile2(
http_t * http,
const char * name,
const char * filename,
const char * title,
int num_options,
cups_option_t * options);
Name | Description |
---|---|
http | HTTP connection |
name | Printer or class name |
filename | File to print |
title | Title of job |
num_options | Number of options |
options | Options |
Job ID
Print one or more files to a printer or class on the default server.
int
cupsPrintFiles(
const char * name,
int num_files,
const char ** files,
const char * title,
int num_options,
cups_option_t * options);
Name | Description |
---|---|
name | Printer or class name |
num_files | Number of files |
files | File(s) to print |
title | Title of job |
num_options | Number of options |
options | Options |
Job ID
Print one or more files to a printer or class on the specified server.
int
cupsPrintFiles2(
http_t * http,
const char * name,
int num_files,
const char ** files,
const char * title,
int num_options,
cups_option_t * options);
Name | Description |
---|---|
http | HTTP connection |
name | Printer or class name |
num_files | Number of files |
files | File(s) to print |
title | Title of job |
num_options | Number of options |
options | Options |
Job ID
Put a file on the server.
This function returns HTTP_CREATED when the file is stored successfully.
http_status_t
cupsPutFd(
http_t * http,
const char * resource,
int fd);
Name | Description |
---|---|
http | HTTP connection to server |
resource | Resource name |
fd | File descriptor |
HTTP status
Put a file on the server.
This function returns HTTP_CREATED when the file is stored successfully.
http_status_t
cupsPutFile(
http_t * http,
const char * resource,
const char * filename);
Name | Description |
---|---|
http | HTTP connection to server |
resource | Resource name |
filename | Filename |
HTTP status
Remove a destination from the destination list.
Removing a destination/instance does not delete the class or printer queue, merely the lpoptions for that destination/instance. Use the cupsSetDests() or cupsSetDests2() functions to save the new options for the user.
int
cupsRemoveDest(
const char * name,
const char * instance,
int num_dests,
cups_dest_t ** dests);
Name | Description |
---|---|
name | Destination name |
instance | Instance name or NULL |
num_dests | Number of destinations |
dests | Destinations |
New number of destinations
Remove an option from an option array.
int
cupsRemoveOption(
const char * name,
int num_options,
cups_option_t ** options);
Name | Description |
---|---|
name | Option name |
num_options | Current number of options |
options | Options |
New number of options
Return the hostname/address of the default server.
The returned value can be a fully-qualified hostname, a numeric IPv4 or IPv6 address, or a domain socket pathname.
const char *
cupsServer(void);
None.
Server name
Set the default destination.
void
cupsSetDefaultDest(
const char * name,
const char * instance,
int num_dests,
cups_dest_t * dests);
Name | Description |
---|---|
name | Destination name |
instance | Instance name or NULL |
num_dests | Number of destinations |
dests | Destinations |
Nothing.
Save the list of destinations for the default server.
This function saves the destinations to /etc/cups/lpoptions when run as root and ~/.cups/lpoptions when run as a normal user.
void
cupsSetDests(
int num_dests,
cups_dest_t * dests);
Name | Description |
---|---|
num_dests | Number of destinations |
dests | Destinations |
Nothing.
Save the list of destinations for the specified server.
This function saves the destinations to /etc/cups/lpoptions when run as root and ~/.cups/lpoptions when run as a normal user.
int
cupsSetDests2(
http_t * http,
int num_dests,
cups_dest_t * dests);
Name | Description |
---|---|
http | HTTP connection |
num_dests | Number of destinations |
dests | Destinations |
0 on success, -1 on error
Set the encryption preference.
void
cupsSetEncryption(
http_encryption_t e);
Name | Description |
---|---|
e | New encryption preference |
Nothing.
Set the password callback for CUPS.
Pass NULL to restore the default (console) password callback.
void
cupsSetPasswordCB(
cups_password_cb_t cb);
Name | Description |
---|---|
cb | Callback function |
Nothing.
Set the default server name.
The "server" string can be a fully-qualified hostname, a numeric IPv4 or IPv6 address, or a domain socket pathname. Pass NULL to restore the default server name.
void
cupsSetServer(
const char * server);
Name | Description |
---|---|
server | Server name |
Nothing.
Set the default user name.
Pass NULL to restore the default user name.
void
cupsSetUser(
const char * user);
Name | Description |
---|---|
user | User name |
Nothing.
Creates a temporary file.
The temporary filename is returned in the filename buffer. The temporary file is opened for reading and writing.
int
cupsTempFd(
char * filename,
int len);
Name | Description |
---|---|
filename | Pointer to buffer |
len | Size of buffer |
New file descriptor or -1 on error
Generates a temporary filename.
The temporary filename is returned in the filename buffer. This function is deprecated - use cupsTempFd() or cupsTempFile2() instead.
char *
cupsTempFile(
char * filename,
int len);
Name | Description |
---|---|
filename | Pointer to buffer |
len | Size of buffer |
Filename or NULL on error
Creates a temporary CUPS file.
The temporary filename is returned in the filename buffer. The temporary file is opened for writing.
cups_file_t *
cupsTempFile2(
char * filename,
int len);
Name | Description |
---|---|
filename | Pointer to buffer |
len | Size of buffer |
CUPS file or NULL on error
Return the current user's name.
const char *
cupsUser(void);
None.
User name
Destination
struct cups_dest_s
{
char *name, * instance;
int is_default;
int num_options;
cups_option_t * options;
};
Name | Description |
---|---|
instance | Local instance name or NULL |
is_default | Is this printer the default? |
num_options | Number of options |
options | Options |
Job
struct cups_job_s
{
time_t completed_time;
time_t creation_time;
char * dest;
char * format;
int id;
int priority;
time_t processing_time;
int size;
ipp_jstate_t state;
char * title;
char * user;
};
Name | Description |
---|---|
completed_time | Time the job was completed |
creation_time | Time the job was created |
dest | Printer or class name |
format | Document format |
id | The job ID |
priority | Priority (1-100) |
processing_time | Time the job was processed |
size | Size in kilobytes |
state | Job state |
title | Title/job name |
user | User the submitted the job |
Printer Options
struct cups_option_s
{
char * name;
char * value;
};
Name | Description |
---|---|
name | Name of option |
value | Value of option |
Destination
typedef struct cups_dest_s cups_dest_t;
Job
typedef struct cups_job_s cups_job_t;
Printer Options
typedef struct cups_option_s cups_option_t;
Password callback
typedef const char * (*cups_password_cb_t)(const char *);
Printer Type/Capability Bits
typedef unsigned cups_ptype_t;