Introduction

The CUPS APIs provide...

General Usage

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

Compatibility

Unless otherwise specified, the CUPS API functions require CUPS 1.1 or higher.

Contents

Enumerations

cups_ptype_e

Description

Not a typedef'd enum so we can OR

Values

NameDescription
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_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_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)
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

Functions

cupsAddDest()

Description

Add a destination to the list of destinations. Use the cupsSaveDests() function to save the updated list of destinations to the user's lpoptions file.

Syntax

int
cupsAddDest(
    const char * name,
    const char * instance,
    int num_dests,
    cups_dest_t ** dests);

Arguments

NameDescription
nameName of destination
instanceInstance of destination or NULL for none/primary
num_destsNumber of destinations
destsDestinations

Returns

New number of destinations

cupsAddOption()

Description

Add an option to an option array.

Syntax

int
cupsAddOption(
    const char * name,
    const char * value,
    int num_options,
    cups_option_t ** options);

Arguments

NameDescription
nameName of option
valueValue of option
num_optionsNumber of options
optionsPointer to options

Returns

Number of options

cupsCancelJob()

Description

Cancel a print job on the default server. Use the cupsLastError() and cupsLastErrorString() functions to get the cause of any failure.

Syntax

int
cupsCancelJob(
    const char * name,
    int job);

Arguments

NameDescription
nameName of printer or class
jobJob ID

Returns

1 on success, 0 on failure

 CUPS 1.1.20 cupsDoAuthentication()

Description

Authenticate a request. This function should be called in response to a HTTP_UNAUTHORIZED status, prior to resubmitting your request.

Syntax

int
cupsDoAuthentication(
    http_t * http,
    const char * method,
    const char * resource);

Arguments

NameDescription
httpHTTP connection to server
methodRequest method (GET, POST, PUT)
resourceResource path

Returns

0 on success, -1 on error

cupsDoFileRequest()

Description

Do an IPP request. This function sends any IPP request to the specified server, retrying and authenticating as necessary.

Syntax

ipp_t *
cupsDoFileRequest(
    http_t * http,
    ipp_t * request,
    const char * resource,
    const char * filename);

Arguments

NameDescription
httpHTTP connection to server
requestIPP request
resourceHTTP resource for POST
filenameFile to send or NULL for none

Returns

Response data

cupsEncodeOptions()

Description

Encode printer options into IPP attributes. This function adds operation, job, and then subscription attributes, in that order. Use the cupsEncodeOptions2() function to add attributes for a single group.

Syntax

void
cupsEncodeOptions(
    ipp_t * ipp,
    int num_options,
    cups_option_t * options);

Arguments

NameDescription
ippRequest to add to
num_optionsNumber of options
optionsOptions

Returns

Nothing.

 CUPS 1.2 cupsEncodeOptions2()

Description

Encode printer options into IPP attributes for a group. This function only adds attributes for a single group. Call this function multiple times for each group, or use cupsEncodeOptions() to add the standard groups.

Syntax

void
cupsEncodeOptions2(
    ipp_t * ipp,
    int num_options,
    cups_option_t * options,
    ipp_tag_t group_tag);

Arguments

NameDescription
ippRequest to add to
num_optionsNumber of options
optionsOptions
group_tagGroup to encode

Returns

Nothing.

cupsEncryption()

Description

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.

Syntax

http_encryption_t
cupsEncryption(void);

Arguments

None.

Returns

Encryption settings

cupsFreeDests()

Description

Free the memory used by the list of destinations.

Syntax

void
cupsFreeDests(
    int num_dests,
    cups_dest_t * dests);

Arguments

NameDescription
num_destsNumber of destinations
destsDestinations

Returns

Nothing.

cupsFreeJobs()

Description

Free memory used by job data.

Syntax

void
cupsFreeJobs(
    int num_jobs,
    cups_job_t * jobs);

Arguments

NameDescription
num_jobsNumber of jobs
jobsJobs

Returns

Nothing.

cupsFreeOptions()

Description

Free all memory used by options.

Syntax

void
cupsFreeOptions(
    int num_options,
    cups_option_t * options);

Arguments

NameDescription
num_optionsNumber of options
optionsPointer to options

Returns

Nothing.

 DEPRECATED cupsGetClasses()

Description

Get a list of printer classes from the default server. This function is deprecated - use cupsGetDests() instead.

Syntax

int
cupsGetClasses(
    char *** classes);

Arguments

NameDescription
classesClasses

Returns

Number of classes

cupsGetDefault()

Description

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.

Syntax

const char *
cupsGetDefault(void);

Arguments

None.

Returns

Default printer or NULL

 CUPS 1.1.21 cupsGetDefault2()

Description

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.

Syntax

const char *
cupsGetDefault2(
    http_t * http);

Arguments

NameDescription
httpHTTP connection

Returns

Default printer or NULL

cupsGetDest()

Description

Get the named destination from the list. Use the cupsGetDests() or cupsGetDests2() functions to get a list of supported destinations for the current user.

Syntax

cups_dest_t *
cupsGetDest(
    const char * name,
    const char * instance,
    int num_dests,
    cups_dest_t * dests);

Arguments

NameDescription
nameName of destination
instanceInstance of destination
num_destsNumber of destinations
destsDestinations

Returns

Destination pointer or NULL

cupsGetDests()

Description

Get the list of destinations from the default server.

Syntax

int
cupsGetDests(
    cups_dest_t ** dests);

Arguments

NameDescription
destsDestinations

Returns

Number of destinations

 CUPS 1.1.21 cupsGetDests2()

Description

Get the list of destinations from the specified server.

Syntax

int
cupsGetDests2(
    http_t * http,
    cups_dest_t ** dests);

Arguments

NameDescription
httpHTTP connection
destsDestinations

Returns

Number of destinations

 CUPS 1.1.20 cupsGetFd()

Description

Get a file from the server. This function returns HTTP_OK when the file is successfully retrieved.

Syntax

http_status_t
cupsGetFd(
    http_t * http,
    const char * resource,
    int fd);

Arguments

NameDescription
httpHTTP connection to server
resourceResource name
fdFile descriptor

Returns

Status

 CUPS 1.1.20 cupsGetFile()

Description

Get a file from the server. This function returns HTTP_OK when the file is successfully retrieved.

Syntax

http_status_t
cupsGetFile(
    http_t * http,
    const char * resource,
    const char * filename);

Arguments

NameDescription
httpHTTP connection to server
resourceResource name
filenameFilename

Returns

Status

cupsGetJobs()

Description

Get the jobs from the default server.

Syntax

int
cupsGetJobs(
    cups_job_t ** jobs,
    const char * mydest,
    int myjobs,
    int completed);

Arguments

NameDescription
jobsJob data
mydestOnly show jobs for dest?
myjobsOnly show my jobs?
completedOnly show completed jobs?

Returns

Number of jobs

 CUPS 1.1.21 cupsGetJobs2()

Description

Get the jobs from the specified server.

Syntax

int
cupsGetJobs2(
    http_t * http,
    cups_job_t ** jobs,
    const char * mydest,
    int myjobs,
    int completed);

Arguments

NameDescription
httpHTTP connection
jobsJob data
mydestOnly show jobs for dest?
myjobsOnly show my jobs?
completedOnly show completed jobs?

Returns

Number of jobs

cupsGetOption()

Description

Get an option value.

Syntax

const char *
cupsGetOption(
    const char * name,
    int num_options,
    cups_option_t * options);

Arguments

NameDescription
nameName of option
num_optionsNumber of options
optionsOptions

Returns

Option value or NULL

cupsGetPPD()

Description

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.

Syntax

const char *
cupsGetPPD(
    const char * name);

Arguments

NameDescription
namePrinter name

Returns

Filename for PPD file

 CUPS 1.1.21 cupsGetPPD2()

Description

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.

Syntax

const char *
cupsGetPPD2(
    http_t * http,
    const char * name);

Arguments

NameDescription
httpHTTP connection
namePrinter name

Returns

Filename for PPD file

cupsGetPassword()

Description

Get a password from the user. Returns NULL if the user does not provide a password.

Syntax

const char *
cupsGetPassword(
    const char * prompt);

Arguments

NameDescription
promptPrompt string

Returns

Password

 DEPRECATED cupsGetPrinters()

Description

Get a list of printers from the default server. This function is deprecated - use cupsGetDests() instead.

Syntax

int
cupsGetPrinters(
    char *** printers);

Arguments

NameDescription
printersPrinters

Returns

Number of printers

cupsLangDefault()

Description

Return the default language.

Syntax

cups_lang_t *
cupsLangDefault(void);

Arguments

None.

Returns

Language data

cupsLangEncoding()

Description

Return the character encoding (us-ascii, etc.) for the given language.

Syntax

const char *
cupsLangEncoding(
    cups_lang_t * lang);

Arguments

NameDescription
langLanguage data

Returns

Character encoding

cupsLangFlush()

Description

Flush all language data out of the cache.

Syntax

void
cupsLangFlush(void);

Arguments

None.

Returns

Nothing.

cupsLangFree()

Description

Free language data. This does not actually free anything; use cupsLangFlush() for that.

Syntax

void
cupsLangFree(
    cups_lang_t * lang);

Arguments

NameDescription
langLanguage to free

Returns

Nothing.

cupsLangGet()

Description

Get a language.

Syntax

cups_lang_t *
cupsLangGet(
    const char * language);

Arguments

NameDescription
languageLanguage or locale

Returns

Language data

cupsLastError()

Description

Return the last IPP status code.

Syntax

ipp_status_t
cupsLastError(void);

Arguments

None.

Returns

IPP status code from last request

 CUPS 1.2 cupsLastErrorString()

Description

Return the last IPP status-message.

Syntax

const char *
cupsLastErrorString(void);

Arguments

None.

Returns

status-message text from last request

cupsMarkOptions()

Description

Mark command-line options in a PPD file.

Syntax

int
cupsMarkOptions(
    ppd_file_t * ppd,
    int num_options,
    cups_option_t * options);

Arguments

NameDescription
ppdPPD file
num_optionsNumber of options
optionsOptions

Returns

1 if conflicting

cupsParseOptions()

Description

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.

Syntax

int
cupsParseOptions(
    const char * arg,
    int num_options,
    cups_option_t ** options);

Arguments

NameDescription
argArgument to parse
num_optionsNumber of options
optionsOptions found

Returns

Number of options found

cupsPrintFile()

Description

Print a file to a printer or class on the default server.

Syntax

int
cupsPrintFile(
    const char * name,
    const char * filename,
    const char * title,
    int num_options,
    cups_option_t * options);

Arguments

NameDescription
namePrinter or class name
filenameFile to print
titleTitle of job
num_optionsNumber of options
optionsOptions

Returns

Job ID

 CUPS 1.1.21 cupsPrintFile2()

Description

Print a file to a printer or class on the specified server.

Syntax

int
cupsPrintFile2(
    http_t * http,
    const char * name,
    const char * filename,
    const char * title,
    int num_options,
    cups_option_t * options);

Arguments

NameDescription
httpHTTP connection
namePrinter or class name
filenameFile to print
titleTitle of job
num_optionsNumber of options
optionsOptions

Returns

Job ID

cupsPrintFiles()

Description

Print one or more files to a printer or class on the default server.

Syntax

int
cupsPrintFiles(
    const char * name,
    int num_files,
    const char ** files,
    const char * title,
    int num_options,
    cups_option_t * options);

Arguments

NameDescription
namePrinter or class name
num_filesNumber of files
filesFile(s) to print
titleTitle of job
num_optionsNumber of options
optionsOptions

Returns

Job ID

 CUPS 1.1.21 cupsPrintFiles2()

Description

Print one or more files to a printer or class on the specified server.

Syntax

int
cupsPrintFiles2(
    http_t * http,
    const char * name,
    int num_files,
    const char ** files,
    const char * title,
    int num_options,
    cups_option_t * options);

Arguments

NameDescription
httpHTTP connection
namePrinter or class name
num_filesNumber of files
filesFile(s) to print
titleTitle of job
num_optionsNumber of options
optionsOptions

Returns

Job ID

 CUPS 1.1.20 cupsPutFd()

Description

Put a file on the server. This function returns HTTP_CREATED when the file is stored successfully.

Syntax

http_status_t
cupsPutFd(
    http_t * http,
    const char * resource,
    int fd);

Arguments

NameDescription
httpHTTP connection to server
resourceResource name
fdFile descriptor

Returns

Status

 CUPS 1.1.20 cupsPutFile()

Description

Put a file on the server. This function returns HTTP_CREATED when the file is stored successfully.

Syntax

http_status_t
cupsPutFile(
    http_t * http,
    const char * resource,
    const char * filename);

Arguments

NameDescription
httpHTTP connection to server
resourceResource name
filenameFilename

Returns

Status

cupsServer()

Description

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.

Syntax

const char *
cupsServer(void);

Arguments

None.

Returns

Server name

cupsSetDests()

Description

Save the list of destinations for the default server. This function saves the destinations to /etc/cups/lpoptions when run as root and ~/.lpoptions when run as a normal user.

Syntax

void
cupsSetDests(
    int num_dests,
    cups_dest_t * dests);

Arguments

NameDescription
num_destsNumber of destinations
destsDestinations

Returns

Nothing.

 CUPS 1.1.21 cupsSetDests2()

Description

Save the list of destinations for the specified server. This function saves the destinations to /etc/cups/lpoptions when run as root and ~/.lpoptions when run as a normal user.

Syntax

int
cupsSetDests2(
    http_t * http,
    int num_dests,
    cups_dest_t * dests);

Arguments

NameDescription
httpHTTP connection
num_destsNumber of destinations
destsDestinations

Returns

0 on success, -1 on error

cupsSetEncryption()

Description

Set the encryption preference.

Syntax

void
cupsSetEncryption(
    http_encryption_t e);

Arguments

NameDescription
eNew encryption preference

Returns

Nothing.

cupsSetPasswordCB()

Description

Set the password callback for CUPS. Pass NULL to restore the default (console) password callback.

Syntax

void
cupsSetPasswordCB(
    cups_password_cb_t cb);

Arguments

NameDescription
cbCallback function

Returns

Nothing.

cupsSetServer()

Description

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.

Syntax

void
cupsSetServer(
    const char * server);

Arguments

NameDescription
serverServer name

Returns

Nothing.

cupsSetUser()

Description

Set the default user name. Pass NULL to restore the default user name.

Syntax

void
cupsSetUser(
    const char * user);

Arguments

NameDescription
userUser name

Returns

Nothing.

cupsTempFd()

Description

Create a temporary file. The temporary filename is stored in the filename buffer.

Syntax

int
cupsTempFd(
    char * filename,
    int len);

Arguments

NameDescription
filenamePointer to buffer
lenSize of buffer

Returns

New file descriptor

 DEPRECATED cupsTempFile()

Description

Generate a temporary filename. The temporary filename is stored in the filename buffer. This function is deprecated - use cupsTempFd() or cupsTempFile2() instead.

Syntax

char *
cupsTempFile(
    char * filename,
    int len);

Arguments

NameDescription
filenamePointer to buffer
lenSize of buffer

Returns

Filename

 CUPS 1.2 cupsTempFile2()

Description

Create a temporary CUPS file. The temporary filename is stored in the filename buffer.

Syntax

cups_file_t *
cupsTempFile2(
    char * filename,
    int len);

Arguments

NameDescription
filenamePointer to buffer
lenSize of buffer

Returns

CUPS file or NULL on error

cupsUser()

Description

Return the current user's name.

Syntax

const char *
cupsUser(void);

Arguments

None.

Returns

User name

Structures

cups_dest_s

Description

Destination

Definition

struct cups_dest_s
{
  char *name, * instance;
  int is_default;
  int num_options;
  cups_option_t * options;
};

Members

NameDescription
instance Local instance name or NULL
is_default Is this printer the default?
num_options Number of options
options Options

cups_job_s

Description

Job

Definition

struct cups_job_s
{
  char *dest, *title, *user, * format;
  int id;
  int size, priority;
  time_t completed_time, creation_time, processing_time;
  ipp_jstate_t state;
};

Members

NameDescription
format Document format
id The job ID
priority Priority (1-100)
processing_time Time the job was processed
state Job state

cups_option_s

Description

Types and structures...

Definition

struct cups_option_s
{
  char * name;
  char * value;
};

Members

NameDescription
name Name of option
value Value of option

Types

char

Description

Printer Options

Definition

typedef const * (*charcups_password_cb_t)(const char *);

cups_dest_t

Description

Destination

Definition

typedef struct cups_dest_s cups_dest_t;

cups_job_t

Description

Job

Definition

typedef struct cups_job_s cups_job_t;

cups_option_t

Description

Types and structures...

Definition

typedef struct cups_option_s cups_option_t;

cups_ptype_t

Description

Printer Type/Capability Bits

Definition

typedef unsigned cups_ptype_t;